learnpath.class.php 454 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
  5. * learnpaths. It is used by the scorm class.
  6. *
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  10. * @todo this file is too big, need a refactor more than 10000 lines!
  11. */
  12. /**
  13. * Defines the learnpath parent class
  14. * @package chamilo.learnpath
  15. */
  16. use \ChamiloSession as Session;
  17. class learnpath
  18. {
  19. public $attempt = 0; // The number for the current ID view.
  20. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  21. public $current; // Id of the current item the user is viewing.
  22. public $current_score; // The score of the current item.
  23. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  24. public $current_time_stop; // The time the user closed this resource.
  25. public $default_status = 'not attempted';
  26. public $encoding = 'UTF-8';
  27. public $error = '';
  28. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  29. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  30. public $index; // The index of the active learnpath_item in $ordered_items array.
  31. public $items = array();
  32. public $last; // item_id of last item viewed in the learning path.
  33. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  34. public $license; // Which license this course has been given - not used yet on 20060522.
  35. public $lp_id; // DB ID for this learnpath.
  36. public $lp_view_id; // DB ID for lp_view
  37. public $log_file; // File where to log learnpath API msg.
  38. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  39. public $message = '';
  40. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  41. public $name; // Learnpath name (they generally have one).
  42. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  43. public $path = ''; // Path inside the scorm directory (if scorm).
  44. public $theme; // The current theme of the learning path.
  45. public $preview_image; // The current image of the learning path.
  46. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  47. public $prevent_reinit = 1;
  48. // Describes the mode of progress bar display.
  49. public $seriousgame_mode = 0;
  50. public $progress_bar_mode = '%';
  51. // Percentage progress as saved in the db.
  52. public $progress_db = '0';
  53. public $proximity; // Wether the content is distant or local or unknown.
  54. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  55. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  56. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  57. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  58. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  59. public $user_id; //ID of the user that is viewing/using the course
  60. public $update_queue = array();
  61. public $scorm_debug = 0;
  62. public $arrMenu = array(); // Array for the menu items.
  63. public $debug = 0; // Logging level.
  64. public $lp_session_id = 0;
  65. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  66. public $prerequisite = 0;
  67. public $use_max_score = 1; // 1 or 0
  68. public $subscribe_users = 0;
  69. public $created_on = '';
  70. public $modified_on = '';
  71. public $publicated_on = '';
  72. public $expired_on = '';
  73. public $ref = null;
  74. public $course_int_id;
  75. public $course_info = array();
  76. public function get_course_int_id()
  77. {
  78. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  79. }
  80. public function set_course_int_id($course_id)
  81. {
  82. return $this->course_int_id = intval($course_id);
  83. }
  84. /**
  85. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  86. * Also builds the list of items into $this->items.
  87. * @param string Course code
  88. * @param integer Learnpath ID
  89. * @param integer User ID
  90. * @return boolean True on success, false on error
  91. */
  92. public function __construct($course, $lp_id, $user_id)
  93. {
  94. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  95. // Check course code.
  96. $course_id = api_get_course_int_id();
  97. if ($this->debug > 0) {
  98. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  99. }
  100. if (empty($course)) {
  101. $this->error = 'Course code is empty';
  102. return false;
  103. } else {
  104. $course_info = api_get_course_info($course);
  105. if (!empty($course_info)) {
  106. $this->cc = $course_info['code'];
  107. $this->course_info = $course_info;
  108. $course_id = $course_info['real_id'];
  109. } else {
  110. $this->error = 'Course code does not exist in database';
  111. return false;
  112. }
  113. }
  114. $this->set_course_int_id($course_id);
  115. // Check learnpath ID.
  116. if (empty($lp_id)) {
  117. $this->error = 'Learnpath ID is empty';
  118. return false;
  119. } else {
  120. // TODO: Make it flexible to use any course_code (still using env course code here).
  121. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  122. $lp_id = intval($lp_id);
  123. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  124. if ($this->debug > 2) {
  125. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  126. }
  127. $res = Database::query($sql);
  128. if (Database::num_rows($res) > 0) {
  129. $this->lp_id = $lp_id;
  130. $row = Database::fetch_array($res);
  131. $this->type = $row['lp_type'];
  132. $this->name = stripslashes($row['name']);
  133. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  134. $this->proximity = $row['content_local'];
  135. $this->theme = $row['theme'];
  136. $this->maker = $row['content_maker'];
  137. $this->prevent_reinit = $row['prevent_reinit'];
  138. $this->seriousgame_mode = $row['seriousgame_mode'];
  139. $this->license = $row['content_license'];
  140. $this->scorm_debug = $row['debug'];
  141. $this->js_lib = $row['js_lib'];
  142. $this->path = $row['path'];
  143. $this->preview_image = $row['preview_image'];
  144. $this->author = $row['author'];
  145. $this->hide_toc_frame = $row['hide_toc_frame'];
  146. $this->lp_session_id = $row['session_id'];
  147. $this->use_max_score = $row['use_max_score'];
  148. $this->subscribe_users = $row['subscribe_users'];
  149. $this->created_on = $row['created_on'];
  150. $this->modified_on = $row['modified_on'];
  151. $this->ref = $row['ref'];
  152. $this->category_id = $row['category_id'];
  153. $this->max_attempts = $row['max_attempts'];
  154. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  155. $this->publicated_on = $row['publicated_on'];
  156. }
  157. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  158. $this->expired_on = $row['expired_on'];
  159. }
  160. if ($this->type == 2) {
  161. if ($row['force_commit'] == 1) {
  162. $this->force_commit = true;
  163. }
  164. }
  165. $this->mode = $row['default_view_mod'];
  166. } else {
  167. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  168. return false;
  169. }
  170. }
  171. // Check user ID.
  172. if (empty($user_id)) {
  173. $this->error = 'User ID is empty';
  174. return false;
  175. } else {
  176. $user_info = api_get_user_info($user_id);
  177. if (!empty($user_info)) {
  178. $this->user_id = $user_info['user_id'];
  179. } else {
  180. $this->error = 'User ID does not exist in database ('.$sql.')';
  181. return false;
  182. }
  183. }
  184. // End of variables checking.
  185. $session_id = api_get_session_id();
  186. // Get the session condition for learning paths of the base + session.
  187. $session = api_get_session_condition($session_id);
  188. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  189. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  190. // Selecting by view_count descending allows to get the highest view_count first.
  191. $sql = "SELECT * FROM $lp_table WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session ORDER BY view_count DESC";
  192. $res = Database::query($sql);
  193. if ($this->debug > 2) {
  194. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  195. }
  196. if (Database :: num_rows($res) > 0) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  199. }
  200. $row = Database :: fetch_array($res);
  201. $this->attempt = $row['view_count'];
  202. $this->lp_view_id = $row['id'];
  203. $this->last_item_seen = $row['last_item'];
  204. $this->progress_db = $row['progress'];
  205. $this->lp_view_session_id = $row['session_id'];
  206. } else {
  207. if ($this->debug > 2) {
  208. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  209. }
  210. $this->attempt = 1;
  211. $sql_ins = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id) VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  212. $res_ins = Database::query($sql_ins);
  213. $this->lp_view_id = Database :: insert_id();
  214. if ($this->debug > 2) {
  215. error_log('New LP - learnpath::__construct() '.__LINE__.' - inserting new lp_view: '.$sql_ins, 0);
  216. }
  217. }
  218. // Initialise items.
  219. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  220. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  221. $res = Database::query($sql);
  222. if ($this->debug > 2) {
  223. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  224. error_log('-- Start while--', 0);
  225. }
  226. $lp_item_id_list = array();
  227. while ($row = Database::fetch_array($res)) {
  228. $oItem = '';
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - aicc object with id '.$my_item_id.' set in items[]',
  243. 0
  244. );
  245. }
  246. }
  247. break;
  248. case 2:
  249. require_once 'scorm.class.php';
  250. require_once 'scormItem.class.php';
  251. $oItem = new scormItem('db', $row['id'], $course_id);
  252. if (is_object($oItem)) {
  253. $my_item_id = $oItem->get_id();
  254. $oItem->set_lp_view($this->lp_view_id, $course_id);
  255. $oItem->set_prevent_reinit($this->prevent_reinit);
  256. // Don't use reference here as the next loop will make the pointed object change.
  257. $this->items[$my_item_id] = $oItem;
  258. $this->refs_list[$oItem->ref] = $my_item_id;
  259. if ($this->debug > 2) {
  260. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  261. }
  262. }
  263. break;
  264. case 1:
  265. default:
  266. if ($this->debug > 2) {
  267. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  268. }
  269. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  270. if ($this->debug > 2) {
  271. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  272. }
  273. if (is_object($oItem)) {
  274. $my_item_id = $oItem->get_id();
  275. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  276. $oItem->set_prevent_reinit($this->prevent_reinit);
  277. // Don't use reference here as the next loop will make the pointed object change.
  278. $this->items[$my_item_id] = $oItem;
  279. $this->refs_list[$my_item_id] = $my_item_id;
  280. if ($this->debug > 2) {
  281. error_log(
  282. 'New LP - learnpath::__construct() '.__LINE__.' - object with id '.$my_item_id.' set in items[]',
  283. 0
  284. );
  285. }
  286. }
  287. break;
  288. }
  289. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  290. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  291. if (is_object($this->items[$row['id']])) {
  292. $this->items[$row['id']]->set_level(0);
  293. }
  294. } else {
  295. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  296. $this->items[$row['id']]->set_level($level);
  297. if (is_object($this->items[$row['parent_item_id']])) {
  298. // Items is a list of pointers from item DB ids to item objects.
  299. $this->items[$row['parent_item_id']]->add_child($row['id']);
  300. } else {
  301. if ($this->debug > 2) {
  302. error_log(
  303. 'New LP - learnpath::__construct() '.__LINE__.' - The parent item ('.$row['parent_item_id'].') of item '.$row['id'].' could not be found',
  304. 0
  305. );
  306. }
  307. }
  308. }
  309. // Setting the view in the item object.
  310. if (is_object($this->items[$row['id']])) {
  311. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  312. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  313. $this->items[$row['id']]->current_start_time = 0;
  314. $this->items[$row['id']]->current_stop_time = 0;
  315. }
  316. }
  317. }
  318. if ($this->debug > 2) {
  319. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  320. }
  321. if (!empty($lp_item_id_list)) {
  322. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  323. // Get last viewing vars.
  324. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  325. // This query should only return one or zero result.
  326. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  327. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  328. ORDER BY view_count DESC ";
  329. if ($this->debug > 2) {
  330. error_log('New LP - learnpath::__construct() - Selecting item_views: '.$sql, 0);
  331. }
  332. $status_list = array();
  333. $res = Database::query($sql);
  334. while ($row = Database :: fetch_array($res)) {
  335. $status_list[$row['lp_item_id']] = $row['status'];
  336. }
  337. foreach ($lp_item_id_list as $item_id) {
  338. if (isset($status_list[$item_id])) {
  339. $status = $status_list[$item_id];
  340. if (is_object($this->items[$item_id])) {
  341. $this->items[$item_id]->set_status($status);
  342. if (empty($status)) {
  343. $this->items[$item_id]->set_status($this->default_status);
  344. }
  345. }
  346. } else {
  347. if (is_object($this->items[$item_id])) {
  348. $this->items[$item_id]->set_status($this->default_status);
  349. }
  350. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  351. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  352. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  353. if ($this->debug > 2) {
  354. error_log(
  355. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  356. 0
  357. );
  358. }
  359. Database::query($sql_ins);
  360. }
  361. }
  362. }
  363. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  364. $this->max_ordered_items = 0;
  365. foreach ($this->ordered_items as $index => $dummy) {
  366. if ($index > $this->max_ordered_items && !empty($dummy)) {
  367. $this->max_ordered_items = $index;
  368. }
  369. }
  370. // TODO: Define the current item better.
  371. $this->first();
  372. if ($this->debug > 2) {
  373. error_log(
  374. 'New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(
  375. ),
  376. 0
  377. );
  378. }
  379. }
  380. /**
  381. * Function rewritten based on old_add_item() from Yannick Warnier. Due the fact that users can decide where the item should come, I had to overlook this function and
  382. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  383. *
  384. * @param int $parent
  385. * @param int $previous
  386. * @param string $type
  387. * @param int resource ID (ref)
  388. * @param string $title
  389. * @param string $description
  390. * @return int
  391. */
  392. public function add_item(
  393. $parent,
  394. $previous,
  395. $type = 'dokeos_chapter',
  396. $id,
  397. $title,
  398. $description,
  399. $prerequisites = 0,
  400. $max_time_allowed = 0
  401. ) {
  402. $course_id = api_get_course_int_id();
  403. if ($this->debug > 0) {
  404. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  405. }
  406. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  407. $parent = intval($parent);
  408. $previous = intval($previous);
  409. $type = Database::escape_string($type);
  410. $id = intval($id);
  411. $max_time_allowed = Database::escape_string($max_time_allowed);
  412. if (empty ($max_time_allowed)) {
  413. $max_time_allowed = 0;
  414. }
  415. $title = Database::escape_string($title);
  416. $description = Database::escape_string($description);
  417. $sql_count = " SELECT COUNT(id) AS num
  418. FROM $tbl_lp_item
  419. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND parent_item_id = ".$parent;
  420. $res_count = Database::query($sql_count);
  421. $row = Database :: fetch_array($res_count);
  422. $num = $row['num'];
  423. if ($num > 0) {
  424. if ($previous == 0) {
  425. $sql = "SELECT id, next_item_id, display_order
  426. FROM ".$tbl_lp_item."
  427. WHERE c_id = $course_id AND
  428. lp_id = ".$this->get_id()." AND
  429. parent_item_id = ".$parent." AND
  430. previous_item_id = 0 OR previous_item_id=".$parent;
  431. $result = Database::query($sql);
  432. $row = Database :: fetch_array($result);
  433. $tmp_previous = 0;
  434. $next = $row['id'];
  435. $display_order = 0;
  436. } else {
  437. $previous = (int)$previous;
  438. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  439. FROM $tbl_lp_item
  440. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND id = ".$previous;
  441. $result = Database::query($sql);
  442. $row = Database :: fetch_array($result);
  443. $tmp_previous = $row['id'];
  444. $next = $row['next_item_id'];
  445. $display_order = $row['display_order'];
  446. }
  447. } else {
  448. $tmp_previous = 0;
  449. $next = 0;
  450. $display_order = 0;
  451. }
  452. $new_item_id = -1;
  453. $id = Database::escape_string($id);
  454. if ($type == 'quiz') {
  455. $sql = 'SELECT SUM(ponderation)
  456. FROM '.Database :: get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  457. INNER JOIN '.Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  458. ON quiz_question.iid = quiz_rel_question.question_id
  459. WHERE quiz_rel_question.exercice_id = '.$id." AND
  460. quiz_question.c_id = $course_id AND
  461. quiz_rel_question.c_id = $course_id ";
  462. $rsQuiz = Database::query($sql);
  463. $max_score = Database :: result($rsQuiz, 0, 0);
  464. //Disabling the exercise if we add it inside a LP
  465. $exercise = new Exercise();
  466. $exercise->read($id);
  467. $exercise->disable();
  468. $exercise->save();
  469. } else {
  470. $max_score = 100;
  471. }
  472. if ($prerequisites != 0) {
  473. $sql_ins = "INSERT INTO ".$tbl_lp_item." (
  474. c_id,
  475. lp_id, ".
  476. "item_type, ".
  477. "ref, ".
  478. "title, ".
  479. "description, ".
  480. "path, ".
  481. "max_score, ".
  482. "parent_item_id, ".
  483. "previous_item_id, ".
  484. "next_item_id, ".
  485. "display_order, ".
  486. "prerequisite, ".
  487. "max_time_allowed ".
  488. ") VALUES (
  489. $course_id ,
  490. ".$this->get_id().", ".
  491. "'".$type."', ".
  492. "'', ".
  493. "'".$title."', ".
  494. "'".$description."', ".
  495. "'".$id."', ".
  496. "'".$max_score."', ".
  497. $parent.", ".
  498. $previous.", ".
  499. $next.", ".
  500. ($display_order + 1).", ".
  501. $prerequisites.", ".
  502. $max_time_allowed.
  503. ")";
  504. } else {
  505. // Insert new item.
  506. $sql_ins = "
  507. INSERT INTO ".$tbl_lp_item." ( ".
  508. "c_id, ".
  509. "lp_id, ".
  510. "item_type, ".
  511. "ref, ".
  512. "title, ".
  513. "description, ".
  514. "path, ".
  515. "max_score, ".
  516. "parent_item_id, ".
  517. "previous_item_id, ".
  518. "next_item_id, ".
  519. "display_order, ".
  520. "max_time_allowed ".
  521. ") VALUES (".
  522. $course_id.",".
  523. $this->get_id().",".
  524. "'".$type."',".
  525. "'',".
  526. "'".$title."',".
  527. "'".$description."',".
  528. "'".$id."',".
  529. "'".$max_score."',".
  530. $parent.",".
  531. $previous.",".
  532. $next.",".
  533. ($display_order + 1).",".
  534. $max_time_allowed.
  535. ")";
  536. }
  537. if ($this->debug > 2) {
  538. error_log('New LP - Inserting dokeos_chapter: '.$sql_ins, 0);
  539. }
  540. $res_ins = Database::query($sql_ins);
  541. if ($res_ins) {
  542. $new_item_id = Database::insert_id();
  543. // Update the item that should come after the new item.
  544. $sql_update_next = "
  545. UPDATE ".$tbl_lp_item."
  546. SET previous_item_id = ".$new_item_id."
  547. WHERE c_id = $course_id AND id = ".$next;
  548. Database::query($sql_update_next);
  549. // Update the item that should be before the new item.
  550. $sql_update_previous = "
  551. UPDATE ".$tbl_lp_item."
  552. SET next_item_id = ".$new_item_id."
  553. WHERE c_id = $course_id AND id = ".$tmp_previous;
  554. Database::query($sql_update_previous);
  555. // Update all the items after the new item.
  556. $sql_update_order = "
  557. UPDATE ".$tbl_lp_item."
  558. SET display_order = display_order + 1
  559. WHERE
  560. c_id = $course_id AND
  561. lp_id = ".$this->get_id()." AND
  562. id <> ".$new_item_id." AND
  563. parent_item_id = ".$parent." AND
  564. display_order > ".$display_order;
  565. Database::query($sql_update_order);
  566. // Update the item that should come after the new item.
  567. $sql_update_ref = "UPDATE ".$tbl_lp_item."
  568. SET ref = ".$new_item_id."
  569. WHERE c_id = $course_id AND id = ".$new_item_id;
  570. Database::query($sql_update_ref);
  571. }
  572. // Upload audio.
  573. if (!empty ($_FILES['mp3']['name'])) {
  574. // Create the audio folder if it does not exist yet.
  575. $_course = api_get_course_info();
  576. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  577. if (!is_dir($filepath.'audio')) {
  578. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  579. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  580. api_item_property_update(
  581. $_course,
  582. TOOL_DOCUMENT,
  583. $audio_id,
  584. 'FolderCreated',
  585. api_get_user_id(),
  586. null,
  587. null,
  588. null,
  589. null,
  590. api_get_session_id()
  591. );
  592. api_item_property_update(
  593. $_course,
  594. TOOL_DOCUMENT,
  595. $audio_id,
  596. 'invisible',
  597. api_get_user_id(),
  598. null,
  599. null,
  600. null,
  601. null,
  602. api_get_session_id()
  603. );
  604. }
  605. // Upload the file in the documents tool.
  606. $file_path = FileManager::handle_uploaded_document(
  607. $_course,
  608. $_FILES['mp3'],
  609. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  610. '/audio',
  611. api_get_user_id(),
  612. '',
  613. '',
  614. '',
  615. '',
  616. false
  617. );
  618. // Getting the filename only.
  619. $file_components = explode('/', $file_path);
  620. $file = $file_components[count($file_components) - 1];
  621. // Store the mp3 file in the lp_item table.
  622. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string(
  623. $file
  624. )."' WHERE id = '".Database::escape_string($new_item_id)."'";
  625. Database::query($sql_insert_audio);
  626. }
  627. return $new_item_id;
  628. }
  629. /**
  630. * Static admin function allowing addition of a learnpath to a course.
  631. * @param string Course code
  632. * @param string Learnpath name
  633. * @param string Learnpath description string, if provided
  634. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  635. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  636. * @param string Zip file containing the learnpath or directory containing the learnpath
  637. * @return integer The new learnpath ID on success, 0 on failure
  638. */
  639. public static function add_lp(
  640. $course,
  641. $name,
  642. $description = '',
  643. $learnpath = 'guess',
  644. $origin = 'zip',
  645. $zipname = '',
  646. $publicated_on = '',
  647. $expired_on = '',
  648. $category_id = 0
  649. ) {
  650. global $charset;
  651. $course_id = api_get_course_int_id();
  652. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  653. // Check course code exists.
  654. // Check lp_name doesn't exist, otherwise append something.
  655. $i = 0;
  656. $name = Database::escape_string($name);
  657. $category_id = intval($category_id);
  658. // Session id.
  659. $session_id = api_get_session_id();
  660. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  661. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  662. $res_name = Database::query($check_name);
  663. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  664. //by default the publication date is the same that the creation date
  665. //The behaviour above was changed due BT#2800
  666. global $_custom;
  667. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  668. $publicated_on = '';
  669. } else {
  670. $publicated_on = api_get_utc_datetime();
  671. }
  672. } else {
  673. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  674. }
  675. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  676. $expired_on = '';
  677. } else {
  678. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  679. }
  680. while (Database :: num_rows($res_name)) {
  681. // There is already one such name, update the current one a bit.
  682. $i++;
  683. $name = $name.' - '.$i;
  684. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  685. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  686. $res_name = Database::query($check_name);
  687. }
  688. // New name does not exist yet; keep it.
  689. // Escape description.
  690. $description = Database::escape_string(
  691. api_htmlentities($description, ENT_QUOTES, $charset)
  692. ); // Kevin: added htmlentities().
  693. $type = 1;
  694. switch ($learnpath) {
  695. case 'guess':
  696. break;
  697. case 'dokeos':
  698. case 'chamilo':
  699. $type = 1;
  700. break;
  701. case 'aicc':
  702. break;
  703. }
  704. switch ($origin) {
  705. case 'zip':
  706. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  707. break;
  708. case 'manual':
  709. default:
  710. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  711. $res_max = Database::query($get_max);
  712. if (Database :: num_rows($res_max) < 1) {
  713. $dsp = 1;
  714. } else {
  715. $row = Database :: fetch_array($res_max);
  716. $dsp = $row[0] + 1;
  717. }
  718. $sql_insert = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id) ".
  719. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime(
  720. )."' , '".$publicated_on."' , '".$expired_on."', $category_id)";
  721. Database::query($sql_insert);
  722. $id = Database :: insert_id();
  723. if ($id > 0) {
  724. $course_info = api_get_course_info();
  725. // Insert into item_property.
  726. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  727. api_set_default_visibility($course_info, $id, TOOL_LEARNPATH);
  728. return $id;
  729. }
  730. break;
  731. }
  732. }
  733. /**
  734. * Appends a message to the message attribute
  735. * @param string Message to append.
  736. */
  737. public function append_message($string)
  738. {
  739. if ($this->debug > 0) {
  740. error_log('New LP - In learnpath::append_message()', 0);
  741. }
  742. $this->message .= $string;
  743. }
  744. /**
  745. * Autocompletes the parents of an item in case it's been completed or passed
  746. * @param integer Optional ID of the item from which to look for parents
  747. */
  748. public function autocomplete_parents($item)
  749. {
  750. $debug = $this->debug;
  751. if ($debug) {
  752. error_log('Learnpath::autocomplete_parents()', 0);
  753. }
  754. if (empty($item)) {
  755. $item = $this->current;
  756. }
  757. $parent_id = $this->items[$item]->get_parent();
  758. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  759. // if $item points to an object and there is a parent.
  760. if ($debug) {
  761. error_log('Autocompleting parent of item '.$item.' (item '.$parent_id.')', 0);
  762. }
  763. $current_item = $this->items[$item];
  764. $parent = $this->items[$parent_id]; // Get the parent.
  765. // New experiment including failed and browsed in completed status.
  766. $current_status = $current_item->get_status();
  767. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  768. // If the current item is completed or passes or succeeded.
  769. $completed = true;
  770. if ($debug) {
  771. error_log('Status of current item is alright', 0);
  772. }
  773. foreach ($parent->get_children() as $child) {
  774. // Check all his brothers (parent's children) for completion status.
  775. if ($child != $item) {
  776. if ($debug) {
  777. error_log(
  778. 'Looking at brother with ID '.$child.', status is '.$this->items[$child]->get_status(),
  779. 0
  780. );
  781. }
  782. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  783. // Trying completing parents of failed and browsed items as well.
  784. if ($this->items[$child]->status_is(
  785. array(
  786. 'completed',
  787. 'passed',
  788. 'succeeded',
  789. 'browsed',
  790. 'failed'
  791. )
  792. )
  793. ) {
  794. // Keep completion status to true.
  795. } else {
  796. if ($this->debug > 2) {
  797. error_log(
  798. 'Found one incomplete child of '.$parent_id.': '.$child.' is '.$this->items[$child]->get_status(
  799. ),
  800. 0
  801. );
  802. }
  803. $completed = false;
  804. }
  805. }
  806. }
  807. if ($completed) { // If all the children were completed:
  808. $parent->set_status('completed');
  809. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  810. $this->update_queue[$parent->get_id()] = $parent->get_status();
  811. if ($debug) {
  812. error_log('Added parent to update queue '.print_r($this->update_queue, true), 0);
  813. }
  814. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  815. }
  816. } else {
  817. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  818. }
  819. } else {
  820. if ($debug) {
  821. error_log("#$item is an item doesn't have parents");
  822. }
  823. }
  824. }
  825. /**
  826. * Autosaves the current results into the database for the whole learnpath
  827. */
  828. public function autosave()
  829. {
  830. if ($this->debug > 0) {
  831. error_log('New LP - In learnpath::autosave()', 0);
  832. }
  833. // TODO: Add save operations for the learnpath itself.
  834. }
  835. /**
  836. * Clears the message attribute
  837. */
  838. public function clear_message()
  839. {
  840. if ($this->debug > 0) {
  841. error_log('New LP - In learnpath::clear_message()', 0);
  842. }
  843. $this->message = '';
  844. }
  845. /**
  846. * Closes the current resource
  847. *
  848. * Stops the timer
  849. * Saves into the database if required
  850. * Clears the current resource data from this object
  851. * @return boolean True on success, false on failure
  852. */
  853. public function close()
  854. {
  855. $course_id = api_get_course_int_id();
  856. if ($this->debug > 0) {
  857. error_log('New LP - In learnpath::close()', 0);
  858. }
  859. if (empty ($this->lp_id)) {
  860. $this->error = 'Trying to close this learnpath but no ID is set';
  861. return false;
  862. }
  863. $this->current_time_stop = time();
  864. if ($this->save) {
  865. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  866. /*
  867. $sql = "UPDATE $learnpath_view_table " .
  868. "SET " .
  869. "stop_time = ".$this->current_time_stop.", " .
  870. "score = ".$this->current_score.", ".
  871. "WHERE learnpath_id = '".$this->lp_id."'";
  872. //$res = Database::query($sql);
  873. $res = Database::query($res);
  874. if (Database::affected_rows($res) < 1) {
  875. $this->error = 'Could not update learnpath_view table while closing learnpath';
  876. return false;
  877. }
  878. */
  879. }
  880. $this->ordered_items = array();
  881. $this->index = 0;
  882. unset ($this->lp_id);
  883. //unset other stuff
  884. return true;
  885. }
  886. /**
  887. * Static admin function allowing removal of a learnpath
  888. * @param string Course code
  889. * @param integer Learnpath ID
  890. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  891. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  892. */
  893. public function delete($course = null, $id = null, $delete = 'keep')
  894. {
  895. $course_id = api_get_course_int_id();
  896. // TODO: Implement a way of getting this to work when the current object is not set.
  897. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  898. //if (empty($course)) { $course = api_get_course_id(); }
  899. //if (empty($id)) { $id = $this->get_id(); }
  900. // If an ID is specifically given and the current LP is not the same, prevent delete.
  901. if (!empty ($id) && ($id != $this->lp_id)) {
  902. return false;
  903. }
  904. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  905. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  906. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  907. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  908. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  909. // Delete lp item id.
  910. foreach ($this->items as $id => $dummy) {
  911. //$this->items[$id]->delete();
  912. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  913. $res_del_item_view = Database::query($sql_del_view);
  914. }
  915. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  916. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  917. $res_del_item = Database::query($sql_del_item);
  918. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  919. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  920. $res_del_view = Database::query($sql_del_view);
  921. self::toggle_publish($this->lp_id, 'i');
  922. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  923. if ($this->type == 2 || $this->type == 3) {
  924. // This is a scorm learning path, delete the files as well.
  925. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  926. $res = Database::query($sql);
  927. if (Database :: num_rows($res) > 0) {
  928. $row = Database :: fetch_array($res);
  929. $path = $row['path'];
  930. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  931. $res = Database::query($sql);
  932. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  933. if ($this->debug > 2) {
  934. error_log(
  935. 'New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory',
  936. 0
  937. );
  938. }
  939. } else {
  940. // No other LP uses that directory, delete it.
  941. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  942. $course_scorm_dir = api_get_path(
  943. SYS_COURSE_PATH
  944. ).$course_rel_dir; // The absolute system path for this course.
  945. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) and !empty ($course_scorm_dir)) {
  946. if ($this->debug > 2) {
  947. error_log(
  948. 'New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path,
  949. 0
  950. );
  951. }
  952. // Proposed by Christophe (clefevre).
  953. if (strcmp(substr($path, -2), "/.") == 0) {
  954. $path = substr($path, 0, -1); // Remove "." at the end.
  955. }
  956. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  957. api_rmdirr($course_scorm_dir.$path);
  958. }
  959. }
  960. }
  961. }
  962. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  963. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  964. $res_del_lp = Database::query($sql_del_lp);
  965. $this->update_display_order(); // Updates the display order of all lps.
  966. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  967. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  968. // Delete link of gradebook tool
  969. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  970. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  971. $result = Database::query($sql);
  972. $row = Database :: fetch_array($result, 'ASSOC');*/
  973. // Fixing gradebook link deleted see #5229.
  974. /*
  975. if (!empty($row['id'])) {
  976. $link = LinkFactory :: load($row['id']);
  977. if ($link[0] != null) {
  978. $link[0]->delete();
  979. }
  980. }*/
  981. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  982. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4, $id, api_get_session_id());
  983. if ($link_info !== false) {
  984. remove_resource_from_course_gradebook($link_info['id']);
  985. }
  986. if (api_get_setting('search_enabled') == 'true') {
  987. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  988. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  989. }
  990. }
  991. /**
  992. * Removes all the children of one item - dangerous!
  993. * @param integer Element ID of which children have to be removed
  994. * @return integer Total number of children removed
  995. */
  996. public function delete_children_items($id)
  997. {
  998. $course_id = api_get_course_int_id();
  999. if ($this->debug > 0) {
  1000. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  1001. }
  1002. $num = 0;
  1003. if (empty ($id) || $id != strval(intval($id))) {
  1004. return false;
  1005. }
  1006. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1007. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1008. $res = Database::query($sql);
  1009. while ($row = Database :: fetch_array($res)) {
  1010. $num += $this->delete_children_items($row['id']);
  1011. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1012. $res_del = Database::query($sql_del);
  1013. $num++;
  1014. }
  1015. return $num;
  1016. }
  1017. /**
  1018. * Removes an item from the current learnpath
  1019. * @param integer Elem ID (0 if first)
  1020. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  1021. * @return integer Number of elements moved
  1022. * @todo implement resource removal
  1023. */
  1024. public function delete_item($id, $remove = 'keep')
  1025. {
  1026. $course_id = api_get_course_int_id();
  1027. if ($this->debug > 0) {
  1028. error_log('New LP - In learnpath::delete_item()', 0);
  1029. }
  1030. // TODO: Implement the resource removal.
  1031. if (empty ($id) || $id != strval(intval($id))) {
  1032. return false;
  1033. }
  1034. // First select item to get previous, next, and display order.
  1035. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1036. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1037. $res_sel = Database::query($sql_sel);
  1038. if (Database :: num_rows($res_sel) < 1) {
  1039. return false;
  1040. }
  1041. $row = Database :: fetch_array($res_sel);
  1042. $previous = $row['previous_item_id'];
  1043. $next = $row['next_item_id'];
  1044. $display = $row['display_order'];
  1045. $parent = $row['parent_item_id'];
  1046. $lp = $row['lp_id'];
  1047. // Delete children items.
  1048. $num = $this->delete_children_items($id);
  1049. if ($this->debug > 2) {
  1050. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1051. }
  1052. // Now delete the item.
  1053. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1054. if ($this->debug > 2) {
  1055. error_log('New LP - Deleting item: '.$sql_del, 0);
  1056. }
  1057. $res_del = Database::query($sql_del);
  1058. // Now update surrounding items.
  1059. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  1060. $res_upd = Database::query($sql_upd);
  1061. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  1062. $res_upd = Database::query($sql_upd);
  1063. // Now update all following items with new display order.
  1064. $sql_all = "UPDATE $lp_item SET display_order = display_order-1 WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1065. $res_all = 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. $res_all = 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. ) {
  1108. $course_id = api_get_course_int_id();
  1109. if ($this->debug > 0) {
  1110. error_log('New LP - In learnpath::edit_item()', 0);
  1111. }
  1112. if (empty ($max_time_allowed)) {
  1113. $max_time_allowed = 0;
  1114. }
  1115. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1116. return false;
  1117. }
  1118. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1119. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1120. $res_select = Database::query($sql_select);
  1121. $row_select = Database :: fetch_array($res_select);
  1122. $audio_update_sql = '';
  1123. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1124. // Create the audio folder if it does not exist yet.
  1125. $_course = api_get_course_info();
  1126. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1127. if (!is_dir($filepath.'audio')) {
  1128. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1129. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  1130. api_item_property_update(
  1131. $_course,
  1132. TOOL_DOCUMENT,
  1133. $audio_id,
  1134. 'FolderCreated',
  1135. api_get_user_id(),
  1136. null,
  1137. null,
  1138. null,
  1139. null,
  1140. api_get_session_id()
  1141. );
  1142. api_item_property_update(
  1143. $_course,
  1144. TOOL_DOCUMENT,
  1145. $audio_id,
  1146. 'invisible',
  1147. api_get_user_id(),
  1148. null,
  1149. null,
  1150. null,
  1151. null,
  1152. api_get_session_id()
  1153. );
  1154. }
  1155. // Upload file in documents.
  1156. $pi = pathinfo($audio['name']);
  1157. if ($pi['extension'] == 'mp3') {
  1158. $c_det = api_get_course_info($this->cc);
  1159. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1160. $path = FileManager::handle_uploaded_document(
  1161. $c_det,
  1162. $audio,
  1163. $bp,
  1164. '/audio',
  1165. api_get_user_id(),
  1166. 0,
  1167. null,
  1168. 0,
  1169. 'rename',
  1170. false,
  1171. 0
  1172. );
  1173. $path = substr($path, 7);
  1174. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1175. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1176. }
  1177. }
  1178. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1179. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1180. // TODO: htmlspecialchars to be checked for encoding related problems.
  1181. if ($same_parent && $same_previous) {
  1182. // Only update title and description.
  1183. $sql_update = " UPDATE ".$tbl_lp_item."
  1184. SET title = '".Database::escape_string($title)."',
  1185. prerequisite = '".$prerequisites."',
  1186. description = '".Database::escape_string($description)."'
  1187. ".$audio_update_sql.",
  1188. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1189. WHERE c_id = ".$course_id." AND id = ".$id;
  1190. $res_update = Database::query($sql_update);
  1191. } else {
  1192. $old_parent = $row_select['parent_item_id'];
  1193. $old_previous = $row_select['previous_item_id'];
  1194. $old_next = $row_select['next_item_id'];
  1195. $old_order = $row_select['display_order'];
  1196. $old_prerequisite = $row_select['prerequisite'];
  1197. $old_max_time_allowed = $row_select['max_time_allowed'];
  1198. /* BEGIN -- virtually remove the current item id */
  1199. /* for the next and previous item it is like the current item doesn't exist anymore */
  1200. if ($old_previous != 0) {
  1201. $sql_update_next = "
  1202. UPDATE ".$tbl_lp_item."
  1203. SET next_item_id = ".$old_next."
  1204. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1205. $res_update_next = Database::query($sql_update_next);
  1206. //echo '<p>' . $sql_update_next . '</p>';
  1207. }
  1208. if ($old_next != 0) {
  1209. $sql_update_previous = "
  1210. UPDATE ".$tbl_lp_item."
  1211. SET previous_item_id = ".$old_previous."
  1212. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1213. $res_update_previous = Database::query($sql_update_previous);
  1214. //echo '<p>' . $sql_update_previous . '</p>';
  1215. }
  1216. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1217. $sql_update_order = "
  1218. UPDATE ".$tbl_lp_item."
  1219. SET display_order = display_order - 1
  1220. WHERE
  1221. c_id = ".$course_id." AND
  1222. display_order > ".$old_order." AND lp_id = ".$this->lp_id." AND
  1223. parent_item_id = ".$old_parent;
  1224. $res_update_order = Database::query($sql_update_order);
  1225. //echo '<p>' . $sql_update_order . '</p>';
  1226. /* END -- virtually remove the current item id */
  1227. /* BEGIN -- update the current item id to his new location */
  1228. if ($previous == 0) {
  1229. // Select the data of the item that should come after the current item.
  1230. $sql_select_old = "SELECT id, display_order
  1231. FROM ".$tbl_lp_item."
  1232. WHERE
  1233. c_id = ".$course_id." AND
  1234. lp_id = ".$this->lp_id." AND
  1235. parent_item_id = ".$parent." AND
  1236. previous_item_id = ".$previous;
  1237. $res_select_old = Database::query($sql_select_old);
  1238. $row_select_old = Database :: fetch_array($res_select_old);
  1239. //echo '<p>' . $sql_select_old . '</p>';
  1240. // If the new parent didn't have children before.
  1241. if (Database :: num_rows($res_select_old) == 0) {
  1242. $new_next = 0;
  1243. $new_order = 1;
  1244. } else {
  1245. $new_next = $row_select_old['id'];
  1246. $new_order = $row_select_old['display_order'];
  1247. }
  1248. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1249. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1250. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1251. } else {
  1252. // Select the data of the item that should come before the current item.
  1253. $sql_select_old = " SELECT next_item_id, display_order
  1254. FROM ".$tbl_lp_item."
  1255. WHERE c_id = ".$course_id." AND id = ".$previous;
  1256. $res_select_old = Database::query($sql_select_old);
  1257. $row_select_old = Database :: fetch_array($res_select_old);
  1258. //echo '<p>' . $sql_select_old . '</p>';
  1259. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1260. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1261. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1262. $new_next = $row_select_old['next_item_id'];
  1263. $new_order = $row_select_old['display_order'] + 1;
  1264. }
  1265. // TODO: htmlspecialchars to be checked for encoding related problems.
  1266. // Update the current item with the new data.
  1267. $sql_update = "UPDATE ".$tbl_lp_item."
  1268. SET
  1269. title = '".Database::escape_string($title)."',
  1270. description = '".Database::escape_string($description)."',
  1271. parent_item_id = ".$parent.",
  1272. previous_item_id = ".$previous.",
  1273. next_item_id = ".$new_next.",
  1274. display_order = ".$new_order."
  1275. ".$audio_update_sql."
  1276. WHERE c_id = ".$course_id." AND id = ".$id;
  1277. $res_update_next = Database::query($sql_update);
  1278. //echo '<p>' . $sql_update . '</p>';
  1279. if ($previous != 0) {
  1280. // Update the previous item's next_item_id.
  1281. $sql_update_previous = "
  1282. UPDATE ".$tbl_lp_item."
  1283. SET next_item_id = ".$id."
  1284. WHERE c_id = ".$course_id." AND id = ".$previous;
  1285. $res_update_next = Database::query($sql_update_previous);
  1286. //echo '<p>' . $sql_update_previous . '</p>';
  1287. }
  1288. if ($new_next != 0) {
  1289. // Update the next item's previous_item_id.
  1290. $sql_update_next = "
  1291. UPDATE ".$tbl_lp_item."
  1292. SET previous_item_id = ".$id."
  1293. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1294. $res_update_next = Database::query($sql_update_next);
  1295. //echo '<p>' . $sql_update_next . '</p>';
  1296. }
  1297. if ($old_prerequisite != $prerequisites) {
  1298. $sql_update_next = "
  1299. UPDATE ".$tbl_lp_item."
  1300. SET prerequisite = ".$prerequisites."
  1301. WHERE c_id = ".$course_id." AND id = ".$id;
  1302. $res_update_next = Database::query($sql_update_next);
  1303. }
  1304. if ($old_max_time_allowed != $max_time_allowed) {
  1305. $sql_update_max_time_allowed = "
  1306. UPDATE ".$tbl_lp_item."
  1307. SET max_time_allowed = ".$max_time_allowed."
  1308. WHERE c_id = ".$course_id." AND id = ".$id;
  1309. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1310. }
  1311. // Update all the items with the same or a bigger display_order than the current item.
  1312. $sql_update_order = "
  1313. UPDATE ".$tbl_lp_item."
  1314. SET display_order = display_order + 1
  1315. WHERE
  1316. c_id = ".$course_id." AND
  1317. lp_id = ".$this->get_id()." AND
  1318. id <> ".$id." AND
  1319. parent_item_id = ".$parent." AND
  1320. display_order >= ".$new_order;
  1321. $res_update_next = Database::query($sql_update_order);
  1322. }
  1323. }
  1324. /**
  1325. * Updates an item's prereq in place
  1326. * @param integer Element ID
  1327. * @param string Prerequisite Element ID
  1328. * @param string Prerequisite item type
  1329. * @param string Prerequisite min score
  1330. * @param string Prerequisite max score
  1331. * @return boolean True on success, false on error
  1332. */
  1333. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1334. {
  1335. $course_id = api_get_course_int_id();
  1336. if ($this->debug > 0) {
  1337. error_log(
  1338. 'New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')',
  1339. 0
  1340. );
  1341. }
  1342. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1343. return false;
  1344. }
  1345. $prerequisite_id = Database::escape_string($prerequisite_id);
  1346. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1347. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1348. $mastery_score = 0;
  1349. }
  1350. if (!is_numeric($max_score) || $max_score < 0) {
  1351. $max_score = 100;
  1352. }
  1353. if ($mastery_score > $max_score) {
  1354. $max_score = $mastery_score;
  1355. }
  1356. if (!is_numeric($prerequisite_id)) {
  1357. $prerequisite_id = 'NULL';
  1358. }
  1359. $sql_upd = " UPDATE ".$tbl_lp_item."
  1360. SET prerequisite = ".$prerequisite_id." WHERE c_id = ".$course_id." AND id = ".$id;
  1361. $res_upd = Database::query($sql_upd);
  1362. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1363. $sql_upd = " UPDATE ".$tbl_lp_item." SET
  1364. mastery_score = ".$mastery_score.
  1365. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1366. " WHERE c_id = ".$course_id." AND ref = '".$prerequisite_id."'"; // Will this be enough to ensure unicity?
  1367. Database::query($sql_upd);
  1368. }
  1369. // TODO: Update the item object (can be ignored for now because refreshed).
  1370. return true;
  1371. }
  1372. /**
  1373. * Escapes a string with the available database escape function
  1374. * @param string String to escape
  1375. * @return string String escaped
  1376. * @deprecated use Database::escape_string
  1377. */
  1378. public function escape_string($string)
  1379. {
  1380. return Database::escape_string($string);
  1381. }
  1382. /**
  1383. * Static admin function exporting a learnpath into a zip file
  1384. * @param string Export type (scorm, zip, cd)
  1385. * @param string Course code
  1386. * @param integer Learnpath ID
  1387. * @param string Zip file name
  1388. * @return string Zip file path (or false on error)
  1389. */
  1390. public function export_lp($type, $course, $id, $zipname)
  1391. {
  1392. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1393. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1394. return false;
  1395. }
  1396. $url = '';
  1397. switch ($type) {
  1398. case 'scorm':
  1399. break;
  1400. case 'zip':
  1401. break;
  1402. case 'cdrom':
  1403. break;
  1404. }
  1405. return $url;
  1406. }
  1407. /**
  1408. * Gets all the chapters belonging to the same parent as the item/chapter given
  1409. * Can also be called as abstract method
  1410. * @param integer Item ID
  1411. * @return array A list of all the "brother items" (or an empty array on failure)
  1412. */
  1413. public function get_brother_chapters($id)
  1414. {
  1415. $course_id = api_get_course_int_id();
  1416. if ($this->debug > 0) {
  1417. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1418. }
  1419. if (empty ($id) OR $id != strval(intval($id))) {
  1420. return array();
  1421. }
  1422. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1423. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1424. $res_parent = Database::query($sql_parent);
  1425. if (Database :: num_rows($res_parent) > 0) {
  1426. $row_parent = Database :: fetch_array($res_parent);
  1427. $parent = $row_parent['parent_item_id'];
  1428. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent AND id = $id AND item_type='dokeos_chapter' ORDER BY display_order";
  1429. $res_bros = Database::query($sql_bros);
  1430. $list = array();
  1431. while ($row_bro = Database :: fetch_array($res_bros)) {
  1432. $list[] = $row_bro;
  1433. }
  1434. return $list;
  1435. }
  1436. return array();
  1437. }
  1438. /**
  1439. * Gets all the items belonging to the same parent as the item given
  1440. * Can also be called as abstract method
  1441. * @param integer Item ID
  1442. * @return array A list of all the "brother items" (or an empty array on failure)
  1443. */
  1444. public function get_brother_items($id)
  1445. {
  1446. $course_id = api_get_course_int_id();
  1447. if ($this->debug > 0) {
  1448. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1449. }
  1450. if (empty ($id) OR $id != strval(intval($id))) {
  1451. return array();
  1452. }
  1453. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1454. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1455. $res_parent = Database::query($sql_parent);
  1456. if (Database :: num_rows($res_parent) > 0) {
  1457. $row_parent = Database :: fetch_array($res_parent);
  1458. $parent = $row_parent['parent_item_id'];
  1459. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1460. $res_bros = Database::query($sql_bros);
  1461. $list = array();
  1462. while ($row_bro = Database :: fetch_array($res_bros)) {
  1463. $list[] = $row_bro;
  1464. }
  1465. return $list;
  1466. }
  1467. return array();
  1468. }
  1469. /**
  1470. * Get the specific prefix index terms of this learning path
  1471. * @return array Array of terms
  1472. */
  1473. public function get_common_index_terms_by_prefix($prefix)
  1474. {
  1475. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1476. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1477. $prefix_terms = array();
  1478. if (!empty($terms)) {
  1479. foreach ($terms as $term) {
  1480. $prefix_terms[] = $term['value'];
  1481. }
  1482. }
  1483. return $prefix_terms;
  1484. }
  1485. /**
  1486. * Gets the number of items currently completed
  1487. * @return integer The number of items currently completed
  1488. */
  1489. public function get_complete_items_count()
  1490. {
  1491. if ($this->debug > 0) {
  1492. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1493. }
  1494. $i = 0;
  1495. foreach ($this->items as $id => $dummy) {
  1496. // Trying failed and browsed considered "progressed" as well.
  1497. if ($this->items[$id]->status_is(
  1498. array(
  1499. 'completed',
  1500. 'passed',
  1501. 'succeeded',
  1502. 'browsed',
  1503. 'failed'
  1504. )
  1505. ) && $this->items[$id]->get_type() != 'dokeos_chapter' && $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. foreach ($this->items as $temp2) {
  1564. if (!in_array(
  1565. $temp2->get_type(),
  1566. array(
  1567. 'dokeos_chapter',
  1568. 'chapter',
  1569. 'dir'
  1570. )
  1571. )
  1572. ) {
  1573. $total++;
  1574. }
  1575. }
  1576. return $total;
  1577. }
  1578. /**
  1579. * Gets the first element URL.
  1580. * @return string URL to load into the viewer
  1581. */
  1582. public function first()
  1583. {
  1584. if ($this->debug > 0) {
  1585. error_log('New LP - In learnpath::first()', 0);
  1586. error_log('$this->last_item_seen '.$this->last_item_seen);
  1587. //error_log('$this->items '.print_r($this->items, 1));
  1588. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1589. }
  1590. // Test if the last_item_seen exists and is not a dir.
  1591. if (count($this->ordered_items) == 0) {
  1592. $this->index = 0;
  1593. }
  1594. if ($this->debug > 0) {
  1595. if (isset($this->items[$this->last_item_seen])) {
  1596. $status = $this->items[$this->last_item_seen]->get_status();
  1597. }
  1598. error_log('status '.$status);
  1599. }
  1600. if (!empty($this->last_item_seen) &&
  1601. !empty($this->items[$this->last_item_seen]) &&
  1602. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1603. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1604. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1605. //&& !$this->items[$this->last_item_seen]->is_done()
  1606. ) {
  1607. if ($this->debug > 2) {
  1608. error_log(
  1609. 'New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(
  1610. ),
  1611. 0
  1612. );
  1613. }
  1614. $index = -1;
  1615. foreach ($this->ordered_items as $myindex => $item_id) {
  1616. if ($item_id == $this->last_item_seen) {
  1617. $index = $myindex;
  1618. break;
  1619. }
  1620. }
  1621. if ($index == -1) {
  1622. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1623. if ($this->debug > 2) {
  1624. error_log(
  1625. 'New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!',
  1626. 0
  1627. );
  1628. }
  1629. return false;
  1630. } else {
  1631. $this->last = $this->last_item_seen;
  1632. $this->current = $this->last_item_seen;
  1633. $this->index = $index;
  1634. }
  1635. } else {
  1636. if ($this->debug > 2) {
  1637. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1638. }
  1639. $index = 0;
  1640. // Loop through all ordered items and stop at the first item that is
  1641. // not a directory *and* that has not been completed yet.
  1642. while (!empty($this->ordered_items[$index]) AND
  1643. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1644. (
  1645. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1646. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1647. $this->items[$this->ordered_items[$index]]->is_done() === true
  1648. ) AND $index < $this->max_ordered_items) {
  1649. $index++;
  1650. }
  1651. $this->last = $this->current;
  1652. // current is
  1653. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1654. $this->index = $index;
  1655. if ($this->debug > 2) {
  1656. error_log('$index '.$index);
  1657. }
  1658. if ($this->debug > 2) {
  1659. error_log(
  1660. 'New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')',
  1661. 0
  1662. );
  1663. }
  1664. }
  1665. if ($this->debug > 2) {
  1666. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1667. }
  1668. }
  1669. /**
  1670. * Gets the information about an item in a format usable as JavaScript to update
  1671. * the JS API by just printing this content into the <head> section of the message frame
  1672. * @param integer Item ID
  1673. * @return string
  1674. */
  1675. public function get_js_info($item_id = '')
  1676. {
  1677. if ($this->debug > 0) {
  1678. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1679. }
  1680. $info = '';
  1681. $item_id = Database::escape_string($item_id);
  1682. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1683. //if item is defined, return values from DB
  1684. $oItem = $this->items[$item_id];
  1685. $info .= '<script language="javascript">';
  1686. $info .= "top.set_score(".$oItem->get_score().");\n";
  1687. $info .= "top.set_max(".$oItem->get_max().");\n";
  1688. $info .= "top.set_min(".$oItem->get_min().");\n";
  1689. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1690. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1691. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1692. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1693. $info .= "top.set_flag_synchronized();";
  1694. $info .= '</script>';
  1695. if ($this->debug > 2) {
  1696. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1697. }
  1698. return $info;
  1699. } else {
  1700. // If item_id is empty, just update to default SCORM data.
  1701. $info .= '<script language="javascript">';
  1702. $info .= "top.set_score(".learnpathItem :: get_score().");\n";
  1703. $info .= "top.set_max(".learnpathItem :: get_max().");\n";
  1704. $info .= "top.set_min(".learnpathItem :: get_min().");\n";
  1705. $info .= "top.set_lesson_status('".learnpathItem :: get_status()."');";
  1706. $info .= "top.set_session_time('".learnpathItem :: get_scorm_time('js')."');";
  1707. $info .= "top.set_suspend_data('".learnpathItem :: get_suspend_data()."');";
  1708. $info .= "top.set_saved_lesson_status('".learnpathItem :: get_status()."');";
  1709. $info .= "top.set_flag_synchronized();";
  1710. $info .= '</script>';
  1711. if ($this->debug > 2) {
  1712. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1713. }
  1714. return $info;
  1715. }
  1716. }
  1717. /**
  1718. * Gets the js library from the database
  1719. * @return string The name of the javascript library to be used
  1720. */
  1721. public function get_js_lib()
  1722. {
  1723. $lib = '';
  1724. if (!empty ($this->js_lib)) {
  1725. $lib = $this->js_lib;
  1726. }
  1727. return $lib;
  1728. }
  1729. /**
  1730. * Gets the learnpath database ID
  1731. * @return integer Learnpath ID in the lp table
  1732. */
  1733. public function get_id()
  1734. {
  1735. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1736. if (!empty ($this->lp_id)) {
  1737. return $this->lp_id;
  1738. } else {
  1739. return 0;
  1740. }
  1741. }
  1742. /**
  1743. * Gets the last element URL.
  1744. * @return string URL to load into the viewer
  1745. */
  1746. public function get_last()
  1747. {
  1748. if ($this->debug > 0) {
  1749. error_log('New LP - In learnpath::get_last()', 0);
  1750. }
  1751. $this->index = count($this->ordered_items) - 1;
  1752. return $this->ordered_items[$this->index];
  1753. }
  1754. /**
  1755. * Gets the navigation bar for the learnpath display screen
  1756. * @return string The HTML string to use as a navigation bar
  1757. */
  1758. public function get_navigation_bar()
  1759. {
  1760. if ($this->debug > 0) {
  1761. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1762. }
  1763. $navbar = null;
  1764. $lp_id = $this->lp_id;
  1765. $mycurrentitemid = $this->get_current_item_id();
  1766. if ($this->mode == 'fullscreen') {
  1767. $navbar = '
  1768. <div class="buttons">
  1769. <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">
  1770. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'</a>
  1771. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1772. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1773. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next">
  1774. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).' </a>
  1775. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1776. '.Display::return_icon('view_choose.gif', get_lang('ScormExitFullScreen')).'</a>
  1777. </div>';
  1778. } else {
  1779. $navbar = '
  1780. <div class="buttons">
  1781. <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">
  1782. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'
  1783. </a>
  1784. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1785. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1786. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" >
  1787. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).'
  1788. </a>
  1789. </div>';
  1790. }
  1791. return $navbar;
  1792. }
  1793. /**
  1794. * Gets the next resource in queue (url).
  1795. * @return string URL to load into the viewer
  1796. */
  1797. public function get_next_index()
  1798. {
  1799. if ($this->debug > 0) {
  1800. error_log('New LP - In learnpath::get_next_index()', 0);
  1801. }
  1802. // TODO
  1803. $index = $this->index;
  1804. $index++;
  1805. if ($this->debug > 2) {
  1806. error_log(
  1807. 'New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type,
  1808. 0
  1809. );
  1810. }
  1811. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1812. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type(
  1813. ) == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1814. $index++;
  1815. if ($index == $this->max_ordered_items) {
  1816. if ($this->items[$this->ordered_items[$index]]->get_type(
  1817. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter'
  1818. ) {
  1819. return $this->index;
  1820. } else {
  1821. return $index;
  1822. }
  1823. }
  1824. }
  1825. if (empty ($this->ordered_items[$index])) {
  1826. return $this->index;
  1827. }
  1828. if ($this->debug > 2) {
  1829. error_log('New LP - index is now '.$index, 0);
  1830. }
  1831. return $index;
  1832. }
  1833. /**
  1834. * Gets item_id for the next element
  1835. * @return integer Next item (DB) ID
  1836. */
  1837. public function get_next_item_id()
  1838. {
  1839. if ($this->debug > 0) {
  1840. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1841. }
  1842. $new_index = $this->get_next_index();
  1843. if (!empty ($new_index)) {
  1844. if (isset ($this->ordered_items[$new_index])) {
  1845. if ($this->debug > 2) {
  1846. error_log(
  1847. 'New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index],
  1848. 0
  1849. );
  1850. }
  1851. return $this->ordered_items[$new_index];
  1852. }
  1853. }
  1854. if ($this->debug > 2) {
  1855. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1856. }
  1857. return 0;
  1858. }
  1859. /**
  1860. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1861. *
  1862. * Generally, the package provided is in the form of a zip file, so the function
  1863. * has been written to test a zip file. If not a zip, the function will return the
  1864. * default return value: ''
  1865. * @param string the path to the file
  1866. * @param string the original name of the file
  1867. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1868. */
  1869. public static function get_package_type($file_path, $file_name)
  1870. {
  1871. // Get name of the zip file without the extension.
  1872. $file_info = pathinfo($file_name);
  1873. $filename = $file_info['basename']; // Name including extension.
  1874. $extension = $file_info['extension']; // Extension only.
  1875. if (!empty($_POST['ppt2lp']) && !in_array(
  1876. strtolower($extension),
  1877. array(
  1878. 'dll',
  1879. 'exe'
  1880. )
  1881. )
  1882. ) {
  1883. return 'oogie';
  1884. }
  1885. if (!empty($_POST['woogie']) && !in_array(
  1886. strtolower($extension),
  1887. array(
  1888. 'dll',
  1889. 'exe'
  1890. )
  1891. )
  1892. ) {
  1893. return 'woogie';
  1894. }
  1895. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  1896. $zipFile = new PclZip($file_path);
  1897. // Check the zip content (real size and file extension).
  1898. $zipContentArray = $zipFile->listContent();
  1899. $package_type = '';
  1900. $at_root = false;
  1901. $manifest = '';
  1902. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1903. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1904. foreach ($zipContentArray as $thisContent) {
  1905. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1906. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1907. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1908. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1909. $package_type = 'scorm';
  1910. break; // Exit the foreach loop.
  1911. } elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1912. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1913. $package_type = 'aicc';
  1914. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1915. } else {
  1916. $package_type = '';
  1917. }
  1918. }
  1919. }
  1920. return $package_type;
  1921. }
  1922. /**
  1923. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1924. * @return string URL to load into the viewer
  1925. */
  1926. public function get_previous_index()
  1927. {
  1928. if ($this->debug > 0) {
  1929. error_log('New LP - In learnpath::get_previous_index()', 0);
  1930. }
  1931. $index = $this->index;
  1932. if (isset ($this->ordered_items[$index - 1])) {
  1933. $index--;
  1934. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1935. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1936. $index--;
  1937. if ($index < 0) {
  1938. return $this->index;
  1939. }
  1940. }
  1941. } else {
  1942. if ($this->debug > 2) {
  1943. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1944. }
  1945. // There is no previous item.
  1946. }
  1947. return $index;
  1948. }
  1949. /**
  1950. * Gets item_id for the next element
  1951. * @return integer Previous item (DB) ID
  1952. */
  1953. public function get_previous_item_id()
  1954. {
  1955. if ($this->debug > 0) {
  1956. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1957. }
  1958. $new_index = $this->get_previous_index();
  1959. return $this->ordered_items[$new_index];
  1960. }
  1961. /**
  1962. * Gets the progress value from the progress_db attribute
  1963. * @return integer Current progress value
  1964. */
  1965. public function get_progress()
  1966. {
  1967. if ($this->debug > 0) {
  1968. error_log('New LP - In learnpath::get_progress()', 0);
  1969. }
  1970. if (!empty ($this->progress_db)) {
  1971. return $this->progress_db;
  1972. }
  1973. return 0;
  1974. }
  1975. /**
  1976. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1977. * @param integer Learnpath ID
  1978. * @param integer User ID
  1979. * @param string Mode of display ('%','abs' or 'both')
  1980. * @param string Course database name (optional, defaults to '')
  1981. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1982. * @return integer Current progress value as found in the database
  1983. */
  1984. public static function get_db_progress(
  1985. $lp_id,
  1986. $user_id,
  1987. $mode = '%',
  1988. $course_code = '',
  1989. $sincere = false,
  1990. $session_id = 0
  1991. ) {
  1992. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1993. $session_id = intval($session_id);
  1994. $course_info = api_get_course_info($course_code);
  1995. $session_condition = api_get_session_condition($session_id);
  1996. $course_id = $course_info['real_id'];
  1997. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1998. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1999. $res = Database::query($sql);
  2000. $view_id = 0;
  2001. if (Database :: num_rows($res) > 0) {
  2002. $row = Database :: fetch_array($res);
  2003. $progress = $row['progress'];
  2004. $view_id = $row['id'];
  2005. } else {
  2006. if ($sincere) {
  2007. return null;
  2008. }
  2009. }
  2010. if (empty ($progress)) {
  2011. $progress = '0';
  2012. }
  2013. if ($mode == '%') {
  2014. return $progress.'%';
  2015. } else {
  2016. // Get the number of items completed and the number of items total.
  2017. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2018. $sql = "SELECT count(*) FROM $tbl
  2019. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = ".$lp_id." AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  2020. $res = Database::query($sql);
  2021. $row = Database :: fetch_array($res);
  2022. $total = $row[0];
  2023. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2024. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2025. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  2026. // Trying as also counting browsed and failed items.
  2027. $sql = "SELECT count(distinct(lp_item_id))
  2028. FROM $tbl_item_view as item_view
  2029. INNER JOIN $tbl_item as item
  2030. ON item.id = item_view.lp_item_id
  2031. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  2032. WHERE
  2033. item_view.c_id = $course_id AND
  2034. item.c_id = $course_id AND
  2035. lp_view_id = ".$view_id." AND
  2036. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  2037. $res = Database::query($sql);
  2038. $row = Database :: fetch_array($res);
  2039. $completed = $row[0];
  2040. if ($mode == 'abs') {
  2041. return $completed.'/'.$total;
  2042. } elseif ($mode == 'both') {
  2043. if ($progress < ($completed / ($total ? $total : 1))) {
  2044. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2045. }
  2046. return $progress.'% ('.$completed.'/'.$total.')';
  2047. }
  2048. }
  2049. return $progress;
  2050. }
  2051. /**
  2052. * Returns the HTML necessary to print a mediaplayer block inside a page
  2053. * @return string The mediaplayer HTML
  2054. */
  2055. public function get_mediaplayer($autostart = 'true')
  2056. {
  2057. $course_id = api_get_course_int_id();
  2058. $_course = api_get_course_info();
  2059. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2060. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2061. // Getting all the information about the item.
  2062. $sql = "SELECT * FROM ".$tbl_lp_item." as lp INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id ".
  2063. "WHERE lp.id = '".$_SESSION['oLP']->current."' AND
  2064. lp.c_id = $course_id AND
  2065. lp_view.c_id = $course_id";
  2066. $result = Database::query($sql);
  2067. $row = Database::fetch_assoc($result);
  2068. $output = '';
  2069. if (!empty ($row['audio'])) {
  2070. $list = $_SESSION['oLP']->get_toc();
  2071. $type_quiz = false;
  2072. foreach ($list as $toc) {
  2073. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2074. $type_quiz = true;
  2075. }
  2076. }
  2077. if ($type_quiz) {
  2078. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2079. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2080. } else {
  2081. $autostart_audio = $autostart;
  2082. }
  2083. } else {
  2084. $autostart_audio = 'true';
  2085. }
  2086. // The mp3 player.
  2087. $output = '<div id="container">';
  2088. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  2089. $output .= '<script type="text/javascript">
  2090. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  2091. s1.addParam("allowscriptaccess","always");
  2092. s1.addParam("flashvars","file='.api_get_path(
  2093. WEB_COURSE_PATH
  2094. ).$_course['path'].'/document/audio/'.$row['audio'].'&autostart='.$autostart_audio.'");
  2095. s1.write("container");
  2096. </script>
  2097. </div>';
  2098. }
  2099. return $output;
  2100. }
  2101. /**
  2102. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  2103. * @param int Learnpath id
  2104. * @param int Student id
  2105. * @param string Course code (optional)
  2106. * @return bool True if
  2107. */
  2108. public static function is_lp_visible_for_student($lp_id, $student_id, $course_code = null)
  2109. {
  2110. $lp_id = (int)$lp_id;
  2111. $course_info = api_get_course_info($course_code);
  2112. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  2113. // Get current prerequisite
  2114. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2115. FROM $tbl_learnpath WHERE c_id = ".$course_info['real_id']." AND id = $lp_id";
  2116. $rs = Database::query($sql);
  2117. $now = time();
  2118. $session_id = api_get_session_id();
  2119. if (Database::num_rows($rs) > 0) {
  2120. $row = Database::fetch_array($rs, 'ASSOC');
  2121. $prerequisite = $row['prerequisite'];
  2122. $is_visible = true;
  2123. $progress = 0;
  2124. //Checking LP prerequisites
  2125. if (!empty($prerequisite)) {
  2126. $progress = intval(self::get_db_progress($prerequisite, $student_id, '%', '', false, $session_id));
  2127. if ($progress < 100) {
  2128. $is_visible = false;
  2129. }
  2130. }
  2131. // Also check the time availability of the LP
  2132. if ($is_visible) {
  2133. //Adding visibility restrictions
  2134. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  2135. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2136. //api_not_allowed();
  2137. $is_visible = false;
  2138. }
  2139. }
  2140. //Blocking empty start times see BT#2800
  2141. global $_custom;
  2142. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  2143. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2144. //api_not_allowed();
  2145. $is_visible = false;
  2146. }
  2147. }
  2148. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2149. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2150. //api_not_allowed();
  2151. $is_visible = false;
  2152. }
  2153. }
  2154. }
  2155. //Check if the subscription users/group to a LP is ON
  2156. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2157. //Checking only the user visibility
  2158. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2159. if ($userVisibility == 1) {
  2160. $is_visible = true;
  2161. } else {
  2162. $is_visible = false;
  2163. }
  2164. //the group visibility is not going to be checked because the user *must* be related to the LP
  2165. /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2166. if (!empty($groupList)) {
  2167. foreach($groupList as $groupId) {
  2168. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2169. if ($groupVisibility == 1) {
  2170. $is_visible = true;
  2171. break;
  2172. }
  2173. }
  2174. }*/
  2175. }
  2176. return $is_visible;
  2177. }
  2178. return false;
  2179. }
  2180. /**
  2181. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2182. * completed so far.
  2183. * @param string Mode in which we want the values
  2184. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2185. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2186. * @param boolean true if it comes from a Diplay LP view
  2187. * @return string HTML string containing the progress bar
  2188. */
  2189. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2190. {
  2191. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2192. global $lp_theme_css;
  2193. // Setting up the CSS path of the current style if exists.
  2194. if (!empty ($lp_theme_css)) {
  2195. //$css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2196. } else {
  2197. //$css_path = '../img/';
  2198. }
  2199. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2200. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2201. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2202. }
  2203. $text = $percentage.$text_add;
  2204. //@todo use Display::display_progress();
  2205. $output = '<div class="progress progress-striped">
  2206. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  2207. </div>
  2208. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2209. return $output;
  2210. }
  2211. /**
  2212. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2213. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2214. * @param integer Additional steps to fake as completed
  2215. * @return list Percentage or number and symbol (% or /xx)
  2216. */
  2217. public function get_progress_bar_text($mode = '', $add = 0)
  2218. {
  2219. if ($this->debug > 0) {
  2220. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2221. }
  2222. if (empty ($mode)) {
  2223. $mode = $this->progress_bar_mode;
  2224. }
  2225. $total_items = $this->get_total_items_count_without_chapters();
  2226. if ($this->debug > 2) {
  2227. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2228. }
  2229. $i = $this->get_complete_items_count();
  2230. if ($this->debug > 2) {
  2231. error_log('New LP - Items completed so far: '.$i, 0);
  2232. }
  2233. if ($add != 0) {
  2234. $i += $add;
  2235. if ($this->debug > 2) {
  2236. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2237. }
  2238. }
  2239. $text = '';
  2240. if ($i > $total_items) {
  2241. $i = $total_items;
  2242. }
  2243. if ($mode == '%') {
  2244. if ($total_items > 0) {
  2245. $percentage = ((float)$i / (float)$total_items) * 100;
  2246. } else {
  2247. $percentage = 0;
  2248. }
  2249. $percentage = number_format($percentage, 0);
  2250. $text = '%';
  2251. } elseif ($mode == 'abs') {
  2252. $percentage = $i;
  2253. $text = '/'.$total_items;
  2254. }
  2255. return array(
  2256. $percentage,
  2257. $text
  2258. );
  2259. }
  2260. /**
  2261. * Gets the progress bar mode
  2262. * @return string The progress bar mode attribute
  2263. */
  2264. public function get_progress_bar_mode()
  2265. {
  2266. if ($this->debug > 0) {
  2267. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2268. }
  2269. if (!empty ($this->progress_bar_mode)) {
  2270. return $this->progress_bar_mode;
  2271. } else {
  2272. return '%';
  2273. }
  2274. }
  2275. /**
  2276. * Gets the learnpath proximity (remote or local)
  2277. * @return string Learnpath proximity
  2278. */
  2279. public function get_proximity()
  2280. {
  2281. if ($this->debug > 0) {
  2282. error_log('New LP - In learnpath::get_proximity()', 0);
  2283. }
  2284. if (!empty ($this->proximity)) {
  2285. return $this->proximity;
  2286. } else {
  2287. return '';
  2288. }
  2289. }
  2290. /**
  2291. * Gets the learnpath theme (remote or local)
  2292. * @return string Learnpath theme
  2293. */
  2294. public function get_theme()
  2295. {
  2296. if ($this->debug > 0) {
  2297. error_log('New LP - In learnpath::get_theme()', 0);
  2298. }
  2299. if (!empty ($this->theme)) {
  2300. return $this->theme;
  2301. } else {
  2302. return '';
  2303. }
  2304. }
  2305. /**
  2306. * Gets the learnpath session id
  2307. * @return string Learnpath theme
  2308. */
  2309. public function get_lp_session_id()
  2310. {
  2311. if ($this->debug > 0) {
  2312. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2313. }
  2314. if (!empty ($this->lp_session_id)) {
  2315. return $this->lp_session_id;
  2316. } else {
  2317. return 0;
  2318. }
  2319. }
  2320. /**
  2321. * Gets the learnpath image
  2322. * @return string Web URL of the LP image
  2323. */
  2324. public function get_preview_image()
  2325. {
  2326. if ($this->debug > 0) {
  2327. error_log('New LP - In learnpath::get_preview_image()', 0);
  2328. }
  2329. if (!empty($this->preview_image)) {
  2330. return $this->preview_image;
  2331. } else {
  2332. return '';
  2333. }
  2334. }
  2335. public function get_preview_image_path($size = null, $path_type = 'web')
  2336. {
  2337. $preview_image = $this->get_preview_image();
  2338. if (isset($preview_image) && !empty($preview_image)) {
  2339. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2340. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2341. if (isset($size)) {
  2342. $info = pathinfo($preview_image);
  2343. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2344. if (file_exists($image_sys_path.$image_custom_size)) {
  2345. if ($path_type == 'web') {
  2346. return $image_path.$image_custom_size;
  2347. } else {
  2348. return $image_sys_path.$image_custom_size;
  2349. }
  2350. }
  2351. } else {
  2352. if ($path_type == 'web') {
  2353. return $image_path.$preview_image;
  2354. } else {
  2355. return $image_sys_path.$preview_image;
  2356. }
  2357. }
  2358. }
  2359. return false;
  2360. }
  2361. public function get_category_id()
  2362. {
  2363. return $this->category_id;
  2364. }
  2365. /**
  2366. * Gets the learnpath author
  2367. * @return string LP's author
  2368. */
  2369. public function get_author()
  2370. {
  2371. if ($this->debug > 0) {
  2372. error_log('New LP - In learnpath::get_author()', 0);
  2373. }
  2374. if (!empty ($this->author)) {
  2375. return $this->author;
  2376. } else {
  2377. return '';
  2378. }
  2379. }
  2380. /**
  2381. * Gets the learnpath author
  2382. * @return string LP's author
  2383. */
  2384. public function get_hide_toc_frame()
  2385. {
  2386. if ($this->debug > 0) {
  2387. error_log('New LP - In learnpath::get_author()', 0);
  2388. }
  2389. if (!empty ($this->hide_toc_frame)) {
  2390. return $this->hide_toc_frame;
  2391. } else {
  2392. return '';
  2393. }
  2394. }
  2395. /**
  2396. * Generate a new prerequisites string for a given item. If this item was a sco and
  2397. * its prerequisites were strings (instead of IDs), then transform those strings into
  2398. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2399. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2400. * same rule as the scorm_export() method
  2401. * @param integer Item ID
  2402. * @return string Prerequisites string ready for the export as SCORM
  2403. */
  2404. public function get_scorm_prereq_string($item_id)
  2405. {
  2406. if ($this->debug > 0) {
  2407. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2408. }
  2409. if (!is_object($this->items[$item_id])) {
  2410. return false;
  2411. }
  2412. $oItem = $this->items[$item_id];
  2413. $prereq = $oItem->get_prereq_string();
  2414. if (empty($prereq)) {
  2415. return '';
  2416. }
  2417. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2418. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2419. // then simply return it (with the ITEM_ prefix).
  2420. //return 'ITEM_' . $prereq;
  2421. return $this->items[$prereq]->ref;
  2422. } else {
  2423. if (isset($this->refs_list[$prereq])) {
  2424. // It's a simple string item from which the ID can be found in the refs list,
  2425. // so we can transform it directly to an ID for export.
  2426. return $this->items[$this->refs_list[$prereq]]->ref;
  2427. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2428. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2429. } else {
  2430. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2431. // and replace them, one by one, by the internal IDs (chamilo db)
  2432. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2433. // by a space as well.
  2434. $find = array(
  2435. '&',
  2436. '|',
  2437. '~',
  2438. '=',
  2439. '<>',
  2440. '{',
  2441. '}',
  2442. '*',
  2443. '(',
  2444. ')'
  2445. );
  2446. $replace = array(
  2447. ' ',
  2448. ' ',
  2449. ' ',
  2450. ' ',
  2451. ' ',
  2452. ' ',
  2453. ' ',
  2454. ' ',
  2455. ' ',
  2456. ' '
  2457. );
  2458. $prereq_mod = str_replace($find, $replace, $prereq);
  2459. $ids = split(' ', $prereq_mod);
  2460. foreach ($ids as $id) {
  2461. $id = trim($id);
  2462. if (isset ($this->refs_list[$id])) {
  2463. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2464. }
  2465. }
  2466. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2467. return $prereq;
  2468. }
  2469. }
  2470. }
  2471. /**
  2472. * Returns the XML DOM document's node
  2473. * @param resource Reference to a list of objects to search for the given ITEM_*
  2474. * @param string The identifier to look for
  2475. * @return mixed The reference to the element found with that identifier. False if not found
  2476. */
  2477. public function get_scorm_xml_node(& $children, $id)
  2478. {
  2479. for ($i = 0; $i < $children->length; $i++) {
  2480. $item_temp = $children->item($i);
  2481. if ($item_temp->nodeName == 'item') {
  2482. if ($item_temp->getAttribute('identifier') == $id) {
  2483. return $item_temp;
  2484. }
  2485. }
  2486. $subchildren = $item_temp->childNodes;
  2487. if ($subchildren->length > 0) {
  2488. $val = $this->get_scorm_xml_node($subchildren, $id);
  2489. if (is_object($val)) {
  2490. return $val;
  2491. }
  2492. }
  2493. }
  2494. return false;
  2495. }
  2496. /**
  2497. * Returns a usable array of stats related to the current learnpath and user
  2498. * @return array Well-formatted array containing status for the current learnpath
  2499. */
  2500. public function get_stats()
  2501. {
  2502. if ($this->debug > 0) {
  2503. error_log('New LP - In learnpath::get_stats()', 0);
  2504. }
  2505. // TODO
  2506. }
  2507. /**
  2508. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2509. * the given course (for all learnpaths and all users)
  2510. * @param string Course code
  2511. * @return array Well-formatted array containing status for the course's learnpaths
  2512. */
  2513. public function get_stats_course($course)
  2514. {
  2515. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2516. // TODO
  2517. }
  2518. /**
  2519. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2520. * the given course and learnpath (for all users)
  2521. * @param string Course code
  2522. * @param integer Learnpath ID
  2523. * @return array Well-formatted array containing status for the specified learnpath
  2524. */
  2525. public function get_stats_lp($course, $lp)
  2526. {
  2527. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2528. // TODO
  2529. }
  2530. /**
  2531. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2532. * the given course, learnpath and user.
  2533. * @param string Course code
  2534. * @param integer Learnpath ID
  2535. * @param integer User ID
  2536. * @return array Well-formatted array containing status for the specified learnpath and user
  2537. */
  2538. public function get_stats_lp_user($course, $lp, $user)
  2539. {
  2540. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2541. // TODO
  2542. }
  2543. /**
  2544. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2545. * the given course and learnpath (for all users)
  2546. * @param string Course code
  2547. * @param integer User ID
  2548. * @return array Well-formatted array containing status for the user's learnpaths
  2549. */
  2550. public function get_stats_user($course, $user)
  2551. {
  2552. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2553. // TODO
  2554. }
  2555. /**
  2556. * Gets the status list for all LP's items
  2557. * @return array Array of [index] => [item ID => current status]
  2558. */
  2559. public function get_items_status_list()
  2560. {
  2561. if ($this->debug > 0) {
  2562. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2563. }
  2564. $list = array();
  2565. foreach ($this->ordered_items as $item_id) {
  2566. $list[] = array(
  2567. $item_id => $this->items[$item_id]->get_status()
  2568. );
  2569. }
  2570. return $list;
  2571. }
  2572. /**
  2573. * Return the number of interactions for the given learnpath Item View ID.
  2574. * This method can be used as static.
  2575. * @param integer Item View ID
  2576. * @param integer course id
  2577. * @return integer Number of interactions
  2578. */
  2579. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2580. {
  2581. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2582. $lp_iv_id = intval($lp_iv_id);
  2583. $course_id = intval($course_id);
  2584. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2585. $res = Database::query($sql);
  2586. $num = 0;
  2587. if (Database::num_rows($res)) {
  2588. $row = Database::fetch_array($res);
  2589. $num = $row[0];
  2590. }
  2591. return $num;
  2592. }
  2593. /**
  2594. * Return the interactions as an array for the given lp_iv_id.
  2595. * This method can be used as static.
  2596. * @param integer Learnpath Item View ID
  2597. * @return array
  2598. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2599. */
  2600. public static function get_iv_interactions_array($lp_iv_id = 0)
  2601. {
  2602. $course_id = api_get_course_int_id();
  2603. $list = array();
  2604. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2605. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2606. $res = Database::query($sql);
  2607. $num = Database :: num_rows($res);
  2608. if ($num > 0) {
  2609. $list[] = array(
  2610. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2611. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2612. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2613. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2614. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2615. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2616. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2617. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2618. );
  2619. while ($row = Database :: fetch_array($res)) {
  2620. $list[] = array(
  2621. 'order_id' => ($row['order_id'] + 1),
  2622. 'id' => urldecode($row['interaction_id']),
  2623. //urldecode because they often have %2F or stuff like that
  2624. 'type' => $row['interaction_type'],
  2625. 'time' => $row['completion_time'],
  2626. //'correct_responses' => $row['correct_responses'],
  2627. 'correct_responses' => '',
  2628. // Hide correct responses from students.
  2629. 'student_response' => $row['student_response'],
  2630. 'result' => $row['result'],
  2631. 'latency' => $row['latency']
  2632. );
  2633. }
  2634. }
  2635. return $list;
  2636. }
  2637. /**
  2638. * Return the number of objectives for the given learnpath Item View ID.
  2639. * This method can be used as static.
  2640. * @param integer Item View ID
  2641. * @return integer Number of objectives
  2642. */
  2643. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2644. {
  2645. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2646. $course_id = intval($course_id);
  2647. $lp_iv_id = intval($lp_iv_id);
  2648. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2649. //@todo seems that this always returns 0
  2650. $res = Database::query($sql);
  2651. $num = 0;
  2652. if (Database::num_rows($res)) {
  2653. $row = Database :: fetch_array($res);
  2654. $num = $row[0];
  2655. }
  2656. return $num;
  2657. }
  2658. /**
  2659. * Return the objectives as an array for the given lp_iv_id.
  2660. * This method can be used as static.
  2661. * @param integer Learnpath Item View ID
  2662. * @return array
  2663. * @todo Translate labels
  2664. */
  2665. public function get_iv_objectives_array($lp_iv_id = 0)
  2666. {
  2667. $course_id = api_get_course_int_id();
  2668. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2669. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2670. $res = Database::query($sql);
  2671. $num = Database :: num_rows($res);
  2672. $list = array();
  2673. if ($num > 0) {
  2674. $list[] = array(
  2675. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2676. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2677. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2678. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2679. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2680. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2681. );
  2682. while ($row = Database :: fetch_array($res)) {
  2683. $list[] = array(
  2684. 'order_id' => ($row['order_id'] + 1),
  2685. 'objective_id' => urldecode($row['objective_id']),
  2686. // urldecode() because they often have %2F or stuff like that.
  2687. 'score_raw' => $row['score_raw'],
  2688. 'score_max' => $row['score_max'],
  2689. 'score_min' => $row['score_min'],
  2690. 'status' => $row['status']
  2691. );
  2692. }
  2693. }
  2694. return $list;
  2695. }
  2696. /**
  2697. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2698. * used by get_html_toc() to be ready to display
  2699. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2700. */
  2701. public function get_toc()
  2702. {
  2703. if ($this->debug > 0) {
  2704. error_log('learnpath::get_toc()', 0);
  2705. }
  2706. $toc = array();
  2707. //echo "<pre>".print_r($this->items,true)."</pre>";
  2708. foreach ($this->ordered_items as $item_id) {
  2709. if ($this->debug > 2) {
  2710. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2711. }
  2712. // TODO: Change this link generation and use new function instead.
  2713. $toc[] = array(
  2714. 'id' => $item_id,
  2715. 'title' => $this->items[$item_id]->get_title(),
  2716. 'status' => $this->items[$item_id]->get_status(),
  2717. 'level' => $this->items[$item_id]->get_level(),
  2718. 'type' => $this->items[$item_id]->get_type(),
  2719. 'description' => $this->items[$item_id]->get_description(),
  2720. 'path' => $this->items[$item_id]->get_path(),
  2721. );
  2722. }
  2723. if ($this->debug > 2) {
  2724. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2725. }
  2726. return $toc;
  2727. }
  2728. /**
  2729. * Generate and return the table of contents for this learnpath. The JS
  2730. * table returned is used inside of scorm_api.php
  2731. * @return string A JS array vairiable construction
  2732. */
  2733. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2734. {
  2735. if ($this->debug > 0) {
  2736. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2737. }
  2738. $toc = $varname.' = new Array();';
  2739. //echo "<pre>".print_r($this->items,true)."</pre>";
  2740. foreach ($this->ordered_items as $item_id) {
  2741. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2742. }
  2743. if ($this->debug > 2) {
  2744. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2745. }
  2746. return $toc;
  2747. }
  2748. /**
  2749. * Gets the learning path type
  2750. * @param boolean Return the name? If false, return the ID. Default is false.
  2751. * @return mixed Type ID or name, depending on the parameter
  2752. */
  2753. public function get_type($get_name = false)
  2754. {
  2755. $res = false;
  2756. if ($this->debug > 0) {
  2757. error_log('New LP - In learnpath::get_type()', 0);
  2758. }
  2759. if (!empty ($this->type)) {
  2760. if ($get_name) {
  2761. // Get it from the lp_type table in main db.
  2762. } else {
  2763. $res = $this->type;
  2764. }
  2765. }
  2766. if ($this->debug > 2) {
  2767. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2768. }
  2769. return $res;
  2770. }
  2771. /**
  2772. * Gets the learning path type as static method
  2773. * @param boolean Return the name? If false, return the ID. Default is false.
  2774. * @return mixed Type ID or name, depending on the parameter
  2775. */
  2776. public static function get_type_static($lp_id = 0)
  2777. {
  2778. $course_id = api_get_course_int_id();
  2779. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2780. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2781. $res = Database::query($sql);
  2782. if ($res === false) {
  2783. return null;
  2784. }
  2785. if (Database :: num_rows($res) <= 0) {
  2786. return null;
  2787. }
  2788. $row = Database :: fetch_array($res);
  2789. return $row['lp_type'];
  2790. }
  2791. /**
  2792. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2793. * This method can be used as abstract and is recursive
  2794. * @param integer Learnpath ID
  2795. * @param integer Parent ID of the items to look for
  2796. * @return mixed Ordered list of item IDs or false on error
  2797. */
  2798. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2799. {
  2800. if (empty($course_id)) {
  2801. $course_id = api_get_course_int_id();
  2802. } else {
  2803. $course_id = intval($course_id);
  2804. }
  2805. $list = array();
  2806. if (empty ($lp)) {
  2807. return false;
  2808. }
  2809. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2810. $sql = "SELECT id FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent ORDER BY display_order";
  2811. $res = Database::query($sql);
  2812. while ($row = Database :: fetch_array($res)) {
  2813. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2814. $list[] = $row['id'];
  2815. foreach ($sublist as $item) {
  2816. $list[] = $item;
  2817. }
  2818. }
  2819. return $list;
  2820. }
  2821. /**
  2822. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2823. * @return string HTML TOC ready to display
  2824. */
  2825. public function get_html_toc($toc_list = null)
  2826. {
  2827. global $_configuration;
  2828. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2829. if ($this->debug > 0) {
  2830. error_log('In learnpath::get_html_toc()', 0);
  2831. }
  2832. if (empty($toc_list)) {
  2833. $toc_list = $this->get_toc();
  2834. }
  2835. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()).'</div>';
  2836. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2837. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2838. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2839. if ($this->get_lp_session_id() == api_get_session_id()) {
  2840. $html .= '<div id="actions_lp" class="actions_lp">';
  2841. $html .= '<div class="btn-group">';
  2842. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2843. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2844. 'Overview'
  2845. )."</a>";
  2846. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2847. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2848. 'Edit'
  2849. )."</a>";
  2850. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2851. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2852. $html .= '</div>';
  2853. $html .= '</div>';
  2854. }
  2855. }
  2856. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2857. require_once 'resourcelinker.inc.php';
  2858. // Temporary variables.
  2859. $mycurrentitemid = $this->get_current_item_id();
  2860. $color_counter = 0;
  2861. $i = 0;
  2862. foreach ($toc_list as $item) {
  2863. // TODO: Complete this
  2864. $icon_name = array(
  2865. 'not attempted' => 'notattempted.gif',
  2866. 'incomplete' => 'incomplete.png',
  2867. 'failed' => 'delete.png',
  2868. 'completed' => 'completed.png',
  2869. 'passed' => 'passed.png',
  2870. 'succeeded' => 'succeeded.png',
  2871. 'browsed' => 'completed.png',
  2872. );
  2873. $style = 'scorm_item';
  2874. $scorm_color_background = 'scorm_item';
  2875. $style_item = 'scorm_item';
  2876. $current = false;
  2877. if ($item['id'] == $this->current) {
  2878. $style = 'scorm_item_highlight';
  2879. $scorm_color_background = 'scorm_item_highlight';
  2880. } else {
  2881. if ($color_counter % 2 == 0) {
  2882. $scorm_color_background = 'scorm_item_1';
  2883. } else {
  2884. $scorm_color_background = 'scorm_item_2';
  2885. }
  2886. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2887. $scorm_color_background = ' scorm_item_section ';
  2888. }
  2889. }
  2890. if ($scorm_color_background != '') {
  2891. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2892. }
  2893. // The anchor will let us center the TOC on the currently viewed item &^D
  2894. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2895. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2896. $html .= '<a name="atoc_'.$item['id'].'" />';
  2897. } else {
  2898. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2899. }
  2900. $title = $item['title'];
  2901. if (empty ($title)) {
  2902. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2903. }
  2904. $title = Security::remove_XSS($title);
  2905. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2906. $url = $this->get_link('http', $item['id'], $toc_list);
  2907. $html .= '<a href="" onClick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;" >'.stripslashes(
  2908. $title
  2909. ).'</a>';
  2910. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2911. $html .= Display::return_icon('lp_dokeos_module.png', $title);
  2912. } elseif ($item['type'] == 'dir') {
  2913. $html .= stripslashes($title);
  2914. }
  2915. if ($item['type'] == 'quiz') {
  2916. if ($item['status'] == 'completed') {
  2917. $html .= "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2918. //$html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr($item['status'],0,1)."' width='14' />";
  2919. } else {
  2920. $html .= "&nbsp;".Display::return_icon($icon_name['not attempted'], substr('not attempted', 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2921. // $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name['not attempted']."' alt='".substr('not attempted', 0, 1)."' width='14' />";
  2922. }
  2923. } else {
  2924. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2925. $html .= "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2926. //$html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr($item['status'], 0, 1)."' width='14' />";
  2927. }
  2928. }
  2929. $html .= "</div>";
  2930. if ($scorm_color_background != '') {
  2931. $html .= '</div>';
  2932. }
  2933. $color_counter++;
  2934. }
  2935. $html .= "</div>";
  2936. return $html;
  2937. }
  2938. /**
  2939. * Gets the learnpath maker name - generally the editor's name
  2940. * @return string Learnpath maker name
  2941. */
  2942. public function get_maker()
  2943. {
  2944. if ($this->debug > 0) {
  2945. error_log('New LP - In learnpath::get_maker()', 0);
  2946. }
  2947. if (!empty ($this->maker)) {
  2948. return $this->maker;
  2949. } else {
  2950. return '';
  2951. }
  2952. }
  2953. /**
  2954. * Gets the user-friendly message stored in $this->message
  2955. * @return string Message
  2956. */
  2957. public function get_message()
  2958. {
  2959. if ($this->debug > 0) {
  2960. error_log('New LP - In learnpath::get_message()', 0);
  2961. }
  2962. return $this->message;
  2963. }
  2964. /**
  2965. * Gets the learnpath name/title
  2966. * @return string Learnpath name/title
  2967. */
  2968. public function get_name()
  2969. {
  2970. if ($this->debug > 0) {
  2971. error_log('New LP - In learnpath::get_name()', 0);
  2972. }
  2973. if (!empty ($this->name)) {
  2974. return $this->name;
  2975. } else {
  2976. return 'N/A';
  2977. }
  2978. }
  2979. /**
  2980. * Gets a link to the resource from the present location, depending on item ID.
  2981. * @param string Type of link expected
  2982. * @param integer Learnpath item ID
  2983. * @return string Link to the lp_item resource
  2984. */
  2985. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  2986. {
  2987. $course_id = $this->get_course_int_id();
  2988. if ($this->debug > 0) {
  2989. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  2990. }
  2991. if (empty($item_id)) {
  2992. if ($this->debug > 2) {
  2993. error_log(
  2994. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  2995. ),
  2996. 0
  2997. );
  2998. }
  2999. $item_id = $this->get_current_item_id();
  3000. }
  3001. if (empty($item_id)) {
  3002. if ($this->debug > 2) {
  3003. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3004. }
  3005. //still empty, this means there was no item_id given and we are not in an object context or
  3006. //the object property is empty, return empty link
  3007. $item_id = $this->first();
  3008. return '';
  3009. }
  3010. $file = '';
  3011. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3012. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3013. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3014. $item_id = Database::escape_string($item_id);
  3015. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3016. FROM $lp_table l
  3017. INNER JOIN $lp_item_table li
  3018. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3019. WHERE li.id = $item_id ";
  3020. if ($this->debug > 2) {
  3021. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3022. }
  3023. $res = Database::query($sql);
  3024. if (Database :: num_rows($res) > 0) {
  3025. $row = Database :: fetch_array($res);
  3026. $lp_type = $row['ltype'];
  3027. $lp_path = $row['lpath'];
  3028. $lp_item_type = $row['litype'];
  3029. $lp_item_path = $row['lipath'];
  3030. $lp_item_params = $row['liparams'];
  3031. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3032. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3033. }
  3034. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3035. if ($type == 'http') {
  3036. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3037. } else {
  3038. $course_path = $sys_course_path; //system path
  3039. }
  3040. // 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.
  3041. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3042. $lp_type = 1;
  3043. }
  3044. if ($this->debug > 2) {
  3045. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3046. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3047. }
  3048. // Now go through the specific cases to get the end of the path
  3049. // @todo Use constants instead of int values.
  3050. switch ($lp_type) {
  3051. case 1 :
  3052. if ($lp_item_type == 'dokeos_chapter') {
  3053. $file = 'lp_content.php?type=dir';
  3054. } else {
  3055. require_once 'resourcelinker.inc.php';
  3056. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3057. if ($this->debug > 0) {
  3058. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3059. }
  3060. if ($lp_item_type == 'link') {
  3061. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3062. if (is_youtube_link($file)) {
  3063. $src = get_youtube_video_id($file);
  3064. $file = 'embed.php?type=youtube&src='.$src;
  3065. }
  3066. if (isVimeoLink($file)) {
  3067. $src = getVimeoLinkId($file);
  3068. $file = 'embed.php?type=vimeo&src='.$src;
  3069. }
  3070. } else {
  3071. // check how much attempts of a exercise exits in lp
  3072. $lp_item_id = $this->get_current_item_id();
  3073. $lp_view_id = $this->get_view_id();
  3074. $prevent_reinit = null;
  3075. if (isset($this->items[$this->current])) {
  3076. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3077. }
  3078. if (empty($provided_toc)) {
  3079. if ($this->debug > 0) {
  3080. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3081. }
  3082. $list = $this->get_toc();
  3083. } else {
  3084. if ($this->debug > 0) {
  3085. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3086. }
  3087. $list = $provided_toc;
  3088. }
  3089. $type_quiz = false;
  3090. foreach ($list as $toc) {
  3091. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3092. $type_quiz = true;
  3093. }
  3094. }
  3095. if ($type_quiz) {
  3096. $lp_item_id = Database::escape_string($lp_item_id);
  3097. $lp_view_id = Database::escape_string($lp_view_id);
  3098. $sql = "SELECT count(*) FROM $lp_item_view_table
  3099. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3100. $result = Database::query($sql);
  3101. $row_count = Database :: fetch_row($result);
  3102. $count_item_view = (int)$row_count[0];
  3103. $not_multiple_attempt = 0;
  3104. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3105. $not_multiple_attempt = 1;
  3106. }
  3107. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3108. }
  3109. $tmp_array = explode('/', $file);
  3110. $document_name = $tmp_array[count($tmp_array) - 1];
  3111. if (strpos($document_name, '_DELETED_')) {
  3112. $file = 'blank.php?error=document_deleted';
  3113. }
  3114. }
  3115. }
  3116. break;
  3117. case 2 :
  3118. if ($this->debug > 2) {
  3119. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3120. }
  3121. if ($lp_item_type != 'dir') {
  3122. // Quite complex here:
  3123. // We want to make sure 'http://' (and similar) links can
  3124. // be loaded as is (withouth the Chamilo path in front) but
  3125. // some contents use this form: resource.htm?resource=http://blablabla
  3126. // which means we have to find a protocol at the path's start, otherwise
  3127. // it should not be considered as an external URL.
  3128. //if ($this->prerequisites_match($item_id)) {
  3129. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3130. if ($this->debug > 2) {
  3131. error_log(
  3132. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3133. 0
  3134. );
  3135. }
  3136. // Distant url, return as is.
  3137. $file = $lp_item_path;
  3138. } else {
  3139. if ($this->debug > 2) {
  3140. error_log(
  3141. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3142. 0
  3143. );
  3144. }
  3145. // Prevent getting untranslatable urls.
  3146. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3147. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3148. // Prepare the path.
  3149. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3150. // TODO: Fix this for urls with protocol header.
  3151. $file = str_replace('//', '/', $file);
  3152. $file = str_replace(':/', '://', $file);
  3153. if (substr($lp_path, -1) == '/') {
  3154. $lp_path = substr($lp_path, 0, -1);
  3155. }
  3156. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3157. // if file not found.
  3158. $decoded = html_entity_decode($lp_item_path);
  3159. list ($decoded) = explode('?', $decoded);
  3160. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3161. require_once 'resourcelinker.inc.php';
  3162. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3163. if (empty($file)) {
  3164. $file = 'blank.php?error=document_not_found';
  3165. } else {
  3166. $tmp_array = explode('/', $file);
  3167. $document_name = $tmp_array[count($tmp_array) - 1];
  3168. if (strpos($document_name, '_DELETED_')) {
  3169. $file = 'blank.php?error=document_deleted';
  3170. } else {
  3171. $file = 'blank.php?error=document_not_found';
  3172. }
  3173. }
  3174. } else {
  3175. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3176. }
  3177. }
  3178. }
  3179. //}else{
  3180. //prerequisites did not match
  3181. //$file = 'blank.php';
  3182. //}
  3183. // We want to use parameters if they were defined in the imsmanifest
  3184. if (strpos($file, 'blank.php') === false) {
  3185. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3186. }
  3187. } else {
  3188. $file = 'lp_content.php?type=dir';
  3189. }
  3190. break;
  3191. case 3 :
  3192. if ($this->debug > 2) {
  3193. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3194. }
  3195. // Formatting AICC HACP append URL.
  3196. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3197. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3198. ).'&';
  3199. if ($lp_item_type != 'dir') {
  3200. // Quite complex here:
  3201. // We want to make sure 'http://' (and similar) links can
  3202. // be loaded as is (withouth the Chamilo path in front) but
  3203. // some contents use this form: resource.htm?resource=http://blablabla
  3204. // which means we have to find a protocol at the path's start, otherwise
  3205. // it should not be considered as an external URL.
  3206. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3207. if ($this->debug > 2) {
  3208. error_log(
  3209. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3210. 0
  3211. );
  3212. }
  3213. // Distant url, return as is.
  3214. $file = $lp_item_path;
  3215. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3216. /*
  3217. if (stristr($file,'<servername>') !== false) {
  3218. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3219. }
  3220. */
  3221. if (stripos($file, '<servername>') !== false) {
  3222. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3223. $web_course_path = str_replace(
  3224. 'https://',
  3225. '',
  3226. str_replace('http://', '', $course_path)
  3227. );
  3228. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3229. }
  3230. //
  3231. $file .= $aicc_append;
  3232. } else {
  3233. if ($this->debug > 2) {
  3234. error_log(
  3235. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3236. 0
  3237. );
  3238. }
  3239. // Prevent getting untranslatable urls.
  3240. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3241. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3242. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3243. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3244. // TODO: Fix this for urls with protocol header.
  3245. $file = str_replace('//', '/', $file);
  3246. $file = str_replace(':/', '://', $file);
  3247. $file .= $aicc_append;
  3248. }
  3249. } else {
  3250. $file = 'lp_content.php?type=dir';
  3251. }
  3252. break;
  3253. case 4 :
  3254. break;
  3255. default :
  3256. break;
  3257. }
  3258. }
  3259. if ($this->debug > 2) {
  3260. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3261. }
  3262. return $file;
  3263. }
  3264. /**
  3265. * Gets the latest usable view or generate a new one
  3266. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3267. * @return integer DB lp_view id
  3268. */
  3269. public function get_view($attempt_num = 0)
  3270. {
  3271. if ($this->debug > 0) {
  3272. error_log('New LP - In learnpath::get_view()', 0);
  3273. }
  3274. $search = '';
  3275. // Use $attempt_num to enable multi-views management (disabled so far).
  3276. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3277. $search = 'AND view_count = '.$attempt_num;
  3278. }
  3279. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3280. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3281. $course_id = api_get_course_int_id();
  3282. $sql = "SELECT id, view_count FROM $lp_view_table
  3283. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3284. )." ".$search.
  3285. " ORDER BY view_count DESC";
  3286. $res = Database::query($sql);
  3287. if (Database :: num_rows($res) > 0) {
  3288. $row = Database :: fetch_array($res);
  3289. $this->lp_view_id = $row['id'];
  3290. } else {
  3291. // There is no database record, create one.
  3292. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3293. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3294. $res = Database::query($sql);
  3295. $id = Database :: insert_id();
  3296. $this->lp_view_id = $id;
  3297. }
  3298. return $this->lp_view_id;
  3299. }
  3300. /**
  3301. * Gets the current view id
  3302. * @return integer View ID (from lp_view)
  3303. */
  3304. public function get_view_id()
  3305. {
  3306. if ($this->debug > 0) {
  3307. error_log('New LP - In learnpath::get_view_id()', 0);
  3308. }
  3309. if (!empty ($this->lp_view_id)) {
  3310. return $this->lp_view_id;
  3311. } else {
  3312. return 0;
  3313. }
  3314. }
  3315. /**
  3316. * Gets the update queue
  3317. * @return array Array containing IDs of items to be updated by JavaScript
  3318. */
  3319. public function get_update_queue()
  3320. {
  3321. if ($this->debug > 0) {
  3322. error_log('New LP - In learnpath::get_update_queue()', 0);
  3323. }
  3324. return $this->update_queue;
  3325. }
  3326. /**
  3327. * Gets the user ID
  3328. * @return integer User ID
  3329. */
  3330. public function get_user_id()
  3331. {
  3332. if ($this->debug > 0) {
  3333. error_log('New LP - In learnpath::get_user_id()', 0);
  3334. }
  3335. if (!empty ($this->user_id)) {
  3336. return $this->user_id;
  3337. } else {
  3338. return false;
  3339. }
  3340. }
  3341. /**
  3342. * Checks if any of the items has an audio element attached
  3343. * @return bool True or false
  3344. */
  3345. public function has_audio()
  3346. {
  3347. if ($this->debug > 1) {
  3348. error_log('New LP - In learnpath::has_audio()', 0);
  3349. }
  3350. $has = false;
  3351. foreach ($this->items as $i => $item) {
  3352. if (!empty ($this->items[$i]->audio)) {
  3353. $has = true;
  3354. break;
  3355. }
  3356. }
  3357. return $has;
  3358. }
  3359. /**
  3360. * Logs a message into a file
  3361. * @param string Message to log
  3362. * @return boolean True on success, false on error or if msg empty
  3363. */
  3364. public function log($msg)
  3365. {
  3366. if ($this->debug > 0) {
  3367. error_log('New LP - In learnpath::log()', 0);
  3368. }
  3369. // TODO
  3370. $this->error .= $msg;
  3371. return true;
  3372. }
  3373. /**
  3374. * Moves an item up and down at its level
  3375. * @param integer Item to move up and down
  3376. * @param string Direction 'up' or 'down'
  3377. * @return integer New display order, or false on error
  3378. */
  3379. public function move_item($id, $direction)
  3380. {
  3381. $course_id = api_get_course_int_id();
  3382. if ($this->debug > 0) {
  3383. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3384. }
  3385. if (empty ($id) or empty ($direction)) {
  3386. return false;
  3387. }
  3388. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3389. $sql_sel = "SELECT *
  3390. FROM ".$tbl_lp_item."
  3391. WHERE c_id = ".$course_id." AND id = ".$id;
  3392. $res_sel = Database::query($sql_sel);
  3393. // Check if elem exists.
  3394. if (Database :: num_rows($res_sel) < 1) {
  3395. return false;
  3396. }
  3397. // Gather data.
  3398. $row = Database :: fetch_array($res_sel);
  3399. $previous = $row['previous_item_id'];
  3400. $next = $row['next_item_id'];
  3401. $display = $row['display_order'];
  3402. $parent = $row['parent_item_id'];
  3403. $lp = $row['lp_id'];
  3404. // Update the item (switch with previous/next one).
  3405. switch ($direction) {
  3406. case 'up' :
  3407. if ($this->debug > 2) {
  3408. error_log('Movement up detected', 0);
  3409. }
  3410. if ($display <= 1) { /*do nothing*/
  3411. } else {
  3412. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3413. WHERE c_id = ".$course_id." AND id = $previous";
  3414. if ($this->debug > 2) {
  3415. error_log('Selecting previous: '.$sql_sel2, 0);
  3416. }
  3417. $res_sel2 = Database::query($sql_sel2);
  3418. if (Database :: num_rows($res_sel2) < 1) {
  3419. $previous_previous = 0;
  3420. }
  3421. // Gather data.
  3422. $row2 = Database :: fetch_array($res_sel2);
  3423. $previous_previous = $row2['previous_item_id'];
  3424. // Update previous_previous item (switch "next" with current).
  3425. if ($previous_previous != 0) {
  3426. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3427. if ($this->debug > 2) {
  3428. error_log($sql_upd2, 0);
  3429. }
  3430. $res_upd2 = Database::query($sql_upd2);
  3431. }
  3432. // Update previous item (switch with current).
  3433. if ($previous != 0) {
  3434. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3435. WHERE c_id = ".$course_id." AND id = $previous";
  3436. if ($this->debug > 2) {
  3437. error_log($sql_upd2, 0);
  3438. }
  3439. $res_upd2 = Database::query($sql_upd2);
  3440. }
  3441. // Update current item (switch with previous).
  3442. if ($id != 0) {
  3443. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3444. WHERE c_id = ".$course_id." AND id = $id";
  3445. if ($this->debug > 2) {
  3446. error_log($sql_upd2, 0);
  3447. }
  3448. $res_upd2 = Database::query($sql_upd2);
  3449. }
  3450. // Update next item (new previous item).
  3451. if ($next != 0) {
  3452. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3453. WHERE c_id = ".$course_id." AND id = $next";
  3454. if ($this->debug > 2) {
  3455. error_log($sql_upd2, 0);
  3456. }
  3457. $res_upd2 = Database::query($sql_upd2);
  3458. }
  3459. $display = $display - 1;
  3460. }
  3461. break;
  3462. case 'down' :
  3463. if ($this->debug > 2) {
  3464. error_log('Movement down detected', 0);
  3465. }
  3466. if ($next == 0) { /* Do nothing. */
  3467. } else {
  3468. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3469. if ($this->debug > 2) {
  3470. error_log('Selecting next: '.$sql_sel2, 0);
  3471. }
  3472. $res_sel2 = Database::query($sql_sel2);
  3473. if (Database :: num_rows($res_sel2) < 1) {
  3474. $next_next = 0;
  3475. }
  3476. // Gather data.
  3477. $row2 = Database :: fetch_array($res_sel2);
  3478. $next_next = $row2['next_item_id'];
  3479. // Update previous item (switch with current).
  3480. if ($previous != 0) {
  3481. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3482. WHERE c_id = ".$course_id." AND id = $previous";
  3483. $res_upd2 = Database::query($sql_upd2);
  3484. }
  3485. // Update current item (switch with previous).
  3486. if ($id != 0) {
  3487. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3488. WHERE c_id = ".$course_id." AND id = $id";
  3489. $res_upd2 = Database::query($sql_upd2);
  3490. }
  3491. // Update next item (new previous item).
  3492. if ($next != 0) {
  3493. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3494. WHERE c_id = ".$course_id." AND id = $next";
  3495. $res_upd2 = Database::query($sql_upd2);
  3496. }
  3497. // Update next_next item (switch "previous" with current).
  3498. if ($next_next != 0) {
  3499. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3500. WHERE c_id = ".$course_id." AND id = $next_next";
  3501. $res_upd2 = Database::query($sql_upd2);
  3502. }
  3503. $display = $display + 1;
  3504. }
  3505. break;
  3506. default :
  3507. return false;
  3508. }
  3509. return $display;
  3510. }
  3511. /**
  3512. * Move a learnpath up (display_order)
  3513. * @param integer Learnpath ID
  3514. */
  3515. public function move_up($lp_id)
  3516. {
  3517. $course_id = api_get_course_int_id();
  3518. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3519. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3520. $res = Database::query($sql);
  3521. if ($res === false) {
  3522. return false;
  3523. }
  3524. $lps = array();
  3525. $lp_order = array();
  3526. $num = Database :: num_rows($res);
  3527. // First check the order is correct, globally (might be wrong because
  3528. // of versions < 1.8.4)
  3529. if ($num > 0) {
  3530. $i = 1;
  3531. while ($row = Database :: fetch_array($res)) {
  3532. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3533. $need_fix = true;
  3534. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3535. $res_u = Database::query($sql_u);
  3536. }
  3537. $row['display_order'] = $i;
  3538. $lps[$row['id']] = $row;
  3539. $lp_order[$i] = $row['id'];
  3540. $i++;
  3541. }
  3542. }
  3543. if ($num > 1) { // If there's only one element, no need to sort.
  3544. $order = $lps[$lp_id]['display_order'];
  3545. if ($order > 1) { // If it's the first element, no need to move up.
  3546. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3547. $res_u1 = Database::query($sql_u1);
  3548. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3549. $res_u2 = Database::query($sql_u2);
  3550. }
  3551. }
  3552. }
  3553. /**
  3554. * Move a learnpath down (display_order)
  3555. * @param integer Learnpath ID
  3556. */
  3557. public function move_down($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 WHERE c_id = ".$course_id." ORDER BY display_order";
  3562. $res = Database::query($sql);
  3563. if ($res === false) {
  3564. return false;
  3565. }
  3566. $lps = array();
  3567. $lp_order = array();
  3568. $num = Database :: num_rows($res);
  3569. $max = 0;
  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. $max = $i;
  3576. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3577. $need_fix = true;
  3578. $sql_u = "UPDATE $lp_table SET display_order = $i
  3579. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3580. $res_u = Database::query($sql_u);
  3581. }
  3582. $row['display_order'] = $i;
  3583. $lps[$row['id']] = $row;
  3584. $lp_order[$i] = $row['id'];
  3585. $i++;
  3586. }
  3587. }
  3588. if ($num > 1) { // If there's only one element, no need to sort.
  3589. $order = $lps[$lp_id]['display_order'];
  3590. if ($order < $max) { // If it's the first element, no need to move up.
  3591. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3592. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3593. $res_u1 = Database::query($sql_u1);
  3594. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3595. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3596. $res_u2 = Database::query($sql_u2);
  3597. }
  3598. }
  3599. }
  3600. /**
  3601. * Updates learnpath attributes to point to the next element
  3602. * The last part is similar to set_current_item but processing the other way around
  3603. */
  3604. public function next()
  3605. {
  3606. if ($this->debug > 0) {
  3607. error_log('New LP - In learnpath::next()', 0);
  3608. }
  3609. $this->last = $this->get_current_item_id();
  3610. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3611. $this->autocomplete_parents($this->last);
  3612. $new_index = $this->get_next_index();
  3613. if ($this->debug > 2) {
  3614. error_log('New LP - New index: '.$new_index, 0);
  3615. }
  3616. $this->index = $new_index;
  3617. if ($this->debug > 2) {
  3618. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3619. }
  3620. $this->current = $this->ordered_items[$new_index];
  3621. if ($this->debug > 2) {
  3622. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3623. }
  3624. }
  3625. /**
  3626. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3627. * class, this might be redefined to allow several behaviours depending on the document type.
  3628. * @param integer Resource ID
  3629. * @return boolean True on success, false otherwise
  3630. */
  3631. public function open($id)
  3632. {
  3633. if ($this->debug > 0) {
  3634. error_log('New LP - In learnpath::open()', 0);
  3635. }
  3636. // TODO:
  3637. // set the current resource attribute to this resource
  3638. // switch on element type (redefine in child class?)
  3639. // set status for this item to "opened"
  3640. // start timer
  3641. // initialise score
  3642. $this->index = 0; //or = the last item seen (see $this->last)
  3643. }
  3644. /**
  3645. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3646. * and the prerequisite string on error.
  3647. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3648. * @param integer Optional item ID. If none given, uses the current open item.
  3649. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3650. */
  3651. public function prerequisites_match($item = null)
  3652. {
  3653. if ($this->debug > 0) {
  3654. error_log('In learnpath::prerequisites_match()', 0);
  3655. }
  3656. if (empty($item)) {
  3657. $item = $this->current;
  3658. }
  3659. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3660. if ($this->type == 2) {
  3661. //Getting prereq from scorm
  3662. $prereq_string = $this->get_scorm_prereq_string($item);
  3663. } else {
  3664. $prereq_string = $this->items[$item]->get_prereq_string();
  3665. }
  3666. if (empty($prereq_string)) {
  3667. return true;
  3668. }
  3669. // Clean spaces.
  3670. $prereq_string = str_replace(' ', '', $prereq_string);
  3671. if ($this->debug > 0) {
  3672. error_log('Found prereq_string: '.$prereq_string, 0);
  3673. }
  3674. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3675. $result = $this->items[$item]->parse_prereq(
  3676. $prereq_string,
  3677. $this->items,
  3678. $this->refs_list,
  3679. $this->get_user_id()
  3680. );
  3681. if ($result === false) {
  3682. $this->set_error_msg($this->items[$item]->prereq_alert);
  3683. }
  3684. } else {
  3685. $result = true;
  3686. if ($this->debug > 1) {
  3687. error_log('$this->items['.$item.'] was not an object', 0);
  3688. }
  3689. }
  3690. if ($this->debug > 1) {
  3691. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3692. }
  3693. return $result;
  3694. }
  3695. /**
  3696. * Updates learnpath attributes to point to the previous element
  3697. * The last part is similar to set_current_item but processing the other way around
  3698. */
  3699. public function previous()
  3700. {
  3701. if ($this->debug > 0) {
  3702. error_log('New LP - In learnpath::previous()', 0);
  3703. }
  3704. $this->last = $this->get_current_item_id();
  3705. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3706. $this->autocomplete_parents($this->last);
  3707. $new_index = $this->get_previous_index();
  3708. $this->index = $new_index;
  3709. $this->current = $this->ordered_items[$new_index];
  3710. }
  3711. /**
  3712. * Publishes a learnpath. This basically means show or hide the learnpath
  3713. * to normal users.
  3714. * Can be used as abstract
  3715. * @param integer Learnpath ID
  3716. * @param string New visibility
  3717. */
  3718. public function toggle_visibility($lp_id, $set_visibility = 1, $course_id = null)
  3719. {
  3720. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3721. $action = 'visible';
  3722. if ($set_visibility != 1) {
  3723. $action = 'invisible';
  3724. }
  3725. if (!empty($course_id)) {
  3726. $course = api_get_course_info_by_id($course_id);
  3727. } else {
  3728. $course = api_get_course_info();
  3729. }
  3730. return api_item_property_update($course, TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3731. }
  3732. /**
  3733. * Publishes a learnpath. This basically means show or hide the learnpath
  3734. * on the course homepage
  3735. * Can be used as abstract
  3736. * @param integer Learnpath id
  3737. * @param string New visibility (v/s - visible/invisible)
  3738. */
  3739. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3740. {
  3741. $course_id = api_get_course_int_id();
  3742. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3743. $lp_id = Database::escape_string($lp_id);
  3744. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3745. $result = Database::query($sql);
  3746. if (Database::num_rows($result)) {
  3747. $row = Database :: fetch_array($result);
  3748. $name = Text::domesticate($row['name']);
  3749. if ($set_visibility == 'i') {
  3750. $s = $name." ".get_lang('LearnpathNotPublished');
  3751. $dialogBox = $s;
  3752. $v = 0;
  3753. }
  3754. if ($set_visibility == 'v') {
  3755. $s = $name." ".get_lang('LearnpathPublished');
  3756. $dialogBox = $s;
  3757. $v = 1;
  3758. }
  3759. } else {
  3760. return false;
  3761. }
  3762. $session_id = api_get_session_id();
  3763. $session_condition = api_get_session_condition($session_id);
  3764. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3765. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3766. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3767. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3768. $result = Database::query($sql);
  3769. $num = Database :: num_rows($result);
  3770. if (($set_visibility == 'i') && ($num > 0)) {
  3771. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3772. Database::query($sql);
  3773. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3774. Database::query($sql);
  3775. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3776. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3777. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3778. Database::query($sql);
  3779. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3780. $sql = "UPDATE $tbl_tool SET c_id = $course_id, name = '$name', link = '$link', image = 'scormbuilder.gif', visibility = '$v', admin = '0', address = 'pastillegris.gif', added_tool = 0, session_id = $session_id
  3781. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3782. Database::query($sql);
  3783. } else {
  3784. // Parameter and database incompatible, do nothing, exit.
  3785. return false;
  3786. }
  3787. }
  3788. /**
  3789. * Restart the whole learnpath. Return the URL of the first element.
  3790. * Make sure the results are saved with anoter method. This method should probably be
  3791. * redefined in children classes.
  3792. * To use a similar method statically, use the create_new_attempt() method
  3793. * @return string URL to load in the viewer
  3794. */
  3795. public function restart()
  3796. {
  3797. if ($this->debug > 0) {
  3798. error_log('New LP - In learnpath::restart()', 0);
  3799. }
  3800. // TODO
  3801. // Call autosave method to save the current progress.
  3802. //$this->index = 0;
  3803. $session_id = api_get_session_id();
  3804. $course_id = api_get_course_int_id();
  3805. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3806. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3807. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3808. if ($this->debug > 2) {
  3809. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3810. }
  3811. $res = Database::query($sql);
  3812. if ($view_id = Database :: insert_id($res)) {
  3813. $this->lp_view_id = $view_id;
  3814. $this->attempt = $this->attempt + 1;
  3815. } else {
  3816. $this->error = 'Could not insert into item_view table...';
  3817. return false;
  3818. }
  3819. $this->autocomplete_parents($this->current);
  3820. foreach ($this->items as $index => $dummy) {
  3821. $this->items[$index]->restart();
  3822. $this->items[$index]->set_lp_view($this->lp_view_id);
  3823. }
  3824. $this->first();
  3825. return true;
  3826. }
  3827. /**
  3828. * Saves the current item
  3829. * @return boolean
  3830. */
  3831. public function save_current()
  3832. {
  3833. if ($this->debug > 0) {
  3834. error_log('learnpath::save_current()', 0);
  3835. }
  3836. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3837. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3838. if ($this->debug > 2) {
  3839. error_log('New LP - save_current() saving item '.$this->current, 0);
  3840. }
  3841. if ($this->debug > 2) {
  3842. error_log(''.print_r($this->items, true), 0);
  3843. }
  3844. if (is_object($this->items[$this->current])) {
  3845. //$res = $this->items[$this->current]->save(false);
  3846. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3847. $this->autocomplete_parents($this->current);
  3848. $status = $this->items[$this->current]->get_status();
  3849. $this->append_message('new_item_status: '.$status);
  3850. $this->update_queue[$this->current] = $status;
  3851. return $res;
  3852. }
  3853. return false;
  3854. }
  3855. /**
  3856. * Saves the given item
  3857. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3858. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3859. * @return boolean
  3860. */
  3861. public function save_item($item_id = null, $from_outside = true)
  3862. {
  3863. $debug = $this->debug;
  3864. if ($debug) {
  3865. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3866. }
  3867. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3868. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3869. if (empty($item_id)) {
  3870. $item_id = intval($_REQUEST['id']);
  3871. }
  3872. if (empty($item_id)) {
  3873. $item_id = $this->get_current_item_id();
  3874. }
  3875. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3876. if ($debug) {
  3877. error_log('Object exists');
  3878. }
  3879. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3880. if ($debug) {
  3881. error_log('update_queue before:');
  3882. error_log(print_r($this->update_queue, 1));
  3883. }
  3884. $this->autocomplete_parents($item_id);
  3885. $status = $this->items[$item_id]->get_status();
  3886. $this->update_queue[$item_id] = $status;
  3887. if ($debug) {
  3888. error_log('get_status(): '.$status);
  3889. error_log('update_queue after:');
  3890. error_log(print_r($this->update_queue, 1));
  3891. }
  3892. return $res;
  3893. }
  3894. return false;
  3895. }
  3896. /**
  3897. * Saves the last item seen's ID only in case
  3898. */
  3899. public function save_last()
  3900. {
  3901. $course_id = api_get_course_int_id();
  3902. if ($this->debug > 0) {
  3903. error_log('New LP - In learnpath::save_last()', 0);
  3904. }
  3905. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3906. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3907. if (isset($this->current)) {
  3908. if ($this->debug > 2) {
  3909. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3910. }
  3911. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3912. WHERE c_id = $course_id AND
  3913. lp_id = ".$this->get_id()." AND
  3914. user_id = ".$this->get_user_id()." ".$session_condition;
  3915. if ($this->debug > 2) {
  3916. error_log('New LP - Saving last item seen : '.$sql, 0);
  3917. }
  3918. $res = Database::query($sql);
  3919. }
  3920. // Save progress.
  3921. list($progress, $text) = $this->get_progress_bar_text('%');
  3922. if ($progress >= 0 && $progress <= 100) {
  3923. $progress = (int)$progress;
  3924. $sql = "UPDATE $table SET progress = $progress
  3925. WHERE c_id = ".$course_id." AND
  3926. lp_id = ".$this->get_id()." AND
  3927. user_id = ".$this->get_user_id()." ".$session_condition;
  3928. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3929. $this->progress_db = $progress;
  3930. }
  3931. }
  3932. /**
  3933. * Sets the current item ID (checks if valid and authorized first)
  3934. * @param integer New item ID. If not given or not authorized, defaults to current
  3935. */
  3936. public function set_current_item($item_id = null)
  3937. {
  3938. if ($this->debug > 0) {
  3939. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3940. }
  3941. if (empty ($item_id)) {
  3942. if ($this->debug > 2) {
  3943. error_log('New LP - No new current item given, ignore...', 0);
  3944. }
  3945. // Do nothing.
  3946. } else {
  3947. if ($this->debug > 2) {
  3948. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3949. }
  3950. if (is_numeric($item_id)) {
  3951. $item_id = Database::escape_string($item_id);
  3952. // TODO: Check in database here.
  3953. $this->last = $this->current;
  3954. $this->current = $item_id;
  3955. // TODO: Update $this->index as well.
  3956. foreach ($this->ordered_items as $index => $item) {
  3957. if ($item == $this->current) {
  3958. $this->index = $index;
  3959. break;
  3960. }
  3961. }
  3962. if ($this->debug > 2) {
  3963. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  3964. }
  3965. } else {
  3966. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  3967. }
  3968. }
  3969. }
  3970. /**
  3971. * Sets the encoding
  3972. * @param string New encoding
  3973. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3974. */
  3975. public function set_encoding($enc = 'UTF-8')
  3976. {
  3977. if ($this->debug > 0) {
  3978. error_log('New LP - In learnpath::set_encoding()', 0);
  3979. }
  3980. $course_id = api_get_course_int_id();
  3981. /* // Deprecated code (Chamilo 1.8.8).
  3982. $enc = strtoupper($enc);
  3983. $encodings = array (
  3984. 'UTF-8',
  3985. 'ISO-8859-1',
  3986. 'ISO-8859-15',
  3987. 'cp1251',
  3988. 'cp1252',
  3989. 'KOI8-R',
  3990. 'BIG5',
  3991. 'GB2312',
  3992. 'Shift_JIS',
  3993. 'EUC-JP',
  3994. ''
  3995. );
  3996. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  3997. $lp = $this->get_id();
  3998. if ($lp != 0) {
  3999. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4000. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4001. $res = Database::query($sql);
  4002. return $res;
  4003. }
  4004. }
  4005. return false;
  4006. */
  4007. $enc = api_refine_encoding_id($enc);
  4008. if (empty($enc)) {
  4009. $enc = api_get_system_encoding();
  4010. }
  4011. if (api_is_encoding_supported($enc)) {
  4012. $lp = $this->get_id();
  4013. if ($lp != 0) {
  4014. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4015. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4016. $res = Database::query($sql);
  4017. return $res;
  4018. }
  4019. }
  4020. return false;
  4021. }
  4022. /**
  4023. * Sets the JS lib setting in the database directly.
  4024. * This is the JavaScript library file this lp needs to load on startup
  4025. * @param string Proximity setting
  4026. * @return boolean True on update success. False otherwise.
  4027. */
  4028. public function set_jslib($lib = '')
  4029. {
  4030. if ($this->debug > 0) {
  4031. error_log('New LP - In learnpath::set_jslib()', 0);
  4032. }
  4033. $lp = $this->get_id();
  4034. $course_id = api_get_course_int_id();
  4035. if ($lp != 0) {
  4036. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4037. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4038. $res = Database::query($sql);
  4039. return $res;
  4040. } else {
  4041. return false;
  4042. }
  4043. }
  4044. /**
  4045. * Sets the name of the LP maker (publisher) (and save)
  4046. * @param string Optional string giving the new content_maker of this learnpath
  4047. * @return boolean True
  4048. */
  4049. public function set_maker($name = '')
  4050. {
  4051. if ($this->debug > 0) {
  4052. error_log('New LP - In learnpath::set_maker()', 0);
  4053. }
  4054. if (empty ($name)) {
  4055. return false;
  4056. }
  4057. $this->maker = Database::escape_string($name);
  4058. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4059. $course_id = api_get_course_int_id();
  4060. $lp_id = $this->get_id();
  4061. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4062. if ($this->debug > 2) {
  4063. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4064. }
  4065. Database::query($sql);
  4066. return true;
  4067. }
  4068. /**
  4069. * Sets the name of the current learnpath (and save)
  4070. * @param string Optional string giving the new name of this learnpath
  4071. * @return boolean True/False
  4072. */
  4073. public function set_name($name = null)
  4074. {
  4075. if ($this->debug > 0) {
  4076. error_log('New LP - In learnpath::set_name()', 0);
  4077. }
  4078. if (empty($name)) {
  4079. return false;
  4080. }
  4081. $this->name = Database::escape_string($name);
  4082. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4083. $lp_id = $this->get_id();
  4084. $course_id = api_get_course_int_id();
  4085. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4086. if ($this->debug > 2) {
  4087. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4088. }
  4089. $result = Database::query($sql);
  4090. // If the lp is visible on the homepage, change his name there.
  4091. if (Database::affected_rows($result)) {
  4092. $session_id = api_get_session_id();
  4093. $session_condition = api_get_session_condition($session_id);
  4094. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4095. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4096. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4097. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4098. Database::query($sql);
  4099. return true;
  4100. } else {
  4101. return false;
  4102. }
  4103. }
  4104. /**
  4105. * Set index specified prefix terms for all items in this path
  4106. * @param string Comma-separated list of terms
  4107. * @param char Xapian term prefix
  4108. * @return boolean False on error, true otherwise
  4109. */
  4110. public function set_terms_by_prefix($terms_string, $prefix)
  4111. {
  4112. $course_id = api_get_course_int_id();
  4113. if (api_get_setting('search_enabled') !== 'true') {
  4114. return false;
  4115. }
  4116. if (!extension_loaded('xapian')) {
  4117. return false;
  4118. }
  4119. $terms_string = trim($terms_string);
  4120. $terms = explode(',', $terms_string);
  4121. array_walk($terms, 'trim_value');
  4122. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4123. // Don't do anything if no change, verify only at DB, not the search engine.
  4124. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4125. return false;
  4126. }
  4127. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4128. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4129. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4130. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4131. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4132. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4133. $lp_id = intval($_POST['lp_id']);
  4134. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4135. $result = Database::query($sql);
  4136. $di = new ChamiloIndexer();
  4137. while ($lp_item = Database :: fetch_array($result)) {
  4138. // Get search_did.
  4139. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4140. $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';
  4141. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4142. //echo $sql; echo '<br>';
  4143. $res = Database::query($sql);
  4144. if (Database::num_rows($res) > 0) {
  4145. $se_ref = Database :: fetch_array($res);
  4146. // Compare terms.
  4147. $doc = $di->get_document($se_ref['search_did']);
  4148. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4149. $xterms = array();
  4150. foreach ($xapian_terms as $xapian_term) {
  4151. $xterms[] = substr($xapian_term['name'], 1);
  4152. }
  4153. $dterms = $terms;
  4154. $missing_terms = array_diff($dterms, $xterms);
  4155. $deprecated_terms = array_diff($xterms, $dterms);
  4156. // Save it to search engine.
  4157. foreach ($missing_terms as $term) {
  4158. $doc->add_term($prefix.$term, 1);
  4159. }
  4160. foreach ($deprecated_terms as $term) {
  4161. $doc->remove_term($prefix.$term);
  4162. }
  4163. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4164. $di->getDb()->flush();
  4165. } else {
  4166. //@todo What we should do here?
  4167. }
  4168. }
  4169. return true;
  4170. }
  4171. /**
  4172. * Sets the theme of the LP (local/remote) (and save)
  4173. * @param string Optional string giving the new theme of this learnpath
  4174. * @return bool Returns true if theme name is not empty
  4175. */
  4176. public function set_theme($name = '')
  4177. {
  4178. $course_id = api_get_course_int_id();
  4179. if ($this->debug > 0) {
  4180. error_log('New LP - In learnpath::set_theme()', 0);
  4181. }
  4182. $this->theme = Database::escape_string($name);
  4183. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4184. $lp_id = $this->get_id();
  4185. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4186. if ($this->debug > 2) {
  4187. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4188. }
  4189. Database::query($sql);
  4190. return true;
  4191. }
  4192. /**
  4193. * Sets the image of an LP (and save)
  4194. * @param string Optional string giving the new image of this learnpath
  4195. * @return bool Returns true if theme name is not empty
  4196. */
  4197. public function set_preview_image($name = '')
  4198. {
  4199. $course_id = api_get_course_int_id();
  4200. if ($this->debug > 0) {
  4201. error_log('New LP - In learnpath::set_preview_image()', 0);
  4202. }
  4203. $this->preview_image = Database::escape_string($name);
  4204. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4205. $lp_id = $this->get_id();
  4206. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4207. if ($this->debug > 2) {
  4208. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4209. }
  4210. Database::query($sql);
  4211. return true;
  4212. }
  4213. /**
  4214. * Sets the author of a LP (and save)
  4215. * @param string Optional string giving the new author of this learnpath
  4216. * @return bool Returns true if author's name is not empty
  4217. */
  4218. public function set_author($name = '')
  4219. {
  4220. $course_id = api_get_course_int_id();
  4221. if ($this->debug > 0) {
  4222. error_log('New LP - In learnpath::set_author()', 0);
  4223. }
  4224. $this->author = Database::escape_string($name);
  4225. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4226. $lp_id = $this->get_id();
  4227. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4228. if ($this->debug > 2) {
  4229. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4230. }
  4231. Database::query($sql);
  4232. return true;
  4233. }
  4234. /**
  4235. * Sets the hide_toc_frame parameter of a LP (and save)
  4236. * @param int 1 if frame is hiddent 0 thenelse
  4237. * @return bool Returns true if author's name is not empty
  4238. */
  4239. public function set_hide_toc_frame($hide)
  4240. {
  4241. $course_id = api_get_course_int_id();
  4242. if ($this->debug > 0) {
  4243. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4244. }
  4245. if (intval($hide) == $hide) {
  4246. $this->hide_toc_frame = $hide;
  4247. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4248. $lp_id = $this->get_id();
  4249. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4250. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4251. if ($this->debug > 2) {
  4252. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4253. }
  4254. Database::query($sql);
  4255. return true;
  4256. } else {
  4257. return false;
  4258. }
  4259. }
  4260. /**
  4261. * Sets the prerequisite of a LP (and save)
  4262. * @param int integer giving the new prerequisite of this learnpath
  4263. * @return bool returns true if prerequisite is not empty
  4264. */
  4265. public function set_prerequisite($prerequisite)
  4266. {
  4267. $course_id = api_get_course_int_id();
  4268. if ($this->debug > 0) {
  4269. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4270. }
  4271. $this->prerequisite = intval($prerequisite);
  4272. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4273. $lp_id = $this->get_id();
  4274. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4275. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4276. if ($this->debug > 2) {
  4277. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4278. }
  4279. Database::query($sql);
  4280. return true;
  4281. }
  4282. /**
  4283. * Sets the location/proximity of the LP (local/remote) (and save)
  4284. * @param string Optional string giving the new location of this learnpath
  4285. * @return boolean True on success / False on error
  4286. */
  4287. public function set_proximity($name = '')
  4288. {
  4289. $course_id = api_get_course_int_id();
  4290. if ($this->debug > 0) {
  4291. error_log('New LP - In learnpath::set_proximity()', 0);
  4292. }
  4293. if (empty ($name)) {
  4294. return false;
  4295. }
  4296. $this->proximity = Database::escape_string($name);
  4297. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4298. $lp_id = $this->get_id();
  4299. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4300. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4301. if ($this->debug > 2) {
  4302. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4303. }
  4304. Database::query($sql);
  4305. return true;
  4306. }
  4307. /**
  4308. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4309. * @param integer DB ID of the item
  4310. */
  4311. public function set_previous_item($id)
  4312. {
  4313. if ($this->debug > 0) {
  4314. error_log('New LP - In learnpath::set_previous_item()', 0);
  4315. }
  4316. $this->last = $id;
  4317. }
  4318. public function get_max_attempts()
  4319. {
  4320. return $this->max_attempts;
  4321. }
  4322. public function get_subscribe_users()
  4323. {
  4324. return $this->subscribe_users;
  4325. }
  4326. public function check_attempts()
  4327. {
  4328. $max_attempts = $this->get_max_attempts();
  4329. switch ($max_attempts) {
  4330. case 0: //unlimited
  4331. return true;
  4332. break;
  4333. case $max_attempts >= 1:
  4334. if ($this->attempt <= $max_attempts) {
  4335. return true;
  4336. }
  4337. break;
  4338. }
  4339. return false;
  4340. }
  4341. function check_item_attempts($item_id)
  4342. {
  4343. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4344. $count = $this->items[$item_id]->get_view_count();
  4345. $max_attempts = $this->get_max_attempts();
  4346. switch ($max_attempts) {
  4347. case 0: //unlimited
  4348. return true;
  4349. break;
  4350. case $max_attempts >= 1:
  4351. if ($count <= $max_attempts) {
  4352. return true;
  4353. }
  4354. break;
  4355. }
  4356. return false;
  4357. }
  4358. }
  4359. public function set_max_attempts($attempts)
  4360. {
  4361. $mode = 'multiple';
  4362. if ($attempts == 1) {
  4363. $mode = 'single';
  4364. }
  4365. $this->max_attempts = intval($attempts);
  4366. $lp_id = $this->get_id();
  4367. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4368. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4369. Database::query($sql);
  4370. $this->set_attempt_mode($mode);
  4371. }
  4372. /**
  4373. * Sets subscribe_users
  4374. * @param string $value Optional string giving the new location of this learnpath
  4375. * @return bool True on success / False on error
  4376. */
  4377. public function set_subscribe_users($value = 0)
  4378. {
  4379. if ($this->debug > 0) {
  4380. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4381. }
  4382. $this->subscribe_users = intval($value);;
  4383. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4384. $lp_id = $this->get_id();
  4385. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4386. if ($this->debug > 2) {
  4387. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4388. }
  4389. Database::query($sql);
  4390. return true;
  4391. }
  4392. /**
  4393. * Sets use_max_score
  4394. * @param string Optional string giving the new location of this learnpath
  4395. * @return boolean True on success / False on error
  4396. */
  4397. public function set_use_max_score($use_max_score = 1)
  4398. {
  4399. if ($this->debug > 0) {
  4400. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4401. }
  4402. $this->use_max_score = intval($use_max_score);
  4403. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4404. $lp_id = $this->get_id();
  4405. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4406. if ($this->debug > 2) {
  4407. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4408. }
  4409. Database::query($sql);
  4410. return true;
  4411. }
  4412. /**
  4413. * Sets and saves the expired_on date
  4414. * @param string Optional string giving the new author of this learnpath
  4415. * @return bool Returns true if author's name is not empty
  4416. */
  4417. public function set_expired_on($expired_on)
  4418. {
  4419. $course_id = api_get_course_int_id();
  4420. if ($this->debug > 0) {
  4421. error_log('New LP - In learnpath::set_expired_on()', 0);
  4422. }
  4423. if (!empty($expired_on)) {
  4424. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4425. } else {
  4426. $this->expired_on = '';
  4427. }
  4428. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4429. $lp_id = $this->get_id();
  4430. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4431. if ($this->debug > 2) {
  4432. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4433. }
  4434. Database::query($sql);
  4435. return true;
  4436. }
  4437. /**
  4438. * Sets and saves the publicated_on date
  4439. * @param string Optional string giving the new author of this learnpath
  4440. * @return bool Returns true if author's name is not empty
  4441. */
  4442. public function set_publicated_on($publicated_on)
  4443. {
  4444. $course_id = api_get_course_int_id();
  4445. if ($this->debug > 0) {
  4446. error_log('New LP - In learnpath::set_expired_on()', 0);
  4447. }
  4448. if (!empty($publicated_on)) {
  4449. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4450. } else {
  4451. $this->publicated_on = '';
  4452. }
  4453. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4454. $lp_id = $this->get_id();
  4455. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4456. if ($this->debug > 2) {
  4457. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4458. }
  4459. Database::query($sql);
  4460. return true;
  4461. }
  4462. public function set_category_id($category_id)
  4463. {
  4464. $this->category_id = $category_id;
  4465. $course_id = api_get_course_int_id();
  4466. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4467. $lp_id = $this->get_id();
  4468. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4469. $category_id
  4470. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4471. Database::query($sql);
  4472. return true;
  4473. }
  4474. /**
  4475. * Sets and saves the expired_on date
  4476. * @param string Optional string giving the new author of this learnpath
  4477. * @return bool Returns true if author's name is not empty
  4478. */
  4479. public function set_modified_on()
  4480. {
  4481. $course_id = api_get_course_int_id();
  4482. if ($this->debug > 0) {
  4483. error_log('New LP - In learnpath::set_expired_on()', 0);
  4484. }
  4485. $this->modified_on = api_get_utc_datetime();
  4486. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4487. $lp_id = $this->get_id();
  4488. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4489. if ($this->debug > 2) {
  4490. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4491. }
  4492. Database::query($sql);
  4493. return true;
  4494. }
  4495. /**
  4496. * Sets the object's error message
  4497. * @param string Error message. If empty, reinits the error string
  4498. * @return void
  4499. */
  4500. public function set_error_msg($error = '')
  4501. {
  4502. if ($this->debug > 0) {
  4503. error_log('New LP - In learnpath::set_error_msg()', 0);
  4504. }
  4505. if (empty ($error)) {
  4506. $this->error = '';
  4507. } else {
  4508. $this->error .= $error;
  4509. }
  4510. }
  4511. /**
  4512. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4513. * @param boolean Whether to allow a new attempt or not
  4514. * @return boolean True
  4515. */
  4516. public function start_current_item($allow_new_attempt = false)
  4517. {
  4518. if ($this->debug > 0) {
  4519. error_log('New LP - In learnpath::start_current_item()', 0);
  4520. }
  4521. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4522. $type = $this->get_type();
  4523. $item_type = $this->items[$this->current]->get_type();
  4524. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4525. $this->items[$this->current]->open($allow_new_attempt);
  4526. $this->autocomplete_parents($this->current);
  4527. $prereq_check = $this->prerequisites_match($this->current);
  4528. $this->items[$this->current]->save(false, $prereq_check);
  4529. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4530. } else {
  4531. // If sco, then it is supposed to have been updated by some other call.
  4532. }
  4533. if ($item_type == 'sco') {
  4534. $this->items[$this->current]->restart();
  4535. }
  4536. }
  4537. if ($this->debug > 0) {
  4538. error_log('New LP - End of learnpath::start_current_item()', 0);
  4539. }
  4540. return true;
  4541. }
  4542. /**
  4543. * Stops the processing and counters for the old item (as held in $this->last)
  4544. * @return boolean True/False
  4545. */
  4546. public function stop_previous_item()
  4547. {
  4548. if ($this->debug > 0) {
  4549. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4550. }
  4551. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4552. if ($this->debug > 2) {
  4553. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4554. }
  4555. switch ($this->get_type()) {
  4556. case '3' :
  4557. if ($this->items[$this->last]->get_type() != 'au') {
  4558. if ($this->debug > 2) {
  4559. error_log(
  4560. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4561. 0
  4562. );
  4563. }
  4564. $this->items[$this->last]->close();
  4565. //$this->autocomplete_parents($this->last);
  4566. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4567. } else {
  4568. if ($this->debug > 2) {
  4569. error_log(
  4570. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4571. 0
  4572. );
  4573. }
  4574. }
  4575. case '2' :
  4576. if ($this->items[$this->last]->get_type() != 'sco') {
  4577. if ($this->debug > 2) {
  4578. error_log(
  4579. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4580. 0
  4581. );
  4582. }
  4583. $this->items[$this->last]->close();
  4584. //$this->autocomplete_parents($this->last);
  4585. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4586. } else {
  4587. if ($this->debug > 2) {
  4588. error_log(
  4589. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4590. 0
  4591. );
  4592. }
  4593. }
  4594. break;
  4595. case '1' :
  4596. default :
  4597. if ($this->debug > 2) {
  4598. error_log(
  4599. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4600. 0
  4601. );
  4602. }
  4603. $this->items[$this->last]->close();
  4604. break;
  4605. }
  4606. } else {
  4607. if ($this->debug > 2) {
  4608. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4609. }
  4610. return false;
  4611. }
  4612. return true;
  4613. }
  4614. /**
  4615. * Updates the default view mode from fullscreen to embedded and inversely
  4616. * @return string The current default view mode ('fullscreen' or 'embedded')
  4617. */
  4618. public function update_default_view_mode()
  4619. {
  4620. $course_id = api_get_course_int_id();
  4621. if ($this->debug > 0) {
  4622. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4623. }
  4624. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4625. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4626. $res = Database::query($sql);
  4627. if (Database :: num_rows($res) > 0) {
  4628. $row = Database :: fetch_array($res);
  4629. $default_view_mode = $row['default_view_mod'];
  4630. $view_mode = $default_view_mode;
  4631. switch ($default_view_mode) {
  4632. case 'fullscreen':
  4633. $view_mode = 'embedded';
  4634. break;
  4635. case 'embedded':
  4636. $view_mode = 'embedframe';
  4637. break;
  4638. case 'embedframe':
  4639. $view_mode = 'impress';
  4640. break;
  4641. case 'impress':
  4642. $view_mode = 'fullscreen';
  4643. break;
  4644. }
  4645. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4646. );
  4647. $res = Database::query($sql);
  4648. $this->mode = $view_mode;
  4649. return $view_mode;
  4650. } else {
  4651. if ($this->debug > 2) {
  4652. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4653. }
  4654. }
  4655. return -1;
  4656. }
  4657. /**
  4658. * Updates the default behaviour about auto-commiting SCORM updates
  4659. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4660. */
  4661. public function update_default_scorm_commit()
  4662. {
  4663. $course_id = api_get_course_int_id();
  4664. if ($this->debug > 0) {
  4665. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4666. }
  4667. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4668. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4669. $res = Database::query($sql);
  4670. if (Database :: num_rows($res) > 0) {
  4671. $row = Database :: fetch_array($res);
  4672. $force = $row['force_commit'];
  4673. if ($force == 1) {
  4674. $force = 0;
  4675. $force_return = false;
  4676. } elseif ($force == 0) {
  4677. $force = 1;
  4678. $force_return = true;
  4679. }
  4680. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4681. $res = Database::query($sql);
  4682. $this->force_commit = $force_return;
  4683. return $force_return;
  4684. } else {
  4685. if ($this->debug > 2) {
  4686. error_log(
  4687. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4688. 0
  4689. );
  4690. }
  4691. }
  4692. return -1;
  4693. }
  4694. /**
  4695. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4696. * @return bool True on success, false on failure
  4697. */
  4698. public function update_display_order()
  4699. {
  4700. $course_id = api_get_course_int_id();
  4701. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4702. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4703. $res = Database::query($sql);
  4704. if ($res === false) {
  4705. return false;
  4706. }
  4707. $lps = array();
  4708. $lp_order = array();
  4709. $num = Database :: num_rows($res);
  4710. // First check the order is correct, globally (might be wrong because
  4711. // of versions < 1.8.4).
  4712. if ($num > 0) {
  4713. $i = 1;
  4714. while ($row = Database :: fetch_array($res)) {
  4715. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4716. $need_fix = true;
  4717. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4718. $res_u = Database::query($sql_u);
  4719. }
  4720. $i++;
  4721. }
  4722. }
  4723. return true;
  4724. }
  4725. /**
  4726. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4727. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4728. */
  4729. public function update_reinit()
  4730. {
  4731. $course_id = api_get_course_int_id();
  4732. if ($this->debug > 0) {
  4733. error_log('New LP - In learnpath::update_reinit()', 0);
  4734. }
  4735. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4736. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4737. $res = Database::query($sql);
  4738. if (Database :: num_rows($res) > 0) {
  4739. $row = Database :: fetch_array($res);
  4740. $force = $row['prevent_reinit'];
  4741. if ($force == 1) {
  4742. $force = 0;
  4743. } elseif ($force == 0) {
  4744. $force = 1;
  4745. }
  4746. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4747. $res = Database::query($sql);
  4748. $this->prevent_reinit = $force;
  4749. return $force;
  4750. } else {
  4751. if ($this->debug > 2) {
  4752. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4753. }
  4754. }
  4755. return -1;
  4756. }
  4757. /**
  4758. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4759. *
  4760. * @return string 'single', 'multi' or 'seriousgame'
  4761. * @author ndiechburg <noel@cblue.be>
  4762. **/
  4763. public function get_attempt_mode()
  4764. {
  4765. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4766. $this->seriousgame_mode = 0;
  4767. }
  4768. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4769. $this->prevent_reinit = 1;
  4770. }
  4771. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4772. return 'seriousgame';
  4773. }
  4774. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4775. return 'single';
  4776. }
  4777. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4778. return 'multiple';
  4779. }
  4780. return 'single';
  4781. }
  4782. /**
  4783. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4784. *
  4785. * @param string 'seriousgame', 'single' or 'multiple'
  4786. * @return boolean
  4787. * @author ndiechburg <noel@cblue.be>
  4788. **/
  4789. public function set_attempt_mode($mode)
  4790. {
  4791. $course_id = api_get_course_int_id();
  4792. switch ($mode) {
  4793. case 'seriousgame' :
  4794. $sg_mode = 1;
  4795. $prevent_reinit = 1;
  4796. break;
  4797. case 'single' :
  4798. $sg_mode = 0;
  4799. $prevent_reinit = 1;
  4800. break;
  4801. case 'multiple' :
  4802. $sg_mode = 0;
  4803. $prevent_reinit = 0;
  4804. break;
  4805. default :
  4806. $sg_mode = 0;
  4807. $prevent_reinit = 0;
  4808. break;
  4809. }
  4810. $this->prevent_reinit = $prevent_reinit;
  4811. $this->seriousgame_mode = $sg_mode;
  4812. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4813. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4814. );
  4815. $res = Database::query($sql);
  4816. if ($res) {
  4817. return true;
  4818. } else {
  4819. return false;
  4820. }
  4821. }
  4822. /**
  4823. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4824. *
  4825. * @return boolean
  4826. * @author ndiechburg <noel@cblue.be>
  4827. **/
  4828. public function switch_attempt_mode()
  4829. {
  4830. if ($this->debug > 0) {
  4831. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4832. }
  4833. $mode = $this->get_attempt_mode();
  4834. switch ($mode) {
  4835. case 'single' :
  4836. $next_mode = 'multiple';
  4837. break;
  4838. case 'multiple' :
  4839. $next_mode = 'seriousgame';
  4840. break;
  4841. case 'seriousgame' :
  4842. $next_mode = 'single';
  4843. break;
  4844. default :
  4845. $next_mode = 'single';
  4846. break;
  4847. }
  4848. $this->set_attempt_mode($next_mode);
  4849. }
  4850. /**
  4851. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4852. *
  4853. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4854. * @author ndiechburg <noel@cblue.be>
  4855. **/
  4856. public function set_seriousgame_mode()
  4857. {
  4858. $course_id = api_get_course_int_id();
  4859. if ($this->debug > 0) {
  4860. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4861. }
  4862. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4863. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4864. $res = Database::query($sql);
  4865. if (Database :: num_rows($res) > 0) {
  4866. $row = Database :: fetch_array($res);
  4867. $force = $row['seriousgame_mode'];
  4868. if ($force == 1) {
  4869. $force = 0;
  4870. } elseif ($force == 0) {
  4871. $force = 1;
  4872. }
  4873. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4874. );
  4875. $res = Database::query($sql);
  4876. $this->seriousgame_mode = $force;
  4877. return $force;
  4878. } else {
  4879. if ($this->debug > 2) {
  4880. error_log(
  4881. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4882. 0
  4883. );
  4884. }
  4885. }
  4886. return -1;
  4887. }
  4888. /**
  4889. * Updates the "scorm_debug" value that shows or hide the debug window
  4890. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4891. */
  4892. public function update_scorm_debug()
  4893. {
  4894. $course_id = api_get_course_int_id();
  4895. if ($this->debug > 0) {
  4896. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4897. }
  4898. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4899. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4900. $res = Database::query($sql);
  4901. if (Database :: num_rows($res) > 0) {
  4902. $row = Database :: fetch_array($res);
  4903. $force = $row['debug'];
  4904. if ($force == 1) {
  4905. $force = 0;
  4906. } elseif ($force == 0) {
  4907. $force = 1;
  4908. }
  4909. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4910. $res = Database::query($sql);
  4911. $this->scorm_debug = $force;
  4912. return $force;
  4913. } else {
  4914. if ($this->debug > 2) {
  4915. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4916. }
  4917. }
  4918. return -1;
  4919. }
  4920. /**
  4921. * Function that makes a call to the function sort_tree_array and create_tree_array
  4922. * @author Kevin Van Den Haute
  4923. * @param array
  4924. */
  4925. public function tree_array($array)
  4926. {
  4927. if ($this->debug > 1) {
  4928. error_log('New LP - In learnpath::tree_array()', 0);
  4929. }
  4930. $array = $this->sort_tree_array($array);
  4931. $this->create_tree_array($array);
  4932. }
  4933. /**
  4934. * Creates an array with the elements of the learning path tree in it
  4935. *
  4936. * @author Kevin Van Den Haute
  4937. * @param array $array
  4938. * @param int $parent
  4939. * @param int $depth
  4940. * @param array $tmp
  4941. */
  4942. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4943. {
  4944. if ($this->debug > 1) {
  4945. error_log('New LP - In learnpath::create_tree_array())', 0);
  4946. }
  4947. if (is_array($array)) {
  4948. for ($i = 0; $i < count($array); $i++) {
  4949. if ($array[$i]['parent_item_id'] == $parent) {
  4950. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4951. $tmp[] = $array[$i]['parent_item_id'];
  4952. $depth++;
  4953. }
  4954. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4955. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4956. $this->arrMenu[] = array(
  4957. 'id' => $array[$i]['id'],
  4958. 'item_type' => $array[$i]['item_type'],
  4959. 'title' => $array[$i]['title'],
  4960. 'path' => isset($array[$i]['path']) ? $array[$i]['path'] : null,
  4961. 'description' => $array[$i]['description'],
  4962. 'parent_item_id' => $array[$i]['parent_item_id'],
  4963. 'previous_item_id' => $array[$i]['previous_item_id'],
  4964. 'next_item_id' => $array[$i]['next_item_id'],
  4965. 'min_score' => $array[$i]['min_score'],
  4966. 'max_score' => $array[$i]['max_score'],
  4967. 'mastery_score' => $array[$i]['mastery_score'],
  4968. 'display_order' => $array[$i]['display_order'],
  4969. 'prerequisite' => $preq,
  4970. 'depth' => $depth,
  4971. 'audio' => $audio
  4972. );
  4973. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4974. }
  4975. }
  4976. }
  4977. }
  4978. /**
  4979. * Sorts a multi dimensional array by parent id and display order
  4980. * @author Kevin Van Den Haute
  4981. *
  4982. * @param array $array (array with al the learning path items in it)
  4983. *
  4984. * @return array
  4985. */
  4986. public function sort_tree_array($array)
  4987. {
  4988. foreach ($array as $key => $row) {
  4989. $parent[$key] = $row['parent_item_id'];
  4990. $position[$key] = $row['display_order'];
  4991. }
  4992. if (count($array) > 0) {
  4993. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4994. }
  4995. return $array;
  4996. }
  4997. /**
  4998. * Function that creates a table structure with a learning path his modules, chapters and documents.
  4999. * Also the actions for the modules, chapters and documents are in this table.
  5000. * @author Kevin Van Den Haute
  5001. * @param int $lp_id
  5002. * @return string
  5003. */
  5004. public function overview()
  5005. {
  5006. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5007. if ($this->debug > 0) {
  5008. error_log('New LP - In learnpath::overview()', 0);
  5009. }
  5010. $_course = api_get_course_info();
  5011. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5012. $return = '';
  5013. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5014. // we need to start a form when we want to update all the mp3 files
  5015. if ($update_audio == 'true') {
  5016. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5017. $_GET['cidReq']
  5018. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5019. $_GET['action']
  5020. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5021. }
  5022. $return .= '<div id="message"></div>';
  5023. $return_audio = '<table class="data_table">';
  5024. $return_audio .= '<tr>';
  5025. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5026. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5027. $return_audio .= '</tr>';
  5028. if ($update_audio != 'true') {
  5029. $return .= '<div class="span12">';
  5030. $return .= self::return_new_tree($update_audio);
  5031. $return .= '</div>';
  5032. $return .= Display::div(
  5033. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5034. array('style' => 'float:left; margin-top:15px;width:100%')
  5035. );
  5036. } else {
  5037. $return_audio .= self::return_new_tree($update_audio);
  5038. $return .= $return_audio.'</table>';
  5039. }
  5040. // We need to close the form when we are updating the mp3 files.
  5041. if ($update_audio == 'true') {
  5042. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5043. 'SaveAudioAndOrganization'
  5044. ).'</button></div>'; // TODO: What kind of language variable is this?
  5045. }
  5046. // We need to close the form when we are updating the mp3 files.
  5047. if ($update_audio == 'true' && count($arrLP) != 0) {
  5048. $return .= '</form>';
  5049. }
  5050. return $return;
  5051. }
  5052. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5053. {
  5054. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5055. echo '<script>
  5056. var newOrderData= "";
  5057. function processChildren(parentId) {
  5058. //Loop through the children of the UL element defined by the parentId
  5059. var ulParentID= "UL_" + parentId;
  5060. $("#" + ulParentID).children().each(function () {
  5061. /*Only process elements with an id attribute (in order to skip the blank,
  5062. unmovable <li> elements.*/
  5063. if ($(this).attr("id")) {
  5064. /*Build a string of data with the childs ID and parent ID,
  5065. using the "|" as a delimiter between the two IDs and the "^"
  5066. as a record delimiter (these delimiters were chosen in case the data
  5067. involved includes more common delimiters like commas within the content)
  5068. */
  5069. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5070. //Determine if this child is a containter
  5071. if ($(this).is(".container")) {
  5072. //Process the child elements of the container
  5073. processChildren($(this).attr("id"));
  5074. }
  5075. }
  5076. }); //end of children loop
  5077. } //end of processChildren function
  5078. $(function() {
  5079. $(".item_data").live("mouseover", function(event) {
  5080. $(".button_actions", this).show();
  5081. });
  5082. $(".item_data").live("mouseout", function() {
  5083. $(".button_actions",this).hide();
  5084. });
  5085. $(".button_actions").hide();
  5086. $( ".lp_resource" ).sortable({
  5087. items: ".lp_resource_element ",
  5088. handle: ".moved", //only the class "moved"
  5089. cursor: "move",
  5090. connectWith: "#lp_item_list",
  5091. placeholder: "ui-state-highlight", //defines the yellow highlight
  5092. start: function(event, ui) {
  5093. $(ui.item).css("width", "160px");
  5094. $(ui.item).find(".item_data").attr("style", "");
  5095. },
  5096. stop: function(event, ui) {
  5097. $(ui.item).css("width", "100%");
  5098. },
  5099. });
  5100. $("#lp_item_list").sortable({
  5101. items: "li",
  5102. handle: ".moved", //only the class "moved"
  5103. cursor: "move",
  5104. placeholder: "ui-state-highlight", //defines the yellow highlight
  5105. update: function(event, ui) {
  5106. //Walk through the direct descendants of the lp_item_list <ul>
  5107. $("#lp_item_list").children().each(function () {
  5108. /*Only process elements with an id attribute (in order to skip the blank,
  5109. unmovable <li> elements.*/
  5110. if ($(this).attr("id")) {
  5111. /*Build a string of data with the child s ID and parent ID,
  5112. using the "|" as a delimiter between the two IDs and the "^"
  5113. as a record delimiter (these delimiters were chosen in case the data
  5114. involved includes more common delimiters like commas within the content)
  5115. */
  5116. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5117. //Determine if this child is a containter
  5118. if ($(this).is(".li_container")) {
  5119. //Process the child elements of the container
  5120. processChildren($(this).attr("id"));
  5121. }
  5122. }
  5123. }); //end of lp_item_list children loop
  5124. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5125. $.post("'.$ajax_url.'", order, function(reponse){
  5126. $("#message").html(reponse);
  5127. });
  5128. },
  5129. receive: function(event, ui) {
  5130. var id = $(ui.item).attr("data_id");
  5131. var type = $(ui.item).attr("data_type");
  5132. var title = $(ui.item).attr("title");
  5133. if (ui.item.parent()[0]) {
  5134. var parent_id = $(ui.item.parent()[0]).attr("id");
  5135. var previous_id = $(ui.item.prev()).attr("id");
  5136. if (parent_id) {
  5137. parent_id = parent_id.split("_")[1];
  5138. var params = {
  5139. "a": "add_lp_item",
  5140. "id": id,
  5141. "parent_id": parent_id,
  5142. "previous_id": previous_id,
  5143. "type": type,
  5144. "title" : title
  5145. };
  5146. $.ajax({
  5147. type: "GET",
  5148. url: "'.$ajax_url.'",
  5149. data: params,
  5150. async: false,
  5151. success: function(data) {
  5152. if (data == -1) {
  5153. } else {
  5154. $(".normal-message").hide();
  5155. $(ui.item).attr("id", data);
  5156. $(ui.item).addClass("lp_resource_element_new");
  5157. $(ui.item).find(".item_data").attr("style", "");
  5158. $(ui.item).addClass("record li_container");
  5159. $(ui.item).removeClass("lp_resource_element");
  5160. $(ui.item).removeClass("doc_resource");
  5161. }
  5162. }
  5163. });
  5164. }
  5165. }//
  5166. }//end receive
  5167. });
  5168. });
  5169. </script>';
  5170. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5171. $course_id = api_get_course_int_id();
  5172. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5173. $sql = "SELECT * FROM $tbl_lp_item
  5174. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5175. $result = Database::query($sql);
  5176. $arrLP = array();
  5177. while ($row = Database :: fetch_array($result)) {
  5178. $row['title'] = Security :: remove_XSS($row['title']);
  5179. $row['description'] = Security :: remove_XSS($row['description']);
  5180. $arrLP[] = array(
  5181. 'id' => $row['id'],
  5182. 'item_type' => $row['item_type'],
  5183. 'title' => $row['title'],
  5184. 'path' => $row['path'],
  5185. 'description' => $row['description'],
  5186. 'parent_item_id' => $row['parent_item_id'],
  5187. 'previous_item_id' => $row['previous_item_id'],
  5188. 'next_item_id' => $row['next_item_id'],
  5189. 'max_score' => $row['max_score'],
  5190. 'min_score' => $row['min_score'],
  5191. 'mastery_score' => $row['mastery_score'],
  5192. 'prerequisite' => $row['prerequisite'],
  5193. 'display_order' => $row['display_order'],
  5194. 'audio' => $row['audio']
  5195. );
  5196. }
  5197. $this->tree_array($arrLP);
  5198. $arrLP = $this->arrMenu;
  5199. unset ($this->arrMenu);
  5200. $default_data = null;
  5201. $default_content = null;
  5202. $elements = array();
  5203. $return_audio = null;
  5204. $iconSysPath = api_get_path(SYS_CODE_PATH).'img/';
  5205. for ($i = 0; $i < count($arrLP); $i++) {
  5206. $title = $arrLP[$i]['title'];
  5207. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5208. //Link for the documents
  5209. if ($arrLP[$i]['item_type'] == 'document') {
  5210. $url = api_get_self().'?'.api_get_cidreq(
  5211. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5212. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5213. }
  5214. if (($i % 2) == 0) {
  5215. $oddclass = 'row_odd';
  5216. } else {
  5217. $oddclass = 'row_even';
  5218. }
  5219. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5220. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5221. $icon = '';
  5222. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5223. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5224. } else {
  5225. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5226. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5227. } else {
  5228. $icon = Display::return_icon('folder_document.gif');
  5229. }
  5230. }
  5231. // The audio column.
  5232. $return_audio .= '<td align="center">';
  5233. $audio = '';
  5234. if (!$update_audio OR $update_audio <> 'true') {
  5235. if (!empty($arrLP[$i]['audio'])) {
  5236. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5237. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5238. $audio .= '<script type="text/javascript">
  5239. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5240. s1.addParam("allowscriptaccess","always");
  5241. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5242. s1.write("container' . $i . '");
  5243. </script>';*/
  5244. } else {
  5245. $audio .= '';
  5246. }
  5247. } else {
  5248. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5249. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5250. if (!empty ($arrLP[$i]['audio'])) {
  5251. $audio .= '<br />'.Security::remove_XSS(
  5252. $arrLP[$i]['audio']
  5253. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5254. 'RemoveAudio'
  5255. );
  5256. }
  5257. }
  5258. }
  5259. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5260. $return_audio .= '</td>';
  5261. $move_icon = '';
  5262. $move_item_icon = '';
  5263. $edit_icon = '';
  5264. $delete_icon = '';
  5265. $audio_icon = '';
  5266. $prerequisities_icon = '';
  5267. if ($is_allowed_to_edit) {
  5268. if (!$update_audio OR $update_audio <> 'true') {
  5269. $move_icon .= '<a class="moved" href="#">';
  5270. $move_icon .= Display::return_icon(
  5271. 'move_everywhere.png',
  5272. get_lang('Move'),
  5273. array(),
  5274. ICON_SIZE_TINY
  5275. );
  5276. $move_icon .= '</a>';
  5277. }
  5278. // No edit for this item types
  5279. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5280. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5281. $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'].'">';
  5282. $edit_icon .= Display::return_icon(
  5283. 'edit.png',
  5284. get_lang('LearnpathEditModule'),
  5285. array(),
  5286. ICON_SIZE_TINY
  5287. );
  5288. $edit_icon .= '</a>';
  5289. } else {
  5290. $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'].'">';
  5291. $edit_icon .= Display::return_icon('edit.png',
  5292. get_lang('LearnpathEditModule'),
  5293. array(),
  5294. ICON_SIZE_TINY
  5295. );
  5296. $edit_icon .= '</a>';
  5297. }
  5298. }
  5299. $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(
  5300. $title
  5301. ).'\');">';
  5302. $delete_icon .= Display::return_icon(
  5303. 'delete.png',
  5304. get_lang('LearnpathDeleteModule'),
  5305. array(),
  5306. ICON_SIZE_TINY
  5307. );
  5308. $delete_icon .= '</a>';
  5309. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5310. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5311. $prerequisities_icon = Display::url(
  5312. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5313. $url.'&action=edit_item_prereq'
  5314. );
  5315. $move_item_icon = Display::url(
  5316. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5317. $url.'&action=move_item'
  5318. );
  5319. $audio_icon = Display::url(
  5320. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5321. $url.'&action=add_audio'
  5322. );
  5323. }
  5324. }
  5325. if ($update_audio != 'true') {
  5326. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5327. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5328. array('class' => 'button_actions')
  5329. );
  5330. } else {
  5331. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5332. }
  5333. $parent_id = $arrLP[$i]['parent_item_id'];
  5334. $default_data[$arrLP[$i]['id']] = $row;
  5335. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5336. if (empty($parent_id)) {
  5337. $elements[$arrLP[$i]['id']]['data'] = $row;
  5338. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5339. } else {
  5340. $parent_arrays = array();
  5341. if ($arrLP[$i]['depth'] > 1) {
  5342. //Getting list of parents
  5343. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5344. foreach ($arrLP as $item) {
  5345. if ($item['id'] == $parent_id) {
  5346. if ($item['parent_item_id'] == 0) {
  5347. $parent_id = $item['id'];
  5348. break;
  5349. } else {
  5350. $parent_id = $item['parent_item_id'];
  5351. if (empty($parent_arrays)) {
  5352. $parent_arrays[] = intval($item['id']);
  5353. }
  5354. $parent_arrays[] = $parent_id;
  5355. break;
  5356. }
  5357. }
  5358. }
  5359. }
  5360. }
  5361. if (!empty($parent_arrays)) {
  5362. $parent_arrays = array_reverse($parent_arrays);
  5363. $val = '$elements';
  5364. $x = 0;
  5365. foreach ($parent_arrays as $item) {
  5366. if ($x != count($parent_arrays) - 1) {
  5367. $val .= '["'.$item.'"]["children"]';
  5368. } else {
  5369. $val .= '["'.$item.'"]["children"]';
  5370. }
  5371. $x++;
  5372. }
  5373. $val .= "";
  5374. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5375. eval($code_str);
  5376. } else {
  5377. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5378. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5379. }
  5380. }
  5381. }
  5382. $return = '<div class="lp_tree well">';
  5383. $return .= '<ul id="lp_item_list">';
  5384. $return .= '<h4>'.$this->name.'</h4><br>';
  5385. $tree = self::print_recursive($elements, $default_data, $default_content);
  5386. if (!empty($tree)) {
  5387. $return .= $tree;
  5388. } else {
  5389. if ($drop_element_here) {
  5390. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5391. }
  5392. }
  5393. $return .= '</ul>';
  5394. if ($update_audio == 'true') {
  5395. $return = $return_audio;
  5396. } else {
  5397. $return .= '</div>';
  5398. }
  5399. return $return;
  5400. }
  5401. function print_recursive($elements, $default_data, $default_content)
  5402. {
  5403. $return = '';
  5404. foreach ($elements as $key => $item) {
  5405. if (isset($item['load_data']) || empty($item['data'])) {
  5406. $item['data'] = $default_data[$item['load_data']];
  5407. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5408. }
  5409. $sub_list = '';
  5410. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5411. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5412. }
  5413. if (empty($item['children'])) {
  5414. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5415. $active = null;
  5416. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5417. $active = 'active';
  5418. }
  5419. $return .= Display::tag(
  5420. 'li',
  5421. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5422. array('id' => $key, 'class' => 'record li_container')
  5423. );
  5424. } else {
  5425. //sections
  5426. if (isset($item['children'])) {
  5427. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5428. }
  5429. $sub_list = Display::tag(
  5430. 'ul',
  5431. $sub_list.$data,
  5432. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5433. );
  5434. $return .= Display::tag(
  5435. 'li',
  5436. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5437. array('id' => $key, 'class' => 'record li_container')
  5438. );
  5439. }
  5440. }
  5441. return $return;
  5442. }
  5443. /**
  5444. * This function builds the action menu
  5445. * @return void
  5446. */
  5447. public function build_action_menu()
  5448. {
  5449. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5450. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5451. $return = '<div class="actions">';
  5452. //$return .= '<a href="lp_controller.php?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=build&lp_id=' . $this->lp_id . '">' . Display :: return_icon('home.png', get_lang('Build'),'',ICON_SIZE_MEDIUM).'</a>';
  5453. //$return .= '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('BasicOverview') . '">' . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  5454. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5455. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5456. 'preview_view.png',
  5457. get_lang('Display'),
  5458. '',
  5459. ICON_SIZE_MEDIUM
  5460. ).'</a> ';
  5461. //$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=step&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewStep') . '">' . Display :: return_icon('new_learnigpath_object.png', get_lang('NewStep'),'',ICON_SIZE_MEDIUM).'</a>';
  5462. // echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=chapter&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewChapter') . '">' . Display :: return_icon('add_learnpath_section.png', get_lang('NewChapter'),'',ICON_SIZE_MEDIUM).'</a>';
  5463. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5464. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5465. 'upload_audio.png',
  5466. get_lang('UpdateAllAudioFragments'),
  5467. '',
  5468. ICON_SIZE_MEDIUM
  5469. ).'</a>';*/
  5470. $return .= '<a href="lp_controller.php?referer=add_item&'.api_get_cidreq().'&amp;action=edit&amp;lp_id='.$_SESSION['oLP']->lp_id.'">'.Display :: return_icon(
  5471. 'settings.png',
  5472. get_lang('CourseSettings'),
  5473. '',
  5474. ICON_SIZE_MEDIUM
  5475. ).'</a>';
  5476. $buttons = array(
  5477. array(
  5478. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5479. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5480. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5481. ),
  5482. array(
  5483. 'title' => get_lang('ClearAllPrerequisites'),
  5484. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5485. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5486. ),
  5487. );
  5488. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5489. $return .= '</div>';
  5490. echo $return;
  5491. }
  5492. /**
  5493. * This functions builds the LP tree based on data from the database.
  5494. * @return string
  5495. * @deprecated use the return_new_tree() function
  5496. * @uses dtree.js :: necessary javascript for building this tree
  5497. */
  5498. public function build_tree()
  5499. {
  5500. $iconSysPath = api_get_path(SYS_CODE_PATH).'img/';
  5501. $iconWebPath = api_get_path(WEB_IMG_PATH);
  5502. $course_id = api_get_course_int_id();
  5503. $return = "<script type=\"text/javascript\">\n";
  5504. $return .= "\tm = new dTree('m');\n\n";
  5505. $return .= "\tm.config.folderLinks = true;\n";
  5506. $return .= "\tm.config.useCookies = true;\n";
  5507. $return .= "\tm.config.useIcons = true;\n";
  5508. $return .= "\tm.config.useLines = true;\n";
  5509. $return .= "\tm.config.useSelection = true;\n";
  5510. $return .= "\tm.config.useStatustext = false;\n\n";
  5511. $menu = 0;
  5512. $parent = '';
  5513. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5514. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5515. $sql = " SELECT id, title, description, item_type, path, parent_item_id, previous_item_id, next_item_id, max_score, min_score, mastery_score, display_order
  5516. FROM $tbl_lp_item
  5517. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5518. $result = Database::query($sql);
  5519. $arrLP = array();
  5520. while ($row = Database :: fetch_array($result)) {
  5521. $row['title'] = Security :: remove_XSS($row['title']);
  5522. $row['description'] = Security :: remove_XSS($row['description']);
  5523. $arrLP[] = array(
  5524. 'id' => $row['id'],
  5525. 'item_type' => $row['item_type'],
  5526. 'title' => $row['title'],
  5527. 'path' => $row['path'],
  5528. 'description' => $row['description'],
  5529. 'parent_item_id' => $row['parent_item_id'],
  5530. 'previous_item_id' => $row['previous_item_id'],
  5531. 'next_item_id' => $row['next_item_id'],
  5532. 'max_score' => $row['max_score'],
  5533. 'min_score' => $row['min_score'],
  5534. 'mastery_score' => $row['mastery_score'],
  5535. 'display_order' => $row['display_order']
  5536. );
  5537. }
  5538. $this->tree_array($arrLP);
  5539. $arrLP = $this->arrMenu;
  5540. unset($this->arrMenu);
  5541. $title = '';
  5542. for ($i = 0; $i < count($arrLP); $i++) {
  5543. $title = addslashes($arrLP[$i]['title']);
  5544. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5545. $_GET['cidReq']
  5546. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5547. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5548. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5549. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."lp_".$icon_name.".png', '".$iconWebPath."lp_".$icon_name.".png');\n";
  5550. } else {
  5551. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5552. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."lp_".$icon_name.".gif', '".$iconWebPath."lp_".$icon_name.".gif');\n";
  5553. } else {
  5554. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."folder_document.gif', '".$iconWebPath."folder_document.gif');\n";
  5555. }
  5556. }
  5557. if ($menu < $arrLP[$i]['id']) {
  5558. $menu = $arrLP[$i]['id'];
  5559. }
  5560. }
  5561. $return .= "\n\tdocument.write(m);\n";
  5562. $return .= "\t if(!m.selectedNode) m.s(1);";
  5563. $return .= "</script>\n";
  5564. return $return;
  5565. }
  5566. /**
  5567. * Creates the default learning path folder
  5568. */
  5569. public static function generate_learning_path_folder($course)
  5570. {
  5571. //Creating learning_path folder
  5572. $dir = '/learning_path';
  5573. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5574. $folder = false;
  5575. if (!is_dir($filepath.'/'.$dir)) {
  5576. $folder = FileManager::create_unexisting_directory(
  5577. $course,
  5578. api_get_user_id(),
  5579. api_get_session_id(),
  5580. 0,
  5581. 0,
  5582. $filepath,
  5583. $dir,
  5584. get_lang('LearningPaths'),
  5585. 0
  5586. );
  5587. } else {
  5588. $folder = true;
  5589. }
  5590. return $folder;
  5591. }
  5592. public function generate_lp_folder($course, $lp_name = null)
  5593. {
  5594. $filepath = '';
  5595. $dir = '/learning_path/';
  5596. if (empty($lp_name)) {
  5597. $lp_name = $this->name;
  5598. }
  5599. $folder = self::generate_learning_path_folder($course);
  5600. //Creating LP folder
  5601. if ($folder) {
  5602. //Limits title size
  5603. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5604. $dir = $dir.$title;
  5605. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5606. if (!is_dir($filepath.'/'.$dir)) {
  5607. $folder = FileManager::create_unexisting_directory(
  5608. $course,
  5609. api_get_user_id(),
  5610. 0,
  5611. 0,
  5612. 0,
  5613. $filepath,
  5614. $dir,
  5615. $lp_name
  5616. );
  5617. } else {
  5618. $folder = true;
  5619. }
  5620. $dir = $dir.'/';
  5621. if ($folder) {
  5622. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5623. }
  5624. }
  5625. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5626. return $array;
  5627. }
  5628. /**
  5629. * Create a new document //still needs some finetuning
  5630. * @param array $_course
  5631. * @return string
  5632. */
  5633. public function create_document($_course)
  5634. {
  5635. $course_id = api_get_course_int_id();
  5636. global $charset;
  5637. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5638. if (strstr($dir, '..')) {
  5639. $dir = '/';
  5640. }
  5641. if ($dir[0] == '.') {
  5642. $dir = substr($dir, 1);
  5643. }
  5644. if ($dir[0] != '/') {
  5645. $dir = '/'.$dir;
  5646. }
  5647. if ($dir[strlen($dir) - 1] != '/') {
  5648. $dir .= '/';
  5649. }
  5650. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5651. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5652. //Generates folder
  5653. $result = $this->generate_lp_folder($_course);
  5654. $dir = $result['dir'];
  5655. $filepath = $result['filepath'];
  5656. }
  5657. if (!is_dir($filepath)) {
  5658. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5659. $dir = '/';
  5660. }
  5661. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5662. // is already escaped twice when it gets here.
  5663. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5664. $title = FileManager::disable_dangerous_file($title);
  5665. $filename = $title;
  5666. $content = $_POST['content_lp'];
  5667. $tmp_filename = $filename;
  5668. $i = 0;
  5669. while (file_exists($filepath.$tmp_filename.'.html')) {
  5670. $tmp_filename = $filename.'_'.++$i;
  5671. }
  5672. $filename = $tmp_filename.'.html';
  5673. $content = stripslashes($content);
  5674. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5675. // Change the path of mp3 to absolute.
  5676. // The first regexp deals with ../../../ urls.
  5677. $content = preg_replace(
  5678. "|(flashvars=\"file=)(\.+/)+|",
  5679. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5680. $content
  5681. );
  5682. // The second regexp deals with audio/ urls.
  5683. $content = preg_replace(
  5684. "|(flashvars=\"file=)([^/]+)/|",
  5685. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5686. $content
  5687. );
  5688. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5689. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5690. if (!file_exists($filepath.$filename)) {
  5691. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5692. fputs($fp, $content);
  5693. fclose($fp);
  5694. $file_size = filesize($filepath.$filename);
  5695. $save_file_path = $dir.$filename;
  5696. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5697. if ($document_id) {
  5698. api_item_property_update(
  5699. $_course,
  5700. TOOL_DOCUMENT,
  5701. $document_id,
  5702. 'DocumentAdded',
  5703. api_get_user_id(),
  5704. null,
  5705. null,
  5706. null,
  5707. null,
  5708. api_get_session_id()
  5709. );
  5710. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5711. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5712. if ($new_comment || $new_title) {
  5713. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5714. $ct = '';
  5715. if ($new_comment) {
  5716. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5717. }
  5718. if ($new_title) {
  5719. $ct .= ", title='".Database::escape_string(
  5720. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5721. )."' ";
  5722. }
  5723. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5724. $ct,
  5725. 1
  5726. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5727. Database::query($sql_update);
  5728. }
  5729. }
  5730. return $document_id;
  5731. }
  5732. }
  5733. }
  5734. /**
  5735. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5736. * @param array $_course array
  5737. * @return void
  5738. */
  5739. public function edit_document($_course)
  5740. {
  5741. $course_id = api_get_course_int_id();
  5742. global $_configuration;
  5743. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5744. if (strstr($dir, '..')) {
  5745. $dir = '/';
  5746. }
  5747. if ($dir[0] == '.') {
  5748. $dir = substr($dir, 1);
  5749. }
  5750. if ($dir[0] != '/') {
  5751. $dir = '/'.$dir;
  5752. }
  5753. if ($dir[strlen($dir) - 1] != '/') {
  5754. $dir .= '/';
  5755. }
  5756. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5757. if (!is_dir($filepath)) {
  5758. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5759. $dir = '/';
  5760. }
  5761. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5762. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5763. $document_id = intval($_POST['path']);
  5764. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5765. $res = Database::query($sql);
  5766. $row = Database :: fetch_array($res);
  5767. $content = stripslashes($_POST['content_lp']);
  5768. $file = $filepath.$row['path'];
  5769. if ($fp = @ fopen($file, 'w')) {
  5770. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/',$content);
  5771. // Change the path of mp3 to absolute.
  5772. // The first regexp deals with ../../../ urls.
  5773. $content = preg_replace(
  5774. "|(flashvars=\"file=)(\.+/)+|",
  5775. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5776. $content
  5777. );
  5778. // The second regexp deals with audio/ urls.
  5779. $content = preg_replace(
  5780. "|(flashvars=\"file=)([^/]+)/|",
  5781. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5782. $content
  5783. );
  5784. fputs($fp, $content);
  5785. fclose($fp);
  5786. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5787. $_POST['title']
  5788. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5789. Database::query($sql_update);
  5790. }
  5791. }
  5792. }
  5793. /**
  5794. * Displays the selected item, with a panel for manipulating the item
  5795. * @param int $item_id
  5796. * @param string $msg
  5797. * @return string
  5798. */
  5799. public function display_item($item_id, $msg = null, $show_actions = true)
  5800. {
  5801. $course_id = api_get_course_int_id();
  5802. $return = '';
  5803. if (is_numeric($item_id)) {
  5804. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5805. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5806. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5807. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5808. $result = Database::query($sql);
  5809. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5810. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5811. // Prevents wrong parent selection for document, see Bug#1251.
  5812. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5813. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5814. }
  5815. if ($show_actions) {
  5816. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5817. }
  5818. $return .= '<div style="padding:10px;">';
  5819. if ($msg != '') {
  5820. $return .= $msg;
  5821. }
  5822. $return .= '<h3>'.$row['title'].'</h3>';
  5823. switch ($row['item_type']) {
  5824. case TOOL_QUIZ:
  5825. if (!empty($row['path'])) {
  5826. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5827. $exercise = new Exercise();
  5828. $exercise->read($row['path']);
  5829. $return .= $exercise->description.'<br />';
  5830. }
  5831. break;
  5832. case TOOL_DOCUMENT:
  5833. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5834. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5835. $row['path']
  5836. );
  5837. $result = Database::query($sql_doc);
  5838. $path_file = Database::result($result, 0, 0);
  5839. $path_parts = pathinfo($path_file);
  5840. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5841. if (in_array(
  5842. $path_parts['extension'],
  5843. array(
  5844. 'html',
  5845. 'txt',
  5846. 'png',
  5847. 'jpg',
  5848. 'JPG',
  5849. 'jpeg',
  5850. 'JPEG',
  5851. 'gif',
  5852. 'swf'
  5853. )
  5854. )
  5855. ) {
  5856. $return .= $this->display_document($row['path'], true, true);
  5857. }
  5858. break;
  5859. }
  5860. $return .= '</div>';
  5861. }
  5862. }
  5863. return $return;
  5864. }
  5865. /**
  5866. * Shows the needed forms for editing a specific item
  5867. * @param int $item_id
  5868. * @return string
  5869. */
  5870. public function display_edit_item($item_id)
  5871. {
  5872. $_course = api_get_course_info(); // It will disappear.
  5873. $course_id = api_get_course_int_id();
  5874. $return = '';
  5875. if (is_numeric($item_id)) {
  5876. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5877. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5878. $res = Database::query($sql);
  5879. $row = Database::fetch_array($res);
  5880. switch ($row['item_type']) {
  5881. case 'dokeos_chapter' :
  5882. case 'dir' :
  5883. case 'asset' :
  5884. case 'sco' :
  5885. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5886. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5887. $return .= $this->display_item_form(
  5888. $row['item_type'],
  5889. get_lang('EditCurrentChapter').' :',
  5890. 'edit',
  5891. $item_id,
  5892. $row
  5893. );
  5894. } else {
  5895. $return .= $this->display_item_small_form(
  5896. $row['item_type'],
  5897. get_lang('EditCurrentChapter').' :',
  5898. $row
  5899. );
  5900. }
  5901. break;
  5902. case TOOL_DOCUMENT :
  5903. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5904. $sql_step = " SELECT lp.*, doc.path as dir
  5905. FROM ".$tbl_lp_item." as lp
  5906. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5907. WHERE lp.c_id = $course_id AND
  5908. doc.c_id = $course_id AND
  5909. lp.id = ".Database::escape_string($item_id);
  5910. $res_step = Database::query($sql_step);
  5911. $row_step = Database :: fetch_array($res_step);
  5912. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5913. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5914. break;
  5915. case TOOL_LINK :
  5916. $link_id = (string)$row['path'];
  5917. if (ctype_digit($link_id)) {
  5918. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5919. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5920. $link_id
  5921. );
  5922. $res_link = Database::query($sql_select);
  5923. $row_link = Database :: fetch_array($res_link);
  5924. if (is_array($row_link)) {
  5925. $row['url'] = $row_link['url'];
  5926. }
  5927. }
  5928. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5929. $return .= $this->display_link_form('edit', $item_id, $row);
  5930. break;
  5931. case 'dokeos_module' :
  5932. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5933. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5934. $return .= $this->display_item_form(
  5935. $row['item_type'],
  5936. get_lang('EditCurrentModule').' :',
  5937. 'edit',
  5938. $item_id,
  5939. $row
  5940. );
  5941. } else {
  5942. $return .= $this->display_item_small_form(
  5943. $row['item_type'],
  5944. get_lang('EditCurrentModule').' :',
  5945. $row
  5946. );
  5947. }
  5948. break;
  5949. case TOOL_QUIZ :
  5950. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5951. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5952. break;
  5953. case TOOL_HOTPOTATOES :
  5954. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5955. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5956. break;
  5957. case TOOL_STUDENTPUBLICATION :
  5958. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5959. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5960. break;
  5961. case TOOL_FORUM :
  5962. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5963. $return .= $this->display_forum_form('edit', $item_id, $row);
  5964. break;
  5965. case TOOL_THREAD :
  5966. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5967. $return .= $this->display_thread_form('edit', $item_id, $row);
  5968. break;
  5969. }
  5970. }
  5971. return $return;
  5972. }
  5973. /**
  5974. * Function that displays a list with al the resources that could be added to the learning path
  5975. * @return string
  5976. */
  5977. public function display_resources()
  5978. {
  5979. $_course = api_get_course_info(); // TODO: Don't use globals.
  5980. $course_code = api_get_course_id();
  5981. //Get all the docs
  5982. $documents = $this->get_documents();
  5983. //Get all the exercises
  5984. $exercises = $this->get_exercises();
  5985. // Get all the links
  5986. $links = $this->get_links();
  5987. //Get al the student publications
  5988. $works = $this->get_student_publications();
  5989. //Get al the forums
  5990. $forums = $this->get_forums(null, $course_code);
  5991. $headers = array(
  5992. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  5993. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  5994. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  5995. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  5996. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  5997. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  5998. );
  5999. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6000. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6001. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6002. return true;
  6003. }
  6004. /**
  6005. * Returns the extension of a document
  6006. * @param string filename
  6007. * @return string Extension (part after the last dot)
  6008. */
  6009. public function get_extension($filename)
  6010. {
  6011. $explode = explode('.', $filename);
  6012. return $explode[count($explode) - 1];
  6013. }
  6014. /**
  6015. * Displays a document by id
  6016. *
  6017. * @param unknown_type $id
  6018. * @return unknown
  6019. */
  6020. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6021. {
  6022. $_course = api_get_course_info(); // It is temporary.
  6023. $course_id = api_get_course_int_id();
  6024. $return = '';
  6025. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6026. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6027. WHERE c_id = ".$course_id." AND id = ".$id;
  6028. $res_doc = Database::query($sql_doc);
  6029. $row_doc = Database :: fetch_array($res_doc);
  6030. // TODO: Add a path filter.
  6031. if ($iframe) {
  6032. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.api_get_path(
  6033. WEB_COURSE_PATH
  6034. ).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq(
  6035. ).'"></iframe>';
  6036. } else {
  6037. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6038. }
  6039. return $return;
  6040. }
  6041. /**
  6042. * Return HTML form to add/edit a quiz
  6043. * @param string Action (add/edit)
  6044. * @param integer Item ID if already exists
  6045. * @param mixed Extra information (quiz ID if integer)
  6046. * @return string HTML form
  6047. */
  6048. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6049. {
  6050. $course_id = api_get_course_int_id();
  6051. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6052. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6053. if ($id != 0 && is_array($extra_info)) {
  6054. $item_title = $extra_info['title'];
  6055. $item_description = $extra_info['description'];
  6056. } elseif (is_numeric($extra_info)) {
  6057. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz."
  6058. WHERE c_id = ".$course_id." AND iid = ".$extra_info;
  6059. $result = Database::query($sql_quiz);
  6060. $row = Database :: fetch_array($result);
  6061. $item_title = $row['title'];
  6062. $item_description = $row['description'];
  6063. } else {
  6064. $item_title = '';
  6065. $item_description = '';
  6066. }
  6067. $item_title = Security::remove_XSS($item_title);
  6068. $item_description = Security::remove_XSS($item_description);
  6069. $legend = '<legend>';
  6070. if ($id != 0 && is_array($extra_info)) {
  6071. $parent = $extra_info['parent_item_id'];
  6072. } else {
  6073. $parent = 0;
  6074. }
  6075. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6076. $result = Database::query($sql);
  6077. $arrLP = array();
  6078. while ($row = Database :: fetch_array($result)) {
  6079. $arrLP[] = array(
  6080. 'id' => $row['id'],
  6081. 'item_type' => $row['item_type'],
  6082. 'title' => $row['title'],
  6083. 'path' => $row['path'],
  6084. 'description' => $row['description'],
  6085. 'parent_item_id' => $row['parent_item_id'],
  6086. 'previous_item_id' => $row['previous_item_id'],
  6087. 'next_item_id' => $row['next_item_id'],
  6088. 'display_order' => $row['display_order'],
  6089. 'max_score' => $row['max_score'],
  6090. 'min_score' => $row['min_score'],
  6091. 'mastery_score' => $row['mastery_score'],
  6092. 'prerequisite' => $row['prerequisite'],
  6093. 'max_time_allowed' => $row['max_time_allowed']
  6094. );
  6095. }
  6096. $this->tree_array($arrLP);
  6097. $arrLP = $this->arrMenu;
  6098. unset ($this->arrMenu);
  6099. if ($action == 'add') {
  6100. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6101. } elseif ($action == 'move') {
  6102. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6103. } else {
  6104. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6105. }
  6106. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6107. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6108. }
  6109. $legend .= '</legend>';
  6110. $return = '<div class="sectioncomment">';
  6111. $return .= '<form method="POST">';
  6112. $return .= $legend;
  6113. $return .= '<table class="lp_form">';
  6114. if ($action != 'move') {
  6115. $return .= '<tr>';
  6116. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6117. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6118. $return .= '</tr>';
  6119. }
  6120. $return .= '<tr>';
  6121. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6122. $return .= '<td class="input">';
  6123. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6124. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6125. $arrHide = array(
  6126. $id
  6127. );
  6128. //$parent_item_id = $_SESSION['parent_item_id'];
  6129. for ($i = 0; $i < count($arrLP); $i++) {
  6130. if ($action != 'add') {
  6131. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6132. $arrLP[$i]['id'],
  6133. $arrHide
  6134. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6135. ) {
  6136. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6137. } else {
  6138. $arrHide[] = $arrLP[$i]['id'];
  6139. }
  6140. } else {
  6141. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6142. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6143. }
  6144. }
  6145. }
  6146. if (is_array($arrLP)) {
  6147. reset($arrLP);
  6148. }
  6149. $return .= '</select>';
  6150. $return .= '</td>';
  6151. $return .= '</tr>';
  6152. $return .= '<tr>';
  6153. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6154. $return .= '<td class="input">';
  6155. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6156. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6157. for ($i = 0; $i < count($arrLP); $i++) {
  6158. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6159. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6160. $selected = 'selected="selected" ';
  6161. } elseif ($action == 'add') {
  6162. $selected = 'selected="selected" ';
  6163. } else {
  6164. $selected = '';
  6165. }
  6166. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6167. 'After'
  6168. ).' "'.$arrLP[$i]['title'].'"</option>';
  6169. }
  6170. }
  6171. $return .= '</select>';
  6172. $return .= '</td>';
  6173. $return .= '</tr>';
  6174. if ($action != 'move') {
  6175. $id_prerequisite = 0;
  6176. if (is_array($arrLP)) {
  6177. foreach ($arrLP as $key => $value) {
  6178. if ($value['id'] == $id) {
  6179. $id_prerequisite = $value['prerequisite'];
  6180. break;
  6181. }
  6182. }
  6183. }
  6184. $arrHide = array();
  6185. for ($i = 0; $i < count($arrLP); $i++) {
  6186. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6187. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6188. $s_selected_position = $arrLP[$i]['id'];
  6189. } elseif ($action == 'add') {
  6190. $s_selected_position = 0;
  6191. }
  6192. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6193. }
  6194. }
  6195. /*// Commented the prerequisites, only visible in edit (exercise).
  6196. $return .= '<tr>';
  6197. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6198. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6199. foreach($arrHide as $key => $value){
  6200. if($key==$s_selected_position && $action == 'add'){
  6201. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6202. }
  6203. elseif($key==$id_prerequisite && $action == 'edit'){
  6204. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6205. }
  6206. else{
  6207. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6208. }
  6209. }
  6210. $return .= "</select></td>";
  6211. */
  6212. $return .= '</tr>';
  6213. /*$return .= '<tr>';
  6214. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6215. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6216. // Remove temporarily the test description.
  6217. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6218. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6219. $return .= '</tr>'; */
  6220. }
  6221. $return .= '<tr>';
  6222. if ($action == 'add') {
  6223. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6224. 'AddExercise'
  6225. ).'</button></td>';
  6226. } else {
  6227. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6228. 'EditCurrentExecice'
  6229. ).'</button></td>';
  6230. }
  6231. $return .= '</tr>';
  6232. $return .= '</table>';
  6233. if ($action == 'move') {
  6234. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6235. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6236. }
  6237. if (is_numeric($extra_info)) {
  6238. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6239. } elseif (is_array($extra_info)) {
  6240. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6241. }
  6242. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6243. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6244. $return .= '</form>';
  6245. $return .= '</div>';
  6246. return $return;
  6247. }
  6248. /**
  6249. * Addition of Hotpotatoes tests
  6250. * @param string Action
  6251. * @param integer Internal ID of the item
  6252. * @param mixed Extra information - can be an array with title and description indexes
  6253. * @return string HTML structure to display the hotpotatoes addition formular
  6254. */
  6255. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6256. {
  6257. $course_id = api_get_course_int_id();
  6258. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6259. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6260. if ($id != 0 && is_array($extra_info)) {
  6261. $item_title = stripslashes($extra_info['title']);
  6262. $item_description = stripslashes($extra_info['description']);
  6263. } elseif (is_numeric($extra_info)) {
  6264. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6265. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6266. WHERE c_id = ".$course_id." AND
  6267. path LIKE '".$uploadPath."/%/%htm%' AND
  6268. id = ".(int)$extra_info."
  6269. ORDER BY id ASC";
  6270. $res_hot = Database::query($sql_hot);
  6271. $row = Database::fetch_array($res_hot);
  6272. $item_title = $row['title'];
  6273. $item_description = $row['description'];
  6274. if (!empty ($row['comment'])) {
  6275. $item_title = $row['comment'];
  6276. }
  6277. } else {
  6278. $item_title = '';
  6279. $item_description = '';
  6280. }
  6281. if ($id != 0 && is_array($extra_info)) {
  6282. $parent = $extra_info['parent_item_id'];
  6283. } else {
  6284. $parent = 0;
  6285. }
  6286. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6287. $result = Database::query($sql);
  6288. $arrLP = array();
  6289. while ($row = Database :: fetch_array($result)) {
  6290. $arrLP[] = array(
  6291. 'id' => $row['id'],
  6292. 'item_type' => $row['item_type'],
  6293. 'title' => $row['title'],
  6294. 'path' => $row['path'],
  6295. 'description' => $row['description'],
  6296. 'parent_item_id' => $row['parent_item_id'],
  6297. 'previous_item_id' => $row['previous_item_id'],
  6298. 'next_item_id' => $row['next_item_id'],
  6299. 'display_order' => $row['display_order'],
  6300. 'max_score' => $row['max_score'],
  6301. 'min_score' => $row['min_score'],
  6302. 'mastery_score' => $row['mastery_score'],
  6303. 'prerequisite' => $row['prerequisite'],
  6304. 'max_time_allowed' => $row['max_time_allowed']
  6305. );
  6306. }
  6307. $legend = '<legend>';
  6308. if ($action == 'add') {
  6309. $legend .= get_lang('CreateTheExercise');
  6310. } elseif ($action == 'move') {
  6311. $legend .= get_lang('MoveTheCurrentExercise');
  6312. } else {
  6313. $legend .= get_lang('EditCurrentExecice');
  6314. }
  6315. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6316. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6317. }
  6318. $legend .= '</legend>';
  6319. $return .= '<form method="POST">';
  6320. $return .= $legend;
  6321. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6322. $return .= '<tr>';
  6323. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6324. $return .= '<td class="input">';
  6325. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6326. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6327. $arrHide = array(
  6328. $id
  6329. );
  6330. if (count($arrLP) > 0) {
  6331. for ($i = 0; $i < count($arrLP); $i++) {
  6332. if ($action != 'add') {
  6333. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6334. $arrLP[$i]['id'],
  6335. $arrHide
  6336. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6337. ) {
  6338. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6339. } else {
  6340. $arrHide[] = $arrLP[$i]['id'];
  6341. }
  6342. } else {
  6343. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6344. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6345. }
  6346. }
  6347. }
  6348. reset($arrLP);
  6349. }
  6350. $return .= '</select>';
  6351. $return .= '</td>';
  6352. $return .= '</tr>';
  6353. $return .= '<tr>';
  6354. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6355. $return .= '<td class="input">';
  6356. $return .= '<select id="previous" name="previous" size="1">';
  6357. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6358. for ($i = 0; $i < count($arrLP); $i++) {
  6359. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6360. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6361. $selected = 'selected="selected" ';
  6362. } elseif ($action == 'add') {
  6363. $selected = 'selected="selected" ';
  6364. } else {
  6365. $selected = '';
  6366. }
  6367. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6368. 'After'
  6369. ).' "'.$arrLP[$i]['title'].'"</option>';
  6370. }
  6371. }
  6372. $return .= '</select>';
  6373. $return .= '</td>';
  6374. $return .= '</tr>';
  6375. if ($action != 'move') {
  6376. $return .= '<tr>';
  6377. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6378. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6379. $return .= '</tr>';
  6380. $id_prerequisite = 0;
  6381. if (is_array($arrLP) && count($arrLP) > 0) {
  6382. foreach ($arrLP as $key => $value) {
  6383. if ($value['id'] == $id) {
  6384. $id_prerequisite = $value['prerequisite'];
  6385. break;
  6386. }
  6387. }
  6388. $arrHide = array();
  6389. for ($i = 0; $i < count($arrLP); $i++) {
  6390. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6391. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6392. $s_selected_position = $arrLP[$i]['id'];
  6393. } elseif ($action == 'add') {
  6394. $s_selected_position = 0;
  6395. }
  6396. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6397. }
  6398. }
  6399. }
  6400. }
  6401. $return .= '<tr>';
  6402. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6403. 'SaveHotpotatoes'
  6404. ).'</button></td>';
  6405. $return .= '</tr>';
  6406. $return .= '</table>';
  6407. if ($action == 'move') {
  6408. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6409. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6410. }
  6411. if (is_numeric($extra_info)) {
  6412. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6413. } elseif (is_array($extra_info)) {
  6414. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6415. }
  6416. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6417. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6418. $return .= '</form>';
  6419. return $return;
  6420. }
  6421. /**
  6422. * Return the form to display the forum edit/add option
  6423. * @param string Action (add/edit)
  6424. * @param integer ID of the lp_item if already exists
  6425. * @param mixed Forum ID or title
  6426. * @return string HTML form
  6427. */
  6428. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6429. {
  6430. $course_id = api_get_course_int_id();
  6431. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6432. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6433. if ($id != 0 && is_array($extra_info)) {
  6434. $item_title = stripslashes($extra_info['title']);
  6435. } elseif (is_numeric($extra_info)) {
  6436. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6437. FROM ".$tbl_forum."
  6438. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6439. $result = Database::query($sql_forum);
  6440. $row = Database :: fetch_array($result);
  6441. $item_title = $row['title'];
  6442. $item_description = $row['comment'];
  6443. } else {
  6444. $item_title = '';
  6445. $item_description = '';
  6446. }
  6447. $legend = '<legend>';
  6448. if ($id != 0 && is_array($extra_info)) {
  6449. $parent = $extra_info['parent_item_id'];
  6450. } else {
  6451. $parent = 0;
  6452. }
  6453. $sql = "SELECT * FROM ".$tbl_lp_item."
  6454. WHERE c_id = ".$course_id." AND
  6455. lp_id = ".$this->lp_id;
  6456. $result = Database::query($sql);
  6457. $arrLP = array();
  6458. while ($row = Database :: fetch_array($result)) {
  6459. $arrLP[] = array(
  6460. 'id' => $row['id'],
  6461. 'item_type' => $row['item_type'],
  6462. 'title' => $row['title'],
  6463. 'path' => $row['path'],
  6464. 'description' => $row['description'],
  6465. 'parent_item_id' => $row['parent_item_id'],
  6466. 'previous_item_id' => $row['previous_item_id'],
  6467. 'next_item_id' => $row['next_item_id'],
  6468. 'display_order' => $row['display_order'],
  6469. 'max_score' => $row['max_score'],
  6470. 'min_score' => $row['min_score'],
  6471. 'mastery_score' => $row['mastery_score'],
  6472. 'prerequisite' => $row['prerequisite']
  6473. );
  6474. }
  6475. $this->tree_array($arrLP);
  6476. $arrLP = $this->arrMenu;
  6477. unset ($this->arrMenu);
  6478. if ($action == 'add') {
  6479. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6480. } elseif ($action == 'move') {
  6481. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6482. } else {
  6483. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6484. }
  6485. $legend .= '</legend>';
  6486. $return .= '<div class="sectioncomment">';
  6487. $return .= '<form method="POST">';
  6488. $return .= $legend;
  6489. $return .= '<table class="lp_form">';
  6490. if ($action != 'move') {
  6491. $return .= '<tr>';
  6492. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6493. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6494. $return .= '</tr>';
  6495. }
  6496. $return .= '<tr>';
  6497. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6498. $return .= '<td class="input">';
  6499. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6500. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6501. $arrHide = array(
  6502. $id
  6503. );
  6504. //$parent_item_id = $_SESSION['parent_item_id'];
  6505. for ($i = 0; $i < count($arrLP); $i++) {
  6506. if ($action != 'add') {
  6507. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6508. $arrLP[$i]['id'],
  6509. $arrHide
  6510. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6511. ) {
  6512. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6513. } else {
  6514. $arrHide[] = $arrLP[$i]['id'];
  6515. }
  6516. } else {
  6517. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6518. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6519. }
  6520. }
  6521. }
  6522. if (is_array($arrLP)) {
  6523. reset($arrLP);
  6524. }
  6525. $return .= "\t\t\t\t".'</select>';
  6526. $return .= '</td>';
  6527. $return .= '</tr>';
  6528. $return .= '<tr>';
  6529. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6530. $return .= '<td class="input">';
  6531. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6532. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6533. for ($i = 0; $i < count($arrLP); $i++) {
  6534. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6535. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6536. $selected = 'selected="selected" ';
  6537. } elseif ($action == 'add') {
  6538. $selected = 'selected="selected" ';
  6539. } else {
  6540. $selected = '';
  6541. }
  6542. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6543. 'After'
  6544. ).' "'.$arrLP[$i]['title'].'"</option>';
  6545. }
  6546. }
  6547. $return .= "\t\t\t\t".'</select>';
  6548. $return .= '</td>';
  6549. $return .= '</tr>';
  6550. if ($action != 'move') {
  6551. $return .= '<tr>';
  6552. $return .= '</tr>';
  6553. $id_prerequisite = 0;
  6554. if (is_array($arrLP)) {
  6555. foreach ($arrLP as $key => $value) {
  6556. if ($value['id'] == $id) {
  6557. $id_prerequisite = $value['prerequisite'];
  6558. break;
  6559. }
  6560. }
  6561. }
  6562. $arrHide = array();
  6563. for ($i = 0; $i < count($arrLP); $i++) {
  6564. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6565. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6566. $s_selected_position = $arrLP[$i]['id'];
  6567. } elseif ($action == 'add') {
  6568. $s_selected_position = 0;
  6569. }
  6570. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6571. }
  6572. }
  6573. $return .= '</tr>';
  6574. }
  6575. $return .= '<tr>';
  6576. if ($action == 'add') {
  6577. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6578. 'AddForumToCourse'
  6579. ).' </button></td>';
  6580. } else {
  6581. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6582. 'EditCurrentForum'
  6583. ).' </button></td>';
  6584. }
  6585. $return .= '</tr>';
  6586. $return .= '</table>';
  6587. if ($action == 'move') {
  6588. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6589. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6590. }
  6591. if (is_numeric($extra_info)) {
  6592. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6593. } elseif (is_array($extra_info)) {
  6594. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6595. }
  6596. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6597. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6598. $return .= '</form>';
  6599. $return .= '</div>';
  6600. return $return;
  6601. }
  6602. /**
  6603. * Return HTML form to add/edit forum threads
  6604. * @param string Action (add/edit)
  6605. * @param integer Item ID if already exists in learning path
  6606. * @param mixed Extra information (thread ID if integer)
  6607. * @return string HTML form
  6608. */
  6609. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6610. {
  6611. $course_id = api_get_course_int_id();
  6612. if (empty($course_id)) {
  6613. return null;
  6614. }
  6615. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6616. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6617. if ($id != 0 && is_array($extra_info)) {
  6618. $item_title = stripslashes($extra_info['title']);
  6619. } elseif (is_numeric($extra_info)) {
  6620. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6621. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6622. $result = Database::query($sql_forum);
  6623. $row = Database :: fetch_array($result);
  6624. $item_title = $row['title'];
  6625. $item_description = '';
  6626. } else {
  6627. $item_title = '';
  6628. $item_description = '';
  6629. }
  6630. $return = null;
  6631. if ($id != 0 && is_array($extra_info)) {
  6632. $parent = $extra_info['parent_item_id'];
  6633. } else {
  6634. $parent = 0;
  6635. }
  6636. $sql = "SELECT * FROM ".$tbl_lp_item."
  6637. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6638. $result = Database::query($sql);
  6639. $arrLP = array();
  6640. while ($row = Database :: fetch_array($result)) {
  6641. $arrLP[] = array(
  6642. 'id' => $row['id'],
  6643. 'item_type' => $row['item_type'],
  6644. 'title' => $row['title'],
  6645. 'path' => $row['path'],
  6646. 'description' => $row['description'],
  6647. 'parent_item_id' => $row['parent_item_id'],
  6648. 'previous_item_id' => $row['previous_item_id'],
  6649. 'next_item_id' => $row['next_item_id'],
  6650. 'display_order' => $row['display_order'],
  6651. 'max_score' => $row['max_score'],
  6652. 'min_score' => $row['min_score'],
  6653. 'mastery_score' => $row['mastery_score'],
  6654. 'prerequisite' => $row['prerequisite']
  6655. );
  6656. }
  6657. $this->tree_array($arrLP);
  6658. $arrLP = $this->arrMenu;
  6659. unset ($this->arrMenu);
  6660. $return .= '<form method="POST">';
  6661. if ($action == 'add') {
  6662. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6663. } elseif ($action == 'move') {
  6664. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6665. } else {
  6666. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6667. }
  6668. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6669. $return .= '<tr>';
  6670. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6671. $return .= '<td class="input">';
  6672. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6673. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6674. $arrHide = array(
  6675. $id
  6676. );
  6677. for ($i = 0; $i < count($arrLP); $i++) {
  6678. if ($action != 'add') {
  6679. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6680. $arrLP[$i]['id'],
  6681. $arrHide
  6682. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6683. ) {
  6684. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6685. } else {
  6686. $arrHide[] = $arrLP[$i]['id'];
  6687. }
  6688. } else {
  6689. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6690. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6691. }
  6692. }
  6693. }
  6694. if ($arrLP != null) {
  6695. reset($arrLP);
  6696. }
  6697. $return .= '</select>';
  6698. $return .= '</td>';
  6699. $return .= '</tr>';
  6700. $return .= '<tr>';
  6701. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6702. $return .= '<td class="input">';
  6703. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6704. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6705. for ($i = 0; $i < count($arrLP); $i++) {
  6706. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6707. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6708. $selected = 'selected="selected" ';
  6709. } elseif ($action == 'add') {
  6710. $selected = 'selected="selected" ';
  6711. } else {
  6712. $selected = '';
  6713. }
  6714. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6715. 'After'
  6716. ).' "'.$arrLP[$i]['title'].'"</option>';
  6717. }
  6718. }
  6719. $return .= "\t\t\t\t".'</select>';
  6720. $return .= '</td>';
  6721. $return .= '</tr>';
  6722. if ($action != 'move') {
  6723. $return .= '<tr>';
  6724. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6725. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6726. $return .= '</tr>';
  6727. $return .= '<tr>';
  6728. $return .= '</tr>';
  6729. $id_prerequisite = 0;
  6730. if ($arrLP != null) {
  6731. foreach ($arrLP as $key => $value) {
  6732. if ($value['id'] == $id) {
  6733. $id_prerequisite = $value['prerequisite'];
  6734. break;
  6735. }
  6736. }
  6737. }
  6738. $arrHide = array();
  6739. for ($i = 0; $i < count($arrLP); $i++) {
  6740. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6741. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6742. $s_selected_position = $arrLP[$i]['id'];
  6743. } elseif ($action == 'add') {
  6744. $s_selected_position = 0;
  6745. }
  6746. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6747. }
  6748. }
  6749. $return .= '<tr>';
  6750. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6751. 'LearnpathPrerequisites'
  6752. ).'</label></td>';
  6753. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6754. 'NoPrerequisites'
  6755. ).'</option>';
  6756. foreach ($arrHide as $key => $value) {
  6757. if ($key == $s_selected_position && $action == 'add') {
  6758. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6759. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6760. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6761. } else {
  6762. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6763. }
  6764. }
  6765. $return .= "</select></td>";
  6766. $return .= '</tr>';
  6767. }
  6768. $return .= '<tr>';
  6769. $return .= '<td></td><td>
  6770. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6771. 'Ok'
  6772. ).'</button></td>';
  6773. $return .= '</tr>';
  6774. $return .= '</table>';
  6775. if ($action == 'move') {
  6776. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6777. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6778. }
  6779. if (is_numeric($extra_info)) {
  6780. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6781. } elseif (is_array($extra_info)) {
  6782. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6783. }
  6784. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6785. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6786. $return .= '</form>';
  6787. $return .= '</div>';
  6788. return $return;
  6789. }
  6790. /**
  6791. * Return the HTML form to display an item (generally a section/module item)
  6792. * @param string Item type (module/dokeos_module)
  6793. * @param string Title (optional, only when creating)
  6794. * @param string Action ('add'/'edit')
  6795. * @param integer lp_item ID
  6796. * @param mixed Extra info
  6797. * @return string HTML form
  6798. */
  6799. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6800. {
  6801. $course_id = api_get_course_int_id();
  6802. $_course = api_get_course_info();
  6803. global $charset;
  6804. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6805. if ($id != 0 && is_array($extra_info)) {
  6806. $item_title = $extra_info['title'];
  6807. $item_description = $extra_info['description'];
  6808. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6809. $extra_info['path']
  6810. );
  6811. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6812. } else {
  6813. $item_title = '';
  6814. $item_description = '';
  6815. $item_path_fck = '';
  6816. }
  6817. if ($id != 0 && is_array($extra_info)) {
  6818. $parent = $extra_info['parent_item_id'];
  6819. } else {
  6820. $parent = 0;
  6821. }
  6822. $id = intval($id);
  6823. $sql = "SELECT * FROM ".$tbl_lp_item."
  6824. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6825. if ($item_type == 'module') {
  6826. $sql .= " AND parent_item_id = 0";
  6827. }
  6828. $result = Database::query($sql);
  6829. $arrLP = array();
  6830. while ($row = Database :: fetch_array($result)) {
  6831. $arrLP[] = array(
  6832. 'id' => $row['id'],
  6833. 'item_type' => $row['item_type'],
  6834. 'title' => $row['title'],
  6835. 'path' => $row['path'],
  6836. 'description' => $row['description'],
  6837. 'parent_item_id' => $row['parent_item_id'],
  6838. 'previous_item_id' => $row['previous_item_id'],
  6839. 'next_item_id' => $row['next_item_id'],
  6840. 'max_score' => $row['max_score'],
  6841. 'min_score' => $row['min_score'],
  6842. 'mastery_score' => $row['mastery_score'],
  6843. 'prerequisite' => $row['prerequisite'],
  6844. 'display_order' => $row['display_order']
  6845. );
  6846. }
  6847. $this->tree_array($arrLP);
  6848. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6849. unset($this->arrMenu);
  6850. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6851. $url = api_get_self().'?'.api_get_cidreq(
  6852. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6853. $form = new FormValidator('form', 'POST', $url);
  6854. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6855. $defaults['description'] = $item_description;
  6856. $form->addElement('header', $title);
  6857. //$arrHide = array($id);
  6858. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6859. $arrHide[0]['padding'] = 3;
  6860. $charset = api_get_system_encoding();
  6861. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6862. for ($i = 0; $i < count($arrLP); $i++) {
  6863. if ($action != 'add') {
  6864. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6865. $arrLP[$i]['id'],
  6866. $arrHide
  6867. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6868. ) {
  6869. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6870. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6871. if ($parent == $arrLP[$i]['id']) {
  6872. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6873. }
  6874. }
  6875. } else {
  6876. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6877. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6878. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6879. if ($parent == $arrLP[$i]['id']) {
  6880. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6881. }
  6882. }
  6883. }
  6884. }
  6885. if ($action != 'move') {
  6886. $form->addElement(
  6887. 'text',
  6888. 'title',
  6889. get_lang('Title'),
  6890. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6891. );
  6892. $form->applyFilter('title', 'html_filter');
  6893. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6894. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6895. } else {
  6896. $form->addElement('hidden', 'title');
  6897. }
  6898. $parent_select = $form->addElement(
  6899. 'select',
  6900. 'parent',
  6901. get_lang('Parent'),
  6902. '',
  6903. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6904. );
  6905. foreach ($arrHide as $key => $value) {
  6906. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6907. }
  6908. if (!empty($s_selected_parent)) {
  6909. $parent_select->setSelected($s_selected_parent);
  6910. }
  6911. }
  6912. if (is_array($arrLP)) {
  6913. reset($arrLP);
  6914. }
  6915. $arrHide = array();
  6916. // POSITION
  6917. for ($i = 0; $i < count($arrLP); $i++) {
  6918. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6919. //this is the same!
  6920. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6921. $s_selected_position = $arrLP[$i]['id'];
  6922. } elseif ($action == 'add') {
  6923. $s_selected_position = $arrLP[$i]['id'];
  6924. }
  6925. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6926. }
  6927. }
  6928. $position = $form->addElement(
  6929. 'select',
  6930. 'previous',
  6931. get_lang('Position'),
  6932. '',
  6933. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6934. );
  6935. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6936. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6937. foreach ($arrHide as $key => $value) {
  6938. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6939. }
  6940. if (!empty ($s_selected_position)) {
  6941. $position->setSelected($s_selected_position);
  6942. }
  6943. if (is_array($arrLP)) {
  6944. reset($arrLP);
  6945. }
  6946. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6947. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6948. $form->addElement('hidden', 'parent', '0');
  6949. }
  6950. //fix in order to use the tab
  6951. if ($item_type == 'chapter') {
  6952. $form->addElement('hidden', 'type', 'chapter');
  6953. }
  6954. $extension = null;
  6955. if (!empty($item_path)) {
  6956. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6957. }
  6958. //assets can't be modified
  6959. //$item_type == 'asset' ||
  6960. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6961. if ($item_type == 'sco') {
  6962. $form->addElement(
  6963. 'html',
  6964. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  6965. );
  6966. }
  6967. $renderer = $form->defaultRenderer();
  6968. $renderer->setElementTemplate(
  6969. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  6970. 'content_lp'
  6971. );
  6972. $relative_prefix = '';
  6973. $editor_config = array(
  6974. 'ToolbarSet' => 'LearningPathDocuments',
  6975. 'Width' => '100%',
  6976. 'Height' => '500',
  6977. 'FullPage' => true,
  6978. 'CreateDocumentDir' => $relative_prefix,
  6979. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  6980. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  6981. );
  6982. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6983. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6984. //$defaults['content_lp'] = file_get_contents($item_path);
  6985. $defaults['content_lp'] = file_get_contents($content_path);
  6986. }
  6987. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  6988. $form->addElement('hidden', 'post_time', time());
  6989. $form->setDefaults($defaults);
  6990. return $form->return_form();
  6991. }
  6992. /**
  6993. * Returns the form to update or create a document
  6994. * @param string Action (add/edit)
  6995. * @param integer ID of the lp_item (if already exists)
  6996. * @param mixed Integer if document ID, string if info ('new')
  6997. * @return string HTML form
  6998. */
  6999. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7000. {
  7001. $course_id = api_get_course_int_id();
  7002. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7003. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7004. $no_display_edit_textarea = false;
  7005. //If action==edit document
  7006. //We don't display the document form if it's not an editable document (html or txt file)
  7007. if ($action == "edit") {
  7008. if (is_array($extra_info)) {
  7009. $path_parts = pathinfo($extra_info['dir']);
  7010. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7011. $no_display_edit_textarea = true;
  7012. }
  7013. }
  7014. }
  7015. $no_display_add = false;
  7016. // If action==add an existing document
  7017. // We don't display the document form if it's not an editable document (html or txt file).
  7018. if ($action == "add") {
  7019. if (is_numeric($extra_info)) {
  7020. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  7021. $extra_info
  7022. );
  7023. $result = Database::query($sql_doc);
  7024. $path_file = Database :: result($result, 0, 0);
  7025. $path_parts = pathinfo($path_file);
  7026. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7027. $no_display_add = true;
  7028. }
  7029. }
  7030. }
  7031. if ($id != 0 && is_array($extra_info)) {
  7032. $item_title = stripslashes($extra_info['title']);
  7033. $item_description = stripslashes($extra_info['description']);
  7034. $item_terms = stripslashes($extra_info['terms']);
  7035. if (empty ($item_title)) {
  7036. $path_parts = pathinfo($extra_info['path']);
  7037. $item_title = stripslashes($path_parts['filename']);
  7038. }
  7039. } elseif (is_numeric($extra_info)) {
  7040. $sql_doc = "SELECT path, title FROM ".$tbl_doc."
  7041. WHERE c_id = ".$course_id." AND id = ".Database::escape_string($extra_info);
  7042. $result = Database::query($sql_doc);
  7043. $row = Database::fetch_array($result);
  7044. $explode = explode('.', $row['title']);
  7045. if (count($explode) > 1) {
  7046. for ($i = 0; $i < count($explode) - 1; $i++) {
  7047. $item_title .= $explode[$i];
  7048. }
  7049. } else {
  7050. $item_title = $row['title'];
  7051. }
  7052. $item_title = str_replace('_', ' ', $item_title);
  7053. if (empty ($item_title)) {
  7054. $path_parts = pathinfo($row['path']);
  7055. $item_title = stripslashes($path_parts['filename']);
  7056. }
  7057. } else {
  7058. $item_title = '';
  7059. $item_description = '';
  7060. }
  7061. $return = '<legend>';
  7062. if ($id != 0 && is_array($extra_info)) {
  7063. $parent = $extra_info['parent_item_id'];
  7064. } else {
  7065. $parent = 0;
  7066. }
  7067. $sql = "SELECT * FROM ".$tbl_lp_item."
  7068. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7069. $result = Database::query($sql);
  7070. $arrLP = array();
  7071. while ($row = Database :: fetch_array($result)) {
  7072. $arrLP[] = array(
  7073. 'id' => $row['id'],
  7074. 'item_type' => $row['item_type'],
  7075. 'title' => $row['title'],
  7076. 'path' => $row['path'],
  7077. 'description' => $row['description'],
  7078. 'parent_item_id' => $row['parent_item_id'],
  7079. 'previous_item_id' => $row['previous_item_id'],
  7080. 'next_item_id' => $row['next_item_id'],
  7081. 'display_order' => $row['display_order'],
  7082. 'max_score' => $row['max_score'],
  7083. 'min_score' => $row['min_score'],
  7084. 'mastery_score' => $row['mastery_score'],
  7085. 'prerequisite' => $row['prerequisite']
  7086. );
  7087. }
  7088. $this->tree_array($arrLP);
  7089. $arrLP = null;
  7090. if (isset($this->arrMenu)) {
  7091. $arrLP = $this->arrMenu;
  7092. unset ($this->arrMenu);
  7093. }
  7094. if ($action == 'add') {
  7095. $return .= get_lang('CreateTheDocument');
  7096. } elseif ($action == 'move') {
  7097. $return .= get_lang('MoveTheCurrentDocument');
  7098. } else {
  7099. $return .= get_lang('EditTheCurrentDocument');
  7100. }
  7101. $return .= '</legend>';
  7102. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7103. $return .= Display :: return_warning_message(
  7104. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7105. false
  7106. );
  7107. }
  7108. $form = new FormValidator('form', 'POST', api_get_self(
  7109. ).'?'.$_SERVER['QUERY_STRING'], '', array('enctype' => "multipart/form-data"));
  7110. $defaults['title'] = Security :: remove_XSS($item_title);
  7111. if (empty($item_title)) {
  7112. $defaults['title'] = Security::remove_XSS($item_title);
  7113. }
  7114. $defaults['description'] = $item_description;
  7115. $form->addElement('html', $return);
  7116. if ($action != 'move') {
  7117. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  7118. $form->applyFilter('title', 'html_filter');
  7119. }
  7120. //$arrHide = array($id);
  7121. $arrHide[0]['value'] = $this->name;
  7122. $arrHide[0]['padding'] = 3;
  7123. for ($i = 0; $i < count($arrLP); $i++) {
  7124. if ($action != 'add') {
  7125. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7126. $arrLP[$i]['id'],
  7127. $arrHide
  7128. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7129. ) {
  7130. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7131. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7132. if ($parent == $arrLP[$i]['id']) {
  7133. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7134. }
  7135. }
  7136. } else {
  7137. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7138. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7139. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7140. if ($parent == $arrLP[$i]['id']) {
  7141. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7142. }
  7143. }
  7144. }
  7145. }
  7146. $parent_select = $form->addElement(
  7147. 'select',
  7148. 'parent',
  7149. get_lang('Parent'),
  7150. '',
  7151. 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"'
  7152. );
  7153. $my_count = 0;
  7154. foreach ($arrHide as $key => $value) {
  7155. if ($my_count != 0) {
  7156. // The LP name is also the first section and is not in the same charset like the other sections.
  7157. $value['value'] = Security :: remove_XSS($value['value']);
  7158. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7159. } else {
  7160. $value['value'] = Security :: remove_XSS($value['value']);
  7161. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7162. }
  7163. $my_count++;
  7164. }
  7165. if (!empty($id)) {
  7166. $parent_select->setSelected($parent);
  7167. } else {
  7168. $parent_item_id = Session::read('parent_item_id');
  7169. $parent_select->setSelected($parent_item_id);
  7170. }
  7171. if (is_array($arrLP)) {
  7172. reset($arrLP);
  7173. }
  7174. $arrHide = array();
  7175. $s_selected_position = null;
  7176. //POSITION
  7177. for ($i = 0; $i < count($arrLP); $i++) {
  7178. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7179. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7180. $s_selected_position = $arrLP[$i]['id'];
  7181. } elseif ($action == 'add') {
  7182. $s_selected_position = $arrLP[$i]['id'];
  7183. }
  7184. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7185. }
  7186. }
  7187. $position = $form->addElement(
  7188. 'select',
  7189. 'previous',
  7190. get_lang('Position'),
  7191. '',
  7192. 'id="previous" class="learnpath_item_form" style="width:40%;"'
  7193. );
  7194. $position->addOption(get_lang('FirstPosition'), 0);
  7195. foreach ($arrHide as $key => $value) {
  7196. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7197. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7198. }
  7199. $position->setSelected($s_selected_position);
  7200. if (is_array($arrLP)) {
  7201. reset($arrLP);
  7202. }
  7203. if ($action != 'move') {
  7204. $id_prerequisite = 0;
  7205. if (is_array($arrLP)) {
  7206. foreach ($arrLP as $key => $value) {
  7207. if ($value['id'] == $id) {
  7208. $id_prerequisite = $value['prerequisite'];
  7209. break;
  7210. }
  7211. }
  7212. }
  7213. $arrHide = array();
  7214. for ($i = 0; $i < count($arrLP); $i++) {
  7215. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7216. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7217. $s_selected_position = $arrLP[$i]['id'];
  7218. } elseif ($action == 'add') {
  7219. $s_selected_position = $arrLP[$i]['id'];
  7220. }
  7221. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7222. }
  7223. }
  7224. if (!$no_display_add) {
  7225. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  7226. if (isset ($_POST['content'])) {
  7227. $content = stripslashes($_POST['content']);
  7228. } elseif (is_array($extra_info)) {
  7229. //If it's an html document or a text file
  7230. if (!$no_display_edit_textarea) {
  7231. $content = $this->display_document($extra_info['path'], false, false);
  7232. }
  7233. } elseif (is_numeric($extra_info)) {
  7234. $content = $this->display_document($extra_info, false, false);
  7235. } else {
  7236. $content = '';
  7237. }
  7238. if (!$no_display_edit_textarea) {
  7239. // We need to calculate here some specific settings for the online editor.
  7240. // The calculated settings work for documents in the Documents tool
  7241. // (on the root or in subfolders).
  7242. // For documents in native scorm packages it is unclear whether the
  7243. // online editor should be activated or not.
  7244. // A new document, it is in the root of the repository.
  7245. $relative_path = '';
  7246. $relative_prefix = '';
  7247. if (is_array($extra_info) && $extra_info != 'new') {
  7248. // The document already exists. Whe have to determine its relative path towards the repository root.
  7249. $relative_path = explode('/', $extra_info['dir']);
  7250. $cnt = count($relative_path) - 2;
  7251. if ($cnt < 0) {
  7252. $cnt = 0;
  7253. }
  7254. $relative_prefix = str_repeat('../', $cnt);
  7255. $relative_path = array_slice($relative_path, 1, $cnt);
  7256. $relative_path = implode('/', $relative_path);
  7257. if (strlen($relative_path) > 0) {
  7258. $relative_path = $relative_path.'/';
  7259. }
  7260. } else {
  7261. $_course = api_get_course_info();
  7262. $result = $this->generate_lp_folder($_course);
  7263. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7264. $relative_prefix = '../../';
  7265. }
  7266. $editor_config = array(
  7267. 'ToolbarSet' => 'LearningPathDocuments',
  7268. 'Width' => '100%',
  7269. 'Height' => '500',
  7270. 'FullPage' => true,
  7271. 'CreateDocumentDir' => $relative_prefix,
  7272. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7273. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path(
  7274. ).'/document/'.$relative_path
  7275. );
  7276. if ($_GET['action'] == 'add_item') {
  7277. $class = 'add';
  7278. $text = get_lang('LPCreateDocument');
  7279. } else {
  7280. if ($_GET['action'] == 'edit_item') {
  7281. $class = 'save';
  7282. $text = get_lang('SaveDocument');
  7283. }
  7284. }
  7285. $form->addElement('style_submit_button', 'submit_button', $text, 'class="'.$class.'"');
  7286. $renderer = $form->defaultRenderer();
  7287. $renderer->setElementTemplate(
  7288. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7289. 'content_lp'
  7290. );
  7291. $form->addElement('html', '<div>');
  7292. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7293. $form->addElement('html', '</div>');
  7294. $defaults['content_lp'] = $content;
  7295. }
  7296. } elseif (is_numeric($extra_info)) {
  7297. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7298. $return = $this->display_document($extra_info, true, true, true);
  7299. $form->addElement('html', $return);
  7300. }
  7301. }
  7302. }
  7303. if ($action == 'move') {
  7304. $form->addElement('hidden', 'title', $item_title);
  7305. $form->addElement('hidden', 'description', $item_description);
  7306. }
  7307. if (is_numeric($extra_info)) {
  7308. $form->addElement(
  7309. 'style_submit_button',
  7310. 'submit_button',
  7311. get_lang('SaveDocument'),
  7312. 'value="submit_button", class="save"'
  7313. );
  7314. $form->addElement('hidden', 'path', $extra_info);
  7315. } elseif (is_array($extra_info)) {
  7316. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7317. $form->addElement('hidden', 'path', $extra_info['path']);
  7318. }
  7319. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7320. $form->addElement('hidden', 'post_time', time());
  7321. $form->setDefaults($defaults);
  7322. return $form->return_form();
  7323. }
  7324. /**
  7325. * Return HTML form to add/edit a link item
  7326. * @param string Action (add/edit)
  7327. * @param integer Item ID if exists
  7328. * @param mixed Extra info
  7329. * @return string HTML form
  7330. */
  7331. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7332. {
  7333. $course_id = api_get_course_int_id();
  7334. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7335. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7336. if ($id != 0 && is_array($extra_info)) {
  7337. $item_title = stripslashes($extra_info['title']);
  7338. $item_description = stripslashes($extra_info['description']);
  7339. $item_url = stripslashes($extra_info['url']);
  7340. } elseif (is_numeric($extra_info)) {
  7341. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7342. $result = Database::query($sql_link);
  7343. $row = Database :: fetch_array($result);
  7344. $item_title = $row['title'];
  7345. $item_description = $row['description'];
  7346. $item_url = $row['url'];
  7347. } else {
  7348. $item_title = '';
  7349. $item_description = '';
  7350. $item_url = '';
  7351. }
  7352. $legend = '<legend>';
  7353. if ($id != 0 && is_array($extra_info)) {
  7354. $parent = $extra_info['parent_item_id'];
  7355. } else {
  7356. $parent = 0;
  7357. }
  7358. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7359. $result = Database::query($sql);
  7360. $arrLP = array();
  7361. while ($row = Database :: fetch_array($result)) {
  7362. $arrLP[] = array(
  7363. 'id' => $row['id'],
  7364. 'item_type' => $row['item_type'],
  7365. 'title' => $row['title'],
  7366. 'path' => $row['path'],
  7367. 'description' => $row['description'],
  7368. 'parent_item_id' => $row['parent_item_id'],
  7369. 'previous_item_id' => $row['previous_item_id'],
  7370. 'next_item_id' => $row['next_item_id'],
  7371. 'display_order' => $row['display_order'],
  7372. 'max_score' => $row['max_score'],
  7373. 'min_score' => $row['min_score'],
  7374. 'mastery_score' => $row['mastery_score'],
  7375. 'prerequisite' => $row['prerequisite']
  7376. );
  7377. }
  7378. $this->tree_array($arrLP);
  7379. $arrLP = $this->arrMenu;
  7380. unset ($this->arrMenu);
  7381. if ($action == 'add') {
  7382. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7383. } elseif ($action == 'move') {
  7384. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7385. } else {
  7386. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7387. }
  7388. $legend .= '</legend>';
  7389. $return .= '<div class="sectioncomment">';
  7390. $return .= '<form method="POST">';
  7391. $return .= $legend;
  7392. $return .= '<table>';
  7393. if ($action != 'move') {
  7394. $return .= '<tr>';
  7395. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7396. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7397. $return .= '</tr>';
  7398. }
  7399. $return .= '<tr>';
  7400. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7401. $return .= '<td class="input">';
  7402. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7403. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7404. $arrHide = array(
  7405. $id
  7406. );
  7407. $parent_item_id = $_SESSION['parent_item_id'];
  7408. for ($i = 0; $i < count($arrLP); $i++) {
  7409. if ($action != 'add') {
  7410. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7411. $arrLP[$i]['id'],
  7412. $arrHide
  7413. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7414. ) {
  7415. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7416. } else {
  7417. $arrHide[] = $arrLP[$i]['id'];
  7418. }
  7419. } else {
  7420. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7421. $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>';
  7422. }
  7423. }
  7424. }
  7425. if (is_array($arrLP)) {
  7426. reset($arrLP);
  7427. }
  7428. $return .= '</select>';
  7429. $return .= '</td>';
  7430. $return .= '</tr>';
  7431. $return .= '<tr>';
  7432. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7433. $return .= '<td class="input">';
  7434. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7435. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7436. for ($i = 0; $i < count($arrLP); $i++) {
  7437. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7438. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7439. $selected = 'selected="selected" ';
  7440. } elseif ($action == 'add') {
  7441. $selected = 'selected="selected" ';
  7442. } else {
  7443. $selected = '';
  7444. }
  7445. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7446. 'After'
  7447. ).' "'.$arrLP[$i]['title'].'"</option>';
  7448. }
  7449. }
  7450. $return .= '</select>';
  7451. $return .= '</td>';
  7452. $return .= '</tr>';
  7453. if ($action != 'move') {
  7454. $return .= '<tr>';
  7455. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7456. $return .= '<td class="input"><input'.(is_numeric(
  7457. $extra_info
  7458. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7459. $return .= '</tr>';
  7460. $id_prerequisite = 0;
  7461. if (is_array($arrLP)) {
  7462. foreach ($arrLP as $key => $value) {
  7463. if ($value['id'] == $id) {
  7464. $id_prerequisite = $value['prerequisite'];
  7465. break;
  7466. }
  7467. }
  7468. }
  7469. $arrHide = array();
  7470. for ($i = 0; $i < count($arrLP); $i++) {
  7471. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7472. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7473. $s_selected_position = $arrLP[$i]['id'];
  7474. } elseif ($action == 'add') {
  7475. $s_selected_position = 0;
  7476. }
  7477. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7478. }
  7479. }
  7480. $return .= '</tr>';
  7481. }
  7482. $return .= '<tr>';
  7483. if ($action == 'add') {
  7484. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7485. 'AddLinkToCourse'
  7486. ).'</button></td>';
  7487. } else {
  7488. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7489. 'EditCurrentLink'
  7490. ).'</button></td>';
  7491. }
  7492. $return .= '</tr>';
  7493. $return .= '</table>';
  7494. if ($action == 'move') {
  7495. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7496. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7497. }
  7498. if (is_numeric($extra_info)) {
  7499. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7500. } elseif (is_array($extra_info)) {
  7501. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7502. }
  7503. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7504. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7505. $return .= '</form>';
  7506. $return .= '</div>';
  7507. return $return;
  7508. }
  7509. /**
  7510. * Return HTML form to add/edit a student publication (work)
  7511. * @param string Action (add/edit)
  7512. * @param integer Item ID if already exists
  7513. * @param mixed Extra info (work ID if integer)
  7514. * @return string HTML form
  7515. */
  7516. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7517. {
  7518. $course_id = api_get_course_int_id();
  7519. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7520. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7521. if ($id != 0 && is_array($extra_info)) {
  7522. $item_title = stripslashes($extra_info['title']);
  7523. $item_description = stripslashes($extra_info['description']);
  7524. } elseif (is_numeric($extra_info)) {
  7525. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7526. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7527. $result = Database::query($sql_publication);
  7528. $row = Database :: fetch_array($result);
  7529. $item_title = $row['title'];
  7530. } else {
  7531. $item_title = get_lang('Student_publication');
  7532. }
  7533. $legend = '<legend>';
  7534. if ($id != 0 && is_array($extra_info)) {
  7535. $parent = $extra_info['parent_item_id'];
  7536. } else {
  7537. $parent = 0;
  7538. }
  7539. $sql = "SELECT * FROM ".$tbl_lp_item."
  7540. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7541. $result = Database::query($sql);
  7542. $arrLP = array();
  7543. while ($row = Database :: fetch_array($result)) {
  7544. $arrLP[] = array(
  7545. 'id' => $row['id'],
  7546. 'item_type' => $row['item_type'],
  7547. 'title' => $row['title'],
  7548. 'path' => $row['path'],
  7549. 'description' => $row['description'],
  7550. 'parent_item_id' => $row['parent_item_id'],
  7551. 'previous_item_id' => $row['previous_item_id'],
  7552. 'next_item_id' => $row['next_item_id'],
  7553. 'display_order' => $row['display_order'],
  7554. 'max_score' => $row['max_score'],
  7555. 'min_score' => $row['min_score'],
  7556. 'mastery_score' => $row['mastery_score'],
  7557. 'prerequisite' => $row['prerequisite']
  7558. );
  7559. }
  7560. $this->tree_array($arrLP);
  7561. $arrLP = $this->arrMenu;
  7562. unset ($this->arrMenu);
  7563. if ($action == 'add') {
  7564. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7565. } elseif ($action == 'move') {
  7566. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7567. } else {
  7568. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7569. }
  7570. $legend .= '</legend>';
  7571. $return .= '<div class="sectioncomment">';
  7572. $return .= '<form method="POST">';
  7573. $return .= $legend;
  7574. $return .= '<table class="lp_form">';
  7575. if ($action != 'move') {
  7576. $return .= '<tr>';
  7577. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7578. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7579. $return .= '</tr>';
  7580. }
  7581. $return .= '<tr>';
  7582. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7583. $return .= '<td class="input">';
  7584. $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">';
  7585. //$parent_item_id = $_SESSION['parent_item_id'];
  7586. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7587. $arrHide = array(
  7588. $id
  7589. );
  7590. for ($i = 0; $i < count($arrLP); $i++) {
  7591. if ($action != 'add') {
  7592. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7593. $arrLP[$i]['id'],
  7594. $arrHide
  7595. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7596. ) {
  7597. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7598. } else {
  7599. $arrHide[] = $arrLP[$i]['id'];
  7600. }
  7601. } else {
  7602. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7603. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7604. }
  7605. }
  7606. }
  7607. if (is_array($arrLP)) {
  7608. reset($arrLP);
  7609. }
  7610. $return .= "\t\t\t\t".'</select>';
  7611. $return .= '</td>';
  7612. $return .= '</tr>';
  7613. $return .= '<tr>';
  7614. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7615. $return .= '<td class="input">';
  7616. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7617. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7618. for ($i = 0; $i < count($arrLP); $i++) {
  7619. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7620. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7621. $selected = 'selected="selected" ';
  7622. } elseif ($action == 'add') {
  7623. $selected = 'selected="selected" ';
  7624. } else {
  7625. $selected = '';
  7626. }
  7627. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7628. 'After'
  7629. ).' "'.$arrLP[$i]['title'].'"</option>';
  7630. }
  7631. }
  7632. $return .= "\t\t\t\t".'</select>';
  7633. $return .= '</td>';
  7634. $return .= '</tr>';
  7635. if ($action != 'move') {
  7636. $id_prerequisite = 0;
  7637. if (is_array($arrLP)) {
  7638. foreach ($arrLP as $key => $value) {
  7639. if ($value['id'] == $id) {
  7640. $id_prerequisite = $value['prerequisite'];
  7641. break;
  7642. }
  7643. }
  7644. }
  7645. $arrHide = array();
  7646. for ($i = 0; $i < count($arrLP); $i++) {
  7647. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7648. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7649. $s_selected_position = $arrLP[$i]['id'];
  7650. } elseif ($action == 'add') {
  7651. $s_selected_position = 0;
  7652. }
  7653. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7654. }
  7655. }
  7656. // Commented the prerequisites, only visible in edit (work).
  7657. /*
  7658. $return .= '<tr>';
  7659. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7660. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7661. foreach($arrHide as $key => $value) {
  7662. if ($key == $s_selected_position && $action == 'add') {
  7663. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7664. }
  7665. elseif ($key == $id_prerequisite && $action == 'edit') {
  7666. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7667. }
  7668. else {
  7669. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7670. }
  7671. }
  7672. $return .= "</select></td>";
  7673. */
  7674. $return .= '</tr>';
  7675. }
  7676. $return .= '<tr>';
  7677. if ($action == 'add') {
  7678. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7679. 'AddAssignmentToCourse'
  7680. ).'</button></td>';
  7681. } else {
  7682. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7683. 'EditCurrentStudentPublication'
  7684. ).'</button></td>';
  7685. }
  7686. $return .= '</tr>';
  7687. $return .= '</table>';
  7688. if ($action == 'move') {
  7689. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7690. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7691. }
  7692. if (is_numeric($extra_info)) {
  7693. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7694. } elseif (is_array($extra_info)) {
  7695. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7696. }
  7697. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7698. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7699. $return .= '</form>';
  7700. $return .= '</div>';
  7701. return $return;
  7702. }
  7703. /**
  7704. * Displays the menu for manipulating a step
  7705. * @return string html
  7706. */
  7707. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7708. {
  7709. $course_id = api_get_course_int_id();
  7710. $course_code = api_get_course_id();
  7711. $_course = api_get_course_info();
  7712. $return = '<div class="actions">';
  7713. switch ($item_type) {
  7714. case 'dokeos_chapter' :
  7715. case 'chapter' :
  7716. // Commented the message cause should not show it.
  7717. //$lang = get_lang('TitleManipulateChapter');
  7718. break;
  7719. case 'dokeos_module' :
  7720. case 'module' :
  7721. // Commented the message cause should not show it.
  7722. //$lang = get_lang('TitleManipulateModule');
  7723. break;
  7724. case TOOL_DOCUMENT :
  7725. // Commented the message cause should not show it.
  7726. //$lang = get_lang('TitleManipulateDocument');
  7727. break;
  7728. case TOOL_LINK :
  7729. case 'link' :
  7730. // Commented the message cause should not show it.
  7731. //$lang = get_lang('TitleManipulateLink');
  7732. break;
  7733. case TOOL_QUIZ :
  7734. // Commented the message cause should not show it.
  7735. //$lang = get_lang('TitleManipulateQuiz');
  7736. break;
  7737. case TOOL_STUDENTPUBLICATION :
  7738. // Commented the message cause should not show it.
  7739. //$lang = get_lang('TitleManipulateStudentPublication');
  7740. break;
  7741. }
  7742. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7743. $item_id = intval($item_id);
  7744. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7745. $result = Database::query($sql);
  7746. $row = Database::fetch_assoc($result);
  7747. $webCodepath = api_get_path(WEB_CODE_PATH);
  7748. $audio_player = null;
  7749. // We display an audio player if needed.
  7750. if (!empty($row['audio'])) {
  7751. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7752. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  7753. $audio_player .= '<script type="text/javascript" src="'.$webCodepath.'inc/lib/mediaplayer/swfobject.js"></script>';
  7754. $audio_player .= '<script>
  7755. var s1 = new SWFObject("'.$webCodepath.'inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7756. s1.addParam("allowscriptaccess","always");
  7757. s1.addParam("flashvars","file='.api_get_path(WEB_PUBLIC_PATH).'courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7758. s1.write("container");
  7759. </script>';
  7760. }
  7761. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7762. $_GET['cidReq']
  7763. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7764. $return .= Display::url(
  7765. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7766. $url.'&action=edit_item&path_item='.$row['path']
  7767. );
  7768. $return .= Display::url(
  7769. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7770. $url.'&action=move_item'
  7771. );
  7772. // Commented for now as prerequisites cannot be added to chapters.
  7773. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7774. $return .= Display::url(
  7775. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7776. $url.'&action=edit_item_prereq'
  7777. );
  7778. }
  7779. $return .= Display::url(
  7780. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7781. $url.'&action=delete_item'
  7782. );
  7783. if ($item_type == TOOL_HOTPOTATOES) {
  7784. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7785. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7786. }
  7787. if ($item_type == TOOL_DOCUMENT) {
  7788. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7789. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7790. }
  7791. $return .= '</div>';
  7792. if (!empty($audio_player)) {
  7793. $return .= '<br />'.$audio_player;
  7794. }
  7795. return $return;
  7796. }
  7797. /**
  7798. * Creates the javascript needed for filling up the checkboxes without page reload
  7799. *
  7800. * @return string
  7801. */
  7802. public function get_js_dropdown_array()
  7803. {
  7804. $course_id = api_get_course_int_id();
  7805. $return = 'var child_name = new Array();'."\n";
  7806. $return .= 'var child_value = new Array();'."\n\n";
  7807. $return .= 'child_name[0] = new Array();'."\n";
  7808. $return .= 'child_value[0] = new Array();'."\n\n";
  7809. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7810. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7811. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7812. ORDER BY display_order ASC";
  7813. $res_zero = Database::query($sql_zero);
  7814. $i = 0;
  7815. while ($row_zero = Database :: fetch_array($res_zero)) {
  7816. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7817. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7818. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7819. }
  7820. $return .= "\n";
  7821. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7822. $res = Database::query($sql);
  7823. while ($row = Database :: fetch_array($res)) {
  7824. $sql_parent = "
  7825. SELECT * FROM ".$tbl_lp_item."
  7826. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7827. ORDER BY display_order ASC";
  7828. $res_parent = Database::query($sql_parent);
  7829. $i = 0;
  7830. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7831. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7832. while ($row_parent = Database :: fetch_array($res_parent)) {
  7833. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7834. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7835. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7836. }
  7837. $return .= "\n";
  7838. }
  7839. return $return;
  7840. }
  7841. /**
  7842. * Display the form to allow moving an item
  7843. * @param integer Item ID
  7844. * @return string HTML form
  7845. */
  7846. public function display_move_item($item_id)
  7847. {
  7848. $course_id = api_get_course_int_id();
  7849. $return = '';
  7850. if (is_numeric($item_id)) {
  7851. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7852. $sql = "SELECT * FROM ".$tbl_lp_item."
  7853. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7854. $res = Database::query($sql);
  7855. $row = Database :: fetch_array($res);
  7856. switch ($row['item_type']) {
  7857. case 'dokeos_chapter' :
  7858. case 'dir' :
  7859. case 'asset' :
  7860. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7861. $return .= $this->display_item_form(
  7862. $row['item_type'],
  7863. get_lang('MoveCurrentChapter'),
  7864. 'move',
  7865. $item_id,
  7866. $row
  7867. );
  7868. break;
  7869. case 'dokeos_module' :
  7870. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7871. $return .= $this->display_item_form(
  7872. $row['item_type'],
  7873. 'Move th current module:',
  7874. 'move',
  7875. $item_id,
  7876. $row
  7877. );
  7878. break;
  7879. case TOOL_DOCUMENT :
  7880. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7881. $return .= $this->display_document_form('move', $item_id, $row);
  7882. break;
  7883. case TOOL_LINK :
  7884. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7885. $return .= $this->display_link_form('move', $item_id, $row);
  7886. break;
  7887. case TOOL_HOTPOTATOES :
  7888. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7889. $return .= $this->display_link_form('move', $item_id, $row);
  7890. break;
  7891. case TOOL_QUIZ :
  7892. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7893. $return .= $this->display_quiz_form('move', $item_id, $row);
  7894. break;
  7895. case TOOL_STUDENTPUBLICATION :
  7896. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7897. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7898. break;
  7899. case TOOL_FORUM :
  7900. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7901. $return .= $this->display_forum_form('move', $item_id, $row);
  7902. break;
  7903. case TOOL_THREAD :
  7904. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7905. $return .= $this->display_forum_form('move', $item_id, $row);
  7906. break;
  7907. }
  7908. }
  7909. return $return;
  7910. }
  7911. /**
  7912. * Displays a basic form on the overview page for changing the item title and the item description.
  7913. * @param string $item_type
  7914. * @param string $title
  7915. * @param array $data
  7916. * @return string
  7917. */
  7918. public function display_item_small_form($item_type, $title = '', $data = array())
  7919. {
  7920. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7921. $form = new FormValidator('small_form', 'post', $url);
  7922. $form->addElement('header', $title);
  7923. $form->addElement('text', 'title', get_lang('Title'));
  7924. $form->addElement('button', 'submit_button', get_lang('Save'));
  7925. $form->addElement('hidden', 'id', $data['id']);
  7926. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7927. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7928. $form->setDefaults(array('title' => $data['title']));
  7929. return $form->toHtml();
  7930. }
  7931. /**
  7932. * Return HTML form to allow prerequisites selection
  7933. * @param integer Item ID
  7934. * @return string HTML form
  7935. */
  7936. public function display_item_prerequisites_form($item_id)
  7937. {
  7938. $course_id = api_get_course_int_id();
  7939. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7940. $item_id = intval($item_id);
  7941. /* Current prerequisite */
  7942. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7943. $result = Database::query($sql);
  7944. $row = Database::fetch_array($result);
  7945. $preq_id = $row['prerequisite'];
  7946. $return = '<legend>';
  7947. $return .= get_lang('AddEditPrerequisites');
  7948. $return .= '</legend>';
  7949. $return .= '<form method="POST">';
  7950. $return .= '<table class="data_table">';
  7951. $return .= '<tr>';
  7952. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7953. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  7954. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  7955. $return .= '</tr>';
  7956. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7957. $return .= '<tr >';
  7958. $return .= '<td colspan="3" class="radio">';
  7959. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7960. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  7961. $return .= '</tr>';
  7962. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7963. $result = Database::query($sql);
  7964. $arrLP = array();
  7965. while ($row = Database :: fetch_array($result)) {
  7966. $arrLP[] = array(
  7967. 'id' => $row['id'],
  7968. 'item_type' => $row['item_type'],
  7969. 'title' => $row['title'],
  7970. 'ref' => $row['ref'],
  7971. 'description' => $row['description'],
  7972. 'parent_item_id' => $row['parent_item_id'],
  7973. 'previous_item_id' => $row['previous_item_id'],
  7974. 'next_item_id' => $row['next_item_id'],
  7975. 'max_score' => $row['max_score'],
  7976. 'min_score' => $row['min_score'],
  7977. 'mastery_score' => $row['mastery_score'],
  7978. 'prerequisite' => $row['prerequisite'],
  7979. 'next_item_id' => $row['next_item_id'],
  7980. 'display_order' => $row['display_order']
  7981. );
  7982. if ($row['ref'] == $preq_id) {
  7983. $preq_mastery = $row['mastery_score'];
  7984. $preq_max = $row['max_score'];
  7985. }
  7986. }
  7987. $this->tree_array($arrLP);
  7988. $arrLP = $this->arrMenu;
  7989. unset($this->arrMenu);
  7990. $imgSysPath = api_get_path(SYS_CODE_PATH).'img/';
  7991. for ($i = 0; $i < count($arrLP); $i++) {
  7992. if ($arrLP[$i]['id'] == $item_id) {
  7993. break;
  7994. }
  7995. $return .= '<tr>';
  7996. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  7997. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  7998. $depth_multi = $arrLP[$i]['depth'] * 10;
  7999. $return .= '<input'.(($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '').(($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ').'id="id'.$arrLP[$i]['id'].'" name="prerequisites" style="margin-left:'.$depth_multi.'px; margin-right:10px;" type="radio" value="'.$arrLP[$i]['id'].'" />';
  8000. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8001. if (file_exists($imgSysPath.'lp_'.$icon_name.'.png')) {
  8002. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8003. } else {
  8004. if (file_exists($imgSysPath.'lp_'.$icon_name.'.gif')) {
  8005. $return .= Display::return_icon('lp_'.$icon_name.'.gif');
  8006. } else {
  8007. $return .= Display::return_icon(
  8008. 'folder_document.gif',
  8009. '',
  8010. array('style' => 'margin-right:5px;')
  8011. );
  8012. }
  8013. }
  8014. $return .= $arrLP[$i]['title'].'</label>';
  8015. $return .= '</td>';
  8016. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8017. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8018. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8019. $return .= '</td>';
  8020. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8021. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8022. $return .= '</td>';
  8023. }
  8024. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8025. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8026. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8027. $return .= '</td>';
  8028. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8029. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8030. $return .= '</td>';
  8031. }
  8032. $return .= '</tr>';
  8033. }
  8034. $return .= '<tr>';
  8035. $return .= '</tr>';
  8036. $return .= '</table>';
  8037. $return .= '<div style="padding-top:3px;">';
  8038. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8039. 'ModifyPrerequisites'
  8040. ).'</button>';
  8041. $return .= '</form>';
  8042. return $return;
  8043. }
  8044. /**
  8045. * Return HTML list to allow prerequisites selection for lp
  8046. * @param integer Item ID
  8047. * @return string HTML form
  8048. */
  8049. public function display_lp_prerequisites_list()
  8050. {
  8051. $course_id = api_get_course_int_id();
  8052. $lp_id = $this->lp_id;
  8053. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8054. // get current prerequisite
  8055. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8056. $result = Database::query($sql);
  8057. $row = Database :: fetch_array($result);
  8058. $preq_id = $row['prerequisite'];
  8059. $session_id = api_get_session_id();
  8060. $session_condition = api_get_session_condition($session_id);
  8061. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8062. $rs = Database::query($sql);
  8063. $return = '';
  8064. $return .= '<select name="prerequisites" >';
  8065. $return .= '<option value="0">'.get_lang('None').'</option>';
  8066. if (Database::num_rows($rs) > 0) {
  8067. while ($row = Database::fetch_array($rs)) {
  8068. if ($row['id'] == $lp_id) {
  8069. continue;
  8070. }
  8071. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8072. }
  8073. }
  8074. $return .= '</select>';
  8075. return $return;
  8076. }
  8077. /**
  8078. * Creates a list with all the documents in it
  8079. * @return string
  8080. */
  8081. public function get_documents()
  8082. {
  8083. $course_info = api_get_course_info();
  8084. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8085. return $document_tree;
  8086. }
  8087. /**
  8088. * Creates a list with all the exercises (quiz) in it
  8089. * @return string
  8090. */
  8091. public function get_exercises()
  8092. {
  8093. $course_id = api_get_course_int_id();
  8094. // New for hotpotatoes.
  8095. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8096. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8097. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8098. $session_id = api_get_session_id();
  8099. $condition_session = api_get_session_condition($session_id);
  8100. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8101. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8102. $res_quiz = Database::query($sql_quiz);
  8103. $res_hot = Database::query($sql_hot);
  8104. $return = '<ul class="lp_resource">';
  8105. $return .= '<li class="lp_resource_element">';
  8106. $return .= Display::return_icon('new_test_small.gif');
  8107. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8108. $return .= '</li>';
  8109. // Display hotpotatoes
  8110. while ($row_hot = Database :: fetch_array($res_hot)) {
  8111. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8112. $return .= '<a class="moved" href="#">';
  8113. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8114. $return .= '</a> ';
  8115. $return .= Display::return_icon('hotpotatoes_s.png');
  8116. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8117. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8118. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8119. $row_hot['title']
  8120. )).'</a>';
  8121. $return .= '</li>';
  8122. }
  8123. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8124. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['iid'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8125. $return .= '<a class="moved" href="#">';
  8126. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8127. $return .= '</a> ';
  8128. $return .= Display::return_icon('quizz_small.gif');
  8129. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8130. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['iid'].'&amp;lp_id='.$this->lp_id.'">'.
  8131. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8132. '</a>';
  8133. $return .= '</li>';
  8134. }
  8135. $return .= '</ul>';
  8136. return $return;
  8137. }
  8138. /**
  8139. * Creates a list with all the links in it
  8140. * @return string
  8141. */
  8142. public function get_links()
  8143. {
  8144. $course_id = api_get_course_int_id();
  8145. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8146. $session_id = api_get_session_id();
  8147. $condition_session = api_get_session_condition($session_id);
  8148. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8149. $res_link = Database::query($sql_link);
  8150. $return = '<ul class="lp_resource">';
  8151. $return .= '<li class="lp_resource_element">';
  8152. $return .= Display::return_icon('linksnew.gif');
  8153. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8154. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8155. $return .= '</li>';
  8156. $course_info = api_get_course_info();
  8157. while ($row_link = Database :: fetch_array($res_link)) {
  8158. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8159. if ($item_visibility != 2) {
  8160. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8161. $return .= '<a class="moved" href="#">';
  8162. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8163. $return .= '</a> ';
  8164. $return .= Display::return_icon('lp_link.gif');
  8165. $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.'">'.
  8166. $row_link['title'].
  8167. '</a>';
  8168. $return .= '</li>';
  8169. }
  8170. }
  8171. $return .= '</ul>';
  8172. return $return;
  8173. }
  8174. /**
  8175. * Creates a list with all the student publications in it
  8176. * @return unknown
  8177. */
  8178. public function get_student_publications()
  8179. {
  8180. $return = '<div class="lp_resource" >';
  8181. $return .= '<div class="lp_resource_element">';
  8182. $return .= Display::return_icon('works_small.gif');
  8183. $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>';
  8184. $return .= '</div>';
  8185. $return .= '</div>';
  8186. return $return;
  8187. }
  8188. /**
  8189. * Creates a list with all the forums in it
  8190. * @return string
  8191. */
  8192. public function get_forums()
  8193. {
  8194. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  8195. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  8196. $a_forums = get_forums();
  8197. $return = '<ul class="lp_resource">';
  8198. //First add link
  8199. $return .= '<li class="lp_resource_element">';
  8200. $return .= Display::return_icon('forum_new_small.gif');
  8201. $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').'">
  8202. '.get_lang('CreateANewForum').'</a>';
  8203. $return .= '</li>';
  8204. $return .= '<script>
  8205. function toggle_forum(forum_id){
  8206. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8207. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8208. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8209. } else {
  8210. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8211. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8212. }
  8213. }
  8214. </script>';
  8215. foreach ($a_forums as $forum) {
  8216. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8217. if (!empty($forum['forum_id'])) {
  8218. $return .= '<a class="moved" href="#">';
  8219. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8220. $return .= ' </a>';
  8221. $return .= Display::return_icon('lp_forum.gif');
  8222. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8223. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8224. </a>
  8225. <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">'.Security :: remove_XSS(
  8226. $forum['forum_title']
  8227. ).'</a>';
  8228. }
  8229. $return .= '</li>';
  8230. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8231. $a_threads = get_threads($forum['forum_id']);
  8232. if (is_array($a_threads)) {
  8233. foreach ($a_threads as $thread) {
  8234. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8235. $return .= '&nbsp;<a class="moved" href="#">';
  8236. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8237. $return .= ' </a>';
  8238. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8239. $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.'">'.Security :: remove_XSS(
  8240. $thread['thread_title']
  8241. ).'</a>';
  8242. $return .= '</li>';
  8243. }
  8244. }
  8245. $return .= '</div>';
  8246. }
  8247. $return .= '</ul>';
  8248. return $return;
  8249. }
  8250. /**
  8251. * // TODO: The output encoding should be equal to the system encoding.
  8252. *
  8253. * Exports the learning path as a SCORM package. This is the main function that
  8254. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8255. * whole thing and returns the zip.
  8256. *
  8257. * This method needs to be called in PHP5, as it will fail with non-adequate
  8258. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8259. * you need to call it on a learnpath object.
  8260. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8261. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8262. * path has been modified, it should use the generic method here below.
  8263. * @TODO link this function with the export_lp() function in the same class
  8264. * @param string Optional name of zip file. If none, title of learnpath is
  8265. * domesticated and trailed with ".zip"
  8266. * @return string Returns the zip package string, or null if error
  8267. */
  8268. public function scorm_export()
  8269. {
  8270. $_course = api_get_course_info();
  8271. $course_id = api_get_course_int_id();
  8272. $links_to_create = null;
  8273. // Remove memory and time limits as much as possible as this might be a long process...
  8274. if (function_exists('ini_set')) {
  8275. api_set_memory_limit('128M');
  8276. ini_set('max_execution_time', 600);
  8277. }
  8278. // Create the zip handler (this will remain available throughout the method).
  8279. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8280. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8281. $temp_dir_short = uniqid();
  8282. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8283. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8284. $zip_folder = new PclZip($temp_zip_file);
  8285. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8286. $root_path = $main_path = api_get_path(SYS_PATH);
  8287. $files_cleanup = array();
  8288. // Place to temporarily stash the zipfiles.
  8289. // create the temp dir if it doesn't exist
  8290. // or do a cleanup befor creating the zipfile.
  8291. if (!is_dir($temp_zip_dir)) {
  8292. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8293. } else {
  8294. // Cleanup: Check the temp dir for old files and delete them.
  8295. $handle = opendir($temp_zip_dir);
  8296. while (false !== ($file = readdir($handle))) {
  8297. if ($file != '.' && $file != '..') {
  8298. unlink("$temp_zip_dir/$file");
  8299. }
  8300. }
  8301. closedir($handle);
  8302. }
  8303. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8304. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8305. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8306. )
  8307. ) {
  8308. // Remove the possible . at the end of the path.
  8309. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8310. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8311. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8312. $zip_files_dist = api_copyr(
  8313. $current_course_path.'/scorm/'.$this->path,
  8314. $dest_path_to_scorm_folder,
  8315. array('imsmanifest'),
  8316. $zip_files
  8317. );
  8318. }
  8319. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8320. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8321. // Aggregation Model official document, secion "2.3 Content Packaging".
  8322. $xmldoc = new DOMDocument('1.0'); // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8323. $root = $xmldoc->createElement('manifest');
  8324. $root->setAttribute('identifier', 'SingleCourseManifest');
  8325. $root->setAttribute('version', '1.1');
  8326. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8327. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8328. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8329. $root->setAttribute(
  8330. 'xsi:schemaLocation',
  8331. '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'
  8332. );
  8333. // Build mandatory sub-root container elements.
  8334. $metadata = $xmldoc->createElement('metadata');
  8335. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8336. $metadata->appendChild($md_schema);
  8337. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8338. $metadata->appendChild($md_schemaversion);
  8339. $root->appendChild($metadata);
  8340. $organizations = $xmldoc->createElement('organizations');
  8341. $resources = $xmldoc->createElement('resources');
  8342. // Build the only organization we will use in building our learnpaths.
  8343. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8344. $organization = $xmldoc->createElement('organization');
  8345. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8346. // To set the title of the SCORM entity (=organization), we take the name given
  8347. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8348. // learning path charset) as it is the encoding that defines how it is stored
  8349. // in the database. Then we convert it to HTML entities again as the "&" character
  8350. // alone is not authorized in XML (must be &amp;).
  8351. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8352. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8353. $organization->appendChild($org_title);
  8354. $folder_name = 'document';
  8355. // Removes the learning_path/scorm_folder path when exporting see #4841
  8356. $path_to_remove = null;
  8357. $result = $this->generate_lp_folder($_course);
  8358. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8359. $path_to_remove = 'document'.$result['dir'];
  8360. $path_to_replace = $folder_name.'/';
  8361. }
  8362. //Fixes chamilo scorm exports
  8363. if ($this->ref == 'chamilo_scorm_export') {
  8364. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8365. }
  8366. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8367. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8368. $link_updates = array();
  8369. foreach ($this->items as $index => $item) {
  8370. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8371. // Get included documents from this item.
  8372. if ($item->type == 'sco') {
  8373. $inc_docs = $item->get_resources_from_source(
  8374. null,
  8375. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8376. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8377. );
  8378. } else {
  8379. $inc_docs = $item->get_resources_from_source();
  8380. }
  8381. // Give a child element <item> to the <organization> element.
  8382. $my_item_id = $item->get_id();
  8383. $my_item = $xmldoc->createElement('item');
  8384. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8385. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8386. $my_item->setAttribute('isvisible', 'true');
  8387. // Give a child element <title> to the <item> element.
  8388. $my_title = $xmldoc->createElement(
  8389. 'title',
  8390. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8391. );
  8392. $my_item->appendChild($my_title);
  8393. // Give a child element <adlcp:prerequisites> to the <item> element.
  8394. $my_prereqs = $xmldoc->createElement(
  8395. 'adlcp:prerequisites',
  8396. $this->get_scorm_prereq_string($my_item_id)
  8397. );
  8398. $my_prereqs->setAttribute('type', 'aicc_script');
  8399. $my_item->appendChild($my_prereqs);
  8400. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8401. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8402. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8403. //$xmldoc->createElement('adlcp:timelimitaction','');
  8404. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8405. //$xmldoc->createElement('adlcp:datafromlms','');
  8406. // Give a child element <adlcp:masteryscore> to the <item> element.
  8407. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8408. $my_item->appendChild($my_masteryscore);
  8409. // Attach this item to the organization element or hits parent if there is one.
  8410. if (!empty($item->parent) && $item->parent != 0) {
  8411. $children = $organization->childNodes;
  8412. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8413. if (is_object($possible_parent)) {
  8414. $possible_parent->appendChild($my_item);
  8415. } else {
  8416. if ($this->debug > 0) {
  8417. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8418. }
  8419. }
  8420. } else {
  8421. if ($this->debug > 0) {
  8422. error_log('No parent');
  8423. }
  8424. $organization->appendChild($my_item);
  8425. }
  8426. // Get the path of the file(s) from the course directory root.
  8427. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8428. if (!empty($path_to_remove)) {
  8429. //From docs
  8430. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8431. //From quiz
  8432. if ($this->ref == 'chamilo_scorm_export') {
  8433. $path_to_remove = 'scorm/'.$this->path.'/';
  8434. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8435. }
  8436. } else {
  8437. $my_xml_file_path = $my_file_path;
  8438. }
  8439. $my_sub_dir = dirname($my_file_path);
  8440. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8441. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8442. $my_xml_sub_dir = $my_sub_dir;
  8443. // Give a <resource> child to the <resources> element
  8444. $my_resource = $xmldoc->createElement('resource');
  8445. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8446. $my_resource->setAttribute('type', 'webcontent');
  8447. $my_resource->setAttribute('href', $my_xml_file_path);
  8448. // adlcp:scormtype can be either 'sco' or 'asset'.
  8449. if ($item->type == 'sco') {
  8450. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8451. } else {
  8452. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8453. }
  8454. // xml:base is the base directory to find the files declared in this resource.
  8455. $my_resource->setAttribute('xml:base', '');
  8456. // Give a <file> child to the <resource> element.
  8457. $my_file = $xmldoc->createElement('file');
  8458. $my_file->setAttribute('href', $my_xml_file_path);
  8459. $my_resource->appendChild($my_file);
  8460. // Dependency to other files - not yet supported.
  8461. $i = 1;
  8462. foreach ($inc_docs as $doc_info) {
  8463. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8464. continue;
  8465. }
  8466. $my_dep = $xmldoc->createElement('resource');
  8467. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8468. $my_dep->setAttribute('identifier', $res_id);
  8469. $my_dep->setAttribute('type', 'webcontent');
  8470. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8471. $my_dep_file = $xmldoc->createElement('file');
  8472. // Check type of URL.
  8473. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8474. if ($doc_info[1] == 'remote') {
  8475. // Remote file. Save url as is.
  8476. $my_dep_file->setAttribute('href', $doc_info[0]);
  8477. $my_dep->setAttribute('xml:base', '');
  8478. } elseif ($doc_info[1] == 'local') {
  8479. switch ($doc_info[2]) {
  8480. case 'url': // Local URL - save path as url for now, don't zip file.
  8481. $abs_path = api_get_path(SYS_PATH).str_replace(
  8482. api_get_path(WEB_PATH),
  8483. '',
  8484. $doc_info[0]
  8485. );
  8486. $current_dir = dirname($abs_path);
  8487. $current_dir = str_replace('\\', '/', $current_dir);
  8488. $file_path = realpath($abs_path);
  8489. $file_path = str_replace('\\', '/', $file_path);
  8490. $my_dep_file->setAttribute('href', $file_path);
  8491. $my_dep->setAttribute('xml:base', '');
  8492. if (strstr($file_path, $main_path) !== false) {
  8493. // The calculated real path is really inside Chamilo's root path.
  8494. // Reduce file path to what's under the DocumentRoot.
  8495. $file_path = substr($file_path, strlen($root_path) - 1);
  8496. //echo $file_path;echo '<br /><br />';
  8497. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8498. $zip_files_abs[] = $file_path;
  8499. $link_updates[$my_file_path][] = array(
  8500. 'orig' => $doc_info[0],
  8501. 'dest' => $file_path
  8502. );
  8503. $my_dep_file->setAttribute('href', $file_path);
  8504. $my_dep->setAttribute('xml:base', '');
  8505. } elseif (empty($file_path)) {
  8506. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8507. if (strpos($document_root, -1) == '/') {
  8508. $document_root = substr(0, -1, $document_root);
  8509. }*/
  8510. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8511. $file_path = str_replace('//', '/', $file_path);
  8512. if (file_exists($file_path)) {
  8513. $file_path = substr(
  8514. $file_path,
  8515. strlen($current_dir)
  8516. ); // We get the relative path.
  8517. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8518. $link_updates[$my_file_path][] = array(
  8519. 'orig' => $doc_info[0],
  8520. 'dest' => $file_path
  8521. );
  8522. $my_dep_file->setAttribute('href', $file_path);
  8523. $my_dep->setAttribute('xml:base', '');
  8524. }
  8525. }
  8526. break;
  8527. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8528. $my_dep_file->setAttribute('href', $doc_info[0]);
  8529. $my_dep->setAttribute('xml:base', '');
  8530. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8531. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8532. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8533. $abs_img_path_without_subdir = $doc_info[0];
  8534. $relp = api_get_path(REL_PATH); // The url-append config param.
  8535. $pos = strpos($abs_img_path_without_subdir, $relp);
  8536. if ($pos === 0) {
  8537. $abs_img_path_without_subdir = '/'.substr(
  8538. $abs_img_path_without_subdir,
  8539. strlen($relp)
  8540. );
  8541. }
  8542. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8543. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8544. $file_path = str_replace('\\', '/', $file_path);
  8545. $file_path = str_replace('//', '/', $file_path);
  8546. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8547. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8548. $cur_path = substr(
  8549. $current_course_path,
  8550. -1
  8551. ) == '/' ? $current_course_path : $current_course_path.'/';
  8552. // Check if the current document is in that path.
  8553. if (strstr($file_path, $cur_path) !== false) {
  8554. // The document is in that path, now get the relative path
  8555. // to the containing document.
  8556. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8557. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8558. $relative_path = '';
  8559. if (strstr($file_path, $cur_path) !== false) {
  8560. $relative_path = substr($file_path, strlen($orig_file_path));
  8561. $file_path = substr($file_path, strlen($cur_path));
  8562. } else {
  8563. // This case is still a problem as it's difficult to calculate a relative path easily
  8564. // might still generate wrong links.
  8565. //$file_path = substr($file_path,strlen($cur_path));
  8566. // Calculate the directory path to the current file (without trailing slash).
  8567. $my_relative_path = dirname($file_path);
  8568. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8569. $my_relative_file = basename($file_path);
  8570. // Calculate the directory path to the containing file (without trailing slash).
  8571. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8572. $dotdot = '';
  8573. $subdir = '';
  8574. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8575. $my_orig_file_path
  8576. ) > 1) && (strlen($my_relative_path) > 1)) {
  8577. $my_relative_path2 = dirname($my_relative_path);
  8578. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8579. $my_orig_file_path = dirname($my_orig_file_path);
  8580. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8581. $subdir = substr(
  8582. $my_relative_path,
  8583. strlen($my_relative_path2) + 1
  8584. ).'/'.$subdir;
  8585. $dotdot += '../';
  8586. $my_relative_path = $my_relative_path2;
  8587. }
  8588. $relative_path = $dotdot.$subdir.$my_relative_file;
  8589. }
  8590. // Put the current document in the zip (this array is the array
  8591. // that will manage documents already in the course folder - relative).
  8592. $zip_files[] = $file_path;
  8593. // Update the links to the current document in the containing document (make them relative).
  8594. $link_updates[$my_file_path][] = array(
  8595. 'orig' => $doc_info[0],
  8596. 'dest' => $relative_path
  8597. );
  8598. $my_dep_file->setAttribute('href', $file_path);
  8599. $my_dep->setAttribute('xml:base', '');
  8600. } elseif (strstr($file_path, $main_path) !== false) {
  8601. // The calculated real path is really inside Chamilo's root path.
  8602. // Reduce file path to what's under the DocumentRoot.
  8603. $file_path = substr($file_path, strlen($root_path));
  8604. //echo $file_path;echo '<br /><br />';
  8605. //error_log('Reduced path: '.$file_path, 0);
  8606. $zip_files_abs[] = $file_path;
  8607. $link_updates[$my_file_path][] = array(
  8608. 'orig' => $doc_info[0],
  8609. 'dest' => $file_path
  8610. );
  8611. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8612. $my_dep->setAttribute('xml:base', '');
  8613. } elseif (empty($file_path)) {
  8614. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8615. if(strpos($document_root,-1) == '/') {
  8616. $document_root = substr(0, -1, $document_root);
  8617. }*/
  8618. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8619. $file_path = str_replace('//', '/', $file_path);
  8620. if (file_exists($file_path)) {
  8621. $file_path = substr(
  8622. $file_path,
  8623. strlen($current_dir)
  8624. ); // We get the relative path.
  8625. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8626. $link_updates[$my_file_path][] = array(
  8627. 'orig' => $doc_info[0],
  8628. 'dest' => $file_path
  8629. );
  8630. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8631. $my_dep->setAttribute('xml:base', '');
  8632. }
  8633. }
  8634. break;
  8635. 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
  8636. if (substr($doc_info[0], 0, 2) == '..') {
  8637. // Relative path going up.
  8638. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8639. $current_dir = str_replace('\\', '/', $current_dir);
  8640. $file_path = realpath($current_dir.$doc_info[0]);
  8641. $file_path = str_replace('\\', '/', $file_path);
  8642. //error_log($file_path.' <-> '.$main_path,0);
  8643. if (strstr($file_path, $main_path) !== false) {
  8644. // The calculated real path is really inside Chamilo's root path.
  8645. // Reduce file path to what's under the DocumentRoot.
  8646. $file_path = substr($file_path, strlen($root_path));
  8647. //error_log('Reduced path: '.$file_path, 0);
  8648. $zip_files_abs[] = $file_path;
  8649. $link_updates[$my_file_path][] = array(
  8650. 'orig' => $doc_info[0],
  8651. 'dest' => $file_path
  8652. );
  8653. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8654. $my_dep->setAttribute('xml:base', '');
  8655. }
  8656. } else {
  8657. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8658. $my_dep_file->setAttribute('href', $doc_info[0]);
  8659. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8660. }
  8661. break;
  8662. default:
  8663. $my_dep_file->setAttribute('href', $doc_info[0]);
  8664. $my_dep->setAttribute('xml:base', '');
  8665. break;
  8666. }
  8667. }
  8668. $my_dep->appendChild($my_dep_file);
  8669. $resources->appendChild($my_dep);
  8670. $dependency = $xmldoc->createElement('dependency');
  8671. $dependency->setAttribute('identifierref', $res_id);
  8672. $my_resource->appendChild($dependency);
  8673. $i++;
  8674. }
  8675. //$my_dependency = $xmldoc->createElement('dependency');
  8676. //$my_dependency->setAttribute('identifierref', '');
  8677. $resources->appendChild($my_resource);
  8678. $zip_files[] = $my_file_path;
  8679. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8680. } else {
  8681. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8682. switch ($item->type) {
  8683. case TOOL_LINK:
  8684. $my_item = $xmldoc->createElement('item');
  8685. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8686. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8687. $my_item->setAttribute('isvisible', 'true');
  8688. // Give a child element <title> to the <item> element.
  8689. $my_title = $xmldoc->createElement(
  8690. 'title',
  8691. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8692. );
  8693. $my_item->appendChild($my_title);
  8694. // Give a child element <adlcp:prerequisites> to the <item> element.
  8695. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8696. $my_prereqs->setAttribute('type', 'aicc_script');
  8697. $my_item->appendChild($my_prereqs);
  8698. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8699. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8700. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8701. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8702. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8703. //$xmldoc->createElement('adlcp:datafromlms', '');
  8704. // Give a child element <adlcp:masteryscore> to the <item> element.
  8705. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8706. $my_item->appendChild($my_masteryscore);
  8707. // Attach this item to the organization element or its parent if there is one.
  8708. if (!empty($item->parent) && $item->parent != 0) {
  8709. $children = $organization->childNodes;
  8710. for ($i = 0; $i < $children->length; $i++) {
  8711. $item_temp = $children->item($i);
  8712. if ($item_temp->nodeName == 'item') {
  8713. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8714. $item_temp->appendChild($my_item);
  8715. }
  8716. }
  8717. }
  8718. } else {
  8719. $organization->appendChild($my_item);
  8720. }
  8721. $my_file_path = 'link_'.$item->get_id().'.html';
  8722. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8723. TABLE_LINK
  8724. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8725. $rs = Database::query($sql);
  8726. if ($link = Database :: fetch_array($rs)) {
  8727. $url = $link['url'];
  8728. $title = stripslashes($link['title']);
  8729. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8730. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8731. $my_xml_file_path = $my_file_path;
  8732. $my_sub_dir = dirname($my_file_path);
  8733. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8734. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8735. $my_xml_sub_dir = $my_sub_dir;
  8736. // Give a <resource> child to the <resources> element.
  8737. $my_resource = $xmldoc->createElement('resource');
  8738. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8739. $my_resource->setAttribute('type', 'webcontent');
  8740. $my_resource->setAttribute('href', $my_xml_file_path);
  8741. // adlcp:scormtype can be either 'sco' or 'asset'.
  8742. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8743. // xml:base is the base directory to find the files declared in this resource.
  8744. $my_resource->setAttribute('xml:base', '');
  8745. // give a <file> child to the <resource> element.
  8746. $my_file = $xmldoc->createElement('file');
  8747. $my_file->setAttribute('href', $my_xml_file_path);
  8748. $my_resource->appendChild($my_file);
  8749. $resources->appendChild($my_resource);
  8750. }
  8751. break;
  8752. case TOOL_QUIZ:
  8753. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8754. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8755. $exe = new Exercise();
  8756. $exe->read($exe_id);
  8757. $my_item = $xmldoc->createElement('item');
  8758. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8759. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8760. $my_item->setAttribute('isvisible', 'true');
  8761. // Give a child element <title> to the <item> element.
  8762. $my_title = $xmldoc->createElement(
  8763. 'title',
  8764. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8765. );
  8766. $my_item->appendChild($my_title);
  8767. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8768. //$my_item->appendChild($my_max_score);
  8769. // Give a child element <adlcp:prerequisites> to the <item> element.
  8770. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8771. $my_prereqs->setAttribute('type', 'aicc_script');
  8772. $my_item->appendChild($my_prereqs);
  8773. // Give a child element <adlcp:masteryscore> to the <item> element.
  8774. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8775. $my_item->appendChild($my_masteryscore);
  8776. // Attach this item to the organization element or hits parent if there is one.
  8777. if (!empty($item->parent) && $item->parent != 0) {
  8778. $children = $organization->childNodes;
  8779. for ($i = 0; $i < $children->length; $i++) {
  8780. $item_temp = $children->item($i);
  8781. if ($item_temp->nodeName == 'item') {
  8782. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8783. $item_temp->appendChild($my_item);
  8784. }
  8785. }
  8786. }
  8787. } else {
  8788. $organization->appendChild($my_item);
  8789. }
  8790. // Include export scripts.
  8791. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8792. // Get the path of the file(s) from the course directory root
  8793. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8794. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8795. // Write the contents of the exported exercise into a (big) html file
  8796. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8797. $contents = export_exercise($exe_id, true);
  8798. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8799. $res = file_put_contents($tmp_file_path, $contents);
  8800. if ($res === false) {
  8801. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8802. }
  8803. $files_cleanup[] = $tmp_file_path;
  8804. //error_log($tmp_path); die();
  8805. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8806. $my_xml_file_path = $my_file_path;
  8807. $my_sub_dir = dirname($my_file_path);
  8808. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8809. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8810. $my_xml_sub_dir = $my_sub_dir;
  8811. // Give a <resource> child to the <resources> element.
  8812. $my_resource = $xmldoc->createElement('resource');
  8813. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8814. $my_resource->setAttribute('type', 'webcontent');
  8815. $my_resource->setAttribute('href', $my_xml_file_path);
  8816. // adlcp:scormtype can be either 'sco' or 'asset'.
  8817. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8818. // xml:base is the base directory to find the files declared in this resource.
  8819. $my_resource->setAttribute('xml:base', '');
  8820. // Give a <file> child to the <resource> element.
  8821. $my_file = $xmldoc->createElement('file');
  8822. $my_file->setAttribute('href', $my_xml_file_path);
  8823. $my_resource->appendChild($my_file);
  8824. // Get included docs.
  8825. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8826. // Dependency to other files - not yet supported.
  8827. $i = 1;
  8828. foreach ($inc_docs as $doc_info) {
  8829. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8830. continue;
  8831. }
  8832. $my_dep = $xmldoc->createElement('resource');
  8833. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8834. $my_dep->setAttribute('identifier', $res_id);
  8835. $my_dep->setAttribute('type', 'webcontent');
  8836. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8837. $my_dep_file = $xmldoc->createElement('file');
  8838. // Check type of URL.
  8839. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8840. if ($doc_info[1] == 'remote') {
  8841. // Remote file. Save url as is.
  8842. $my_dep_file->setAttribute('href', $doc_info[0]);
  8843. $my_dep->setAttribute('xml:base', '');
  8844. } elseif ($doc_info[1] == 'local') {
  8845. switch ($doc_info[2]) {
  8846. case 'url': // Local URL - save path as url for now, don't zip file.
  8847. // Save file but as local file (retrieve from URL).
  8848. $abs_path = api_get_path(SYS_PATH).str_replace(
  8849. api_get_path(WEB_PATH),
  8850. '',
  8851. $doc_info[0]
  8852. );
  8853. $current_dir = dirname($abs_path);
  8854. $current_dir = str_replace('\\', '/', $current_dir);
  8855. $file_path = realpath($abs_path);
  8856. $file_path = str_replace('\\', '/', $file_path);
  8857. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8858. $my_dep->setAttribute('xml:base', '');
  8859. if (strstr($file_path, $main_path) !== false) {
  8860. // The calculated real path is really inside the chamilo root path.
  8861. // Reduce file path to what's under the DocumentRoot.
  8862. $file_path = substr($file_path, strlen($root_path));
  8863. //echo $file_path;echo '<br /><br />';
  8864. //error_log('Reduced path: '.$file_path, 0);
  8865. $zip_files_abs[] = $file_path;
  8866. $link_updates[$my_file_path][] = array(
  8867. 'orig' => $doc_info[0],
  8868. 'dest' => 'document/'.$file_path
  8869. );
  8870. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8871. $my_dep->setAttribute('xml:base', '');
  8872. } elseif (empty($file_path)) {
  8873. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8874. if (strpos($document_root,-1) == '/') {
  8875. $document_root = substr(0, -1, $document_root);
  8876. }*/
  8877. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8878. $file_path = str_replace('//', '/', $file_path);
  8879. if (file_exists($file_path)) {
  8880. $file_path = substr(
  8881. $file_path,
  8882. strlen($current_dir)
  8883. ); // We get the relative path.
  8884. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8885. $link_updates[$my_file_path][] = array(
  8886. 'orig' => $doc_info[0],
  8887. 'dest' => 'document/'.$file_path
  8888. );
  8889. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8890. $my_dep->setAttribute('xml:base', '');
  8891. }
  8892. }
  8893. break;
  8894. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8895. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8896. $current_dir = str_replace('\\', '/', $current_dir);
  8897. $file_path = realpath($doc_info[0]);
  8898. $file_path = str_replace('\\', '/', $file_path);
  8899. $my_dep_file->setAttribute('href', $file_path);
  8900. $my_dep->setAttribute('xml:base', '');
  8901. if (strstr($file_path, $main_path) !== false) {
  8902. // The calculated real path is really inside the chamilo root path.
  8903. // Reduce file path to what's under the DocumentRoot.
  8904. $file_path = substr($file_path, strlen($root_path));
  8905. //echo $file_path;echo '<br /><br />';
  8906. //error_log('Reduced path: '.$file_path, 0);
  8907. $zip_files_abs[] = $file_path;
  8908. $link_updates[$my_file_path][] = array(
  8909. 'orig' => $doc_info[0],
  8910. 'dest' => $file_path
  8911. );
  8912. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8913. $my_dep->setAttribute('xml:base', '');
  8914. } elseif (empty($file_path)) {
  8915. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8916. if (strpos($document_root,-1) == '/') {
  8917. $document_root = substr(0, -1, $document_root);
  8918. }*/
  8919. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8920. $file_path = str_replace('//', '/', $file_path);
  8921. if (file_exists($file_path)) {
  8922. $file_path = substr(
  8923. $file_path,
  8924. strlen($current_dir)
  8925. ); // We get the relative path.
  8926. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8927. $link_updates[$my_file_path][] = array(
  8928. 'orig' => $doc_info[0],
  8929. 'dest' => $file_path
  8930. );
  8931. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8932. $my_dep->setAttribute('xml:base', '');
  8933. }
  8934. }
  8935. break;
  8936. 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
  8937. if (substr($doc_info[0], 0, 2) == '..') {
  8938. // Relative path going up.
  8939. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8940. $current_dir = str_replace('\\', '/', $current_dir);
  8941. $file_path = realpath($current_dir.$doc_info[0]);
  8942. $file_path = str_replace('\\', '/', $file_path);
  8943. //error_log($file_path.' <-> '.$main_path, 0);
  8944. if (strstr($file_path, $main_path) !== false) {
  8945. // The calculated real path is really inside Chamilo's root path.
  8946. // Reduce file path to what's under the DocumentRoot.
  8947. $file_path = substr($file_path, strlen($root_path));
  8948. $file_path_dest = $file_path;
  8949. // File path is courses/CHAMILO/document/....
  8950. $info_file_path = explode('/', $file_path);
  8951. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8952. $file_path_dest = 'document/'.$file_path;
  8953. }
  8954. //error_log('Reduced path: '.$file_path, 0);
  8955. $zip_files_abs[] = $file_path;
  8956. $link_updates[$my_file_path][] = array(
  8957. 'orig' => $doc_info[0],
  8958. 'dest' => $file_path_dest
  8959. );
  8960. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8961. $my_dep->setAttribute('xml:base', '');
  8962. }
  8963. } else {
  8964. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8965. $my_dep_file->setAttribute('href', $doc_info[0]);
  8966. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8967. }
  8968. break;
  8969. default:
  8970. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8971. $my_dep->setAttribute('xml:base', '');
  8972. break;
  8973. }
  8974. }
  8975. $my_dep->appendChild($my_dep_file);
  8976. $resources->appendChild($my_dep);
  8977. $dependency = $xmldoc->createElement('dependency');
  8978. $dependency->setAttribute('identifierref', $res_id);
  8979. $my_resource->appendChild($dependency);
  8980. $i++;
  8981. }
  8982. $resources->appendChild($my_resource);
  8983. $zip_files[] = $my_file_path;
  8984. break;
  8985. default:
  8986. // Get the path of the file(s) from the course directory root
  8987. $my_file_path = 'non_exportable.html';
  8988. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8989. $my_xml_file_path = $my_file_path;
  8990. $my_sub_dir = dirname($my_file_path);
  8991. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8992. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8993. $my_xml_sub_dir = $my_sub_dir;
  8994. // Give a <resource> child to the <resources> element.
  8995. $my_resource = $xmldoc->createElement('resource');
  8996. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8997. $my_resource->setAttribute('type', 'webcontent');
  8998. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8999. // adlcp:scormtype can be either 'sco' or 'asset'.
  9000. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9001. // xml:base is the base directory to find the files declared in this resource.
  9002. $my_resource->setAttribute('xml:base', '');
  9003. // Give a <file> child to the <resource> element.
  9004. $my_file = $xmldoc->createElement('file');
  9005. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9006. $my_resource->appendChild($my_file);
  9007. $resources->appendChild($my_resource);
  9008. break;
  9009. }
  9010. }
  9011. }
  9012. $organizations->appendChild($organization);
  9013. $root->appendChild($organizations);
  9014. $root->appendChild($resources);
  9015. $xmldoc->appendChild($root);
  9016. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9017. // then add the file to the zip, then destroy the file (this is done automatically).
  9018. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9019. //error_log(print_r($zip_files,true), 0);
  9020. foreach ($zip_files as $file_path) {
  9021. if (empty($file_path)) {
  9022. continue;
  9023. }
  9024. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9025. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9026. $this->create_path($dest_file);
  9027. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path,0);
  9028. //echo $main_path.$file_path.'<br />';
  9029. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9030. // Check if the file needs a link update.
  9031. if (in_array($file_path, array_keys($link_updates))) {
  9032. $string = file_get_contents($dest_file);
  9033. unlink($dest_file);
  9034. foreach ($link_updates[$file_path] as $old_new) {
  9035. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9036. // This is an ugly hack that allows .flv files to be found by the flv player that
  9037. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9038. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9039. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9040. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9041. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9042. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9043. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9044. }
  9045. //Fix to avoid problems with default_course_document
  9046. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9047. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9048. } else {
  9049. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9050. $new_dest = $old_new['dest'];
  9051. }
  9052. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9053. $string = str_replace($old_new['orig'], $new_dest, $string);
  9054. //Add files inside the HTMLs
  9055. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9056. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9057. if (file_exists($sys_course_path.$new_path)) {
  9058. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9059. }
  9060. }
  9061. file_put_contents($dest_file, $string);
  9062. }
  9063. }
  9064. foreach ($zip_files_abs as $file_path) {
  9065. if (empty($file_path)) {
  9066. continue;
  9067. }
  9068. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9069. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9070. continue;
  9071. }
  9072. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9073. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9074. $this->create_path($dest_file);
  9075. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9076. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path, 0);
  9077. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9078. copy($main_path.$file_path, $dest_file);
  9079. // Check if the file needs a link update.
  9080. if (in_array($file_path, array_keys($link_updates))) {
  9081. $string = file_get_contents($dest_file);
  9082. unlink($dest_file);
  9083. foreach ($link_updates[$file_path] as $old_new) {
  9084. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9085. // This is an ugly hack that allows .flv files to be found by the flv player that
  9086. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9087. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9088. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9089. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9090. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9091. }
  9092. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9093. }
  9094. file_put_contents($dest_file, $string);
  9095. }
  9096. }
  9097. if (is_array($links_to_create)) {
  9098. foreach ($links_to_create as $file => $link) {
  9099. $file_content = '<!DOCTYPE html>
  9100. <head>
  9101. <meta charset="'.api_get_language_isocode().'" />
  9102. <title>'.$link['title'].'</title>
  9103. </head>
  9104. <body dir="'.api_get_text_direction().'">
  9105. <div style="text-align:center">
  9106. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9107. </body>
  9108. </html>';
  9109. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9110. }
  9111. }
  9112. // Add non exportable message explanation.
  9113. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9114. $file_content = '<!DOCTYPE html>
  9115. <head>
  9116. <meta charset="'.api_get_language_isocode().'" />
  9117. <title>'.$lang_not_exportable.'</title>
  9118. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9119. </head>
  9120. <body dir="'.api_get_text_direction().'">';
  9121. $file_content .=
  9122. <<<EOD
  9123. <style>
  9124. .error-message {
  9125. font-family: arial, verdana, helvetica, sans-serif;
  9126. border-width: 1px;
  9127. border-style: solid;
  9128. left: 50%;
  9129. margin: 10px auto;
  9130. min-height: 30px;
  9131. padding: 5px;
  9132. right: 50%;
  9133. width: 500px;
  9134. background-color: #FFD1D1;
  9135. border-color: #FF0000;
  9136. color: #000;
  9137. }
  9138. </style>
  9139. <body>
  9140. <div class="error-message">
  9141. $lang_not_exportable
  9142. </div>
  9143. </body>
  9144. </html>
  9145. EOD;
  9146. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9147. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9148. }
  9149. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9150. // Add the extra files that go along with a SCORM package.
  9151. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9152. $extra_files = scandir($main_code_path);
  9153. foreach ($extra_files as $extra_file) {
  9154. if (strpos($extra_file, '.') === 0) {
  9155. continue;
  9156. } else {
  9157. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9158. $this->create_path($dest_file);
  9159. copy($main_code_path.$extra_file, $dest_file);
  9160. }
  9161. }
  9162. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9163. $manifest = @$xmldoc->saveXML();
  9164. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9165. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9166. $zip_folder->add(
  9167. $archive_path.'/'.$temp_dir_short,
  9168. PCLZIP_OPT_REMOVE_PATH,
  9169. $archive_path.'/'.$temp_dir_short.'/'
  9170. );
  9171. // Clean possible temporary files.
  9172. foreach ($files_cleanup as $file) {
  9173. $res = unlink($file);
  9174. if ($res === false) {
  9175. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9176. }
  9177. }
  9178. // Send file to client
  9179. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9180. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9181. }
  9182. public function scorm_export_to_pdf($lp_id)
  9183. {
  9184. $lp_id = intval($lp_id);
  9185. $files_to_export = array();
  9186. $course_data = api_get_course_info($this->cc);
  9187. if (!empty($course_data)) {
  9188. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9189. $list = self::get_flat_ordered_items_list($lp_id);
  9190. if (!empty($list)) {
  9191. foreach ($list as $item_id) {
  9192. $item = $this->items[$item_id];
  9193. switch ($item->type) {
  9194. case 'document':
  9195. //Getting documents from a LP with chamilo documents
  9196. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9197. $file_path = api_get_path(
  9198. SYS_COURSE_PATH
  9199. ).$course_data['path'].'/document'.$file_data['path'];
  9200. if (file_exists($file_path)) {
  9201. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9202. }
  9203. break;
  9204. case 'asset': //commes from a scorm package generated by chamilo
  9205. case 'sco':
  9206. $file_path = $scorm_path.'/'.$item->path;
  9207. if (file_exists($file_path)) {
  9208. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9209. }
  9210. break;
  9211. case 'dokeos_chapter':
  9212. case 'dir':
  9213. case 'chapter':
  9214. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9215. break;
  9216. }
  9217. }
  9218. }
  9219. $pdf = new PDF();
  9220. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9221. return $result;
  9222. }
  9223. return false;
  9224. }
  9225. /**
  9226. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9227. * if it doesn't exist
  9228. */
  9229. public function create_path($path)
  9230. {
  9231. $path_bits = explode('/', dirname($path));
  9232. // IS_WINDOWS_OS has been defined in api.lib.php
  9233. $path_built = IS_WINDOWS_OS ? '' : '/';
  9234. foreach ($path_bits as $bit) {
  9235. if (!empty ($bit)) {
  9236. $new_path = $path_built.$bit;
  9237. if (is_dir($new_path)) {
  9238. $path_built = $new_path.'/';
  9239. } else {
  9240. mkdir($new_path, api_get_permissions_for_new_directories());
  9241. $path_built = $new_path.'/';
  9242. }
  9243. }
  9244. }
  9245. }
  9246. /**
  9247. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9248. * @return boolean The results of the unlink function, or false if there was no image to start with
  9249. */
  9250. public function delete_lp_image()
  9251. {
  9252. $img = $this->get_preview_image();
  9253. if ($img != '') {
  9254. $del_file = $this->get_preview_image_path(null, 'sys');
  9255. if (isset($del_file) && file_exists($del_file)) {
  9256. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9257. if (file_exists($del_file_2)) {
  9258. unlink($del_file_2);
  9259. }
  9260. $this->set_preview_image('');
  9261. return @unlink($del_file);
  9262. }
  9263. }
  9264. return false;
  9265. }
  9266. /**
  9267. * Uploads an author image to the upload/learning_path/images directory
  9268. * @param array The image array, coming from the $_FILES superglobal
  9269. * @return boolean True on success, false on error
  9270. */
  9271. public function upload_image($image_array)
  9272. {
  9273. $image_moved = false;
  9274. if (!empty ($image_array['name'])) {
  9275. $upload_ok = FileManager::process_uploaded_file($image_array);
  9276. $has_attachment = true;
  9277. } else {
  9278. $image_moved = true;
  9279. }
  9280. if ($upload_ok) {
  9281. if ($has_attachment) {
  9282. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9283. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9284. $updir = $sys_course_path.$courseDir;
  9285. // Try to add an extension to the file if it hasn't one.
  9286. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9287. if (!FileManager::filter_extension($new_file_name)) {
  9288. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9289. $image_moved = false;
  9290. } else {
  9291. $file_extension = explode('.', $image_array['name']);
  9292. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9293. $filename = uniqid('');
  9294. $new_file_name = $filename.'.'.$file_extension;
  9295. $new_path = $updir.'/'.$new_file_name;
  9296. // Resize the image.
  9297. $temp = new Image($image_array['tmp_name']);
  9298. $picture_infos = $temp->get_image_info();
  9299. if ($picture_infos['width'] > 104) {
  9300. $thumbwidth = 104;
  9301. } else {
  9302. $thumbwidth = $picture_infos['width'];
  9303. }
  9304. if ($picture_infos['height'] > 96) {
  9305. $new_height = 96;
  9306. } else {
  9307. $new_height = $picture_infos['height'];
  9308. }
  9309. $temp->resize($thumbwidth, $new_height, 0);
  9310. $result = $temp->send_image($new_path);
  9311. // Storing the image filename.
  9312. if ($result) {
  9313. $image_moved = true;
  9314. $this->set_preview_image($new_file_name);
  9315. //Resize to 32
  9316. $temp->resize(64, 64, 0);
  9317. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9318. return true;
  9319. }
  9320. }
  9321. }
  9322. }
  9323. return false;
  9324. }
  9325. public function set_autolunch($lp_id, $status)
  9326. {
  9327. $course_id = api_get_course_int_id();
  9328. $lp_id = intval($lp_id);
  9329. $status = intval($status);
  9330. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9331. //Setting everything to autolunch = 0
  9332. $attributes['autolunch'] = 0;
  9333. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9334. Database::update($lp_table, $attributes, $where);
  9335. if ($status == 1) {
  9336. //Setting my lp_id to autolunch = 1
  9337. $attributes['autolunch'] = 1;
  9338. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9339. Database::update($lp_table, $attributes, $where);
  9340. }
  9341. }
  9342. /**
  9343. * Gets previous_item_id for the next element of the lp_item table
  9344. * @author Isaac flores paz
  9345. * @return integer Previous item ID
  9346. */
  9347. function select_previous_item_id()
  9348. {
  9349. $course_id = api_get_course_int_id();
  9350. if ($this->debug > 0) {
  9351. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9352. }
  9353. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9354. // Get the max order of the items
  9355. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'";
  9356. $rs_max_order = Database::query($sql_max_order);
  9357. $row_max_order = Database::fetch_object($rs_max_order);
  9358. $max_order = $row_max_order->display_order;
  9359. // Get the previous item ID
  9360. $sql_max = "SELECT id as previous FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' AND display_order = '".$max_order."' ";
  9361. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9362. $row_max = Database::fetch_object($rs_max);
  9363. // Return the previous item ID
  9364. return $row_max->previous;
  9365. }
  9366. function copy()
  9367. {
  9368. $main_path = api_get_path(SYS_CODE_PATH);
  9369. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9370. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9371. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9372. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9373. //Course builder
  9374. $cb = new CourseBuilder();
  9375. //Setting tools that will be copied
  9376. $cb->set_tools_to_build(array('learnpaths'));
  9377. //Setting elements that will be copied
  9378. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9379. $course = $cb->build();
  9380. //Course restorer
  9381. $course_restorer = new CourseRestorer($course);
  9382. $course_restorer->set_add_text_in_items(true);
  9383. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9384. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9385. }
  9386. function verify_document_size($s)
  9387. {
  9388. $post_max = ini_get('post_max_size');
  9389. $upl_max = ini_get('upload_max_filesize');
  9390. $documents_total_space = DocumentManager::documents_total_space();
  9391. $course_max_space = DocumentManager::get_course_quota();
  9392. $total_size = filesize($s) + $documents_total_space;
  9393. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9394. return true;
  9395. } else {
  9396. return false;
  9397. }
  9398. }
  9399. function clear_prerequisites()
  9400. {
  9401. $course_id = $this->get_course_int_id();
  9402. if ($this->debug > 0) {
  9403. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9404. }
  9405. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9406. $lp_id = $this->get_id();
  9407. //Cleaning prerequisites
  9408. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9409. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9410. Database::query($sql);
  9411. //Cleaning mastery score for exercises
  9412. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9413. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9414. Database::query($sql);
  9415. }
  9416. function set_previous_step_as_prerequisite_for_all_items()
  9417. {
  9418. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9419. $course_id = $this->get_course_int_id();
  9420. $lp_id = $this->get_id();
  9421. if (!empty($this->items)) {
  9422. $previous_item_id = null;
  9423. $previous_item_max = 0;
  9424. $previous_item_type = null;
  9425. $last_item_not_chapter = null;
  9426. $last_item_not_chapter_type = null;
  9427. $last_item_not_chapter_max = null;
  9428. foreach ($this->items as $item) {
  9429. // if there was a previous item... (otherwise jump to set it)
  9430. if (!empty($previous_item_id)) {
  9431. $current_item_id = $item->get_id(); //save current id
  9432. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9433. // Current item is not a folder, so it qualifies to get a prerequisites
  9434. if ($last_item_not_chapter_type == 'quiz') {
  9435. // if previous is quiz, mark its max score as default score to be achieved
  9436. $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'";
  9437. Database::query($sql);
  9438. }
  9439. // now simply update the prerequisite to set it to the last non-chapter item
  9440. $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'";
  9441. Database::query($sql);
  9442. // record item as 'non-chapter' reference
  9443. $last_item_not_chapter = $item->get_id();
  9444. $last_item_not_chapter_type = $item->get_type();
  9445. $last_item_not_chapter_max = $item->get_max();
  9446. }
  9447. } else {
  9448. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9449. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9450. $last_item_not_chapter = $item->get_id();
  9451. $last_item_not_chapter_type = $item->get_type();
  9452. $last_item_not_chapter_max = $item->get_max();
  9453. }
  9454. $old_id = $item->get_id();
  9455. $old_max = $item->get_max();
  9456. $old_type = $item->get_type();
  9457. }
  9458. // Saving the item as "previous item" for the next loop
  9459. $previous_item_id = $item->get_id();
  9460. $previous_item_max = $item->get_max();
  9461. $previous_item_type = $item->get_type();
  9462. }
  9463. }
  9464. }
  9465. static function create_category($params)
  9466. {
  9467. global $app;
  9468. $em = $app['orm.ems']['db_write'];
  9469. $item = new Entity\CLpCategory();
  9470. $item->setName($params['name']);
  9471. $item->setCId($params['c_id']);
  9472. $em->persist($item);
  9473. $em->flush();
  9474. }
  9475. static function update_category($params)
  9476. {
  9477. global $app;
  9478. $em = $app['orm.ems']['db_write'];
  9479. $item = $em->find('Entity\CLpCategory', $params['id']);
  9480. if ($item) {
  9481. $item->setName($params['name']);
  9482. $item->setCId($params['c_id']);
  9483. $em->persist($item);
  9484. $em->flush();
  9485. }
  9486. }
  9487. static function move_up_category($id)
  9488. {
  9489. global $app;
  9490. $em = $app['orm.ems']['db_write'];
  9491. $item = $em->find('Entity\CLpCategory', $id);
  9492. if ($item) {
  9493. $position = $item->getPosition() - 1;
  9494. $item->setPosition($position);
  9495. $em->persist($item);
  9496. $em->flush();
  9497. }
  9498. }
  9499. static function move_down_category($id)
  9500. {
  9501. global $app;
  9502. $em = $app['orm.ems']['db_write'];
  9503. $item = $em->find('Entity\CLpCategory', $id);
  9504. if ($item) {
  9505. $position = $item->getPosition() + 1;
  9506. $item->setPosition($position);
  9507. $em->persist($item);
  9508. $em->flush();
  9509. }
  9510. }
  9511. static function get_count_categories($course_id)
  9512. {
  9513. global $app;
  9514. if (empty($course_id)) {
  9515. return 0;
  9516. }
  9517. $em = $app['orm.em'];
  9518. $query = $em->createQuery('SELECT COUNT(u.id) FROM Entity\CLpCategory u WHERE u.cId = :id');
  9519. $query->setParameter('id', $course_id);
  9520. return $query->getSingleScalarResult();
  9521. }
  9522. static function get_categories($course_id)
  9523. {
  9524. global $app;
  9525. $em = $app['orm.em'];
  9526. //Default behaviour
  9527. /*$items = $em->getRepository('Entity\CLpCategory')->findBy(
  9528. array('cId' => $course_id),
  9529. array('name' => 'ASC')
  9530. );*/
  9531. //Using doctrine extensions
  9532. $items = $em->getRepository('Entity\CLpCategory')->getBySortableGroupsQuery(
  9533. array('cId' => $course_id)
  9534. )->getResult();
  9535. return $items;
  9536. }
  9537. static function get_category($id)
  9538. {
  9539. global $app;
  9540. $em = $app['orm.em'];
  9541. $item = $em->find('Entity\CLpCategory', $id);
  9542. return $item;
  9543. }
  9544. static function get_category_by_course($course_id)
  9545. {
  9546. global $app;
  9547. $items = $app['orm.em']->getRepository('Entity\CLpCategory')->findBy(array('cId' => $course_id));
  9548. return $items;
  9549. }
  9550. static function delete_category($id)
  9551. {
  9552. global $app;
  9553. $em = $app['orm.ems']['db_write'];
  9554. $item = $em->find('Entity\CLpCategory', $id);
  9555. if ($item) {
  9556. $courseId = $item->getCId();
  9557. $query = $em->createQuery('SELECT u FROM Entity\CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9558. $query->setParameter('id', $courseId);
  9559. $query->setParameter('catId', $item->getId());
  9560. $lps = $query->getResult();
  9561. // Setting category = 0.
  9562. if ($lps) {
  9563. foreach ($lps as $lpItem) {
  9564. $lpItem->setCategoryId(0);
  9565. }
  9566. }
  9567. // Removing category.
  9568. $em->remove($item);
  9569. $em->flush();
  9570. }
  9571. }
  9572. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9573. {
  9574. $items = self::get_category_by_course($course_id);
  9575. $cats = array();
  9576. if ($addSelectOption) {
  9577. $cats = array(get_lang('SelectACategory'));
  9578. }
  9579. if (!empty($items)) {
  9580. foreach ($items as $cat) {
  9581. $cats[$cat->getId()] = $cat->getName();
  9582. }
  9583. }
  9584. return $cats;
  9585. }
  9586. }
  9587. if (!function_exists('trim_value')) {
  9588. function trim_value(& $value)
  9589. {
  9590. $value = trim($value);
  9591. }
  9592. }