learnpath.class.php 429 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
  5. * learnpaths. It is used by the scorm class.
  6. *
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  10. */
  11. /**
  12. * Defines the learnpath parent class
  13. * @package chamilo.learnpath
  14. */
  15. class learnpath {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $created_on = '';
  66. public $modified_on = '';
  67. public $publicated_on = '';
  68. public $expired_on = '';
  69. public $ref = null;
  70. public $course_int_id;
  71. public $course_info = array();
  72. public function get_course_int_id() {
  73. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  74. }
  75. public function set_course_int_id($course_id) {
  76. return $this->course_int_id = intval($course_id);
  77. }
  78. /**
  79. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  80. * Also builds the list of items into $this->items.
  81. * @param string Course code
  82. * @param integer Learnpath ID
  83. * @param integer User ID
  84. * @return boolean True on success, false on error
  85. */
  86. public function __construct($course, $lp_id, $user_id) {
  87. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  88. // Check course code.
  89. $course_id = api_get_course_int_id();
  90. if ($this->debug > 0) { error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0); }
  91. if (empty($course)) {
  92. $this->error = 'Course code is empty';
  93. return false;
  94. } else {
  95. $course_info = api_get_course_info($course);
  96. if (!empty($course_info)) {
  97. $this->cc = $course_info['code'];
  98. $this->course_info = $course_info;
  99. $course_id = $course_info['real_id'];
  100. } else {
  101. $this->error = 'Course code does not exist in database ('.$sql.')';
  102. return false;
  103. }
  104. }
  105. $this->set_course_int_id($course_id);
  106. // Check learnpath ID.
  107. if (empty($lp_id)) {
  108. $this->error = 'Learnpath ID is empty';
  109. return false;
  110. } else {
  111. // TODO: Make it flexible to use any course_code (still using env course code here).
  112. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  113. $lp_id = intval($lp_id);
  114. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  115. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0); }
  116. $res = Database::query($sql);
  117. if (Database::num_rows($res) > 0) {
  118. $this->lp_id = $lp_id;
  119. $row = Database::fetch_array($res);
  120. $this->type = $row['lp_type'];
  121. $this->name = stripslashes($row['name']);
  122. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->created_on = $row['created_on'];
  138. $this->modified_on = $row['modified_on'];
  139. $this->ref = $row['ref'];
  140. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  141. $this->publicated_on = $row['publicated_on'];
  142. }
  143. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  144. $this->expired_on = $row['expired_on'];
  145. }
  146. if ($this->type == 2) {
  147. if ($row['force_commit'] == 1) {
  148. $this->force_commit = true;
  149. }
  150. }
  151. $this->mode = $row['default_view_mod'];
  152. } else {
  153. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  154. return false;
  155. }
  156. }
  157. // Check user ID.
  158. if (empty($user_id)) {
  159. $this->error = 'User ID is empty';
  160. return false;
  161. } else {
  162. $user_info = api_get_user_info($user_id);
  163. if (!empty($user_info)) {
  164. $this->user_id = $user_info['user_id'];
  165. } else {
  166. $this->error = 'User ID does not exist in database ('.$sql.')';
  167. return false;
  168. }
  169. }
  170. // End of variables checking.
  171. $session_id = api_get_session_id();
  172. // Get the session condition for learning paths of the base + session.
  173. $session = api_get_session_condition($session_id);
  174. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  175. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  176. // Selecting by view_count descending allows to get the highest view_count first.
  177. $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";
  178. $res = Database::query($sql);
  179. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0); }
  180. if (Database :: num_rows($res) > 0) {
  181. if ($this->debug > 2) {
  182. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  183. }
  184. $row = Database :: fetch_array($res);
  185. $this->attempt = $row['view_count'];
  186. $this->lp_view_id = $row['id'];
  187. $this->last_item_seen = $row['last_item'];
  188. $this->progress_db = $row['progress'];
  189. $this->lp_view_session_id = $row['session_id'];
  190. } else {
  191. if ($this->debug > 2) {
  192. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  193. }
  194. $this->attempt = 1;
  195. $sql_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)";
  196. $res_ins = Database::query($sql_ins);
  197. $this->lp_view_id = Database :: insert_id();
  198. if ($this->debug > 2) {
  199. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
  200. }
  201. }
  202. // Initialise items.
  203. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  204. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  205. $res = Database::query($sql);
  206. if ($this->debug > 2) {
  207. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  208. error_log('-- Start while--', 0);
  209. }
  210. $lp_item_id_list = array();
  211. while ($row = Database::fetch_array($res)) {
  212. $oItem = '';
  213. $lp_item_id_list[] = $row['id'];
  214. switch ($this->type) {
  215. case 3: //aicc
  216. $oItem = new aiccItem('db', $row['id'], $course_id);
  217. if (is_object($oItem)) {
  218. $my_item_id = $oItem->get_id();
  219. $oItem->set_lp_view($this->lp_view_id, $course_id);
  220. $oItem->set_prevent_reinit($this->prevent_reinit);
  221. // Don't use reference here as the next loop will make the pointed object change.
  222. $this->items[$my_item_id] = $oItem;
  223. $this->refs_list[$oItem->ref] = $my_item_id;
  224. if ($this->debug > 2) {
  225. error_log('New LP - learnpath::__construct() - aicc object with id ' . $my_item_id . ' set in items[]', 0);
  226. }
  227. }
  228. break;
  229. case 2:
  230. require_once 'scorm.class.php';
  231. require_once 'scormItem.class.php';
  232. $oItem = new scormItem('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('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  242. }
  243. }
  244. break;
  245. case 1:
  246. default:
  247. if ($this->debug > 2) {
  248. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  249. }
  250. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  251. if ($this->debug > 2) {
  252. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  253. }
  254. if (is_object($oItem)) {
  255. $my_item_id = $oItem->get_id();
  256. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  257. $oItem->set_prevent_reinit($this->prevent_reinit);
  258. // Don't use reference here as the next loop will make the pointed object change.
  259. $this->items[$my_item_id] = $oItem;
  260. $this->refs_list[$my_item_id] = $my_item_id;
  261. if ($this->debug > 2) {
  262. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - object with id ' . $my_item_id . ' set in items[]', 0);
  263. }
  264. }
  265. break;
  266. }
  267. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  268. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  269. if (is_object($this->items[$row['id']])) {
  270. $this->items[$row['id']]->set_level(0);
  271. }
  272. } else {
  273. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  274. $this->items[$row['id']]->set_level($level);
  275. if (is_object($this->items[$row['parent_item_id']])) {
  276. // Items is a list of pointers from item DB ids to item objects.
  277. $this->items[$row['parent_item_id']]->add_child($row['id']);
  278. } else {
  279. if ($this->debug > 2) {
  280. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - The parent item (' . $row['parent_item_id'] . ') of item ' . $row['id'] . ' could not be found', 0);
  281. }
  282. }
  283. }
  284. // Setting the view in the item object.
  285. if (is_object($this->items[$row['id']])) {
  286. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  287. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  288. $this->items[$row['id']]->current_start_time = 0;
  289. $this->items[$row['id']]->current_stop_time = 0;
  290. }
  291. }
  292. }
  293. if ($this->debug > 2) {
  294. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  295. }
  296. if (!empty($lp_item_id_list)) {
  297. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  298. // Get last viewing vars.
  299. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  300. // This query should only return one or zero result.
  301. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  302. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  303. ORDER BY view_count DESC ";
  304. if ($this->debug > 2) {
  305. error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
  306. }
  307. $status_list = array();
  308. $res = Database::query($sql);
  309. while ($row = Database :: fetch_array($res) ) {
  310. $status_list[$row['lp_item_id']] = $row['status'];
  311. }
  312. foreach ($lp_item_id_list as $item_id) {
  313. if (isset($status_list[$item_id])) {
  314. $status = $status_list[$item_id];
  315. if (is_object($this->items[$item_id])) {
  316. $this->items[$item_id]->set_status($status);
  317. if (empty($status)) {
  318. $this->items[$item_id]->set_status($this->default_status);
  319. }
  320. }
  321. } else {
  322. if (is_object($this->items[$item_id])) {
  323. $this->items[$item_id]->set_status($this->default_status);
  324. }
  325. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  326. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  327. VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')";
  328. if ($this->debug > 2) {
  329. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql_ins, 0);
  330. }
  331. $res_ins = Database::query($sql_ins);
  332. }
  333. }
  334. }
  335. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  336. $this->max_ordered_items = 0;
  337. foreach ($this->ordered_items as $index => $dummy) {
  338. if ($index > $this->max_ordered_items && !empty($dummy)) {
  339. $this->max_ordered_items = $index;
  340. }
  341. }
  342. // TODO: Define the current item better.
  343. $this->first();
  344. if ($this->debug > 2) {
  345. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  346. }
  347. }
  348. /**
  349. * 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
  350. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  351. *
  352. * @param int $parent
  353. * @param int $previous
  354. * @param string $type
  355. * @param int resource ID (ref)
  356. * @param string $title
  357. * @param string $description
  358. * @return int
  359. */
  360. public function add_item($parent, $previous, $type = 'dokeos_chapter', $id, $title, $description, $prerequisites = 0, $max_time_allowed = 0) {
  361. $course_id = api_get_course_int_id();
  362. if ($this->debug > 0) {
  363. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  364. }
  365. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  366. $parent = intval($parent);
  367. $previous = intval($previous);
  368. $type = Database::escape_string($type);
  369. $id = intval($id);
  370. $max_time_allowed = Database::escape_string(htmlentities($max_time_allowed));
  371. if (empty ($max_time_allowed)) {
  372. $max_time_allowed = 0;
  373. }
  374. $title = Database::escape_string($title);
  375. $description = Database::escape_string($description);
  376. $sql_count = " SELECT COUNT(id) AS num
  377. FROM $tbl_lp_item
  378. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND parent_item_id = " . $parent;
  379. $res_count = Database::query($sql_count);
  380. $row = Database :: fetch_array($res_count);
  381. $num = $row['num'];
  382. if ($num > 0) {
  383. if ($previous == 0) {
  384. $sql = "SELECT id, next_item_id, display_order
  385. FROM " . $tbl_lp_item . "
  386. WHERE c_id = $course_id AND
  387. lp_id = " . $this->get_id() . " AND
  388. parent_item_id = " . $parent . " AND
  389. previous_item_id = 0 OR previous_item_id=" . $parent;
  390. $result = Database::query($sql);
  391. $row = Database :: fetch_array($result);
  392. $tmp_previous = 0;
  393. $next = $row['id'];
  394. $display_order = 0;
  395. } else {
  396. $previous = (int) $previous;
  397. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  398. FROM $tbl_lp_item
  399. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND id = " . $previous;
  400. $result = Database::query($sql);
  401. $row = Database :: fetch_array($result);
  402. $tmp_previous = $row['id'];
  403. $next = $row['next_item_id'];
  404. $display_order = $row['display_order'];
  405. }
  406. } else {
  407. $tmp_previous = 0;
  408. $next = 0;
  409. $display_order = 0;
  410. }
  411. $new_item_id = -1;
  412. $id = Database::escape_string($id);
  413. if ($type == 'quiz') {
  414. $sql = 'SELECT SUM(ponderation)
  415. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  416. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  417. ON quiz_question.id = quiz_rel_question.question_id
  418. WHERE quiz_rel_question.exercice_id = '.$id." AND
  419. quiz_question.c_id = $course_id AND
  420. quiz_rel_question.c_id = $course_id ";
  421. $rsQuiz = Database::query($sql);
  422. $max_score = Database :: result($rsQuiz, 0, 0);
  423. //Disabling the exercise if we add it inside a LP
  424. $exercise = new Exercise();
  425. $exercise->read($id);
  426. $exercise->disable();
  427. $exercise->save();
  428. } else {
  429. $max_score = 100;
  430. }
  431. if ($prerequisites != 0) {
  432. $sql_ins = "INSERT INTO " . $tbl_lp_item . " (
  433. c_id,
  434. lp_id, ".
  435. "item_type, ".
  436. "ref, ".
  437. "title, ".
  438. "description, ".
  439. "path, ".
  440. "max_score, ".
  441. "parent_item_id, ".
  442. "previous_item_id, ".
  443. "next_item_id, ".
  444. "display_order, ".
  445. "prerequisite, ".
  446. "max_time_allowed ".
  447. ") VALUES (
  448. $course_id ,
  449. ".$this->get_id() . ", ".
  450. "'" . $type . "', ".
  451. "'', ".
  452. "'" . $title . "', ".
  453. "'" . $description . "', ".
  454. "'" . $id . "', ".
  455. "'" . $max_score . "', ".
  456. $parent . ", ".
  457. $previous . ", ".
  458. $next . ", ".
  459. ($display_order +1) . ", ".
  460. $prerequisites . ", ".
  461. $max_time_allowed .
  462. ")";
  463. } else {
  464. // Insert new item.
  465. $sql_ins = "
  466. INSERT INTO " . $tbl_lp_item . " ( ".
  467. "c_id, ".
  468. "lp_id, ".
  469. "item_type, ".
  470. "ref, ".
  471. "title, ".
  472. "description, ".
  473. "path, ".
  474. "max_score, ".
  475. "parent_item_id, ".
  476. "previous_item_id, ".
  477. "next_item_id, ".
  478. "display_order, ".
  479. "max_time_allowed ".
  480. ") VALUES (".
  481. $course_id. ",".
  482. $this->get_id() . ",".
  483. "'" . $type . "',".
  484. "'',".
  485. "'" . $title . "',".
  486. "'" . $description . "',".
  487. "'" . $id . "',".
  488. "'" . $max_score . "',".
  489. $parent . ",".
  490. $previous . ",".
  491. $next . ",".
  492. ($display_order +1) . ",".
  493. $max_time_allowed .
  494. ")";
  495. }
  496. if ($this->debug > 2) {
  497. error_log('New LP - Inserting dokeos_chapter: ' . $sql_ins, 0);
  498. }
  499. $res_ins = Database::query($sql_ins);
  500. if ($res_ins > 0) {
  501. $new_item_id = Database :: insert_id($res_ins);
  502. // Update the item that should come after the new item.
  503. $sql_update_next = "
  504. UPDATE " . $tbl_lp_item . "
  505. SET previous_item_id = " . $new_item_id . "
  506. WHERE c_id = $course_id AND id = " . $next;
  507. Database::query($sql_update_next);
  508. // Update the item that should be before the new item.
  509. $sql_update_previous = "
  510. UPDATE " . $tbl_lp_item . "
  511. SET next_item_id = " . $new_item_id . "
  512. WHERE c_id = $course_id AND id = " . $tmp_previous;
  513. Database::query($sql_update_previous);
  514. // Update all the items after the new item.
  515. $sql_update_order = "
  516. UPDATE " . $tbl_lp_item . "
  517. SET display_order = display_order + 1
  518. WHERE
  519. c_id = $course_id AND
  520. lp_id = " . $this->get_id() . " AND
  521. id <> " . $new_item_id . " AND
  522. parent_item_id = " . $parent . " AND
  523. display_order > " . $display_order;
  524. Database::query($sql_update_order);
  525. // Update the item that should come after the new item.
  526. $sql_update_ref = "UPDATE " . $tbl_lp_item . "
  527. SET ref = " . $new_item_id . "
  528. WHERE c_id = $course_id AND id = " . $new_item_id;
  529. Database::query($sql_update_ref);
  530. }
  531. // Upload audio.
  532. if (!empty ($_FILES['mp3']['name'])) {
  533. // Create the audio folder if it does not exist yet.
  534. global $_course;
  535. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  536. if (!is_dir($filepath . 'audio')) {
  537. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  538. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  539. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  540. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  541. }
  542. // Upload the file in the documents tool.
  543. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  544. $file_path = handle_uploaded_document($_course, $_FILES['mp3'], api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document', '/audio', api_get_user_id(), '', '', '', '', false);
  545. // Getting the filename only.
  546. $file_components = explode('/', $file_path);
  547. $file = $file_components[count($file_components) - 1];
  548. // Store the mp3 file in the lp_item table.
  549. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '" . Database::escape_string($file) . "' WHERE id = '" . Database::escape_string($new_item_id) . "'";
  550. Database::query($sql_insert_audio);
  551. }
  552. return $new_item_id;
  553. }
  554. /**
  555. * Static admin function allowing addition of a learnpath to a course.
  556. * @param string Course code
  557. * @param string Learnpath name
  558. * @param string Learnpath description string, if provided
  559. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  560. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  561. * @param string Zip file containing the learnpath or directory containing the learnpath
  562. * @return integer The new learnpath ID on success, 0 on failure
  563. */
  564. public static function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '', $publicated_on = '', $expired_on = '') {
  565. global $charset;
  566. $course_id = api_get_course_int_id();
  567. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  568. // Check course code exists.
  569. // Check lp_name doesn't exist, otherwise append something.
  570. $i = 0;
  571. $name = Database::escape_string($name);
  572. // Session id.
  573. $session_id = api_get_session_id();
  574. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  575. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  576. $res_name = Database::query($check_name);
  577. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  578. //by default the publication date is the same that the creation date
  579. //The behaviour above was changed due BT#2800
  580. global $_custom;
  581. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  582. $publicated_on = '';
  583. } else {
  584. $publicated_on = api_get_utc_datetime();
  585. }
  586. } else {
  587. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  588. }
  589. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  590. $expired_on = '';
  591. } else {
  592. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  593. }
  594. while (Database :: num_rows($res_name)) {
  595. // There is already one such name, update the current one a bit.
  596. $i++;
  597. $name = $name . ' - ' . $i;
  598. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  599. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  600. $res_name = Database::query($check_name);
  601. }
  602. // New name does not exist yet; keep it.
  603. // Escape description.
  604. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset)); // Kevin: added htmlentities().
  605. $type = 1;
  606. switch ($learnpath) {
  607. case 'guess':
  608. break;
  609. case 'dokeos':
  610. case 'chamilo':
  611. $type = 1;
  612. break;
  613. case 'aicc':
  614. break;
  615. }
  616. switch ($origin) {
  617. case 'zip':
  618. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  619. break;
  620. case 'manual':
  621. default:
  622. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  623. $res_max = Database::query($get_max);
  624. if (Database :: num_rows($res_max) < 1) {
  625. $dsp = 1;
  626. } else {
  627. $row = Database :: fetch_array($res_max);
  628. $dsp = $row[0] + 1;
  629. }
  630. $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) " .
  631. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
  632. Database::query($sql_insert);
  633. $id = Database :: insert_id();
  634. if ($id > 0) {
  635. $course_info = api_get_course_info();
  636. // Insert into item_property.
  637. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  638. api_set_default_visibility($id, TOOL_LEARNPATH);
  639. return $id;
  640. }
  641. break;
  642. }
  643. }
  644. /**
  645. * Appends a message to the message attribute
  646. * @param string Message to append.
  647. */
  648. public function append_message($string) {
  649. if ($this->debug > 0) {
  650. error_log('New LP - In learnpath::append_message()', 0);
  651. }
  652. $this->message .= $string;
  653. }
  654. /**
  655. * Autocompletes the parents of an item in case it's been completed or passed
  656. * @param integer Optional ID of the item from which to look for parents
  657. */
  658. public function autocomplete_parents($item) {
  659. $debug = $this->debug;
  660. if ($debug) {
  661. error_log('Learnpath::autocomplete_parents()', 0);
  662. }
  663. if (empty($item)) {
  664. $item = $this->current;
  665. }
  666. $parent_id = $this->items[$item]->get_parent();
  667. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  668. // if $item points to an object and there is a parent.
  669. if ($debug) {
  670. error_log('Autocompleting parent of item ' . $item . ' (item ' . $parent_id . ')', 0);
  671. }
  672. $current_item = $this->items[$item];
  673. $parent = $this->items[$parent_id]; // Get the parent.
  674. // New experiment including failed and browsed in completed status.
  675. $current_status = $current_item->get_status();
  676. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  677. // If the current item is completed or passes or succeeded.
  678. $completed = true;
  679. if ($debug) {
  680. error_log('Status of current item is alright', 0);
  681. }
  682. foreach ($parent->get_children() as $child) {
  683. // Check all his brothers (parent's children) for completion status.
  684. if ($child != $item) {
  685. if ($debug) {
  686. error_log('Looking at brother with ID ' . $child . ', status is ' . $this->items[$child]->get_status(), 0);
  687. }
  688. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  689. // Trying completing parents of failed and browsed items as well.
  690. if ($this->items[$child]->status_is(array(
  691. 'completed',
  692. 'passed',
  693. 'succeeded',
  694. 'browsed',
  695. 'failed'
  696. ))) {
  697. // Keep completion status to true.
  698. } else {
  699. if ($this->debug > 2) {
  700. error_log('Found one incomplete child of ' . $parent_id . ': ' . $child . ' is ' . $this->items[$child]->get_status(), 0);
  701. }
  702. $completed = false;
  703. }
  704. }
  705. }
  706. if ($completed) { // If all the children were completed:
  707. $parent->set_status('completed');
  708. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  709. $this->update_queue[$parent->get_id()] = $parent->get_status();
  710. if ($debug) {
  711. error_log('Added parent to update queue ' . print_r($this->update_queue, true), 0);
  712. }
  713. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  714. }
  715. } else {
  716. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  717. }
  718. } else {
  719. if ($debug) {
  720. error_log("#$item is an item doesn't have parents");
  721. }
  722. }
  723. }
  724. /**
  725. * Autosaves the current results into the database for the whole learnpath
  726. */
  727. public function autosave() {
  728. if ($this->debug > 0) {
  729. error_log('New LP - In learnpath::autosave()', 0);
  730. }
  731. // TODO: Add save operations for the learnpath itself.
  732. }
  733. /**
  734. * Clears the message attribute
  735. */
  736. public function clear_message() {
  737. if ($this->debug > 0) {
  738. error_log('New LP - In learnpath::clear_message()', 0);
  739. }
  740. $this->message = '';
  741. }
  742. /**
  743. * Closes the current resource
  744. *
  745. * Stops the timer
  746. * Saves into the database if required
  747. * Clears the current resource data from this object
  748. * @return boolean True on success, false on failure
  749. */
  750. public function close() {
  751. $course_id = api_get_course_int_id();
  752. if ($this->debug > 0) {
  753. error_log('New LP - In learnpath::close()', 0);
  754. }
  755. if (empty ($this->lp_id)) {
  756. $this->error = 'Trying to close this learnpath but no ID is set';
  757. return false;
  758. }
  759. $this->current_time_stop = time();
  760. if ($this->save) {
  761. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  762. /*
  763. $sql = "UPDATE $learnpath_view_table " .
  764. "SET " .
  765. "stop_time = ".$this->current_time_stop.", " .
  766. "score = ".$this->current_score.", ".
  767. "WHERE learnpath_id = '".$this->lp_id."'";
  768. //$res = Database::query($sql);
  769. $res = Database::query($res);
  770. if (Database::affected_rows($res) < 1) {
  771. $this->error = 'Could not update learnpath_view table while closing learnpath';
  772. return false;
  773. }
  774. */
  775. }
  776. $this->ordered_items = array ();
  777. $this->index = 0;
  778. unset ($this->lp_id);
  779. //unset other stuff
  780. return true;
  781. }
  782. /**
  783. * Static admin function allowing removal of a learnpath
  784. * @param string Course code
  785. * @param integer Learnpath ID
  786. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  787. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  788. */
  789. public function delete($course = null, $id = null, $delete = 'keep') {
  790. $course_id = api_get_course_int_id();
  791. // TODO: Implement a way of getting this to work when the current object is not set.
  792. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  793. //if (empty($course)) { $course = api_get_course_id(); }
  794. //if (empty($id)) { $id = $this->get_id(); }
  795. // If an ID is specifically given and the current LP is not the same, prevent delete.
  796. if (!empty ($id) && ($id != $this->lp_id)) {
  797. return false;
  798. }
  799. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  800. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  801. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  802. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  803. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  804. // Delete lp item id.
  805. foreach ($this->items as $id => $dummy) {
  806. //$this->items[$id]->delete();
  807. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  808. $res_del_item_view = Database::query($sql_del_view);
  809. }
  810. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  811. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  812. $res_del_item = Database::query($sql_del_item);
  813. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  814. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  815. $res_del_view = Database::query($sql_del_view);
  816. self::toggle_publish($this->lp_id, 'i');
  817. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  818. if ($this->type == 2 || $this->type == 3) {
  819. // This is a scorm learning path, delete the files as well.
  820. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  821. $res = Database::query($sql);
  822. if (Database :: num_rows($res) > 0) {
  823. $row = Database :: fetch_array($res);
  824. $path = $row['path'];
  825. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  826. $res = Database::query($sql);
  827. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  828. if ($this->debug > 2) {
  829. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  830. }
  831. } else {
  832. // No other LP uses that directory, delete it.
  833. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  834. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  835. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  836. if ($this->debug > 2) {
  837. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  838. }
  839. // Proposed by Christophe (clefevre).
  840. if (strcmp(substr($path, -2), "/.") == 0) {
  841. $path = substr($path, 0, -1); // Remove "." at the end.
  842. }
  843. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  844. rmdirr($course_scorm_dir . $path);
  845. }
  846. }
  847. }
  848. }
  849. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  850. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  851. $res_del_lp = Database::query($sql_del_lp);
  852. $this->update_display_order(); // Updates the display order of all lps.
  853. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  854. require_once '../gradebook/lib/be.inc.php';
  855. // Delete link of gradebook tool
  856. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  857. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  858. $result = Database::query($sql);
  859. $row = Database :: fetch_array($result, 'ASSOC');*/
  860. // Fixing gradebook link deleted see #5229.
  861. /*
  862. if (!empty($row['id'])) {
  863. $link = LinkFactory :: load($row['id']);
  864. if ($link[0] != null) {
  865. $link[0]->delete();
  866. }
  867. }*/
  868. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  869. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  870. if ($link_info !== false) {
  871. remove_resource_from_course_gradebook($link_info['id']);
  872. }
  873. if (api_get_setting('search_enabled') == 'true') {
  874. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  875. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  876. }
  877. }
  878. /**
  879. * Removes all the children of one item - dangerous!
  880. * @param integer Element ID of which children have to be removed
  881. * @return integer Total number of children removed
  882. */
  883. public function delete_children_items($id) {
  884. $course_id = api_get_course_int_id();
  885. if ($this->debug > 0) {
  886. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  887. }
  888. $num = 0;
  889. if (empty ($id) || $id != strval(intval($id))) {
  890. return false;
  891. }
  892. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  893. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  894. $res = Database::query($sql);
  895. while ($row = Database :: fetch_array($res)) {
  896. $num += $this->delete_children_items($row['id']);
  897. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  898. $res_del = Database::query($sql_del);
  899. $num++;
  900. }
  901. return $num;
  902. }
  903. /**
  904. * Removes an item from the current learnpath
  905. * @param integer Elem ID (0 if first)
  906. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  907. * @return integer Number of elements moved
  908. * @todo implement resource removal
  909. */
  910. public function delete_item($id, $remove = 'keep') {
  911. $course_id = api_get_course_int_id();
  912. if ($this->debug > 0) {
  913. error_log('New LP - In learnpath::delete_item()', 0);
  914. }
  915. // TODO: Implement the resource removal.
  916. if (empty ($id) || $id != strval(intval($id))) {
  917. return false;
  918. }
  919. // First select item to get previous, next, and display order.
  920. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  921. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  922. $res_sel = Database::query($sql_sel);
  923. if (Database :: num_rows($res_sel) < 1) {
  924. return false;
  925. }
  926. $row = Database :: fetch_array($res_sel);
  927. $previous = $row['previous_item_id'];
  928. $next = $row['next_item_id'];
  929. $display = $row['display_order'];
  930. $parent = $row['parent_item_id'];
  931. $lp = $row['lp_id'];
  932. // Delete children items.
  933. $num = $this->delete_children_items($id);
  934. if ($this->debug > 2) {
  935. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  936. }
  937. // Now delete the item.
  938. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  939. if ($this->debug > 2) {
  940. error_log('New LP - Deleting item: ' . $sql_del, 0);
  941. }
  942. $res_del = Database::query($sql_del);
  943. // Now update surrounding items.
  944. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  945. $res_upd = Database::query($sql_upd);
  946. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  947. $res_upd = Database::query($sql_upd);
  948. // Now update all following items with new display order.
  949. $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";
  950. $res_all = Database::query($sql_all);
  951. //Removing prerequisites since the item will not longer exist
  952. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  953. $res_all = Database::query($sql_all);
  954. // Remove from search engine if enabled.
  955. if (api_get_setting('search_enabled') == 'true') {
  956. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  957. $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';
  958. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  959. $res = Database::query($sql);
  960. if (Database :: num_rows($res) > 0) {
  961. $row2 = Database :: fetch_array($res);
  962. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  963. $di = new ChamiloIndexer();
  964. $di->remove_document((int) $row2['search_did']);
  965. }
  966. $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';
  967. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  968. Database::query($sql);
  969. }
  970. }
  971. /**
  972. * Updates an item's content in place
  973. * @param integer Element ID
  974. * @param integer Parent item ID
  975. * @param integer Previous item ID
  976. * @param string Item title
  977. * @param string Item description
  978. * @param string Prerequisites (optional)
  979. * @param string Indexing terms (optional)
  980. * @param array The array resulting of the $_FILES[mp3] element
  981. * @return boolean True on success, false on error
  982. */
  983. public function edit_item($id, $parent, $previous, $title, $description, $prerequisites = 0, $audio = null, $max_time_allowed = 0) {
  984. $course_id = api_get_course_int_id();
  985. if ($this->debug > 0) {
  986. error_log('New LP - In learnpath::edit_item()', 0);
  987. }
  988. if (empty ($max_time_allowed)) {
  989. $max_time_allowed = 0;
  990. }
  991. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  992. return false;
  993. }
  994. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  995. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  996. $res_select = Database::query($sql_select);
  997. $row_select = Database :: fetch_array($res_select);
  998. $audio_update_sql = '';
  999. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1000. // Create the audio folder if it does not exist yet.
  1001. global $_course;
  1002. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1003. if (!is_dir($filepath . 'audio')) {
  1004. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1005. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  1006. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  1007. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  1008. }
  1009. // Upload file in documents.
  1010. $pi = pathinfo($audio['name']);
  1011. if ($pi['extension'] == 'mp3') {
  1012. $c_det = api_get_course_info($this->cc);
  1013. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1014. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1015. $path = substr($path, 7);
  1016. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1017. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1018. }
  1019. }
  1020. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1021. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1022. // TODO: htmlspecialchars to be checked for encoding related problems.
  1023. if ($same_parent && $same_previous) {
  1024. // Only update title and description.
  1025. $sql_update = " UPDATE " . $tbl_lp_item . "
  1026. SET title = '" . Database::escape_string($title) . "',
  1027. prerequisite = '" . $prerequisites . "',
  1028. description = '" . Database::escape_string($description) . "'
  1029. " . $audio_update_sql . ",
  1030. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1031. WHERE c_id = ".$course_id." AND id = " . $id;
  1032. $res_update = Database::query($sql_update);
  1033. } else {
  1034. $old_parent = $row_select['parent_item_id'];
  1035. $old_previous = $row_select['previous_item_id'];
  1036. $old_next = $row_select['next_item_id'];
  1037. $old_order = $row_select['display_order'];
  1038. $old_prerequisite = $row_select['prerequisite'];
  1039. $old_max_time_allowed = $row_select['max_time_allowed'];
  1040. /* BEGIN -- virtually remove the current item id */
  1041. /* for the next and previous item it is like the current item doesn't exist anymore */
  1042. if ($old_previous != 0) {
  1043. $sql_update_next = "
  1044. UPDATE " . $tbl_lp_item . "
  1045. SET next_item_id = " . $old_next . "
  1046. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1047. $res_update_next = Database::query($sql_update_next);
  1048. //echo '<p>' . $sql_update_next . '</p>';
  1049. }
  1050. if ($old_next != 0) {
  1051. $sql_update_previous = "
  1052. UPDATE " . $tbl_lp_item . "
  1053. SET previous_item_id = " . $old_previous . "
  1054. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1055. $res_update_previous = Database::query($sql_update_previous);
  1056. //echo '<p>' . $sql_update_previous . '</p>';
  1057. }
  1058. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1059. $sql_update_order = "
  1060. UPDATE " . $tbl_lp_item . "
  1061. SET display_order = display_order - 1
  1062. WHERE
  1063. c_id = ".$course_id." AND
  1064. display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
  1065. parent_item_id = " . $old_parent;
  1066. $res_update_order = Database::query($sql_update_order);
  1067. //echo '<p>' . $sql_update_order . '</p>';
  1068. /* END -- virtually remove the current item id */
  1069. /* BEGIN -- update the current item id to his new location */
  1070. if ($previous == 0) {
  1071. // Select the data of the item that should come after the current item.
  1072. $sql_select_old = "SELECT id, display_order
  1073. FROM " . $tbl_lp_item . "
  1074. WHERE
  1075. c_id = ".$course_id." AND
  1076. lp_id = " . $this->lp_id . " AND
  1077. parent_item_id = " . $parent . " AND
  1078. previous_item_id = " . $previous;
  1079. $res_select_old = Database::query($sql_select_old);
  1080. $row_select_old = Database :: fetch_array($res_select_old);
  1081. //echo '<p>' . $sql_select_old . '</p>';
  1082. // If the new parent didn't have children before.
  1083. if (Database :: num_rows($res_select_old) == 0) {
  1084. $new_next = 0;
  1085. $new_order = 1;
  1086. } else {
  1087. $new_next = $row_select_old['id'];
  1088. $new_order = $row_select_old['display_order'];
  1089. }
  1090. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1091. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1092. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1093. } else {
  1094. // Select the data of the item that should come before the current item.
  1095. $sql_select_old = " SELECT next_item_id, display_order
  1096. FROM " . $tbl_lp_item . "
  1097. WHERE c_id = ".$course_id." AND id = " . $previous;
  1098. $res_select_old = Database::query($sql_select_old);
  1099. $row_select_old = Database :: fetch_array($res_select_old);
  1100. //echo '<p>' . $sql_select_old . '</p>';
  1101. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1102. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1103. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1104. $new_next = $row_select_old['next_item_id'];
  1105. $new_order = $row_select_old['display_order'] + 1;
  1106. }
  1107. // TODO: htmlspecialchars to be checked for encoding related problems.
  1108. // Update the current item with the new data.
  1109. $sql_update = "UPDATE " . $tbl_lp_item . "
  1110. SET
  1111. title = '" . Database::escape_string($title) . "',
  1112. description = '" . Database::escape_string($description) . "',
  1113. parent_item_id = " . $parent . ",
  1114. previous_item_id = " . $previous . ",
  1115. next_item_id = " . $new_next . ",
  1116. display_order = " . $new_order . "
  1117. " . $audio_update_sql . "
  1118. WHERE c_id = ".$course_id." AND id = " . $id;
  1119. $res_update_next = Database::query($sql_update);
  1120. //echo '<p>' . $sql_update . '</p>';
  1121. if ($previous != 0) {
  1122. // Update the previous item's next_item_id.
  1123. $sql_update_previous = "
  1124. UPDATE " . $tbl_lp_item . "
  1125. SET next_item_id = " . $id . "
  1126. WHERE c_id = ".$course_id." AND id = " . $previous;
  1127. $res_update_next = Database::query($sql_update_previous);
  1128. //echo '<p>' . $sql_update_previous . '</p>';
  1129. }
  1130. if ($new_next != 0) {
  1131. // Update the next item's previous_item_id.
  1132. $sql_update_next = "
  1133. UPDATE " . $tbl_lp_item . "
  1134. SET previous_item_id = " . $id . "
  1135. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1136. $res_update_next = Database::query($sql_update_next);
  1137. //echo '<p>' . $sql_update_next . '</p>';
  1138. }
  1139. if ($old_prerequisite != $prerequisites) {
  1140. $sql_update_next = "
  1141. UPDATE " . $tbl_lp_item . "
  1142. SET prerequisite = " . $prerequisites . "
  1143. WHERE c_id = ".$course_id." AND id = " . $id;
  1144. $res_update_next = Database::query($sql_update_next);
  1145. }
  1146. if ($old_max_time_allowed != $max_time_allowed) {
  1147. $sql_update_max_time_allowed = "
  1148. UPDATE " . $tbl_lp_item . "
  1149. SET max_time_allowed = " . $max_time_allowed . "
  1150. WHERE c_id = ".$course_id." AND id = " . $id;
  1151. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1152. }
  1153. // Update all the items with the same or a bigger display_order than the current item.
  1154. $sql_update_order = "
  1155. UPDATE " . $tbl_lp_item . "
  1156. SET display_order = display_order + 1
  1157. WHERE
  1158. c_id = ".$course_id." AND
  1159. lp_id = " . $this->get_id() . " AND
  1160. id <> " . $id . " AND
  1161. parent_item_id = " . $parent . " AND
  1162. display_order >= " . $new_order;
  1163. $res_update_next = Database::query($sql_update_order);
  1164. }
  1165. }
  1166. /**
  1167. * Updates an item's prereq in place
  1168. * @param integer Element ID
  1169. * @param string Prerequisite Element ID
  1170. * @param string Prerequisite item type
  1171. * @param string Prerequisite min score
  1172. * @param string Prerequisite max score
  1173. * @return boolean True on success, false on error
  1174. */
  1175. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100) {
  1176. $course_id = api_get_course_int_id();
  1177. if ($this->debug > 0) {
  1178. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1179. }
  1180. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1181. return false;
  1182. }
  1183. $prerequisite_id = Database::escape_string($prerequisite_id);
  1184. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1185. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1186. $mastery_score = 0;
  1187. }
  1188. if (!is_numeric($max_score) || $max_score < 0) {
  1189. $max_score = 100;
  1190. }
  1191. if ($mastery_score > $max_score) {
  1192. $max_score = $mastery_score;
  1193. }
  1194. if (!is_numeric($prerequisite_id)) {
  1195. $prerequisite_id = 'NULL';
  1196. }
  1197. $sql_upd = " UPDATE " . $tbl_lp_item . "
  1198. SET prerequisite = " . $prerequisite_id . " WHERE c_id = ".$course_id." AND id = " . $id;
  1199. $res_upd = Database::query($sql_upd);
  1200. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1201. $sql_upd = " UPDATE " . $tbl_lp_item . " SET
  1202. mastery_score = " . $mastery_score .
  1203. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1204. " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
  1205. $res_upd = Database::query($sql_upd);
  1206. }
  1207. // TODO: Update the item object (can be ignored for now because refreshed).
  1208. return true;
  1209. }
  1210. /**
  1211. * Escapes a string with the available database escape function
  1212. * @param string String to escape
  1213. * @return string String escaped
  1214. * @deprecated use Database::escape_string
  1215. */
  1216. public function escape_string($string) {
  1217. //if ($this->debug > 0) { error_log('New LP - In learnpath::escape_string('.$string.')', 0); }
  1218. return Database::escape_string($string);
  1219. }
  1220. /**
  1221. * Static admin function exporting a learnpath into a zip file
  1222. * @param string Export type (scorm, zip, cd)
  1223. * @param string Course code
  1224. * @param integer Learnpath ID
  1225. * @param string Zip file name
  1226. * @return string Zip file path (or false on error)
  1227. */
  1228. public function export_lp($type, $course, $id, $zipname) {
  1229. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1230. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1231. return false;
  1232. }
  1233. $url = '';
  1234. switch ($type) {
  1235. case 'scorm':
  1236. break;
  1237. case 'zip':
  1238. break;
  1239. case 'cdrom':
  1240. break;
  1241. }
  1242. return $url;
  1243. }
  1244. /**
  1245. * Gets all the chapters belonging to the same parent as the item/chapter given
  1246. * Can also be called as abstract method
  1247. * @param integer Item ID
  1248. * @return array A list of all the "brother items" (or an empty array on failure)
  1249. */
  1250. public function get_brother_chapters($id) {
  1251. $course_id = api_get_course_int_id();
  1252. if ($this->debug > 0) {
  1253. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1254. }
  1255. if (empty ($id) OR $id != strval(intval($id))) {
  1256. return array ();
  1257. }
  1258. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1259. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1260. $res_parent = Database::query($sql_parent);
  1261. if (Database :: num_rows($res_parent) > 0) {
  1262. $row_parent = Database :: fetch_array($res_parent);
  1263. $parent = $row_parent['parent_item_id'];
  1264. $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";
  1265. $res_bros = Database::query($sql_bros);
  1266. $list = array ();
  1267. while ($row_bro = Database :: fetch_array($res_bros)) {
  1268. $list[] = $row_bro;
  1269. }
  1270. return $list;
  1271. }
  1272. return array ();
  1273. }
  1274. /**
  1275. * Gets all the items belonging to the same parent as the item given
  1276. * Can also be called as abstract method
  1277. * @param integer Item ID
  1278. * @return array A list of all the "brother items" (or an empty array on failure)
  1279. */
  1280. public function get_brother_items($id) {
  1281. $course_id = api_get_course_int_id();
  1282. if ($this->debug > 0) {
  1283. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1284. }
  1285. if (empty ($id) OR $id != strval(intval($id))) {
  1286. return array ();
  1287. }
  1288. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1289. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1290. $res_parent = Database::query($sql_parent);
  1291. if (Database :: num_rows($res_parent) > 0) {
  1292. $row_parent = Database :: fetch_array($res_parent);
  1293. $parent = $row_parent['parent_item_id'];
  1294. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1295. $res_bros = Database::query($sql_bros);
  1296. $list = array ();
  1297. while ($row_bro = Database :: fetch_array($res_bros)) {
  1298. $list[] = $row_bro;
  1299. }
  1300. return $list;
  1301. }
  1302. return array ();
  1303. }
  1304. /**
  1305. * Get the specific prefix index terms of this learning path
  1306. * @return array Array of terms
  1307. */
  1308. public function get_common_index_terms_by_prefix($prefix) {
  1309. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1310. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1311. $prefix_terms = array();
  1312. if (!empty($terms)) {
  1313. foreach ($terms as $term) {
  1314. $prefix_terms[] = $term['value'];
  1315. }
  1316. }
  1317. return $prefix_terms;
  1318. }
  1319. /**
  1320. * Gets the number of items currently completed
  1321. * @return integer The number of items currently completed
  1322. */
  1323. public function get_complete_items_count() {
  1324. if ($this->debug > 0) {
  1325. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1326. }
  1327. $i = 0;
  1328. foreach ($this->items as $id => $dummy) {
  1329. // Trying failed and browsed considered "progressed" as well.
  1330. if ($this->items[$id]->status_is(array(
  1331. 'completed',
  1332. 'passed',
  1333. 'succeeded',
  1334. 'browsed',
  1335. 'failed'
  1336. )) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir') {
  1337. $i++;
  1338. }
  1339. }
  1340. return $i;
  1341. }
  1342. /**
  1343. * Gets the current item ID
  1344. * @return integer The current learnpath item id
  1345. */
  1346. public function get_current_item_id() {
  1347. $current = 0;
  1348. if ($this->debug > 0) {
  1349. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1350. }
  1351. if (!empty ($this->current)) {
  1352. $current = $this->current;
  1353. }
  1354. if ($this->debug > 2) {
  1355. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1356. }
  1357. return $current;
  1358. }
  1359. /**
  1360. * Force to get the first learnpath item id
  1361. * @return integer The current learnpath item id
  1362. */
  1363. public function get_first_item_id() {
  1364. $current = 0;
  1365. if (is_array($this->ordered_items)) {
  1366. $current = $this->ordered_items[0];
  1367. }
  1368. return $current;
  1369. }
  1370. /**
  1371. * Gets the total number of items available for viewing in this SCORM
  1372. * @return integer The total number of items
  1373. */
  1374. public function get_total_items_count() {
  1375. if ($this->debug > 0) {
  1376. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1377. }
  1378. return count($this->items);
  1379. }
  1380. /**
  1381. * Gets the total number of items available for viewing in this SCORM but without chapters
  1382. * @return integer The total no-chapters number of items
  1383. */
  1384. public function get_total_items_count_without_chapters() {
  1385. if ($this->debug > 0) {
  1386. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1387. }
  1388. $total = 0;
  1389. foreach ($this->items as $temp2) {
  1390. if (!in_array($temp2->get_type(), array(
  1391. 'dokeos_chapter',
  1392. 'chapter',
  1393. 'dir'
  1394. )))
  1395. $total++;
  1396. }
  1397. return $total;
  1398. }
  1399. /**
  1400. * Gets the first element URL.
  1401. * @return string URL to load into the viewer
  1402. */
  1403. public function first() {
  1404. if ($this->debug > 0) {
  1405. error_log('New LP - In learnpath::first()', 0);
  1406. error_log('$this->last_item_seen '.$this->last_item_seen);
  1407. //error_log('$this->items '.print_r($this->items, 1));
  1408. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1409. }
  1410. // Test if the last_item_seen exists and is not a dir.
  1411. if (count($this->ordered_items) == 0) {
  1412. $this->index = 0;
  1413. }
  1414. if ($this->debug > 0) {
  1415. if (isset($this->items[$this->last_item_seen])) {
  1416. $status = $this->items[$this->last_item_seen]->get_status();
  1417. }
  1418. error_log('status '.$status);
  1419. }
  1420. if (!empty($this->last_item_seen) &&
  1421. !empty($this->items[$this->last_item_seen]) &&
  1422. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1423. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1424. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1425. //&& !$this->items[$this->last_item_seen]->is_done()
  1426. ) {
  1427. if ($this->debug > 2) {
  1428. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1429. }
  1430. $index = -1;
  1431. foreach ($this->ordered_items as $myindex => $item_id) {
  1432. if ($item_id == $this->last_item_seen) {
  1433. $index = $myindex;
  1434. break;
  1435. }
  1436. }
  1437. if ($index == -1) {
  1438. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1439. if ($this->debug > 2) {
  1440. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1441. }
  1442. return false;
  1443. } else {
  1444. $this->last = $this->last_item_seen;
  1445. $this->current = $this->last_item_seen;
  1446. $this->index = $index;
  1447. }
  1448. } else {
  1449. if ($this->debug > 2) {
  1450. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1451. }
  1452. $index = 0;
  1453. // Loop through all ordered items and stop at the first item that is
  1454. // not a directory *and* that has not been completed yet.
  1455. while ( !empty($this->ordered_items[$index]) AND
  1456. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1457. (
  1458. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1459. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1460. $this->items[$this->ordered_items[$index]]->is_done() === true
  1461. ) AND $index < $this->max_ordered_items) {
  1462. $index++;
  1463. }
  1464. $this->last = $this->current;
  1465. // current is
  1466. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1467. $this->index = $index;
  1468. if ($this->debug > 2) {
  1469. error_log('$index ' . $index);
  1470. }
  1471. if ($this->debug > 2) {
  1472. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1473. }
  1474. }
  1475. if ($this->debug > 2) {
  1476. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1477. }
  1478. }
  1479. /**
  1480. * Gets the information about an item in a format usable as JavaScript to update
  1481. * the JS API by just printing this content into the <head> section of the message frame
  1482. * @param integer Item ID
  1483. * @return string
  1484. */
  1485. public function get_js_info($item_id = '') {
  1486. if ($this->debug > 0) {
  1487. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1488. }
  1489. $info = '';
  1490. $item_id = Database::escape_string($item_id);
  1491. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1492. //if item is defined, return values from DB
  1493. $oItem = $this->items[$item_id];
  1494. $info .= '<script language="javascript">';
  1495. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1496. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1497. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1498. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1499. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1500. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1501. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1502. $info .= "top.set_flag_synchronized();";
  1503. $info .= '</script>';
  1504. if ($this->debug > 2) {
  1505. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1506. }
  1507. return $info;
  1508. } else {
  1509. // If item_id is empty, just update to default SCORM data.
  1510. $info .= '<script language="javascript">';
  1511. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1512. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1513. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1514. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1515. $info .= "top.set_session_time('" . learnpathItem :: get_scorm_time('js') . "');";
  1516. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1517. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1518. $info .= "top.set_flag_synchronized();";
  1519. $info .= '</script>';
  1520. if ($this->debug > 2) {
  1521. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1522. }
  1523. return $info;
  1524. }
  1525. }
  1526. /**
  1527. * Gets the js library from the database
  1528. * @return string The name of the javascript library to be used
  1529. */
  1530. public function get_js_lib() {
  1531. $lib = '';
  1532. if (!empty ($this->js_lib)) {
  1533. $lib = $this->js_lib;
  1534. }
  1535. return $lib;
  1536. }
  1537. /**
  1538. * Gets the learnpath database ID
  1539. * @return integer Learnpath ID in the lp table
  1540. */
  1541. public function get_id() {
  1542. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1543. if (!empty ($this->lp_id)) {
  1544. return $this->lp_id;
  1545. } else {
  1546. return 0;
  1547. }
  1548. }
  1549. /**
  1550. * Gets the last element URL.
  1551. * @return string URL to load into the viewer
  1552. */
  1553. public function get_last() {
  1554. if ($this->debug > 0) {
  1555. error_log('New LP - In learnpath::get_last()', 0);
  1556. }
  1557. $this->index = count($this->ordered_items) - 1;
  1558. return $this->ordered_items[$this->index];
  1559. }
  1560. /**
  1561. * Gets the navigation bar for the learnpath display screen
  1562. * @return string The HTML string to use as a navigation bar
  1563. */
  1564. public function get_navigation_bar() {
  1565. if ($this->debug > 0) {
  1566. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1567. }
  1568. $navbar = null;
  1569. $lp_id = $this->lp_id;
  1570. $mycurrentitemid = $this->get_current_item_id();
  1571. if ($this->mode == 'fullscreen') {
  1572. $navbar = '
  1573. <div class="buttons">
  1574. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
  1575. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1576. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>.
  1577. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
  1578. </div>';
  1579. } else {
  1580. $navbar = '
  1581. <div class="buttons">
  1582. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
  1583. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1584. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>
  1585. </div>';
  1586. }
  1587. return $navbar;
  1588. }
  1589. /**
  1590. * Gets the next resource in queue (url).
  1591. * @return string URL to load into the viewer
  1592. */
  1593. public function get_next_index() {
  1594. if ($this->debug > 0) {
  1595. error_log('New LP - In learnpath::get_next_index()', 0);
  1596. }
  1597. // TODO
  1598. $index = $this->index;
  1599. $index++;
  1600. if ($this->debug > 2) {
  1601. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1602. }
  1603. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1604. $index++;
  1605. if ($index == $this->max_ordered_items){
  1606. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1607. return $this->index;
  1608. } else {
  1609. return $index;
  1610. }
  1611. }
  1612. }
  1613. if (empty ($this->ordered_items[$index])) {
  1614. return $this->index;
  1615. }
  1616. if ($this->debug > 2) {
  1617. error_log('New LP - index is now ' . $index, 0);
  1618. }
  1619. return $index;
  1620. }
  1621. /**
  1622. * Gets item_id for the next element
  1623. * @return integer Next item (DB) ID
  1624. */
  1625. public function get_next_item_id() {
  1626. if ($this->debug > 0) {
  1627. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1628. }
  1629. $new_index = $this->get_next_index();
  1630. if (!empty ($new_index)) {
  1631. if (isset ($this->ordered_items[$new_index])) {
  1632. if ($this->debug > 2) {
  1633. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1634. }
  1635. return $this->ordered_items[$new_index];
  1636. }
  1637. }
  1638. if ($this->debug > 2) {
  1639. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1640. }
  1641. return 0;
  1642. }
  1643. /**
  1644. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1645. *
  1646. * Generally, the package provided is in the form of a zip file, so the function
  1647. * has been written to test a zip file. If not a zip, the function will return the
  1648. * default return value: ''
  1649. * @param string the path to the file
  1650. * @param string the original name of the file
  1651. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1652. */
  1653. public static function get_package_type($file_path, $file_name) {
  1654. // Get name of the zip file without the extension.
  1655. $file_info = pathinfo($file_name);
  1656. $filename = $file_info['basename']; // Name including extension.
  1657. $extension = $file_info['extension']; // Extension only.
  1658. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1659. 'dll',
  1660. 'exe'
  1661. ))) {
  1662. return 'oogie';
  1663. }
  1664. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1665. 'dll',
  1666. 'exe'
  1667. ))) {
  1668. return 'woogie';
  1669. }
  1670. $file_base_name = str_replace('.' . $extension, '', $filename); // Filename without its extension.
  1671. $zipFile = new PclZip($file_path);
  1672. // Check the zip content (real size and file extension).
  1673. $zipContentArray = $zipFile->listContent();
  1674. $package_type = '';
  1675. $at_root = false;
  1676. $manifest = '';
  1677. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1678. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1679. foreach ($zipContentArray as $thisContent) {
  1680. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1681. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1682. }
  1683. elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1684. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1685. $package_type = 'scorm';
  1686. break; // Exit the foreach loop.
  1687. }
  1688. elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1689. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1690. $package_type = 'aicc';
  1691. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1692. } else {
  1693. $package_type = '';
  1694. }
  1695. }
  1696. }
  1697. return $package_type;
  1698. }
  1699. /**
  1700. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1701. * @return string URL to load into the viewer
  1702. */
  1703. public function get_previous_index() {
  1704. if ($this->debug > 0) {
  1705. error_log('New LP - In learnpath::get_previous_index()', 0);
  1706. }
  1707. $index = $this->index;
  1708. if (isset ($this->ordered_items[$index -1])) {
  1709. $index--;
  1710. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1711. $index--;
  1712. if ($index < 0) {
  1713. return $this->index;
  1714. }
  1715. }
  1716. } else {
  1717. if ($this->debug > 2) {
  1718. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1719. }
  1720. // There is no previous item.
  1721. }
  1722. return $index;
  1723. }
  1724. /**
  1725. * Gets item_id for the next element
  1726. * @return integer Previous item (DB) ID
  1727. */
  1728. public function get_previous_item_id() {
  1729. if ($this->debug > 0) {
  1730. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1731. }
  1732. $new_index = $this->get_previous_index();
  1733. return $this->ordered_items[$new_index];
  1734. }
  1735. /**
  1736. * Gets the progress value from the progress_db attribute
  1737. * @return integer Current progress value
  1738. */
  1739. public function get_progress() {
  1740. if ($this->debug > 0) {
  1741. error_log('New LP - In learnpath::get_progress()', 0);
  1742. }
  1743. if (!empty ($this->progress_db)) {
  1744. return $this->progress_db;
  1745. }
  1746. return 0;
  1747. }
  1748. /**
  1749. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1750. * @param integer Learnpath ID
  1751. * @param integer User ID
  1752. * @param string Mode of display ('%','abs' or 'both')
  1753. * @param string Course database name (optional, defaults to '')
  1754. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1755. * @return integer Current progress value as found in the database
  1756. */
  1757. public static function get_db_progress($lp_id, $user_id, $mode = '%', $course_code = '', $sincere = false,$session_id = 0) {
  1758. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1759. $session_id = intval($session_id);
  1760. $course_info = api_get_course_info($course_code);
  1761. $session_condition = api_get_session_condition($session_id);
  1762. $course_id = $course_info['real_id'];
  1763. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1764. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1765. $res = Database::query($sql);
  1766. $view_id = 0;
  1767. if (Database :: num_rows($res) > 0) {
  1768. $row = Database :: fetch_array($res);
  1769. $progress = $row['progress'];
  1770. $view_id = $row['id'];
  1771. } else {
  1772. if ($sincere) {
  1773. return null;
  1774. }
  1775. }
  1776. if (empty ($progress)) {
  1777. $progress = '0';
  1778. }
  1779. if ($mode == '%') {
  1780. return $progress . '%';
  1781. } else {
  1782. // Get the number of items completed and the number of items total.
  1783. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  1784. $sql = "SELECT count(*) FROM $tbl
  1785. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = " . $lp_id . " AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  1786. $res = Database::query($sql);
  1787. $row = Database :: fetch_array($res);
  1788. $total = $row[0];
  1789. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1790. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  1791. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  1792. // Trying as also counting browsed and failed items.
  1793. $sql = "SELECT count(distinct(lp_item_id))
  1794. FROM $tbl_item_view as item_view
  1795. INNER JOIN $tbl_item as item
  1796. ON item.id = item_view.lp_item_id
  1797. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  1798. WHERE
  1799. item_view.c_id = $course_id AND
  1800. item.c_id = $course_id AND
  1801. lp_view_id = " . $view_id . " AND
  1802. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  1803. $res = Database::query($sql);
  1804. $row = Database :: fetch_array($res);
  1805. $completed = $row[0];
  1806. if ($mode == 'abs') {
  1807. return $completed . '/' . $total;
  1808. } elseif ($mode == 'both') {
  1809. if ($progress < ($completed / ($total ? $total : 1))) {
  1810. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  1811. }
  1812. return $progress . '% (' . $completed . '/' . $total . ')';
  1813. }
  1814. }
  1815. return $progress;
  1816. }
  1817. /**
  1818. * Returns the HTML necessary to print a mediaplayer block inside a page
  1819. * @return string The mediaplayer HTML
  1820. */
  1821. public function get_mediaplayer($autostart='true') {
  1822. $course_id = api_get_course_int_id();
  1823. global $_course;
  1824. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1825. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1826. // Getting all the information about the item.
  1827. $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 " .
  1828. "WHERE lp.id = '" . $_SESSION['oLP']->current . "' AND
  1829. lp.c_id = $course_id AND
  1830. lp_view.c_id = $course_id";
  1831. $result = Database::query($sql);
  1832. $row = Database::fetch_assoc($result);
  1833. $output = '';
  1834. if (!empty ($row['audio'])) {
  1835. $list = $_SESSION['oLP']->get_toc();
  1836. $type_quiz = false;
  1837. foreach($list as $toc) {
  1838. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  1839. $type_quiz = true;
  1840. }
  1841. }
  1842. if ($type_quiz) {
  1843. if ($_SESSION['oLP']->prevent_reinit == 1) {
  1844. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  1845. } else {
  1846. $autostart_audio = $autostart;
  1847. }
  1848. } else {
  1849. $autostart_audio = 'true';
  1850. }
  1851. // The mp3 player.
  1852. $output = '<div id="container">';
  1853. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  1854. $output .= '<script type="text/javascript">
  1855. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  1856. s1.addParam("allowscriptaccess","always");
  1857. s1.addParam("flashvars","file=' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=' . $autostart_audio.'");
  1858. s1.write("container");
  1859. </script>
  1860. </div>';
  1861. }
  1862. return $output;
  1863. }
  1864. /**
  1865. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  1866. * @param int Learnpath id
  1867. * @param int Student id
  1868. * @param string Course code (optional)
  1869. * @return bool True if
  1870. */
  1871. public static function is_lp_visible_for_student($lp_id, $student_id, $course = null) {
  1872. $lp_id = (int)$lp_id;
  1873. $course = api_get_course_info($course);
  1874. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  1875. // Get current prerequisite
  1876. $sql = "SELECT id, prerequisite, publicated_on, expired_on FROM $tbl_learnpath WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  1877. $rs = Database::query($sql);
  1878. $now = time();
  1879. if (Database::num_rows($rs)>0) {
  1880. $row = Database::fetch_array($rs, 'ASSOC');
  1881. $prerequisite = $row['prerequisite'];
  1882. $is_visible = true;
  1883. $progress = 0;
  1884. if (!empty($prerequisite)) {
  1885. $progress = self::get_db_progress($prerequisite,$student_id,'%', '', false, api_get_session_id());
  1886. $progress = intval($progress);
  1887. if ($progress < 100) {
  1888. $is_visible = false;
  1889. }
  1890. }
  1891. // Also check the time availability of the LP
  1892. if ($is_visible) {
  1893. //Adding visibility reestrinctions
  1894. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  1895. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  1896. //api_not_allowed();
  1897. $is_visible = false;
  1898. }
  1899. }
  1900. //Blocking empty start times see BT#2800
  1901. global $_custom;
  1902. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  1903. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  1904. //api_not_allowed();
  1905. $is_visible = false;
  1906. }
  1907. }
  1908. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  1909. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  1910. //api_not_allowed();
  1911. $is_visible = false;
  1912. }
  1913. }
  1914. }
  1915. return $is_visible;
  1916. }
  1917. return false;
  1918. }
  1919. /**
  1920. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  1921. * completed so far.
  1922. * @param string Mode in which we want the values
  1923. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  1924. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  1925. * @param boolean true if it comes from a Diplay LP view
  1926. * @return string HTML string containing the progress bar
  1927. */
  1928. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false) {
  1929. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  1930. global $lp_theme_css;
  1931. // Setting up the CSS path of the current style if exists.
  1932. if (!empty ($lp_theme_css)) {
  1933. $css_path = api_get_path(WEB_CODE_PATH) . 'css/' . $lp_theme_css . '/images/';
  1934. } else {
  1935. $css_path = '../img/';
  1936. }
  1937. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  1938. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  1939. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  1940. }
  1941. $text = $percentage . $text_add;
  1942. //@todo use Display::display_progress();
  1943. $output = '<div class="progress progress-striped">
  1944. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  1945. </div>
  1946. <div class="progresstext" id="progress_text">' . $text . '</div>';
  1947. return $output;
  1948. }
  1949. /**
  1950. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  1951. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  1952. * @param integer Additional steps to fake as completed
  1953. * @return list Percentage or number and symbol (% or /xx)
  1954. */
  1955. public function get_progress_bar_text($mode = '', $add = 0) {
  1956. if ($this->debug > 0) {
  1957. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  1958. }
  1959. if (empty ($mode)) {
  1960. $mode = $this->progress_bar_mode;
  1961. }
  1962. $total_items = $this->get_total_items_count_without_chapters();
  1963. if ($this->debug > 2) {
  1964. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  1965. }
  1966. $i = $this->get_complete_items_count();
  1967. if ($this->debug > 2) {
  1968. error_log('New LP - Items completed so far: ' . $i, 0);
  1969. }
  1970. if ($add != 0) {
  1971. $i += $add;
  1972. if ($this->debug > 2) {
  1973. error_log('New LP - Items completed so far (+modifier): ' . $i, 0);
  1974. }
  1975. }
  1976. $text = '';
  1977. if ($i > $total_items) {
  1978. $i = $total_items;
  1979. }
  1980. if ($mode == '%') {
  1981. if ($total_items > 0) {
  1982. $percentage = ((float) $i / (float) $total_items) * 100;
  1983. } else {
  1984. $percentage = 0;
  1985. }
  1986. $percentage = number_format($percentage, 0);
  1987. $text = '%';
  1988. }
  1989. elseif ($mode == 'abs') {
  1990. $percentage = $i;
  1991. $text = '/' . $total_items;
  1992. }
  1993. return array (
  1994. $percentage,
  1995. $text
  1996. );
  1997. }
  1998. /**
  1999. * Gets the progress bar mode
  2000. * @return string The progress bar mode attribute
  2001. */
  2002. public function get_progress_bar_mode() {
  2003. if ($this->debug > 0) {
  2004. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2005. }
  2006. if (!empty ($this->progress_bar_mode)) {
  2007. return $this->progress_bar_mode;
  2008. } else {
  2009. return '%';
  2010. }
  2011. }
  2012. /**
  2013. * Gets the learnpath proximity (remote or local)
  2014. * @return string Learnpath proximity
  2015. */
  2016. public function get_proximity() {
  2017. if ($this->debug > 0) {
  2018. error_log('New LP - In learnpath::get_proximity()', 0);
  2019. }
  2020. if (!empty ($this->proximity)) {
  2021. return $this->proximity;
  2022. } else {
  2023. return '';
  2024. }
  2025. }
  2026. /**
  2027. * Gets the learnpath theme (remote or local)
  2028. * @return string Learnpath theme
  2029. */
  2030. public function get_theme() {
  2031. if ($this->debug > 0) {
  2032. error_log('New LP - In learnpath::get_theme()', 0);
  2033. }
  2034. if (!empty ($this->theme)) {
  2035. return $this->theme;
  2036. } else {
  2037. return '';
  2038. }
  2039. }
  2040. /**
  2041. * Gets the learnpath session id
  2042. * @return string Learnpath theme
  2043. */
  2044. public function get_lp_session_id() {
  2045. if ($this->debug > 0) {
  2046. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2047. }
  2048. if (!empty ($this->lp_session_id)) {
  2049. return $this->lp_session_id;
  2050. } else {
  2051. return 0;
  2052. }
  2053. }
  2054. /**
  2055. * Gets the learnpath image
  2056. * @return string Web URL of the LP image
  2057. */
  2058. public function get_preview_image() {
  2059. if ($this->debug > 0) {
  2060. error_log('New LP - In learnpath::get_preview_image()', 0);
  2061. }
  2062. if (!empty($this->preview_image)) {
  2063. return $this->preview_image;
  2064. } else {
  2065. return '';
  2066. }
  2067. }
  2068. public function get_preview_image_path($size = null, $path_type = 'web') {
  2069. $preview_image = $this->get_preview_image();
  2070. if (isset($preview_image) && !empty($preview_image)) {
  2071. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2072. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2073. if (isset($size)) {
  2074. $info = pathinfo($preview_image);
  2075. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2076. if (file_exists($image_sys_path.$image_custom_size)) {
  2077. if ($path_type == 'web') {
  2078. return $image_path.$image_custom_size;
  2079. } else {
  2080. return $image_sys_path.$image_custom_size;
  2081. }
  2082. }
  2083. } else {
  2084. if ($path_type == 'web') {
  2085. return $image_path.$preview_image;
  2086. } else {
  2087. return $image_sys_path.$preview_image;
  2088. }
  2089. }
  2090. }
  2091. return false;
  2092. }
  2093. /**
  2094. * Gets the learnpath author
  2095. * @return string LP's author
  2096. */
  2097. public function get_author() {
  2098. if ($this->debug > 0) {
  2099. error_log('New LP - In learnpath::get_author()', 0);
  2100. }
  2101. if (!empty ($this->author)) {
  2102. return $this->author;
  2103. } else {
  2104. return '';
  2105. }
  2106. }
  2107. /**
  2108. * Gets the learnpath author
  2109. * @return string LP's author
  2110. */
  2111. public function get_hide_toc_frame() {
  2112. if ($this->debug > 0) {
  2113. error_log('New LP - In learnpath::get_author()', 0);
  2114. }
  2115. if (!empty ($this->hide_toc_frame)) {
  2116. return $this->hide_toc_frame;
  2117. } else {
  2118. return '';
  2119. }
  2120. }
  2121. /**
  2122. * Generate a new prerequisites string for a given item. If this item was a sco and
  2123. * its prerequisites were strings (instead of IDs), then transform those strings into
  2124. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2125. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2126. * same rule as the scorm_export() method
  2127. * @param integer Item ID
  2128. * @return string Prerequisites string ready for the export as SCORM
  2129. */
  2130. public function get_scorm_prereq_string($item_id) {
  2131. if ($this->debug > 0) {
  2132. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2133. }
  2134. if (!is_object($this->items[$item_id])) {
  2135. return false;
  2136. }
  2137. $oItem = $this->items[$item_id];
  2138. $prereq = $oItem->get_prereq_string();
  2139. if (empty($prereq)) {
  2140. return '';
  2141. }
  2142. //var_dump($this->refs_list, $prereq);
  2143. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2144. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2145. // then simply return it (with the ITEM_ prefix).
  2146. //return 'ITEM_' . $prereq;
  2147. return $this->items[$prereq]->ref;
  2148. } else {
  2149. if (isset($this->refs_list[$prereq])) {
  2150. // It's a simple string item from which the ID can be found in the refs list,
  2151. // so we can transform it directly to an ID for export.
  2152. return 'ITEM_' . $this->refs_list[$prereq];
  2153. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2154. return 'ITEM_'.$prereq;
  2155. } else {
  2156. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2157. // and replace them, one by one, by the internal IDs (chamilo db)
  2158. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2159. // by a space as well.
  2160. $find = array (
  2161. '&',
  2162. '|',
  2163. '~',
  2164. '=',
  2165. '<>',
  2166. '{',
  2167. '}',
  2168. '*',
  2169. '(',
  2170. ')'
  2171. );
  2172. $replace = array (
  2173. ' ',
  2174. ' ',
  2175. ' ',
  2176. ' ',
  2177. ' ',
  2178. ' ',
  2179. ' ',
  2180. ' ',
  2181. ' ',
  2182. ' '
  2183. );
  2184. $prereq_mod = str_replace($find, $replace, $prereq);
  2185. $ids = split(' ', $prereq_mod);
  2186. foreach ($ids as $id) {
  2187. $id = trim($id);
  2188. if (isset ($this->refs_list[$id])) {
  2189. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2190. }
  2191. }
  2192. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2193. return $prereq;
  2194. }
  2195. }
  2196. }
  2197. /**
  2198. * Returns the XML DOM document's node
  2199. * @param resource Reference to a list of objects to search for the given ITEM_*
  2200. * @param string The identifier to look for
  2201. * @return mixed The reference to the element found with that identifier. False if not found
  2202. */
  2203. public function get_scorm_xml_node(& $children, $id) {
  2204. for ($i = 0; $i < $children->length; $i++) {
  2205. $item_temp = $children->item($i);
  2206. if ($item_temp->nodeName == 'item') {
  2207. if ($item_temp->getAttribute('identifier') == $id) {
  2208. return $item_temp;
  2209. }
  2210. }
  2211. $subchildren = $item_temp->childNodes;
  2212. if ($subchildren->length > 0) {
  2213. $val = $this->get_scorm_xml_node($subchildren, $id);
  2214. if (is_object($val)) {
  2215. return $val;
  2216. }
  2217. }
  2218. }
  2219. return false;
  2220. }
  2221. /**
  2222. * Returns a usable array of stats related to the current learnpath and user
  2223. * @return array Well-formatted array containing status for the current learnpath
  2224. */
  2225. public function get_stats() {
  2226. if ($this->debug > 0) {
  2227. error_log('New LP - In learnpath::get_stats()', 0);
  2228. }
  2229. // TODO
  2230. }
  2231. /**
  2232. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2233. * the given course (for all learnpaths and all users)
  2234. * @param string Course code
  2235. * @return array Well-formatted array containing status for the course's learnpaths
  2236. */
  2237. public function get_stats_course($course) {
  2238. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2239. // TODO
  2240. }
  2241. /**
  2242. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2243. * the given course and learnpath (for all users)
  2244. * @param string Course code
  2245. * @param integer Learnpath ID
  2246. * @return array Well-formatted array containing status for the specified learnpath
  2247. */
  2248. public function get_stats_lp($course, $lp) {
  2249. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2250. // TODO
  2251. }
  2252. /**
  2253. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2254. * the given course, learnpath and user.
  2255. * @param string Course code
  2256. * @param integer Learnpath ID
  2257. * @param integer User ID
  2258. * @return array Well-formatted array containing status for the specified learnpath and user
  2259. */
  2260. public function get_stats_lp_user($course, $lp, $user) {
  2261. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2262. // TODO
  2263. }
  2264. /**
  2265. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2266. * the given course and learnpath (for all users)
  2267. * @param string Course code
  2268. * @param integer User ID
  2269. * @return array Well-formatted array containing status for the user's learnpaths
  2270. */
  2271. public function get_stats_user($course, $user) {
  2272. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2273. // TODO
  2274. }
  2275. /**
  2276. * Gets the status list for all LP's items
  2277. * @return array Array of [index] => [item ID => current status]
  2278. */
  2279. public function get_items_status_list() {
  2280. if ($this->debug > 0) {
  2281. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2282. }
  2283. $list = array ();
  2284. foreach ($this->ordered_items as $item_id) {
  2285. $list[] = array (
  2286. $item_id => $this->items[$item_id]->get_status()
  2287. );
  2288. }
  2289. return $list;
  2290. }
  2291. /**
  2292. * Return the number of interactions for the given learnpath Item View ID.
  2293. * This method can be used as static.
  2294. * @param integer Item View ID
  2295. * @param integer course id
  2296. * @return integer Number of interactions
  2297. */
  2298. public static function get_interactions_count_from_db($lp_iv_id, $course_id) {
  2299. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2300. $lp_iv_id = intval($lp_iv_id);
  2301. $course_id = intval($course_id);
  2302. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2303. $res = Database::query($sql);
  2304. $res = 0;
  2305. if (Database::num_rows($res)) {
  2306. $row = Database::fetch_array($res);
  2307. $num = $row[0];
  2308. }
  2309. return $num;
  2310. }
  2311. /**
  2312. * Return the interactions as an array for the given lp_iv_id.
  2313. * This method can be used as static.
  2314. * @param integer Learnpath Item View ID
  2315. * @return array
  2316. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2317. */
  2318. public static function get_iv_interactions_array($lp_iv_id = 0) {
  2319. $course_id = api_get_course_int_id();
  2320. $list = array ();
  2321. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2322. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2323. $res = Database::query($sql);
  2324. $num = Database :: num_rows($res);
  2325. if ($num > 0) {
  2326. $list[] = array (
  2327. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2328. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2329. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2330. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2331. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2332. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2333. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2334. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2335. );
  2336. while ($row = Database :: fetch_array($res)) {
  2337. $list[] = array (
  2338. 'order_id' => ($row['order_id'] + 1),
  2339. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2340. 'type' => $row['interaction_type'],
  2341. 'time' => $row['completion_time'],
  2342. //'correct_responses' => $row['correct_responses'],
  2343. 'correct_responses' => '', // Hide correct responses from students.
  2344. 'student_response' => $row['student_response'],
  2345. 'result' => $row['result'],
  2346. 'latency' => $row['latency']
  2347. );
  2348. }
  2349. }
  2350. return $list;
  2351. }
  2352. /**
  2353. * Return the number of objectives for the given learnpath Item View ID.
  2354. * This method can be used as static.
  2355. * @param integer Item View ID
  2356. * @return integer Number of objectives
  2357. */
  2358. public static function get_objectives_count_from_db($lp_iv_id, $course_id) {
  2359. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2360. $course_id = intval($course_id);
  2361. $lp_iv_id = intval($lp_iv_id);
  2362. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2363. //@todo seems that this always returns 0
  2364. $res = Database::query($sql);
  2365. $res = 0;
  2366. if (Database::num_rows($res)) {
  2367. $row = Database :: fetch_array($res);
  2368. $num = $row[0];
  2369. }
  2370. return $num;
  2371. }
  2372. /**
  2373. * Return the objectives as an array for the given lp_iv_id.
  2374. * This method can be used as static.
  2375. * @param integer Learnpath Item View ID
  2376. * @return array
  2377. * @todo Translate labels
  2378. */
  2379. public function get_iv_objectives_array($lp_iv_id = 0) {
  2380. $course_id = api_get_course_int_id();
  2381. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2382. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2383. $res = Database::query($sql);
  2384. $num = Database :: num_rows($res);
  2385. $list = array();
  2386. if ($num > 0) {
  2387. $list[] = array (
  2388. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2389. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2390. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2391. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2392. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2393. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2394. );
  2395. while ($row = Database :: fetch_array($res)) {
  2396. $list[] = array (
  2397. 'order_id' => ($row['order_id'] + 1),
  2398. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2399. 'score_raw' => $row['score_raw'],
  2400. 'score_max' => $row['score_max'],
  2401. 'score_min' => $row['score_min'],
  2402. 'status' => $row['status']
  2403. );
  2404. }
  2405. }
  2406. return $list;
  2407. }
  2408. /**
  2409. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2410. * used by get_html_toc() to be ready to display
  2411. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2412. */
  2413. public function get_toc() {
  2414. if ($this->debug > 0) {
  2415. error_log('learnpath::get_toc()', 0);
  2416. }
  2417. $toc = array();
  2418. //echo "<pre>".print_r($this->items,true)."</pre>";
  2419. foreach ($this->ordered_items as $item_id) {
  2420. if ($this->debug > 2) {
  2421. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2422. }
  2423. // TODO: Change this link generation and use new function instead.
  2424. $toc[] = array (
  2425. 'id' => $item_id,
  2426. 'title' => $this->items[$item_id]->get_title(),
  2427. 'status' => $this->items[$item_id]->get_status(),
  2428. 'level' => $this->items[$item_id]->get_level(),
  2429. 'type' => $this->items[$item_id]->get_type(),
  2430. 'description' => $this->items[$item_id]->get_description(),
  2431. 'path' => $this->items[$item_id]->get_path(),
  2432. );
  2433. }
  2434. if ($this->debug > 2) {
  2435. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2436. }
  2437. return $toc;
  2438. }
  2439. /**
  2440. * Generate and return the table of contents for this learnpath. The JS
  2441. * table returned is used inside of scorm_api.php
  2442. * @return string A JS array vairiable construction
  2443. */
  2444. public function get_items_details_as_js($varname = 'olms.lms_item_types') {
  2445. if ($this->debug > 0) {
  2446. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2447. }
  2448. $toc = $varname.' = new Array();';
  2449. //echo "<pre>".print_r($this->items,true)."</pre>";
  2450. foreach ($this->ordered_items as $item_id) {
  2451. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2452. }
  2453. if ($this->debug > 2) {
  2454. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2455. }
  2456. return $toc;
  2457. }
  2458. /**
  2459. * Gets the learning path type
  2460. * @param boolean Return the name? If false, return the ID. Default is false.
  2461. * @return mixed Type ID or name, depending on the parameter
  2462. */
  2463. public function get_type($get_name = false) {
  2464. $res = false;
  2465. if ($this->debug > 0) {
  2466. error_log('New LP - In learnpath::get_type()', 0);
  2467. }
  2468. if (!empty ($this->type)) {
  2469. if ($get_name) {
  2470. // Get it from the lp_type table in main db.
  2471. } else {
  2472. $res = $this->type;
  2473. }
  2474. }
  2475. if ($this->debug > 2) {
  2476. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2477. }
  2478. return $res;
  2479. }
  2480. /**
  2481. * Gets the learning path type as static method
  2482. * @param boolean Return the name? If false, return the ID. Default is false.
  2483. * @return mixed Type ID or name, depending on the parameter
  2484. */
  2485. public static function get_type_static($lp_id = 0) {
  2486. $course_id = api_get_course_int_id();
  2487. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2488. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2489. $res = Database::query($sql);
  2490. if ($res === false) {
  2491. return null;
  2492. }
  2493. if (Database :: num_rows($res) <= 0) {
  2494. return null;
  2495. }
  2496. $row = Database :: fetch_array($res);
  2497. return $row['lp_type'];
  2498. }
  2499. /**
  2500. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2501. * This method can be used as abstract and is recursive
  2502. * @param integer Learnpath ID
  2503. * @param integer Parent ID of the items to look for
  2504. * @return mixed Ordered list of item IDs or false on error
  2505. */
  2506. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null) {
  2507. if (empty($course_id)) {
  2508. $course_id = api_get_course_int_id();
  2509. } else {
  2510. $course_id = intval($course_id);
  2511. }
  2512. $list = array();
  2513. if (empty ($lp)) {
  2514. return false;
  2515. }
  2516. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2517. $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";
  2518. $res = Database::query($sql);
  2519. while ($row = Database :: fetch_array($res)) {
  2520. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2521. $list[] = $row['id'];
  2522. foreach ($sublist as $item) {
  2523. $list[] = $item;
  2524. }
  2525. }
  2526. return $list;
  2527. }
  2528. /**
  2529. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2530. * @return string HTML TOC ready to display
  2531. */
  2532. public function get_html_toc($toc_list = null) {
  2533. global $_configuration;
  2534. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2535. if ($this->debug > 0) {
  2536. error_log('In learnpath::get_html_toc()', 0);
  2537. }
  2538. if (empty($toc_list)) {
  2539. $toc_list = $this->get_toc();
  2540. }
  2541. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()) . '</div>';
  2542. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2543. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2544. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2545. if ($this->get_lp_session_id() == api_get_session_id()) {
  2546. $html .= '<div id="actions_lp" class="actions_lp">';
  2547. $html .= '<div class="btn-group">';
  2548. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2549. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;action=add_item&amp;type=step&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2550. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2551. $html .= '</div>';
  2552. $html .= '</div>';
  2553. }
  2554. }
  2555. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2556. require_once 'resourcelinker.inc.php';
  2557. // Temporary variables.
  2558. $mycurrentitemid = $this->get_current_item_id();
  2559. $color_counter = 0;
  2560. $i = 0;
  2561. foreach ($toc_list as $item) {
  2562. // TODO: Complete this
  2563. $icon_name = array (
  2564. 'not attempted' => '../img/notattempted.gif',
  2565. 'incomplete' => '../img/incomplete.png',
  2566. 'failed' => '../img/delete.png',
  2567. 'completed' => '../img/completed.png',
  2568. 'passed' => '../img/passed.png',
  2569. 'succeeded' => '../img/succeeded.png',
  2570. 'browsed' => '../img/completed.png',
  2571. );
  2572. $style = 'scorm_item';
  2573. $scorm_color_background = 'scorm_item';
  2574. $style_item = 'scorm_item';
  2575. $current = false;
  2576. if ($item['id'] == $this->current) {
  2577. $style = 'scorm_item_highlight';
  2578. $scorm_color_background = 'scorm_item_highlight';
  2579. } else {
  2580. if ($color_counter % 2 == 0) {
  2581. $scorm_color_background = 'scorm_item_1';
  2582. } else {
  2583. $scorm_color_background = 'scorm_item_2';
  2584. }
  2585. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2586. $scorm_color_background =' scorm_item_section ';
  2587. }
  2588. }
  2589. if ($scorm_color_background != '') {
  2590. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . '">';
  2591. }
  2592. // The anchor will let us center the TOC on the currently viewed item &^D
  2593. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2594. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 1.5) . 'em; padding-right:' . ($item['level'] / 2) . 'em" title="' . $item['description'] . '" >';
  2595. $html .= '<a name="atoc_' . $item['id'] . '" />';
  2596. } else {
  2597. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 2) . 'em; padding-right:' . ($item['level'] * 1.5) . 'em" title="' . $item['description'] . '" >';
  2598. }
  2599. $title = $item['title'];
  2600. if (empty ($title)) {
  2601. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2602. }
  2603. $title = Security::remove_XSS($title);
  2604. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2605. //$html .= "<a href='lp_controller.php?".api_get_cidreq()."&action=content&lp_id=".$this->get_id()."&item_id=".$item['id']."' target='lp_content_frame_name'>".$title."</a>" ;
  2606. $url = $this->get_link('http', $item['id'], $toc_list);
  2607. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2608. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2609. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2610. $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2611. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2612. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;" . stripslashes($title);
  2613. } elseif ($item['type'] == 'dir') {
  2614. $html .= stripslashes($title);
  2615. }
  2616. /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2617. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2618. $user_id = api_get_user_id();
  2619. $sql = "SELECT path FROM $tbl_track_e_exercises, $tbl_lp_item
  2620. WHERE c_id = $course_id AND
  2621. path = '" . $item['path'] . "' AND
  2622. exe_user_id = '$user_id' AND
  2623. exe_cours_id = '$course_code' AND
  2624. path = exe_exo_id AND
  2625. status <> 'incomplete'";
  2626. $result = Database::query($sql);
  2627. $count = Database :: num_rows($result);*/
  2628. if ($item['type'] == 'quiz') {
  2629. if ($item['status'] == 'completed') {
  2630. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2631. } else {
  2632. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name['not attempted'] . "' alt='" . substr('not attempted', 0, 1) . "' width='14' />";
  2633. }
  2634. } else {
  2635. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2636. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2637. }
  2638. }
  2639. $html .= "</div>";
  2640. if ($scorm_color_background != '') {
  2641. $html .= '</div>';
  2642. }
  2643. $color_counter++;
  2644. }
  2645. $html .= "</div>";
  2646. return $html;
  2647. }
  2648. /**
  2649. * Gets the learnpath maker name - generally the editor's name
  2650. * @return string Learnpath maker name
  2651. */
  2652. public function get_maker() {
  2653. if ($this->debug > 0) {
  2654. error_log('New LP - In learnpath::get_maker()', 0);
  2655. }
  2656. if (!empty ($this->maker)) {
  2657. return $this->maker;
  2658. } else {
  2659. return '';
  2660. }
  2661. }
  2662. /**
  2663. * Gets the user-friendly message stored in $this->message
  2664. * @return string Message
  2665. */
  2666. public function get_message() {
  2667. if ($this->debug > 0) {
  2668. error_log('New LP - In learnpath::get_message()', 0);
  2669. }
  2670. return $this->message;
  2671. }
  2672. /**
  2673. * Gets the learnpath name/title
  2674. * @return string Learnpath name/title
  2675. */
  2676. public function get_name() {
  2677. if ($this->debug > 0) {
  2678. error_log('New LP - In learnpath::get_name()', 0);
  2679. }
  2680. if (!empty ($this->name)) {
  2681. return $this->name;
  2682. } else {
  2683. return 'N/A';
  2684. }
  2685. }
  2686. /**
  2687. * Gets a link to the resource from the present location, depending on item ID.
  2688. * @param string Type of link expected
  2689. * @param integer Learnpath item ID
  2690. * @return string Link to the lp_item resource
  2691. */
  2692. public function get_link($type = 'http', $item_id = null, $provided_toc = false) {
  2693. $course_id = $this->get_course_int_id();
  2694. if ($this->debug > 0) {
  2695. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  2696. }
  2697. if (empty($item_id)) {
  2698. if ($this->debug > 2) {
  2699. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  2700. }
  2701. $item_id = $this->get_current_item_id();
  2702. }
  2703. if (empty($item_id)) {
  2704. if ($this->debug > 2) {
  2705. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  2706. }
  2707. //still empty, this means there was no item_id given and we are not in an object context or
  2708. //the object property is empty, return empty link
  2709. $item_id = $this->first();
  2710. return '';
  2711. }
  2712. $file = '';
  2713. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  2714. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  2715. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2716. $item_id = Database::escape_string($item_id);
  2717. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  2718. FROM $lp_table l
  2719. INNER JOIN $lp_item_table li
  2720. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  2721. WHERE li.id = $item_id ";
  2722. if ($this->debug > 2) {
  2723. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  2724. }
  2725. $res = Database::query($sql);
  2726. if (Database :: num_rows($res) > 0) {
  2727. $row = Database :: fetch_array($res);
  2728. $lp_type = $row['ltype'];
  2729. $lp_path = $row['lpath'];
  2730. $lp_item_type = $row['litype'];
  2731. $lp_item_path = $row['lipath'];
  2732. $lp_item_params = $row['liparams'];
  2733. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  2734. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  2735. }
  2736. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  2737. if ($type == 'http') {
  2738. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  2739. } else {
  2740. $course_path = $sys_course_path; //system path
  2741. }
  2742. // 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.
  2743. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  2744. $lp_type = 1;
  2745. }
  2746. if ($this->debug > 2) {
  2747. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  2748. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  2749. }
  2750. // Now go through the specific cases to get the end of the path
  2751. // @todo Use constants instead of int values.
  2752. switch ($lp_type) {
  2753. case 1 :
  2754. if ($lp_item_type == 'dokeos_chapter') {
  2755. $file = 'lp_content.php?type=dir';
  2756. } else {
  2757. require_once 'resourcelinker.inc.php';
  2758. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  2759. if ($this->debug > 0) {
  2760. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  2761. }
  2762. if ($lp_item_type == 'link') {
  2763. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  2764. if (is_youtube_link($file)) {
  2765. $src = get_youtube_video_id($file);
  2766. $file = 'embed.php?type=youtube&src='.$src;
  2767. }
  2768. } else {
  2769. // check how much attempts of a exercise exits in lp
  2770. $lp_item_id = $this->get_current_item_id();
  2771. $lp_view_id = $this->get_view_id();
  2772. $prevent_reinit = null;
  2773. if (isset($this->items[$this->current])) {
  2774. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  2775. }
  2776. if (empty($provided_toc)) {
  2777. if ($this->debug > 0) {
  2778. error_log('In learnpath::get_link() Loading get_toc ', 0);
  2779. }
  2780. $list = $this->get_toc();
  2781. } else {
  2782. if ($this->debug > 0) {
  2783. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  2784. }
  2785. $list = $provided_toc;
  2786. }
  2787. $type_quiz = false;
  2788. foreach ($list as $toc) {
  2789. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  2790. $type_quiz = true;
  2791. }
  2792. }
  2793. if ($type_quiz) {
  2794. $lp_item_id = Database::escape_string($lp_item_id);
  2795. $lp_view_id = Database::escape_string($lp_view_id);
  2796. $sql = "SELECT count(*) FROM $lp_item_view_table
  2797. WHERE c_id = $course_id AND lp_item_id='" . (int) $lp_item_id . "' AND lp_view_id ='" . (int) $lp_view_id . "' AND status='completed'";
  2798. $result = Database::query($sql);
  2799. $row_count = Database :: fetch_row($result);
  2800. $count_item_view = (int) $row_count[0];
  2801. $not_multiple_attempt = 0;
  2802. if ($prevent_reinit === 1 && $count_item_view > 0) {
  2803. $not_multiple_attempt = 1;
  2804. }
  2805. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  2806. }
  2807. $tmp_array = explode('/', $file);
  2808. $document_name = $tmp_array[count($tmp_array) - 1];
  2809. if (strpos($document_name, '_DELETED_')) {
  2810. $file = 'blank.php?error=document_deleted';
  2811. }
  2812. }
  2813. }
  2814. break;
  2815. case 2 :
  2816. if ($this->debug > 2) {
  2817. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2818. }
  2819. if ($lp_item_type != 'dir') {
  2820. // Quite complex here:
  2821. // We want to make sure 'http://' (and similar) links can
  2822. // be loaded as is (withouth the Chamilo path in front) but
  2823. // some contents use this form: resource.htm?resource=http://blablabla
  2824. // which means we have to find a protocol at the path's start, otherwise
  2825. // it should not be considered as an external URL.
  2826. //if ($this->prerequisites_match($item_id)) {
  2827. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2828. if ($this->debug > 2) {
  2829. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2830. }
  2831. // Distant url, return as is.
  2832. $file = $lp_item_path;
  2833. } else {
  2834. if ($this->debug > 2) {
  2835. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2836. }
  2837. // Prevent getting untranslatable urls.
  2838. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2839. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2840. // Prepare the path.
  2841. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2842. // TODO: Fix this for urls with protocol header.
  2843. $file = str_replace('//', '/', $file);
  2844. $file = str_replace(':/', '://', $file);
  2845. if (substr($lp_path, -1) == '/') {
  2846. $lp_path = substr($lp_path, 0, -1);
  2847. }
  2848. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  2849. // if file not found.
  2850. $decoded = html_entity_decode($lp_item_path);
  2851. list ($decoded) = explode('?', $decoded);
  2852. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  2853. require_once 'resourcelinker.inc.php';
  2854. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  2855. if (empty($file)) {
  2856. $file = 'blank.php?error=document_not_found';
  2857. } else {
  2858. $tmp_array = explode('/', $file);
  2859. $document_name = $tmp_array[count($tmp_array) - 1];
  2860. if (strpos($document_name, '_DELETED_')) {
  2861. $file = 'blank.php?error=document_deleted';
  2862. } else {
  2863. $file = 'blank.php?error=document_not_found';
  2864. }
  2865. }
  2866. } else {
  2867. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  2868. }
  2869. }
  2870. }
  2871. //}else{
  2872. //prerequisites did not match
  2873. //$file = 'blank.php';
  2874. //}
  2875. // We want to use parameters if they were defined in the imsmanifest
  2876. if (strpos($file, 'blank.php') === false) {
  2877. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  2878. }
  2879. } else {
  2880. $file = 'lp_content.php?type=dir';
  2881. }
  2882. break;
  2883. case 3 :
  2884. if ($this->debug > 2) {
  2885. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2886. }
  2887. // Formatting AICC HACP append URL.
  2888. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  2889. if ($lp_item_type != 'dir') {
  2890. // Quite complex here:
  2891. // We want to make sure 'http://' (and similar) links can
  2892. // be loaded as is (withouth the Chamilo path in front) but
  2893. // some contents use this form: resource.htm?resource=http://blablabla
  2894. // which means we have to find a protocol at the path's start, otherwise
  2895. // it should not be considered as an external URL.
  2896. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2897. if ($this->debug > 2) {
  2898. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2899. }
  2900. // Distant url, return as is.
  2901. $file = $lp_item_path;
  2902. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  2903. /*
  2904. if (stristr($file,'<servername>') !== false) {
  2905. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  2906. }
  2907. */
  2908. if (stripos($file, '<servername>') !== false) {
  2909. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  2910. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  2911. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  2912. }
  2913. //
  2914. $file .= $aicc_append;
  2915. } else {
  2916. if ($this->debug > 2) {
  2917. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2918. }
  2919. // Prevent getting untranslatable urls.
  2920. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2921. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2922. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  2923. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2924. // TODO: Fix this for urls with protocol header.
  2925. $file = str_replace('//', '/', $file);
  2926. $file = str_replace(':/', '://', $file);
  2927. $file .= $aicc_append;
  2928. }
  2929. } else {
  2930. $file = 'lp_content.php?type=dir';
  2931. }
  2932. break;
  2933. case 4 :
  2934. break;
  2935. default :
  2936. break;
  2937. }
  2938. }
  2939. if ($this->debug > 2) {
  2940. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  2941. }
  2942. return $file;
  2943. }
  2944. /**
  2945. * Gets the latest usable view or generate a new one
  2946. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  2947. * @return integer DB lp_view id
  2948. */
  2949. public function get_view($attempt_num = 0) {
  2950. if ($this->debug > 0) {
  2951. error_log('New LP - In learnpath::get_view()', 0);
  2952. }
  2953. $search = '';
  2954. // Use $attempt_num to enable multi-views management (disabled so far).
  2955. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  2956. $search = 'AND view_count = ' . $attempt_num;
  2957. }
  2958. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  2959. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  2960. $course_id = api_get_course_int_id();
  2961. $sql = "SELECT id, view_count FROM $lp_view_table
  2962. WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() ." AND user_id = " . $this->get_user_id() . " " .$search .
  2963. " ORDER BY view_count DESC";
  2964. $res = Database::query($sql);
  2965. if (Database :: num_rows($res) > 0) {
  2966. $row = Database :: fetch_array($res);
  2967. $this->lp_view_id = $row['id'];
  2968. } else {
  2969. // There is no database record, create one.
  2970. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  2971. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ",1)";
  2972. $res = Database::query($sql);
  2973. $id = Database :: insert_id();
  2974. $this->lp_view_id = $id;
  2975. }
  2976. return $this->lp_view_id;
  2977. }
  2978. /**
  2979. * Gets the current view id
  2980. * @return integer View ID (from lp_view)
  2981. */
  2982. public function get_view_id() {
  2983. if ($this->debug > 0) {
  2984. error_log('New LP - In learnpath::get_view_id()', 0);
  2985. }
  2986. if (!empty ($this->lp_view_id)) {
  2987. return $this->lp_view_id;
  2988. } else {
  2989. return 0;
  2990. }
  2991. }
  2992. /**
  2993. * Gets the update queue
  2994. * @return array Array containing IDs of items to be updated by JavaScript
  2995. */
  2996. public function get_update_queue() {
  2997. if ($this->debug > 0) {
  2998. error_log('New LP - In learnpath::get_update_queue()', 0);
  2999. }
  3000. return $this->update_queue;
  3001. }
  3002. /**
  3003. * Gets the user ID
  3004. * @return integer User ID
  3005. */
  3006. public function get_user_id() {
  3007. if ($this->debug > 0) {
  3008. error_log('New LP - In learnpath::get_user_id()', 0);
  3009. }
  3010. if (!empty ($this->user_id)) {
  3011. return $this->user_id;
  3012. } else {
  3013. return false;
  3014. }
  3015. }
  3016. /**
  3017. * Checks if any of the items has an audio element attached
  3018. * @return bool True or false
  3019. */
  3020. public function has_audio() {
  3021. if ($this->debug > 1) {
  3022. error_log('New LP - In learnpath::has_audio()', 0);
  3023. }
  3024. $has = false;
  3025. foreach ($this->items as $i => $item) {
  3026. if (!empty ($this->items[$i]->audio)) {
  3027. $has = true;
  3028. break;
  3029. }
  3030. }
  3031. return $has;
  3032. }
  3033. /**
  3034. * Logs a message into a file
  3035. * @param string Message to log
  3036. * @return boolean True on success, false on error or if msg empty
  3037. */
  3038. public function log($msg) {
  3039. if ($this->debug > 0) {
  3040. error_log('New LP - In learnpath::log()', 0);
  3041. }
  3042. // TODO
  3043. $this->error .= $msg;
  3044. return true;
  3045. }
  3046. /**
  3047. * Moves an item up and down at its level
  3048. * @param integer Item to move up and down
  3049. * @param string Direction 'up' or 'down'
  3050. * @return integer New display order, or false on error
  3051. */
  3052. public function move_item($id, $direction) {
  3053. $course_id = api_get_course_int_id();
  3054. if ($this->debug > 0) {
  3055. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3056. }
  3057. if (empty ($id) or empty ($direction)) {
  3058. return false;
  3059. }
  3060. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3061. $sql_sel = "SELECT *
  3062. FROM " . $tbl_lp_item . "
  3063. WHERE c_id = ".$course_id." AND id = " . $id;
  3064. $res_sel = Database::query($sql_sel);
  3065. // Check if elem exists.
  3066. if (Database :: num_rows($res_sel) < 1) {
  3067. return false;
  3068. }
  3069. // Gather data.
  3070. $row = Database :: fetch_array($res_sel);
  3071. $previous = $row['previous_item_id'];
  3072. $next = $row['next_item_id'];
  3073. $display = $row['display_order'];
  3074. $parent = $row['parent_item_id'];
  3075. $lp = $row['lp_id'];
  3076. // Update the item (switch with previous/next one).
  3077. switch ($direction) {
  3078. case 'up' :
  3079. if ($this->debug > 2) {
  3080. error_log('Movement up detected', 0);
  3081. }
  3082. if ($display <= 1) { /*do nothing*/
  3083. } else {
  3084. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3085. WHERE c_id = ".$course_id." AND id = $previous";
  3086. if ($this->debug > 2) {
  3087. error_log('Selecting previous: ' . $sql_sel2, 0);
  3088. }
  3089. $res_sel2 = Database::query($sql_sel2);
  3090. if (Database :: num_rows($res_sel2) < 1) {
  3091. $previous_previous = 0;
  3092. }
  3093. // Gather data.
  3094. $row2 = Database :: fetch_array($res_sel2);
  3095. $previous_previous = $row2['previous_item_id'];
  3096. // Update previous_previous item (switch "next" with current).
  3097. if ($previous_previous != 0) {
  3098. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3099. if ($this->debug > 2) {
  3100. error_log($sql_upd2, 0);
  3101. }
  3102. $res_upd2 = Database::query($sql_upd2);
  3103. }
  3104. // Update previous item (switch with current).
  3105. if ($previous != 0) {
  3106. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3107. WHERE c_id = ".$course_id." AND id = $previous";
  3108. if ($this->debug > 2) {
  3109. error_log($sql_upd2, 0);
  3110. }
  3111. $res_upd2 = Database::query($sql_upd2);
  3112. }
  3113. // Update current item (switch with previous).
  3114. if ($id != 0) {
  3115. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3116. WHERE c_id = ".$course_id." AND id = $id";
  3117. if ($this->debug > 2) {
  3118. error_log($sql_upd2, 0);
  3119. }
  3120. $res_upd2 = Database::query($sql_upd2);
  3121. }
  3122. // Update next item (new previous item).
  3123. if ($next != 0) {
  3124. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3125. WHERE c_id = ".$course_id." AND id = $next";
  3126. if ($this->debug > 2) {
  3127. error_log($sql_upd2, 0);
  3128. }
  3129. $res_upd2 = Database::query($sql_upd2);
  3130. }
  3131. $display = $display -1;
  3132. }
  3133. break;
  3134. case 'down' :
  3135. if ($this->debug > 2) {
  3136. error_log('Movement down detected', 0);
  3137. }
  3138. if ($next == 0) { /* Do nothing. */
  3139. } else {
  3140. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3141. if ($this->debug > 2) {
  3142. error_log('Selecting next: ' . $sql_sel2, 0);
  3143. }
  3144. $res_sel2 = Database::query($sql_sel2);
  3145. if (Database :: num_rows($res_sel2) < 1) {
  3146. $next_next = 0;
  3147. }
  3148. // Gather data.
  3149. $row2 = Database :: fetch_array($res_sel2);
  3150. $next_next = $row2['next_item_id'];
  3151. // Update previous item (switch with current).
  3152. if ($previous != 0) {
  3153. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3154. WHERE c_id = ".$course_id." AND id = $previous";
  3155. $res_upd2 = Database::query($sql_upd2);
  3156. }
  3157. // Update current item (switch with previous).
  3158. if ($id != 0) {
  3159. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3160. WHERE c_id = ".$course_id." AND id = $id";
  3161. $res_upd2 = Database::query($sql_upd2);
  3162. }
  3163. // Update next item (new previous item).
  3164. if ($next != 0) {
  3165. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3166. WHERE c_id = ".$course_id." AND id = $next";
  3167. $res_upd2 = Database::query($sql_upd2);
  3168. }
  3169. // Update next_next item (switch "previous" with current).
  3170. if ($next_next != 0) {
  3171. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3172. WHERE c_id = ".$course_id." AND id = $next_next";
  3173. $res_upd2 = Database::query($sql_upd2);
  3174. }
  3175. $display = $display +1;
  3176. }
  3177. break;
  3178. default :
  3179. return false;
  3180. }
  3181. return $display;
  3182. }
  3183. /**
  3184. * Move a learnpath up (display_order)
  3185. * @param integer Learnpath ID
  3186. */
  3187. public function move_up($lp_id) {
  3188. $course_id = api_get_course_int_id();
  3189. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3190. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3191. $res = Database::query($sql);
  3192. if ($res === false)
  3193. return false;
  3194. $lps = array ();
  3195. $lp_order = array ();
  3196. $num = Database :: num_rows($res);
  3197. // First check the order is correct, globally (might be wrong because
  3198. // of versions < 1.8.4)
  3199. if ($num > 0) {
  3200. $i = 1;
  3201. while ($row = Database :: fetch_array($res)) {
  3202. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3203. $need_fix = true;
  3204. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3205. $res_u = Database::query($sql_u);
  3206. }
  3207. $row['display_order'] = $i;
  3208. $lps[$row['id']] = $row;
  3209. $lp_order[$i] = $row['id'];
  3210. $i++;
  3211. }
  3212. }
  3213. if ($num > 1) { // If there's only one element, no need to sort.
  3214. $order = $lps[$lp_id]['display_order'];
  3215. if ($order > 1) { // If it's the first element, no need to move up.
  3216. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3217. $res_u1 = Database::query($sql_u1);
  3218. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . " WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3219. $res_u2 = Database::query($sql_u2);
  3220. }
  3221. }
  3222. }
  3223. /**
  3224. * Move a learnpath down (display_order)
  3225. * @param integer Learnpath ID
  3226. */
  3227. public function move_down($lp_id) {
  3228. $course_id = api_get_course_int_id();
  3229. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3230. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3231. $res = Database::query($sql);
  3232. if ($res === false)
  3233. return false;
  3234. $lps = array ();
  3235. $lp_order = array ();
  3236. $num = Database :: num_rows($res);
  3237. $max = 0;
  3238. // First check the order is correct, globally (might be wrong because
  3239. // of versions < 1.8.4).
  3240. if ($num > 0) {
  3241. $i = 1;
  3242. while ($row = Database :: fetch_array($res)) {
  3243. $max = $i;
  3244. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3245. $need_fix = true;
  3246. $sql_u = "UPDATE $lp_table SET display_order = $i
  3247. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3248. $res_u = Database::query($sql_u);
  3249. }
  3250. $row['display_order'] = $i;
  3251. $lps[$row['id']] = $row;
  3252. $lp_order[$i] = $row['id'];
  3253. $i++;
  3254. }
  3255. }
  3256. if ($num > 1) { // If there's only one element, no need to sort.
  3257. $order = $lps[$lp_id]['display_order'];
  3258. if ($order < $max) { // If it's the first element, no need to move up.
  3259. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3260. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3261. $res_u1 = Database::query($sql_u1);
  3262. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3263. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3264. $res_u2 = Database::query($sql_u2);
  3265. }
  3266. }
  3267. }
  3268. /**
  3269. * Updates learnpath attributes to point to the next element
  3270. * The last part is similar to set_current_item but processing the other way around
  3271. */
  3272. public function next() {
  3273. if ($this->debug > 0) {
  3274. error_log('New LP - In learnpath::next()', 0);
  3275. }
  3276. $this->last = $this->get_current_item_id();
  3277. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3278. $this->autocomplete_parents($this->last);
  3279. $new_index = $this->get_next_index();
  3280. if ($this->debug > 2) {
  3281. error_log('New LP - New index: ' . $new_index, 0);
  3282. }
  3283. $this->index = $new_index;
  3284. if ($this->debug > 2) {
  3285. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3286. }
  3287. $this->current = $this->ordered_items[$new_index];
  3288. if ($this->debug > 2) {
  3289. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3290. }
  3291. }
  3292. /**
  3293. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3294. * class, this might be redefined to allow several behaviours depending on the document type.
  3295. * @param integer Resource ID
  3296. * @return boolean True on success, false otherwise
  3297. */
  3298. public function open($id) {
  3299. if ($this->debug > 0) {
  3300. error_log('New LP - In learnpath::open()', 0);
  3301. }
  3302. // TODO:
  3303. // set the current resource attribute to this resource
  3304. // switch on element type (redefine in child class?)
  3305. // set status for this item to "opened"
  3306. // start timer
  3307. // initialise score
  3308. $this->index = 0; //or = the last item seen (see $this->last)
  3309. }
  3310. /**
  3311. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3312. * and the prerequisite string on error.
  3313. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3314. * @param integer Optional item ID. If none given, uses the current open item.
  3315. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3316. */
  3317. public function prerequisites_match($item = null) {
  3318. if ($this->debug > 0) {
  3319. error_log('In learnpath::prerequisites_match()', 0);
  3320. }
  3321. if (empty($item)) {
  3322. $item = $this->current;
  3323. }
  3324. if (is_object($this->items[$item])) {
  3325. if ($this->type == 2) {
  3326. //Getting prereq from scorm
  3327. $prereq_string = $this->get_scorm_prereq_string($item);
  3328. } else {
  3329. $prereq_string = $this->items[$item]->get_prereq_string();
  3330. }
  3331. if (empty($prereq_string)) {
  3332. return true;
  3333. }
  3334. // Clean spaces.
  3335. $prereq_string = str_replace(' ', '', $prereq_string);
  3336. if ($this->debug > 0) {
  3337. error_log('Found prereq_string: ' . $prereq_string, 0);
  3338. }
  3339. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3340. $result = $this->items[$item]->parse_prereq($prereq_string, $this->items, $this->refs_list, $this->get_user_id());
  3341. if ($result === false) {
  3342. $this->set_error_msg($this->items[$item]->prereq_alert);
  3343. }
  3344. } else {
  3345. $result = true;
  3346. if ($this->debug > 1) {
  3347. error_log('$this->items[' . $item . '] was not an object', 0);
  3348. }
  3349. }
  3350. if ($this->debug > 1) {
  3351. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3352. }
  3353. return $result;
  3354. }
  3355. /**
  3356. * Updates learnpath attributes to point to the previous element
  3357. * The last part is similar to set_current_item but processing the other way around
  3358. */
  3359. public function previous() {
  3360. if ($this->debug > 0) {
  3361. error_log('New LP - In learnpath::previous()', 0);
  3362. }
  3363. $this->last = $this->get_current_item_id();
  3364. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3365. $this->autocomplete_parents($this->last);
  3366. $new_index = $this->get_previous_index();
  3367. $this->index = $new_index;
  3368. $this->current = $this->ordered_items[$new_index];
  3369. }
  3370. /**
  3371. * Publishes a learnpath. This basically means show or hide the learnpath
  3372. * to normal users.
  3373. * Can be used as abstract
  3374. * @param integer Learnpath ID
  3375. * @param string New visibility
  3376. */
  3377. public function toggle_visibility($lp_id, $set_visibility = 1) {
  3378. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3379. $action = 'visible';
  3380. if ($set_visibility != 1) {
  3381. $action = 'invisible';
  3382. }
  3383. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3384. }
  3385. /**
  3386. * Publishes a learnpath. This basically means show or hide the learnpath
  3387. * on the course homepage
  3388. * Can be used as abstract
  3389. * @param integer Learnpath id
  3390. * @param string New visibility (v/s - visible/invisible)
  3391. */
  3392. public static function toggle_publish($lp_id, $set_visibility = 'v') {
  3393. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_publish()', 0); }
  3394. $course_id = api_get_course_int_id();
  3395. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3396. $lp_id = Database::escape_string($lp_id);
  3397. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3398. $result = Database::query($sql);
  3399. if (Database::num_rows($result)) {
  3400. $row = Database :: fetch_array($result);
  3401. $name = domesticate($row['name']);
  3402. if ($set_visibility == 'i') {
  3403. $s = $name . " " . get_lang('LearnpathNotPublished');
  3404. $dialogBox = $s;
  3405. $v = 0;
  3406. }
  3407. if ($set_visibility == 'v') {
  3408. $s = $name . " " . get_lang('LearnpathPublished');
  3409. $dialogBox = $s;
  3410. $v = 1;
  3411. }
  3412. } else {
  3413. return false;
  3414. }
  3415. $session_id = api_get_session_id();
  3416. $session_condition = api_get_session_condition($session_id);
  3417. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3418. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  3419. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3420. $result = Database::query($sql);
  3421. $num = Database :: num_rows($result);
  3422. //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
  3423. if (($set_visibility == 'i') && ($num > 0)) {
  3424. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3425. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3426. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3427. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3428. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3429. $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
  3430. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3431. } else {
  3432. // Parameter and database incompatible, do nothing, exit.
  3433. return false;
  3434. }
  3435. $result = Database::query($sql);
  3436. //if ($this->debug > 2) { error_log('New LP - Leaving learnpath::toggle_visibility: '.$sql, 0); }
  3437. }
  3438. /**
  3439. * Restart the whole learnpath. Return the URL of the first element.
  3440. * Make sure the results are saved with anoter method. This method should probably be
  3441. * redefined in children classes.
  3442. * To use a similar method statically, use the create_new_attempt() method
  3443. * @return string URL to load in the viewer
  3444. */
  3445. public function restart() {
  3446. if ($this->debug > 0) {
  3447. error_log('New LP - In learnpath::restart()', 0);
  3448. }
  3449. // TODO
  3450. // Call autosave method to save the current progress.
  3451. //$this->index = 0;
  3452. $session_id = api_get_session_id();
  3453. $course_id = api_get_course_int_id();
  3454. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3455. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
  3456. "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3457. if ($this->debug > 2) {
  3458. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3459. }
  3460. $res = Database::query($sql);
  3461. if ($view_id = Database :: insert_id($res)) {
  3462. $this->lp_view_id = $view_id;
  3463. $this->attempt = $this->attempt + 1;
  3464. } else {
  3465. $this->error = 'Could not insert into item_view table...';
  3466. return false;
  3467. }
  3468. $this->autocomplete_parents($this->current);
  3469. foreach ($this->items as $index => $dummy) {
  3470. $this->items[$index]->restart();
  3471. $this->items[$index]->set_lp_view($this->lp_view_id);
  3472. }
  3473. $this->first();
  3474. return true;
  3475. }
  3476. /**
  3477. * Saves the current item
  3478. * @return boolean
  3479. */
  3480. public function save_current() {
  3481. if ($this->debug > 0) {
  3482. error_log('learnpath::save_current()', 0);
  3483. }
  3484. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3485. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3486. if ($this->debug > 2) {
  3487. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3488. }
  3489. if ($this->debug > 2) {
  3490. error_log('' . print_r($this->items, true), 0);
  3491. }
  3492. if (is_object($this->items[$this->current])) {
  3493. //$res = $this->items[$this->current]->save(false);
  3494. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3495. $this->autocomplete_parents($this->current);
  3496. $status = $this->items[$this->current]->get_status();
  3497. $this->append_message('new_item_status: ' . $status);
  3498. $this->update_queue[$this->current] = $status;
  3499. return $res;
  3500. }
  3501. return false;
  3502. }
  3503. /**
  3504. * Saves the given item
  3505. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3506. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3507. * @return boolean
  3508. */
  3509. public function save_item($item_id = null, $from_outside = true) {
  3510. $debug = $this->debug;
  3511. if ($debug) {
  3512. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3513. }
  3514. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3515. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3516. if (empty($item_id)) {
  3517. $item_id = intval($_REQUEST['id']);
  3518. }
  3519. if (empty($item_id)) {
  3520. $item_id = $this->get_current_item_id();
  3521. }
  3522. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3523. if ($debug) { error_log('Object exists'); }
  3524. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3525. if ($debug) {
  3526. error_log('update_queue before:');
  3527. error_log(print_r($this->update_queue,1));
  3528. }
  3529. $this->autocomplete_parents($item_id);
  3530. $status = $this->items[$item_id]->get_status();
  3531. $this->update_queue[$item_id] = $status;
  3532. if ($debug) {
  3533. error_log('get_status(): ' . $status);
  3534. error_log('update_queue after:');
  3535. error_log(print_r($this->update_queue,1));
  3536. }
  3537. return $res;
  3538. }
  3539. return false;
  3540. }
  3541. /**
  3542. * Saves the last item seen's ID only in case
  3543. */
  3544. public function save_last() {
  3545. $course_id = api_get_course_int_id();
  3546. if ($this->debug > 0) {
  3547. error_log('New LP - In learnpath::save_last()', 0);
  3548. }
  3549. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3550. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3551. if (isset($this->current)) {
  3552. if ($this->debug > 2) {
  3553. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3554. }
  3555. $sql = "UPDATE $table SET last_item = " . Database::escape_string($this->get_current_item_id()). "
  3556. WHERE c_id = $course_id AND
  3557. lp_id = " . $this->get_id() . " AND
  3558. user_id = " . $this->get_user_id()." ".$session_condition;
  3559. if ($this->debug > 2) {
  3560. error_log('New LP - Saving last item seen : ' . $sql, 0);
  3561. }
  3562. $res = Database::query($sql);
  3563. }
  3564. // Save progress.
  3565. list($progress, $text) = $this->get_progress_bar_text('%');
  3566. if ($progress >= 0 && $progress <= 100) {
  3567. $progress = (int) $progress;
  3568. $sql = "UPDATE $table SET progress = $progress
  3569. WHERE c_id = ".$course_id." AND
  3570. lp_id = " . $this->get_id() . " AND
  3571. user_id = " . $this->get_user_id()." ".$session_condition;
  3572. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3573. $this->progress_db = $progress;
  3574. }
  3575. }
  3576. /**
  3577. * Sets the current item ID (checks if valid and authorized first)
  3578. * @param integer New item ID. If not given or not authorized, defaults to current
  3579. */
  3580. public function set_current_item($item_id = null) {
  3581. if ($this->debug > 0) {
  3582. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  3583. }
  3584. if (empty ($item_id)) {
  3585. if ($this->debug > 2) {
  3586. error_log('New LP - No new current item given, ignore...', 0);
  3587. }
  3588. // Do nothing.
  3589. } else {
  3590. if ($this->debug > 2) {
  3591. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  3592. }
  3593. if (is_numeric($item_id)) {
  3594. $item_id = Database::escape_string($item_id);
  3595. // TODO: Check in database here.
  3596. $this->last = $this->current;
  3597. $this->current = $item_id;
  3598. // TODO: Update $this->index as well.
  3599. foreach ($this->ordered_items as $index => $item) {
  3600. if ($item == $this->current) {
  3601. $this->index = $index;
  3602. break;
  3603. }
  3604. }
  3605. if ($this->debug > 2) {
  3606. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  3607. }
  3608. } else {
  3609. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  3610. }
  3611. }
  3612. }
  3613. /**
  3614. * Sets the encoding
  3615. * @param string New encoding
  3616. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3617. */
  3618. public function set_encoding($enc = 'UTF-8') {
  3619. if ($this->debug > 0) {
  3620. error_log('New LP - In learnpath::set_encoding()', 0);
  3621. }
  3622. $course_id = api_get_course_int_id();
  3623. /* // Deprecated code (Chamilo 1.8.8).
  3624. $enc = strtoupper($enc);
  3625. $encodings = array (
  3626. 'UTF-8',
  3627. 'ISO-8859-1',
  3628. 'ISO-8859-15',
  3629. 'cp1251',
  3630. 'cp1252',
  3631. 'KOI8-R',
  3632. 'BIG5',
  3633. 'GB2312',
  3634. 'Shift_JIS',
  3635. 'EUC-JP',
  3636. ''
  3637. );
  3638. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  3639. $lp = $this->get_id();
  3640. if ($lp != 0) {
  3641. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3642. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  3643. $res = Database::query($sql);
  3644. return $res;
  3645. }
  3646. }
  3647. return false;
  3648. */
  3649. $enc = api_refine_encoding_id($enc);
  3650. if (empty($enc)) {
  3651. $enc = api_get_system_encoding();
  3652. }
  3653. if (api_is_encoding_supported($enc)) {
  3654. $lp = $this->get_id();
  3655. if ($lp != 0) {
  3656. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3657. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  3658. $res = Database::query($sql);
  3659. return $res;
  3660. }
  3661. }
  3662. return false;
  3663. }
  3664. /**
  3665. * Sets the JS lib setting in the database directly.
  3666. * This is the JavaScript library file this lp needs to load on startup
  3667. * @param string Proximity setting
  3668. * @return boolean True on update success. False otherwise.
  3669. */
  3670. public function set_jslib($lib = '') {
  3671. if ($this->debug > 0) {
  3672. error_log('New LP - In learnpath::set_jslib()', 0);
  3673. }
  3674. $lp = $this->get_id();
  3675. $course_id = api_get_course_int_id();
  3676. if ($lp != 0) {
  3677. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3678. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  3679. $res = Database::query($sql);
  3680. return $res;
  3681. } else {
  3682. return false;
  3683. }
  3684. }
  3685. /**
  3686. * Sets the name of the LP maker (publisher) (and save)
  3687. * @param string Optional string giving the new content_maker of this learnpath
  3688. * @return boolean True
  3689. */
  3690. public function set_maker($name = '') {
  3691. if ($this->debug > 0) {
  3692. error_log('New LP - In learnpath::set_maker()', 0);
  3693. }
  3694. if (empty ($name))
  3695. return false;
  3696. $this->maker = Database::escape_string($name);
  3697. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3698. $course_id = api_get_course_int_id();
  3699. $lp_id = $this->get_id();
  3700. $sql = "UPDATE $lp_table SET content_maker = '" . $this->maker . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3701. if ($this->debug > 2) {
  3702. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  3703. }
  3704. Database::query($sql);
  3705. return true;
  3706. }
  3707. /**
  3708. * Sets the name of the current learnpath (and save)
  3709. * @param string Optional string giving the new name of this learnpath
  3710. * @return boolean True/False
  3711. */
  3712. public function set_name($name = null) {
  3713. if ($this->debug > 0) {
  3714. error_log('New LP - In learnpath::set_name()', 0);
  3715. }
  3716. if (empty($name)) {
  3717. return false;
  3718. }
  3719. $this->name = Database::escape_string($name);
  3720. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3721. $lp_id = $this->get_id();
  3722. $course_id = api_get_course_int_id();
  3723. $sql = "UPDATE $lp_table SET name = '" . $this->name . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3724. if ($this->debug > 2) {
  3725. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  3726. }
  3727. Database::query($sql);
  3728. // If the lp is visible on the homepage, change his name there.
  3729. if (Database::affected_rows()) {
  3730. $session_id = api_get_session_id();
  3731. $session_condition = api_get_session_condition($session_id);
  3732. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3733. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  3734. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  3735. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3736. Database::query($sql);
  3737. return true;
  3738. } else {
  3739. return false;
  3740. }
  3741. }
  3742. /**
  3743. * Set index specified prefix terms for all items in this path
  3744. * @param string Comma-separated list of terms
  3745. * @param char Xapian term prefix
  3746. * @return boolean False on error, true otherwise
  3747. */
  3748. public function set_terms_by_prefix($terms_string, $prefix) {
  3749. $course_id = api_get_course_int_id();
  3750. if (api_get_setting('search_enabled') !== 'true')
  3751. return false;
  3752. if (!extension_loaded('xapian')) {
  3753. return false;
  3754. }
  3755. $terms_string = trim($terms_string);
  3756. $terms = explode(',', $terms_string);
  3757. array_walk($terms, 'trim_value');
  3758. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  3759. // Don't do anything if no change, verify only at DB, not the search engine.
  3760. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  3761. return false;
  3762. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  3763. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  3764. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  3765. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  3766. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  3767. // TODO: Make query secure agains XSS : use member attr instead of post var.
  3768. $lp_id = intval($_POST['lp_id']);
  3769. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  3770. $result = Database::query($sql);
  3771. $di = new ChamiloIndexer();
  3772. while ($lp_item = Database :: fetch_array($result)) {
  3773. // Get search_did.
  3774. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  3775. $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';
  3776. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  3777. //echo $sql; echo '<br>';
  3778. $res = Database::query($sql);
  3779. if (Database::num_rows($res) > 0) {
  3780. $se_ref = Database :: fetch_array($res);
  3781. // Compare terms.
  3782. $doc = $di->get_document($se_ref['search_did']);
  3783. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  3784. $xterms = array ();
  3785. foreach ($xapian_terms as $xapian_term) {
  3786. $xterms[] = substr($xapian_term['name'], 1);
  3787. }
  3788. $dterms = $terms;
  3789. $missing_terms = array_diff($dterms, $xterms);
  3790. $deprecated_terms = array_diff($xterms, $dterms);
  3791. // Save it to search engine.
  3792. foreach ($missing_terms as $term) {
  3793. $doc->add_term($prefix . $term, 1);
  3794. }
  3795. foreach ($deprecated_terms as $term) {
  3796. $doc->remove_term($prefix . $term);
  3797. }
  3798. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  3799. $di->getDb()->flush();
  3800. } else {
  3801. //@todo What we should do here?
  3802. }
  3803. }
  3804. return true;
  3805. }
  3806. /**
  3807. * Sets the theme of the LP (local/remote) (and save)
  3808. * @param string Optional string giving the new theme of this learnpath
  3809. * @return bool Returns true if theme name is not empty
  3810. */
  3811. public function set_theme($name = '') {
  3812. $course_id = api_get_course_int_id();
  3813. if ($this->debug > 0) {
  3814. error_log('New LP - In learnpath::set_theme()', 0);
  3815. }
  3816. $this->theme = Database::escape_string($name);
  3817. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3818. $lp_id = $this->get_id();
  3819. $sql = "UPDATE $lp_table SET theme = '" . $this->theme . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3820. if ($this->debug > 2) {
  3821. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  3822. }
  3823. //$res = Database::query($sql);
  3824. $res = Database::query($sql);
  3825. return true;
  3826. }
  3827. /**
  3828. * Sets the image of an LP (and save)
  3829. * @param string Optional string giving the new image of this learnpath
  3830. * @return bool Returns true if theme name is not empty
  3831. */
  3832. public function set_preview_image($name = '') {
  3833. $course_id = api_get_course_int_id();
  3834. if ($this->debug > 0) {
  3835. error_log('New LP - In learnpath::set_preview_image()', 0);
  3836. }
  3837. $this->preview_image = Database::escape_string($name);
  3838. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3839. $lp_id = $this->get_id();
  3840. $sql = "UPDATE $lp_table SET preview_image = '" . $this->preview_image . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3841. if ($this->debug > 2) {
  3842. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  3843. }
  3844. $res = Database::query($sql);
  3845. return true;
  3846. }
  3847. /**
  3848. * Sets the author of a LP (and save)
  3849. * @param string Optional string giving the new author of this learnpath
  3850. * @return bool Returns true if author's name is not empty
  3851. */
  3852. public function set_author($name = '') {
  3853. $course_id = api_get_course_int_id();
  3854. if ($this->debug > 0) {
  3855. error_log('New LP - In learnpath::set_author()', 0);
  3856. }
  3857. $this->author = Database::escape_string($name);
  3858. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3859. $lp_id = $this->get_id();
  3860. $sql = "UPDATE $lp_table SET author = '" . $this->author . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3861. if ($this->debug > 2) {
  3862. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  3863. }
  3864. Database::query($sql);
  3865. return true;
  3866. }
  3867. /**
  3868. * Sets the hide_toc_frame parameter of a LP (and save)
  3869. * @param int 1 if frame is hiddent 0 thenelse
  3870. * @return bool Returns true if author's name is not empty
  3871. */
  3872. public function set_hide_toc_frame($hide) {
  3873. $course_id = api_get_course_int_id();
  3874. if ($this->debug > 0) {
  3875. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  3876. }
  3877. if (intval($hide) == $hide){
  3878. $this->hide_toc_frame = $hide;
  3879. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3880. $lp_id = $this->get_id();
  3881. $sql = "UPDATE $lp_table SET hide_toc_frame = '" . $this->hide_toc_frame . "'
  3882. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3883. if ($this->debug > 2) {
  3884. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  3885. }
  3886. Database::query($sql);
  3887. return true;
  3888. } else {
  3889. return false;
  3890. }
  3891. }
  3892. /**
  3893. * Sets the prerequisite of a LP (and save)
  3894. * @param int integer giving the new prerequisite of this learnpath
  3895. * @return bool returns true if prerequisite is not empty
  3896. */
  3897. public function set_prerequisite($prerequisite) {
  3898. $course_id = api_get_course_int_id();
  3899. if ($this->debug > 0) {
  3900. error_log('New LP - In learnpath::set_prerequisite()', 0);
  3901. }
  3902. $this->prerequisite = intval($prerequisite);
  3903. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3904. $lp_id = $this->get_id();
  3905. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  3906. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3907. if ($this->debug > 2) {
  3908. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  3909. }
  3910. Database::query($sql);
  3911. return true;
  3912. }
  3913. /**
  3914. * Sets the location/proximity of the LP (local/remote) (and save)
  3915. * @param string Optional string giving the new location of this learnpath
  3916. * @return boolean True on success / False on error
  3917. */
  3918. public function set_proximity($name = '') {
  3919. $course_id = api_get_course_int_id();
  3920. if ($this->debug > 0) {
  3921. error_log('New LP - In learnpath::set_proximity()', 0);
  3922. }
  3923. if (empty ($name))
  3924. return false;
  3925. $this->proximity = Database::escape_string($name);
  3926. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3927. $lp_id = $this->get_id();
  3928. $sql = "UPDATE $lp_table SET content_local = '" . $this->proximity . "'
  3929. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3930. if ($this->debug > 2) {
  3931. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  3932. }
  3933. $res = Database::query($sql);
  3934. return true;
  3935. }
  3936. /**
  3937. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  3938. * @param integer DB ID of the item
  3939. */
  3940. public function set_previous_item($id) {
  3941. if ($this->debug > 0) {
  3942. error_log('New LP - In learnpath::set_previous_item()', 0);
  3943. }
  3944. $this->last = $id;
  3945. }
  3946. /**
  3947. * Sets use_max_score
  3948. * @param string Optional string giving the new location of this learnpath
  3949. * @return boolean True on success / False on error
  3950. */
  3951. public function set_use_max_score($use_max_score = 1) {
  3952. $course_id = api_get_course_int_id();
  3953. if ($this->debug > 0) {
  3954. error_log('New LP - In learnpath::set_use_max_score()', 0);
  3955. }
  3956. $use_max_score = intval($use_max_score);
  3957. $this->use_max_score = $use_max_score;
  3958. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3959. $lp_id = $this->get_id();
  3960. $sql = "UPDATE $lp_table SET use_max_score = '" . $this->use_max_score . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3961. if ($this->debug > 2) {
  3962. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  3963. }
  3964. $res = Database::query($sql);
  3965. return true;
  3966. }
  3967. /**
  3968. * Sets and saves the expired_on date
  3969. * @param string Optional string giving the new author of this learnpath
  3970. * @return bool Returns true if author's name is not empty
  3971. */
  3972. public function set_expired_on($expired_on) {
  3973. $course_id = api_get_course_int_id();
  3974. if ($this->debug > 0) {
  3975. error_log('New LP - In learnpath::set_expired_on()', 0);
  3976. }
  3977. if (!empty($expired_on)) {
  3978. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  3979. } else {
  3980. $this->expired_on = '';
  3981. }
  3982. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3983. $lp_id = $this->get_id();
  3984. $sql = "UPDATE $lp_table SET expired_on = '" . $this->expired_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3985. if ($this->debug > 2) {
  3986. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  3987. }
  3988. $res = Database::query($sql);
  3989. return true;
  3990. }
  3991. /**
  3992. * Sets and saves the publicated_on date
  3993. * @param string Optional string giving the new author of this learnpath
  3994. * @return bool Returns true if author's name is not empty
  3995. */
  3996. public function set_publicated_on($publicated_on) {
  3997. $course_id = api_get_course_int_id();
  3998. if ($this->debug > 0) {
  3999. error_log('New LP - In learnpath::set_expired_on()', 0);
  4000. }
  4001. if (!empty($publicated_on)) {
  4002. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4003. } else {
  4004. $this->publicated_on = '';
  4005. }
  4006. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4007. $lp_id = $this->get_id();
  4008. $sql = "UPDATE $lp_table SET publicated_on = '" . $this->publicated_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4009. if ($this->debug > 2) {
  4010. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4011. }
  4012. $res = Database::query($sql);
  4013. return true;
  4014. }
  4015. /**
  4016. * Sets and saves the expired_on date
  4017. * @param string Optional string giving the new author of this learnpath
  4018. * @return bool Returns true if author's name is not empty
  4019. */
  4020. public function set_modified_on() {
  4021. $course_id = api_get_course_int_id();
  4022. if ($this->debug > 0) {
  4023. error_log('New LP - In learnpath::set_expired_on()', 0);
  4024. }
  4025. $this->modified_on = api_get_utc_datetime();
  4026. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4027. $lp_id = $this->get_id();
  4028. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4029. if ($this->debug > 2) {
  4030. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4031. }
  4032. Database::query($sql);
  4033. return true;
  4034. }
  4035. /**
  4036. * Sets the object's error message
  4037. * @param string Error message. If empty, reinits the error string
  4038. * @return void
  4039. */
  4040. public function set_error_msg($error = '') {
  4041. if ($this->debug > 0) {
  4042. error_log('New LP - In learnpath::set_error_msg()', 0);
  4043. }
  4044. if (empty ($error)) {
  4045. $this->error = '';
  4046. } else {
  4047. $this->error .= $error;
  4048. }
  4049. }
  4050. /**
  4051. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4052. * @param boolean Whether to allow a new attempt or not
  4053. * @return boolean True
  4054. */
  4055. public function start_current_item($allow_new_attempt = false) {
  4056. if ($this->debug > 0) {
  4057. error_log('New LP - In learnpath::start_current_item()', 0);
  4058. }
  4059. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4060. $type = $this->get_type();
  4061. $item_type = $this->items[$this->current]->get_type();
  4062. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4063. $this->items[$this->current]->open($allow_new_attempt);
  4064. $this->autocomplete_parents($this->current);
  4065. $prereq_check = $this->prerequisites_match($this->current);
  4066. $this->items[$this->current]->save(false, $prereq_check);
  4067. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4068. } else {
  4069. // If sco, then it is supposed to have been updated by some other call.
  4070. }
  4071. if ($item_type == 'sco') {
  4072. $this->items[$this->current]->restart();
  4073. }
  4074. }
  4075. if ($this->debug > 0) {
  4076. error_log('New LP - End of learnpath::start_current_item()', 0);
  4077. }
  4078. return true;
  4079. }
  4080. /**
  4081. * Stops the processing and counters for the old item (as held in $this->last)
  4082. * @return boolean True/False
  4083. */
  4084. public function stop_previous_item() {
  4085. if ($this->debug > 0) {
  4086. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4087. }
  4088. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4089. if ($this->debug > 2) {
  4090. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4091. }
  4092. switch ($this->get_type()) {
  4093. case '3' :
  4094. if ($this->items[$this->last]->get_type() != 'au') {
  4095. if ($this->debug > 2) {
  4096. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4097. }
  4098. $this->items[$this->last]->close();
  4099. //$this->autocomplete_parents($this->last);
  4100. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4101. } else {
  4102. if ($this->debug > 2) {
  4103. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4104. }
  4105. }
  4106. case '2' :
  4107. if ($this->items[$this->last]->get_type() != 'sco') {
  4108. if ($this->debug > 2) {
  4109. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4110. }
  4111. $this->items[$this->last]->close();
  4112. //$this->autocomplete_parents($this->last);
  4113. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4114. } else {
  4115. if ($this->debug > 2) {
  4116. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4117. }
  4118. }
  4119. break;
  4120. case '1' :
  4121. default :
  4122. if ($this->debug > 2) {
  4123. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4124. }
  4125. $this->items[$this->last]->close();
  4126. break;
  4127. }
  4128. } else {
  4129. if ($this->debug > 2) {
  4130. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4131. }
  4132. return false;
  4133. }
  4134. return true;
  4135. }
  4136. /**
  4137. * Updates the default view mode from fullscreen to embedded and inversely
  4138. * @return string The current default view mode ('fullscreen' or 'embedded')
  4139. */
  4140. public function update_default_view_mode() {
  4141. $course_id = api_get_course_int_id();
  4142. if ($this->debug > 0) {
  4143. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4144. }
  4145. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4146. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4147. $res = Database::query($sql);
  4148. if (Database :: num_rows($res) > 0) {
  4149. $row = Database :: fetch_array($res);
  4150. $view_mode = $row['default_view_mod'];
  4151. if ($view_mode == 'fullscreen') {
  4152. $view_mode = 'embedded';
  4153. } elseif ($view_mode == 'embedded') {
  4154. $view_mode = 'embedframe';
  4155. } elseif ($view_mode == 'embedframe') {
  4156. $view_mode = 'fullscreen';
  4157. }
  4158. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4159. $res = Database::query($sql);
  4160. $this->mode = $view_mode;
  4161. return $view_mode;
  4162. } else {
  4163. if ($this->debug > 2) {
  4164. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4165. }
  4166. }
  4167. return -1;
  4168. }
  4169. /**
  4170. * Updates the default behaviour about auto-commiting SCORM updates
  4171. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4172. */
  4173. public function update_default_scorm_commit() {
  4174. $course_id = api_get_course_int_id();
  4175. if ($this->debug > 0) {
  4176. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4177. }
  4178. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4179. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4180. $res = Database::query($sql);
  4181. if (Database :: num_rows($res) > 0) {
  4182. $row = Database :: fetch_array($res);
  4183. $force = $row['force_commit'];
  4184. if ($force == 1) {
  4185. $force = 0;
  4186. $force_return = false;
  4187. } elseif ($force == 0) {
  4188. $force = 1;
  4189. $force_return = true;
  4190. }
  4191. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4192. $res = Database::query($sql);
  4193. $this->force_commit = $force_return;
  4194. return $force_return;
  4195. } else {
  4196. if ($this->debug > 2) {
  4197. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4198. }
  4199. }
  4200. return -1;
  4201. }
  4202. /**
  4203. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4204. * @return bool True on success, false on failure
  4205. */
  4206. public function update_display_order() {
  4207. $course_id = api_get_course_int_id();
  4208. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4209. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4210. $res = Database::query($sql);
  4211. if ($res === false)
  4212. return false;
  4213. $lps = array ();
  4214. $lp_order = array ();
  4215. $num = Database :: num_rows($res);
  4216. // First check the order is correct, globally (might be wrong because
  4217. // of versions < 1.8.4).
  4218. if ($num > 0) {
  4219. $i = 1;
  4220. while ($row = Database :: fetch_array($res)) {
  4221. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4222. $need_fix = true;
  4223. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4224. $res_u = Database::query($sql_u);
  4225. }
  4226. $i++;
  4227. }
  4228. }
  4229. return true;
  4230. }
  4231. /**
  4232. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4233. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4234. */
  4235. public function update_reinit() {
  4236. $course_id = api_get_course_int_id();
  4237. if ($this->debug > 0) {
  4238. error_log('New LP - In learnpath::update_reinit()', 0);
  4239. }
  4240. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4241. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4242. $res = Database::query($sql);
  4243. if (Database :: num_rows($res) > 0) {
  4244. $row = Database :: fetch_array($res);
  4245. $force = $row['prevent_reinit'];
  4246. if ($force == 1) {
  4247. $force = 0;
  4248. } elseif ($force == 0) {
  4249. $force = 1;
  4250. }
  4251. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4252. $res = Database::query($sql);
  4253. $this->prevent_reinit = $force;
  4254. return $force;
  4255. } else {
  4256. if ($this->debug > 2) {
  4257. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4258. }
  4259. }
  4260. return -1;
  4261. }
  4262. /**
  4263. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4264. *
  4265. * @return string 'single', 'multi' or 'seriousgame'
  4266. * @author ndiechburg <noel@cblue.be>
  4267. **/
  4268. public function get_attempt_mode() {
  4269. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4270. $this->seriousgame_mode=0;
  4271. }
  4272. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4273. $this->prevent_reinit =1;
  4274. }
  4275. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4276. return 'seriousgame';
  4277. }
  4278. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4279. return 'single';
  4280. }
  4281. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4282. return 'multiple';
  4283. }
  4284. return 'single';
  4285. }
  4286. /**
  4287. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4288. *
  4289. * @param string 'seriousgame', 'single' or 'multiple'
  4290. * @return boolean
  4291. * @author ndiechburg <noel@cblue.be>
  4292. **/
  4293. public function set_attempt_mode($mode) {
  4294. $course_id = api_get_course_int_id();
  4295. switch ($mode) {
  4296. case 'seriousgame' :
  4297. $sg_mode = 1;
  4298. $prevent_reinit = 1;
  4299. break;
  4300. case 'single' :
  4301. $sg_mode = 0;
  4302. $prevent_reinit = 1;
  4303. break;
  4304. case 'multiple' :
  4305. $sg_mode = 0;
  4306. $prevent_reinit = 0;
  4307. break;
  4308. default :
  4309. $sg_mode = 0;
  4310. $prevent_reinit = 0;
  4311. break;
  4312. }
  4313. $this->prevent_reinit = $prevent_reinit;
  4314. $this->seriousgame_mode = $sg_mode;
  4315. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4316. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4317. $res = Database::query($sql);
  4318. if ($res) {
  4319. return true;
  4320. } else {
  4321. return false;
  4322. }
  4323. }
  4324. /**
  4325. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4326. *
  4327. * @return boolean
  4328. * @author ndiechburg <noel@cblue.be>
  4329. **/
  4330. public function switch_attempt_mode() {
  4331. if ($this->debug > 0) {
  4332. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4333. }
  4334. $mode = $this->get_attempt_mode();
  4335. switch ($mode) {
  4336. case 'single' :
  4337. $next_mode = 'multiple';
  4338. break;
  4339. case 'multiple' :
  4340. $next_mode = 'seriousgame';
  4341. break;
  4342. case 'seriousgame' :
  4343. $next_mode = 'single';
  4344. break;
  4345. default :
  4346. $next_mode = 'single';
  4347. break;
  4348. }
  4349. $this->set_attempt_mode($next_mode);
  4350. }
  4351. /**
  4352. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4353. *
  4354. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4355. * @author ndiechburg <noel@cblue.be>
  4356. **/
  4357. public function set_seriousgame_mode() {
  4358. $course_id = api_get_course_int_id();
  4359. if ($this->debug > 0) {
  4360. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4361. }
  4362. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4363. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4364. $res = Database::query($sql);
  4365. if (Database :: num_rows($res) > 0) {
  4366. $row = Database :: fetch_array($res);
  4367. $force = $row['seriousgame_mode'];
  4368. if ($force == 1) {
  4369. $force = 0;
  4370. } elseif ($force == 0) {
  4371. $force = 1;
  4372. }
  4373. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4374. $res = Database::query($sql);
  4375. $this->seriousgame_mode = $force;
  4376. return $force;
  4377. } else {
  4378. if ($this->debug > 2) {
  4379. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4380. }
  4381. }
  4382. return -1;
  4383. }
  4384. /**
  4385. * Updates the "scorm_debug" value that shows or hide the debug window
  4386. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4387. */
  4388. public function update_scorm_debug() {
  4389. $course_id = api_get_course_int_id();
  4390. if ($this->debug > 0) {
  4391. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4392. }
  4393. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4394. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4395. $res = Database::query($sql);
  4396. if (Database :: num_rows($res) > 0) {
  4397. $row = Database :: fetch_array($res);
  4398. $force = $row['debug'];
  4399. if ($force == 1) {
  4400. $force = 0;
  4401. } elseif ($force == 0) {
  4402. $force = 1;
  4403. }
  4404. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4405. $res = Database::query($sql);
  4406. $this->scorm_debug = $force;
  4407. return $force;
  4408. } else {
  4409. if ($this->debug > 2) {
  4410. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4411. }
  4412. }
  4413. return -1;
  4414. }
  4415. /**
  4416. * Function that makes a call to the function sort_tree_array and create_tree_array
  4417. * @author Kevin Van Den Haute
  4418. * @param array
  4419. */
  4420. public function tree_array($array) {
  4421. if ($this->debug > 1) {
  4422. error_log('New LP - In learnpath::tree_array()', 0);
  4423. }
  4424. $array = $this->sort_tree_array($array);
  4425. $this->create_tree_array($array);
  4426. }
  4427. /**
  4428. * Creates an array with the elements of the learning path tree in it
  4429. *
  4430. * @author Kevin Van Den Haute
  4431. * @param array $array
  4432. * @param int $parent
  4433. * @param int $depth
  4434. * @param array $tmp
  4435. */
  4436. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ()) {
  4437. if ($this->debug > 1) {
  4438. error_log('New LP - In learnpath::create_tree_array())', 0);
  4439. }
  4440. if (is_array($array)) {
  4441. for ($i = 0; $i < count($array); $i++) {
  4442. if ($array[$i]['parent_item_id'] == $parent) {
  4443. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4444. $tmp[] = $array[$i]['parent_item_id'];
  4445. $depth++;
  4446. }
  4447. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4448. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4449. $this->arrMenu[] = array (
  4450. 'id' => $array[$i]['id'],
  4451. 'item_type' => $array[$i]['item_type'],
  4452. 'title' => $array[$i]['title'],
  4453. 'path' => $array[$i]['path'],
  4454. 'description' => $array[$i]['description'],
  4455. 'parent_item_id' => $array[$i]['parent_item_id'],
  4456. 'previous_item_id' => $array[$i]['previous_item_id'],
  4457. 'next_item_id' => $array[$i]['next_item_id'],
  4458. 'min_score' => $array[$i]['min_score'],
  4459. 'max_score' => $array[$i]['max_score'],
  4460. 'mastery_score' => $array[$i]['mastery_score'],
  4461. 'display_order' => $array[$i]['display_order'],
  4462. 'prerequisite' => $preq,
  4463. 'depth' => $depth,
  4464. 'audio' => $audio
  4465. );
  4466. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4467. }
  4468. }
  4469. }
  4470. }
  4471. /**
  4472. * Sorts a multi dimensional array by parent id and display order
  4473. * @author Kevin Van Den Haute
  4474. *
  4475. * @param array $array (array with al the learning path items in it)
  4476. *
  4477. * @return array
  4478. */
  4479. public function sort_tree_array($array) {
  4480. foreach ($array as $key => $row) {
  4481. $parent[$key] = $row['parent_item_id'];
  4482. $position[$key] = $row['display_order'];
  4483. }
  4484. if (count($array) > 0)
  4485. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4486. return $array;
  4487. }
  4488. /**
  4489. * Function that creates a table structure with a learning path his modules, chapters and documents.
  4490. * Also the actions for the modules, chapters and documents are in this table.
  4491. * @author Kevin Van Den Haute
  4492. * @param int $lp_id
  4493. * @return string
  4494. */
  4495. public function overview() {
  4496. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4497. if ($this->debug > 0) {
  4498. error_log('New LP - In learnpath::overview()', 0);
  4499. }
  4500. global $_course;
  4501. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4502. $return = '';
  4503. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  4504. // we need to start a form when we want to update all the mp3 files
  4505. if ($update_audio == 'true') {
  4506. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&amp;action=' . Security :: remove_XSS($_GET['action']) . '&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  4507. }
  4508. $return .= '<div id="message"></div>';
  4509. $return_audio = '<table class="data_table">';
  4510. $return_audio .= '<tr>';
  4511. $return_audio .= '<th width="60%">' . get_lang('Title') . '</th>';
  4512. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  4513. $return_audio .= '</tr>';
  4514. if ($update_audio != 'true') {
  4515. $return .= '<div class="span12">';
  4516. $return .= self::return_new_tree($update_audio);
  4517. $return .='</div>';
  4518. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn')), array('style'=>'float:left; margin-top:15px;width:100%'));
  4519. } else {
  4520. $return_audio .= self::return_new_tree($update_audio);
  4521. $return .= $return_audio.'</table>';
  4522. }
  4523. // We need to close the form when we are updating the mp3 files.
  4524. if ($update_audio == 'true') {
  4525. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  4526. }
  4527. // We need to close the form when we are updating the mp3 files.
  4528. if ($update_audio == 'true' && count($arrLP) != 0) {
  4529. $return .= '</form>';
  4530. }
  4531. return $return;
  4532. }
  4533. public function return_new_tree($update_audio = 'false', $drop_element_here = false) {
  4534. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  4535. echo '<script>
  4536. var newOrderData= "";
  4537. function processChildren(parentId) {
  4538. //Loop through the children of the UL element defined by the parentId
  4539. var ulParentID= "UL_" + parentId;
  4540. $("#" + ulParentID).children().each(function () {
  4541. /*Only process elements with an id attribute (in order to skip the blank,
  4542. unmovable <li> elements.*/
  4543. if ($(this).attr("id")) {
  4544. /*Build a string of data with the childs ID and parent ID,
  4545. using the "|" as a delimiter between the two IDs and the "^"
  4546. as a record delimiter (these delimiters were chosen in case the data
  4547. involved includes more common delimiters like commas within the content)
  4548. */
  4549. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  4550. //Determine if this child is a containter
  4551. if ($(this).is(".container")) {
  4552. //Process the child elements of the container
  4553. processChildren($(this).attr("id"));
  4554. }
  4555. }
  4556. }); //end of children loop
  4557. } //end of processChildren function
  4558. $(function() {
  4559. $(".item_data").live("mouseover", function(event) {
  4560. $(".button_actions", this).show();
  4561. });
  4562. $(".item_data").live("mouseout", function() {
  4563. $(".button_actions",this).hide();
  4564. });
  4565. $(".button_actions").hide();
  4566. $( ".lp_resource" ).sortable({
  4567. items: ".lp_resource_element ",
  4568. handle: ".moved", //only the class "moved"
  4569. cursor: "move",
  4570. connectWith: "#lp_item_list",
  4571. placeholder: "ui-state-highlight", //defines the yellow highlight
  4572. start: function(event, ui) {
  4573. $(ui.item).css("width", "160px");
  4574. $(ui.item).find(".item_data").attr("style", "");
  4575. },
  4576. stop: function(event, ui) {
  4577. $(ui.item).css("width", "100%");
  4578. },
  4579. });
  4580. $("#lp_item_list").sortable({
  4581. items: "li",
  4582. handle: ".moved", //only the class "moved"
  4583. cursor: "move",
  4584. placeholder: "ui-state-highlight", //defines the yellow highlight
  4585. update: function(event, ui) {
  4586. //Walk through the direct descendants of the lp_item_list <ul>
  4587. $("#lp_item_list").children().each(function () {
  4588. /*Only process elements with an id attribute (in order to skip the blank,
  4589. unmovable <li> elements.*/
  4590. if ($(this).attr("id")) {
  4591. /*Build a string of data with the child s ID and parent ID,
  4592. using the "|" as a delimiter between the two IDs and the "^"
  4593. as a record delimiter (these delimiters were chosen in case the data
  4594. involved includes more common delimiters like commas within the content)
  4595. */
  4596. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  4597. //Determine if this child is a containter
  4598. if ($(this).is(".li_container")) {
  4599. //Process the child elements of the container
  4600. processChildren($(this).attr("id"));
  4601. }
  4602. }
  4603. }); //end of lp_item_list children loop
  4604. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  4605. $.post("'.$ajax_url.'", order, function(reponse){
  4606. $("#message").html(reponse);
  4607. });
  4608. },
  4609. receive: function(event, ui) {
  4610. var id = $(ui.item).attr("data_id");
  4611. var type = $(ui.item).attr("data_type");
  4612. var title = $(ui.item).attr("title");
  4613. if (ui.item.parent()[0]) {
  4614. var parent_id = $(ui.item.parent()[0]).attr("id");
  4615. var previous_id = $(ui.item.prev()).attr("id");
  4616. if (parent_id) {
  4617. parent_id = parent_id.split("_")[1];
  4618. var params = {
  4619. "a": "add_lp_item",
  4620. "id": id,
  4621. "parent_id": parent_id,
  4622. "previous_id": previous_id,
  4623. "type": type,
  4624. "title" : title
  4625. };
  4626. $.ajax({
  4627. type: "GET",
  4628. url: "'.$ajax_url.'",
  4629. data: params,
  4630. async: false,
  4631. success: function(data) {
  4632. if (data == -1) {
  4633. } else {
  4634. $(".normal-message").hide();
  4635. $(ui.item).attr("id", data);
  4636. $(ui.item).addClass("lp_resource_element_new");
  4637. $(ui.item).find(".item_data").attr("style", "");
  4638. $(ui.item).addClass("record li_container");
  4639. $(ui.item).removeClass("lp_resource_element");
  4640. $(ui.item).removeClass("doc_resource");
  4641. }
  4642. }
  4643. });
  4644. }
  4645. }//
  4646. }//end receive
  4647. });
  4648. });
  4649. </script>';
  4650. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4651. $course_id = api_get_course_int_id();
  4652. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  4653. $sql = "SELECT * FROM $tbl_lp_item
  4654. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  4655. $result = Database::query($sql);
  4656. $arrLP = array();
  4657. while ($row = Database :: fetch_array($result)) {
  4658. $row['title'] = Security :: remove_XSS($row['title']);
  4659. $row['description'] = Security :: remove_XSS($row['description']);
  4660. $arrLP[] = array (
  4661. 'id' => $row['id'],
  4662. 'item_type' => $row['item_type'],
  4663. 'title' => $row['title'],
  4664. 'path' => $row['path'],
  4665. 'description' => $row['description'],
  4666. 'parent_item_id' => $row['parent_item_id'],
  4667. 'previous_item_id' => $row['previous_item_id'],
  4668. 'next_item_id' => $row['next_item_id'],
  4669. 'max_score' => $row['max_score'],
  4670. 'min_score' => $row['min_score'],
  4671. 'mastery_score' => $row['mastery_score'],
  4672. 'prerequisite' => $row['prerequisite'],
  4673. 'display_order' => $row['display_order'],
  4674. 'audio' => $row['audio']
  4675. );
  4676. }
  4677. $this->tree_array($arrLP);
  4678. $arrLP = $this->arrMenu;
  4679. unset ($this->arrMenu);
  4680. $default_data = null;
  4681. $default_content = null;
  4682. $elements = array();
  4683. $return_audio = null;
  4684. for ($i = 0; $i < count($arrLP); $i++) {
  4685. $title = $arrLP[$i]['title'];
  4686. $title_cut = cut($arrLP[$i]['title'], 25);
  4687. //Link for the documents
  4688. if ($arrLP[$i]['item_type'] == 'document') {
  4689. $url = api_get_self() . '?'.api_get_cidreq().'&amp;action=view_item&amp;mode=preview_document&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id;
  4690. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  4691. }
  4692. if (($i % 2) == 0) {
  4693. $oddclass = 'row_odd';
  4694. } else {
  4695. $oddclass = 'row_even';
  4696. }
  4697. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddclass . '">';
  4698. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4699. $icon = '';
  4700. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4701. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  4702. } else {
  4703. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4704. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  4705. } else {
  4706. $icon = '<img src="../img/folder_document.gif" />';
  4707. }
  4708. }
  4709. // The audio column.
  4710. $return_audio .= '<td align="center">';
  4711. $audio = '';
  4712. if (!$update_audio OR $update_audio <> 'true') {
  4713. if (!empty($arrLP[$i]['audio'])) {
  4714. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  4715. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  4716. $audio .= '<script type="text/javascript">
  4717. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  4718. s1.addParam("allowscriptaccess","always");
  4719. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  4720. s1.write("container' . $i . '");
  4721. </script>';*/
  4722. } else {
  4723. $audio .= '';
  4724. }
  4725. } else {
  4726. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  4727. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  4728. if (!empty ($arrLP[$i]['audio'])) {
  4729. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br /><input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  4730. }
  4731. }
  4732. }
  4733. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  4734. $return_audio .= '</td>';
  4735. $move_icon = '';
  4736. $move_item_icon = '';
  4737. $edit_icon = '';
  4738. $delete_icon = '';
  4739. $audio_icon = '';
  4740. $prerequisities_icon = '';
  4741. if ($is_allowed_to_edit) {
  4742. if (!$update_audio OR $update_audio <> 'true') {
  4743. $move_icon .= '<a class="moved" href="#">';
  4744. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  4745. $move_icon .= '</a>';
  4746. }
  4747. // No edit for this item types
  4748. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  4749. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  4750. $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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'] . '">';
  4751. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4752. $edit_icon .= '</a>';
  4753. } else {
  4754. $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=edit_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
  4755. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4756. $edit_icon .= '</a>';
  4757. }
  4758. }
  4759. $delete_icon .= ' <a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=delete_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
  4760. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  4761. $delete_icon .= '</a>';
  4762. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  4763. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  4764. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  4765. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  4766. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  4767. }
  4768. }
  4769. if ($update_audio != 'true') {
  4770. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
  4771. } else {
  4772. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  4773. }
  4774. $parent_id = $arrLP[$i]['parent_item_id'];
  4775. $default_data[$arrLP[$i]['id']] = $row;
  4776. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  4777. if (empty($parent_id)) {
  4778. $elements[$arrLP[$i]['id']]['data'] = $row;
  4779. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4780. } else {
  4781. $parent_arrays = array();
  4782. if ($arrLP[$i]['depth'] > 1) {
  4783. //Getting list of parents
  4784. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  4785. foreach($arrLP as $item) {
  4786. if ($item['id'] == $parent_id) {
  4787. if ($item['parent_item_id'] == 0) {
  4788. $parent_id = $item['id'];
  4789. break;
  4790. } else {
  4791. $parent_id = $item['parent_item_id'];
  4792. if (empty($parent_arrays)) {
  4793. $parent_arrays[] = intval($item['id']);
  4794. }
  4795. $parent_arrays[] = $parent_id;
  4796. break;
  4797. }
  4798. }
  4799. }
  4800. }
  4801. }
  4802. if (!empty($parent_arrays)) {
  4803. $parent_arrays = array_reverse($parent_arrays);
  4804. $val = '$elements';
  4805. $x = 0;
  4806. foreach($parent_arrays as $item) {
  4807. if ($x != count($parent_arrays) -1) {
  4808. $val .= '["'.$item.'"]["children"]';
  4809. } else {
  4810. $val .= '["'.$item.'"]["children"]';
  4811. }
  4812. $x++;
  4813. }
  4814. $val .= "";
  4815. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  4816. eval($code_str);
  4817. } else {
  4818. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  4819. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4820. }
  4821. }
  4822. }
  4823. $return = '<div class="lp_tree well">';
  4824. $return .= '<ul id="lp_item_list">';
  4825. $return .='<h4>'.$this->name.'</h4><br>';
  4826. $tree = self::print_recursive($elements, $default_data, $default_content);
  4827. if (!empty($tree)) {
  4828. $return .= $tree;
  4829. } else {
  4830. if ($drop_element_here) {
  4831. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  4832. }
  4833. }
  4834. $return .= '</ul>';
  4835. if ($update_audio == 'true') {
  4836. $return = $return_audio;
  4837. } else {
  4838. $return .= '</div>';
  4839. }
  4840. return $return;
  4841. }
  4842. function print_recursive($elements, $default_data, $default_content) {
  4843. $return = '';
  4844. foreach ($elements as $key => $item) {
  4845. if (isset($item['load_data']) || empty($item['data'])) {
  4846. $item['data'] = $default_data[$item['load_data']];
  4847. $item['type'] = $default_content[$item['load_data']]['item_type'];
  4848. }
  4849. $sub_list = '';
  4850. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  4851. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  4852. }
  4853. if (empty($item['children'])) {
  4854. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4855. $active = null;
  4856. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  4857. $active = 'active';
  4858. }
  4859. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4860. } else {
  4861. //sections
  4862. if (isset($item['children'])) {
  4863. $data = self::print_recursive($item['children'], $default_data, $default_content);
  4864. }
  4865. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4866. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4867. }
  4868. }
  4869. return $return;
  4870. }
  4871. /**
  4872. * This function builds the action menu
  4873. * @return void
  4874. */
  4875. public function build_action_menu() {
  4876. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4877. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4878. $return = '<div class="actions">';
  4879. //$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>';
  4880. //$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>';
  4881. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '&isStudentView=true">' . Display :: return_icon('preview_view.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  4882. //$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>';
  4883. // 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>';
  4884. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  4885. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;action=edit&amp;lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  4886. $buttons = array(
  4887. array(
  4888. 'title' => get_lang('SetPrerequisiteForEachItem'),
  4889. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=set_previous_step_as_prerequisite&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  4890. ),
  4891. array(
  4892. 'title' => get_lang('ClearAllPrerequisites'),
  4893. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=clear_prerequisites&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  4894. ),
  4895. );
  4896. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  4897. $return .= '</div>';
  4898. echo $return;
  4899. }
  4900. /**
  4901. * This functions builds the LP tree based on data from the database.
  4902. * @return string
  4903. * @deprecated use the return_new_tree() function
  4904. * @uses dtree.js :: necessary javascript for building this tree
  4905. */
  4906. public function build_tree() {
  4907. $course_id = api_get_course_int_id();
  4908. $return = "<script type=\"text/javascript\">\n";
  4909. $return .= "\tm = new dTree('m');\n\n";
  4910. $return .= "\tm.config.folderLinks = true;\n";
  4911. $return .= "\tm.config.useCookies = true;\n";
  4912. $return .= "\tm.config.useIcons = true;\n";
  4913. $return .= "\tm.config.useLines = true;\n";
  4914. $return .= "\tm.config.useSelection = true;\n";
  4915. $return .= "\tm.config.useStatustext = false;\n\n";
  4916. $menu = 0;
  4917. $parent = '';
  4918. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  4919. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  4920. $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
  4921. FROM $tbl_lp_item
  4922. WHERE c_id = ".$course_id." AND lp_id = " . Database::escape_string($this->lp_id);
  4923. $result = Database::query($sql);
  4924. $arrLP = array ();
  4925. while ($row = Database :: fetch_array($result)) {
  4926. $row['title'] = Security :: remove_XSS($row['title']);
  4927. $row['description'] = Security :: remove_XSS($row['description']);
  4928. $arrLP[] = array (
  4929. 'id' => $row['id'],
  4930. 'item_type' => $row['item_type'],
  4931. 'title' => $row['title'],
  4932. 'path' => $row['path'],
  4933. 'description' => $row['description'],
  4934. 'parent_item_id' => $row['parent_item_id'],
  4935. 'previous_item_id' => $row['previous_item_id'],
  4936. 'next_item_id' => $row['next_item_id'],
  4937. 'max_score' => $row['max_score'],
  4938. 'min_score' => $row['min_score'],
  4939. 'mastery_score' => $row['mastery_score'],
  4940. 'display_order' => $row['display_order']
  4941. );
  4942. }
  4943. $this->tree_array($arrLP);
  4944. $arrLP = $this->arrMenu;
  4945. unset ($this->arrMenu);
  4946. $title = '';
  4947. for ($i = 0; $i < count($arrLP); $i++) {
  4948. $title = addslashes($arrLP[$i]['title']);
  4949. $menu_page = api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=view_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $_SESSION['oLP']->lp_id;
  4950. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4951. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4952. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/lp_" . $icon_name . ".png', '../img/lp_" . $icon_name . ".png');\n";
  4953. } else
  4954. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4955. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/lp_" . $icon_name . ".gif', '../img/lp_" . $icon_name . ".gif');\n";
  4956. } else {
  4957. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  4958. }
  4959. if ($menu < $arrLP[$i]['id'])
  4960. $menu = $arrLP[$i]['id'];
  4961. }
  4962. $return .= "\n\tdocument.write(m);\n";
  4963. $return .= "\t if(!m.selectedNode) m.s(1);";
  4964. $return .= "</script>\n";
  4965. return $return;
  4966. }
  4967. /**
  4968. * Creates the default learning path folder
  4969. */
  4970. public static function generate_learning_path_folder($course) {
  4971. //Creating learning_path folder
  4972. $dir = '/learning_path';
  4973. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4974. $folder = false;
  4975. if (!is_dir($filepath.'/'.$dir)) {
  4976. $folder = create_unexisting_directory($course, api_get_user_id(), api_get_session_id(), 0, 0, $filepath, $dir , get_lang('LearningPaths'), 0);
  4977. } else {
  4978. $folder = true;
  4979. }
  4980. return $folder;
  4981. }
  4982. public function generate_lp_folder($course, $lp_name = null) {
  4983. $filepath = '';
  4984. $dir = '/learning_path/';
  4985. if (empty($lp_name)) {
  4986. $lp_name = $this->name;
  4987. }
  4988. $folder = self::generate_learning_path_folder($course);
  4989. //Creating LP folder
  4990. if ($folder) {
  4991. //Limits title size
  4992. $title = api_substr(replace_dangerous_char($lp_name), 0 , 80);
  4993. $dir = $dir.$title;
  4994. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4995. if (!is_dir($filepath.'/'.$dir)) {
  4996. $folder = create_unexisting_directory($course, api_get_user_id(), 0, 0, 0, $filepath, $dir , $lp_name);
  4997. } else {
  4998. $folder = true;
  4999. }
  5000. $dir = $dir.'/';
  5001. if ($folder) {
  5002. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5003. }
  5004. }
  5005. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5006. return $array;
  5007. }
  5008. /**
  5009. * Create a new document //still needs some finetuning
  5010. * @param array $_course
  5011. * @return string
  5012. */
  5013. public function create_document($_course) {
  5014. $course_id = api_get_course_int_id();
  5015. global $charset;
  5016. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5017. if (strstr($dir, '..'))
  5018. $dir = '/';
  5019. if ($dir[0] == '.')
  5020. $dir = substr($dir, 1);
  5021. if ($dir[0] != '/')
  5022. $dir = '/' . $dir;
  5023. if ($dir[strlen($dir) - 1] != '/')
  5024. $dir .= '/';
  5025. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5026. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5027. //Generates folder
  5028. $result = $this->generate_lp_folder($_course);
  5029. $dir = $result['dir'];
  5030. $filepath = $result['filepath'];
  5031. }
  5032. if (!is_dir($filepath)) {
  5033. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5034. $dir = '/';
  5035. }
  5036. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  5037. // is already escaped twice when it gets here.
  5038. $title = replace_dangerous_char(stripslashes($_POST['title']));
  5039. $title = disable_dangerous_file($title);
  5040. $filename = $title;
  5041. $content = $_POST['content_lp'];
  5042. $tmp_filename = $filename;
  5043. $i = 0;
  5044. while (file_exists($filepath . $tmp_filename . '.html'))
  5045. $tmp_filename = $filename . '_' . ++ $i;
  5046. $filename = $tmp_filename . '.html';
  5047. $content = stripslashes($content);
  5048. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5049. // Change the path of mp3 to absolute.
  5050. // The first regexp deals with ../../../ urls.
  5051. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5052. // The second regexp deals with audio/ urls.
  5053. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5054. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5055. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5056. if (!file_exists($filepath . $filename)) {
  5057. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5058. fputs($fp, $content);
  5059. fclose($fp);
  5060. $file_size = filesize($filepath . $filename);
  5061. $save_file_path = $dir.$filename;
  5062. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5063. if ($document_id) {
  5064. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id(), null, null, null, null, api_get_session_id());
  5065. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5066. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5067. if ($new_comment || $new_title) {
  5068. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5069. $ct = '';
  5070. if ($new_comment)
  5071. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5072. if ($new_title)
  5073. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5074. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5075. Database::query($sql_update);
  5076. }
  5077. }
  5078. return $document_id;
  5079. }
  5080. }
  5081. }
  5082. /**
  5083. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5084. * @param array $_course array
  5085. * @return void
  5086. */
  5087. public function edit_document($_course) {
  5088. $course_id = api_get_course_int_id();
  5089. global $_configuration;
  5090. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5091. if (strstr($dir, '..'))
  5092. $dir = '/';
  5093. if ($dir[0] == '.')
  5094. $dir = substr($dir, 1);
  5095. if ($dir[0] != '/')
  5096. $dir = '/' . $dir;
  5097. if ($dir[strlen($dir) - 1] != '/')
  5098. $dir .= '/';
  5099. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5100. if (!is_dir($filepath)) {
  5101. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5102. $dir = '/';
  5103. }
  5104. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5105. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5106. $document_id = intval($_POST['path']);
  5107. $sql = "SELECT path FROM " . $table_doc . " WHERE c_id = $course_id AND id = " . $document_id;
  5108. $res = Database::query($sql);
  5109. $row = Database :: fetch_array($res);
  5110. $content = stripslashes($_POST['content_lp']);
  5111. $file = $filepath . $row['path'];
  5112. if ($fp = @ fopen($file, 'w')) {
  5113. $content = text_filter($content);
  5114. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5115. // Change the path of mp3 to absolute.
  5116. // The first regexp deals with ../../../ urls.
  5117. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5118. // The second regexp deals with audio/ urls.
  5119. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5120. fputs($fp, $content);
  5121. fclose($fp);
  5122. $sql_update = "UPDATE " . $table_doc ." SET title='".Database::escape_string($_POST['title'])."' WHERE c_id = ".$course_id." AND id = " . $document_id;
  5123. Database::query($sql_update);
  5124. }
  5125. }
  5126. }
  5127. /**
  5128. * Displays the selected item, with a panel for manipulating the item
  5129. * @param int $item_id
  5130. * @param string $msg
  5131. * @return string
  5132. */
  5133. public function display_item($item_id, $msg = null, $show_actions = true) {
  5134. $course_id = api_get_course_int_id();
  5135. $return = '';
  5136. if (is_numeric($item_id)) {
  5137. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5138. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5139. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5140. WHERE c_id = ".$course_id." AND lp.id = " . Database::escape_string($item_id);
  5141. $result = Database::query($sql);
  5142. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5143. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5144. // Prevents wrong parent selection for document, see Bug#1251.
  5145. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5146. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5147. }
  5148. if ($show_actions) {
  5149. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5150. }
  5151. $return .= '<div style="padding:10px;">';
  5152. if ($msg != '')
  5153. $return .= $msg;
  5154. $return .= '<h3>'.$row['title'].'</h3>';
  5155. switch ($row['item_type']) {
  5156. case TOOL_QUIZ:
  5157. if (!empty($row['path'])) {
  5158. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5159. $exercise = new Exercise();
  5160. $exercise->read($row['path']);
  5161. $return .= $exercise->description.'<br />';
  5162. }
  5163. break;
  5164. case TOOL_DOCUMENT:
  5165. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5166. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($row['path']);
  5167. $result = Database::query($sql_doc);
  5168. $path_file = Database::result($result, 0, 0);
  5169. $path_parts = pathinfo($path_file);
  5170. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5171. if (in_array($path_parts['extension'], array (
  5172. 'html',
  5173. 'txt',
  5174. 'png',
  5175. 'jpg',
  5176. 'JPG',
  5177. 'jpeg',
  5178. 'JPEG',
  5179. 'gif',
  5180. 'swf'
  5181. ))) {
  5182. $return .= $this->display_document($row['path'], true, true);
  5183. }
  5184. break;
  5185. }
  5186. $return .= '</div>';
  5187. }
  5188. }
  5189. return $return;
  5190. }
  5191. /**
  5192. * Shows the needed forms for editing a specific item
  5193. * @param int $item_id
  5194. * @return string
  5195. */
  5196. public function display_edit_item($item_id) {
  5197. global $_course; // It will disappear.
  5198. $course_id = api_get_course_int_id();
  5199. $return = '';
  5200. if (is_numeric($item_id)) {
  5201. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5202. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = " . Database::escape_string($item_id);
  5203. $res = Database::query($sql);
  5204. $row = Database::fetch_array($res);
  5205. switch ($row['item_type']) {
  5206. case 'dokeos_chapter' :
  5207. case 'dir' :
  5208. case 'asset' :
  5209. case 'sco' :
  5210. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5211. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5212. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5213. } else {
  5214. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5215. }
  5216. break;
  5217. case TOOL_DOCUMENT :
  5218. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5219. $sql_step = " SELECT lp.*, doc.path as dir
  5220. FROM " . $tbl_lp_item . " as lp
  5221. LEFT JOIN " . $tbl_doc . " as doc ON doc.id = lp.path
  5222. WHERE lp.c_id = $course_id AND
  5223. doc.c_id = $course_id AND
  5224. lp.id = " . Database::escape_string($item_id);
  5225. $res_step = Database::query($sql_step);
  5226. $row_step = Database :: fetch_array($res_step);
  5227. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5228. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5229. break;
  5230. case TOOL_LINK :
  5231. $link_id = (string) $row['path'];
  5232. if (ctype_digit($link_id)) {
  5233. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5234. $sql_select = 'SELECT url FROM ' . $tbl_link . ' WHERE c_id = '.$course_id.' AND id = ' . Database::escape_string($link_id);
  5235. $res_link = Database::query($sql_select);
  5236. $row_link = Database :: fetch_array($res_link);
  5237. if (is_array($row_link)) {
  5238. $row['url'] = $row_link['url'];
  5239. }
  5240. }
  5241. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5242. $return .= $this->display_link_form('edit', $item_id, $row);
  5243. break;
  5244. case 'dokeos_module' :
  5245. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5246. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5247. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5248. } else {
  5249. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5250. }
  5251. break;
  5252. case TOOL_QUIZ :
  5253. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5254. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5255. break;
  5256. case TOOL_HOTPOTATOES :
  5257. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5258. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5259. break;
  5260. case TOOL_STUDENTPUBLICATION :
  5261. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5262. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5263. break;
  5264. case TOOL_FORUM :
  5265. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5266. $return .= $this->display_forum_form('edit', $item_id, $row);
  5267. break;
  5268. case TOOL_THREAD :
  5269. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5270. $return .= $this->display_thread_form('edit', $item_id, $row);
  5271. break;
  5272. }
  5273. }
  5274. return $return;
  5275. }
  5276. /**
  5277. * Function that displays a list with al the resources that could be added to the learning path
  5278. * @return string
  5279. */
  5280. public function display_resources() {
  5281. global $_course; // TODO: Don't use globals.
  5282. $course_code = api_get_course_id();
  5283. //Get all the docs
  5284. $documents = $this->get_documents();
  5285. //Get all the exercises
  5286. $exercises = $this->get_exercises();
  5287. // Get all the links
  5288. $links = $this->get_links();
  5289. //Get al the student publications
  5290. $works = $this->get_student_publications();
  5291. //Get al the forums
  5292. $forums = $this->get_forums(null, $course_code);
  5293. $headers = array( Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  5294. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  5295. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  5296. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  5297. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  5298. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  5299. );
  5300. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5301. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5302. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  5303. return true;
  5304. }
  5305. /**
  5306. * Returns the extension of a document
  5307. * @param string filename
  5308. * @return string Extension (part after the last dot)
  5309. */
  5310. public function get_extension($filename) {
  5311. $explode = explode('.', $filename);
  5312. return $explode[count($explode) - 1];
  5313. }
  5314. /**
  5315. * Displays a document by id
  5316. *
  5317. * @param unknown_type $id
  5318. * @return unknown
  5319. */
  5320. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false) {
  5321. global $_course; // It is temporary.
  5322. $course_id = api_get_course_int_id();
  5323. $return = '';
  5324. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5325. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5326. WHERE c_id = ".$course_id." AND id = " . $id;
  5327. $res_doc = Database::query($sql_doc);
  5328. $row_doc = Database :: fetch_array($res_doc);
  5329. // TODO: Add a path filter.
  5330. if ($iframe) {
  5331. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document' . str_replace('%2F', '/', urlencode($row_doc['path'])) . '?' . api_get_cidreq() . '"></iframe>';
  5332. } else {
  5333. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5334. }
  5335. return $return;
  5336. }
  5337. /**
  5338. * Return HTML form to add/edit a quiz
  5339. * @param string Action (add/edit)
  5340. * @param integer Item ID if already exists
  5341. * @param mixed Extra information (quiz ID if integer)
  5342. * @return string HTML form
  5343. */
  5344. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '') {
  5345. $course_id = api_get_course_int_id();
  5346. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5347. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5348. if ($id != 0 && is_array($extra_info)) {
  5349. $item_title = $extra_info['title'];
  5350. $item_description = $extra_info['description'];
  5351. } elseif (is_numeric($extra_info)) {
  5352. $sql_quiz = "SELECT title, description FROM " . $tbl_quiz . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5353. $result = Database::query($sql_quiz);
  5354. $row = Database :: fetch_array($result);
  5355. $item_title = $row['title'];
  5356. $item_description = $row['description'];
  5357. } else {
  5358. $item_title = '';
  5359. $item_description = '';
  5360. }
  5361. $item_title = Security::remove_XSS($item_title);
  5362. $item_description = Security::remove_XSS($item_description);
  5363. $legend = '<legend>';
  5364. if ($id != 0 && is_array($extra_info))
  5365. $parent = $extra_info['parent_item_id'];
  5366. else
  5367. $parent = 0;
  5368. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5369. $result = Database::query($sql);
  5370. $arrLP = array ();
  5371. while ($row = Database :: fetch_array($result)) {
  5372. $arrLP[] = array (
  5373. 'id' => $row['id'],
  5374. 'item_type' => $row['item_type'],
  5375. 'title' => $row['title'],
  5376. 'path' => $row['path'],
  5377. 'description' => $row['description'],
  5378. 'parent_item_id' => $row['parent_item_id'],
  5379. 'previous_item_id' => $row['previous_item_id'],
  5380. 'next_item_id' => $row['next_item_id'],
  5381. 'display_order' => $row['display_order'],
  5382. 'max_score' => $row['max_score'],
  5383. 'min_score' => $row['min_score'],
  5384. 'mastery_score' => $row['mastery_score'],
  5385. 'prerequisite' => $row['prerequisite'],
  5386. 'max_time_allowed' => $row['max_time_allowed']
  5387. );
  5388. }
  5389. $this->tree_array($arrLP);
  5390. $arrLP = $this->arrMenu;
  5391. unset ($this->arrMenu);
  5392. if ($action == 'add')
  5393. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5394. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5395. else
  5396. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5397. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5398. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5399. }
  5400. $legend .= '</legend>';
  5401. $return .= '<div class="sectioncomment">';
  5402. $return .= '<form method="POST">';
  5403. $return .= $legend;
  5404. $return .= '<table class="lp_form">';
  5405. if ($action != 'move') {
  5406. $return .= '<tr>';
  5407. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5408. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5409. $return .= '</tr>';
  5410. }
  5411. $return .= '<tr>';
  5412. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5413. $return .= '<td class="input">';
  5414. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5415. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5416. $arrHide = array (
  5417. $id
  5418. );
  5419. //$parent_item_id = $_SESSION['parent_item_id'];
  5420. for ($i = 0; $i < count($arrLP); $i++) {
  5421. if ($action != 'add') {
  5422. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5423. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5424. } else {
  5425. $arrHide[] = $arrLP[$i]['id'];
  5426. }
  5427. } else {
  5428. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5429. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5430. }
  5431. }
  5432. if (is_array($arrLP)) {
  5433. reset($arrLP);
  5434. }
  5435. $return .= '</select>';
  5436. $return .= '</td>';
  5437. $return .= '</tr>';
  5438. $return .= '<tr>';
  5439. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5440. $return .= '<td class="input">';
  5441. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5442. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5443. for ($i = 0; $i < count($arrLP); $i++) {
  5444. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5445. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5446. $selected = 'selected="selected" ';
  5447. elseif ($action == 'add') $selected = 'selected="selected" ';
  5448. else
  5449. $selected = '';
  5450. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5451. }
  5452. }
  5453. $return .= '</select>';
  5454. $return .= '</td>';
  5455. $return .= '</tr>';
  5456. if ($action != 'move') {
  5457. $id_prerequisite = 0;
  5458. if (is_array($arrLP)) {
  5459. foreach ($arrLP as $key => $value) {
  5460. if ($value['id'] == $id) {
  5461. $id_prerequisite = $value['prerequisite'];
  5462. break;
  5463. }
  5464. }
  5465. }
  5466. $arrHide = array ();
  5467. for ($i = 0; $i < count($arrLP); $i++) {
  5468. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5469. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5470. $s_selected_position = $arrLP[$i]['id'];
  5471. elseif ($action == 'add') $s_selected_position = 0;
  5472. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5473. }
  5474. }
  5475. /*// Commented the prerequisites, only visible in edit (exercise).
  5476. $return .= '<tr>';
  5477. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5478. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5479. foreach($arrHide as $key => $value){
  5480. if($key==$s_selected_position && $action == 'add'){
  5481. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5482. }
  5483. elseif($key==$id_prerequisite && $action == 'edit'){
  5484. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5485. }
  5486. else{
  5487. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5488. }
  5489. }
  5490. $return .= "</select></td>";
  5491. */
  5492. $return .= '</tr>';
  5493. /*$return .= '<tr>';
  5494. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5495. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5496. // Remove temporarily the test description.
  5497. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5498. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5499. $return .= '</tr>'; */
  5500. }
  5501. $return .= '<tr>';
  5502. if ($action == 'add') {
  5503. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5504. } else {
  5505. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5506. }
  5507. $return .= '</tr>';
  5508. $return .= '</table>';
  5509. if ($action == 'move') {
  5510. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5511. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5512. }
  5513. if (is_numeric($extra_info)) {
  5514. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5515. }
  5516. elseif (is_array($extra_info)) {
  5517. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5518. }
  5519. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5520. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5521. $return .= '</form>';
  5522. $return .= '</div>';
  5523. return $return;
  5524. }
  5525. /**
  5526. * Addition of Hotpotatoes tests
  5527. * @param string Action
  5528. * @param integer Internal ID of the item
  5529. * @param mixed Extra information - can be an array with title and description indexes
  5530. * @return string HTML structure to display the hotpotatoes addition formular
  5531. */
  5532. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') {
  5533. $course_id = api_get_course_int_id();
  5534. global $charset;
  5535. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5536. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5537. if ($id != 0 && is_array($extra_info)) {
  5538. $item_title = stripslashes($extra_info['title']);
  5539. $item_description = stripslashes($extra_info['description']);
  5540. } elseif (is_numeric($extra_info)) {
  5541. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5542. $sql_hot = "SELECT * FROM " . $TBL_DOCUMENT . "
  5543. WHERE c_id = ".$course_id." AND
  5544. path LIKE '" . $uploadPath . "/%/%htm%' AND
  5545. id = " . (int) $extra_info . "
  5546. ORDER BY id ASC";
  5547. $res_hot = Database::query($sql_hot);
  5548. $row = Database::fetch_array($res_hot);
  5549. $item_title = $row['title'];
  5550. $item_description = $row['description'];
  5551. if (!empty ($row['comment'])) {
  5552. $item_title = $row['comment'];
  5553. }
  5554. } else {
  5555. $item_title = '';
  5556. $item_description = '';
  5557. }
  5558. if ($id != 0 && is_array($extra_info))
  5559. $parent = $extra_info['parent_item_id'];
  5560. else
  5561. $parent = 0;
  5562. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5563. $result = Database::query($sql);
  5564. $arrLP = array ();
  5565. while ($row = Database :: fetch_array($result)) {
  5566. $arrLP[] = array (
  5567. 'id' => $row['id'],
  5568. 'item_type' => $row['item_type'],
  5569. 'title' => $row['title'],
  5570. 'path' => $row['path'],
  5571. 'description' => $row['description'],
  5572. 'parent_item_id' => $row['parent_item_id'],
  5573. 'previous_item_id' => $row['previous_item_id'],
  5574. 'next_item_id' => $row['next_item_id'],
  5575. 'display_order' => $row['display_order'],
  5576. 'max_score' => $row['max_score'],
  5577. 'min_score' => $row['min_score'],
  5578. 'mastery_score' => $row['mastery_score'],
  5579. 'prerequisite' => $row['prerequisite'],
  5580. 'max_time_allowed' => $row['max_time_allowed']
  5581. );
  5582. }
  5583. $legend = '<legend>';
  5584. if ($action == 'add')
  5585. $legend .= get_lang('CreateTheExercise');
  5586. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  5587. else
  5588. $legend .= get_lang('EditCurrentExecice');
  5589. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5590. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5591. }
  5592. $legend .= '</legend>';
  5593. $return .= '<form method="POST">';
  5594. $return .= $legend;
  5595. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5596. $return .= '<tr>';
  5597. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  5598. $return .= '<td class="input">';
  5599. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5600. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5601. $arrHide = array (
  5602. $id
  5603. );
  5604. if (count($arrLP) > 0) {
  5605. for ($i = 0; $i < count($arrLP); $i++) {
  5606. if ($action != 'add') {
  5607. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5608. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5609. } else {
  5610. $arrHide[] = $arrLP[$i]['id'];
  5611. }
  5612. } else {
  5613. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5614. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5615. }
  5616. }
  5617. reset($arrLP);
  5618. }
  5619. $return .= '</select>';
  5620. $return .= '</td>';
  5621. $return .= '</tr>';
  5622. $return .= '<tr>';
  5623. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  5624. $return .= '<td class="input">';
  5625. $return .= '<select id="previous" name="previous" size="1">';
  5626. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5627. for ($i = 0; $i < count($arrLP); $i++) {
  5628. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5629. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5630. $selected = 'selected="selected" ';
  5631. elseif ($action == 'add') $selected = 'selected="selected" ';
  5632. else
  5633. $selected = '';
  5634. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5635. }
  5636. }
  5637. $return .= '</select>';
  5638. $return .= '</td>';
  5639. $return .= '</tr>';
  5640. if ($action != 'move') {
  5641. $return .= '<tr>';
  5642. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  5643. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5644. $return .= '</tr>';
  5645. $id_prerequisite = 0;
  5646. if (is_array($arrLP) && count($arrLP) > 0) {
  5647. foreach ($arrLP as $key => $value) {
  5648. if ($value['id'] == $id) {
  5649. $id_prerequisite = $value['prerequisite'];
  5650. break;
  5651. }
  5652. }
  5653. $arrHide = array ();
  5654. for ($i = 0; $i < count($arrLP); $i++) {
  5655. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5656. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5657. $s_selected_position = $arrLP[$i]['id'];
  5658. elseif ($action == 'add') $s_selected_position = 0;
  5659. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5660. }
  5661. }
  5662. }
  5663. }
  5664. $return .= '<tr>';
  5665. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  5666. $return .= '</tr>';
  5667. $return .= '</table>';
  5668. if ($action == 'move') {
  5669. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5670. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5671. }
  5672. if (is_numeric($extra_info)) {
  5673. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5674. } elseif (is_array($extra_info)) {
  5675. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5676. }
  5677. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  5678. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5679. $return .= '</form>';
  5680. return $return;
  5681. }
  5682. /**
  5683. * Return the form to display the forum edit/add option
  5684. * @param string Action (add/edit)
  5685. * @param integer ID of the lp_item if already exists
  5686. * @param mixed Forum ID or title
  5687. * @return string HTML form
  5688. */
  5689. public function display_forum_form($action = 'add', $id = 0, $extra_info = '') {
  5690. $course_id = api_get_course_int_id();
  5691. global $charset;
  5692. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5693. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  5694. if ($id != 0 && is_array($extra_info)) {
  5695. $item_title = stripslashes($extra_info['title']);
  5696. }
  5697. elseif (is_numeric($extra_info)) {
  5698. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  5699. FROM " . $tbl_forum . "
  5700. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  5701. $result = Database::query($sql_forum);
  5702. $row = Database :: fetch_array($result);
  5703. $item_title = $row['title'];
  5704. $item_description = $row['comment'];
  5705. } else {
  5706. $item_title = '';
  5707. $item_description = '';
  5708. }
  5709. $legend = '<legend>';
  5710. if ($id != 0 && is_array($extra_info))
  5711. $parent = $extra_info['parent_item_id'];
  5712. else
  5713. $parent = 0;
  5714. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5715. WHERE c_id = ".$course_id." AND
  5716. lp_id = " . $this->lp_id;
  5717. $result = Database::query($sql);
  5718. $arrLP = array ();
  5719. while ($row = Database :: fetch_array($result)) {
  5720. $arrLP[] = array (
  5721. 'id' => $row['id'],
  5722. 'item_type' => $row['item_type'],
  5723. 'title' => $row['title'],
  5724. 'path' => $row['path'],
  5725. 'description' => $row['description'],
  5726. 'parent_item_id' => $row['parent_item_id'],
  5727. 'previous_item_id' => $row['previous_item_id'],
  5728. 'next_item_id' => $row['next_item_id'],
  5729. 'display_order' => $row['display_order'],
  5730. 'max_score' => $row['max_score'],
  5731. 'min_score' => $row['min_score'],
  5732. 'mastery_score' => $row['mastery_score'],
  5733. 'prerequisite' => $row['prerequisite']
  5734. );
  5735. }
  5736. $this->tree_array($arrLP);
  5737. $arrLP = $this->arrMenu;
  5738. unset ($this->arrMenu);
  5739. if ($action == 'add')
  5740. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  5741. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  5742. else
  5743. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  5744. $legend .= '</legend>';
  5745. $return .= '<div class="sectioncomment">';
  5746. $return .= '<form method="POST">';
  5747. $return .= $legend;
  5748. $return .= '<table class="lp_form">';
  5749. if ($action != 'move') {
  5750. $return .= '<tr>';
  5751. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5752. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  5753. $return .= '</tr>';
  5754. }
  5755. $return .= '<tr>';
  5756. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5757. $return .= '<td class="input">';
  5758. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  5759. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5760. $arrHide = array (
  5761. $id
  5762. );
  5763. //$parent_item_id = $_SESSION['parent_item_id'];
  5764. for ($i = 0; $i < count($arrLP); $i++) {
  5765. if ($action != 'add') {
  5766. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5767. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5768. } else {
  5769. $arrHide[] = $arrLP[$i]['id'];
  5770. }
  5771. } else {
  5772. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5773. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5774. }
  5775. }
  5776. if (is_array($arrLP)) {
  5777. reset($arrLP);
  5778. }
  5779. $return .= "\t\t\t\t" . '</select>';
  5780. $return .= '</td>';
  5781. $return .= '</tr>';
  5782. $return .= '<tr>';
  5783. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5784. $return .= '<td class="input">';
  5785. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  5786. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5787. for ($i = 0; $i < count($arrLP); $i++) {
  5788. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5789. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5790. $selected = 'selected="selected" ';
  5791. elseif ($action == 'add') $selected = 'selected="selected" ';
  5792. else
  5793. $selected = '';
  5794. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5795. }
  5796. }
  5797. $return .= "\t\t\t\t" . '</select>';
  5798. $return .= '</td>';
  5799. $return .= '</tr>';
  5800. if ($action != 'move') {
  5801. $return .= '<tr>';
  5802. $return .= '</tr>';
  5803. $id_prerequisite = 0;
  5804. if (is_array($arrLP)) {
  5805. foreach ($arrLP as $key => $value) {
  5806. if ($value['id'] == $id) {
  5807. $id_prerequisite = $value['prerequisite'];
  5808. break;
  5809. }
  5810. }
  5811. }
  5812. $arrHide = array ();
  5813. for ($i = 0; $i < count($arrLP); $i++) {
  5814. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5815. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5816. $s_selected_position = $arrLP[$i]['id'];
  5817. elseif ($action == 'add') $s_selected_position = 0;
  5818. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5819. }
  5820. }
  5821. $return .= '</tr>';
  5822. }
  5823. $return .= '<tr>';
  5824. if ($action == 'add') {
  5825. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  5826. } else {
  5827. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  5828. }
  5829. $return .= '</tr>';
  5830. $return .= '</table>';
  5831. if ($action == 'move') {
  5832. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5833. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5834. }
  5835. if (is_numeric($extra_info)) {
  5836. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5837. }
  5838. elseif (is_array($extra_info)) {
  5839. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5840. }
  5841. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  5842. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5843. $return .= '</form>';
  5844. $return .= '</div>';
  5845. return $return;
  5846. }
  5847. /**
  5848. * Return HTML form to add/edit forum threads
  5849. * @param string Action (add/edit)
  5850. * @param integer Item ID if already exists in learning path
  5851. * @param mixed Extra information (thread ID if integer)
  5852. * @return string HTML form
  5853. */
  5854. public function display_thread_form($action = 'add', $id = 0, $extra_info = '') {
  5855. $course_id = api_get_course_int_id();
  5856. if (empty($course_id)) {
  5857. return null;
  5858. }
  5859. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5860. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  5861. if ($id != 0 && is_array($extra_info)) {
  5862. $item_title = stripslashes($extra_info['title']);
  5863. } elseif (is_numeric($extra_info)) {
  5864. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  5865. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  5866. $result = Database::query($sql_forum);
  5867. $row = Database :: fetch_array($result);
  5868. $item_title = $row['title'];
  5869. $item_description = '';
  5870. } else {
  5871. $item_title = '';
  5872. $item_description = '';
  5873. }
  5874. $return = null;
  5875. if ($id != 0 && is_array($extra_info))
  5876. $parent = $extra_info['parent_item_id'];
  5877. else
  5878. $parent = 0;
  5879. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5880. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5881. $result = Database::query($sql);
  5882. $arrLP = array ();
  5883. while ($row = Database :: fetch_array($result)) {
  5884. $arrLP[] = array (
  5885. 'id' => $row['id'],
  5886. 'item_type' => $row['item_type'],
  5887. 'title' => $row['title'],
  5888. 'path' => $row['path'],
  5889. 'description' => $row['description'],
  5890. 'parent_item_id' => $row['parent_item_id'],
  5891. 'previous_item_id' => $row['previous_item_id'],
  5892. 'next_item_id' => $row['next_item_id'],
  5893. 'display_order' => $row['display_order'],
  5894. 'max_score' => $row['max_score'],
  5895. 'min_score' => $row['min_score'],
  5896. 'mastery_score' => $row['mastery_score'],
  5897. 'prerequisite' => $row['prerequisite']
  5898. );
  5899. }
  5900. $this->tree_array($arrLP);
  5901. $arrLP = $this->arrMenu;
  5902. unset ($this->arrMenu);
  5903. $return .= '<form method="POST">';
  5904. if ($action == 'add')
  5905. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  5906. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  5907. else
  5908. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  5909. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5910. $return .= '<tr>';
  5911. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5912. $return .= '<td class="input">';
  5913. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5914. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5915. $arrHide = array (
  5916. $id
  5917. );
  5918. for ($i = 0; $i < count($arrLP); $i++) {
  5919. if ($action != 'add') {
  5920. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5921. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5922. } else {
  5923. $arrHide[] = $arrLP[$i]['id'];
  5924. }
  5925. } else {
  5926. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5927. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5928. }
  5929. }
  5930. if ($arrLP != null) {
  5931. reset($arrLP);
  5932. }
  5933. $return .= '</select>';
  5934. $return .= '</td>';
  5935. $return .= '</tr>';
  5936. $return .= '<tr>';
  5937. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5938. $return .= '<td class="input">';
  5939. $return .= "\t\t\t\t" . '<select id="previous" name="previous" size="1">';
  5940. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5941. for ($i = 0; $i < count($arrLP); $i++) {
  5942. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5943. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5944. $selected = 'selected="selected" ';
  5945. elseif ($action == 'add') $selected = 'selected="selected" ';
  5946. else
  5947. $selected = '';
  5948. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5949. }
  5950. }
  5951. $return .= "\t\t\t\t" . '</select>';
  5952. $return .= '</td>';
  5953. $return .= '</tr>';
  5954. if ($action != 'move') {
  5955. $return .= '<tr>';
  5956. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5957. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5958. $return .= '</tr>';
  5959. $return .= '<tr>';
  5960. $return .= '</tr>';
  5961. $id_prerequisite = 0;
  5962. if ($arrLP != null) {
  5963. foreach ($arrLP as $key => $value) {
  5964. if ($value['id'] == $id) {
  5965. $id_prerequisite = $value['prerequisite'];
  5966. break;
  5967. }
  5968. }
  5969. }
  5970. $arrHide = array();
  5971. for ($i = 0; $i < count($arrLP); $i++) {
  5972. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5973. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5974. $s_selected_position = $arrLP[$i]['id'];
  5975. elseif ($action == 'add') $s_selected_position = 0;
  5976. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5977. }
  5978. }
  5979. $return .= '<tr>';
  5980. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  5981. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  5982. foreach ($arrHide as $key => $value) {
  5983. if ($key == $s_selected_position && $action == 'add') {
  5984. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5985. }
  5986. elseif ($key == $id_prerequisite && $action == 'edit') {
  5987. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5988. } else {
  5989. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  5990. }
  5991. }
  5992. $return .= "</select></td>";
  5993. $return .= '</tr>';
  5994. }
  5995. $return .= '<tr>';
  5996. $return .= '<td></td><td>
  5997. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  5998. $return .= '</tr>';
  5999. $return .= '</table>';
  6000. if ($action == 'move') {
  6001. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6002. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6003. }
  6004. if (is_numeric($extra_info)) {
  6005. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6006. }
  6007. elseif (is_array($extra_info)) {
  6008. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6009. }
  6010. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6011. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6012. $return .= '</form>';
  6013. $return .= '</div>';
  6014. return $return;
  6015. }
  6016. /**
  6017. * Return the HTML form to display an item (generally a section/module item)
  6018. * @param string Item type (module/dokeos_module)
  6019. * @param string Title (optional, only when creating)
  6020. * @param string Action ('add'/'edit')
  6021. * @param integer lp_item ID
  6022. * @param mixed Extra info
  6023. * @return string HTML form
  6024. */
  6025. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new') {
  6026. $course_id = api_get_course_int_id();
  6027. global $_course;
  6028. global $charset;
  6029. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6030. if ($id != 0 && is_array($extra_info)) {
  6031. $item_title = $extra_info['title'];
  6032. $item_description = $extra_info['description'];
  6033. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6034. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6035. } else {
  6036. $item_title = '';
  6037. $item_description = '';
  6038. $item_path_fck = '';
  6039. }
  6040. if ($id != 0 && is_array($extra_info))
  6041. $parent = $extra_info['parent_item_id'];
  6042. else
  6043. $parent = 0;
  6044. $id = intval($id);
  6045. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6046. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND id != $id";
  6047. if ($item_type == 'module')
  6048. $sql .= " AND parent_item_id = 0";
  6049. $result = Database::query($sql);
  6050. $arrLP = array ();
  6051. while ($row = Database :: fetch_array($result)) {
  6052. $arrLP[] = array (
  6053. 'id' => $row['id'],
  6054. 'item_type' => $row['item_type'],
  6055. 'title' => $row['title'],
  6056. 'path' => $row['path'],
  6057. 'description' => $row['description'],
  6058. 'parent_item_id' => $row['parent_item_id'],
  6059. 'previous_item_id' => $row['previous_item_id'],
  6060. 'next_item_id' => $row['next_item_id'],
  6061. 'max_score' => $row['max_score'],
  6062. 'min_score' => $row['min_score'],
  6063. 'mastery_score' => $row['mastery_score'],
  6064. 'prerequisite' => $row['prerequisite'],
  6065. 'display_order' => $row['display_order']
  6066. );
  6067. }
  6068. $this->tree_array($arrLP);
  6069. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6070. unset ($this->arrMenu);
  6071. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6072. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6073. $form = new FormValidator('form', 'POST', $url);
  6074. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6075. $defaults['description'] = $item_description;
  6076. $form->addElement('header', $title);
  6077. //$arrHide = array($id);
  6078. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6079. $arrHide[0]['padding'] = 3;
  6080. $charset = api_get_system_encoding();
  6081. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6082. for ($i = 0; $i < count($arrLP); $i++) {
  6083. if ($action != 'add') {
  6084. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6085. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6086. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6087. if ($parent == $arrLP[$i]['id']) {
  6088. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6089. }
  6090. }
  6091. } else {
  6092. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6093. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6094. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6095. if ($parent == $arrLP[$i]['id']) {
  6096. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6097. }
  6098. }
  6099. }
  6100. }
  6101. if ($action != 'move') {
  6102. $form->addElement('text', 'title', get_lang('Title'), 'id="idTitle" class="learnpath_chapter_form" size="40%"');
  6103. $form->applyFilter('title', 'html_filter');
  6104. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6105. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6106. } else {
  6107. $form->addElement('hidden', 'title');
  6108. }
  6109. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"');
  6110. foreach ($arrHide as $key => $value) {
  6111. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6112. }
  6113. if (!empty($s_selected_parent)) {
  6114. $parent_select->setSelected($s_selected_parent);
  6115. }
  6116. }
  6117. if (is_array($arrLP)) {
  6118. reset($arrLP);
  6119. }
  6120. $arrHide = array();
  6121. // POSITION
  6122. for ($i = 0; $i < count($arrLP); $i++) {
  6123. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6124. //this is the same!
  6125. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6126. $s_selected_position = $arrLP[$i]['id'];
  6127. } elseif ($action == 'add') {
  6128. $s_selected_position = $arrLP[$i]['id'];
  6129. }
  6130. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6131. }
  6132. }
  6133. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_chapter_form" style="width:37%;"');
  6134. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6135. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6136. foreach ($arrHide as $key => $value) {
  6137. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6138. }
  6139. if (!empty ($s_selected_position)) {
  6140. $position->setSelected($s_selected_position);
  6141. }
  6142. if (is_array($arrLP)) {
  6143. reset($arrLP);
  6144. }
  6145. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6146. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6147. $form->addElement('hidden', 'parent', '0');
  6148. }
  6149. //fix in order to use the tab
  6150. if ($item_type == 'chapter') {
  6151. $form->addElement('hidden', 'type', 'chapter');
  6152. }
  6153. $extension = null;
  6154. if (!empty($item_path)) {
  6155. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6156. }
  6157. //assets can't be modified
  6158. //$item_type == 'asset' ||
  6159. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6160. if ($item_type == 'sco') {
  6161. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6162. }
  6163. $renderer = $form->defaultRenderer();
  6164. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6165. $relative_prefix = '';
  6166. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6167. 'Width' => '100%',
  6168. 'Height' => '500',
  6169. 'FullPage' => true,
  6170. 'CreateDocumentDir' => $relative_prefix,
  6171. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6172. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6173. );
  6174. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6175. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6176. //$defaults['content_lp'] = file_get_contents($item_path);
  6177. $defaults['content_lp'] = file_get_contents($content_path);
  6178. }
  6179. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6180. $form->addElement('hidden', 'post_time', time());
  6181. $form->setDefaults($defaults);
  6182. return $form->return_form();
  6183. }
  6184. /**
  6185. * Returns the form to update or create a document
  6186. * @param string Action (add/edit)
  6187. * @param integer ID of the lp_item (if already exists)
  6188. * @param mixed Integer if document ID, string if info ('new')
  6189. * @return string HTML form
  6190. */
  6191. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new') {
  6192. $course_id = api_get_course_int_id();
  6193. global $charset;
  6194. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6195. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6196. $no_display_edit_textarea = false;
  6197. //If action==edit document
  6198. //We don't display the document form if it's not an editable document (html or txt file)
  6199. if ($action == "edit") {
  6200. if (is_array($extra_info)) {
  6201. $path_parts = pathinfo($extra_info['dir']);
  6202. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6203. $no_display_edit_textarea = true;
  6204. }
  6205. }
  6206. }
  6207. $no_display_add = false;
  6208. // If action==add an existing document
  6209. // We don't display the document form if it's not an editable document (html or txt file).
  6210. if ($action == "add") {
  6211. if (is_numeric($extra_info)) {
  6212. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6213. $result = Database::query($sql_doc);
  6214. $path_file = Database :: result($result, 0, 0);
  6215. $path_parts = pathinfo($path_file);
  6216. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6217. $no_display_add = true;
  6218. }
  6219. }
  6220. }
  6221. if ($id != 0 && is_array($extra_info)) {
  6222. $item_title = stripslashes($extra_info['title']);
  6223. $item_description = stripslashes($extra_info['description']);
  6224. $item_terms = stripslashes($extra_info['terms']);
  6225. if (empty ($item_title)) {
  6226. $path_parts = pathinfo($extra_info['path']);
  6227. $item_title = stripslashes($path_parts['filename']);
  6228. }
  6229. } elseif (is_numeric($extra_info)) {
  6230. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6231. WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6232. $result = Database::query($sql_doc);
  6233. $row = Database::fetch_array($result);
  6234. $explode = explode('.', $row['title']);
  6235. if (count($explode) > 1) {
  6236. for ($i = 0; $i < count($explode) - 1; $i++)
  6237. $item_title .= $explode[$i];
  6238. } else {
  6239. $item_title = $row['title'];
  6240. }
  6241. $item_title = str_replace('_', ' ', $item_title);
  6242. if (empty ($item_title)) {
  6243. $path_parts = pathinfo($row['path']);
  6244. $item_title = stripslashes($path_parts['filename']);
  6245. }
  6246. } else {
  6247. $item_title = '';
  6248. $item_description = '';
  6249. }
  6250. $return = '<legend>';
  6251. if ($id != 0 && is_array($extra_info))
  6252. $parent = $extra_info['parent_item_id'];
  6253. else
  6254. $parent = 0;
  6255. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6256. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6257. $result = Database::query($sql);
  6258. $arrLP = array ();
  6259. while ($row = Database :: fetch_array($result)) {
  6260. $arrLP[] = array (
  6261. 'id' => $row['id'],
  6262. 'item_type' => $row['item_type'],
  6263. 'title' => $row['title'],
  6264. 'path' => $row['path'],
  6265. 'description' => $row['description'],
  6266. 'parent_item_id' => $row['parent_item_id'],
  6267. 'previous_item_id' => $row['previous_item_id'],
  6268. 'next_item_id' => $row['next_item_id'],
  6269. 'display_order' => $row['display_order'],
  6270. 'max_score' => $row['max_score'],
  6271. 'min_score' => $row['min_score'],
  6272. 'mastery_score' => $row['mastery_score'],
  6273. 'prerequisite' => $row['prerequisite']
  6274. );
  6275. }
  6276. $this->tree_array($arrLP);
  6277. $arrLP = $this->arrMenu;
  6278. unset ($this->arrMenu);
  6279. if ($action == 'add') {
  6280. $return .= get_lang('CreateTheDocument');
  6281. } elseif ($action == 'move') {
  6282. $return .= get_lang('MoveTheCurrentDocument');
  6283. } else {
  6284. $return .= get_lang('EditTheCurrentDocument');
  6285. }
  6286. $return .= '</legend>';
  6287. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6288. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6289. }
  6290. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6291. $defaults['title'] = Security :: remove_XSS($item_title);
  6292. if (empty($item_title)) {
  6293. $defaults['title'] = Security::remove_XSS($item_title);
  6294. }
  6295. $defaults['description'] = $item_description;
  6296. $form->addElement('html', $return);
  6297. if ($action != 'move') {
  6298. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6299. $form->applyFilter('title', 'html_filter');
  6300. }
  6301. //$arrHide = array($id);
  6302. $arrHide[0]['value'] = $this->name;
  6303. $arrHide[0]['padding'] = 3;
  6304. for ($i = 0; $i < count($arrLP); $i++) {
  6305. if ($action != 'add') {
  6306. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6307. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6308. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6309. if ($parent == $arrLP[$i]['id']) {
  6310. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6311. }
  6312. }
  6313. } else {
  6314. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6315. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6316. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6317. if ($parent == $arrLP[$i]['id']) {
  6318. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6319. }
  6320. }
  6321. }
  6322. }
  6323. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6324. $my_count=0;
  6325. foreach ($arrHide as $key => $value) {
  6326. if ($my_count!=0) {
  6327. // The LP name is also the first section and is not in the same charset like the other sections.
  6328. $value['value'] = Security :: remove_XSS($value['value']);
  6329. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6330. } else {
  6331. $value['value'] = Security :: remove_XSS($value['value']);
  6332. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6333. }
  6334. $my_count++;
  6335. }
  6336. if (!empty($id)) {
  6337. $parent_select->setSelected($parent);
  6338. } else {
  6339. $parent_item_id = $_SESSION['parent_item_id'];
  6340. $parent_select->setSelected($parent_item_id);
  6341. }
  6342. if (is_array($arrLP)) {
  6343. reset($arrLP);
  6344. }
  6345. $arrHide = array ();
  6346. //POSITION
  6347. for ($i = 0; $i < count($arrLP); $i++) {
  6348. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6349. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6350. $s_selected_position = $arrLP[$i]['id'];
  6351. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6352. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6353. }
  6354. }
  6355. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6356. $position->addOption(get_lang('FirstPosition'), 0);
  6357. foreach ($arrHide as $key => $value) {
  6358. $padding = isset($value['padding']) ? $value['padding']: 0;
  6359. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6360. }
  6361. $position->setSelected($s_selected_position);
  6362. if (is_array($arrLP)) {
  6363. reset($arrLP);
  6364. }
  6365. if ($action != 'move') {
  6366. $id_prerequisite = 0;
  6367. if (is_array($arrLP)) {
  6368. foreach ($arrLP as $key => $value) {
  6369. if ($value['id'] == $id) {
  6370. $id_prerequisite = $value['prerequisite'];
  6371. break;
  6372. }
  6373. }
  6374. }
  6375. $arrHide = array ();
  6376. for ($i = 0; $i < count($arrLP); $i++) {
  6377. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6378. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6379. $s_selected_position = $arrLP[$i]['id'];
  6380. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6381. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6382. }
  6383. }
  6384. if (!$no_display_add) {
  6385. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6386. if (isset ($_POST['content']))
  6387. $content = stripslashes($_POST['content']);
  6388. elseif (is_array($extra_info)) {
  6389. //If it's an html document or a text file
  6390. if (!$no_display_edit_textarea) {
  6391. $content = $this->display_document($extra_info['path'], false, false);
  6392. }
  6393. } elseif (is_numeric($extra_info))
  6394. $content = $this->display_document($extra_info, false, false);
  6395. else
  6396. $content = '';
  6397. if (!$no_display_edit_textarea) {
  6398. // We need to calculate here some specific settings for the online editor.
  6399. // The calculated settings work for documents in the Documents tool
  6400. // (on the root or in subfolders).
  6401. // For documents in native scorm packages it is unclear whether the
  6402. // online editor should be activated or not.
  6403. // A new document, it is in the root of the repository.
  6404. $relative_path = '';
  6405. $relative_prefix = '';
  6406. if (is_array($extra_info) && $extra_info != 'new') {
  6407. // The document already exists. Whe have to determine its relative path towards the repository root.
  6408. $relative_path = explode('/', $extra_info['dir']);
  6409. $cnt = count($relative_path) - 2;
  6410. if ($cnt < 0) {
  6411. $cnt = 0;
  6412. }
  6413. $relative_prefix = str_repeat('../', $cnt);
  6414. $relative_path = array_slice($relative_path, 1, $cnt);
  6415. $relative_path = implode('/', $relative_path);
  6416. if (strlen($relative_path) > 0) {
  6417. $relative_path = $relative_path . '/';
  6418. }
  6419. } else {
  6420. global $_course;
  6421. $result = $this->generate_lp_folder($_course);
  6422. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6423. $relative_prefix = '../../';
  6424. }
  6425. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6426. 'Width' => '100%',
  6427. 'Height' => '500',
  6428. 'FullPage' => true,
  6429. 'CreateDocumentDir' => $relative_prefix,
  6430. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6431. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6432. );
  6433. if ($_GET['action'] == 'add_item') {
  6434. $class = 'add';
  6435. $text = get_lang('LPCreateDocument');
  6436. } else
  6437. if ($_GET['action'] == 'edit_item') {
  6438. $class = 'save';
  6439. $text = get_lang('SaveDocument');
  6440. }
  6441. $form->addElement('style_submit_button', 'submit_button', $text, 'class="' . $class . '"');
  6442. $renderer = $form->defaultRenderer();
  6443. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6444. $form->addElement('html', '<div>');
  6445. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6446. $form->addElement('html', '</div>');
  6447. $defaults['content_lp'] = $content;
  6448. }
  6449. } elseif (is_numeric($extra_info)) {
  6450. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6451. $return = $this->display_document($extra_info, true, true, true);
  6452. $form->addElement('html', $return);
  6453. }
  6454. }
  6455. }
  6456. if ($action == 'move') {
  6457. $form->addElement('hidden', 'title', $item_title);
  6458. $form->addElement('hidden', 'description', $item_description);
  6459. }
  6460. if (is_numeric($extra_info)) {
  6461. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'value="submit_button", class="save"');
  6462. $form->addElement('hidden', 'path', $extra_info);
  6463. } elseif (is_array($extra_info)) {
  6464. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6465. $form->addElement('hidden', 'path', $extra_info['path']);
  6466. }
  6467. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6468. $form->addElement('hidden', 'post_time', time());
  6469. $form->setDefaults($defaults);
  6470. return $form->return_form();
  6471. }
  6472. /**
  6473. * Return HTML form to add/edit a link item
  6474. * @param string Action (add/edit)
  6475. * @param integer Item ID if exists
  6476. * @param mixed Extra info
  6477. * @return string HTML form
  6478. */
  6479. public function display_link_form($action = 'add', $id = 0, $extra_info = '') {
  6480. $course_id = api_get_course_int_id();
  6481. global $charset;
  6482. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6483. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6484. if ($id != 0 && is_array($extra_info)) {
  6485. $item_title = stripslashes($extra_info['title']);
  6486. $item_description = stripslashes($extra_info['description']);
  6487. $item_url = stripslashes($extra_info['url']);
  6488. } elseif (is_numeric($extra_info)) {
  6489. $sql_link = "SELECT title, description, url FROM " . $tbl_link . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6490. $result = Database::query($sql_link);
  6491. $row = Database :: fetch_array($result);
  6492. $item_title = $row['title'];
  6493. $item_description = $row['description'];
  6494. $item_url = $row['url'];
  6495. } else {
  6496. $item_title = '';
  6497. $item_description = '';
  6498. $item_url = '';
  6499. }
  6500. $legend = '<legend>';
  6501. if ($id != 0 && is_array($extra_info))
  6502. $parent = $extra_info['parent_item_id'];
  6503. else
  6504. $parent = 0;
  6505. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6506. $result = Database::query($sql);
  6507. $arrLP = array ();
  6508. while ($row = Database :: fetch_array($result)) {
  6509. $arrLP[] = array (
  6510. 'id' => $row['id'],
  6511. 'item_type' => $row['item_type'],
  6512. 'title' => $row['title'],
  6513. 'path' => $row['path'],
  6514. 'description' => $row['description'],
  6515. 'parent_item_id' => $row['parent_item_id'],
  6516. 'previous_item_id' => $row['previous_item_id'],
  6517. 'next_item_id' => $row['next_item_id'],
  6518. 'display_order' => $row['display_order'],
  6519. 'max_score' => $row['max_score'],
  6520. 'min_score' => $row['min_score'],
  6521. 'mastery_score' => $row['mastery_score'],
  6522. 'prerequisite' => $row['prerequisite']
  6523. );
  6524. }
  6525. $this->tree_array($arrLP);
  6526. $arrLP = $this->arrMenu;
  6527. unset ($this->arrMenu);
  6528. if ($action == 'add')
  6529. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6530. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6531. else
  6532. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  6533. $legend .= '</legend>';
  6534. $return .= '<div class="sectioncomment">';
  6535. $return .= '<form method="POST">';
  6536. $return .= $legend;
  6537. $return .= '<table>';
  6538. if ($action != 'move') {
  6539. $return .= '<tr>';
  6540. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6541. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  6542. $return .= '</tr>';
  6543. }
  6544. $return .= '<tr>';
  6545. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6546. $return .= '<td class="input">';
  6547. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6548. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6549. $arrHide = array (
  6550. $id
  6551. );
  6552. $parent_item_id = $_SESSION['parent_item_id'];
  6553. for ($i = 0; $i < count($arrLP); $i++) {
  6554. if ($action != 'add') {
  6555. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6556. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6557. } else {
  6558. $arrHide[] = $arrLP[$i]['id'];
  6559. }
  6560. } else {
  6561. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6562. $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>';
  6563. }
  6564. }
  6565. if (is_array($arrLP)) {
  6566. reset($arrLP);
  6567. }
  6568. $return .= '</select>';
  6569. $return .= '</td>';
  6570. $return .= '</tr>';
  6571. $return .= '<tr>';
  6572. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6573. $return .= '<td class="input">';
  6574. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6575. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6576. for ($i = 0; $i < count($arrLP); $i++) {
  6577. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  6578. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6579. $selected = 'selected="selected" ';
  6580. elseif ($action == 'add')
  6581. $selected = 'selected="selected" ';
  6582. else
  6583. $selected = '';
  6584. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6585. }
  6586. }
  6587. $return .= '</select>';
  6588. $return .= '</td>';
  6589. $return .= '</tr>';
  6590. if ($action != 'move') {
  6591. $return .= '<tr>';
  6592. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  6593. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  6594. $return .= '</tr>';
  6595. $id_prerequisite = 0;
  6596. if (is_array($arrLP)) {
  6597. foreach ($arrLP as $key => $value) {
  6598. if ($value['id'] == $id) {
  6599. $id_prerequisite = $value['prerequisite'];
  6600. break;
  6601. }
  6602. }
  6603. }
  6604. $arrHide = array();
  6605. for ($i = 0; $i < count($arrLP); $i++) {
  6606. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6607. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6608. $s_selected_position = $arrLP[$i]['id'];
  6609. elseif ($action == 'add') $s_selected_position = 0;
  6610. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6611. }
  6612. }
  6613. $return .= '</tr>';
  6614. }
  6615. $return .= '<tr>';
  6616. if ($action == 'add') {
  6617. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  6618. } else {
  6619. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  6620. }
  6621. $return .= '</tr>';
  6622. $return .= '</table>';
  6623. if ($action == 'move') {
  6624. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6625. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6626. }
  6627. if (is_numeric($extra_info)) {
  6628. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6629. } elseif (is_array($extra_info)) {
  6630. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6631. }
  6632. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  6633. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6634. $return .= '</form>';
  6635. $return .= '</div>';
  6636. return $return;
  6637. }
  6638. /**
  6639. * Return HTML form to add/edit a student publication (work)
  6640. * @param string Action (add/edit)
  6641. * @param integer Item ID if already exists
  6642. * @param mixed Extra info (work ID if integer)
  6643. * @return string HTML form
  6644. */
  6645. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '') {
  6646. $course_id = api_get_course_int_id();
  6647. global $charset;
  6648. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6649. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  6650. if ($id != 0 && is_array($extra_info)) {
  6651. $item_title = stripslashes($extra_info['title']);
  6652. $item_description = stripslashes($extra_info['description']);
  6653. }
  6654. elseif (is_numeric($extra_info)) {
  6655. $sql_publication = "SELECT title, description FROM " . $tbl_publication . "
  6656. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6657. $result = Database::query($sql_publication);
  6658. $row = Database :: fetch_array($result);
  6659. $item_title = $row['title'];
  6660. } else {
  6661. $item_title = get_lang('Student_publication');
  6662. }
  6663. $legend = '<legend>';
  6664. if ($id != 0 && is_array($extra_info))
  6665. $parent = $extra_info['parent_item_id'];
  6666. else
  6667. $parent = 0;
  6668. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6669. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6670. $result = Database::query($sql);
  6671. $arrLP = array ();
  6672. while ($row = Database :: fetch_array($result)) {
  6673. $arrLP[] = array (
  6674. 'id' => $row['id'],
  6675. 'item_type' => $row['item_type'],
  6676. 'title' => $row['title'],
  6677. 'path' => $row['path'],
  6678. 'description' => $row['description'],
  6679. 'parent_item_id' => $row['parent_item_id'],
  6680. 'previous_item_id' => $row['previous_item_id'],
  6681. 'next_item_id' => $row['next_item_id'],
  6682. 'display_order' => $row['display_order'],
  6683. 'max_score' => $row['max_score'],
  6684. 'min_score' => $row['min_score'],
  6685. 'mastery_score' => $row['mastery_score'],
  6686. 'prerequisite' => $row['prerequisite']
  6687. );
  6688. }
  6689. $this->tree_array($arrLP);
  6690. $arrLP = $this->arrMenu;
  6691. unset ($this->arrMenu);
  6692. if ($action == 'add')
  6693. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  6694. elseif ($action == 'move') $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  6695. else
  6696. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  6697. $legend .= '</legend>';
  6698. $return .= '<div class="sectioncomment">';
  6699. $return .= '<form method="POST">';
  6700. $return .= $legend;
  6701. $return .= '<table class="lp_form">';
  6702. if ($action != 'move') {
  6703. $return .= '<tr>';
  6704. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6705. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6706. $return .= '</tr>';
  6707. }
  6708. $return .= '<tr>';
  6709. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6710. $return .= '<td class="input">';
  6711. $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">';
  6712. //$parent_item_id = $_SESSION['parent_item_id'];
  6713. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6714. $arrHide = array (
  6715. $id
  6716. );
  6717. for ($i = 0; $i < count($arrLP); $i++) {
  6718. if ($action != 'add') {
  6719. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6720. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6721. } else {
  6722. $arrHide[] = $arrLP[$i]['id'];
  6723. }
  6724. } else {
  6725. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6726. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6727. }
  6728. }
  6729. if (is_array($arrLP)) {
  6730. reset($arrLP);
  6731. }
  6732. $return .= "\t\t\t\t" . '</select>';
  6733. $return .= '</td>';
  6734. $return .= '</tr>';
  6735. $return .= '<tr>';
  6736. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6737. $return .= '<td class="input">';
  6738. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6739. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6740. for ($i = 0; $i < count($arrLP); $i++) {
  6741. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6742. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6743. $selected = 'selected="selected" ';
  6744. elseif ($action == 'add') $selected = 'selected="selected" ';
  6745. else
  6746. $selected = '';
  6747. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6748. }
  6749. }
  6750. $return .= "\t\t\t\t" . '</select>';
  6751. $return .= '</td>';
  6752. $return .= '</tr>';
  6753. if ($action != 'move') {
  6754. $id_prerequisite = 0;
  6755. if (is_array($arrLP)) {
  6756. foreach ($arrLP as $key => $value) {
  6757. if ($value['id'] == $id) {
  6758. $id_prerequisite = $value['prerequisite'];
  6759. break;
  6760. }
  6761. }
  6762. }
  6763. $arrHide = array ();
  6764. for ($i = 0; $i < count($arrLP); $i++) {
  6765. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6766. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6767. $s_selected_position = $arrLP[$i]['id'];
  6768. elseif ($action == 'add') $s_selected_position = 0;
  6769. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6770. }
  6771. }
  6772. // Commented the prerequisites, only visible in edit (work).
  6773. /*
  6774. $return .= '<tr>';
  6775. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6776. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6777. foreach($arrHide as $key => $value) {
  6778. if ($key == $s_selected_position && $action == 'add') {
  6779. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6780. }
  6781. elseif ($key == $id_prerequisite && $action == 'edit') {
  6782. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6783. }
  6784. else {
  6785. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6786. }
  6787. }
  6788. $return .= "</select></td>";
  6789. */
  6790. $return .= '</tr>';
  6791. }
  6792. $return .= '<tr>';
  6793. if ($action == 'add') {
  6794. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  6795. } else {
  6796. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  6797. }
  6798. $return .= '</tr>';
  6799. $return .= '</table>';
  6800. if ($action == 'move') {
  6801. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6802. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6803. }
  6804. if (is_numeric($extra_info)) {
  6805. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6806. } elseif (is_array($extra_info)) {
  6807. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6808. }
  6809. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  6810. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6811. $return .= '</form>';
  6812. $return .= '</div>';
  6813. return $return;
  6814. }
  6815. /**
  6816. * Displays the menu for manipulating a step
  6817. * @return string html
  6818. */
  6819. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT) {
  6820. $course_id = api_get_course_int_id();
  6821. $course_code = api_get_course_id();
  6822. global $charset, $_course;
  6823. $return = '<div class="actions">';
  6824. switch ($item_type) {
  6825. case 'dokeos_chapter' :
  6826. case 'chapter' :
  6827. // Commented the message cause should not show it.
  6828. //$lang = get_lang('TitleManipulateChapter');
  6829. break;
  6830. case 'dokeos_module' :
  6831. case 'module' :
  6832. // Commented the message cause should not show it.
  6833. //$lang = get_lang('TitleManipulateModule');
  6834. break;
  6835. case TOOL_DOCUMENT :
  6836. // Commented the message cause should not show it.
  6837. //$lang = get_lang('TitleManipulateDocument');
  6838. break;
  6839. case TOOL_LINK :
  6840. case 'link' :
  6841. // Commented the message cause should not show it.
  6842. //$lang = get_lang('TitleManipulateLink');
  6843. break;
  6844. case TOOL_QUIZ :
  6845. // Commented the message cause should not show it.
  6846. //$lang = get_lang('TitleManipulateQuiz');
  6847. break;
  6848. case TOOL_STUDENTPUBLICATION :
  6849. // Commented the message cause should not show it.
  6850. //$lang = get_lang('TitleManipulateStudentPublication');
  6851. break;
  6852. }
  6853. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6854. $item_id = intval($item_id);
  6855. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  6856. $result = Database::query($sql);
  6857. $row = Database::fetch_assoc($result);
  6858. $audio_player = null;
  6859. // We display an audio player if needed.
  6860. if (!empty($row['audio'])) {
  6861. $audio_player .= '<div class="lp_mediaplayer" id="container"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  6862. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  6863. $audio_player .= '<script>
  6864. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  6865. s1.addParam("allowscriptaccess","always");
  6866. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  6867. s1.write("container");
  6868. </script>';
  6869. }
  6870. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  6871. $return .= Display::url(Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item&path_item=' . $row['path']);
  6872. $return .= Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL), $url.'&action=move_item');
  6873. // Commented for now as prerequisites cannot be added to chapters.
  6874. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  6875. $return .= Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item_prereq');
  6876. }
  6877. $return .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), $url.'&action=delete_item');
  6878. if ($item_type == TOOL_HOTPOTATOES ) {
  6879. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6880. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6881. }
  6882. if ($item_type == TOOL_DOCUMENT ) {
  6883. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6884. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6885. }
  6886. $return .= '</div>';
  6887. if (!empty($audio_player)) {
  6888. $return .= '<br />'.$audio_player;
  6889. }
  6890. return $return;
  6891. }
  6892. /**
  6893. * Creates the javascript needed for filling up the checkboxes without page reload
  6894. *
  6895. * @return string
  6896. */
  6897. public function get_js_dropdown_array() {
  6898. $course_id = api_get_course_int_id();
  6899. $return = 'var child_name = new Array();' . "\n";
  6900. $return .= 'var child_value = new Array();' . "\n\n";
  6901. $return .= 'child_name[0] = new Array();' . "\n";
  6902. $return .= 'child_value[0] = new Array();' . "\n\n";
  6903. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6904. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  6905. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  6906. ORDER BY display_order ASC";
  6907. $res_zero = Database::query($sql_zero);
  6908. global $charset;
  6909. $i = 0;
  6910. while ($row_zero = Database :: fetch_array($res_zero)) {
  6911. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  6912. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  6913. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  6914. }
  6915. $return .= "\n";
  6916. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6917. $res = Database::query($sql);
  6918. while ($row = Database :: fetch_array($res)) {
  6919. $sql_parent = "
  6920. SELECT * FROM " . $tbl_lp_item . "
  6921. WHERE c_id = ".$course_id." AND parent_item_id = " . $row['id'] . "
  6922. ORDER BY display_order ASC";
  6923. $res_parent = Database::query($sql_parent);
  6924. $i = 0;
  6925. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  6926. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  6927. while ($row_parent = Database :: fetch_array($res_parent)) {
  6928. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  6929. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  6930. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  6931. }
  6932. $return .= "\n";
  6933. }
  6934. return $return;
  6935. }
  6936. /**
  6937. * Display the form to allow moving an item
  6938. * @param integer Item ID
  6939. * @return string HTML form
  6940. */
  6941. public function display_move_item($item_id) {
  6942. $course_id = api_get_course_int_id();
  6943. global $_course; //will disappear
  6944. global $charset;
  6945. $return = '';
  6946. if (is_numeric($item_id)) {
  6947. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6948. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6949. WHERE c_id = ".$course_id." AND id = " . $item_id;
  6950. $res = Database::query($sql);
  6951. $row = Database :: fetch_array($res);
  6952. switch ($row['item_type']) {
  6953. case 'dokeos_chapter' :
  6954. case 'dir' :
  6955. case 'asset' :
  6956. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6957. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  6958. break;
  6959. case 'dokeos_module' :
  6960. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6961. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  6962. break;
  6963. case TOOL_DOCUMENT :
  6964. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6965. $return .= $this->display_document_form('move', $item_id, $row);
  6966. break;
  6967. case TOOL_LINK :
  6968. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6969. $return .= $this->display_link_form('move', $item_id, $row);
  6970. break;
  6971. case TOOL_HOTPOTATOES :
  6972. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6973. $return .= $this->display_link_form('move', $item_id, $row);
  6974. break;
  6975. case TOOL_QUIZ :
  6976. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6977. $return .= $this->display_quiz_form('move', $item_id, $row);
  6978. break;
  6979. case TOOL_STUDENTPUBLICATION :
  6980. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6981. $return .= $this->display_student_publication_form('move', $item_id, $row);
  6982. break;
  6983. case TOOL_FORUM :
  6984. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6985. $return .= $this->display_forum_form('move', $item_id, $row);
  6986. break;
  6987. case TOOL_THREAD :
  6988. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6989. $return .= $this->display_forum_form('move', $item_id, $row);
  6990. break;
  6991. }
  6992. }
  6993. return $return;
  6994. }
  6995. /**
  6996. * Displays a basic form on the overview page for changing the item title and the item description.
  6997. * @param string $item_type
  6998. * @param string $title
  6999. * @param array $data
  7000. * @return string
  7001. */
  7002. public function display_item_small_form($item_type, $title = '', $data = array()) {
  7003. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7004. $form = new FormValidator('small_form', 'post', $url);
  7005. $form->addElement('header', $title);
  7006. $form->addElement('text', 'title', get_lang('Title'));
  7007. $form->addElement('button', 'submit_button', get_lang('Save'));
  7008. $form->addElement('hidden', 'id', $data['id']);
  7009. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7010. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7011. $form->setDefaults(array('title' => $data['title']));
  7012. return $form->toHtml();
  7013. }
  7014. /**
  7015. * Return HTML form to allow prerequisites selection
  7016. * @param integer Item ID
  7017. * @return string HTML form
  7018. */
  7019. public function display_item_prerequisites_form($item_id) {
  7020. $course_id = api_get_course_int_id();
  7021. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7022. $item_id = intval($item_id);
  7023. /* Current prerequisite */
  7024. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = " . $item_id;
  7025. $result = Database::query($sql);
  7026. $row = Database::fetch_array($result);
  7027. $preq_id = $row['prerequisite'];
  7028. //$preq_mastery = $row['mastery_score'];
  7029. //$preq_max = $row['max_score'];
  7030. $return = $this->display_manipulate($item_id, TOOL_DOCUMENT);
  7031. $return = '<legend>';
  7032. $return .= get_lang('AddEditPrerequisites');
  7033. $return .= '</legend>';
  7034. $return .= '<form method="POST">';
  7035. $return .= '<table class="data_table">';
  7036. $return .= '<tr>';
  7037. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7038. $return .= '<th width="70" height="24">' . get_lang('Minimum') . '</th>';
  7039. $return .= '<th width="70" height="24">' . get_lang('Maximum') . '</th>';
  7040. $return .= '</tr>';
  7041. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7042. $return .= '<tr >';
  7043. $return .= '<td colspan="3" class="radio">';
  7044. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7045. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7046. $return .= '</tr>';
  7047. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7048. $result = Database::query($sql);
  7049. $arrLP = array ();
  7050. while ($row = Database :: fetch_array($result)) {
  7051. $arrLP[] = array (
  7052. 'id' => $row['id'],
  7053. 'item_type' => $row['item_type'],
  7054. 'title' => $row['title'],
  7055. 'ref' => $row['ref'],
  7056. 'description' => $row['description'],
  7057. 'parent_item_id' => $row['parent_item_id'],
  7058. 'previous_item_id' => $row['previous_item_id'],
  7059. 'next_item_id' => $row['next_item_id'],
  7060. 'max_score' => $row['max_score'],
  7061. 'min_score' => $row['min_score'],
  7062. 'mastery_score' => $row['mastery_score'],
  7063. 'prerequisite' => $row['prerequisite'],
  7064. 'next_item_id' => $row['next_item_id'],
  7065. 'display_order' => $row['display_order']
  7066. );
  7067. if ($row['ref'] == $preq_id) {
  7068. $preq_mastery = $row['mastery_score'];
  7069. $preq_max = $row['max_score'];
  7070. }
  7071. }
  7072. $this->tree_array($arrLP);
  7073. $arrLP = $this->arrMenu;
  7074. unset ($this->arrMenu);
  7075. for ($i = 0; $i < count($arrLP); $i++) {
  7076. if ($arrLP[$i]['id'] == $item_id)
  7077. break;
  7078. $return .= '<tr>';
  7079. $return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7080. $return .= '<label for="id' . $arrLP[$i]['id'] . '">';
  7081. $return .= '<input' . (($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '') . (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $arrLP[$i]['id'] . '" name="prerequisites" style="margin-left:' . $arrLP[$i]['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $arrLP[$i]['id'] . '" />';
  7082. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  7083. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7084. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7085. } else
  7086. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7087. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7088. } else {
  7089. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7090. }
  7091. $return .= $arrLP[$i]['title'] . '</label>';
  7092. $return .= '</td>';
  7093. //$return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . ' />';
  7094. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  7095. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7096. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7097. $return .= '</td>';
  7098. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7099. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7100. $return .= '</td>';
  7101. }
  7102. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  7103. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7104. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7105. $return .= '</td>';
  7106. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7107. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7108. $return .= '</td>';
  7109. }
  7110. $return .= '</tr>';
  7111. }
  7112. $return .= '<tr>';
  7113. $return .= '</tr>';
  7114. $return .= '</table>';
  7115. $return .= '<div style="padding-top:3px;">';
  7116. $return .= '<button class="save" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7117. $return .= '</form>';
  7118. return $return;
  7119. }
  7120. /**
  7121. * Return HTML list to allow prerequisites selection for lp
  7122. * @param integer Item ID
  7123. * @return string HTML form
  7124. */
  7125. public function display_lp_prerequisites_list() {
  7126. $course_id = api_get_course_int_id();
  7127. $lp_id = $this->lp_id;
  7128. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7129. // get current prerequisite
  7130. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7131. $result = Database::query($sql);
  7132. $row = Database :: fetch_array($result);
  7133. $preq_id = $row['prerequisite'];
  7134. $session_id = api_get_session_id();
  7135. $session_condition = api_get_session_condition($session_id);
  7136. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  7137. $rs = Database::query($sql);
  7138. $return = '';
  7139. $return .= '<select name="prerequisites" >';
  7140. $return .= '<option value="0">'.get_lang('None').'</option>';
  7141. if (Database::num_rows($rs) > 0) {
  7142. while ($row = Database::fetch_array($rs)) {
  7143. if ($row['id'] == $lp_id) {
  7144. continue;
  7145. }
  7146. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$preq_id)?' selected ' : '').'>'.$row['name'].'</option>';
  7147. }
  7148. }
  7149. $return .= '</select>';
  7150. return $return;
  7151. }
  7152. /**
  7153. * Creates a list with all the documents in it
  7154. * @return string
  7155. */
  7156. public function get_documents() {
  7157. $course_info = api_get_course_info();
  7158. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  7159. return $document_tree;
  7160. }
  7161. /**
  7162. * Creates a list with all the exercises (quiz) in it
  7163. * @return string
  7164. */
  7165. public function get_exercises() {
  7166. $course_id = api_get_course_int_id();
  7167. // New for hotpotatoes.
  7168. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7169. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7170. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7171. $session_id = api_get_session_id();
  7172. $condition_session = api_get_session_condition($session_id);
  7173. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  7174. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session ORDER BY id ASC";
  7175. $res_quiz = Database::query($sql_quiz);
  7176. $res_hot = Database::query($sql_hot);
  7177. $return = '<ul class="lp_resource">';
  7178. $return .= '<li class="lp_resource_element">';
  7179. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7180. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?lp_id=' . $this->lp_id . '">' . get_lang('NewExercise') . '</a>';
  7181. $return .= '</li>';
  7182. // Display hotpotatoes
  7183. while ($row_hot = Database :: fetch_array($res_hot)) {
  7184. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7185. $return .= '<a class="moved" href="#">';
  7186. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7187. $return .= '</a> ';
  7188. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7189. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_HOTPOTATOES . '&amp;file=' . $row_hot['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7190. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7191. $return .= '</li>';
  7192. }
  7193. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7194. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7195. $return .= '<a class="moved" href="#">';
  7196. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7197. $return .= '</a> ';
  7198. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7199. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_QUIZ . '&amp;file=' . $row_quiz['id'] . '&amp;lp_id=' . $this->lp_id . '">' .
  7200. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7201. '</a>';
  7202. $return .= '</li>';
  7203. }
  7204. $return .= '</ul>';
  7205. return $return;
  7206. }
  7207. /**
  7208. * Creates a list with all the links in it
  7209. * @return string
  7210. */
  7211. public function get_links() {
  7212. $course_id = api_get_course_int_id();
  7213. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7214. $session_id = api_get_session_id();
  7215. $condition_session = api_get_session_condition($session_id);
  7216. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  7217. $res_link = Database::query($sql_link);
  7218. $return = '<ul class="lp_resource">';
  7219. $return .= '<li class="lp_resource_element">';
  7220. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  7221. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'link/link.php?' . api_get_cidreq() . '&action=addlink&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('LinkAdd') . '">' . get_lang('LinkAdd') . '</a>';
  7222. $return .= '</li>';
  7223. $course_info = api_get_course_info();
  7224. while ($row_link = Database :: fetch_array($res_link)) {
  7225. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  7226. if ($item_visibility != 2) {
  7227. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  7228. $return .= '<a class="moved" href="#">';
  7229. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7230. $return .= '</a> ';
  7231. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  7232. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_LINK . '&amp;file=' . $row_link['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7233. $row_link['title'].
  7234. '</a>';
  7235. $return .= '</li>';
  7236. }
  7237. }
  7238. $return .= '</ul>';
  7239. return $return;
  7240. }
  7241. /**
  7242. * Creates a list with all the student publications in it
  7243. * @return unknown
  7244. */
  7245. public function get_student_publications() {
  7246. $return = '<div class="lp_resource" >';
  7247. $return .= '<div class="lp_resource_element">';
  7248. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7249. $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>';
  7250. $return .= '</div>';
  7251. $return .= '</div>';
  7252. return $return;
  7253. }
  7254. /**
  7255. * Creates a list with all the forums in it
  7256. * @return string
  7257. */
  7258. public function get_forums() {
  7259. require_once '../forum/forumfunction.inc.php';
  7260. require_once '../forum/forumconfig.inc.php';
  7261. $a_forums = get_forums();
  7262. $return = '<ul class="lp_resource">';
  7263. //First add link
  7264. $return .= '<li class="lp_resource_element">';
  7265. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7266. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7267. $return .= '</li>';
  7268. $return .= '<script>
  7269. function toggle_forum(forum_id){
  7270. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7271. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7272. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7273. } else {
  7274. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7275. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7276. }
  7277. }
  7278. </script>';
  7279. foreach ($a_forums as $forum) {
  7280. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7281. if (!empty($forum['forum_id'])) {
  7282. $return .= '<a class="moved" href="#">';
  7283. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7284. $return .= ' </a>';
  7285. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7286. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7287. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7288. </a>
  7289. <a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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($forum['forum_title']) . '</a>';
  7290. }
  7291. $return .= '</li>';
  7292. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7293. $a_threads = get_threads($forum['forum_id']);
  7294. if (is_array($a_threads)) {
  7295. foreach ($a_threads as $thread) {
  7296. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7297. $return .= '&nbsp;<a class="moved" href="#">';
  7298. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7299. $return .= ' </a>';
  7300. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7301. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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($thread['thread_title']) . '</a>';
  7302. $return .= '</li>';
  7303. }
  7304. }
  7305. $return .= '</div>';
  7306. }
  7307. $return .= '</ul>';
  7308. return $return;
  7309. }
  7310. /**
  7311. * // TODO: The output encoding should be equal to the system encoding.
  7312. *
  7313. * Exports the learning path as a SCORM package. This is the main function that
  7314. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7315. * whole thing and returns the zip.
  7316. *
  7317. * This method needs to be called in PHP5, as it will fail with non-adequate
  7318. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7319. * you need to call it on a learnpath object.
  7320. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7321. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7322. * path has been modified, it should use the generic method here below.
  7323. * @TODO link this function with the export_lp() function in the same class
  7324. * @param string Optional name of zip file. If none, title of learnpath is
  7325. * domesticated and trailed with ".zip"
  7326. * @return string Returns the zip package string, or null if error
  7327. */
  7328. public function scorm_export() {
  7329. global $_course;
  7330. $course_id = api_get_course_int_id();
  7331. // Remove memory and time limits as much as possible as this might be a long process...
  7332. if (function_exists('ini_set')) {
  7333. $mem = ini_get('memory_limit');
  7334. if (substr($mem, -1, 1) == 'M') {
  7335. $mem_num = substr($mem, 0, -1);
  7336. if ($mem_num < 128) {
  7337. ini_set('memory_limit', '128M');
  7338. }
  7339. } else {
  7340. ini_set('memory_limit', '128M');
  7341. }
  7342. ini_set('max_execution_time', 600);
  7343. }
  7344. // Create the zip handler (this will remain available throughout the method).
  7345. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7346. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7347. $temp_dir_short = uniqid();
  7348. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7349. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7350. $zip_folder = new PclZip($temp_zip_file);
  7351. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7352. $root_path = $main_path = api_get_path(SYS_PATH);
  7353. $files_cleanup = array();
  7354. // Place to temporarily stash the zipfiles.
  7355. // create the temp dir if it doesn't exist
  7356. // or do a cleanup befor creating the zipfile.
  7357. if (!is_dir($temp_zip_dir)) {
  7358. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7359. } else {
  7360. // Cleanup: Check the temp dir for old files and delete them.
  7361. $handle = opendir($temp_zip_dir);
  7362. while (false !== ($file = readdir($handle))) {
  7363. if ($file != '.' && $file != '..') {
  7364. unlink("$temp_zip_dir/$file");
  7365. }
  7366. }
  7367. closedir($handle);
  7368. }
  7369. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7370. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7371. // Remove the possible . at the end of the path.
  7372. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7373. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7374. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7375. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7376. }
  7377. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  7378. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7379. // Aggregation Model official document, secion "2.3 Content Packaging".
  7380. $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.
  7381. $root = $xmldoc->createElement('manifest');
  7382. $root->setAttribute('identifier', 'SingleCourseManifest');
  7383. $root->setAttribute('version', '1.1');
  7384. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7385. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7386. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7387. $root->setAttribute('xsi:schemaLocation', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd');
  7388. // Build mandatory sub-root container elements.
  7389. $metadata = $xmldoc->createElement('metadata');
  7390. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7391. $metadata->appendChild($md_schema);
  7392. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7393. $metadata->appendChild($md_schemaversion);
  7394. $root->appendChild($metadata);
  7395. $organizations = $xmldoc->createElement('organizations');
  7396. $resources = $xmldoc->createElement('resources');
  7397. // Build the only organization we will use in building our learnpaths.
  7398. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7399. $organization = $xmldoc->createElement('organization');
  7400. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7401. // To set the title of the SCORM entity (=organization), we take the name given
  7402. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7403. // learning path charset) as it is the encoding that defines how it is stored
  7404. // in the database. Then we convert it to HTML entities again as the "&" character
  7405. // alone is not authorized in XML (must be &amp;).
  7406. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7407. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7408. $organization->appendChild($org_title);
  7409. $folder_name = 'document';
  7410. // Removes the learning_path/scorm_folder path when exporting see #4841
  7411. $path_to_remove = null;
  7412. $result = $this->generate_lp_folder($_course);
  7413. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7414. $path_to_remove = 'document'.$result['dir'];
  7415. $path_to_replace = $folder_name.'/';
  7416. }
  7417. //Fixes chamilo scorm exports
  7418. if ($this->ref == 'chamilo_scorm_export') {
  7419. $path_to_remove = 'scorm/'.$this->path.'/document/';
  7420. }
  7421. // For each element, add it to the imsmanifest structure, then add it to the zip.
  7422. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  7423. $link_updates = array();
  7424. foreach ($this->items as $index => $item) {
  7425. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  7426. // Get included documents from this item.
  7427. if ($item->type == 'sco')
  7428. $inc_docs = $item->get_resources_from_source(null, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.'scorm/'.$this->path.'/'.$item->get_path());
  7429. else
  7430. $inc_docs = $item->get_resources_from_source();
  7431. // Give a child element <item> to the <organization> element.
  7432. $my_item_id = $item->get_id();
  7433. $my_item = $xmldoc->createElement('item');
  7434. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  7435. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  7436. $my_item->setAttribute('isvisible', 'true');
  7437. // Give a child element <title> to the <item> element.
  7438. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7439. $my_item->appendChild($my_title);
  7440. // Give a child element <adlcp:prerequisites> to the <item> element.
  7441. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  7442. $my_prereqs->setAttribute('type', 'aicc_script');
  7443. $my_item->appendChild($my_prereqs);
  7444. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7445. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  7446. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7447. //$xmldoc->createElement('adlcp:timelimitaction','');
  7448. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7449. //$xmldoc->createElement('adlcp:datafromlms','');
  7450. // Give a child element <adlcp:masteryscore> to the <item> element.
  7451. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7452. $my_item->appendChild($my_masteryscore);
  7453. // Attach this item to the organization element or hits parent if there is one.
  7454. if (!empty($item->parent) && $item->parent != 0) {
  7455. $children = $organization->childNodes;
  7456. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  7457. if (is_object($possible_parent)) {
  7458. $possible_parent->appendChild($my_item);
  7459. } else {
  7460. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  7461. }
  7462. } else {
  7463. if ($this->debug > 0) { error_log('No parent'); }
  7464. $organization->appendChild($my_item);
  7465. }
  7466. // Get the path of the file(s) from the course directory root.
  7467. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7468. if (!empty($path_to_remove)) {
  7469. //From docs
  7470. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  7471. //From quiz
  7472. if ($this->ref == 'chamilo_scorm_export') {
  7473. $path_to_remove = 'scorm/'.$this->path.'/';
  7474. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  7475. }
  7476. } else {
  7477. $my_xml_file_path = $my_file_path;
  7478. }
  7479. $my_sub_dir = dirname($my_file_path);
  7480. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7481. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7482. $my_xml_sub_dir = $my_sub_dir;
  7483. // Give a <resource> child to the <resources> element
  7484. $my_resource = $xmldoc->createElement('resource');
  7485. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7486. $my_resource->setAttribute('type', 'webcontent');
  7487. $my_resource->setAttribute('href', $my_xml_file_path);
  7488. // adlcp:scormtype can be either 'sco' or 'asset'.
  7489. if ($item->type == 'sco') {
  7490. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7491. } else {
  7492. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7493. }
  7494. // xml:base is the base directory to find the files declared in this resource.
  7495. $my_resource->setAttribute('xml:base', '');
  7496. // Give a <file> child to the <resource> element.
  7497. $my_file = $xmldoc->createElement('file');
  7498. $my_file->setAttribute('href', $my_xml_file_path);
  7499. $my_resource->appendChild($my_file);
  7500. // Dependency to other files - not yet supported.
  7501. $i = 1;
  7502. foreach ($inc_docs as $doc_info) {
  7503. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7504. $my_dep = $xmldoc->createElement('resource');
  7505. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7506. $my_dep->setAttribute('identifier', $res_id);
  7507. $my_dep->setAttribute('type', 'webcontent');
  7508. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7509. $my_dep_file = $xmldoc->createElement('file');
  7510. // Check type of URL.
  7511. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7512. if ($doc_info[1] == 'remote') {
  7513. // Remote file. Save url as is.
  7514. $my_dep_file->setAttribute('href', $doc_info[0]);
  7515. $my_dep->setAttribute('xml:base', '');
  7516. } elseif ($doc_info[1] == 'local') {
  7517. switch ($doc_info[2]) {
  7518. case 'url': // Local URL - save path as url for now, don't zip file.
  7519. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7520. $current_dir = dirname($abs_path);
  7521. $current_dir = str_replace('\\', '/', $current_dir);
  7522. $file_path = realpath($abs_path);
  7523. $file_path = str_replace('\\', '/', $file_path);
  7524. $my_dep_file->setAttribute('href', $file_path);
  7525. $my_dep->setAttribute('xml:base', '');
  7526. if (strstr($file_path, $main_path) !== false) {
  7527. // The calculated real path is really inside Chamilo's root path.
  7528. // Reduce file path to what's under the DocumentRoot.
  7529. $file_path = substr($file_path, strlen($root_path) - 1);
  7530. //echo $file_path;echo '<br /><br />';
  7531. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  7532. $zip_files_abs[] = $file_path;
  7533. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7534. $my_dep_file->setAttribute('href', $file_path);
  7535. $my_dep->setAttribute('xml:base', '');
  7536. } elseif (empty($file_path)) {
  7537. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7538. if (strpos($document_root, -1) == '/') {
  7539. $document_root = substr(0, -1, $document_root);
  7540. }*/
  7541. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7542. $file_path = str_replace('//', '/', $file_path);
  7543. if (file_exists($file_path)) {
  7544. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7545. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7546. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7547. $my_dep_file->setAttribute('href', $file_path);
  7548. $my_dep->setAttribute('xml:base', '');
  7549. }
  7550. }
  7551. break;
  7552. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7553. $my_dep_file->setAttribute('href', $doc_info[0]);
  7554. $my_dep->setAttribute('xml:base', '');
  7555. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  7556. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  7557. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  7558. $abs_img_path_without_subdir = $doc_info[0];
  7559. $relp = api_get_path(REL_PATH); // The url-append config param.
  7560. $pos = strpos($abs_img_path_without_subdir, $relp);
  7561. if ($pos === 0) {
  7562. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  7563. }
  7564. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  7565. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  7566. $file_path = str_replace('\\', '/', $file_path);
  7567. $file_path = str_replace('//', '/', $file_path);
  7568. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  7569. // Prepare the current directory path (until just under 'document') with a trailing slash.
  7570. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  7571. // Check if the current document is in that path.
  7572. if (strstr($file_path, $cur_path) !== false) {
  7573. // The document is in that path, now get the relative path
  7574. // to the containing document.
  7575. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  7576. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  7577. $relative_path = '';
  7578. if (strstr($file_path, $cur_path) !== false) {
  7579. $relative_path = substr($file_path, strlen($orig_file_path));
  7580. $file_path = substr($file_path, strlen($cur_path));
  7581. } else {
  7582. // This case is still a problem as it's difficult to calculate a relative path easily
  7583. // might still generate wrong links.
  7584. //$file_path = substr($file_path,strlen($cur_path));
  7585. // Calculate the directory path to the current file (without trailing slash).
  7586. $my_relative_path = dirname($file_path);
  7587. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  7588. $my_relative_file = basename($file_path);
  7589. // Calculate the directory path to the containing file (without trailing slash).
  7590. $my_orig_file_path = substr($orig_file_path, 0, -1);
  7591. $dotdot = '';
  7592. $subdir = '';
  7593. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  7594. $my_relative_path2 = dirname($my_relative_path);
  7595. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  7596. $my_orig_file_path = dirname($my_orig_file_path);
  7597. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  7598. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  7599. $dotdot += '../';
  7600. $my_relative_path = $my_relative_path2;
  7601. }
  7602. $relative_path = $dotdot.$subdir.$my_relative_file;
  7603. }
  7604. // Put the current document in the zip (this array is the array
  7605. // that will manage documents already in the course folder - relative).
  7606. $zip_files[] = $file_path;
  7607. // Update the links to the current document in the containing document (make them relative).
  7608. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  7609. $my_dep_file->setAttribute('href', $file_path);
  7610. $my_dep->setAttribute('xml:base', '');
  7611. } elseif (strstr($file_path, $main_path) !== false) {
  7612. // The calculated real path is really inside Chamilo's root path.
  7613. // Reduce file path to what's under the DocumentRoot.
  7614. $file_path = substr($file_path, strlen($root_path));
  7615. //echo $file_path;echo '<br /><br />';
  7616. //error_log('Reduced path: '.$file_path, 0);
  7617. $zip_files_abs[] = $file_path;
  7618. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7619. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7620. $my_dep->setAttribute('xml:base', '');
  7621. } elseif (empty($file_path)) {
  7622. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7623. if(strpos($document_root,-1) == '/') {
  7624. $document_root = substr(0, -1, $document_root);
  7625. }*/
  7626. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7627. $file_path = str_replace('//', '/', $file_path);
  7628. if (file_exists($file_path)) {
  7629. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7630. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7631. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7632. $my_dep_file->setAttribute('href','document/'.$file_path);
  7633. $my_dep->setAttribute('xml:base', '');
  7634. }
  7635. }
  7636. break;
  7637. 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
  7638. if (substr($doc_info[0], 0, 2) == '..') {
  7639. // Relative path going up.
  7640. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7641. $current_dir = str_replace('\\', '/', $current_dir);
  7642. $file_path = realpath($current_dir.$doc_info[0]);
  7643. $file_path = str_replace('\\', '/', $file_path);
  7644. //error_log($file_path.' <-> '.$main_path,0);
  7645. if (strstr($file_path, $main_path) !== false) {
  7646. // The calculated real path is really inside Chamilo's root path.
  7647. // Reduce file path to what's under the DocumentRoot.
  7648. $file_path = substr($file_path, strlen($root_path));
  7649. //error_log('Reduced path: '.$file_path, 0);
  7650. $zip_files_abs[] = $file_path;
  7651. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7652. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7653. $my_dep->setAttribute('xml:base', '');
  7654. }
  7655. } else {
  7656. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7657. $my_dep_file->setAttribute('href', $doc_info[0]);
  7658. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7659. }
  7660. break;
  7661. default:
  7662. $my_dep_file->setAttribute('href', $doc_info[0]);
  7663. $my_dep->setAttribute('xml:base', '');
  7664. break;
  7665. }
  7666. }
  7667. $my_dep->appendChild($my_dep_file);
  7668. $resources->appendChild($my_dep);
  7669. $dependency = $xmldoc->createElement('dependency');
  7670. $dependency->setAttribute('identifierref', $res_id);
  7671. $my_resource->appendChild($dependency);
  7672. $i++;
  7673. }
  7674. //$my_dependency = $xmldoc->createElement('dependency');
  7675. //$my_dependency->setAttribute('identifierref', '');
  7676. $resources->appendChild($my_resource);
  7677. $zip_files[] = $my_file_path;
  7678. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  7679. } else {
  7680. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  7681. switch ($item->type) {
  7682. case TOOL_LINK:
  7683. $my_item = $xmldoc->createElement('item');
  7684. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7685. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7686. $my_item->setAttribute('isvisible', 'true');
  7687. // Give a child element <title> to the <item> element.
  7688. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7689. $my_item->appendChild($my_title);
  7690. // Give a child element <adlcp:prerequisites> to the <item> element.
  7691. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7692. $my_prereqs->setAttribute('type', 'aicc_script');
  7693. $my_item->appendChild($my_prereqs);
  7694. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7695. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  7696. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7697. //$xmldoc->createElement('adlcp:timelimitaction', '');
  7698. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7699. //$xmldoc->createElement('adlcp:datafromlms', '');
  7700. // Give a child element <adlcp:masteryscore> to the <item> element.
  7701. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7702. $my_item->appendChild($my_masteryscore);
  7703. // Attach this item to the organization element or its parent if there is one.
  7704. if (!empty($item->parent) && $item->parent != 0) {
  7705. $children = $organization->childNodes;
  7706. for ($i = 0; $i < $children->length; $i++) {
  7707. $item_temp = $children->item($i);
  7708. if ($item_temp -> nodeName == 'item') {
  7709. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7710. $item_temp -> appendChild($my_item);
  7711. }
  7712. }
  7713. }
  7714. } else {
  7715. $organization->appendChild($my_item);
  7716. }
  7717. $my_file_path = 'link_'.$item->get_id().'.html';
  7718. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  7719. $rs = Database::query($sql);
  7720. if ($link = Database :: fetch_array($rs)) {
  7721. $url = $link['url'];
  7722. $title = stripslashes($link['title']);
  7723. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  7724. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7725. $my_xml_file_path = $my_file_path;
  7726. $my_sub_dir = dirname($my_file_path);
  7727. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7728. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7729. $my_xml_sub_dir = $my_sub_dir;
  7730. // Give a <resource> child to the <resources> element.
  7731. $my_resource = $xmldoc->createElement('resource');
  7732. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7733. $my_resource->setAttribute('type', 'webcontent');
  7734. $my_resource->setAttribute('href', $my_xml_file_path);
  7735. // adlcp:scormtype can be either 'sco' or 'asset'.
  7736. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7737. // xml:base is the base directory to find the files declared in this resource.
  7738. $my_resource->setAttribute('xml:base', '');
  7739. // give a <file> child to the <resource> element.
  7740. $my_file = $xmldoc->createElement('file');
  7741. $my_file->setAttribute('href', $my_xml_file_path);
  7742. $my_resource->appendChild($my_file);
  7743. $resources->appendChild($my_resource);
  7744. }
  7745. break;
  7746. case TOOL_QUIZ:
  7747. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  7748. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  7749. $exe = new Exercise();
  7750. $exe->read($exe_id);
  7751. $my_item = $xmldoc->createElement('item');
  7752. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7753. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7754. $my_item->setAttribute('isvisible', 'true');
  7755. // Give a child element <title> to the <item> element.
  7756. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7757. $my_item->appendChild($my_title);
  7758. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  7759. //$my_item->appendChild($my_max_score);
  7760. // Give a child element <adlcp:prerequisites> to the <item> element.
  7761. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7762. $my_prereqs->setAttribute('type','aicc_script');
  7763. $my_item->appendChild($my_prereqs);
  7764. // Give a child element <adlcp:masteryscore> to the <item> element.
  7765. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7766. $my_item->appendChild($my_masteryscore);
  7767. // Attach this item to the organization element or hits parent if there is one.
  7768. if (!empty($item->parent) && $item->parent != 0) {
  7769. $children = $organization->childNodes;
  7770. for ($i = 0; $i < $children->length; $i++) {
  7771. $item_temp = $children->item($i);
  7772. if ($item_temp -> nodeName == 'item') {
  7773. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7774. $item_temp -> appendChild($my_item);
  7775. }
  7776. }
  7777. }
  7778. } else {
  7779. $organization->appendChild($my_item);
  7780. }
  7781. // Include export scripts.
  7782. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  7783. // Get the path of the file(s) from the course directory root
  7784. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7785. $my_file_path = 'quiz_'.$item->get_id().'.html';
  7786. // Write the contents of the exported exercise into a (big) html file
  7787. // to later pack it into the exported SCORM. The file will be removed afterwards.
  7788. $contents = export_exercise($exe_id, true);
  7789. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  7790. $res = file_put_contents($tmp_file_path, $contents);
  7791. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  7792. $files_cleanup[] = $tmp_file_path;
  7793. //error_log($tmp_path); die();
  7794. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7795. $my_xml_file_path = $my_file_path;
  7796. $my_sub_dir = dirname($my_file_path);
  7797. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7798. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7799. $my_xml_sub_dir = $my_sub_dir;
  7800. // Give a <resource> child to the <resources> element.
  7801. $my_resource = $xmldoc->createElement('resource');
  7802. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7803. $my_resource->setAttribute('type', 'webcontent');
  7804. $my_resource->setAttribute('href', $my_xml_file_path);
  7805. // adlcp:scormtype can be either 'sco' or 'asset'.
  7806. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7807. // xml:base is the base directory to find the files declared in this resource.
  7808. $my_resource->setAttribute('xml:base', '');
  7809. // Give a <file> child to the <resource> element.
  7810. $my_file = $xmldoc->createElement('file');
  7811. $my_file->setAttribute('href', $my_xml_file_path);
  7812. $my_resource->appendChild($my_file);
  7813. // Get included docs.
  7814. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  7815. // Dependency to other files - not yet supported.
  7816. $i = 1;
  7817. foreach ($inc_docs as $doc_info) {
  7818. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7819. $my_dep = $xmldoc->createElement('resource');
  7820. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7821. $my_dep->setAttribute('identifier', $res_id);
  7822. $my_dep->setAttribute('type', 'webcontent');
  7823. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7824. $my_dep_file = $xmldoc->createElement('file');
  7825. // Check type of URL.
  7826. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7827. if ($doc_info[1] == 'remote') {
  7828. // Remote file. Save url as is.
  7829. $my_dep_file->setAttribute('href', $doc_info[0]);
  7830. $my_dep->setAttribute('xml:base', '');
  7831. } elseif ($doc_info[1] == 'local') {
  7832. switch ($doc_info[2]) {
  7833. case 'url': // Local URL - save path as url for now, don't zip file.
  7834. // Save file but as local file (retrieve from URL).
  7835. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7836. $current_dir = dirname($abs_path);
  7837. $current_dir = str_replace('\\', '/', $current_dir);
  7838. $file_path = realpath($abs_path);
  7839. $file_path = str_replace('\\', '/', $file_path);
  7840. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7841. $my_dep->setAttribute('xml:base', '');
  7842. if (strstr($file_path, $main_path) !== false) {
  7843. // The calculated real path is really inside the chamilo root path.
  7844. // Reduce file path to what's under the DocumentRoot.
  7845. $file_path = substr($file_path, strlen($root_path));
  7846. //echo $file_path;echo '<br /><br />';
  7847. //error_log('Reduced path: '.$file_path, 0);
  7848. $zip_files_abs[] = $file_path;
  7849. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7850. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7851. $my_dep->setAttribute('xml:base', '');
  7852. } elseif (empty($file_path)) {
  7853. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  7854. if (strpos($document_root,-1) == '/') {
  7855. $document_root = substr(0, -1, $document_root);
  7856. }*/
  7857. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7858. $file_path = str_replace('//', '/', $file_path);
  7859. if (file_exists($file_path)) {
  7860. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7861. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7862. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7863. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7864. $my_dep->setAttribute('xml:base', '');
  7865. }
  7866. }
  7867. break;
  7868. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7869. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7870. $current_dir = str_replace('\\', '/', $current_dir);
  7871. $file_path = realpath($doc_info[0]);
  7872. $file_path = str_replace('\\', '/', $file_path);
  7873. $my_dep_file->setAttribute('href', $file_path);
  7874. $my_dep->setAttribute('xml:base', '');
  7875. if (strstr($file_path,$main_path) !== false) {
  7876. // The calculated real path is really inside the chamilo root path.
  7877. // Reduce file path to what's under the DocumentRoot.
  7878. $file_path = substr($file_path, strlen($root_path));
  7879. //echo $file_path;echo '<br /><br />';
  7880. //error_log('Reduced path: '.$file_path, 0);
  7881. $zip_files_abs[] = $file_path;
  7882. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7883. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7884. $my_dep->setAttribute('xml:base', '');
  7885. } elseif (empty($file_path)) {
  7886. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7887. if (strpos($document_root,-1) == '/') {
  7888. $document_root = substr(0, -1, $document_root);
  7889. }*/
  7890. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7891. $file_path = str_replace('//', '/', $file_path);
  7892. if (file_exists($file_path)) {
  7893. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7894. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7895. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7896. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7897. $my_dep->setAttribute('xml:base', '');
  7898. }
  7899. }
  7900. break;
  7901. 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
  7902. if (substr($doc_info[0], 0, 2) == '..') {
  7903. // Relative path going up.
  7904. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7905. $current_dir = str_replace('\\', '/', $current_dir);
  7906. $file_path = realpath($current_dir.$doc_info[0]);
  7907. $file_path = str_replace('\\', '/', $file_path);
  7908. //error_log($file_path.' <-> '.$main_path, 0);
  7909. if (strstr($file_path, $main_path) !== false) {
  7910. // The calculated real path is really inside Chamilo's root path.
  7911. // Reduce file path to what's under the DocumentRoot.
  7912. $file_path = substr($file_path, strlen($root_path));
  7913. $file_path_dest = $file_path;
  7914. // File path is courses/CHAMILO/document/....
  7915. $info_file_path = explode('/', $file_path);
  7916. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  7917. $file_path_dest = 'document/'.$file_path;
  7918. }
  7919. //error_log('Reduced path: '.$file_path, 0);
  7920. $zip_files_abs[] = $file_path;
  7921. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  7922. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7923. $my_dep->setAttribute('xml:base', '');
  7924. }
  7925. } else {
  7926. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7927. $my_dep_file->setAttribute('href', $doc_info[0]);
  7928. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7929. }
  7930. break;
  7931. default:
  7932. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  7933. $my_dep->setAttribute('xml:base', '');
  7934. break;
  7935. }
  7936. }
  7937. $my_dep->appendChild($my_dep_file);
  7938. $resources->appendChild($my_dep);
  7939. $dependency = $xmldoc->createElement('dependency');
  7940. $dependency->setAttribute('identifierref', $res_id);
  7941. $my_resource->appendChild($dependency);
  7942. $i++;
  7943. }
  7944. $resources->appendChild($my_resource);
  7945. $zip_files[] = $my_file_path;
  7946. break;
  7947. default:
  7948. // Get the path of the file(s) from the course directory root
  7949. $my_file_path = 'non_exportable.html';
  7950. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  7951. $my_xml_file_path = $my_file_path;
  7952. $my_sub_dir = dirname($my_file_path);
  7953. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7954. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  7955. $my_xml_sub_dir = $my_sub_dir;
  7956. // Give a <resource> child to the <resources> element.
  7957. $my_resource = $xmldoc->createElement('resource');
  7958. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7959. $my_resource->setAttribute('type', 'webcontent');
  7960. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  7961. // adlcp:scormtype can be either 'sco' or 'asset'.
  7962. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7963. // xml:base is the base directory to find the files declared in this resource.
  7964. $my_resource->setAttribute('xml:base', '');
  7965. // Give a <file> child to the <resource> element.
  7966. $my_file = $xmldoc->createElement('file');
  7967. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  7968. $my_resource->appendChild($my_file);
  7969. $resources->appendChild($my_resource);
  7970. break;
  7971. }
  7972. }
  7973. }
  7974. $organizations->appendChild($organization);
  7975. $root->appendChild($organizations);
  7976. $root->appendChild($resources);
  7977. $xmldoc->appendChild($root);
  7978. // TODO: Add a readme file here, with a short description and a link to the Reload player
  7979. // then add the file to the zip, then destroy the file (this is done automatically).
  7980. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  7981. //error_log(print_r($zip_files,true), 0);
  7982. foreach ($zip_files as $file_path) {
  7983. if (empty($file_path)) { continue; }
  7984. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  7985. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  7986. $this->create_path($dest_file);
  7987. //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);
  7988. //echo $main_path.$file_path.'<br />';
  7989. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  7990. // Check if the file needs a link update.
  7991. if (in_array($file_path, array_keys($link_updates))) {
  7992. $string = file_get_contents($dest_file);
  7993. unlink($dest_file);
  7994. foreach ($link_updates[$file_path] as $old_new) {
  7995. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  7996. // This is an ugly hack that allows .flv files to be found by the flv player that
  7997. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  7998. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  7999. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8000. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8001. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8002. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8003. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8004. }
  8005. //Fix to avoid problems with default_course_document
  8006. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8007. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  8008. } else {
  8009. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  8010. $new_dest = $old_new['dest'];
  8011. }
  8012. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8013. $string = str_replace($old_new['orig'], $new_dest, $string);
  8014. //Add files inside the HTMLs
  8015. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8016. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  8017. if (file_exists($sys_course_path.$new_path)) {
  8018. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  8019. }
  8020. }
  8021. file_put_contents($dest_file, $string);
  8022. }
  8023. }
  8024. foreach ($zip_files_abs as $file_path) {
  8025. if (empty($file_path)) { continue; }
  8026. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  8027. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) { continue; }
  8028. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  8029. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8030. $this->create_path($dest_file);
  8031. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  8032. //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);
  8033. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  8034. copy($main_path.$file_path, $dest_file);
  8035. // Check if the file needs a link update.
  8036. if (in_array($file_path, array_keys($link_updates))) {
  8037. $string = file_get_contents($dest_file);
  8038. unlink($dest_file);
  8039. foreach ($link_updates[$file_path] as $old_new) {
  8040. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  8041. // This is an ugly hack that allows .flv files to be found by the flv player that
  8042. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8043. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8044. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8045. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8046. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8047. }
  8048. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8049. }
  8050. file_put_contents($dest_file, $string);
  8051. }
  8052. }
  8053. if (is_array($links_to_create)) {
  8054. foreach ($links_to_create as $file => $link) {
  8055. $file_content = '<!DOCTYPE html>
  8056. <head>
  8057. <meta charset="'.api_get_language_isocode().'" />
  8058. <title>'.$link['title'].'</title>
  8059. </head>
  8060. <body dir="'.api_get_text_direction().'">
  8061. <div style="text-align:center">
  8062. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8063. </body>
  8064. </html>';
  8065. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8066. }
  8067. }
  8068. // Add non exportable message explanation.
  8069. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8070. $file_content = '<!DOCTYPE html>
  8071. <head>
  8072. <meta charset="'.api_get_language_isocode().'" />
  8073. <title>'.$lang_not_exportable.'</title>
  8074. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8075. </head>
  8076. <body dir="'.api_get_text_direction().'">';
  8077. $file_content .=
  8078. <<<EOD
  8079. <style>
  8080. .error-message {
  8081. font-family: arial, verdana, helvetica, sans-serif;
  8082. border-width: 1px;
  8083. border-style: solid;
  8084. left: 50%;
  8085. margin: 10px auto;
  8086. min-height: 30px;
  8087. padding: 5px;
  8088. right: 50%;
  8089. width: 500px;
  8090. background-color: #FFD1D1;
  8091. border-color: #FF0000;
  8092. color: #000;
  8093. }
  8094. </style>
  8095. <body>
  8096. <div class="error-message">
  8097. $lang_not_exportable
  8098. </div>
  8099. </body>
  8100. </html>
  8101. EOD;
  8102. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8103. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8104. }
  8105. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8106. // Add the extra files that go along with a SCORM package.
  8107. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8108. $extra_files = scandir($main_code_path);
  8109. foreach ($extra_files as $extra_file) {
  8110. if (strpos($extra_file, '.') === 0)
  8111. continue;
  8112. else {
  8113. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8114. $this->create_path($dest_file);
  8115. copy($main_code_path.$extra_file, $dest_file);
  8116. }
  8117. }
  8118. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8119. $manifest = @$xmldoc->saveXML();
  8120. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8121. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8122. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8123. // Clean possible temporary files.
  8124. foreach ($files_cleanup as $file) {
  8125. $res = unlink($file);
  8126. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8127. }
  8128. // Send file to client.
  8129. //$name = 'scorm_export_'.$this->lp_id.'.zip';
  8130. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  8131. $name = replace_dangerous_char($this->get_name()).'.zip';
  8132. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8133. }
  8134. public function scorm_export_to_pdf($lp_id) {
  8135. $lp_id = intval($lp_id);
  8136. $files_to_export = array();
  8137. $course_data = api_get_course_info($this->cc);
  8138. if (!empty($course_data)) {
  8139. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8140. $list = self::get_flat_ordered_items_list($lp_id);
  8141. if (!empty($list)) {
  8142. foreach ($list as $item_id) {
  8143. $item = $this->items[$item_id];
  8144. switch ($item->type) {
  8145. case 'document':
  8146. //Getting documents from a LP with chamilo documents
  8147. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8148. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8149. if (file_exists($file_path)) {
  8150. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8151. }
  8152. break;
  8153. case 'asset': //commes from a scorm package generated by chamilo
  8154. case 'sco':
  8155. $file_path = $scorm_path.'/'.$item->path;
  8156. if (file_exists($file_path)) {
  8157. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8158. }
  8159. break;
  8160. case 'dokeos_chapter':
  8161. case 'dir':
  8162. case 'chapter':
  8163. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8164. break;
  8165. }
  8166. }
  8167. }
  8168. $pdf = new PDF();
  8169. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8170. return $result;
  8171. }
  8172. return false;
  8173. }
  8174. /**
  8175. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  8176. * if it doesn't exist
  8177. */
  8178. public function create_path($path) {
  8179. $path_bits = split('/', dirname($path));
  8180. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8181. $path_built = IS_WINDOWS_OS ? '' : '/';
  8182. foreach ($path_bits as $bit) {
  8183. if (!empty ($bit)) {
  8184. $new_path = $path_built . $bit;
  8185. if (is_dir($new_path)) {
  8186. $path_built = $new_path . '/';
  8187. } else {
  8188. mkdir($new_path, api_get_permissions_for_new_directories());
  8189. $path_built = $new_path . '/';
  8190. }
  8191. }
  8192. }
  8193. }
  8194. /**
  8195. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8196. * @return boolean The results of the unlink function, or false if there was no image to start with
  8197. */
  8198. public function delete_lp_image() {
  8199. $img = $this->get_preview_image();
  8200. if ($img != '') {
  8201. $del_file = $this->get_preview_image_path(null, 'sys');
  8202. if (isset($del_file) && file_exists($del_file)) {
  8203. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8204. if (file_exists($del_file_2)) {
  8205. unlink($del_file_2);
  8206. }
  8207. $this->set_preview_image('');
  8208. return @unlink($del_file);
  8209. }
  8210. }
  8211. return false;
  8212. }
  8213. /**
  8214. * Uploads an author image to the upload/learning_path/images directory
  8215. * @param array The image array, coming from the $_FILES superglobal
  8216. * @return boolean True on success, false on error
  8217. */
  8218. public function upload_image($image_array) {
  8219. $image_moved = false;
  8220. if (!empty ($image_array['name'])) {
  8221. $upload_ok = process_uploaded_file($image_array);
  8222. $has_attachment = true;
  8223. } else {
  8224. $image_moved = true;
  8225. }
  8226. if ($upload_ok) {
  8227. if ($has_attachment) {
  8228. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8229. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8230. $updir = $sys_course_path . $courseDir;
  8231. // Try to add an extension to the file if it hasn't one.
  8232. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8233. if (!filter_extension($new_file_name)) {
  8234. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8235. $image_moved = false;
  8236. } else {
  8237. $file_extension = explode('.', $image_array['name']);
  8238. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8239. $filename = uniqid('');
  8240. $new_file_name = $filename.'.'.$file_extension;
  8241. $new_path = $updir.'/'.$new_file_name;
  8242. // Resize the image.
  8243. $temp = new Image($image_array['tmp_name']);
  8244. $picture_infos = $temp->get_image_info();
  8245. if ($picture_infos['width'] > 104) {
  8246. $thumbwidth = 104;
  8247. } else {
  8248. $thumbwidth = $picture_infos['width'];
  8249. }
  8250. if ($picture_infos['height'] > 96) {
  8251. $new_height = 96;
  8252. } else {
  8253. $new_height = $picture_infos['height'];
  8254. }
  8255. $temp->resize($thumbwidth, $new_height, 0);
  8256. $result = $temp->send_image($new_path);
  8257. // Storing the image filename.
  8258. if ($result) {
  8259. $image_moved = true;
  8260. $this->set_preview_image($new_file_name);
  8261. //Resize to 32
  8262. $temp->resize(64, 64, 0);
  8263. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8264. return true;
  8265. }
  8266. }
  8267. }
  8268. }
  8269. return false;
  8270. }
  8271. public function set_autolunch($lp_id, $status) {
  8272. $course_id = api_get_course_int_id();
  8273. $lp_id = intval($lp_id);
  8274. $status = intval($status);
  8275. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8276. //Setting everything to autolunch = 0
  8277. $attributes['autolunch'] = 0;
  8278. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8279. Database::update($lp_table, $attributes, $where);
  8280. if ($status == 1) {
  8281. //Setting my lp_id to autolunch = 1
  8282. $attributes['autolunch'] = 1;
  8283. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8284. Database::update($lp_table, $attributes, $where );
  8285. }
  8286. }
  8287. /**
  8288. * Gets previous_item_id for the next element of the lp_item table
  8289. * @author Isaac flores paz
  8290. * @return integer Previous item ID
  8291. */
  8292. function select_previous_item_id() {
  8293. $course_id = api_get_course_int_id();
  8294. if ($this->debug > 0) {
  8295. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8296. }
  8297. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8298. // Get the max order of the items
  8299. $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 . "'";
  8300. $rs_max_order = Database::query($sql_max_order);
  8301. $row_max_order = Database::fetch_object($rs_max_order);
  8302. $max_order = $row_max_order->display_order;
  8303. // Get the previous item ID
  8304. $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."' ";
  8305. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  8306. $row_max = Database::fetch_object($rs_max);
  8307. // Return the previous item ID
  8308. return $row_max->previous;
  8309. }
  8310. function copy() {
  8311. $main_path = api_get_path(SYS_CODE_PATH);
  8312. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8313. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8314. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8315. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8316. //Course builder
  8317. $cb = new CourseBuilder();
  8318. //Setting tools that will be copied
  8319. $cb->set_tools_to_build(array('learnpaths'));
  8320. //Setting elements that will be copied
  8321. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  8322. $course = $cb->build();
  8323. //Course restorer
  8324. $course_restorer = new CourseRestorer($course);
  8325. $course_restorer->set_add_text_in_items(true);
  8326. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8327. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8328. }
  8329. function verify_document_size($s) {
  8330. $post_max = ini_get('post_max_size');
  8331. $upl_max = ini_get('upload_max_filesize');
  8332. $documents_total_space = DocumentManager::documents_total_space();
  8333. $course_max_space = DocumentManager::get_course_quota();
  8334. $total_size = filesize($s) + $documents_total_space;
  8335. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8336. return true;
  8337. } else{
  8338. return false;
  8339. }
  8340. }
  8341. function clear_prerequisites() {
  8342. $course_id = $this->get_course_int_id();
  8343. if ($this->debug > 0) {
  8344. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8345. }
  8346. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8347. $lp_id = $this->get_id();
  8348. //Cleaning prerequisites
  8349. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8350. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8351. Database::query($sql);
  8352. //Cleaning mastery score for exercises
  8353. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8354. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8355. Database::query($sql);
  8356. }
  8357. function set_previous_step_as_prerequisite_for_all_items() {
  8358. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8359. $course_id = $this->get_course_int_id();
  8360. $lp_id = $this->get_id();
  8361. if (!empty($this->items)) {
  8362. $old_id = null;
  8363. $old_max = 0;
  8364. $old_type = null;
  8365. foreach ($this->items as $item) {
  8366. if (!empty($old_id)) {
  8367. $current_item_id = $item->get_id();
  8368. if ($old_type == 'quiz') {
  8369. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$old_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$old_id'";
  8370. Database::query($sql);
  8371. }
  8372. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$old_id' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  8373. Database::query($sql);
  8374. }
  8375. $old_id = $item->get_id();
  8376. $old_max = $item->get_max();
  8377. $old_type = $item->get_type();
  8378. }
  8379. }
  8380. }
  8381. }
  8382. if (!function_exists('trim_value')) {
  8383. function trim_value(& $value) {
  8384. $value = trim($value);
  8385. }
  8386. }