learnpath.class.php 445 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  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 $categoryId;
  73. /**
  74. * Constructor.
  75. * Needs a database handler, a course code and a learnpath id from the database.
  76. * Also builds the list of items into $this->items.
  77. * @param string $course Course code
  78. * @param integer $lp_id
  79. * @param integer $user_id
  80. * @return boolean True on success, false on error
  81. */
  82. public function __construct($course, $lp_id, $user_id)
  83. {
  84. $this->encoding = api_get_system_encoding();
  85. if ($this->debug > 0) {
  86. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  87. }
  88. if (empty($course)) {
  89. $this->error = 'Course code is empty';
  90. return false;
  91. } else {
  92. $course_info = api_get_course_info($course);
  93. if (!empty($course_info)) {
  94. $this->cc = $course_info['code'];
  95. $this->course_info = $course_info;
  96. $course_id = $course_info['real_id'];
  97. } else {
  98. $this->error = 'Course code does not exist in database.';
  99. return false;
  100. }
  101. }
  102. $this->set_course_int_id($course_id);
  103. // Check learnpath ID.
  104. if (empty($lp_id)) {
  105. $this->error = 'Learnpath ID is empty';
  106. return false;
  107. } else {
  108. // TODO: Make it flexible to use any course_code (still using env course code here).
  109. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  110. $lp_id = intval($lp_id);
  111. $sql = "SELECT * FROM $lp_table
  112. WHERE id = '$lp_id' AND c_id = $course_id";
  113. if ($this->debug > 2) {
  114. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  115. }
  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->proximity = $row['content_local'];
  123. $this->theme = $row['theme'];
  124. $this->maker = $row['content_maker'];
  125. $this->prevent_reinit = $row['prevent_reinit'];
  126. $this->seriousgame_mode = $row['seriousgame_mode'];
  127. $this->license = $row['content_license'];
  128. $this->scorm_debug = $row['debug'];
  129. $this->js_lib = $row['js_lib'];
  130. $this->path = $row['path'];
  131. $this->preview_image = $row['preview_image'];
  132. $this->author = $row['author'];
  133. $this->hide_toc_frame = $row['hide_toc_frame'];
  134. $this->lp_session_id = $row['session_id'];
  135. $this->use_max_score = $row['use_max_score'];
  136. $this->created_on = $row['created_on'];
  137. $this->modified_on = $row['modified_on'];
  138. $this->ref = $row['ref'];
  139. $this->categoryId = $row['category_id'];
  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
  178. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  179. ORDER BY view_count DESC";
  180. $res = Database::query($sql);
  181. if ($this->debug > 2) {
  182. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  183. }
  184. if (Database :: num_rows($res) > 0) {
  185. if ($this->debug > 2) {
  186. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  187. }
  188. $row = Database :: fetch_array($res);
  189. $this->attempt = $row['view_count'];
  190. $this->lp_view_id = $row['id'];
  191. $this->last_item_seen = $row['last_item'];
  192. $this->progress_db = $row['progress'];
  193. $this->lp_view_session_id = $row['session_id'];
  194. } else if (!api_is_invitee()) {
  195. if ($this->debug > 2) {
  196. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  197. }
  198. $this->attempt = 1;
  199. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  200. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  201. Database::query($sql);
  202. $this->lp_view_id = Database::insert_id();
  203. if ($this->debug > 2) {
  204. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  205. }
  206. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  207. Database::query($sql);
  208. }
  209. // Initialise items.
  210. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  211. $sql = "SELECT * FROM $lp_item_table
  212. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  213. ORDER BY parent_item_id, display_order";
  214. $res = Database::query($sql);
  215. if ($this->debug > 2) {
  216. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  217. error_log('-- Start while--', 0);
  218. }
  219. $lp_item_id_list = array();
  220. while ($row = Database::fetch_array($res)) {
  221. $lp_item_id_list[] = $row['id'];
  222. switch ($this->type) {
  223. case 3: //aicc
  224. $oItem = new aiccItem('db', $row['id'], $course_id);
  225. if (is_object($oItem)) {
  226. $my_item_id = $oItem->get_id();
  227. $oItem->set_lp_view($this->lp_view_id, $course_id);
  228. $oItem->set_prevent_reinit($this->prevent_reinit);
  229. // Don't use reference here as the next loop will make the pointed object change.
  230. $this->items[$my_item_id] = $oItem;
  231. $this->refs_list[$oItem->ref] = $my_item_id;
  232. if ($this->debug > 2) {
  233. error_log(
  234. 'New LP - learnpath::__construct() - ' .
  235. 'aicc object with id ' . $my_item_id .
  236. ' set in items[]', 0);
  237. }
  238. }
  239. break;
  240. case 2:
  241. $oItem = new scormItem('db', $row['id'], $course_id);
  242. if (is_object($oItem)) {
  243. $my_item_id = $oItem->get_id();
  244. $oItem->set_lp_view($this->lp_view_id, $course_id);
  245. $oItem->set_prevent_reinit($this->prevent_reinit);
  246. // Don't use reference here as the next loop will make the pointed object change.
  247. $this->items[$my_item_id] = $oItem;
  248. $this->refs_list[$oItem->ref] = $my_item_id;
  249. if ($this->debug > 2) {
  250. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  251. }
  252. }
  253. break;
  254. case 1:
  255. default:
  256. if ($this->debug > 2) {
  257. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  258. }
  259. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  260. if ($this->debug > 2) {
  261. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  262. }
  263. if (is_object($oItem)) {
  264. $my_item_id = $oItem->get_id();
  265. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  266. $oItem->set_prevent_reinit($this->prevent_reinit);
  267. // Don't use reference here as the next loop will make the pointed object change.
  268. $this->items[$my_item_id] = $oItem;
  269. $this->refs_list[$my_item_id] = $my_item_id;
  270. if ($this->debug > 2) {
  271. error_log(
  272. 'New LP - learnpath::__construct() ' . __LINE__ .
  273. ' - object with id ' . $my_item_id . ' set in items[]',
  274. 0);
  275. }
  276. }
  277. break;
  278. }
  279. // Setting the object level with variable $this->items[$i][parent]
  280. foreach ($this->items as $itemLPObject) {
  281. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  282. $itemLPObject->level = $level;
  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. if (!empty($lp_item_id_list_to_string)) {
  299. // Get last viewing vars.
  300. $lp_item_view_table = Database:: get_course_table(
  301. TABLE_LP_ITEM_VIEW
  302. );
  303. // This query should only return one or zero result.
  304. $sql = "SELECT lp_item_id, status
  305. FROM $lp_item_view_table
  306. WHERE
  307. c_id = $course_id AND
  308. lp_view_id = ".$this->lp_view_id." AND
  309. lp_item_id IN ('".$lp_item_id_list_to_string."')
  310. ORDER BY view_count DESC ";
  311. if ($this->debug > 2) {
  312. error_log(
  313. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  314. 0
  315. );
  316. }
  317. $status_list = array();
  318. $res = Database::query($sql);
  319. while ($row = Database:: fetch_array($res)) {
  320. $status_list[$row['lp_item_id']] = $row['status'];
  321. }
  322. foreach ($lp_item_id_list as $item_id) {
  323. if (isset($status_list[$item_id])) {
  324. $status = $status_list[$item_id];
  325. if (is_object($this->items[$item_id])) {
  326. $this->items[$item_id]->set_status($status);
  327. if (empty($status)) {
  328. $this->items[$item_id]->set_status(
  329. $this->default_status
  330. );
  331. }
  332. }
  333. } else {
  334. if (!api_is_invitee()) {
  335. if (is_object($this->items[$item_id])) {
  336. $this->items[$item_id]->set_status(
  337. $this->default_status
  338. );
  339. }
  340. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  341. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  342. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  343. if ($this->debug > 2) {
  344. error_log(
  345. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  346. 0
  347. );
  348. }
  349. $this->items[$item_id]->set_lp_view(
  350. $this->lp_view_id,
  351. $course_id
  352. );
  353. Database::query($sql);
  354. $insertId = Database::insert_id();
  355. $sql = "UPDATE $lp_item_view_table SET id = iid
  356. WHERE iid = $insertId";
  357. Database::query($sql);
  358. }
  359. }
  360. }
  361. }
  362. }
  363. $this->ordered_items = self::get_flat_ordered_items_list(
  364. $this->get_id(),
  365. 0,
  366. $course_id
  367. );
  368. $this->max_ordered_items = 0;
  369. foreach ($this->ordered_items as $index => $dummy) {
  370. if ($index > $this->max_ordered_items && !empty($dummy)) {
  371. $this->max_ordered_items = $index;
  372. }
  373. }
  374. // TODO: Define the current item better.
  375. $this->first();
  376. if ($this->debug > 2) {
  377. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  378. }
  379. }
  380. /**
  381. * @return string
  382. */
  383. public function getCourseCode()
  384. {
  385. return $this->cc;
  386. }
  387. /**
  388. * @return int
  389. */
  390. public function get_course_int_id()
  391. {
  392. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  393. }
  394. /**
  395. * @param $course_id
  396. * @return int
  397. */
  398. public function set_course_int_id($course_id)
  399. {
  400. return $this->course_int_id = intval($course_id);
  401. }
  402. /**
  403. * Get the depth level of LP item
  404. * @param $in_tab_items
  405. * @param $in_current_item_id
  406. * @return int
  407. */
  408. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  409. {
  410. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  411. if ($parent_item_id == 0) {
  412. return 0;
  413. } else {
  414. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  415. }
  416. }
  417. /**
  418. * Function rewritten based on old_add_item() from Yannick Warnier.
  419. * Due the fact that users can decide where the item should come, I had to overlook this function and
  420. * I found it better to rewrite it. Old function is still available.
  421. * Added also the possibility to add a description.
  422. *
  423. * @param int $parent
  424. * @param int $previous
  425. * @param string $type
  426. * @param int resource ID (ref)
  427. * @param string $title
  428. * @param string $description
  429. * @param int $prerequisites
  430. * @param int $max_time_allowed
  431. * @return int
  432. */
  433. public function add_item(
  434. $parent,
  435. $previous,
  436. $type = 'dokeos_chapter',
  437. $id,
  438. $title,
  439. $description,
  440. $prerequisites = 0,
  441. $max_time_allowed = 0
  442. ) {
  443. $course_id = api_get_course_int_id();
  444. if ($this->debug > 0) {
  445. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  446. }
  447. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  448. $parent = intval($parent);
  449. $previous = intval($previous);
  450. $id = intval($id);
  451. $max_time_allowed = htmlentities($max_time_allowed);
  452. if (empty($max_time_allowed)) {
  453. $max_time_allowed = 0;
  454. }
  455. $sql_count = " SELECT COUNT(id) AS num
  456. FROM $tbl_lp_item
  457. WHERE
  458. c_id = $course_id AND
  459. lp_id = " . $this->get_id() . " AND
  460. parent_item_id = " . $parent;
  461. $res_count = Database::query($sql_count);
  462. $row = Database :: fetch_array($res_count);
  463. $num = $row['num'];
  464. if ($num > 0) {
  465. if ($previous == 0) {
  466. $sql = "SELECT id, next_item_id, display_order
  467. FROM " . $tbl_lp_item . "
  468. WHERE
  469. c_id = $course_id AND
  470. lp_id = " . $this->get_id() . " AND
  471. parent_item_id = " . $parent . " AND
  472. previous_item_id = 0 OR
  473. previous_item_id=" . $parent;
  474. $result = Database::query($sql);
  475. $row = Database :: fetch_array($result);
  476. $tmp_previous = 0;
  477. $next = $row['id'];
  478. $display_order = 0;
  479. } else {
  480. $previous = (int) $previous;
  481. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  482. FROM $tbl_lp_item
  483. WHERE
  484. c_id = $course_id AND
  485. lp_id = " . $this->get_id() . " AND
  486. id = " . $previous;
  487. $result = Database::query($sql);
  488. $row = Database:: fetch_array($result);
  489. $tmp_previous = $row['id'];
  490. $next = $row['next_item_id'];
  491. $display_order = $row['display_order'];
  492. }
  493. } else {
  494. $tmp_previous = 0;
  495. $next = 0;
  496. $display_order = 0;
  497. }
  498. $id = intval($id);
  499. $typeCleaned = Database::escape_string($type);
  500. if ($type == 'quiz') {
  501. $sql = 'SELECT SUM(ponderation)
  502. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  503. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  504. ON quiz_question.id = quiz_rel_question.question_id
  505. WHERE quiz_rel_question.exercice_id = '.$id." AND
  506. quiz_question.c_id = $course_id AND
  507. quiz_rel_question.c_id = $course_id ";
  508. $rsQuiz = Database::query($sql);
  509. $max_score = Database :: result($rsQuiz, 0, 0);
  510. //Disabling the exercise if we add it inside a LP
  511. $exercise = new Exercise();
  512. $exercise->read($id);
  513. $exercise->disable();
  514. $exercise->save();
  515. } else {
  516. $max_score = 100;
  517. }
  518. $params = array(
  519. "c_id" => $course_id,
  520. "lp_id" => $this->get_id(),
  521. "item_type" =>$typeCleaned ,
  522. "ref" => '',
  523. "title" =>$title ,
  524. "description" => $description,
  525. "path" => $id,
  526. "max_score" => $max_score,
  527. "parent_item_id" => $parent,
  528. "previous_item_id" => $previous,
  529. "next_item_id" => $next,
  530. "display_order" => $display_order +1,
  531. "prerequisite" => $prerequisites,
  532. "max_time_allowed" => $max_time_allowed
  533. );
  534. if ($prerequisites != 0) {
  535. $params['prerequisite'] = $prerequisites;
  536. }
  537. $new_item_id = Database::insert($tbl_lp_item, $params);
  538. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  539. Database::query($sql);
  540. if ($this->debug > 2) {
  541. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  542. }
  543. if ($new_item_id) {
  544. // Update the item that should come after the new item.
  545. $sql = " UPDATE $tbl_lp_item SET
  546. previous_item_id = $new_item_id,
  547. next_item_id = $new_item_id,
  548. id = $new_item_id
  549. WHERE iid = $new_item_id";
  550. Database::query($sql);
  551. // Update all the items after the new item.
  552. $sql = "UPDATE " . $tbl_lp_item . "
  553. SET display_order = display_order + 1
  554. WHERE
  555. c_id = $course_id AND
  556. lp_id = " . $this->get_id() . " AND
  557. id <> " . $new_item_id . " AND
  558. parent_item_id = " . $parent . " AND
  559. display_order > " . $display_order;
  560. Database::query($sql);
  561. // Update the item that should come after the new item.
  562. $sql = "UPDATE " . $tbl_lp_item . "
  563. SET ref = " . $new_item_id . "
  564. WHERE c_id = $course_id AND id = " . $new_item_id;
  565. Database::query($sql);
  566. // Upload audio.
  567. if (!empty($_FILES['mp3']['name'])) {
  568. // Create the audio folder if it does not exist yet.
  569. global $_course;
  570. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  571. if (!is_dir($filepath . 'audio')) {
  572. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  573. $audio_id = add_document(
  574. $_course,
  575. '/audio',
  576. 'folder',
  577. 0,
  578. 'audio'
  579. );
  580. api_item_property_update(
  581. $_course,
  582. TOOL_DOCUMENT,
  583. $audio_id,
  584. 'FolderCreated',
  585. api_get_user_id(),
  586. null,
  587. null,
  588. null,
  589. null,
  590. api_get_session_id()
  591. );
  592. api_item_property_update(
  593. $_course,
  594. TOOL_DOCUMENT,
  595. $audio_id,
  596. 'invisible',
  597. api_get_user_id(),
  598. null,
  599. null,
  600. null,
  601. null,
  602. api_get_session_id()
  603. );
  604. }
  605. $file_path = handle_uploaded_document(
  606. $_course,
  607. $_FILES['mp3'],
  608. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  609. '/audio',
  610. api_get_user_id(),
  611. '',
  612. '',
  613. '',
  614. '',
  615. false
  616. );
  617. // Getting the filename only.
  618. $file_components = explode('/', $file_path);
  619. $file = $file_components[count($file_components) - 1];
  620. // Store the mp3 file in the lp_item table.
  621. $sql = "UPDATE $tbl_lp_item SET
  622. audio = '" . Database::escape_string($file) . "'
  623. WHERE id = '" . intval($new_item_id) . "'";
  624. Database::query($sql);
  625. }
  626. }
  627. return $new_item_id;
  628. }
  629. /**
  630. * Static admin function allowing addition of a learnpath to a course.
  631. * @param string Course code
  632. * @param string Learnpath name
  633. * @param string Learnpath description string, if provided
  634. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  635. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  636. * @param string Zip file containing the learnpath or directory containing the learnpath
  637. * @return integer The new learnpath ID on success, 0 on failure
  638. */
  639. public static function add_lp(
  640. $course,
  641. $name,
  642. $description = '',
  643. $learnpath = 'guess',
  644. $origin = 'zip',
  645. $zipname = '',
  646. $publicated_on = '',
  647. $expired_on = '',
  648. $categoryId = 0
  649. ) {
  650. global $charset;
  651. $course_id = api_get_course_int_id();
  652. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  653. // Check course code exists.
  654. // Check lp_name doesn't exist, otherwise append something.
  655. $i = 0;
  656. $name = Database::escape_string($name);
  657. $categoryId = intval($categoryId);
  658. // Session id.
  659. $session_id = api_get_session_id();
  660. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  661. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  662. $res_name = Database::query($check_name);
  663. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  664. //by default the publication date is the same that the creation date
  665. //The behaviour above was changed due BT#2800
  666. global $_custom;
  667. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  668. $publicated_on = '';
  669. } else {
  670. $publicated_on = api_get_utc_datetime();
  671. }
  672. } else {
  673. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  674. }
  675. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  676. $expired_on = '';
  677. } else {
  678. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  679. }
  680. while (Database :: num_rows($res_name)) {
  681. // There is already one such name, update the current one a bit.
  682. $i++;
  683. $name = $name . ' - ' . $i;
  684. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  685. $res_name = Database::query($check_name);
  686. }
  687. // New name does not exist yet; keep it.
  688. // Escape description.
  689. // Kevin: added htmlentities().
  690. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  691. $type = 1;
  692. switch ($learnpath) {
  693. case 'guess':
  694. break;
  695. case 'dokeos':
  696. case 'chamilo':
  697. $type = 1;
  698. break;
  699. case 'aicc':
  700. break;
  701. }
  702. switch ($origin) {
  703. case 'zip':
  704. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  705. break;
  706. case 'manual':
  707. default:
  708. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  709. $res_max = Database::query($get_max);
  710. if (Database :: num_rows($res_max) < 1) {
  711. $dsp = 1;
  712. } else {
  713. $row = Database :: fetch_array($res_max);
  714. $dsp = $row[0] + 1;
  715. }
  716. $sql = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id)
  717. VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."', $categoryId)";
  718. Database::query($sql);
  719. $id = Database :: insert_id();
  720. if ($id > 0) {
  721. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  722. Database::query($sql);
  723. $course_info = api_get_course_info();
  724. // Insert into item_property.
  725. api_item_property_update(
  726. $course_info,
  727. TOOL_LEARNPATH,
  728. $id,
  729. 'LearnpathAdded',
  730. api_get_user_id()
  731. );
  732. api_set_default_visibility($id, TOOL_LEARNPATH);
  733. return $id;
  734. }
  735. break;
  736. }
  737. }
  738. /**
  739. * Appends a message to the message attribute
  740. * @param string $string Message to append.
  741. */
  742. public function append_message($string)
  743. {
  744. if ($this->debug > 0) {
  745. error_log('New LP - In learnpath::append_message()', 0);
  746. }
  747. $this->message .= $string;
  748. }
  749. /**
  750. * Auto completes the parents of an item in case it's been completed or passed
  751. * @param integer $item Optional ID of the item from which to look for parents
  752. */
  753. public function autocomplete_parents($item)
  754. {
  755. $debug = $this->debug;
  756. if ($debug) {
  757. error_log('Learnpath::autocomplete_parents()', 0);
  758. }
  759. if (empty($item)) {
  760. $item = $this->current;
  761. }
  762. $currentItem = $this->getItem($item);
  763. if ($currentItem) {
  764. $parent_id = $currentItem->get_parent();
  765. $parent = $this->getItem($parent_id);
  766. if ($parent) {
  767. // if $item points to an object and there is a parent.
  768. if ($debug) {
  769. error_log(
  770. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  771. 0
  772. );
  773. }
  774. // New experiment including failed and browsed in completed status.
  775. //$current_status = $currentItem->get_status();
  776. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  777. // Fixes chapter auto complete
  778. if (true) {
  779. // If the current item is completed or passes or succeeded.
  780. $updateParentStatus = true;
  781. if ($debug) {
  782. error_log('Status of current item is alright', 0);
  783. }
  784. foreach ($parent->get_children() as $childItemId) {
  785. $childItem = $this->getItem($childItemId);
  786. // If children was not set try to get the info
  787. if (empty($childItem->db_item_view_id)) {
  788. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  789. }
  790. // Check all his brothers (parent's children) for completion status.
  791. if ($childItemId != $item) {
  792. if ($debug) {
  793. error_log(
  794. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  795. 0
  796. );
  797. }
  798. // Trying completing parents of failed and browsed items as well.
  799. if ($childItem->status_is(
  800. array(
  801. 'completed',
  802. 'passed',
  803. 'succeeded',
  804. 'browsed',
  805. 'failed'
  806. )
  807. )
  808. ) {
  809. // Keep completion status to true.
  810. continue;
  811. } else {
  812. if ($debug > 2) {
  813. error_log(
  814. 'Found one incomplete child of parent #' . $parent_id . ': child #'.$childItemId . ' "' . $childItem->get_title() . '", is ' . $childItem->get_status().' db_item_view_id:#'.$childItem->db_item_view_id,
  815. 0
  816. );
  817. }
  818. $updateParentStatus = false;
  819. break;
  820. }
  821. }
  822. }
  823. if ($updateParentStatus) {
  824. // If all the children were completed:
  825. $parent->set_status('completed');
  826. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  827. // Force the status to "completed"
  828. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  829. $this->update_queue[$parent->get_id()] = 'completed';
  830. if ($debug) {
  831. error_log(
  832. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  833. print_r($this->update_queue, 1),
  834. 0
  835. );
  836. }
  837. // Recursive call.
  838. $this->autocomplete_parents($parent->get_id());
  839. }
  840. }
  841. } else {
  842. if ($debug) {
  843. error_log("Parent #$parent_id does not exists");
  844. }
  845. }
  846. } else {
  847. if ($debug) {
  848. error_log("#$item is an item that doesn't have parents");
  849. }
  850. }
  851. }
  852. /**
  853. * Auto saves the current results into the database for the whole learnpath
  854. */
  855. public function autosave()
  856. {
  857. if ($this->debug > 0) {
  858. error_log('New LP - In learnpath::autosave()', 0);
  859. }
  860. // TODO: Add save operations for the learnpath itself.
  861. }
  862. /**
  863. * Clears the message attribute
  864. */
  865. public function clear_message()
  866. {
  867. if ($this->debug > 0) {
  868. error_log('New LP - In learnpath::clear_message()', 0);
  869. }
  870. $this->message = '';
  871. }
  872. /**
  873. * Closes the current resource
  874. *
  875. * Stops the timer
  876. * Saves into the database if required
  877. * Clears the current resource data from this object
  878. * @return boolean True on success, false on failure
  879. */
  880. public function close()
  881. {
  882. $course_id = api_get_course_int_id();
  883. if ($this->debug > 0) {
  884. error_log('New LP - In learnpath::close()', 0);
  885. }
  886. if (empty ($this->lp_id)) {
  887. $this->error = 'Trying to close this learnpath but no ID is set';
  888. return false;
  889. }
  890. $this->current_time_stop = time();
  891. if ($this->save) {
  892. /*
  893. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  894. $sql = "UPDATE $learnpath_view_table " .
  895. "SET " .
  896. "stop_time = ".$this->current_time_stop.", " .
  897. "score = ".$this->current_score.", ".
  898. "WHERE learnpath_id = '".$this->lp_id."'";
  899. //$res = Database::query($sql);
  900. $res = Database::query($res);
  901. if (Database::affected_rows($res) < 1) {
  902. $this->error = 'Could not update learnpath_view table while closing learnpath';
  903. return false;
  904. }
  905. */
  906. }
  907. $this->ordered_items = array();
  908. $this->index = 0;
  909. unset ($this->lp_id);
  910. //unset other stuff
  911. return true;
  912. }
  913. /**
  914. * Static admin function allowing removal of a learnpath
  915. * @param string Course code
  916. * @param integer Learnpath ID
  917. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  918. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  919. */
  920. public function delete($course = null, $id = null, $delete = 'keep')
  921. {
  922. $course_id = api_get_course_int_id();
  923. // TODO: Implement a way of getting this to work when the current object is not set.
  924. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  925. // If an ID is specifically given and the current LP is not the same, prevent delete.
  926. if (!empty ($id) && ($id != $this->lp_id)) {
  927. return false;
  928. }
  929. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  930. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  931. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  932. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  933. // Delete lp item id.
  934. foreach ($this->items as $id => $dummy) {
  935. $sql = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  936. Database::query($sql);
  937. }
  938. // Proposed by Christophe (nickname: clefevre)
  939. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  940. Database::query($sql);
  941. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  942. Database::query($sql);
  943. self::toggle_publish($this->lp_id, 'i');
  944. if ($this->type == 2 || $this->type == 3) {
  945. // This is a scorm learning path, delete the files as well.
  946. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  947. $res = Database::query($sql);
  948. if (Database :: num_rows($res) > 0) {
  949. $row = Database :: fetch_array($res);
  950. $path = $row['path'];
  951. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  952. $res = Database::query($sql);
  953. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  954. if ($this->debug > 2) {
  955. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  956. }
  957. } else {
  958. // No other LP uses that directory, delete it.
  959. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  960. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  961. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  962. if ($this->debug > 2) {
  963. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  964. }
  965. // Proposed by Christophe (clefevre).
  966. if (strcmp(substr($path, -2), "/.") == 0) {
  967. $path = substr($path, 0, -1); // Remove "." at the end.
  968. }
  969. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  970. rmdirr($course_scorm_dir . $path);
  971. }
  972. }
  973. }
  974. }
  975. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  976. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  977. // Delete tools
  978. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  979. Database::query($sql);
  980. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  981. Database::query($sql);
  982. // Updates the display order of all lps.
  983. $this->update_display_order();
  984. api_item_property_update(
  985. api_get_course_info(),
  986. TOOL_LEARNPATH,
  987. $this->lp_id,
  988. 'delete',
  989. api_get_user_id()
  990. );
  991. // Delete link of gradebook tool
  992. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  993. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  994. $result = Database::query($sql);
  995. $row = Database :: fetch_array($result, 'ASSOC');*/
  996. // Fixing gradebook link deleted see #5229.
  997. /*
  998. if (!empty($row['id'])) {
  999. $link = LinkFactory :: load($row['id']);
  1000. if ($link[0] != null) {
  1001. $link[0]->delete();
  1002. }
  1003. }*/
  1004. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1005. if ($link_info !== false) {
  1006. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1007. }
  1008. if (api_get_setting('search_enabled') == 'true') {
  1009. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1010. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1011. }
  1012. }
  1013. /**
  1014. * Removes all the children of one item - dangerous!
  1015. * @param integer $id Element ID of which children have to be removed
  1016. * @return integer Total number of children removed
  1017. */
  1018. public function delete_children_items($id)
  1019. {
  1020. $course_id = api_get_course_int_id();
  1021. if ($this->debug > 0) {
  1022. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1023. }
  1024. $num = 0;
  1025. if (empty ($id) || $id != strval(intval($id))) {
  1026. return false;
  1027. }
  1028. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1029. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1030. $res = Database::query($sql);
  1031. while ($row = Database :: fetch_array($res)) {
  1032. $num += $this->delete_children_items($row['id']);
  1033. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1034. Database::query($sql_del);
  1035. $num++;
  1036. }
  1037. return $num;
  1038. }
  1039. /**
  1040. * Removes an item from the current learnpath
  1041. * @param integer $id Elem ID (0 if first)
  1042. * @param integer $remove Whether to remove the resource/data from the
  1043. * system or leave it (default: 'keep', others 'remove')
  1044. * @return integer Number of elements moved
  1045. * @todo implement resource removal
  1046. */
  1047. public function delete_item($id, $remove = 'keep')
  1048. {
  1049. $course_id = api_get_course_int_id();
  1050. if ($this->debug > 0) {
  1051. error_log('New LP - In learnpath::delete_item()', 0);
  1052. }
  1053. // TODO: Implement the resource removal.
  1054. if (empty ($id) || $id != strval(intval($id))) {
  1055. return false;
  1056. }
  1057. // First select item to get previous, next, and display order.
  1058. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1059. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1060. $res_sel = Database::query($sql_sel);
  1061. if (Database :: num_rows($res_sel) < 1) {
  1062. return false;
  1063. }
  1064. $row = Database :: fetch_array($res_sel);
  1065. $previous = $row['previous_item_id'];
  1066. $next = $row['next_item_id'];
  1067. $display = $row['display_order'];
  1068. $parent = $row['parent_item_id'];
  1069. $lp = $row['lp_id'];
  1070. // Delete children items.
  1071. $num = $this->delete_children_items($id);
  1072. if ($this->debug > 2) {
  1073. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1074. }
  1075. // Now delete the item.
  1076. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1077. if ($this->debug > 2) {
  1078. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1079. }
  1080. Database::query($sql_del);
  1081. // Now update surrounding items.
  1082. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1083. WHERE c_id = ".$course_id." AND id = $previous";
  1084. Database::query($sql_upd);
  1085. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1086. WHERE c_id = ".$course_id." AND id = $next";
  1087. Database::query($sql_upd);
  1088. // Now update all following items with new display order.
  1089. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1090. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1091. Database::query($sql_all);
  1092. //Removing prerequisites since the item will not longer exist
  1093. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1094. Database::query($sql_all);
  1095. // Remove from search engine if enabled.
  1096. if (api_get_setting('search_enabled') == 'true') {
  1097. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1098. $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';
  1099. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1100. $res = Database::query($sql);
  1101. if (Database :: num_rows($res) > 0) {
  1102. $row2 = Database :: fetch_array($res);
  1103. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1104. $di = new ChamiloIndexer();
  1105. $di->remove_document((int) $row2['search_did']);
  1106. }
  1107. $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';
  1108. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1109. Database::query($sql);
  1110. }
  1111. }
  1112. /**
  1113. * Updates an item's content in place
  1114. * @param integer Element ID
  1115. * @param integer Parent item ID
  1116. * @param integer Previous item ID
  1117. * @param string Item title
  1118. * @param string Item description
  1119. * @param string Prerequisites (optional)
  1120. * @param string Indexing terms (optional)
  1121. * @param array The array resulting of the $_FILES[mp3] element
  1122. * @return boolean True on success, false on error
  1123. */
  1124. public function edit_item(
  1125. $id,
  1126. $parent,
  1127. $previous,
  1128. $title,
  1129. $description,
  1130. $prerequisites = 0,
  1131. $audio = null,
  1132. $max_time_allowed = 0,
  1133. $url = ''
  1134. ) {
  1135. $course_id = api_get_course_int_id();
  1136. if ($this->debug > 0) {
  1137. error_log('New LP - In learnpath::edit_item()', 0);
  1138. }
  1139. if (empty ($max_time_allowed)) {
  1140. $max_time_allowed = 0;
  1141. }
  1142. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1143. return false;
  1144. }
  1145. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1146. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1147. $res_select = Database::query($sql_select);
  1148. $row_select = Database :: fetch_array($res_select);
  1149. $audio_update_sql = '';
  1150. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1151. // Create the audio folder if it does not exist yet.
  1152. global $_course;
  1153. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1154. if (!is_dir($filepath . 'audio')) {
  1155. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1156. $audio_id = add_document(
  1157. $_course,
  1158. '/audio',
  1159. 'folder',
  1160. 0,
  1161. 'audio'
  1162. );
  1163. api_item_property_update(
  1164. $_course,
  1165. TOOL_DOCUMENT,
  1166. $audio_id,
  1167. 'FolderCreated',
  1168. api_get_user_id(),
  1169. null,
  1170. null,
  1171. null,
  1172. null,
  1173. api_get_session_id()
  1174. );
  1175. api_item_property_update(
  1176. $_course,
  1177. TOOL_DOCUMENT,
  1178. $audio_id,
  1179. 'invisible',
  1180. api_get_user_id(),
  1181. null,
  1182. null,
  1183. null,
  1184. null,
  1185. api_get_session_id()
  1186. );
  1187. }
  1188. // Upload file in documents.
  1189. $pi = pathinfo($audio['name']);
  1190. if ($pi['extension'] == 'mp3') {
  1191. $c_det = api_get_course_info($this->cc);
  1192. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1193. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1194. $path = substr($path, 7);
  1195. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1196. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1197. }
  1198. }
  1199. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1200. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1201. // TODO: htmlspecialchars to be checked for encoding related problems.
  1202. if ($same_parent && $same_previous) {
  1203. // Only update title and description.
  1204. $sql = "UPDATE " . $tbl_lp_item . "
  1205. SET title = '" . Database::escape_string($title) . "',
  1206. prerequisite = '" . $prerequisites . "',
  1207. description = '" . Database::escape_string($description) . "'
  1208. " . $audio_update_sql . ",
  1209. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1210. WHERE c_id = ".$course_id." AND id = " . $id;
  1211. Database::query($sql);
  1212. } else {
  1213. $old_parent = $row_select['parent_item_id'];
  1214. $old_previous = $row_select['previous_item_id'];
  1215. $old_next = $row_select['next_item_id'];
  1216. $old_order = $row_select['display_order'];
  1217. $old_prerequisite = $row_select['prerequisite'];
  1218. $old_max_time_allowed = $row_select['max_time_allowed'];
  1219. /* BEGIN -- virtually remove the current item id */
  1220. /* for the next and previous item it is like the current item doesn't exist anymore */
  1221. if ($old_previous != 0) {
  1222. // Next
  1223. $sql = "UPDATE " . $tbl_lp_item . "
  1224. SET next_item_id = " . $old_next . "
  1225. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1226. Database::query($sql);
  1227. }
  1228. if ($old_next != 0) {
  1229. // Previous
  1230. $sql = "UPDATE " . $tbl_lp_item . "
  1231. SET previous_item_id = " . $old_previous . "
  1232. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1233. Database::query($sql);
  1234. }
  1235. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1236. $sql = "UPDATE " . $tbl_lp_item . "
  1237. SET display_order = display_order - 1
  1238. WHERE
  1239. c_id = ".$course_id." AND
  1240. display_order > " . $old_order . " AND
  1241. lp_id = " . $this->lp_id . " AND
  1242. parent_item_id = " . $old_parent;
  1243. Database::query($sql);
  1244. /* END -- virtually remove the current item id */
  1245. /* BEGIN -- update the current item id to his new location */
  1246. if ($previous == 0) {
  1247. // Select the data of the item that should come after the current item.
  1248. $sql = "SELECT id, display_order
  1249. FROM " . $tbl_lp_item . "
  1250. WHERE
  1251. c_id = ".$course_id." AND
  1252. lp_id = " . $this->lp_id . " AND
  1253. parent_item_id = " . $parent . " AND
  1254. previous_item_id = " . $previous;
  1255. $res_select_old = Database::query($sql);
  1256. $row_select_old = Database::fetch_array($res_select_old);
  1257. // If the new parent didn't have children before.
  1258. if (Database :: num_rows($res_select_old) == 0) {
  1259. $new_next = 0;
  1260. $new_order = 1;
  1261. } else {
  1262. $new_next = $row_select_old['id'];
  1263. $new_order = $row_select_old['display_order'];
  1264. }
  1265. } else {
  1266. // Select the data of the item that should come before the current item.
  1267. $sql = "SELECT next_item_id, display_order
  1268. FROM " . $tbl_lp_item . "
  1269. WHERE c_id = ".$course_id." AND id = " . $previous;
  1270. $res_select_old = Database::query($sql);
  1271. $row_select_old = Database :: fetch_array($res_select_old);
  1272. $new_next = $row_select_old['next_item_id'];
  1273. $new_order = $row_select_old['display_order'] + 1;
  1274. }
  1275. // TODO: htmlspecialchars to be checked for encoding related problems.
  1276. // Update the current item with the new data.
  1277. $sql = "UPDATE " . $tbl_lp_item . "
  1278. SET
  1279. title = '" . Database::escape_string($title) . "',
  1280. description = '" . Database::escape_string($description) . "',
  1281. parent_item_id = " . $parent . ",
  1282. previous_item_id = " . $previous . ",
  1283. next_item_id = " . $new_next . ",
  1284. display_order = " . $new_order . "
  1285. " . $audio_update_sql . "
  1286. WHERE c_id = ".$course_id." AND id = " . $id;
  1287. Database::query($sql);
  1288. if ($previous != 0) {
  1289. // Update the previous item's next_item_id.
  1290. $sql = "UPDATE " . $tbl_lp_item . "
  1291. SET next_item_id = " . $id . "
  1292. WHERE c_id = ".$course_id." AND id = " . $previous;
  1293. Database::query($sql);
  1294. }
  1295. if ($new_next != 0) {
  1296. // Update the next item's previous_item_id.
  1297. $sql = "UPDATE " . $tbl_lp_item . "
  1298. SET previous_item_id = " . $id . "
  1299. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1300. Database::query($sql);
  1301. }
  1302. if ($old_prerequisite != $prerequisites) {
  1303. $sql = "UPDATE " . $tbl_lp_item . "
  1304. SET prerequisite = " . $prerequisites . "
  1305. WHERE c_id = ".$course_id." AND id = " . $id;
  1306. Database::query($sql);
  1307. }
  1308. if ($old_max_time_allowed != $max_time_allowed) {
  1309. // update max time allowed
  1310. $sql = "UPDATE " . $tbl_lp_item . "
  1311. SET max_time_allowed = " . $max_time_allowed . "
  1312. WHERE c_id = ".$course_id." AND id = " . $id;
  1313. Database::query($sql);
  1314. }
  1315. // Update all the items with the same or a bigger display_order than the current item.
  1316. $sql = "UPDATE " . $tbl_lp_item . "
  1317. SET display_order = display_order + 1
  1318. WHERE
  1319. c_id = ".$course_id." AND
  1320. lp_id = " . $this->get_id() . " AND
  1321. id <> " . $id . " AND
  1322. parent_item_id = " . $parent . " AND
  1323. display_order >= " . $new_order;
  1324. Database::query($sql);
  1325. }
  1326. if ($row_select['item_type'] == 'link') {
  1327. $link = new Link();
  1328. $linkId = $row_select['path'];
  1329. $link->updateLink($linkId, $url);
  1330. }
  1331. }
  1332. /**
  1333. * Updates an item's prereq in place
  1334. * @param integer $id Element ID
  1335. * @param string $prerequisite_id Prerequisite Element ID
  1336. * @param string $mastery_score Prerequisite min score
  1337. * @param string $max_score Prerequisite max score
  1338. *
  1339. * @return boolean True on success, false on error
  1340. */
  1341. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1342. {
  1343. $course_id = api_get_course_int_id();
  1344. if ($this->debug > 0) {
  1345. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1346. }
  1347. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1348. return false;
  1349. }
  1350. $prerequisite_id = intval($prerequisite_id);
  1351. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1352. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1353. $mastery_score = 0;
  1354. }
  1355. if (!is_numeric($max_score) || $max_score < 0) {
  1356. $max_score = 100;
  1357. }
  1358. /*if ($mastery_score > $max_score) {
  1359. $max_score = $mastery_score;
  1360. }*/
  1361. if (!is_numeric($prerequisite_id)) {
  1362. $prerequisite_id = 'NULL';
  1363. }
  1364. $mastery_score = floatval($mastery_score);
  1365. $max_score = floatval($max_score);
  1366. $sql = " UPDATE $tbl_lp_item
  1367. SET
  1368. prerequisite = $prerequisite_id ,
  1369. prerequisite_min_score = $mastery_score ,
  1370. prerequisite_max_score = $max_score
  1371. WHERE c_id = $course_id AND id = $id";
  1372. Database::query($sql);
  1373. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1374. // Will this be enough to ensure unicity?
  1375. /*$sql = " UPDATE $tbl_lp_item
  1376. SET mastery_score = $mastery_score
  1377. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1378. Database::query($sql);*/
  1379. }
  1380. // TODO: Update the item object (can be ignored for now because refreshed).
  1381. return true;
  1382. }
  1383. /**
  1384. * Escapes a string with the available database escape function
  1385. * @param string String to escape
  1386. * @return string String escaped
  1387. * @deprecated use Database::escape_string
  1388. */
  1389. public function escape_string($string)
  1390. {
  1391. return Database::escape_string($string);
  1392. }
  1393. /**
  1394. * Static admin function exporting a learnpath into a zip file
  1395. * @param string Export type (scorm, zip, cd)
  1396. * @param string Course code
  1397. * @param integer Learnpath ID
  1398. * @param string Zip file name
  1399. * @return string Zip file path (or false on error)
  1400. */
  1401. public function export_lp($type, $course, $id, $zipname)
  1402. {
  1403. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1404. return false;
  1405. }
  1406. $url = '';
  1407. switch ($type) {
  1408. case 'scorm':
  1409. break;
  1410. case 'zip':
  1411. break;
  1412. case 'cdrom':
  1413. break;
  1414. }
  1415. return $url;
  1416. }
  1417. /**
  1418. * Gets all the chapters belonging to the same parent as the item/chapter given
  1419. * Can also be called as abstract method
  1420. * @param integer Item ID
  1421. * @return array A list of all the "brother items" (or an empty array on failure)
  1422. */
  1423. public function get_brother_chapters($id)
  1424. {
  1425. $course_id = api_get_course_int_id();
  1426. if ($this->debug > 0) {
  1427. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1428. }
  1429. if (empty($id)|| $id != strval(intval($id))) {
  1430. return array ();
  1431. }
  1432. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1433. $sql_parent = "SELECT * FROM $lp_item
  1434. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1435. $res_parent = Database::query($sql_parent);
  1436. if (Database :: num_rows($res_parent) > 0) {
  1437. $row_parent = Database :: fetch_array($res_parent);
  1438. $parent = $row_parent['parent_item_id'];
  1439. $sql_bros = "SELECT * FROM $lp_item
  1440. WHERE
  1441. c_id = ".$course_id." AND
  1442. parent_item_id = $parent AND
  1443. id = $id AND
  1444. item_type='dokeos_chapter'
  1445. ORDER BY display_order";
  1446. $res_bros = Database::query($sql_bros);
  1447. $list = array ();
  1448. while ($row_bro = Database :: fetch_array($res_bros)) {
  1449. $list[] = $row_bro;
  1450. }
  1451. return $list;
  1452. }
  1453. return array ();
  1454. }
  1455. /**
  1456. * Gets all the items belonging to the same parent as the item given
  1457. * Can also be called as abstract method
  1458. * @param integer Item ID
  1459. * @return array A list of all the "brother items" (or an empty array on failure)
  1460. */
  1461. public function get_brother_items($id) {
  1462. $course_id = api_get_course_int_id();
  1463. if ($this->debug > 0) {
  1464. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1465. }
  1466. if (empty ($id) || $id != strval(intval($id))) {
  1467. return array ();
  1468. }
  1469. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1470. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1471. $res_parent = Database::query($sql_parent);
  1472. if (Database :: num_rows($res_parent) > 0) {
  1473. $row_parent = Database :: fetch_array($res_parent);
  1474. $parent = $row_parent['parent_item_id'];
  1475. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1476. ORDER BY display_order";
  1477. $res_bros = Database::query($sql_bros);
  1478. $list = array ();
  1479. while ($row_bro = Database :: fetch_array($res_bros)) {
  1480. $list[] = $row_bro;
  1481. }
  1482. return $list;
  1483. }
  1484. return array ();
  1485. }
  1486. /**
  1487. * Get the specific prefix index terms of this learning path
  1488. * @param string $prefix
  1489. * @return array Array of terms
  1490. */
  1491. public function get_common_index_terms_by_prefix($prefix)
  1492. {
  1493. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1494. $terms = get_specific_field_values_list_by_prefix(
  1495. $prefix,
  1496. $this->cc,
  1497. TOOL_LEARNPATH,
  1498. $this->lp_id
  1499. );
  1500. $prefix_terms = array();
  1501. if (!empty($terms)) {
  1502. foreach ($terms as $term) {
  1503. $prefix_terms[] = $term['value'];
  1504. }
  1505. }
  1506. return $prefix_terms;
  1507. }
  1508. /**
  1509. * Gets the number of items currently completed
  1510. * @return integer The number of items currently completed
  1511. */
  1512. public function get_complete_items_count()
  1513. {
  1514. if ($this->debug > 0) {
  1515. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1516. }
  1517. $i = 0;
  1518. $completedStatusList = array(
  1519. 'completed',
  1520. 'passed',
  1521. 'succeeded',
  1522. 'browsed',
  1523. 'failed'
  1524. );
  1525. foreach ($this->items as $id => $dummy) {
  1526. // Trying failed and browsed considered "progressed" as well.
  1527. if ($this->items[$id]->status_is($completedStatusList) &&
  1528. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1529. $this->items[$id]->get_type() != 'dir'
  1530. ) {
  1531. $i++;
  1532. }
  1533. }
  1534. return $i;
  1535. }
  1536. /**
  1537. * Gets the current item ID
  1538. * @return integer The current learnpath item id
  1539. */
  1540. public function get_current_item_id()
  1541. {
  1542. $current = 0;
  1543. if ($this->debug > 0) {
  1544. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1545. }
  1546. if (!empty($this->current)) {
  1547. $current = $this->current;
  1548. }
  1549. if ($this->debug > 2) {
  1550. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1551. }
  1552. return $current;
  1553. }
  1554. /**
  1555. * Force to get the first learnpath item id
  1556. * @return integer The current learnpath item id
  1557. */
  1558. public function get_first_item_id()
  1559. {
  1560. $current = 0;
  1561. if (is_array($this->ordered_items)) {
  1562. $current = $this->ordered_items[0];
  1563. }
  1564. return $current;
  1565. }
  1566. /**
  1567. * Gets the total number of items available for viewing in this SCORM
  1568. * @return integer The total number of items
  1569. */
  1570. public function get_total_items_count()
  1571. {
  1572. if ($this->debug > 0) {
  1573. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1574. }
  1575. return count($this->items);
  1576. }
  1577. /**
  1578. * Gets the total number of items available for viewing in this SCORM but without chapters
  1579. * @return integer The total no-chapters number of items
  1580. */
  1581. public function get_total_items_count_without_chapters()
  1582. {
  1583. if ($this->debug > 0) {
  1584. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1585. }
  1586. $total = 0;
  1587. $typeListNotToCount = self::getChapterTypes();
  1588. foreach ($this->items as $temp2) {
  1589. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1590. $total++;
  1591. }
  1592. }
  1593. return $total;
  1594. }
  1595. /**
  1596. * Gets the first element URL.
  1597. * @return string URL to load into the viewer
  1598. */
  1599. public function first()
  1600. {
  1601. if ($this->debug > 0) {
  1602. error_log('New LP - In learnpath::first()', 0);
  1603. error_log('$this->last_item_seen '.$this->last_item_seen);
  1604. }
  1605. // Test if the last_item_seen exists and is not a dir.
  1606. if (count($this->ordered_items) == 0) {
  1607. $this->index = 0;
  1608. }
  1609. if ($this->debug > 0) {
  1610. if (isset($this->items[$this->last_item_seen])) {
  1611. $status = $this->items[$this->last_item_seen]->get_status();
  1612. }
  1613. error_log('status '.$status);
  1614. }
  1615. if (!empty($this->last_item_seen) &&
  1616. !empty($this->items[$this->last_item_seen]) &&
  1617. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1618. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1619. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1620. //&& !$this->items[$this->last_item_seen]->is_done()
  1621. ) {
  1622. if ($this->debug > 2) {
  1623. 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);
  1624. }
  1625. $index = -1;
  1626. foreach ($this->ordered_items as $myindex => $item_id) {
  1627. if ($item_id == $this->last_item_seen) {
  1628. $index = $myindex;
  1629. break;
  1630. }
  1631. }
  1632. if ($index == -1) {
  1633. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1634. if ($this->debug > 2) {
  1635. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1636. }
  1637. return false;
  1638. } else {
  1639. $this->last = $this->last_item_seen;
  1640. $this->current = $this->last_item_seen;
  1641. $this->index = $index;
  1642. }
  1643. } else {
  1644. if ($this->debug > 2) {
  1645. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1646. }
  1647. $index = 0;
  1648. // Loop through all ordered items and stop at the first item that is
  1649. // not a directory *and* that has not been completed yet.
  1650. while ( !empty($this->ordered_items[$index]) AND
  1651. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1652. (
  1653. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1654. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1655. $this->items[$this->ordered_items[$index]]->is_done() === true
  1656. ) AND $index < $this->max_ordered_items) {
  1657. $index++;
  1658. }
  1659. $this->last = $this->current;
  1660. // current is
  1661. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1662. $this->index = $index;
  1663. if ($this->debug > 2) {
  1664. error_log('$index ' . $index);
  1665. }
  1666. if ($this->debug > 2) {
  1667. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1668. }
  1669. }
  1670. if ($this->debug > 2) {
  1671. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1672. }
  1673. }
  1674. /**
  1675. * Gets the information about an item in a format usable as JavaScript to update
  1676. * the JS API by just printing this content into the <head> section of the message frame
  1677. * @param int $item_id
  1678. * @return string
  1679. */
  1680. public function get_js_info($item_id = '')
  1681. {
  1682. if ($this->debug > 0) {
  1683. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1684. }
  1685. $info = '';
  1686. $item_id = intval($item_id);
  1687. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1688. //if item is defined, return values from DB
  1689. $oItem = $this->items[$item_id];
  1690. $info .= '<script language="javascript">';
  1691. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1692. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1693. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1694. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1695. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1696. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1697. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1698. $info .= "top.set_flag_synchronized();";
  1699. $info .= '</script>';
  1700. if ($this->debug > 2) {
  1701. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1702. }
  1703. return $info;
  1704. } else {
  1705. // If item_id is empty, just update to default SCORM data.
  1706. $info .= '<script language="javascript">';
  1707. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1708. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1709. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1710. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1711. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1712. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1713. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1714. $info .= "top.set_flag_synchronized();";
  1715. $info .= '</script>';
  1716. if ($this->debug > 2) {
  1717. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1718. }
  1719. return $info;
  1720. }
  1721. }
  1722. /**
  1723. * Gets the js library from the database
  1724. * @return string The name of the javascript library to be used
  1725. */
  1726. public function get_js_lib()
  1727. {
  1728. $lib = '';
  1729. if (!empty ($this->js_lib)) {
  1730. $lib = $this->js_lib;
  1731. }
  1732. return $lib;
  1733. }
  1734. /**
  1735. * Gets the learnpath database ID
  1736. * @return integer Learnpath ID in the lp table
  1737. */
  1738. public function get_id()
  1739. {
  1740. if (!empty ($this->lp_id)) {
  1741. return $this->lp_id;
  1742. } else {
  1743. return 0;
  1744. }
  1745. }
  1746. /**
  1747. * Gets the last element URL.
  1748. * @return string URL to load into the viewer
  1749. */
  1750. public function get_last()
  1751. {
  1752. if ($this->debug > 0) {
  1753. error_log('New LP - In learnpath::get_last()', 0);
  1754. }
  1755. $this->index = count($this->ordered_items) - 1;
  1756. return $this->ordered_items[$this->index];
  1757. }
  1758. /**
  1759. * Gets the navigation bar for the learnpath display screen
  1760. * @return string The HTML string to use as a navigation bar
  1761. */
  1762. public function get_navigation_bar($idBar = null, $display=null) {
  1763. if ($this->debug > 0) {
  1764. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1765. }
  1766. if(empty($idBar)){
  1767. $idBar='control-top';
  1768. }
  1769. if(empty($display)){
  1770. $display='display:block';
  1771. }
  1772. $navbar = null;
  1773. $lp_id = $this->lp_id;
  1774. $mycurrentitemid = $this->get_current_item_id();
  1775. if ($this->mode == 'fullscreen') {
  1776. $navbar = '
  1777. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1778. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1779. <a id="scorm-previous" href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1780. <a id="scorm-next" href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1781. <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>
  1782. </div>';
  1783. } else {
  1784. $navbar = '
  1785. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1786. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1787. <a id="scorm-previous" href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1788. <a id="scorm-next" href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>
  1789. </div>';
  1790. }
  1791. return $navbar;
  1792. }
  1793. /**
  1794. * Gets the next resource in queue (url).
  1795. * @return string URL to load into the viewer
  1796. */
  1797. public function get_next_index()
  1798. {
  1799. if ($this->debug > 0) {
  1800. error_log('New LP - In learnpath::get_next_index()', 0);
  1801. }
  1802. // TODO
  1803. $index = $this->index;
  1804. $index++;
  1805. if ($this->debug > 2) {
  1806. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1807. }
  1808. 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) {
  1809. $index++;
  1810. if ($index == $this->max_ordered_items){
  1811. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1812. return $this->index;
  1813. } else {
  1814. return $index;
  1815. }
  1816. }
  1817. }
  1818. if (empty ($this->ordered_items[$index])) {
  1819. return $this->index;
  1820. }
  1821. if ($this->debug > 2) {
  1822. error_log('New LP - index is now ' . $index, 0);
  1823. }
  1824. return $index;
  1825. }
  1826. /**
  1827. * Gets item_id for the next element
  1828. * @return integer Next item (DB) ID
  1829. */
  1830. public function get_next_item_id()
  1831. {
  1832. if ($this->debug > 0) {
  1833. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1834. }
  1835. $new_index = $this->get_next_index();
  1836. if (!empty ($new_index)) {
  1837. if (isset ($this->ordered_items[$new_index])) {
  1838. if ($this->debug > 2) {
  1839. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1840. }
  1841. return $this->ordered_items[$new_index];
  1842. }
  1843. }
  1844. if ($this->debug > 2) {
  1845. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1846. }
  1847. return 0;
  1848. }
  1849. /**
  1850. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1851. *
  1852. * Generally, the package provided is in the form of a zip file, so the function
  1853. * has been written to test a zip file. If not a zip, the function will return the
  1854. * default return value: ''
  1855. * @param string the path to the file
  1856. * @param string the original name of the file
  1857. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1858. */
  1859. public static function get_package_type($file_path, $file_name)
  1860. {
  1861. // Get name of the zip file without the extension.
  1862. $file_info = pathinfo($file_name);
  1863. $filename = $file_info['basename']; // Name including extension.
  1864. $extension = $file_info['extension']; // Extension only.
  1865. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1866. 'dll',
  1867. 'exe'
  1868. ))) {
  1869. return 'oogie';
  1870. }
  1871. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1872. 'dll',
  1873. 'exe'
  1874. ))) {
  1875. return 'woogie';
  1876. }
  1877. // Filename without its extension.
  1878. $file_base_name = str_replace('.' . $extension, '', $filename);
  1879. $zipFile = new PclZip($file_path);
  1880. // Check the zip content (real size and file extension).
  1881. $zipContentArray = $zipFile->listContent();
  1882. $package_type = '';
  1883. $at_root = false;
  1884. $manifest = '';
  1885. $aicc_match_crs = 0;
  1886. $aicc_match_au = 0;
  1887. $aicc_match_des = 0;
  1888. $aicc_match_cst = 0;
  1889. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1890. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1891. foreach ($zipContentArray as $thisContent) {
  1892. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1893. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1894. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1895. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1896. $package_type = 'scorm';
  1897. break; // Exit the foreach loop.
  1898. } elseif (
  1899. preg_match('/aicc\//i', $thisContent['filename']) ||
  1900. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1901. ) {
  1902. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1903. switch ($ext) {
  1904. case 'crs':
  1905. $aicc_match_crs = 1;
  1906. break;
  1907. case 'au':
  1908. $aicc_match_au = 1;
  1909. break;
  1910. case 'des':
  1911. $aicc_match_des = 1;
  1912. break;
  1913. case 'cst':
  1914. $aicc_match_cst = 1;
  1915. break;
  1916. default:
  1917. break;
  1918. }
  1919. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1920. } else {
  1921. $package_type = '';
  1922. }
  1923. }
  1924. }
  1925. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1926. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1927. $package_type = 'aicc';
  1928. }
  1929. return $package_type;
  1930. }
  1931. /**
  1932. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1933. * @return string URL to load into the viewer
  1934. */
  1935. public function get_previous_index()
  1936. {
  1937. if ($this->debug > 0) {
  1938. error_log('New LP - In learnpath::get_previous_index()', 0);
  1939. }
  1940. $index = $this->index;
  1941. if (isset ($this->ordered_items[$index -1])) {
  1942. $index--;
  1943. 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')) {
  1944. $index--;
  1945. if ($index < 0) {
  1946. return $this->index;
  1947. }
  1948. }
  1949. } else {
  1950. if ($this->debug > 2) {
  1951. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1952. }
  1953. // There is no previous item.
  1954. }
  1955. return $index;
  1956. }
  1957. /**
  1958. * Gets item_id for the next element
  1959. * @return integer Previous item (DB) ID
  1960. */
  1961. public function get_previous_item_id()
  1962. {
  1963. if ($this->debug > 0) {
  1964. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1965. }
  1966. $new_index = $this->get_previous_index();
  1967. return $this->ordered_items[$new_index];
  1968. }
  1969. /**
  1970. * Gets the progress value from the progress_db attribute
  1971. * @return integer Current progress value
  1972. */
  1973. public function get_progress()
  1974. {
  1975. if ($this->debug > 0) {
  1976. error_log('New LP - In learnpath::get_progress()', 0);
  1977. }
  1978. if (!empty ($this->progress_db)) {
  1979. return $this->progress_db;
  1980. }
  1981. return 0;
  1982. }
  1983. /**
  1984. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1985. * @param integer $lp_id Learnpath ID
  1986. * @param integer $user_id User ID
  1987. * @param string $mode Mode of display ('%','abs' or 'both')
  1988. * @param string $course_code Course code (optional, defaults to '')
  1989. * @param boolean $sincere Whether to return null if no record
  1990. * was found (true), or 0 (false) (optional, defaults to false)
  1991. * @param int $session_id
  1992. * @return integer Current progress value as found in the database
  1993. * @deprecated
  1994. */
  1995. public static function get_db_progress(
  1996. $lp_id,
  1997. $user_id,
  1998. $mode = '%',
  1999. $course_code = '',
  2000. $sincere = false,
  2001. $session_id = 0
  2002. ) {
  2003. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2004. $lp_id = intval($lp_id);
  2005. $session_id = intval($session_id);
  2006. $user_id = intval($user_id);
  2007. $course_info = api_get_course_info($course_code);
  2008. $session_condition = api_get_session_condition($session_id);
  2009. $course_id = $course_info['real_id'];
  2010. $sql = "SELECT * FROM $table
  2011. WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  2012. $res = Database::query($sql);
  2013. $view_id = 0;
  2014. if (Database :: num_rows($res) > 0) {
  2015. $row = Database :: fetch_array($res);
  2016. $progress = $row['progress'];
  2017. $view_id = $row['id'];
  2018. } else {
  2019. if ($sincere) {
  2020. return null;
  2021. }
  2022. }
  2023. if (empty($progress)) {
  2024. $progress = '0';
  2025. }
  2026. if ($mode == '%') {
  2027. return $progress . '%';
  2028. } else {
  2029. // Get the number of items completed and the number of items total.
  2030. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2031. $sql = "SELECT count(*) FROM $tbl
  2032. WHERE
  2033. c_id = $course_id AND
  2034. c_id = ".$course_id." AND
  2035. lp_id = " . $lp_id . " AND
  2036. item_type NOT IN('dokeos_chapter','chapter','dir')";
  2037. $res = Database::query($sql);
  2038. $row = Database :: fetch_array($res);
  2039. $total = $row[0];
  2040. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2041. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2042. // Trying as also counting browsed and failed items.
  2043. $sql = "SELECT count(distinct(lp_item_id))
  2044. FROM $tbl_item_view as item_view
  2045. INNER JOIN $tbl_item as item
  2046. ON
  2047. item.id = item_view.lp_item_id AND
  2048. item_type NOT IN('dokeos_chapter','chapter','dir')
  2049. WHERE
  2050. item_view.c_id = $course_id AND
  2051. item.c_id = $course_id AND
  2052. lp_view_id = $view_id AND
  2053. status IN ('passed','completed','succeeded','browsed','failed')";
  2054. $res = Database::query($sql);
  2055. $row = Database :: fetch_array($res);
  2056. $completed = $row[0];
  2057. if ($mode == 'abs') {
  2058. return $completed . '/' . $total;
  2059. } elseif ($mode == 'both') {
  2060. if ($progress < ($completed / ($total ? $total : 1))) {
  2061. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2062. }
  2063. return $progress . '% (' . $completed . '/' . $total . ')';
  2064. }
  2065. }
  2066. return $progress;
  2067. }
  2068. /**
  2069. * Returns the HTML necessary to print a mediaplayer block inside a page
  2070. * @return string The mediaplayer HTML
  2071. */
  2072. public function get_mediaplayer($autostart = 'true')
  2073. {
  2074. $course_id = api_get_course_int_id();
  2075. global $_course;
  2076. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2077. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2078. // Getting all the information about the item.
  2079. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2080. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2081. ON lp.id = lp_view.lp_item_id
  2082. WHERE
  2083. lp.id = '".$_SESSION['oLP']->current."' AND
  2084. lp.c_id = $course_id AND
  2085. lp_view.c_id = $course_id";
  2086. $result = Database::query($sql);
  2087. $row = Database::fetch_assoc($result);
  2088. $output = '';
  2089. if (!empty ($row['audio'])) {
  2090. $list = $_SESSION['oLP']->get_toc();
  2091. $type_quiz = false;
  2092. foreach($list as $toc) {
  2093. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2094. $type_quiz = true;
  2095. }
  2096. }
  2097. if ($type_quiz) {
  2098. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2099. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2100. } else {
  2101. $autostart_audio = $autostart;
  2102. }
  2103. } else {
  2104. $autostart_audio = 'true';
  2105. }
  2106. $courseInfo = api_get_course_info();
  2107. $audio = $row['audio'];
  2108. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2109. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2110. if (!file_exists($file)) {
  2111. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2112. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2113. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2114. }
  2115. $player = Display::getMediaPlayer(
  2116. $file,
  2117. array(
  2118. 'id' => 'lp_audio_media_player',
  2119. 'url' => $url,
  2120. 'autoplay' => $autostart_audio,
  2121. 'width' => '100%'
  2122. )
  2123. );
  2124. // The mp3 player.
  2125. $output = '<div id="container">';
  2126. $output .= $player;
  2127. $output .= '</div>';
  2128. }
  2129. return $output;
  2130. }
  2131. /**
  2132. * Checks if the learning path is visible for student after the progress
  2133. * of its prerequisite is completed, considering the time availability and
  2134. * the LP visibility.
  2135. * @param int Learnpath id
  2136. * @param int Student id
  2137. * @param string Course code (optional)
  2138. * @param int $sessionId
  2139. * @return bool
  2140. */
  2141. public static function is_lp_visible_for_student(
  2142. $lp_id,
  2143. $student_id,
  2144. $courseCode = null,
  2145. $sessionId = null
  2146. ) {
  2147. $lp_id = (int)$lp_id;
  2148. $course = api_get_course_info($courseCode);
  2149. $sessionId = intval($sessionId);
  2150. if (empty($sessionId)) {
  2151. $sessionId = api_get_session_id();
  2152. }
  2153. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2154. // Get current prerequisite
  2155. $sql = "SELECT id, prerequisite, publicated_on, expired_on
  2156. FROM $tbl_learnpath
  2157. WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  2158. $itemInfo = api_get_item_property_info(
  2159. $course['real_id'],
  2160. TOOL_LEARNPATH,
  2161. $lp_id,
  2162. $sessionId
  2163. );
  2164. // If the item was deleted.
  2165. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2166. return false;
  2167. }
  2168. $rs = Database::query($sql);
  2169. $now = time();
  2170. if (Database::num_rows($rs)>0) {
  2171. $row = Database::fetch_array($rs, 'ASSOC');
  2172. $prerequisite = $row['prerequisite'];
  2173. $is_visible = true;
  2174. if (!empty($prerequisite)) {
  2175. $progress = self::getProgress(
  2176. $prerequisite,
  2177. $student_id,
  2178. $course['real_id'],
  2179. $sessionId
  2180. );
  2181. $progress = intval($progress);
  2182. if ($progress < 100) {
  2183. $is_visible = false;
  2184. }
  2185. }
  2186. // Also check the time availability of the LP
  2187. if ($is_visible) {
  2188. // Adding visibility restrictions
  2189. if (!empty($row['publicated_on']) &&
  2190. $row['publicated_on'] != '0000-00-00 00:00:00'
  2191. ) {
  2192. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2193. //api_not_allowed();
  2194. $is_visible = false;
  2195. }
  2196. }
  2197. // Blocking empty start times see BT#2800
  2198. global $_custom;
  2199. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2200. $_custom['lps_hidden_when_no_start_date']
  2201. ) {
  2202. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2203. //api_not_allowed();
  2204. $is_visible = false;
  2205. }
  2206. }
  2207. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2208. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2209. //api_not_allowed();
  2210. $is_visible = false;
  2211. }
  2212. }
  2213. }
  2214. return $is_visible;
  2215. }
  2216. return false;
  2217. }
  2218. /**
  2219. * @param int $lpId
  2220. * @param int $userId
  2221. * @param int $courseId
  2222. * @param int $sessionId
  2223. * @return int
  2224. */
  2225. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2226. {
  2227. $lpId = intval($lpId);
  2228. $userId = intval($userId);
  2229. $courseId = intval($courseId);
  2230. $sessionId = intval($sessionId);
  2231. $progress = 0;
  2232. $sessionCondition = api_get_session_condition($sessionId);
  2233. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2234. $sql = "SELECT * FROM $table
  2235. WHERE
  2236. c_id = ".$courseId." AND
  2237. lp_id = $lpId AND
  2238. user_id = $userId $sessionCondition";
  2239. $res = Database::query($sql);
  2240. if (Database :: num_rows($res) > 0) {
  2241. $row = Database:: fetch_array($res);
  2242. $progress = $row['progress'];
  2243. }
  2244. return $progress;
  2245. }
  2246. /**
  2247. * Displays a progress bar
  2248. * completed so far.
  2249. * @param integer $percentage Progress value to display
  2250. * @param string $text_add Text to display near the progress value
  2251. * @return string HTML string containing the progress bar
  2252. */
  2253. public static function get_progress_bar($percentage = -1, $text_add = '')
  2254. {
  2255. $text = $percentage . $text_add;
  2256. $output = '<div class="progress">
  2257. <div id="progress_bar_value" class="progress-bar" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2258. '. $text .'
  2259. </div>
  2260. </div>';
  2261. return $output;
  2262. }
  2263. /**
  2264. * @param string $mode can be '%' or 'abs'
  2265. * otherwise this value will be used $this->progress_bar_mode
  2266. * @return string
  2267. */
  2268. public function getProgressBar($mode = null)
  2269. {
  2270. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2271. return self::get_progress_bar($percentage, $text_add);
  2272. }
  2273. /**
  2274. * Gets the progress bar info to display inside the progress bar.
  2275. * Also used by scorm_api.php
  2276. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2277. * we display a number of completed elements per total elements
  2278. * @param integer $add Additional steps to fake as completed
  2279. * @return list array Percentage or number and symbol (% or /xx)
  2280. */
  2281. public function get_progress_bar_text($mode = '', $add = 0)
  2282. {
  2283. if ($this->debug > 0) {
  2284. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2285. }
  2286. if (empty($mode)) {
  2287. $mode = $this->progress_bar_mode;
  2288. }
  2289. $total_items = $this->get_total_items_count_without_chapters();
  2290. if ($this->debug > 2) {
  2291. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2292. }
  2293. $completeItems = $this->get_complete_items_count();
  2294. if ($this->debug > 2) {
  2295. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2296. }
  2297. if ($add != 0) {
  2298. $completeItems += $add;
  2299. if ($this->debug > 2) {
  2300. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2301. }
  2302. }
  2303. $text = '';
  2304. if ($completeItems > $total_items) {
  2305. $completeItems = $total_items;
  2306. }
  2307. $percentage = 0;
  2308. if ($mode == '%') {
  2309. if ($total_items > 0) {
  2310. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2311. } else {
  2312. $percentage = 0;
  2313. }
  2314. $percentage = number_format($percentage, 0);
  2315. $text = '%';
  2316. } elseif ($mode == 'abs') {
  2317. $percentage = $completeItems;
  2318. $text = '/' . $total_items;
  2319. }
  2320. return array(
  2321. $percentage,
  2322. $text
  2323. );
  2324. }
  2325. /**
  2326. * Gets the progress bar mode
  2327. * @return string The progress bar mode attribute
  2328. */
  2329. public function get_progress_bar_mode()
  2330. {
  2331. if ($this->debug > 0) {
  2332. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2333. }
  2334. if (!empty ($this->progress_bar_mode)) {
  2335. return $this->progress_bar_mode;
  2336. } else {
  2337. return '%';
  2338. }
  2339. }
  2340. /**
  2341. * Gets the learnpath proximity (remote or local)
  2342. * @return string Learnpath proximity
  2343. */
  2344. public function get_proximity()
  2345. {
  2346. if ($this->debug > 0) {
  2347. error_log('New LP - In learnpath::get_proximity()', 0);
  2348. }
  2349. if (!empty ($this->proximity)) {
  2350. return $this->proximity;
  2351. } else {
  2352. return '';
  2353. }
  2354. }
  2355. /**
  2356. * Gets the learnpath theme (remote or local)
  2357. * @return string Learnpath theme
  2358. */
  2359. public function get_theme()
  2360. {
  2361. if ($this->debug > 0) {
  2362. error_log('New LP - In learnpath::get_theme()', 0);
  2363. }
  2364. if (!empty ($this->theme)) {
  2365. return $this->theme;
  2366. } else {
  2367. return '';
  2368. }
  2369. }
  2370. /**
  2371. * Gets the learnpath session id
  2372. * @return string Learnpath theme
  2373. */
  2374. public function get_lp_session_id()
  2375. {
  2376. if ($this->debug > 0) {
  2377. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2378. }
  2379. if (!empty ($this->lp_session_id)) {
  2380. return $this->lp_session_id;
  2381. } else {
  2382. return 0;
  2383. }
  2384. }
  2385. /**
  2386. * Gets the learnpath image
  2387. * @return string Web URL of the LP image
  2388. */
  2389. public function get_preview_image()
  2390. {
  2391. if ($this->debug > 0) {
  2392. error_log('New LP - In learnpath::get_preview_image()', 0);
  2393. }
  2394. if (!empty($this->preview_image)) {
  2395. return $this->preview_image;
  2396. } else {
  2397. return '';
  2398. }
  2399. }
  2400. /**
  2401. * @param string $size
  2402. * @param string $path_type
  2403. * @return bool|string
  2404. */
  2405. public function get_preview_image_path($size = null, $path_type = 'web')
  2406. {
  2407. $preview_image = $this->get_preview_image();
  2408. if (isset($preview_image) && !empty($preview_image)) {
  2409. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2410. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2411. if (isset($size)) {
  2412. $info = pathinfo($preview_image);
  2413. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2414. if (file_exists($image_sys_path.$image_custom_size)) {
  2415. if ($path_type == 'web') {
  2416. return $image_path.$image_custom_size;
  2417. } else {
  2418. return $image_sys_path.$image_custom_size;
  2419. }
  2420. }
  2421. } else {
  2422. if ($path_type == 'web') {
  2423. return $image_path.$preview_image;
  2424. } else {
  2425. return $image_sys_path.$preview_image;
  2426. }
  2427. }
  2428. }
  2429. return false;
  2430. }
  2431. /**
  2432. * Gets the learnpath author
  2433. * @return string LP's author
  2434. */
  2435. public function get_author()
  2436. {
  2437. if ($this->debug > 0) {
  2438. error_log('New LP - In learnpath::get_author()', 0);
  2439. }
  2440. if (!empty ($this->author)) {
  2441. return $this->author;
  2442. } else {
  2443. return '';
  2444. }
  2445. }
  2446. /**
  2447. * Gets the learnpath author
  2448. * @return string LP's author
  2449. */
  2450. public function get_hide_toc_frame()
  2451. {
  2452. if ($this->debug > 0) {
  2453. error_log('New LP - In learnpath::get_author()', 0);
  2454. }
  2455. if (!empty ($this->hide_toc_frame)) {
  2456. return $this->hide_toc_frame;
  2457. } else {
  2458. return '';
  2459. }
  2460. }
  2461. /**
  2462. * Generate a new prerequisites string for a given item. If this item was a sco and
  2463. * its prerequisites were strings (instead of IDs), then transform those strings into
  2464. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2465. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2466. * same rule as the scorm_export() method
  2467. * @param integer Item ID
  2468. * @return string Prerequisites string ready for the export as SCORM
  2469. */
  2470. public function get_scorm_prereq_string($item_id)
  2471. {
  2472. if ($this->debug > 0) {
  2473. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2474. }
  2475. if (!is_object($this->items[$item_id])) {
  2476. return false;
  2477. }
  2478. /** @var learnpathItem $oItem */
  2479. $oItem = $this->items[$item_id];
  2480. $prereq = $oItem->get_prereq_string();
  2481. if (empty($prereq)) {
  2482. return '';
  2483. }
  2484. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2485. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2486. // then simply return it (with the ITEM_ prefix).
  2487. //return 'ITEM_' . $prereq;
  2488. return $this->items[$prereq]->ref;
  2489. } else {
  2490. if (isset($this->refs_list[$prereq])) {
  2491. // It's a simple string item from which the ID can be found in the refs list,
  2492. // so we can transform it directly to an ID for export.
  2493. return $this->items[$this->refs_list[$prereq]]->ref;
  2494. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2495. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2496. } else {
  2497. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2498. // and replace them, one by one, by the internal IDs (chamilo db)
  2499. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2500. // by a space as well.
  2501. $find = array (
  2502. '&',
  2503. '|',
  2504. '~',
  2505. '=',
  2506. '<>',
  2507. '{',
  2508. '}',
  2509. '*',
  2510. '(',
  2511. ')'
  2512. );
  2513. $replace = array (
  2514. ' ',
  2515. ' ',
  2516. ' ',
  2517. ' ',
  2518. ' ',
  2519. ' ',
  2520. ' ',
  2521. ' ',
  2522. ' ',
  2523. ' '
  2524. );
  2525. $prereq_mod = str_replace($find, $replace, $prereq);
  2526. $ids = explode(' ', $prereq_mod);
  2527. foreach ($ids as $id) {
  2528. $id = trim($id);
  2529. if (isset ($this->refs_list[$id])) {
  2530. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2531. }
  2532. }
  2533. return $prereq;
  2534. }
  2535. }
  2536. }
  2537. /**
  2538. * Returns the XML DOM document's node
  2539. * @param resource Reference to a list of objects to search for the given ITEM_*
  2540. * @param string The identifier to look for
  2541. * @return mixed The reference to the element found with that identifier. False if not found
  2542. */
  2543. public function get_scorm_xml_node(& $children, $id)
  2544. {
  2545. for ($i = 0; $i < $children->length; $i++) {
  2546. $item_temp = $children->item($i);
  2547. if ($item_temp->nodeName == 'item') {
  2548. if ($item_temp->getAttribute('identifier') == $id) {
  2549. return $item_temp;
  2550. }
  2551. }
  2552. $subchildren = $item_temp->childNodes;
  2553. if ($subchildren->length > 0) {
  2554. $val = $this->get_scorm_xml_node($subchildren, $id);
  2555. if (is_object($val)) {
  2556. return $val;
  2557. }
  2558. }
  2559. }
  2560. return false;
  2561. }
  2562. /**
  2563. * Returns a usable array of stats related to the current learnpath and user
  2564. * @return array Well-formatted array containing status for the current learnpath
  2565. */
  2566. public function get_stats()
  2567. {
  2568. if ($this->debug > 0) {
  2569. error_log('New LP - In learnpath::get_stats()', 0);
  2570. }
  2571. }
  2572. /**
  2573. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2574. * the given course (for all learnpaths and all users)
  2575. * @param string Course code
  2576. * @return array Well-formatted array containing status for the course's learnpaths
  2577. */
  2578. public function get_stats_course($course)
  2579. {
  2580. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2581. // TODO
  2582. }
  2583. /**
  2584. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2585. * the given course and learnpath (for all users)
  2586. * @param string Course code
  2587. * @param integer Learnpath ID
  2588. * @return array Well-formatted array containing status for the specified learnpath
  2589. */
  2590. public function get_stats_lp($course, $lp)
  2591. {
  2592. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2593. // TODO
  2594. }
  2595. /**
  2596. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2597. * the given course, learnpath and user.
  2598. * @param string Course code
  2599. * @param integer Learnpath ID
  2600. * @param integer User ID
  2601. * @return array Well-formatted array containing status for the specified learnpath and user
  2602. */
  2603. public function get_stats_lp_user($course, $lp, $user)
  2604. {
  2605. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2606. // TODO
  2607. }
  2608. /**
  2609. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2610. * the given course and learnpath (for all users)
  2611. * @param string Course code
  2612. * @param integer User ID
  2613. * @return array Well-formatted array containing status for the user's learnpaths
  2614. */
  2615. public function get_stats_user($course, $user) {
  2616. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2617. // TODO
  2618. }
  2619. /**
  2620. * Gets the status list for all LP's items
  2621. * @return array Array of [index] => [item ID => current status]
  2622. */
  2623. public function get_items_status_list()
  2624. {
  2625. if ($this->debug > 0) {
  2626. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2627. }
  2628. $list = array ();
  2629. foreach ($this->ordered_items as $item_id) {
  2630. $list[] = array (
  2631. $item_id => $this->items[$item_id]->get_status()
  2632. );
  2633. }
  2634. return $list;
  2635. }
  2636. /**
  2637. * Return the number of interactions for the given learnpath Item View ID.
  2638. * This method can be used as static.
  2639. * @param integer Item View ID
  2640. * @param integer course id
  2641. * @return integer Number of interactions
  2642. */
  2643. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2644. {
  2645. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2646. $lp_iv_id = intval($lp_iv_id);
  2647. $course_id = intval($course_id);
  2648. $sql = "SELECT count(*) FROM $table
  2649. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2650. $res = Database::query($sql);
  2651. $num = 0;
  2652. if (Database::num_rows($res)) {
  2653. $row = Database::fetch_array($res);
  2654. $num = $row[0];
  2655. }
  2656. return $num;
  2657. }
  2658. /**
  2659. * Return the interactions as an array for the given lp_iv_id.
  2660. * This method can be used as static.
  2661. * @param integer Learnpath Item View ID
  2662. * @return array
  2663. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2664. */
  2665. public static function get_iv_interactions_array($lp_iv_id)
  2666. {
  2667. $course_id = api_get_course_int_id();
  2668. $list = array();
  2669. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2670. if (empty($lp_iv_id)) {
  2671. return array();
  2672. }
  2673. $sql = "SELECT * FROM $table
  2674. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2675. ORDER BY order_id ASC";
  2676. $res = Database::query($sql);
  2677. $num = Database :: num_rows($res);
  2678. if ($num > 0) {
  2679. $list[] = array (
  2680. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2681. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2682. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2683. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2684. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2685. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2686. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2687. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2688. );
  2689. while ($row = Database :: fetch_array($res)) {
  2690. $list[] = array (
  2691. 'order_id' => ($row['order_id'] + 1),
  2692. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2693. 'type' => $row['interaction_type'],
  2694. 'time' => $row['completion_time'],
  2695. //'correct_responses' => $row['correct_responses'],
  2696. 'correct_responses' => '', // Hide correct responses from students.
  2697. 'student_response' => $row['student_response'],
  2698. 'result' => $row['result'],
  2699. 'latency' => $row['latency']
  2700. );
  2701. }
  2702. }
  2703. return $list;
  2704. }
  2705. /**
  2706. * Return the number of objectives for the given learnpath Item View ID.
  2707. * This method can be used as static.
  2708. * @param integer Item View ID
  2709. * @return integer Number of objectives
  2710. */
  2711. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2712. {
  2713. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2714. $course_id = intval($course_id);
  2715. $lp_iv_id = intval($lp_iv_id);
  2716. $sql = "SELECT count(*) FROM $table
  2717. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2718. //@todo seems that this always returns 0
  2719. $res = Database::query($sql);
  2720. $num = 0;
  2721. if (Database::num_rows($res)) {
  2722. $row = Database :: fetch_array($res);
  2723. $num = $row[0];
  2724. }
  2725. return $num;
  2726. }
  2727. /**
  2728. * Return the objectives as an array for the given lp_iv_id.
  2729. * This method can be used as static.
  2730. * @param integer Learnpath Item View ID
  2731. * @return array
  2732. * @todo Translate labels
  2733. */
  2734. public static function get_iv_objectives_array($lp_iv_id = 0)
  2735. {
  2736. $course_id = api_get_course_int_id();
  2737. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2738. $sql = "SELECT * FROM $table
  2739. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2740. ORDER BY order_id ASC";
  2741. $res = Database::query($sql);
  2742. $num = Database :: num_rows($res);
  2743. $list = array();
  2744. if ($num > 0) {
  2745. $list[] = array(
  2746. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2747. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2748. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2749. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2750. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2751. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2752. );
  2753. while ($row = Database :: fetch_array($res)) {
  2754. $list[] = array (
  2755. 'order_id' => ($row['order_id'] + 1),
  2756. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2757. 'score_raw' => $row['score_raw'],
  2758. 'score_max' => $row['score_max'],
  2759. 'score_min' => $row['score_min'],
  2760. 'status' => $row['status']
  2761. );
  2762. }
  2763. }
  2764. return $list;
  2765. }
  2766. /**
  2767. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2768. * used by get_html_toc() to be ready to display
  2769. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2770. */
  2771. public function get_toc()
  2772. {
  2773. if ($this->debug > 0) {
  2774. error_log('learnpath::get_toc()', 0);
  2775. }
  2776. $toc = array();
  2777. foreach ($this->ordered_items as $item_id) {
  2778. if ($this->debug > 2) {
  2779. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2780. }
  2781. // TODO: Change this link generation and use new function instead.
  2782. $toc[] = array (
  2783. 'id' => $item_id,
  2784. 'title' => $this->items[$item_id]->get_title(),
  2785. 'status' => $this->items[$item_id]->get_status(),
  2786. 'level' => $this->items[$item_id]->get_level(),
  2787. 'type' => $this->items[$item_id]->get_type(),
  2788. 'description' => $this->items[$item_id]->get_description(),
  2789. 'path' => $this->items[$item_id]->get_path(),
  2790. );
  2791. }
  2792. if ($this->debug > 2) {
  2793. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2794. }
  2795. return $toc;
  2796. }
  2797. /**
  2798. * Generate and return the table of contents for this learnpath. The JS
  2799. * table returned is used inside of scorm_api.php
  2800. * @return string A JS array vairiable construction
  2801. */
  2802. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2803. {
  2804. if ($this->debug > 0) {
  2805. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2806. }
  2807. $toc = $varname.' = new Array();';
  2808. foreach ($this->ordered_items as $item_id) {
  2809. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2810. }
  2811. if ($this->debug > 2) {
  2812. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2813. }
  2814. return $toc;
  2815. }
  2816. /**
  2817. * Gets the learning path type
  2818. * @param boolean Return the name? If false, return the ID. Default is false.
  2819. * @return mixed Type ID or name, depending on the parameter
  2820. */
  2821. public function get_type($get_name = false)
  2822. {
  2823. $res = false;
  2824. if ($this->debug > 0) {
  2825. error_log('New LP - In learnpath::get_type()', 0);
  2826. }
  2827. if (!empty ($this->type)) {
  2828. if ($get_name) {
  2829. // Get it from the lp_type table in main db.
  2830. } else {
  2831. $res = $this->type;
  2832. }
  2833. }
  2834. if ($this->debug > 2) {
  2835. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2836. }
  2837. return $res;
  2838. }
  2839. /**
  2840. * Gets the learning path type as static method
  2841. * @param boolean Return the name? If false, return the ID. Default is false.
  2842. * @return mixed Type ID or name, depending on the parameter
  2843. */
  2844. public static function get_type_static($lp_id = 0)
  2845. {
  2846. $course_id = api_get_course_int_id();
  2847. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2848. $lp_id = intval($lp_id);
  2849. $sql = "SELECT lp_type FROM $tbl_lp
  2850. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2851. $res = Database::query($sql);
  2852. if ($res === false) {
  2853. return null;
  2854. }
  2855. if (Database :: num_rows($res) <= 0) {
  2856. return null;
  2857. }
  2858. $row = Database :: fetch_array($res);
  2859. return $row['lp_type'];
  2860. }
  2861. /**
  2862. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2863. * This method can be used as abstract and is recursive
  2864. * @param integer Learnpath ID
  2865. * @param integer Parent ID of the items to look for
  2866. * @return mixed Ordered list of item IDs or false on error
  2867. */
  2868. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2869. {
  2870. if (empty($course_id)) {
  2871. $course_id = api_get_course_int_id();
  2872. } else {
  2873. $course_id = intval($course_id);
  2874. }
  2875. $list = array();
  2876. if (empty($lp)) {
  2877. return false;
  2878. }
  2879. $lp = intval($lp);
  2880. $parent = intval($parent);
  2881. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2882. $sql = "SELECT id FROM $tbl_lp_item
  2883. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2884. ORDER BY display_order";
  2885. $res = Database::query($sql);
  2886. while ($row = Database :: fetch_array($res)) {
  2887. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2888. $list[] = $row['id'];
  2889. foreach ($sublist as $item) {
  2890. $list[] = $item;
  2891. }
  2892. }
  2893. return $list;
  2894. }
  2895. /**
  2896. * @return array
  2897. */
  2898. public static function getChapterTypes()
  2899. {
  2900. return array(
  2901. 'dokeos_chapter',
  2902. 'dokeos_module',
  2903. 'chapter',
  2904. 'dir'
  2905. );
  2906. }
  2907. /**
  2908. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2909. * @return string HTML TOC ready to display
  2910. */
  2911. public function get_html_toc($toc_list = null)
  2912. {
  2913. global $_configuration;
  2914. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2915. if ($this->debug > 0) {
  2916. error_log('In learnpath::get_html_toc()', 0);
  2917. }
  2918. if (empty($toc_list)) {
  2919. $toc_list = $this->get_toc();
  2920. }
  2921. $html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2922. $html .= '<div class="scorm-body">';
  2923. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2924. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2925. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2926. if ($this->get_lp_session_id() == api_get_session_id()) {
  2927. $html .= '<div id="actions_lp" class="actions_lp">';
  2928. $html .= '<div class="btn-group">';
  2929. $html .= "<a class='btn btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2930. $html .= "<a class='btn btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2931. $html .= '<a class="btn btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2932. $html .= '</div>';
  2933. $html .= '</div>';
  2934. }
  2935. }
  2936. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2937. require_once 'resourcelinker.inc.php';
  2938. // Temporary variables.
  2939. $mycurrentitemid = $this->get_current_item_id();
  2940. $color_counter = 0;
  2941. $i = 0;
  2942. foreach ($toc_list as $item) {
  2943. // TODO: Complete this
  2944. $icon_name = array (
  2945. 'not attempted' => '../img/notattempted.gif',
  2946. 'incomplete' => '../img/incomplete.png',
  2947. 'failed' => '../img/delete.png',
  2948. 'completed' => '../img/completed.png',
  2949. 'passed' => '../img/passed.png',
  2950. 'succeeded' => '../img/succeeded.png',
  2951. 'browsed' => '../img/completed.png',
  2952. );
  2953. // Style Status
  2954. $class_name = array (
  2955. 'not attempted' => 'scorm_not_attempted',
  2956. 'incomplete' => 'scorm_not_attempted',
  2957. 'failed' => 'scorm_failed',
  2958. 'completed' => 'scorm_completed',
  2959. 'passed' => 'scorm_completed',
  2960. 'succeeded' => 'scorm_completed',
  2961. 'browsed' => 'scorm_completed',
  2962. );
  2963. $scorm_color_background = 'scorm_item_2';
  2964. $style_item = '';
  2965. if ($color_counter % 2 == 0) {
  2966. $scorm_color_background = 'scorm_item_1';
  2967. }
  2968. $dirTypes = self::getChapterTypes();
  2969. if (in_array($item['type'], $dirTypes)) {
  2970. $scorm_color_background ='scorm_item_section ';
  2971. $style_item = '';
  2972. }
  2973. if ($item['id'] == $this->current) {
  2974. $scorm_color_background = 'scorm_item_normal scorm_highlight '.$scorm_color_background.' ';
  2975. } elseif (!in_array($item['type'], $dirTypes)) {
  2976. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2977. }
  2978. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2979. // Learning path title
  2980. $title = $item['title'];
  2981. if (empty ($title)) {
  2982. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2983. }
  2984. $title = Security::remove_XSS($title);
  2985. // Learning path personalization
  2986. // build the LP tree
  2987. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2988. $description = $item['description'];
  2989. if (empty($description)) {
  2990. $description = $title;
  2991. }
  2992. if (in_array($item['type'], $dirTypes)) {
  2993. // Chapters
  2994. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2995. } else {
  2996. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2997. $html .= '<a name="atoc_'.$item['id'].'" />';
  2998. }
  2999. if (in_array($item['type'], $dirTypes)) {
  3000. // Chapter
  3001. // if you want to put an image before, you should use css
  3002. $html .= stripslashes($title);
  3003. } else {
  3004. $this->get_link('http', $item['id'], $toc_list);
  3005. $html .= '<a class="items-list" href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  3006. }
  3007. $html .= "</div>";
  3008. if ($scorm_color_background != '') {
  3009. $html .= '</div>';
  3010. }
  3011. $color_counter++;
  3012. }
  3013. $html .= "</div>";
  3014. $html .= "</div>";
  3015. return $html;
  3016. }
  3017. /**
  3018. * Gets the learnpath maker name - generally the editor's name
  3019. * @return string Learnpath maker name
  3020. */
  3021. public function get_maker()
  3022. {
  3023. if ($this->debug > 0) {
  3024. error_log('New LP - In learnpath::get_maker()', 0);
  3025. }
  3026. if (!empty ($this->maker)) {
  3027. return $this->maker;
  3028. } else {
  3029. return '';
  3030. }
  3031. }
  3032. /**
  3033. * Gets the user-friendly message stored in $this->message
  3034. * @return string Message
  3035. */
  3036. public function get_message()
  3037. {
  3038. if ($this->debug > 0) {
  3039. error_log('New LP - In learnpath::get_message()', 0);
  3040. }
  3041. return $this->message;
  3042. }
  3043. /**
  3044. * Gets the learnpath name/title
  3045. * @return string Learnpath name/title
  3046. */
  3047. public function get_name()
  3048. {
  3049. if ($this->debug > 0) {
  3050. error_log('New LP - In learnpath::get_name()', 0);
  3051. }
  3052. if (!empty ($this->name)) {
  3053. return $this->name;
  3054. } else {
  3055. return 'N/A';
  3056. }
  3057. }
  3058. /**
  3059. * Gets a link to the resource from the present location, depending on item ID.
  3060. * @param string $type Type of link expected
  3061. * @param integer $item_id Learnpath item ID
  3062. * @return string $provided_toc Link to the lp_item resource
  3063. */
  3064. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3065. {
  3066. $course_id = $this->get_course_int_id();
  3067. if ($this->debug > 0) {
  3068. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3069. }
  3070. if (empty($item_id)) {
  3071. if ($this->debug > 2) {
  3072. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3073. }
  3074. $item_id = $this->get_current_item_id();
  3075. }
  3076. if (empty($item_id)) {
  3077. if ($this->debug > 2) {
  3078. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3079. }
  3080. //still empty, this means there was no item_id given and we are not in an object context or
  3081. //the object property is empty, return empty link
  3082. $item_id = $this->first();
  3083. return '';
  3084. }
  3085. $file = '';
  3086. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3087. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3088. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3089. $item_id = intval($item_id);
  3090. $sql = "SELECT
  3091. l.lp_type as ltype,
  3092. l.path as lpath,
  3093. li.item_type as litype,
  3094. li.path as lipath,
  3095. li.parameters as liparams
  3096. FROM $lp_table l
  3097. INNER JOIN $lp_item_table li
  3098. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3099. WHERE li.id = $item_id ";
  3100. if ($this->debug > 2) {
  3101. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3102. }
  3103. $res = Database::query($sql);
  3104. if (Database :: num_rows($res) > 0) {
  3105. $row = Database :: fetch_array($res);
  3106. $lp_type = $row['ltype'];
  3107. $lp_path = $row['lpath'];
  3108. $lp_item_type = $row['litype'];
  3109. $lp_item_path = $row['lipath'];
  3110. $lp_item_params = $row['liparams'];
  3111. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3112. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3113. }
  3114. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3115. if ($type == 'http') {
  3116. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3117. } else {
  3118. $course_path = $sys_course_path; //system path
  3119. }
  3120. // 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.
  3121. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3122. $lp_type = 1;
  3123. }
  3124. if ($this->debug > 2) {
  3125. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3126. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3127. }
  3128. // Now go through the specific cases to get the end of the path
  3129. // @todo Use constants instead of int values.
  3130. switch ($lp_type) {
  3131. case 1 :
  3132. if ($lp_item_type == 'dokeos_chapter') {
  3133. $file = 'lp_content.php?type=dir';
  3134. } else {
  3135. require_once 'resourcelinker.inc.php';
  3136. $file = rl_get_resource_link_for_learnpath(
  3137. $course_id,
  3138. $this->get_id(),
  3139. $item_id,
  3140. $this->get_view_id()
  3141. );
  3142. if ($this->debug > 0) {
  3143. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3144. }
  3145. if ($lp_item_type == 'link') {
  3146. if (Link::is_youtube_link($file)) {
  3147. $src = Link::get_youtube_video_id($file);
  3148. $file = 'embed.php?type=youtube&source='.$src;
  3149. } elseif (Link::isVimeoLink($file)) {
  3150. $src = Link::getVimeoLinkId($file);
  3151. $file = 'embed.php?type=vimeo&source='.$src;
  3152. } else {
  3153. // If the current site is HTTPS and the link is
  3154. // HTTP, browsers will refuse opening the link
  3155. $urlId = api_get_current_access_url_id();
  3156. $url = api_get_access_url($urlId, false);
  3157. $protocol = substr($url['url'], 0, 5);
  3158. if ($protocol === 'https') {
  3159. $linkProtocol = substr($file, 0, 5);
  3160. if ($linkProtocol === 'http:') {
  3161. //this is the special intervention case
  3162. $file = 'embed.php?type=nonhttps&source=' . $file;
  3163. }
  3164. }
  3165. }
  3166. } else {
  3167. // Check how much attempts of a exercise exits in lp
  3168. $lp_item_id = $this->get_current_item_id();
  3169. $lp_view_id = $this->get_view_id();
  3170. $prevent_reinit = null;
  3171. if (isset($this->items[$this->current])) {
  3172. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3173. }
  3174. if (empty($provided_toc)) {
  3175. if ($this->debug > 0) {
  3176. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3177. }
  3178. $list = $this->get_toc();
  3179. } else {
  3180. if ($this->debug > 0) {
  3181. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3182. }
  3183. $list = $provided_toc;
  3184. }
  3185. $type_quiz = false;
  3186. foreach ($list as $toc) {
  3187. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3188. $type_quiz = true;
  3189. }
  3190. }
  3191. if ($type_quiz) {
  3192. $lp_item_id = intval($lp_item_id);
  3193. $lp_view_id = intval($lp_view_id);
  3194. $sql = "SELECT count(*) FROM $lp_item_view_table
  3195. WHERE
  3196. c_id = $course_id AND
  3197. lp_item_id='" . $lp_item_id . "' AND
  3198. lp_view_id ='" . $lp_view_id . "' AND
  3199. status='completed'";
  3200. $result = Database::query($sql);
  3201. $row_count = Database :: fetch_row($result);
  3202. $count_item_view = (int) $row_count[0];
  3203. $not_multiple_attempt = 0;
  3204. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3205. $not_multiple_attempt = 1;
  3206. }
  3207. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3208. }
  3209. $tmp_array = explode('/', $file);
  3210. $document_name = $tmp_array[count($tmp_array) - 1];
  3211. if (strpos($document_name, '_DELETED_')) {
  3212. $file = 'blank.php?error=document_deleted';
  3213. }
  3214. }
  3215. }
  3216. break;
  3217. case 2 :
  3218. if ($this->debug > 2) {
  3219. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3220. }
  3221. if ($lp_item_type != 'dir') {
  3222. // Quite complex here:
  3223. // We want to make sure 'http://' (and similar) links can
  3224. // be loaded as is (withouth the Chamilo path in front) but
  3225. // some contents use this form: resource.htm?resource=http://blablabla
  3226. // which means we have to find a protocol at the path's start, otherwise
  3227. // it should not be considered as an external URL.
  3228. //if ($this->prerequisites_match($item_id)) {
  3229. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3230. if ($this->debug > 2) {
  3231. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3232. }
  3233. // Distant url, return as is.
  3234. $file = $lp_item_path;
  3235. } else {
  3236. if ($this->debug > 2) {
  3237. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3238. }
  3239. // Prevent getting untranslatable urls.
  3240. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3241. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3242. // Prepare the path.
  3243. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3244. // TODO: Fix this for urls with protocol header.
  3245. $file = str_replace('//', '/', $file);
  3246. $file = str_replace(':/', '://', $file);
  3247. if (substr($lp_path, -1) == '/') {
  3248. $lp_path = substr($lp_path, 0, -1);
  3249. }
  3250. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3251. // if file not found.
  3252. $decoded = html_entity_decode($lp_item_path);
  3253. list ($decoded) = explode('?', $decoded);
  3254. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3255. require_once 'resourcelinker.inc.php';
  3256. $file = rl_get_resource_link_for_learnpath(
  3257. $course_id,
  3258. $this->get_id(),
  3259. $item_id,
  3260. $this->get_view_id()
  3261. );
  3262. if (empty($file)) {
  3263. $file = 'blank.php?error=document_not_found';
  3264. } else {
  3265. $tmp_array = explode('/', $file);
  3266. $document_name = $tmp_array[count($tmp_array) - 1];
  3267. if (strpos($document_name, '_DELETED_')) {
  3268. $file = 'blank.php?error=document_deleted';
  3269. } else {
  3270. $file = 'blank.php?error=document_not_found';
  3271. }
  3272. }
  3273. } else {
  3274. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3275. }
  3276. }
  3277. }
  3278. // We want to use parameters if they were defined in the imsmanifest
  3279. if (strpos($file, 'blank.php') === false) {
  3280. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3281. }
  3282. } else {
  3283. $file = 'lp_content.php?type=dir';
  3284. }
  3285. break;
  3286. case 3 :
  3287. if ($this->debug > 2) {
  3288. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3289. }
  3290. // Formatting AICC HACP append URL.
  3291. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3292. if (!empty($lp_item_params)) {
  3293. $aicc_append .= $lp_item_params . '&';
  3294. }
  3295. if ($lp_item_type != 'dir') {
  3296. // Quite complex here:
  3297. // We want to make sure 'http://' (and similar) links can
  3298. // be loaded as is (withouth the Chamilo path in front) but
  3299. // some contents use this form: resource.htm?resource=http://blablabla
  3300. // which means we have to find a protocol at the path's start, otherwise
  3301. // it should not be considered as an external URL.
  3302. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3303. if ($this->debug > 2) {
  3304. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3305. }
  3306. // Distant url, return as is.
  3307. $file = $lp_item_path;
  3308. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3309. /*
  3310. if (stristr($file,'<servername>') !== false) {
  3311. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3312. }
  3313. */
  3314. if (stripos($file, '<servername>') !== false) {
  3315. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3316. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3317. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3318. }
  3319. //
  3320. $file .= $aicc_append;
  3321. } else {
  3322. if ($this->debug > 2) {
  3323. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3324. }
  3325. // Prevent getting untranslatable urls.
  3326. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3327. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3328. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3329. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3330. // TODO: Fix this for urls with protocol header.
  3331. $file = str_replace('//', '/', $file);
  3332. $file = str_replace(':/', '://', $file);
  3333. $file .= $aicc_append;
  3334. }
  3335. } else {
  3336. $file = 'lp_content.php?type=dir';
  3337. }
  3338. break;
  3339. case 4 :
  3340. break;
  3341. default :
  3342. break;
  3343. }
  3344. // Replace &amp; by & because &amp; will break URL with params
  3345. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3346. }
  3347. if ($this->debug > 2) {
  3348. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3349. }
  3350. return $file;
  3351. }
  3352. /**
  3353. * Gets the latest usable view or generate a new one
  3354. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3355. * @return integer DB lp_view id
  3356. */
  3357. public function get_view($attempt_num = 0)
  3358. {
  3359. if ($this->debug > 0) {
  3360. error_log('New LP - In learnpath::get_view()', 0);
  3361. }
  3362. $search = '';
  3363. // Use $attempt_num to enable multi-views management (disabled so far).
  3364. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3365. $search = 'AND view_count = ' . $attempt_num;
  3366. }
  3367. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3368. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3369. $course_id = api_get_course_int_id();
  3370. $sessionId = api_get_session_id();
  3371. $sql = "SELECT id, view_count FROM $lp_view_table
  3372. WHERE
  3373. c_id = " . $course_id . " AND
  3374. lp_id = " . $this->get_id() . " AND
  3375. user_id = " . $this->get_user_id() . " AND
  3376. session_id = $sessionId
  3377. $search
  3378. ORDER BY view_count DESC";
  3379. $res = Database::query($sql);
  3380. if (Database :: num_rows($res) > 0) {
  3381. $row = Database :: fetch_array($res);
  3382. $this->lp_view_id = $row['id'];
  3383. } else if (!api_is_invitee()) {
  3384. // There is no database record, create one.
  3385. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3386. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3387. Database::query($sql);
  3388. $id = Database :: insert_id();
  3389. $this->lp_view_id = $id;
  3390. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3391. Database::query($sql);
  3392. }
  3393. return $this->lp_view_id;
  3394. }
  3395. /**
  3396. * Gets the current view id
  3397. * @return integer View ID (from lp_view)
  3398. */
  3399. public function get_view_id()
  3400. {
  3401. if ($this->debug > 0) {
  3402. error_log('New LP - In learnpath::get_view_id()', 0);
  3403. }
  3404. if (!empty ($this->lp_view_id)) {
  3405. return $this->lp_view_id;
  3406. } else {
  3407. return 0;
  3408. }
  3409. }
  3410. /**
  3411. * Gets the update queue
  3412. * @return array Array containing IDs of items to be updated by JavaScript
  3413. */
  3414. public function get_update_queue()
  3415. {
  3416. if ($this->debug > 0) {
  3417. error_log('New LP - In learnpath::get_update_queue()', 0);
  3418. }
  3419. return $this->update_queue;
  3420. }
  3421. /**
  3422. * Gets the user ID
  3423. * @return integer User ID
  3424. */
  3425. public function get_user_id()
  3426. {
  3427. if ($this->debug > 0) {
  3428. error_log('New LP - In learnpath::get_user_id()', 0);
  3429. }
  3430. if (!empty ($this->user_id)) {
  3431. return $this->user_id;
  3432. } else {
  3433. return false;
  3434. }
  3435. }
  3436. /**
  3437. * Checks if any of the items has an audio element attached
  3438. * @return bool True or false
  3439. */
  3440. public function has_audio()
  3441. {
  3442. if ($this->debug > 1) {
  3443. error_log('New LP - In learnpath::has_audio()', 0);
  3444. }
  3445. $has = false;
  3446. foreach ($this->items as $i => $item) {
  3447. if (!empty ($this->items[$i]->audio)) {
  3448. $has = true;
  3449. break;
  3450. }
  3451. }
  3452. return $has;
  3453. }
  3454. /**
  3455. * Logs a message into a file
  3456. * @param string Message to log
  3457. * @return boolean True on success, false on error or if msg empty
  3458. */
  3459. public function log($msg)
  3460. {
  3461. if ($this->debug > 0) {
  3462. error_log('New LP - In learnpath::log()', 0);
  3463. }
  3464. // TODO
  3465. $this->error .= $msg;
  3466. return true;
  3467. }
  3468. /**
  3469. * Moves an item up and down at its level
  3470. * @param integer Item to move up and down
  3471. * @param string Direction 'up' or 'down'
  3472. * @return integer New display order, or false on error
  3473. */
  3474. public function move_item($id, $direction)
  3475. {
  3476. $course_id = api_get_course_int_id();
  3477. if ($this->debug > 0) {
  3478. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3479. }
  3480. if (empty($id) || empty($direction)) {
  3481. return false;
  3482. }
  3483. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3484. $sql_sel = "SELECT *
  3485. FROM " . $tbl_lp_item . "
  3486. WHERE c_id = ".$course_id." AND id = " . $id;
  3487. $res_sel = Database::query($sql_sel);
  3488. // Check if elem exists.
  3489. if (Database :: num_rows($res_sel) < 1) {
  3490. return false;
  3491. }
  3492. // Gather data.
  3493. $row = Database :: fetch_array($res_sel);
  3494. $previous = $row['previous_item_id'];
  3495. $next = $row['next_item_id'];
  3496. $display = $row['display_order'];
  3497. $parent = $row['parent_item_id'];
  3498. $lp = $row['lp_id'];
  3499. // Update the item (switch with previous/next one).
  3500. switch ($direction) {
  3501. case 'up':
  3502. if ($this->debug > 2) {
  3503. error_log('Movement up detected', 0);
  3504. }
  3505. if ($display <= 1) { /*do nothing*/
  3506. } else {
  3507. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3508. WHERE c_id = ".$course_id." AND id = $previous";
  3509. if ($this->debug > 2) {
  3510. error_log('Selecting previous: ' . $sql_sel2, 0);
  3511. }
  3512. $res_sel2 = Database::query($sql_sel2);
  3513. if (Database :: num_rows($res_sel2) < 1) {
  3514. $previous_previous = 0;
  3515. }
  3516. // Gather data.
  3517. $row2 = Database :: fetch_array($res_sel2);
  3518. $previous_previous = $row2['previous_item_id'];
  3519. // Update previous_previous item (switch "next" with current).
  3520. if ($previous_previous != 0) {
  3521. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3522. next_item_id = $id
  3523. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3524. if ($this->debug > 2) {
  3525. error_log($sql_upd2, 0);
  3526. }
  3527. Database::query($sql_upd2);
  3528. }
  3529. // Update previous item (switch with current).
  3530. if ($previous != 0) {
  3531. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3532. next_item_id = $next,
  3533. previous_item_id = $id,
  3534. display_order = display_order +1
  3535. WHERE c_id = ".$course_id." AND id = $previous";
  3536. if ($this->debug > 2) {
  3537. error_log($sql_upd2, 0);
  3538. }
  3539. Database::query($sql_upd2);
  3540. }
  3541. // Update current item (switch with previous).
  3542. if ($id != 0) {
  3543. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3544. next_item_id = $previous,
  3545. previous_item_id = $previous_previous,
  3546. display_order = display_order-1
  3547. WHERE c_id = ".$course_id." AND id = $id";
  3548. if ($this->debug > 2) {
  3549. error_log($sql_upd2, 0);
  3550. }
  3551. Database::query($sql_upd2);
  3552. }
  3553. // Update next item (new previous item).
  3554. if ($next != 0) {
  3555. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3556. WHERE c_id = ".$course_id." AND id = $next";
  3557. if ($this->debug > 2) {
  3558. error_log($sql_upd2, 0);
  3559. }
  3560. Database::query($sql_upd2);
  3561. }
  3562. $display = $display -1;
  3563. }
  3564. break;
  3565. case 'down':
  3566. if ($this->debug > 2) {
  3567. error_log('Movement down detected', 0);
  3568. }
  3569. if ($next == 0) { /* Do nothing. */
  3570. } else {
  3571. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3572. if ($this->debug > 2) {
  3573. error_log('Selecting next: ' . $sql_sel2, 0);
  3574. }
  3575. $res_sel2 = Database::query($sql_sel2);
  3576. if (Database :: num_rows($res_sel2) < 1) {
  3577. $next_next = 0;
  3578. }
  3579. // Gather data.
  3580. $row2 = Database :: fetch_array($res_sel2);
  3581. $next_next = $row2['next_item_id'];
  3582. // Update previous item (switch with current).
  3583. if ($previous != 0) {
  3584. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3585. WHERE c_id = ".$course_id." AND id = $previous";
  3586. Database::query($sql_upd2);
  3587. }
  3588. // Update current item (switch with previous).
  3589. if ($id != 0) {
  3590. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3591. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3592. WHERE c_id = ".$course_id." AND id = $id";
  3593. Database::query($sql_upd2);
  3594. }
  3595. // Update next item (new previous item).
  3596. if ($next != 0) {
  3597. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3598. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3599. WHERE c_id = ".$course_id." AND id = $next";
  3600. Database::query($sql_upd2);
  3601. }
  3602. // Update next_next item (switch "previous" with current).
  3603. if ($next_next != 0) {
  3604. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3605. previous_item_id = $id
  3606. WHERE c_id = ".$course_id." AND id = $next_next";
  3607. Database::query($sql_upd2);
  3608. }
  3609. $display = $display +1;
  3610. }
  3611. break;
  3612. default :
  3613. return false;
  3614. }
  3615. return $display;
  3616. }
  3617. /**
  3618. * Move a learnpath up (display_order)
  3619. * @param integer Learnpath ID
  3620. */
  3621. public function move_up($lp_id)
  3622. {
  3623. $course_id = api_get_course_int_id();
  3624. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3625. $sql = "SELECT * FROM $lp_table
  3626. WHERE c_id = ".$course_id."
  3627. ORDER BY display_order";
  3628. $res = Database::query($sql);
  3629. if ($res === false)
  3630. return false;
  3631. $lps = array ();
  3632. $lp_order = array ();
  3633. $num = Database :: num_rows($res);
  3634. // First check the order is correct, globally (might be wrong because
  3635. // of versions < 1.8.4)
  3636. if ($num > 0) {
  3637. $i = 1;
  3638. while ($row = Database :: fetch_array($res)) {
  3639. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3640. $need_fix = true;
  3641. $sql_u = "UPDATE $lp_table SET display_order = $i
  3642. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3643. Database::query($sql_u);
  3644. }
  3645. $row['display_order'] = $i;
  3646. $lps[$row['id']] = $row;
  3647. $lp_order[$i] = $row['id'];
  3648. $i++;
  3649. }
  3650. }
  3651. if ($num > 1) { // If there's only one element, no need to sort.
  3652. $order = $lps[$lp_id]['display_order'];
  3653. if ($order > 1) { // If it's the first element, no need to move up.
  3654. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3655. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3656. Database::query($sql_u1);
  3657. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3658. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3659. Database::query($sql_u2);
  3660. }
  3661. }
  3662. }
  3663. /**
  3664. * Move a learnpath down (display_order)
  3665. * @param integer Learnpath ID
  3666. */
  3667. public function move_down($lp_id)
  3668. {
  3669. $course_id = api_get_course_int_id();
  3670. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3671. $sql = "SELECT * FROM $lp_table
  3672. WHERE c_id = ".$course_id."
  3673. ORDER BY display_order";
  3674. $res = Database::query($sql);
  3675. if ($res === false)
  3676. return false;
  3677. $lps = array ();
  3678. $lp_order = array ();
  3679. $num = Database :: num_rows($res);
  3680. $max = 0;
  3681. // First check the order is correct, globally (might be wrong because
  3682. // of versions < 1.8.4).
  3683. if ($num > 0) {
  3684. $i = 1;
  3685. while ($row = Database :: fetch_array($res)) {
  3686. $max = $i;
  3687. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3688. $need_fix = true;
  3689. $sql_u = "UPDATE $lp_table SET display_order = $i
  3690. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3691. $res_u = Database::query($sql_u);
  3692. }
  3693. $row['display_order'] = $i;
  3694. $lps[$row['id']] = $row;
  3695. $lp_order[$i] = $row['id'];
  3696. $i++;
  3697. }
  3698. }
  3699. if ($num > 1) { // If there's only one element, no need to sort.
  3700. $order = $lps[$lp_id]['display_order'];
  3701. if ($order < $max) { // If it's the first element, no need to move up.
  3702. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3703. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3704. Database::query($sql_u1);
  3705. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3706. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3707. Database::query($sql_u2);
  3708. }
  3709. }
  3710. }
  3711. /**
  3712. * Updates learnpath attributes to point to the next element
  3713. * The last part is similar to set_current_item but processing the other way around
  3714. */
  3715. public function next()
  3716. {
  3717. if ($this->debug > 0) {
  3718. error_log('New LP - In learnpath::next()', 0);
  3719. }
  3720. $this->last = $this->get_current_item_id();
  3721. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3722. $this->autocomplete_parents($this->last);
  3723. $new_index = $this->get_next_index();
  3724. if ($this->debug > 2) {
  3725. error_log('New LP - New index: ' . $new_index, 0);
  3726. }
  3727. $this->index = $new_index;
  3728. if ($this->debug > 2) {
  3729. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3730. }
  3731. $this->current = $this->ordered_items[$new_index];
  3732. if ($this->debug > 2) {
  3733. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3734. }
  3735. }
  3736. /**
  3737. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3738. * class, this might be redefined to allow several behaviours depending on the document type.
  3739. * @param integer Resource ID
  3740. * @return boolean True on success, false otherwise
  3741. */
  3742. public function open($id)
  3743. {
  3744. if ($this->debug > 0) {
  3745. error_log('New LP - In learnpath::open()', 0);
  3746. }
  3747. // TODO:
  3748. // set the current resource attribute to this resource
  3749. // switch on element type (redefine in child class?)
  3750. // set status for this item to "opened"
  3751. // start timer
  3752. // initialise score
  3753. $this->index = 0; //or = the last item seen (see $this->last)
  3754. }
  3755. /**
  3756. * Check that all prerequisites are fulfilled. Returns true and an
  3757. * empty string on succes, returns false
  3758. * and the prerequisite string on error.
  3759. * This function is based on the rules for aicc_script language as
  3760. * described in the SCORM 1.2 CAM documentation page 108.
  3761. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3762. * @return boolean True if prerequisites are matched, false otherwise -
  3763. * Empty string if true returned, prerequisites string otherwise.
  3764. */
  3765. public function prerequisites_match($itemId = null)
  3766. {
  3767. $debug = $this->debug;
  3768. if ($debug > 0) {
  3769. error_log('In learnpath::prerequisites_match()', 0);
  3770. }
  3771. if (empty($itemId)) {
  3772. $itemId = $this->current;
  3773. }
  3774. $currentItem = $this->getItem($itemId);
  3775. if ($currentItem) {
  3776. if ($this->type == 2) {
  3777. // Getting prereq from scorm
  3778. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3779. } else {
  3780. $prereq_string = $currentItem->get_prereq_string();
  3781. }
  3782. if (empty($prereq_string)) {
  3783. if ($debug > 0) {
  3784. error_log('Found prereq_string is empty return true');
  3785. }
  3786. return true;
  3787. }
  3788. // Clean spaces.
  3789. $prereq_string = str_replace(' ', '', $prereq_string);
  3790. if ($debug > 0) {
  3791. error_log('Found prereq_string: ' . $prereq_string, 0);
  3792. }
  3793. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3794. $result = $currentItem->parse_prereq(
  3795. $prereq_string,
  3796. $this->items,
  3797. $this->refs_list,
  3798. $this->get_user_id()
  3799. );
  3800. if ($result === false) {
  3801. $this->set_error_msg($currentItem->prereq_alert);
  3802. }
  3803. } else {
  3804. $result = true;
  3805. if ($debug > 1) {
  3806. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3807. }
  3808. }
  3809. if ($debug > 1) {
  3810. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3811. }
  3812. return $result;
  3813. }
  3814. /**
  3815. * Updates learnpath attributes to point to the previous element
  3816. * The last part is similar to set_current_item but processing the other way around
  3817. */
  3818. public function previous()
  3819. {
  3820. if ($this->debug > 0) {
  3821. error_log('New LP - In learnpath::previous()', 0);
  3822. }
  3823. $this->last = $this->get_current_item_id();
  3824. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3825. $this->autocomplete_parents($this->last);
  3826. $new_index = $this->get_previous_index();
  3827. $this->index = $new_index;
  3828. $this->current = $this->ordered_items[$new_index];
  3829. }
  3830. /**
  3831. * Publishes a learnpath. This basically means show or hide the learnpath
  3832. * to normal users.
  3833. * Can be used as abstract
  3834. * @param integer Learnpath ID
  3835. * @param string New visibility
  3836. */
  3837. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3838. {
  3839. $action = 'visible';
  3840. if ($set_visibility != 1) {
  3841. $action = 'invisible';
  3842. self::toggle_publish($lp_id, 'i');
  3843. }
  3844. return api_item_property_update(
  3845. api_get_course_info(),
  3846. TOOL_LEARNPATH,
  3847. $lp_id,
  3848. $action,
  3849. api_get_user_id()
  3850. );
  3851. }
  3852. /**
  3853. * Publishes a learnpath. This basically means show or hide the learnpath
  3854. * on the course homepage
  3855. * Can be used as abstract
  3856. * @param integer $lp_id Learnpath id
  3857. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3858. * @return bool
  3859. */
  3860. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3861. {
  3862. $course_id = api_get_course_int_id();
  3863. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3864. $lp_id = intval($lp_id);
  3865. $sql = "SELECT * FROM $tbl_lp
  3866. WHERE c_id = ".$course_id." AND id = $lp_id";
  3867. $result = Database::query($sql);
  3868. if (Database::num_rows($result)) {
  3869. $row = Database :: fetch_array($result);
  3870. $name = domesticate($row['name']);
  3871. if ($set_visibility == 'i') {
  3872. $s = $name . " " . get_lang('LearnpathNotPublished');
  3873. $dialogBox = $s;
  3874. $v = 0;
  3875. }
  3876. if ($set_visibility == 'v') {
  3877. $s = $name . " " . get_lang('LearnpathPublished');
  3878. $dialogBox = $s;
  3879. $v = 1;
  3880. }
  3881. } else {
  3882. return false;
  3883. }
  3884. $session_id = api_get_session_id();
  3885. $session_condition = api_get_session_condition($session_id);
  3886. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3887. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3888. $sql = "SELECT * FROM $tbl_tool
  3889. WHERE
  3890. c_id = ".$course_id." AND
  3891. link='$link' and
  3892. image='scormbuilder.gif' and
  3893. link LIKE '$link%'
  3894. $session_condition
  3895. ";
  3896. $result = Database::query($sql);
  3897. $num = Database :: num_rows($result);
  3898. if ($set_visibility == 'i' && $num > 0) {
  3899. $sql = "DELETE FROM $tbl_tool
  3900. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3901. Database::query($sql);
  3902. } elseif ($set_visibility == 'v' && $num == 0) {
  3903. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3904. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3905. Database::query($sql);
  3906. $insertId = Database::insert_id();
  3907. if ($insertId) {
  3908. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3909. Database::query($sql);
  3910. }
  3911. } elseif ($set_visibility == 'v' && $num > 0) {
  3912. $sql = "UPDATE $tbl_tool SET
  3913. c_id = $course_id,
  3914. name = '$name',
  3915. link = '$link',
  3916. image = 'scormbuilder.gif',
  3917. visibility = '$v',
  3918. admin = '0',
  3919. address = 'pastillegris.gif',
  3920. added_tool = 0,
  3921. session_id = $session_id
  3922. WHERE
  3923. c_id = ".$course_id." AND
  3924. (link='$link' and image='scormbuilder.gif' $session_condition)
  3925. ";
  3926. Database::query($sql);
  3927. } else {
  3928. // Parameter and database incompatible, do nothing, exit.
  3929. return false;
  3930. }
  3931. }
  3932. /**
  3933. * Restart the whole learnpath. Return the URL of the first element.
  3934. * Make sure the results are saved with anoter method. This method should probably be
  3935. * redefined in children classes.
  3936. * To use a similar method statically, use the create_new_attempt() method
  3937. * @return string URL to load in the viewer
  3938. */
  3939. public function restart()
  3940. {
  3941. if ($this->debug > 0) {
  3942. error_log('New LP - In learnpath::restart()', 0);
  3943. }
  3944. // TODO
  3945. // Call autosave method to save the current progress.
  3946. //$this->index = 0;
  3947. if (api_is_invitee()) {
  3948. return false;
  3949. }
  3950. $session_id = api_get_session_id();
  3951. $course_id = api_get_course_int_id();
  3952. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3953. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3954. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3955. if ($this->debug > 2) {
  3956. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3957. }
  3958. $res = Database::query($sql);
  3959. $view_id = Database::insert_id();
  3960. if ($view_id) {
  3961. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3962. Database::query($sql);
  3963. $this->lp_view_id = $view_id;
  3964. $this->attempt = $this->attempt + 1;
  3965. } else {
  3966. $this->error = 'Could not insert into item_view table...';
  3967. return false;
  3968. }
  3969. $this->autocomplete_parents($this->current);
  3970. foreach ($this->items as $index => $dummy) {
  3971. $this->items[$index]->restart();
  3972. $this->items[$index]->set_lp_view($this->lp_view_id);
  3973. }
  3974. $this->first();
  3975. return true;
  3976. }
  3977. /**
  3978. * Saves the current item
  3979. * @return boolean
  3980. */
  3981. public function save_current()
  3982. {
  3983. if ($this->debug > 0) {
  3984. error_log('learnpath::save_current()', 0);
  3985. }
  3986. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3987. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3988. if ($this->debug > 2) {
  3989. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3990. }
  3991. if ($this->debug > 2) {
  3992. error_log('' . print_r($this->items, true), 0);
  3993. }
  3994. if (isset($this->items[$this->current]) &&
  3995. is_object($this->items[$this->current])
  3996. ) {
  3997. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3998. $this->autocomplete_parents($this->current);
  3999. $status = $this->items[$this->current]->get_status();
  4000. $this->append_message('new_item_status: ' . $status);
  4001. $this->update_queue[$this->current] = $status;
  4002. return $res;
  4003. }
  4004. return false;
  4005. }
  4006. /**
  4007. * Saves the given item
  4008. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  4009. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4010. * @return boolean
  4011. */
  4012. public function save_item($item_id = null, $from_outside = true)
  4013. {
  4014. $debug = $this->debug;
  4015. if ($debug) {
  4016. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4017. }
  4018. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4019. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4020. if (empty($item_id)) {
  4021. $item_id = intval($_REQUEST['id']);
  4022. }
  4023. if (empty($item_id)) {
  4024. $item_id = $this->get_current_item_id();
  4025. }
  4026. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4027. if ($debug) {
  4028. error_log('Object exists');
  4029. }
  4030. // Saving the item.
  4031. $res = $this->items[$item_id]->save(
  4032. $from_outside,
  4033. $this->prerequisites_match($item_id)
  4034. );
  4035. if ($debug) {
  4036. error_log('update_queue before:');
  4037. error_log(print_r($this->update_queue,1));
  4038. }
  4039. $this->autocomplete_parents($item_id);
  4040. $status = $this->items[$item_id]->get_status();
  4041. $this->update_queue[$item_id] = $status;
  4042. if ($debug) {
  4043. error_log('get_status(): ' . $status);
  4044. error_log('update_queue after:');
  4045. error_log(print_r($this->update_queue,1));
  4046. }
  4047. return $res;
  4048. }
  4049. return false;
  4050. }
  4051. /**
  4052. * Saves the last item seen's ID only in case
  4053. */
  4054. public function save_last()
  4055. {
  4056. $course_id = api_get_course_int_id();
  4057. if ($this->debug > 0) {
  4058. error_log('New LP - In learnpath::save_last()', 0);
  4059. }
  4060. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4061. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4062. if (isset($this->current) && !api_is_invitee()) {
  4063. if ($this->debug > 2) {
  4064. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4065. }
  4066. $sql = "UPDATE $table SET
  4067. last_item = " . intval($this->get_current_item_id()). "
  4068. WHERE
  4069. c_id = $course_id AND
  4070. lp_id = " . $this->get_id() . " AND
  4071. user_id = " . $this->get_user_id()." ".$session_condition;
  4072. if ($this->debug > 2) {
  4073. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4074. }
  4075. Database::query($sql);
  4076. }
  4077. if (!api_is_invitee()) {
  4078. // Save progress.
  4079. list($progress, $text) = $this->get_progress_bar_text('%');
  4080. if ($progress >= 0 && $progress <= 100) {
  4081. $progress = (int) $progress;
  4082. $sql = "UPDATE $table SET
  4083. progress = $progress
  4084. WHERE
  4085. c_id = ".$course_id." AND
  4086. lp_id = " . $this->get_id() . " AND
  4087. user_id = " . $this->get_user_id()." ".$session_condition;
  4088. // Ignore errors as some tables might not have the progress field just yet.
  4089. Database::query($sql);
  4090. $this->progress_db = $progress;
  4091. }
  4092. }
  4093. }
  4094. /**
  4095. * Sets the current item ID (checks if valid and authorized first)
  4096. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4097. */
  4098. public function set_current_item($item_id = null)
  4099. {
  4100. if ($this->debug > 0) {
  4101. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4102. }
  4103. if (empty ($item_id)) {
  4104. if ($this->debug > 2) {
  4105. error_log('New LP - No new current item given, ignore...', 0);
  4106. }
  4107. // Do nothing.
  4108. } else {
  4109. if ($this->debug > 2) {
  4110. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4111. }
  4112. if (is_numeric($item_id)) {
  4113. $item_id = intval($item_id);
  4114. // TODO: Check in database here.
  4115. $this->last = $this->current;
  4116. $this->current = $item_id;
  4117. // TODO: Update $this->index as well.
  4118. foreach ($this->ordered_items as $index => $item) {
  4119. if ($item == $this->current) {
  4120. $this->index = $index;
  4121. break;
  4122. }
  4123. }
  4124. if ($this->debug > 2) {
  4125. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4126. }
  4127. } else {
  4128. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4129. }
  4130. }
  4131. }
  4132. /**
  4133. * Sets the encoding
  4134. * @param string New encoding
  4135. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4136. */
  4137. public function set_encoding($enc = 'UTF-8')
  4138. {
  4139. if ($this->debug > 0) {
  4140. error_log('New LP - In learnpath::set_encoding()', 0);
  4141. }
  4142. $course_id = api_get_course_int_id();
  4143. $enc = api_refine_encoding_id($enc);
  4144. if (empty($enc)) {
  4145. $enc = api_get_system_encoding();
  4146. }
  4147. if (api_is_encoding_supported($enc)) {
  4148. $lp = $this->get_id();
  4149. if ($lp != 0) {
  4150. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4151. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4152. $res = Database::query($sql);
  4153. return $res;
  4154. }
  4155. }
  4156. return false;
  4157. }
  4158. /**
  4159. * Sets the JS lib setting in the database directly.
  4160. * This is the JavaScript library file this lp needs to load on startup
  4161. * @param string Proximity setting
  4162. * @return boolean True on update success. False otherwise.
  4163. */
  4164. public function set_jslib($lib = '')
  4165. {
  4166. if ($this->debug > 0) {
  4167. error_log('New LP - In learnpath::set_jslib()', 0);
  4168. }
  4169. $lp = $this->get_id();
  4170. $course_id = api_get_course_int_id();
  4171. if ($lp != 0) {
  4172. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4173. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4174. $res = Database::query($sql);
  4175. return $res;
  4176. } else {
  4177. return false;
  4178. }
  4179. }
  4180. /**
  4181. * Sets the name of the LP maker (publisher) (and save)
  4182. * @param string Optional string giving the new content_maker of this learnpath
  4183. * @return boolean True
  4184. */
  4185. public function set_maker($name = '')
  4186. {
  4187. if ($this->debug > 0) {
  4188. error_log('New LP - In learnpath::set_maker()', 0);
  4189. }
  4190. if (empty ($name))
  4191. return false;
  4192. $this->maker = $name;
  4193. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4194. $course_id = api_get_course_int_id();
  4195. $lp_id = $this->get_id();
  4196. $sql = "UPDATE $lp_table SET
  4197. content_maker = '" . Database::escape_string($this->maker) . "'
  4198. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4199. if ($this->debug > 2) {
  4200. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4201. }
  4202. Database::query($sql);
  4203. return true;
  4204. }
  4205. /**
  4206. * Sets the name of the current learnpath (and save)
  4207. * @param string $name Optional string giving the new name of this learnpath
  4208. * @return boolean True/False
  4209. */
  4210. public function set_name($name = null)
  4211. {
  4212. if ($this->debug > 0) {
  4213. error_log('New LP - In learnpath::set_name()', 0);
  4214. }
  4215. if (empty($name)) {
  4216. return false;
  4217. }
  4218. $this->name = $name;
  4219. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4220. $lp_id = $this->get_id();
  4221. $course_id = api_get_course_int_id();
  4222. $sql = "UPDATE $lp_table SET
  4223. name = '" . Database::escape_string($this->name). "'
  4224. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4225. if ($this->debug > 2) {
  4226. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4227. }
  4228. $result = Database::query($sql);
  4229. // If the lp is visible on the homepage, change his name there.
  4230. if (Database::affected_rows($result)) {
  4231. $session_id = api_get_session_id();
  4232. $session_condition = api_get_session_condition($session_id);
  4233. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4234. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4235. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4236. WHERE
  4237. c_id = $course_id AND
  4238. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4239. Database::query($sql);
  4240. return true;
  4241. } else {
  4242. return false;
  4243. }
  4244. }
  4245. /**
  4246. * Set index specified prefix terms for all items in this path
  4247. * @param string Comma-separated list of terms
  4248. * @param char Xapian term prefix
  4249. * @return boolean False on error, true otherwise
  4250. */
  4251. public function set_terms_by_prefix($terms_string, $prefix)
  4252. {
  4253. $course_id = api_get_course_int_id();
  4254. if (api_get_setting('search_enabled') !== 'true')
  4255. return false;
  4256. if (!extension_loaded('xapian')) {
  4257. return false;
  4258. }
  4259. $terms_string = trim($terms_string);
  4260. $terms = explode(',', $terms_string);
  4261. array_walk($terms, 'trim_value');
  4262. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4263. // Don't do anything if no change, verify only at DB, not the search engine.
  4264. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4265. return false;
  4266. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4267. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4268. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4269. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4270. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4271. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4272. $lp_id = intval($_POST['lp_id']);
  4273. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4274. $result = Database::query($sql);
  4275. $di = new ChamiloIndexer();
  4276. while ($lp_item = Database :: fetch_array($result)) {
  4277. // Get search_did.
  4278. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4279. $sql = 'SELECT * FROM %s
  4280. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4281. LIMIT 1';
  4282. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4283. //echo $sql; echo '<br>';
  4284. $res = Database::query($sql);
  4285. if (Database::num_rows($res) > 0) {
  4286. $se_ref = Database :: fetch_array($res);
  4287. // Compare terms.
  4288. $doc = $di->get_document($se_ref['search_did']);
  4289. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4290. $xterms = array();
  4291. foreach ($xapian_terms as $xapian_term) {
  4292. $xterms[] = substr($xapian_term['name'], 1);
  4293. }
  4294. $dterms = $terms;
  4295. $missing_terms = array_diff($dterms, $xterms);
  4296. $deprecated_terms = array_diff($xterms, $dterms);
  4297. // Save it to search engine.
  4298. foreach ($missing_terms as $term) {
  4299. $doc->add_term($prefix . $term, 1);
  4300. }
  4301. foreach ($deprecated_terms as $term) {
  4302. $doc->remove_term($prefix . $term);
  4303. }
  4304. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4305. $di->getDb()->flush();
  4306. } else {
  4307. //@todo What we should do here?
  4308. }
  4309. }
  4310. return true;
  4311. }
  4312. /**
  4313. * Sets the theme of the LP (local/remote) (and save)
  4314. * @param string Optional string giving the new theme of this learnpath
  4315. * @return bool Returns true if theme name is not empty
  4316. */
  4317. public function set_theme($name = '')
  4318. {
  4319. $course_id = api_get_course_int_id();
  4320. if ($this->debug > 0) {
  4321. error_log('New LP - In learnpath::set_theme()', 0);
  4322. }
  4323. $this->theme = $name;
  4324. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4325. $lp_id = $this->get_id();
  4326. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4327. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4328. if ($this->debug > 2) {
  4329. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4330. }
  4331. Database::query($sql);
  4332. return true;
  4333. }
  4334. /**
  4335. * Sets the image of an LP (and save)
  4336. * @param string Optional string giving the new image of this learnpath
  4337. * @return bool Returns true if theme name is not empty
  4338. */
  4339. public function set_preview_image($name = '')
  4340. {
  4341. $course_id = api_get_course_int_id();
  4342. if ($this->debug > 0) {
  4343. error_log('New LP - In learnpath::set_preview_image()', 0);
  4344. }
  4345. $this->preview_image = $name;
  4346. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4347. $lp_id = $this->get_id();
  4348. $sql = "UPDATE $lp_table SET
  4349. preview_image = '" . Database::escape_string($this->preview_image). "'
  4350. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4351. if ($this->debug > 2) {
  4352. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4353. }
  4354. Database::query($sql);
  4355. return true;
  4356. }
  4357. /**
  4358. * Sets the author of a LP (and save)
  4359. * @param string Optional string giving the new author of this learnpath
  4360. * @return bool Returns true if author's name is not empty
  4361. */
  4362. public function set_author($name = '')
  4363. {
  4364. $course_id = api_get_course_int_id();
  4365. if ($this->debug > 0) {
  4366. error_log('New LP - In learnpath::set_author()', 0);
  4367. }
  4368. $this->author = $name;
  4369. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4370. $lp_id = $this->get_id();
  4371. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4372. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4373. if ($this->debug > 2) {
  4374. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4375. }
  4376. Database::query($sql);
  4377. return true;
  4378. }
  4379. /**
  4380. * Sets the hide_toc_frame parameter of a LP (and save)
  4381. * @param int 1 if frame is hidden 0 then else
  4382. * @return bool Returns true if author's name is not empty
  4383. */
  4384. public function set_hide_toc_frame($hide)
  4385. {
  4386. $course_id = api_get_course_int_id();
  4387. if ($this->debug > 0) {
  4388. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4389. }
  4390. if (intval($hide) == $hide){
  4391. $this->hide_toc_frame = $hide;
  4392. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4393. $lp_id = $this->get_id();
  4394. $sql = "UPDATE $lp_table SET
  4395. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4396. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4397. if ($this->debug > 2) {
  4398. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4399. }
  4400. Database::query($sql);
  4401. return true;
  4402. } else {
  4403. return false;
  4404. }
  4405. }
  4406. /**
  4407. * Sets the prerequisite of a LP (and save)
  4408. * @param int integer giving the new prerequisite of this learnpath
  4409. * @return bool returns true if prerequisite is not empty
  4410. */
  4411. public function set_prerequisite($prerequisite)
  4412. {
  4413. $course_id = api_get_course_int_id();
  4414. if ($this->debug > 0) {
  4415. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4416. }
  4417. $this->prerequisite = intval($prerequisite);
  4418. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4419. $lp_id = $this->get_id();
  4420. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4421. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4422. if ($this->debug > 2) {
  4423. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4424. }
  4425. Database::query($sql);
  4426. return true;
  4427. }
  4428. /**
  4429. * Sets the location/proximity of the LP (local/remote) (and save)
  4430. * @param string Optional string giving the new location of this learnpath
  4431. * @return boolean True on success / False on error
  4432. */
  4433. public function set_proximity($name = '')
  4434. {
  4435. $course_id = api_get_course_int_id();
  4436. if ($this->debug > 0) {
  4437. error_log('New LP - In learnpath::set_proximity()', 0);
  4438. }
  4439. if (empty ($name))
  4440. return false;
  4441. $this->proximity = $name;
  4442. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4443. $lp_id = $this->get_id();
  4444. $sql = "UPDATE $lp_table SET
  4445. content_local = '" . Database::escape_string($name) . "'
  4446. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4447. if ($this->debug > 2) {
  4448. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4449. }
  4450. Database::query($sql);
  4451. return true;
  4452. }
  4453. /**
  4454. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4455. * @param integer DB ID of the item
  4456. */
  4457. public function set_previous_item($id)
  4458. {
  4459. if ($this->debug > 0) {
  4460. error_log('New LP - In learnpath::set_previous_item()', 0);
  4461. }
  4462. $this->last = $id;
  4463. }
  4464. /**
  4465. * Sets use_max_score
  4466. * @param string $use_max_score Optional string giving the new location of this learnpath
  4467. * @return boolean True on success / False on error
  4468. */
  4469. public function set_use_max_score($use_max_score = 1)
  4470. {
  4471. $course_id = api_get_course_int_id();
  4472. if ($this->debug > 0) {
  4473. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4474. }
  4475. $use_max_score = intval($use_max_score);
  4476. $this->use_max_score = $use_max_score;
  4477. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4478. $lp_id = $this->get_id();
  4479. $sql = "UPDATE $lp_table SET
  4480. use_max_score = '" . $this->use_max_score . "'
  4481. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4482. if ($this->debug > 2) {
  4483. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4484. }
  4485. Database::query($sql);
  4486. return true;
  4487. }
  4488. /**
  4489. * Sets and saves the expired_on date
  4490. * @param string $expired_on Optional string giving the new author of this learnpath
  4491. * @return bool Returns true if author's name is not empty
  4492. */
  4493. public function set_expired_on($expired_on)
  4494. {
  4495. $course_id = api_get_course_int_id();
  4496. if ($this->debug > 0) {
  4497. error_log('New LP - In learnpath::set_expired_on()', 0);
  4498. }
  4499. if (!empty($expired_on)) {
  4500. $this->expired_on = api_get_utc_datetime($expired_on);
  4501. } else {
  4502. $this->expired_on = '';
  4503. }
  4504. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4505. $lp_id = $this->get_id();
  4506. $sql = "UPDATE $lp_table SET
  4507. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4508. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4509. if ($this->debug > 2) {
  4510. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4511. }
  4512. Database::query($sql);
  4513. return true;
  4514. }
  4515. /**
  4516. * Sets and saves the publicated_on date
  4517. * @param string $publicated_on Optional string giving the new author of this learnpath
  4518. * @return bool Returns true if author's name is not empty
  4519. */
  4520. public function set_publicated_on($publicated_on)
  4521. {
  4522. $course_id = api_get_course_int_id();
  4523. if ($this->debug > 0) {
  4524. error_log('New LP - In learnpath::set_expired_on()', 0);
  4525. }
  4526. if (!empty($publicated_on)) {
  4527. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4528. } else {
  4529. $this->publicated_on = '';
  4530. }
  4531. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4532. $lp_id = $this->get_id();
  4533. $sql = "UPDATE $lp_table SET
  4534. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4535. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4536. if ($this->debug > 2) {
  4537. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4538. }
  4539. Database::query($sql);
  4540. return true;
  4541. }
  4542. /**
  4543. * Sets and saves the expired_on date
  4544. * @return bool Returns true if author's name is not empty
  4545. */
  4546. public function set_modified_on()
  4547. {
  4548. $course_id = api_get_course_int_id();
  4549. if ($this->debug > 0) {
  4550. error_log('New LP - In learnpath::set_expired_on()', 0);
  4551. }
  4552. $this->modified_on = api_get_utc_datetime();
  4553. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4554. $lp_id = $this->get_id();
  4555. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4556. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4557. if ($this->debug > 2) {
  4558. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4559. }
  4560. Database::query($sql);
  4561. return true;
  4562. }
  4563. /**
  4564. * Sets the object's error message
  4565. * @param string Error message. If empty, reinits the error string
  4566. * @return void
  4567. */
  4568. public function set_error_msg($error = '')
  4569. {
  4570. if ($this->debug > 0) {
  4571. error_log('New LP - In learnpath::set_error_msg()', 0);
  4572. }
  4573. if (empty ($error)) {
  4574. $this->error = '';
  4575. } else {
  4576. $this->error .= $error;
  4577. }
  4578. }
  4579. /**
  4580. * Launches the current item if not 'sco'
  4581. * (starts timer and make sure there is a record ready in the DB)
  4582. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4583. * @return boolean
  4584. */
  4585. public function start_current_item($allow_new_attempt = false)
  4586. {
  4587. if ($this->debug > 0) {
  4588. error_log('New LP - In learnpath::start_current_item()', 0);
  4589. }
  4590. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4591. $type = $this->get_type();
  4592. $item_type = $this->items[$this->current]->get_type();
  4593. if (($type == 2 && $item_type != 'sco') ||
  4594. ($type == 3 && $item_type != 'au') ||
  4595. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4596. ) {
  4597. $this->items[$this->current]->open($allow_new_attempt);
  4598. $this->autocomplete_parents($this->current);
  4599. $prereq_check = $this->prerequisites_match($this->current);
  4600. $this->items[$this->current]->save(false, $prereq_check);
  4601. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4602. } else {
  4603. // If sco, then it is supposed to have been updated by some other call.
  4604. }
  4605. if ($item_type == 'sco') {
  4606. $this->items[$this->current]->restart();
  4607. }
  4608. }
  4609. if ($this->debug > 0) {
  4610. error_log('New LP - End of learnpath::start_current_item()', 0);
  4611. }
  4612. return true;
  4613. }
  4614. /**
  4615. * Stops the processing and counters for the old item (as held in $this->last)
  4616. * @return boolean True/False
  4617. */
  4618. public function stop_previous_item()
  4619. {
  4620. if ($this->debug > 0) {
  4621. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4622. }
  4623. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4624. if ($this->debug > 2) {
  4625. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4626. }
  4627. switch ($this->get_type()) {
  4628. case '3' :
  4629. if ($this->items[$this->last]->get_type() != 'au') {
  4630. if ($this->debug > 2) {
  4631. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4632. }
  4633. $this->items[$this->last]->close();
  4634. //$this->autocomplete_parents($this->last);
  4635. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4636. } else {
  4637. if ($this->debug > 2) {
  4638. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4639. }
  4640. }
  4641. case '2' :
  4642. if ($this->items[$this->last]->get_type() != 'sco') {
  4643. if ($this->debug > 2) {
  4644. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4645. }
  4646. $this->items[$this->last]->close();
  4647. //$this->autocomplete_parents($this->last);
  4648. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4649. } else {
  4650. if ($this->debug > 2) {
  4651. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4652. }
  4653. }
  4654. break;
  4655. case '1' :
  4656. default :
  4657. if ($this->debug > 2) {
  4658. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4659. }
  4660. $this->items[$this->last]->close();
  4661. break;
  4662. }
  4663. } else {
  4664. if ($this->debug > 2) {
  4665. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4666. }
  4667. return false;
  4668. }
  4669. return true;
  4670. }
  4671. /**
  4672. * Updates the default view mode from fullscreen to embedded and inversely
  4673. * @return string The current default view mode ('fullscreen' or 'embedded')
  4674. */
  4675. public function update_default_view_mode()
  4676. {
  4677. $course_id = api_get_course_int_id();
  4678. if ($this->debug > 0) {
  4679. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4680. }
  4681. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4682. $sql = "SELECT * FROM $lp_table
  4683. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4684. $res = Database::query($sql);
  4685. if (Database :: num_rows($res) > 0) {
  4686. $row = Database :: fetch_array($res);
  4687. $default_view_mode = $row['default_view_mod'];
  4688. $view_mode = $default_view_mode;
  4689. switch ($default_view_mode) {
  4690. case 'fullscreen':
  4691. $view_mode = 'embedded';
  4692. break;
  4693. case 'embedded':
  4694. $view_mode = 'embedframe';
  4695. break;
  4696. case 'embedframe':
  4697. $view_mode = 'impress';
  4698. break;
  4699. case 'impress':
  4700. $view_mode = 'fullscreen';
  4701. break;
  4702. }
  4703. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4704. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4705. Database::query($sql);
  4706. $this->mode = $view_mode;
  4707. return $view_mode;
  4708. } else {
  4709. if ($this->debug > 2) {
  4710. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4711. }
  4712. }
  4713. return -1;
  4714. }
  4715. /**
  4716. * Updates the default behaviour about auto-commiting SCORM updates
  4717. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4718. */
  4719. public function update_default_scorm_commit()
  4720. {
  4721. $course_id = api_get_course_int_id();
  4722. if ($this->debug > 0) {
  4723. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4724. }
  4725. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4726. $sql = "SELECT * FROM $lp_table
  4727. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4728. $res = Database::query($sql);
  4729. if (Database :: num_rows($res) > 0) {
  4730. $row = Database :: fetch_array($res);
  4731. $force = $row['force_commit'];
  4732. if ($force == 1) {
  4733. $force = 0;
  4734. $force_return = false;
  4735. } elseif ($force == 0) {
  4736. $force = 1;
  4737. $force_return = true;
  4738. }
  4739. $sql = "UPDATE $lp_table SET force_commit = $force
  4740. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4741. Database::query($sql);
  4742. $this->force_commit = $force_return;
  4743. return $force_return;
  4744. } else {
  4745. if ($this->debug > 2) {
  4746. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4747. }
  4748. }
  4749. return -1;
  4750. }
  4751. /**
  4752. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4753. * @return bool True on success, false on failure
  4754. */
  4755. public function update_display_order()
  4756. {
  4757. $course_id = api_get_course_int_id();
  4758. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4759. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4760. $res = Database::query($sql);
  4761. if ($res === false)
  4762. return false;
  4763. $num = Database :: num_rows($res);
  4764. // First check the order is correct, globally (might be wrong because
  4765. // of versions < 1.8.4).
  4766. if ($num > 0) {
  4767. $i = 1;
  4768. while ($row = Database :: fetch_array($res)) {
  4769. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4770. $need_fix = true;
  4771. $sql = "UPDATE $lp_table SET display_order = $i
  4772. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4773. Database::query($sql);
  4774. }
  4775. $i++;
  4776. }
  4777. }
  4778. return true;
  4779. }
  4780. /**
  4781. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4782. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4783. */
  4784. public function update_reinit()
  4785. {
  4786. $course_id = api_get_course_int_id();
  4787. if ($this->debug > 0) {
  4788. error_log('New LP - In learnpath::update_reinit()', 0);
  4789. }
  4790. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4791. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4792. $res = Database::query($sql);
  4793. if (Database :: num_rows($res) > 0) {
  4794. $row = Database :: fetch_array($res);
  4795. $force = $row['prevent_reinit'];
  4796. if ($force == 1) {
  4797. $force = 0;
  4798. } elseif ($force == 0) {
  4799. $force = 1;
  4800. }
  4801. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4802. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4803. Database::query($sql);
  4804. $this->prevent_reinit = $force;
  4805. return $force;
  4806. } else {
  4807. if ($this->debug > 2) {
  4808. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4809. }
  4810. }
  4811. return -1;
  4812. }
  4813. /**
  4814. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4815. *
  4816. * @return string 'single', 'multi' or 'seriousgame'
  4817. * @author ndiechburg <noel@cblue.be>
  4818. **/
  4819. public function get_attempt_mode()
  4820. {
  4821. //Set default value for seriousgame_mode
  4822. if (!isset($this->seriousgame_mode)) {
  4823. $this->seriousgame_mode=0;
  4824. }
  4825. // Set default value for prevent_reinit
  4826. if (!isset($this->prevent_reinit)) {
  4827. $this->prevent_reinit =1;
  4828. }
  4829. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4830. return 'seriousgame';
  4831. }
  4832. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4833. return 'single';
  4834. }
  4835. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4836. return 'multiple';
  4837. }
  4838. return 'single';
  4839. }
  4840. /**
  4841. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4842. *
  4843. * @param string 'seriousgame', 'single' or 'multiple'
  4844. * @return boolean
  4845. * @author ndiechburg <noel@cblue.be>
  4846. **/
  4847. public function set_attempt_mode($mode)
  4848. {
  4849. $course_id = api_get_course_int_id();
  4850. switch ($mode) {
  4851. case 'seriousgame' :
  4852. $sg_mode = 1;
  4853. $prevent_reinit = 1;
  4854. break;
  4855. case 'single' :
  4856. $sg_mode = 0;
  4857. $prevent_reinit = 1;
  4858. break;
  4859. case 'multiple' :
  4860. $sg_mode = 0;
  4861. $prevent_reinit = 0;
  4862. break;
  4863. default :
  4864. $sg_mode = 0;
  4865. $prevent_reinit = 0;
  4866. break;
  4867. }
  4868. $this->prevent_reinit = $prevent_reinit;
  4869. $this->seriousgame_mode = $sg_mode;
  4870. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4871. $sql = "UPDATE $lp_table SET
  4872. prevent_reinit = $prevent_reinit ,
  4873. seriousgame_mode = $sg_mode
  4874. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4875. $res = Database::query($sql);
  4876. if ($res) {
  4877. return true;
  4878. } else {
  4879. return false;
  4880. }
  4881. }
  4882. /**
  4883. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4884. *
  4885. * @return boolean
  4886. * @author ndiechburg <noel@cblue.be>
  4887. **/
  4888. public function switch_attempt_mode()
  4889. {
  4890. if ($this->debug > 0) {
  4891. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4892. }
  4893. $mode = $this->get_attempt_mode();
  4894. switch ($mode) {
  4895. case 'single' :
  4896. $next_mode = 'multiple';
  4897. break;
  4898. case 'multiple' :
  4899. $next_mode = 'seriousgame';
  4900. break;
  4901. case 'seriousgame' :
  4902. $next_mode = 'single';
  4903. break;
  4904. default :
  4905. $next_mode = 'single';
  4906. break;
  4907. }
  4908. $this->set_attempt_mode($next_mode);
  4909. }
  4910. /**
  4911. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4912. * but possibility to do again a completed item.
  4913. *
  4914. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4915. * @author ndiechburg <noel@cblue.be>
  4916. **/
  4917. public function set_seriousgame_mode()
  4918. {
  4919. $course_id = api_get_course_int_id();
  4920. if ($this->debug > 0) {
  4921. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4922. }
  4923. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4924. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4925. $res = Database::query($sql);
  4926. if (Database :: num_rows($res) > 0) {
  4927. $row = Database :: fetch_array($res);
  4928. $force = $row['seriousgame_mode'];
  4929. if ($force == 1) {
  4930. $force = 0;
  4931. } elseif ($force == 0) {
  4932. $force = 1;
  4933. }
  4934. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4935. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4936. Database::query($sql);
  4937. $this->seriousgame_mode = $force;
  4938. return $force;
  4939. } else {
  4940. if ($this->debug > 2) {
  4941. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4942. }
  4943. }
  4944. return -1;
  4945. }
  4946. /**
  4947. * Updates the "scorm_debug" value that shows or hide the debug window
  4948. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4949. */
  4950. public function update_scorm_debug()
  4951. {
  4952. $course_id = api_get_course_int_id();
  4953. if ($this->debug > 0) {
  4954. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4955. }
  4956. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4957. $sql = "SELECT * FROM $lp_table
  4958. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4959. $res = Database::query($sql);
  4960. if (Database :: num_rows($res) > 0) {
  4961. $row = Database :: fetch_array($res);
  4962. $force = $row['debug'];
  4963. if ($force == 1) {
  4964. $force = 0;
  4965. } elseif ($force == 0) {
  4966. $force = 1;
  4967. }
  4968. $sql = "UPDATE $lp_table SET debug = $force
  4969. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4970. $res = Database::query($sql);
  4971. $this->scorm_debug = $force;
  4972. return $force;
  4973. } else {
  4974. if ($this->debug > 2) {
  4975. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4976. }
  4977. }
  4978. return -1;
  4979. }
  4980. /**
  4981. * Function that makes a call to the function sort_tree_array and create_tree_array
  4982. * @author Kevin Van Den Haute
  4983. * @param array
  4984. */
  4985. public function tree_array($array)
  4986. {
  4987. if ($this->debug > 1) {
  4988. error_log('New LP - In learnpath::tree_array()', 0);
  4989. }
  4990. $array = $this->sort_tree_array($array);
  4991. $this->create_tree_array($array);
  4992. }
  4993. /**
  4994. * Creates an array with the elements of the learning path tree in it
  4995. *
  4996. * @author Kevin Van Den Haute
  4997. * @param array $array
  4998. * @param int $parent
  4999. * @param int $depth
  5000. * @param array $tmp
  5001. */
  5002. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  5003. {
  5004. if ($this->debug > 1) {
  5005. error_log('New LP - In learnpath::create_tree_array())', 0);
  5006. }
  5007. if (is_array($array)) {
  5008. for ($i = 0; $i < count($array); $i++) {
  5009. if ($array[$i]['parent_item_id'] == $parent) {
  5010. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5011. $tmp[] = $array[$i]['parent_item_id'];
  5012. $depth++;
  5013. }
  5014. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5015. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5016. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5017. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5018. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5019. $this->arrMenu[] = array(
  5020. 'id' => $array[$i]['id'],
  5021. 'item_type' => $array[$i]['item_type'],
  5022. 'title' => $array[$i]['title'],
  5023. 'path' => $path,
  5024. 'description' => $array[$i]['description'],
  5025. 'parent_item_id' => $array[$i]['parent_item_id'],
  5026. 'previous_item_id' => $array[$i]['previous_item_id'],
  5027. 'next_item_id' => $array[$i]['next_item_id'],
  5028. 'min_score' => $array[$i]['min_score'],
  5029. 'max_score' => $array[$i]['max_score'],
  5030. 'mastery_score' => $array[$i]['mastery_score'],
  5031. 'display_order' => $array[$i]['display_order'],
  5032. 'prerequisite' => $preq,
  5033. 'depth' => $depth,
  5034. 'audio' => $audio,
  5035. 'prerequisite_min_score' => $prerequisiteMinScore,
  5036. 'prerequisite_max_score' => $prerequisiteMaxScore
  5037. );
  5038. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5039. }
  5040. }
  5041. }
  5042. }
  5043. /**
  5044. * Sorts a multi dimensional array by parent id and display order
  5045. * @author Kevin Van Den Haute
  5046. *
  5047. * @param array $array (array with al the learning path items in it)
  5048. *
  5049. * @return array
  5050. */
  5051. public function sort_tree_array($array) {
  5052. foreach ($array as $key => $row) {
  5053. $parent[$key] = $row['parent_item_id'];
  5054. $position[$key] = $row['display_order'];
  5055. }
  5056. if (count($array) > 0)
  5057. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5058. return $array;
  5059. }
  5060. /**
  5061. * Function that creates a html list of learning path items so that we can add audio files to them
  5062. * @author Kevin Van Den Haute
  5063. * @param int $lp_id
  5064. * @return string
  5065. */
  5066. public function overview()
  5067. {
  5068. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5069. if ($this->debug > 0) {
  5070. error_log('New LP - In learnpath::overview()', 0);
  5071. }
  5072. global $_course;
  5073. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5074. $return = '';
  5075. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5076. // we need to start a form when we want to update all the mp3 files
  5077. if ($update_audio == 'true') {
  5078. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&action=' . Security :: remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5079. }
  5080. $return .= '<div id="message"></div>';
  5081. if (count($this->items) == 0) {
  5082. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5083. } else {
  5084. $return_audio = '<table class="data_table">';
  5085. $return_audio .= '<tr>';
  5086. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5087. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5088. $return_audio .= '</tr>';
  5089. if ($update_audio != 'true') {
  5090. $return .= '<div class="col-md-12">';
  5091. $return .= self::return_new_tree($update_audio);
  5092. $return .='</div>';
  5093. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5094. } else {
  5095. $return_audio .= self::return_new_tree($update_audio);
  5096. $return .= $return_audio.'</table>';
  5097. }
  5098. // We need to close the form when we are updating the mp3 files.
  5099. if ($update_audio == 'true') {
  5100. $return .= '<div class="footer-audio">';
  5101. $return .= Display::button('save_audio','<i class="fa fa-file-audio-o"></i> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5102. $return .= '</div>';
  5103. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5104. }
  5105. }
  5106. // We need to close the form when we are updating the mp3 files.
  5107. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5108. $return .= '</form>';
  5109. }
  5110. return $return;
  5111. }
  5112. /**
  5113. * @param string string $update_audio
  5114. * @param bool $drop_element_here
  5115. * @return string
  5116. */
  5117. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5118. {
  5119. $return = '';
  5120. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5121. $course_id = api_get_course_int_id();
  5122. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5123. $sql = "SELECT * FROM $tbl_lp_item
  5124. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5125. $result = Database::query($sql);
  5126. $arrLP = array();
  5127. while ($row = Database :: fetch_array($result)) {
  5128. $arrLP[] = array(
  5129. 'id' => $row['id'],
  5130. 'item_type' => $row['item_type'],
  5131. 'title' => Security :: remove_XSS($row['title']),
  5132. 'path' => $row['path'],
  5133. 'description' => Security::remove_XSS($row['description']),
  5134. 'parent_item_id' => $row['parent_item_id'],
  5135. 'previous_item_id' => $row['previous_item_id'],
  5136. 'next_item_id' => $row['next_item_id'],
  5137. 'max_score' => $row['max_score'],
  5138. 'min_score' => $row['min_score'],
  5139. 'mastery_score' => $row['mastery_score'],
  5140. 'prerequisite' => $row['prerequisite'],
  5141. 'display_order' => $row['display_order'],
  5142. 'audio' => $row['audio'],
  5143. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5144. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5145. );
  5146. }
  5147. $this->tree_array($arrLP);
  5148. $arrLP = $this->arrMenu;
  5149. unset ($this->arrMenu);
  5150. $default_data = null;
  5151. $default_content = null;
  5152. $elements = array();
  5153. $return_audio = null;
  5154. for ($i = 0; $i < count($arrLP); $i++) {
  5155. $title = $arrLP[$i]['title'];
  5156. $title_cut = cut($arrLP[$i]['title'], 25);
  5157. //Link for the documents
  5158. if ($arrLP[$i]['item_type'] == 'document') {
  5159. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5160. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5161. }
  5162. if (($i % 2) == 0) {
  5163. $oddClass = 'row_odd';
  5164. } else {
  5165. $oddClass = 'row_even';
  5166. }
  5167. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5168. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5169. $icon = '';
  5170. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5171. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5172. } else {
  5173. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5174. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5175. } else {
  5176. $icon = '<img src="../img/folder_document.gif" />';
  5177. }
  5178. }
  5179. // The audio column.
  5180. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5181. $audio = '';
  5182. if (!$update_audio || $update_audio <> 'true') {
  5183. if (!empty($arrLP[$i]['audio'])) {
  5184. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5185. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5186. $audio .= '<script type="text/javascript">
  5187. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5188. s1.addParam("allowscriptaccess","always");
  5189. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5190. s1.write("container' . $i . '");
  5191. </script>';*/
  5192. } else {
  5193. $audio .= '';
  5194. }
  5195. } else {
  5196. $types = self::getChapterTypes();
  5197. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5198. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5199. if (!empty ($arrLP[$i]['audio'])) {
  5200. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5201. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5202. }
  5203. }
  5204. }
  5205. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5206. $return_audio .= '</td>';
  5207. $move_icon = '';
  5208. $move_item_icon = '';
  5209. $edit_icon = '';
  5210. $delete_icon = '';
  5211. $audio_icon = '';
  5212. $prerequisities_icon = '';
  5213. if ($is_allowed_to_edit) {
  5214. if (!$update_audio || $update_audio <> 'true') {
  5215. $move_icon .= '<a class="moved" href="#">';
  5216. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5217. $move_icon .= '</a>';
  5218. }
  5219. // No edit for this item types
  5220. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5221. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5222. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&view=build&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
  5223. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5224. $edit_icon .= '</a>';
  5225. } else {
  5226. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
  5227. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5228. $edit_icon .= '</a>';
  5229. }
  5230. }
  5231. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
  5232. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5233. $delete_icon .= '</a>';
  5234. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5235. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5236. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5237. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5238. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5239. }
  5240. }
  5241. if ($update_audio != 'true') {
  5242. $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'));
  5243. } else {
  5244. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5245. }
  5246. $parent_id = $arrLP[$i]['parent_item_id'];
  5247. $default_data[$arrLP[$i]['id']] = $row;
  5248. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5249. if (empty($parent_id)) {
  5250. $elements[$arrLP[$i]['id']]['data'] = $row;
  5251. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5252. } else {
  5253. $parent_arrays = array();
  5254. if ($arrLP[$i]['depth'] > 1) {
  5255. //Getting list of parents
  5256. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5257. foreach($arrLP as $item) {
  5258. if ($item['id'] == $parent_id) {
  5259. if ($item['parent_item_id'] == 0) {
  5260. $parent_id = $item['id'];
  5261. break;
  5262. } else {
  5263. $parent_id = $item['parent_item_id'];
  5264. if (empty($parent_arrays)) {
  5265. $parent_arrays[] = intval($item['id']);
  5266. }
  5267. $parent_arrays[] = $parent_id;
  5268. break;
  5269. }
  5270. }
  5271. }
  5272. }
  5273. }
  5274. if (!empty($parent_arrays)) {
  5275. $parent_arrays = array_reverse($parent_arrays);
  5276. $val = '$elements';
  5277. $x = 0;
  5278. foreach($parent_arrays as $item) {
  5279. if ($x != count($parent_arrays) -1) {
  5280. $val .= '["'.$item.'"]["children"]';
  5281. } else {
  5282. $val .= '["'.$item.'"]["children"]';
  5283. }
  5284. $x++;
  5285. }
  5286. $val .= "";
  5287. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5288. eval($code_str);
  5289. } else {
  5290. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5291. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5292. }
  5293. }
  5294. }
  5295. $list = '<ul id="lp_item_list">';
  5296. $tree = self::print_recursive($elements, $default_data, $default_content);
  5297. if (!empty($tree)) {
  5298. $list .= $tree;
  5299. } else {
  5300. if ($drop_element_here) {
  5301. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5302. }
  5303. }
  5304. $list .= '</ul>';
  5305. $return .= Display::panel($list, $this->name);
  5306. if ($update_audio == 'true') {
  5307. $return = $return_audio;
  5308. }
  5309. return $return;
  5310. }
  5311. /**
  5312. * @param array $elements
  5313. * @param array $default_data
  5314. * @param array $default_content
  5315. * @return string
  5316. */
  5317. public function print_recursive($elements, $default_data, $default_content)
  5318. {
  5319. $return = '';
  5320. foreach ($elements as $key => $item) {
  5321. if (isset($item['load_data']) || empty($item['data'])) {
  5322. $item['data'] = $default_data[$item['load_data']];
  5323. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5324. }
  5325. $sub_list = '';
  5326. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5327. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5328. }
  5329. if (empty($item['children'])) {
  5330. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5331. $active = null;
  5332. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5333. $active = 'active';
  5334. }
  5335. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5336. } else {
  5337. //sections
  5338. if (isset($item['children'])) {
  5339. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5340. }
  5341. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5342. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5343. }
  5344. }
  5345. return $return;
  5346. }
  5347. /**
  5348. * This function builds the action menu
  5349. * @param bool $returnContent
  5350. * @return void
  5351. */
  5352. public function build_action_menu($returnContent = false)
  5353. {
  5354. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5355. $return = '<div class="actions">';
  5356. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&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> ';
  5357. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=admin_view&lp_id=' . $_SESSION['oLP']->lp_id . '&updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5358. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&action=edit&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5359. $buttons = array(
  5360. array(
  5361. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5362. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5363. ),
  5364. array(
  5365. 'title' => get_lang('ClearAllPrerequisites'),
  5366. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5367. ),
  5368. );
  5369. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5370. $return .= '</div>';
  5371. if ($returnContent) {
  5372. return $return;
  5373. }
  5374. echo $return;
  5375. }
  5376. /**
  5377. * This functions builds the LP tree based on data from the database.
  5378. * @return string
  5379. * @deprecated use the return_new_tree() function
  5380. * @uses dtree.js :: necessary javascript for building this tree
  5381. */
  5382. public function build_tree()
  5383. {
  5384. $course_id = api_get_course_int_id();
  5385. $return = "<script type=\"text/javascript\">\n";
  5386. $return .= "\tm = new dTree('m');\n\n";
  5387. $return .= "\tm.config.folderLinks = true;\n";
  5388. $return .= "\tm.config.useCookies = true;\n";
  5389. $return .= "\tm.config.useIcons = true;\n";
  5390. $return .= "\tm.config.useLines = true;\n";
  5391. $return .= "\tm.config.useSelection = true;\n";
  5392. $return .= "\tm.config.useStatustext = false;\n\n";
  5393. $menu = 0;
  5394. $parent = '';
  5395. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  5396. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5397. $sql = " SELECT
  5398. id,
  5399. title,
  5400. description,
  5401. item_type,
  5402. path,
  5403. parent_item_id,
  5404. previous_item_id,
  5405. next_item_id,
  5406. max_score,
  5407. min_score,
  5408. mastery_score,
  5409. display_order
  5410. FROM $tbl_lp_item
  5411. WHERE c_id = ".$course_id." AND lp_id = " . intval($this->lp_id);
  5412. $result = Database::query($sql);
  5413. $arrLP = array ();
  5414. while ($row = Database :: fetch_array($result)) {
  5415. $row['title'] = Security :: remove_XSS($row['title']);
  5416. $row['description'] = Security :: remove_XSS($row['description']);
  5417. $arrLP[] = array(
  5418. 'id' => $row['id'],
  5419. 'item_type' => $row['item_type'],
  5420. 'title' => $row['title'],
  5421. 'path' => $row['path'],
  5422. 'description' => $row['description'],
  5423. 'parent_item_id' => $row['parent_item_id'],
  5424. 'previous_item_id' => $row['previous_item_id'],
  5425. 'next_item_id' => $row['next_item_id'],
  5426. 'max_score' => $row['max_score'],
  5427. 'min_score' => $row['min_score'],
  5428. 'mastery_score' => $row['mastery_score'],
  5429. 'display_order' => $row['display_order']
  5430. );
  5431. }
  5432. $this->tree_array($arrLP);
  5433. $arrLP = $this->arrMenu;
  5434. unset ($this->arrMenu);
  5435. for ($i = 0; $i < count($arrLP); $i++) {
  5436. $title = addslashes($arrLP[$i]['title']);
  5437. $menu_page = api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=view_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $_SESSION['oLP']->lp_id;
  5438. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5439. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5440. $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";
  5441. } else
  5442. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5443. $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";
  5444. } else {
  5445. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5446. }
  5447. if ($menu < $arrLP[$i]['id'])
  5448. $menu = $arrLP[$i]['id'];
  5449. }
  5450. $return .= "\n\tdocument.write(m);\n";
  5451. $return .= "\t if(!m.selectedNode) m.s(1);";
  5452. $return .= "</script>\n";
  5453. return $return;
  5454. }
  5455. /**
  5456. * Creates the default learning path folder
  5457. * @param array $course
  5458. * @return bool
  5459. */
  5460. public static function generate_learning_path_folder($course)
  5461. {
  5462. // Creating learning_path folder
  5463. $dir = '/learning_path';
  5464. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5465. $folder = false;
  5466. if (!is_dir($filepath.'/'.$dir)) {
  5467. $folderData = create_unexisting_directory(
  5468. $course,
  5469. api_get_user_id(),
  5470. api_get_session_id(),
  5471. 0,
  5472. 0,
  5473. $filepath,
  5474. $dir,
  5475. get_lang('LearningPaths'),
  5476. 0
  5477. );
  5478. if (!empty($folderData)) {
  5479. $folder = true;
  5480. }
  5481. } else {
  5482. $folder = true;
  5483. }
  5484. return $folder;
  5485. }
  5486. /**
  5487. * @param array $course
  5488. * @param string $lp_name
  5489. * @return array
  5490. */
  5491. public function generate_lp_folder($course, $lp_name = null)
  5492. {
  5493. $filepath = '';
  5494. $dir = '/learning_path/';
  5495. if (empty($lp_name)) {
  5496. $lp_name = $this->name;
  5497. }
  5498. $folder = self::generate_learning_path_folder($course);
  5499. // Creating LP folder
  5500. if ($folder) {
  5501. //Limits title size
  5502. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5503. $dir = $dir.$title;
  5504. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5505. if (!is_dir($filepath.'/'.$dir)) {
  5506. $folderData = create_unexisting_directory(
  5507. $course,
  5508. api_get_user_id(),
  5509. 0,
  5510. 0,
  5511. 0,
  5512. $filepath,
  5513. $dir,
  5514. $lp_name
  5515. );
  5516. if (!empty($folderData)) {
  5517. $folder = true;
  5518. }
  5519. } else {
  5520. $folder = true;
  5521. }
  5522. $dir = $dir.'/';
  5523. if ($folder) {
  5524. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5525. }
  5526. }
  5527. $array = array(
  5528. 'dir' => $dir,
  5529. 'filepath' => $filepath,
  5530. 'folder' => $folder
  5531. );
  5532. return $array;
  5533. }
  5534. /**
  5535. * Create a new document //still needs some finetuning
  5536. * @param array $_course
  5537. * @return string
  5538. */
  5539. public function create_document($_course)
  5540. {
  5541. $course_id = api_get_course_int_id();
  5542. global $charset;
  5543. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5544. if (strstr($dir, '..'))
  5545. $dir = '/';
  5546. if ($dir[0] == '.')
  5547. $dir = substr($dir, 1);
  5548. if ($dir[0] != '/')
  5549. $dir = '/' . $dir;
  5550. if ($dir[strlen($dir) - 1] != '/')
  5551. $dir .= '/';
  5552. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5553. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5554. //Generates folder
  5555. $result = $this->generate_lp_folder($_course);
  5556. $dir = $result['dir'];
  5557. $filepath = $result['filepath'];
  5558. }
  5559. if (!is_dir($filepath)) {
  5560. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5561. $dir = '/';
  5562. }
  5563. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5564. // is already escaped twice when it gets here.
  5565. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5566. $title = disable_dangerous_file($title);
  5567. $filename = $title;
  5568. $content = $_POST['content_lp'];
  5569. $tmp_filename = $filename;
  5570. $i = 0;
  5571. while (file_exists($filepath . $tmp_filename . '.html'))
  5572. $tmp_filename = $filename . '_' . ++ $i;
  5573. $filename = $tmp_filename . '.html';
  5574. $content = stripslashes($content);
  5575. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5576. // Change the path of mp3 to absolute.
  5577. // The first regexp deals with :// urls.
  5578. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5579. // The second regexp deals with audio/ urls.
  5580. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5581. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5582. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5583. if (!file_exists($filepath . $filename)) {
  5584. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5585. fputs($fp, $content);
  5586. fclose($fp);
  5587. $file_size = filesize($filepath . $filename);
  5588. $save_file_path = $dir.$filename;
  5589. $document_id = add_document(
  5590. $_course,
  5591. $save_file_path,
  5592. 'file',
  5593. $file_size,
  5594. $tmp_filename
  5595. );
  5596. if ($document_id) {
  5597. api_item_property_update(
  5598. $_course,
  5599. TOOL_DOCUMENT,
  5600. $document_id,
  5601. 'DocumentAdded',
  5602. api_get_user_id(),
  5603. null,
  5604. null,
  5605. null,
  5606. null,
  5607. api_get_session_id()
  5608. );
  5609. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5610. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5611. if ($new_comment || $new_title) {
  5612. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5613. $ct = '';
  5614. if ($new_comment)
  5615. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5616. if ($new_title)
  5617. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5618. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5619. Database::query($sql_update);
  5620. }
  5621. }
  5622. return $document_id;
  5623. }
  5624. }
  5625. }
  5626. /**
  5627. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5628. * @param array $_course array
  5629. * @return void
  5630. */
  5631. public function edit_document($_course)
  5632. {
  5633. $course_id = api_get_course_int_id();
  5634. global $_configuration;
  5635. // Please, do not modify this dirname formatting.
  5636. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5637. if (strstr($dir, '..'))
  5638. $dir = '/';
  5639. if ($dir[0] == '.')
  5640. $dir = substr($dir, 1);
  5641. if ($dir[0] != '/')
  5642. $dir = '/' . $dir;
  5643. if ($dir[strlen($dir) - 1] != '/')
  5644. $dir .= '/';
  5645. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5646. if (!is_dir($filepath)) {
  5647. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5648. $dir = '/';
  5649. }
  5650. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5651. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5652. $document_id = intval($_POST['path']);
  5653. $sql = "SELECT path FROM " . $table_doc . "
  5654. WHERE c_id = $course_id AND id = " . $document_id;
  5655. $res = Database::query($sql);
  5656. $row = Database :: fetch_array($res);
  5657. $content = stripslashes($_POST['content_lp']);
  5658. $file = $filepath . $row['path'];
  5659. if ($fp = @ fopen($file, 'w')) {
  5660. $content = text_filter($content);
  5661. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5662. // Change the path of mp3 to absolute.
  5663. // The first regexp deals with :// urls.
  5664. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5665. // The second regexp deals with audio/ urls.
  5666. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5667. fputs($fp, $content);
  5668. fclose($fp);
  5669. $sql = "UPDATE " . $table_doc ." SET
  5670. title='".Database::escape_string($_POST['title'])."'
  5671. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5672. Database::query($sql);
  5673. }
  5674. }
  5675. }
  5676. /**
  5677. * Displays the selected item, with a panel for manipulating the item
  5678. * @param int $item_id
  5679. * @param string $msg
  5680. * @return string
  5681. */
  5682. public function display_item($item_id, $msg = null, $show_actions = true)
  5683. {
  5684. $course_id = api_get_course_int_id();
  5685. $return = '';
  5686. if (is_numeric($item_id)) {
  5687. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5688. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5689. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5690. $result = Database::query($sql);
  5691. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5692. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5693. // Prevents wrong parent selection for document, see Bug#1251.
  5694. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5695. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5696. }
  5697. if ($show_actions) {
  5698. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5699. }
  5700. $return .= '<div style="padding:10px;">';
  5701. if ($msg != '')
  5702. $return .= $msg;
  5703. $return .= '<h3>'.$row['title'].'</h3>';
  5704. switch ($row['item_type']) {
  5705. case TOOL_QUIZ:
  5706. if (!empty($row['path'])) {
  5707. $exercise = new Exercise();
  5708. $exercise->read($row['path']);
  5709. $return .= $exercise->description.'<br />';
  5710. }
  5711. break;
  5712. case TOOL_DOCUMENT:
  5713. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5714. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5715. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5716. $result = Database::query($sql_doc);
  5717. $path_file = Database::result($result, 0, 0);
  5718. $path_parts = pathinfo($path_file);
  5719. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5720. if (in_array($path_parts['extension'], array(
  5721. 'html',
  5722. 'txt',
  5723. 'png',
  5724. 'jpg',
  5725. 'JPG',
  5726. 'jpeg',
  5727. 'JPEG',
  5728. 'gif',
  5729. 'swf'
  5730. ))) {
  5731. $return .= $this->display_document($row['path'], true, true);
  5732. }
  5733. break;
  5734. }
  5735. $return .= '</div>';
  5736. }
  5737. }
  5738. return $return;
  5739. }
  5740. /**
  5741. * Shows the needed forms for editing a specific item
  5742. * @param int $item_id
  5743. * @return string
  5744. */
  5745. public function display_edit_item($item_id)
  5746. {
  5747. $course_id = api_get_course_int_id();
  5748. $return = '';
  5749. if (is_numeric($item_id)) {
  5750. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5751. $sql = "SELECT * FROM $tbl_lp_item
  5752. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5753. $res = Database::query($sql);
  5754. $row = Database::fetch_array($res);
  5755. switch ($row['item_type']) {
  5756. case 'dokeos_chapter' :
  5757. case 'dir' :
  5758. case 'asset' :
  5759. case 'sco' :
  5760. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5761. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5762. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5763. } else {
  5764. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5765. }
  5766. break;
  5767. case TOOL_DOCUMENT :
  5768. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5769. $sql = "SELECT lp.*, doc.path as dir
  5770. FROM " . $tbl_lp_item . " as lp
  5771. LEFT JOIN " . $tbl_doc . " as doc
  5772. ON doc.id = lp.path
  5773. WHERE
  5774. lp.c_id = $course_id AND
  5775. doc.c_id = $course_id AND
  5776. lp.id = " . intval($item_id);
  5777. $res_step = Database::query($sql);
  5778. $row_step = Database :: fetch_array($res_step);
  5779. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5780. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5781. break;
  5782. case TOOL_LINK :
  5783. $link_id = (string) $row['path'];
  5784. if (ctype_digit($link_id)) {
  5785. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5786. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5787. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5788. $res_link = Database::query($sql_select);
  5789. $row_link = Database :: fetch_array($res_link);
  5790. if (is_array($row_link)) {
  5791. $row['url'] = $row_link['url'];
  5792. }
  5793. }
  5794. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5795. $return .= $this->display_link_form('edit', $item_id, $row);
  5796. break;
  5797. case 'dokeos_module' :
  5798. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5799. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5800. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5801. } else {
  5802. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5803. }
  5804. break;
  5805. case TOOL_QUIZ :
  5806. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5807. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5808. break;
  5809. case TOOL_HOTPOTATOES :
  5810. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5811. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5812. break;
  5813. case TOOL_STUDENTPUBLICATION :
  5814. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5815. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5816. break;
  5817. case TOOL_FORUM :
  5818. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5819. $return .= $this->display_forum_form('edit', $item_id, $row);
  5820. break;
  5821. case TOOL_THREAD :
  5822. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5823. $return .= $this->display_thread_form('edit', $item_id, $row);
  5824. break;
  5825. }
  5826. }
  5827. return $return;
  5828. }
  5829. /**
  5830. * Function that displays a list with al the resources that
  5831. * could be added to the learning path
  5832. * @return string
  5833. */
  5834. public function display_resources()
  5835. {
  5836. $course_code = api_get_course_id();
  5837. // Get all the docs.
  5838. $documents = $this->get_documents(true);
  5839. // Get all the exercises.
  5840. $exercises = $this->get_exercises();
  5841. // Get all the links.
  5842. $links = $this->get_links();
  5843. // Get al the student publications.
  5844. $works = $this->get_student_publications();
  5845. // Get al the forums.
  5846. $forums = $this->get_forums(null, $course_code);
  5847. $headers = array(
  5848. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5849. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5850. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5851. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5852. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5853. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5854. );
  5855. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5856. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5857. echo Display::tabs(
  5858. $headers,
  5859. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5860. );
  5861. return true;
  5862. }
  5863. /**
  5864. * Returns the extension of a document
  5865. * @param string filename
  5866. * @return string Extension (part after the last dot)
  5867. */
  5868. public function get_extension($filename)
  5869. {
  5870. $explode = explode('.', $filename);
  5871. return $explode[count($explode) - 1];
  5872. }
  5873. /**
  5874. * Displays a document by id
  5875. *
  5876. * @param int $id
  5877. * @return string
  5878. */
  5879. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5880. {
  5881. global $_course; // It is temporary.
  5882. $course_id = api_get_course_int_id();
  5883. $return = '';
  5884. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5885. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5886. WHERE c_id = ".$course_id." AND id = " . $id;
  5887. $res_doc = Database::query($sql_doc);
  5888. $row_doc = Database :: fetch_array($res_doc);
  5889. // TODO: Add a path filter.
  5890. if ($iframe) {
  5891. $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>';
  5892. } else {
  5893. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5894. }
  5895. return $return;
  5896. }
  5897. /**
  5898. * Return HTML form to add/edit a quiz
  5899. * @param string Action (add/edit)
  5900. * @param integer Item ID if already exists
  5901. * @param mixed Extra information (quiz ID if integer)
  5902. * @return string HTML form
  5903. */
  5904. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5905. {
  5906. $course_id = api_get_course_int_id();
  5907. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5908. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5909. if ($id != 0 && is_array($extra_info)) {
  5910. $item_title = $extra_info['title'];
  5911. $item_description = $extra_info['description'];
  5912. } elseif (is_numeric($extra_info)) {
  5913. $sql = "SELECT title, description
  5914. FROM " . $tbl_quiz . "
  5915. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5916. $result = Database::query($sql);
  5917. $row = Database::fetch_array($result);
  5918. $item_title = $row['title'];
  5919. $item_description = $row['description'];
  5920. } else {
  5921. $item_title = '';
  5922. $item_description = '';
  5923. }
  5924. $item_title = Security::remove_XSS($item_title);
  5925. $item_description = Security::remove_XSS($item_description);
  5926. $legend = '<legend>';
  5927. if ($id != 0 && is_array($extra_info))
  5928. $parent = $extra_info['parent_item_id'];
  5929. else
  5930. $parent = 0;
  5931. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5932. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5933. $result = Database::query($sql);
  5934. $arrLP = array ();
  5935. while ($row = Database :: fetch_array($result)) {
  5936. $arrLP[] = array (
  5937. 'id' => $row['id'],
  5938. 'item_type' => $row['item_type'],
  5939. 'title' => $row['title'],
  5940. 'path' => $row['path'],
  5941. 'description' => $row['description'],
  5942. 'parent_item_id' => $row['parent_item_id'],
  5943. 'previous_item_id' => $row['previous_item_id'],
  5944. 'next_item_id' => $row['next_item_id'],
  5945. 'display_order' => $row['display_order'],
  5946. 'max_score' => $row['max_score'],
  5947. 'min_score' => $row['min_score'],
  5948. 'mastery_score' => $row['mastery_score'],
  5949. 'prerequisite' => $row['prerequisite'],
  5950. 'max_time_allowed' => $row['max_time_allowed']
  5951. );
  5952. }
  5953. $this->tree_array($arrLP);
  5954. $arrLP = $this->arrMenu;
  5955. unset ($this->arrMenu);
  5956. if ($action == 'add') {
  5957. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5958. } elseif ($action == 'move') {
  5959. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5960. } else {
  5961. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5962. }
  5963. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5964. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5965. }
  5966. $legend .= '</legend>';
  5967. $return = '';
  5968. $return .= '<div class="sectioncomment">';
  5969. $return .= '<form method="POST">';
  5970. $return .= $legend;
  5971. $return .= '<table class="lp_form">';
  5972. if ($action != 'move') {
  5973. $return .= '<tr>';
  5974. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5975. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5976. $return .= '</tr>';
  5977. }
  5978. $return .= '<tr>';
  5979. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5980. $return .= '<td class="input">';
  5981. // Select for Parent item, root or chapter
  5982. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5983. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5984. $arrHide = array (
  5985. $id
  5986. );
  5987. for ($i = 0; $i < count($arrLP); $i++) {
  5988. if ($action != 'add') {
  5989. 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)) {
  5990. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5991. } else {
  5992. $arrHide[] = $arrLP[$i]['id'];
  5993. }
  5994. } else {
  5995. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5996. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5997. }
  5998. }
  5999. if (is_array($arrLP)) {
  6000. reset($arrLP);
  6001. }
  6002. $return .= '</select>';
  6003. $return .= '</td>';
  6004. $return .= '</tr>';
  6005. $return .= '<tr>';
  6006. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6007. $return .= '<td class="input">';
  6008. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6009. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6010. for ($i = 0; $i < count($arrLP); $i++) {
  6011. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6012. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6013. $selected = 'selected="selected" ';
  6014. elseif ($action == 'add') $selected = 'selected="selected" ';
  6015. else
  6016. $selected = '';
  6017. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6018. }
  6019. }
  6020. $return .= '</select>';
  6021. $return .= '</td>';
  6022. $return .= '</tr>';
  6023. if ($action != 'move') {
  6024. $id_prerequisite = 0;
  6025. if (is_array($arrLP)) {
  6026. foreach ($arrLP as $key => $value) {
  6027. if ($value['id'] == $id) {
  6028. $id_prerequisite = $value['prerequisite'];
  6029. break;
  6030. }
  6031. }
  6032. }
  6033. $arrHide = array ();
  6034. for ($i = 0; $i < count($arrLP); $i++) {
  6035. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6036. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6037. $s_selected_position = $arrLP[$i]['id'];
  6038. elseif ($action == 'add') $s_selected_position = 0;
  6039. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6040. }
  6041. }
  6042. /*// Commented the prerequisites, only visible in edit (exercise).
  6043. $return .= '<tr>';
  6044. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6045. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6046. foreach($arrHide as $key => $value){
  6047. if($key==$s_selected_position && $action == 'add'){
  6048. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6049. }
  6050. elseif($key==$id_prerequisite && $action == 'edit'){
  6051. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6052. }
  6053. else{
  6054. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6055. }
  6056. }
  6057. $return .= "</select></td>";
  6058. */
  6059. $return .= '</tr>';
  6060. /*$return .= '<tr>';
  6061. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6062. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6063. // Remove temporarily the test description.
  6064. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6065. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6066. $return .= '</tr>'; */
  6067. }
  6068. $return .= '<tr>';
  6069. if ($action == 'add') {
  6070. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  6071. } else {
  6072. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  6073. }
  6074. $return .= '</tr>';
  6075. $return .= '</table>';
  6076. if ($action == 'move') {
  6077. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6078. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6079. }
  6080. if (is_numeric($extra_info)) {
  6081. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6082. } elseif (is_array($extra_info)) {
  6083. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6084. }
  6085. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  6086. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6087. $return .= '</form>';
  6088. $return .= '</div>';
  6089. return $return;
  6090. }
  6091. /**
  6092. * Addition of Hotpotatoes tests
  6093. * @param string Action
  6094. * @param integer Internal ID of the item
  6095. * @param mixed Extra information - can be an array with title and description indexes
  6096. * @return string HTML structure to display the hotpotatoes addition formular
  6097. */
  6098. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6099. {
  6100. $course_id = api_get_course_int_id();
  6101. global $charset;
  6102. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6103. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6104. if ($id != 0 && is_array($extra_info)) {
  6105. $item_title = stripslashes($extra_info['title']);
  6106. $item_description = stripslashes($extra_info['description']);
  6107. } elseif (is_numeric($extra_info)) {
  6108. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6109. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6110. WHERE
  6111. c_id = ".$course_id." AND
  6112. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6113. id = " . (int) $extra_info . "
  6114. ORDER BY id ASC";
  6115. $res_hot = Database::query($sql);
  6116. $row = Database::fetch_array($res_hot);
  6117. $item_title = $row['title'];
  6118. $item_description = $row['description'];
  6119. if (!empty ($row['comment'])) {
  6120. $item_title = $row['comment'];
  6121. }
  6122. } else {
  6123. $item_title = '';
  6124. $item_description = '';
  6125. }
  6126. if ($id != 0 && is_array($extra_info)) {
  6127. $parent = $extra_info['parent_item_id'];
  6128. } else {
  6129. $parent = 0;
  6130. }
  6131. $sql = "SELECT * FROM $tbl_lp_item
  6132. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6133. $result = Database::query($sql);
  6134. $arrLP = array ();
  6135. while ($row = Database :: fetch_array($result)) {
  6136. $arrLP[] = array (
  6137. 'id' => $row['id'],
  6138. 'item_type' => $row['item_type'],
  6139. 'title' => $row['title'],
  6140. 'path' => $row['path'],
  6141. 'description' => $row['description'],
  6142. 'parent_item_id' => $row['parent_item_id'],
  6143. 'previous_item_id' => $row['previous_item_id'],
  6144. 'next_item_id' => $row['next_item_id'],
  6145. 'display_order' => $row['display_order'],
  6146. 'max_score' => $row['max_score'],
  6147. 'min_score' => $row['min_score'],
  6148. 'mastery_score' => $row['mastery_score'],
  6149. 'prerequisite' => $row['prerequisite'],
  6150. 'max_time_allowed' => $row['max_time_allowed']
  6151. );
  6152. }
  6153. $legend = '<legend>';
  6154. if ($action == 'add')
  6155. $legend .= get_lang('CreateTheExercise');
  6156. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6157. else
  6158. $legend .= get_lang('EditCurrentExecice');
  6159. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6160. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6161. }
  6162. $legend .= '</legend>';
  6163. $return = '<form method="POST">';
  6164. $return .= $legend;
  6165. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6166. $return .= '<tr>';
  6167. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6168. $return .= '<td class="input">';
  6169. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6170. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6171. $arrHide = array (
  6172. $id
  6173. );
  6174. if (count($arrLP) > 0) {
  6175. for ($i = 0; $i < count($arrLP); $i++) {
  6176. if ($action != 'add') {
  6177. 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)) {
  6178. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6179. } else {
  6180. $arrHide[] = $arrLP[$i]['id'];
  6181. }
  6182. } else {
  6183. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6184. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6185. }
  6186. }
  6187. reset($arrLP);
  6188. }
  6189. $return .= '</select>';
  6190. $return .= '</td>';
  6191. $return .= '</tr>';
  6192. $return .= '<tr>';
  6193. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6194. $return .= '<td class="input">';
  6195. $return .= '<select id="previous" name="previous" size="1">';
  6196. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6197. for ($i = 0; $i < count($arrLP); $i++) {
  6198. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6199. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6200. $selected = 'selected="selected" ';
  6201. elseif ($action == 'add') $selected = 'selected="selected" ';
  6202. else
  6203. $selected = '';
  6204. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6205. }
  6206. }
  6207. $return .= '</select>';
  6208. $return .= '</td>';
  6209. $return .= '</tr>';
  6210. if ($action != 'move') {
  6211. $return .= '<tr>';
  6212. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6213. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6214. $return .= '</tr>';
  6215. $id_prerequisite = 0;
  6216. if (is_array($arrLP) && count($arrLP) > 0) {
  6217. foreach ($arrLP as $key => $value) {
  6218. if ($value['id'] == $id) {
  6219. $id_prerequisite = $value['prerequisite'];
  6220. break;
  6221. }
  6222. }
  6223. $arrHide = array ();
  6224. for ($i = 0; $i < count($arrLP); $i++) {
  6225. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6226. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6227. $s_selected_position = $arrLP[$i]['id'];
  6228. elseif ($action == 'add') $s_selected_position = 0;
  6229. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6230. }
  6231. }
  6232. }
  6233. }
  6234. $return .= '<tr>';
  6235. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6236. $return .= '</tr>';
  6237. $return .= '</table>';
  6238. if ($action == 'move') {
  6239. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6240. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6241. }
  6242. if (is_numeric($extra_info)) {
  6243. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6244. } elseif (is_array($extra_info)) {
  6245. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6246. }
  6247. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6248. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6249. $return .= '</form>';
  6250. return $return;
  6251. }
  6252. /**
  6253. * Return the form to display the forum edit/add option
  6254. * @param string Action (add/edit)
  6255. * @param integer ID of the lp_item if already exists
  6256. * @param mixed Forum ID or title
  6257. * @return string HTML form
  6258. */
  6259. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6260. {
  6261. $course_id = api_get_course_int_id();
  6262. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6263. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6264. if ($id != 0 && is_array($extra_info)) {
  6265. $item_title = stripslashes($extra_info['title']);
  6266. } elseif (is_numeric($extra_info)) {
  6267. $sql = "SELECT forum_title as title, forum_comment as comment
  6268. FROM " . $tbl_forum . "
  6269. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6270. $result = Database::query($sql);
  6271. $row = Database :: fetch_array($result);
  6272. $item_title = $row['title'];
  6273. $item_description = $row['comment'];
  6274. } else {
  6275. $item_title = '';
  6276. $item_description = '';
  6277. }
  6278. $legend = '<legend>';
  6279. if ($id != 0 && is_array($extra_info)) {
  6280. $parent = $extra_info['parent_item_id'];
  6281. } else {
  6282. $parent = 0;
  6283. }
  6284. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6285. WHERE
  6286. c_id = ".$course_id." AND
  6287. lp_id = " . $this->lp_id;
  6288. $result = Database::query($sql);
  6289. $arrLP = array();
  6290. while ($row = Database :: fetch_array($result)) {
  6291. $arrLP[] = array (
  6292. 'id' => $row['id'],
  6293. 'item_type' => $row['item_type'],
  6294. 'title' => $row['title'],
  6295. 'path' => $row['path'],
  6296. 'description' => $row['description'],
  6297. 'parent_item_id' => $row['parent_item_id'],
  6298. 'previous_item_id' => $row['previous_item_id'],
  6299. 'next_item_id' => $row['next_item_id'],
  6300. 'display_order' => $row['display_order'],
  6301. 'max_score' => $row['max_score'],
  6302. 'min_score' => $row['min_score'],
  6303. 'mastery_score' => $row['mastery_score'],
  6304. 'prerequisite' => $row['prerequisite']
  6305. );
  6306. }
  6307. $this->tree_array($arrLP);
  6308. $arrLP = $this->arrMenu;
  6309. unset($this->arrMenu);
  6310. if ($action == 'add')
  6311. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6312. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6313. else
  6314. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6315. $legend .= '</legend>';
  6316. $return = '<div class="sectioncomment">';
  6317. $return .= '<form method="POST">';
  6318. $return .= $legend;
  6319. $return .= '<table class="lp_form">';
  6320. if ($action != 'move') {
  6321. $return .= '<tr>';
  6322. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6323. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6324. $return .= '</tr>';
  6325. }
  6326. $return .= '<tr>';
  6327. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6328. $return .= '<td class="input">';
  6329. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6330. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6331. $arrHide = array(
  6332. $id
  6333. );
  6334. //$parent_item_id = $_SESSION['parent_item_id'];
  6335. for ($i = 0; $i < count($arrLP); $i++) {
  6336. if ($action != 'add') {
  6337. 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)) {
  6338. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6339. } else {
  6340. $arrHide[] = $arrLP[$i]['id'];
  6341. }
  6342. } else {
  6343. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6344. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6345. }
  6346. }
  6347. if (is_array($arrLP)) {
  6348. reset($arrLP);
  6349. }
  6350. $return .= '</select>';
  6351. $return .= '</td>';
  6352. $return .= '</tr>';
  6353. $return .= '<tr>';
  6354. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6355. $return .= '<td class="input">';
  6356. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6357. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6358. for ($i = 0; $i < count($arrLP); $i++) {
  6359. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6360. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6361. $selected = 'selected="selected" ';
  6362. elseif ($action == 'add') $selected = 'selected="selected" ';
  6363. else
  6364. $selected = '';
  6365. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6366. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6367. }
  6368. }
  6369. $return .= '</select>';
  6370. $return .= '</td>';
  6371. $return .= '</tr>';
  6372. if ($action != 'move') {
  6373. $return .= '<tr>';
  6374. $return .= '</tr>';
  6375. $id_prerequisite = 0;
  6376. if (is_array($arrLP)) {
  6377. foreach ($arrLP as $key => $value) {
  6378. if ($value['id'] == $id) {
  6379. $id_prerequisite = $value['prerequisite'];
  6380. break;
  6381. }
  6382. }
  6383. }
  6384. $arrHide = array();
  6385. for ($i = 0; $i < count($arrLP); $i++) {
  6386. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6387. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6388. $s_selected_position = $arrLP[$i]['id'];
  6389. elseif ($action == 'add') $s_selected_position = 0;
  6390. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6391. }
  6392. }
  6393. $return .= '</tr>';
  6394. }
  6395. $return .= '<tr>';
  6396. if ($action == 'add') {
  6397. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6398. } else {
  6399. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6400. }
  6401. $return .= '</tr>';
  6402. $return .= '</table>';
  6403. if ($action == 'move') {
  6404. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6405. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6406. }
  6407. if (is_numeric($extra_info)) {
  6408. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6409. } elseif (is_array($extra_info)) {
  6410. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6411. }
  6412. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6413. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6414. $return .= '</form>';
  6415. $return .= '</div>';
  6416. return $return;
  6417. }
  6418. /**
  6419. * Return HTML form to add/edit forum threads
  6420. * @param string Action (add/edit)
  6421. * @param integer Item ID if already exists in learning path
  6422. * @param mixed Extra information (thread ID if integer)
  6423. * @return string HTML form
  6424. */
  6425. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6426. {
  6427. $course_id = api_get_course_int_id();
  6428. if (empty($course_id)) {
  6429. return null;
  6430. }
  6431. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6432. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6433. if ($id != 0 && is_array($extra_info)) {
  6434. $item_title = stripslashes($extra_info['title']);
  6435. } elseif (is_numeric($extra_info)) {
  6436. $sql = "SELECT thread_title as title FROM $tbl_forum
  6437. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6438. $result = Database::query($sql);
  6439. $row = Database :: fetch_array($result);
  6440. $item_title = $row['title'];
  6441. $item_description = '';
  6442. } else {
  6443. $item_title = '';
  6444. $item_description = '';
  6445. }
  6446. $return = null;
  6447. if ($id != 0 && is_array($extra_info)) {
  6448. $parent = $extra_info['parent_item_id'];
  6449. } else {
  6450. $parent = 0;
  6451. }
  6452. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6453. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6454. $result = Database::query($sql);
  6455. $arrLP = array ();
  6456. while ($row = Database :: fetch_array($result)) {
  6457. $arrLP[] = array (
  6458. 'id' => $row['id'],
  6459. 'item_type' => $row['item_type'],
  6460. 'title' => $row['title'],
  6461. 'path' => $row['path'],
  6462. 'description' => $row['description'],
  6463. 'parent_item_id' => $row['parent_item_id'],
  6464. 'previous_item_id' => $row['previous_item_id'],
  6465. 'next_item_id' => $row['next_item_id'],
  6466. 'display_order' => $row['display_order'],
  6467. 'max_score' => $row['max_score'],
  6468. 'min_score' => $row['min_score'],
  6469. 'mastery_score' => $row['mastery_score'],
  6470. 'prerequisite' => $row['prerequisite']
  6471. );
  6472. }
  6473. $this->tree_array($arrLP);
  6474. $arrLP = $this->arrMenu;
  6475. unset ($this->arrMenu);
  6476. $return .= '<form method="POST">';
  6477. if ($action == 'add')
  6478. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6479. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6480. else
  6481. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6482. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6483. $return .= '<tr>';
  6484. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6485. $return .= '<td class="input">';
  6486. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6487. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6488. $arrHide = array (
  6489. $id
  6490. );
  6491. for ($i = 0; $i < count($arrLP); $i++) {
  6492. if ($action != 'add') {
  6493. 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)) {
  6494. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6495. } else {
  6496. $arrHide[] = $arrLP[$i]['id'];
  6497. }
  6498. } else {
  6499. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6500. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6501. }
  6502. }
  6503. if ($arrLP != null) {
  6504. reset($arrLP);
  6505. }
  6506. $return .= '</select>';
  6507. $return .= '</td>';
  6508. $return .= '</tr>';
  6509. $return .= '<tr>';
  6510. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6511. $return .= '<td class="input">';
  6512. $return .= '<select id="previous" name="previous" size="1">';
  6513. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6514. for ($i = 0; $i < count($arrLP); $i++) {
  6515. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6516. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6517. $selected = 'selected="selected" ';
  6518. elseif ($action == 'add') $selected = 'selected="selected" ';
  6519. else
  6520. $selected = '';
  6521. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6522. }
  6523. }
  6524. $return .= '</select>';
  6525. $return .= '</td>';
  6526. $return .= '</tr>';
  6527. if ($action != 'move') {
  6528. $return .= '<tr>';
  6529. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6530. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6531. $return .= '</tr>';
  6532. $return .= '<tr>';
  6533. $return .= '</tr>';
  6534. $id_prerequisite = 0;
  6535. if ($arrLP != null) {
  6536. foreach ($arrLP as $key => $value) {
  6537. if ($value['id'] == $id) {
  6538. $id_prerequisite = $value['prerequisite'];
  6539. break;
  6540. }
  6541. }
  6542. }
  6543. $arrHide = array();
  6544. for ($i = 0; $i < count($arrLP); $i++) {
  6545. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6546. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6547. $s_selected_position = $arrLP[$i]['id'];
  6548. elseif ($action == 'add') $s_selected_position = 0;
  6549. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6550. }
  6551. }
  6552. $return .= '<tr>';
  6553. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6554. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6555. foreach ($arrHide as $key => $value) {
  6556. if ($key == $s_selected_position && $action == 'add') {
  6557. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6558. }
  6559. elseif ($key == $id_prerequisite && $action == 'edit') {
  6560. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6561. } else {
  6562. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6563. }
  6564. }
  6565. $return .= "</select></td>";
  6566. $return .= '</tr>';
  6567. }
  6568. $return .= '<tr>';
  6569. $return .= '<td></td><td>
  6570. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6571. $return .= '</tr>';
  6572. $return .= '</table>';
  6573. if ($action == 'move') {
  6574. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6575. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6576. }
  6577. if (is_numeric($extra_info)) {
  6578. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6579. }
  6580. elseif (is_array($extra_info)) {
  6581. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6582. }
  6583. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6584. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6585. $return .= '</form>';
  6586. $return .= '</div>';
  6587. return $return;
  6588. }
  6589. /**
  6590. * Return the HTML form to display an item (generally a section/module item)
  6591. * @param string Item type (module/dokeos_module)
  6592. * @param string Title (optional, only when creating)
  6593. * @param string Action ('add'/'edit')
  6594. * @param integer lp_item ID
  6595. * @param mixed Extra info
  6596. * @return string HTML form
  6597. */
  6598. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6599. {
  6600. $course_id = api_get_course_int_id();
  6601. $_course = api_get_course_info();
  6602. global $charset;
  6603. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6604. if ($id != 0 && is_array($extra_info)) {
  6605. $item_title = $extra_info['title'];
  6606. $item_description = $extra_info['description'];
  6607. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6608. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6609. } else {
  6610. $item_title = '';
  6611. $item_description = '';
  6612. $item_path_fck = '';
  6613. }
  6614. if ($id != 0 && is_array($extra_info)) {
  6615. $parent = $extra_info['parent_item_id'];
  6616. } else {
  6617. $parent = 0;
  6618. }
  6619. $id = intval($id);
  6620. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6621. WHERE
  6622. c_id = ".$course_id." AND
  6623. lp_id = " . $this->lp_id . " AND
  6624. id != $id";
  6625. if ($item_type == 'module')
  6626. $sql .= " AND parent_item_id = 0";
  6627. $result = Database::query($sql);
  6628. $arrLP = array ();
  6629. while ($row = Database :: fetch_array($result)) {
  6630. $arrLP[] = array(
  6631. 'id' => $row['id'],
  6632. 'item_type' => $row['item_type'],
  6633. 'title' => $row['title'],
  6634. 'path' => $row['path'],
  6635. 'description' => $row['description'],
  6636. 'parent_item_id' => $row['parent_item_id'],
  6637. 'previous_item_id' => $row['previous_item_id'],
  6638. 'next_item_id' => $row['next_item_id'],
  6639. 'max_score' => $row['max_score'],
  6640. 'min_score' => $row['min_score'],
  6641. 'mastery_score' => $row['mastery_score'],
  6642. 'prerequisite' => $row['prerequisite'],
  6643. 'display_order' => $row['display_order']
  6644. );
  6645. }
  6646. $this->tree_array($arrLP);
  6647. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6648. unset ($this->arrMenu);
  6649. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6650. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6651. $form = new FormValidator('form', 'POST', $url);
  6652. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6653. $defaults['description'] = $item_description;
  6654. $form->addElement('header', $title);
  6655. //$arrHide = array($id);
  6656. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6657. $arrHide[0]['padding'] = 3;
  6658. $charset = api_get_system_encoding();
  6659. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6660. for ($i = 0; $i < count($arrLP); $i++) {
  6661. if ($action != 'add') {
  6662. 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)) {
  6663. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6664. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6665. if ($parent == $arrLP[$i]['id']) {
  6666. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6667. }
  6668. }
  6669. } else {
  6670. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6671. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6672. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6673. if ($parent == $arrLP[$i]['id']) {
  6674. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6675. }
  6676. }
  6677. }
  6678. }
  6679. if ($action != 'move') {
  6680. $form->addElement('text', 'title', get_lang('Title'));
  6681. $form->applyFilter('title', 'html_filter');
  6682. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6683. } else {
  6684. $form->addElement('hidden', 'title');
  6685. }
  6686. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6687. foreach ($arrHide as $key => $value) {
  6688. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6689. }
  6690. if (!empty($s_selected_parent)) {
  6691. $parent_select->setSelected($s_selected_parent);
  6692. }
  6693. }
  6694. if (is_array($arrLP)) {
  6695. reset($arrLP);
  6696. }
  6697. $arrHide = array();
  6698. // POSITION
  6699. for ($i = 0; $i < count($arrLP); $i++) {
  6700. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6701. //this is the same!
  6702. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6703. $s_selected_position = $arrLP[$i]['id'];
  6704. } elseif ($action == 'add') {
  6705. $s_selected_position = $arrLP[$i]['id'];
  6706. }
  6707. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6708. }
  6709. }
  6710. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6711. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6712. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6713. foreach ($arrHide as $key => $value) {
  6714. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6715. }
  6716. if (!empty ($s_selected_position)) {
  6717. $position->setSelected($s_selected_position);
  6718. }
  6719. if (is_array($arrLP)) {
  6720. reset($arrLP);
  6721. }
  6722. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6723. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6724. $form->addElement('hidden', 'parent', '0');
  6725. }
  6726. //fix in order to use the tab
  6727. if ($item_type == 'chapter') {
  6728. $form->addElement('hidden', 'type', 'chapter');
  6729. }
  6730. $extension = null;
  6731. if (!empty($item_path)) {
  6732. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6733. }
  6734. //assets can't be modified
  6735. //$item_type == 'asset' ||
  6736. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6737. if ($item_type == 'sco') {
  6738. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6739. }
  6740. $renderer = $form->defaultRenderer();
  6741. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6742. $relative_prefix = '';
  6743. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6744. 'Width' => '100%',
  6745. 'Height' => '500',
  6746. 'FullPage' => true,
  6747. 'CreateDocumentDir' => $relative_prefix,
  6748. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6749. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6750. );
  6751. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6752. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6753. //$defaults['content_lp'] = file_get_contents($item_path);
  6754. $defaults['content_lp'] = file_get_contents($content_path);
  6755. }
  6756. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6757. $form->addElement('hidden', 'post_time', time());
  6758. $form->setDefaults($defaults);
  6759. return $form->return_form();
  6760. }
  6761. /**
  6762. * Returns the form to update or create a document
  6763. * @param string Action (add/edit)
  6764. * @param integer ID of the lp_item (if already exists)
  6765. * @param mixed Integer if document ID, string if info ('new')
  6766. * @return string HTML form
  6767. */
  6768. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6769. {
  6770. $course_id = api_get_course_int_id();
  6771. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6772. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6773. $no_display_edit_textarea = false;
  6774. //If action==edit document
  6775. //We don't display the document form if it's not an editable document (html or txt file)
  6776. if ($action == "edit") {
  6777. if (is_array($extra_info)) {
  6778. $path_parts = pathinfo($extra_info['dir']);
  6779. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6780. $no_display_edit_textarea = true;
  6781. }
  6782. }
  6783. }
  6784. $no_display_add = false;
  6785. // If action==add an existing document
  6786. // We don't display the document form if it's not an editable document (html or txt file).
  6787. if ($action == "add") {
  6788. if (is_numeric($extra_info)) {
  6789. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6790. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6791. $result = Database::query($sql_doc);
  6792. $path_file = Database :: result($result, 0, 0);
  6793. $path_parts = pathinfo($path_file);
  6794. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6795. $no_display_add = true;
  6796. }
  6797. }
  6798. }
  6799. if ($id != 0 && is_array($extra_info)) {
  6800. $item_title = stripslashes($extra_info['title']);
  6801. $item_description = stripslashes($extra_info['description']);
  6802. $item_terms = stripslashes($extra_info['terms']);
  6803. if (empty ($item_title)) {
  6804. $path_parts = pathinfo($extra_info['path']);
  6805. $item_title = stripslashes($path_parts['filename']);
  6806. }
  6807. } elseif (is_numeric($extra_info)) {
  6808. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6809. WHERE
  6810. c_id = ".$course_id." AND
  6811. id = " . intval($extra_info);
  6812. $result = Database::query($sql_doc);
  6813. $row = Database::fetch_array($result);
  6814. $item_title = $row['title'];
  6815. $item_title = str_replace('_', ' ', $item_title);
  6816. if (empty ($item_title)) {
  6817. $path_parts = pathinfo($row['path']);
  6818. $item_title = stripslashes($path_parts['filename']);
  6819. }
  6820. } else {
  6821. $item_title = '';
  6822. $item_description = '';
  6823. }
  6824. $return = '<legend>';
  6825. if ($id != 0 && is_array($extra_info)) {
  6826. $parent = $extra_info['parent_item_id'];
  6827. } else {
  6828. $parent = 0;
  6829. }
  6830. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6831. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6832. $result = Database::query($sql);
  6833. $arrLP = array ();
  6834. while ($row = Database :: fetch_array($result)) {
  6835. $arrLP[] = array(
  6836. 'id' => $row['id'],
  6837. 'item_type' => $row['item_type'],
  6838. 'title' => $row['title'],
  6839. 'path' => $row['path'],
  6840. 'description' => $row['description'],
  6841. 'parent_item_id' => $row['parent_item_id'],
  6842. 'previous_item_id' => $row['previous_item_id'],
  6843. 'next_item_id' => $row['next_item_id'],
  6844. 'display_order' => $row['display_order'],
  6845. 'max_score' => $row['max_score'],
  6846. 'min_score' => $row['min_score'],
  6847. 'mastery_score' => $row['mastery_score'],
  6848. 'prerequisite' => $row['prerequisite']
  6849. );
  6850. }
  6851. $this->tree_array($arrLP);
  6852. $arrLP = $this->arrMenu;
  6853. unset ($this->arrMenu);
  6854. if ($action == 'add') {
  6855. $return .= get_lang('CreateTheDocument');
  6856. } elseif ($action == 'move') {
  6857. $return .= get_lang('MoveTheCurrentDocument');
  6858. } else {
  6859. $return .= get_lang('EditTheCurrentDocument');
  6860. }
  6861. $return .= '</legend>';
  6862. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6863. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6864. }
  6865. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6866. $defaults['title'] = Security :: remove_XSS($item_title);
  6867. if (empty($item_title)) {
  6868. $defaults['title'] = Security::remove_XSS($item_title);
  6869. }
  6870. $defaults['description'] = $item_description;
  6871. $form->addElement('html', $return);
  6872. if ($action != 'move') {
  6873. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6874. $form->applyFilter('title', 'html_filter');
  6875. }
  6876. $arrHide[0]['value'] = $this->name;
  6877. $arrHide[0]['padding'] = 3;
  6878. for ($i = 0; $i < count($arrLP); $i++) {
  6879. if ($action != 'add') {
  6880. 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)) {
  6881. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6882. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6883. if ($parent == $arrLP[$i]['id']) {
  6884. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6885. }
  6886. }
  6887. } else {
  6888. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6889. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6890. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6891. if ($parent == $arrLP[$i]['id']) {
  6892. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6893. }
  6894. }
  6895. }
  6896. }
  6897. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6898. $my_count=0;
  6899. foreach ($arrHide as $key => $value) {
  6900. if ($my_count!=0) {
  6901. // The LP name is also the first section and is not in the same charset like the other sections.
  6902. $value['value'] = Security :: remove_XSS($value['value']);
  6903. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6904. } else {
  6905. $value['value'] = Security :: remove_XSS($value['value']);
  6906. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6907. }
  6908. $my_count++;
  6909. }
  6910. if (!empty($id)) {
  6911. $parent_select->setSelected($parent);
  6912. } else {
  6913. $parent_item_id = $_SESSION['parent_item_id'];
  6914. $parent_select->setSelected($parent_item_id);
  6915. }
  6916. if (is_array($arrLP)) {
  6917. reset($arrLP);
  6918. }
  6919. $arrHide = array();
  6920. $s_selected_position = null;
  6921. //POSITION
  6922. for ($i = 0; $i < count($arrLP); $i++) {
  6923. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6924. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6925. $s_selected_position = $arrLP[$i]['id'];
  6926. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6927. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6928. }
  6929. }
  6930. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6931. $position->addOption(get_lang('FirstPosition'), 0);
  6932. foreach ($arrHide as $key => $value) {
  6933. $padding = isset($value['padding']) ? $value['padding']: 0;
  6934. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6935. }
  6936. $position->setSelected($s_selected_position);
  6937. if (is_array($arrLP)) {
  6938. reset($arrLP);
  6939. }
  6940. if ($action != 'move') {
  6941. $id_prerequisite = 0;
  6942. if (is_array($arrLP)) {
  6943. foreach ($arrLP as $key => $value) {
  6944. if ($value['id'] == $id) {
  6945. $id_prerequisite = $value['prerequisite'];
  6946. break;
  6947. }
  6948. }
  6949. }
  6950. $arrHide = array();
  6951. for ($i = 0; $i < count($arrLP); $i++) {
  6952. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6953. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6954. $s_selected_position = $arrLP[$i]['id'];
  6955. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6956. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6957. }
  6958. }
  6959. if (!$no_display_add) {
  6960. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6961. if (isset ($_POST['content']))
  6962. $content = stripslashes($_POST['content']);
  6963. elseif (is_array($extra_info)) {
  6964. //If it's an html document or a text file
  6965. if (!$no_display_edit_textarea) {
  6966. $content = $this->display_document($extra_info['path'], false, false);
  6967. }
  6968. } elseif (is_numeric($extra_info))
  6969. $content = $this->display_document($extra_info, false, false);
  6970. else
  6971. $content = '';
  6972. if (!$no_display_edit_textarea) {
  6973. // We need to calculate here some specific settings for the online editor.
  6974. // The calculated settings work for documents in the Documents tool
  6975. // (on the root or in subfolders).
  6976. // For documents in native scorm packages it is unclear whether the
  6977. // online editor should be activated or not.
  6978. // A new document, it is in the root of the repository.
  6979. $relative_path = '';
  6980. $relative_prefix = '';
  6981. if (is_array($extra_info) && $extra_info != 'new') {
  6982. // The document already exists. Whe have to determine its relative path towards the repository root.
  6983. $relative_path = explode('/', $extra_info['dir']);
  6984. $cnt = count($relative_path) - 2;
  6985. if ($cnt < 0) {
  6986. $cnt = 0;
  6987. }
  6988. $relative_prefix = str_repeat('../', $cnt);
  6989. $relative_path = array_slice($relative_path, 1, $cnt);
  6990. $relative_path = implode('/', $relative_path);
  6991. if (strlen($relative_path) > 0) {
  6992. $relative_path = $relative_path . '/';
  6993. }
  6994. } else {
  6995. global $_course;
  6996. $result = $this->generate_lp_folder($_course);
  6997. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6998. $relative_prefix = '../../';
  6999. }
  7000. $editor_config = array(
  7001. 'ToolbarSet'=> 'LearningPathDocuments',
  7002. 'Width' => '100%',
  7003. 'Height' => '500',
  7004. 'FullPage' => true,
  7005. 'CreateDocumentDir' => $relative_prefix,
  7006. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  7007. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  7008. );
  7009. if ($_GET['action'] == 'add_item') {
  7010. $class = 'add';
  7011. $text = get_lang('LPCreateDocument');
  7012. } else {
  7013. if ($_GET['action'] == 'edit_item') {
  7014. $class = 'save';
  7015. $text = get_lang('SaveDocument');
  7016. }
  7017. }
  7018. $form->addButtonSave($text, 'submit_button');
  7019. $renderer = $form->defaultRenderer();
  7020. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7021. $form->addElement('html', '<div>');
  7022. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7023. $form->addElement('html', '</div>');
  7024. $defaults['content_lp'] = $content;
  7025. }
  7026. } elseif (is_numeric($extra_info)) {
  7027. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7028. $return = $this->display_document($extra_info, true, true, true);
  7029. $form->addElement('html', $return);
  7030. }
  7031. }
  7032. }
  7033. if ($action == 'move') {
  7034. $form->addElement('hidden', 'title', $item_title);
  7035. $form->addElement('hidden', 'description', $item_description);
  7036. }
  7037. if (is_numeric($extra_info)) {
  7038. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7039. $form->addElement('hidden', 'path', $extra_info);
  7040. } elseif (is_array($extra_info)) {
  7041. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7042. $form->addElement('hidden', 'path', $extra_info['path']);
  7043. }
  7044. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7045. $form->addElement('hidden', 'post_time', time());
  7046. $form->setDefaults($defaults);
  7047. return $form->return_form();
  7048. }
  7049. /**
  7050. * Return HTML form to add/edit a link item
  7051. * @param string $action (add/edit)
  7052. * @param integer $id Item ID if exists
  7053. * @param mixed $extra_info
  7054. * @return string HTML form
  7055. */
  7056. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7057. {
  7058. $course_id = api_get_course_int_id();
  7059. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7060. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7061. if ($id != 0 && is_array($extra_info)) {
  7062. $item_title = stripslashes($extra_info['title']);
  7063. $item_description = stripslashes($extra_info['description']);
  7064. $item_url = stripslashes($extra_info['url']);
  7065. } elseif (is_numeric($extra_info)) {
  7066. $extra_info = intval($extra_info);
  7067. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7068. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7069. $result = Database::query($sql);
  7070. $row = Database :: fetch_array($result);
  7071. $item_title = $row['title'];
  7072. $item_description = $row['description'];
  7073. $item_url = $row['url'];
  7074. } else {
  7075. $item_title = '';
  7076. $item_description = '';
  7077. $item_url = '';
  7078. }
  7079. $legend = '<legend>';
  7080. if ($id != 0 && is_array($extra_info)) {
  7081. $parent = $extra_info['parent_item_id'];
  7082. } else {
  7083. $parent = 0;
  7084. }
  7085. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7086. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7087. $result = Database::query($sql);
  7088. $arrLP = array();
  7089. while ($row = Database :: fetch_array($result)) {
  7090. $arrLP[] = array(
  7091. 'id' => $row['id'],
  7092. 'item_type' => $row['item_type'],
  7093. 'title' => $row['title'],
  7094. 'path' => $row['path'],
  7095. 'description' => $row['description'],
  7096. 'parent_item_id' => $row['parent_item_id'],
  7097. 'previous_item_id' => $row['previous_item_id'],
  7098. 'next_item_id' => $row['next_item_id'],
  7099. 'display_order' => $row['display_order'],
  7100. 'max_score' => $row['max_score'],
  7101. 'min_score' => $row['min_score'],
  7102. 'mastery_score' => $row['mastery_score'],
  7103. 'prerequisite' => $row['prerequisite']
  7104. );
  7105. }
  7106. $this->tree_array($arrLP);
  7107. $arrLP = $this->arrMenu;
  7108. unset ($this->arrMenu);
  7109. if ($action == 'add')
  7110. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  7111. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  7112. else
  7113. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7114. $legend .= '</legend>';
  7115. $return = '<div class="sectioncomment">';
  7116. $return .= '<form method="POST">';
  7117. $return .= $legend;
  7118. $return .= '<table>';
  7119. if ($action != 'move') {
  7120. $return .= '<tr>';
  7121. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7122. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7123. $return .= '</tr>';
  7124. }
  7125. $return .= '<tr>';
  7126. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7127. $return .= '<td class="input">';
  7128. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7129. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7130. $arrHide = array(
  7131. $id
  7132. );
  7133. $parent_item_id = $_SESSION['parent_item_id'];
  7134. for ($i = 0; $i < count($arrLP); $i++) {
  7135. if ($action != 'add') {
  7136. 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)) {
  7137. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7138. } else {
  7139. $arrHide[] = $arrLP[$i]['id'];
  7140. }
  7141. } else {
  7142. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7143. $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>';
  7144. }
  7145. }
  7146. if (is_array($arrLP)) {
  7147. reset($arrLP);
  7148. }
  7149. $return .= '</select>';
  7150. $return .= '</td>';
  7151. $return .= '</tr>';
  7152. $return .= '<tr>';
  7153. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7154. $return .= '<td class="input">';
  7155. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7156. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7157. for ($i = 0; $i < count($arrLP); $i++) {
  7158. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7159. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7160. $selected = 'selected="selected" ';
  7161. elseif ($action == 'add')
  7162. $selected = 'selected="selected" ';
  7163. else
  7164. $selected = '';
  7165. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7166. }
  7167. }
  7168. $return .= '</select>';
  7169. $return .= '</td>';
  7170. $return .= '</tr>';
  7171. if ($action != 'move') {
  7172. $return .= '<tr>';
  7173. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7174. $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>';
  7175. $return .= '</tr>';
  7176. $id_prerequisite = 0;
  7177. if (is_array($arrLP)) {
  7178. foreach ($arrLP as $key => $value) {
  7179. if ($value['id'] == $id) {
  7180. $id_prerequisite = $value['prerequisite'];
  7181. break;
  7182. }
  7183. }
  7184. }
  7185. $arrHide = array();
  7186. for ($i = 0; $i < count($arrLP); $i++) {
  7187. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7188. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7189. $s_selected_position = $arrLP[$i]['id'];
  7190. elseif ($action == 'add') $s_selected_position = 0;
  7191. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7192. }
  7193. }
  7194. $return .= '</tr>';
  7195. }
  7196. $return .= '<tr>';
  7197. if ($action == 'add') {
  7198. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7199. } else {
  7200. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7201. }
  7202. $return .= '</tr>';
  7203. $return .= '</table>';
  7204. if ($action == 'move') {
  7205. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7206. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7207. }
  7208. if (is_numeric($extra_info)) {
  7209. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7210. } elseif (is_array($extra_info)) {
  7211. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7212. }
  7213. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7214. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7215. $return .= '</form>';
  7216. $return .= '</div>';
  7217. return $return;
  7218. }
  7219. /**
  7220. * Return HTML form to add/edit a student publication (work)
  7221. * @param string Action (add/edit)
  7222. * @param integer Item ID if already exists
  7223. * @param mixed Extra info (work ID if integer)
  7224. * @return string HTML form
  7225. */
  7226. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7227. {
  7228. $course_id = api_get_course_int_id();
  7229. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7230. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7231. if ($id != 0 && is_array($extra_info)) {
  7232. $item_title = stripslashes($extra_info['title']);
  7233. $item_description = stripslashes($extra_info['description']);
  7234. } elseif (is_numeric($extra_info)) {
  7235. $extra_info = intval($extra_info);
  7236. $sql = "SELECT title, description
  7237. FROM " . $tbl_publication . "
  7238. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7239. $result = Database::query($sql);
  7240. $row = Database :: fetch_array($result);
  7241. $item_title = $row['title'];
  7242. } else {
  7243. $item_title = get_lang('Student_publication');
  7244. }
  7245. $legend = '<legend>';
  7246. if ($id != 0 && is_array($extra_info)) {
  7247. $parent = $extra_info['parent_item_id'];
  7248. } else {
  7249. $parent = 0;
  7250. }
  7251. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7252. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7253. $result = Database::query($sql);
  7254. $arrLP = array();
  7255. while ($row = Database :: fetch_array($result)) {
  7256. $arrLP[] = array (
  7257. 'id' => $row['id'],
  7258. 'item_type' => $row['item_type'],
  7259. 'title' => $row['title'],
  7260. 'path' => $row['path'],
  7261. 'description' => $row['description'],
  7262. 'parent_item_id' => $row['parent_item_id'],
  7263. 'previous_item_id' => $row['previous_item_id'],
  7264. 'next_item_id' => $row['next_item_id'],
  7265. 'display_order' => $row['display_order'],
  7266. 'max_score' => $row['max_score'],
  7267. 'min_score' => $row['min_score'],
  7268. 'mastery_score' => $row['mastery_score'],
  7269. 'prerequisite' => $row['prerequisite']
  7270. );
  7271. }
  7272. $this->tree_array($arrLP);
  7273. $arrLP = $this->arrMenu;
  7274. unset ($this->arrMenu);
  7275. if ($action == 'add') {
  7276. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7277. } elseif ($action == 'move') {
  7278. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7279. } else {
  7280. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7281. }
  7282. $legend .= '</legend>';
  7283. $return = '<div class="sectioncomment">';
  7284. $return .= '<form method="POST">';
  7285. $return .= $legend;
  7286. $return .= '<table class="lp_form">';
  7287. if ($action != 'move') {
  7288. $return .= '<tr>';
  7289. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7290. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7291. $return .= '</tr>';
  7292. }
  7293. $return .= '<tr>';
  7294. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7295. $return .= '<td class="input">';
  7296. $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">';
  7297. //$parent_item_id = $_SESSION['parent_item_id'];
  7298. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7299. $arrHide = array (
  7300. $id
  7301. );
  7302. for ($i = 0; $i < count($arrLP); $i++) {
  7303. if ($action != 'add') {
  7304. 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)) {
  7305. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7306. } else {
  7307. $arrHide[] = $arrLP[$i]['id'];
  7308. }
  7309. } else {
  7310. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7311. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7312. }
  7313. }
  7314. if (is_array($arrLP)) {
  7315. reset($arrLP);
  7316. }
  7317. $return .= '</select>';
  7318. $return .= '</td>';
  7319. $return .= '</tr>';
  7320. $return .= '<tr>';
  7321. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7322. $return .= '<td class="input">';
  7323. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7324. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7325. for ($i = 0; $i < count($arrLP); $i++) {
  7326. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7327. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7328. $selected = 'selected="selected" ';
  7329. elseif ($action == 'add') $selected = 'selected="selected" ';
  7330. else
  7331. $selected = '';
  7332. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7333. }
  7334. }
  7335. $return .= '</select>';
  7336. $return .= '</td>';
  7337. $return .= '</tr>';
  7338. if ($action != 'move') {
  7339. $id_prerequisite = 0;
  7340. if (is_array($arrLP)) {
  7341. foreach ($arrLP as $key => $value) {
  7342. if ($value['id'] == $id) {
  7343. $id_prerequisite = $value['prerequisite'];
  7344. break;
  7345. }
  7346. }
  7347. }
  7348. $arrHide = array ();
  7349. for ($i = 0; $i < count($arrLP); $i++) {
  7350. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7351. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7352. $s_selected_position = $arrLP[$i]['id'];
  7353. elseif ($action == 'add') $s_selected_position = 0;
  7354. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7355. }
  7356. }
  7357. // Commented the prerequisites, only visible in edit (work).
  7358. /*
  7359. $return .= '<tr>';
  7360. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7361. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7362. foreach($arrHide as $key => $value) {
  7363. if ($key == $s_selected_position && $action == 'add') {
  7364. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7365. }
  7366. elseif ($key == $id_prerequisite && $action == 'edit') {
  7367. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7368. }
  7369. else {
  7370. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7371. }
  7372. }
  7373. $return .= "</select></td>";
  7374. */
  7375. $return .= '</tr>';
  7376. }
  7377. $return .= '<tr>';
  7378. if ($action == 'add') {
  7379. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7380. } else {
  7381. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7382. }
  7383. $return .= '</tr>';
  7384. $return .= '</table>';
  7385. if ($action == 'move') {
  7386. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7387. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7388. }
  7389. if (is_numeric($extra_info)) {
  7390. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7391. } elseif (is_array($extra_info)) {
  7392. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7393. }
  7394. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7395. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7396. $return .= '</form>';
  7397. $return .= '</div>';
  7398. return $return;
  7399. }
  7400. /**
  7401. * Displays the menu for manipulating a step
  7402. *
  7403. * @param $item_id
  7404. * @param string $item_type
  7405. * @return string
  7406. */
  7407. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7408. {
  7409. $_course = api_get_course_info();
  7410. $course_id = api_get_course_int_id();
  7411. $course_code = api_get_course_id();
  7412. $return = '<div class="actions">';
  7413. switch ($item_type) {
  7414. case 'dokeos_chapter' :
  7415. case 'chapter' :
  7416. // Commented the message cause should not show it.
  7417. //$lang = get_lang('TitleManipulateChapter');
  7418. break;
  7419. case 'dokeos_module' :
  7420. case 'module' :
  7421. // Commented the message cause should not show it.
  7422. //$lang = get_lang('TitleManipulateModule');
  7423. break;
  7424. case TOOL_DOCUMENT :
  7425. // Commented the message cause should not show it.
  7426. //$lang = get_lang('TitleManipulateDocument');
  7427. break;
  7428. case TOOL_LINK :
  7429. case 'link' :
  7430. // Commented the message cause should not show it.
  7431. //$lang = get_lang('TitleManipulateLink');
  7432. break;
  7433. case TOOL_QUIZ :
  7434. // Commented the message cause should not show it.
  7435. //$lang = get_lang('TitleManipulateQuiz');
  7436. break;
  7437. case TOOL_STUDENTPUBLICATION :
  7438. // Commented the message cause should not show it.
  7439. //$lang = get_lang('TitleManipulateStudentPublication');
  7440. break;
  7441. }
  7442. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7443. $item_id = intval($item_id);
  7444. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7445. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7446. $result = Database::query($sql);
  7447. $row = Database::fetch_assoc($result);
  7448. $audio_player = null;
  7449. // We display an audio player if needed.
  7450. if (!empty($row['audio'])) {
  7451. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7452. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7453. </div>';
  7454. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7455. $audio_player .= '<script>
  7456. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7457. s1.addParam("allowscriptaccess","always");
  7458. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7459. s1.write("container");
  7460. </script>';
  7461. }
  7462. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7463. $return .= Display::url(
  7464. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7465. $url.'&action=edit_item&path_item=' . $row['path']
  7466. );
  7467. $return .= Display::url(
  7468. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7469. $url.'&action=move_item'
  7470. );
  7471. // Commented for now as prerequisites cannot be added to chapters.
  7472. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7473. $return .= Display::url(
  7474. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7475. $url.'&action=edit_item_prereq'
  7476. );
  7477. }
  7478. $return .= Display::url(
  7479. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7480. $url.'&action=delete_item'
  7481. );
  7482. if ($item_type == TOOL_HOTPOTATOES ) {
  7483. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7484. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7485. }
  7486. if ($item_type == TOOL_DOCUMENT ) {
  7487. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7488. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7489. }
  7490. $return .= '</div>';
  7491. if (!empty($audio_player)) {
  7492. $return .= '<br />'.$audio_player;
  7493. }
  7494. return $return;
  7495. }
  7496. /**
  7497. * Creates the javascript needed for filling up the checkboxes without page reload
  7498. * @return string
  7499. */
  7500. public function get_js_dropdown_array()
  7501. {
  7502. $course_id = api_get_course_int_id();
  7503. $return = 'var child_name = new Array();' . "\n";
  7504. $return .= 'var child_value = new Array();' . "\n\n";
  7505. $return .= 'child_name[0] = new Array();' . "\n";
  7506. $return .= 'child_value[0] = new Array();' . "\n\n";
  7507. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7508. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7509. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7510. ORDER BY display_order ASC";
  7511. $res_zero = Database::query($sql_zero);
  7512. $i = 0;
  7513. while ($row_zero = Database :: fetch_array($res_zero)) {
  7514. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7515. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7516. }
  7517. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7518. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7519. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7520. }
  7521. $return .= "\n";
  7522. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7523. $res = Database::query($sql);
  7524. while ($row = Database :: fetch_array($res)) {
  7525. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7526. WHERE
  7527. c_id = ".$course_id." AND
  7528. parent_item_id = " . $row['id'] . "
  7529. ORDER BY display_order ASC";
  7530. $res_parent = Database::query($sql_parent);
  7531. $i = 0;
  7532. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7533. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7534. while ($row_parent = Database :: fetch_array($res_parent)) {
  7535. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7536. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7537. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7538. }
  7539. $return .= "\n";
  7540. }
  7541. return $return;
  7542. }
  7543. /**
  7544. * Display the form to allow moving an item
  7545. * @param integer Item ID
  7546. * @return string HTML form
  7547. */
  7548. public function display_move_item($item_id)
  7549. {
  7550. $course_id = api_get_course_int_id();
  7551. $_course = api_get_course_info();
  7552. global $charset;
  7553. $return = '';
  7554. if (is_numeric($item_id)) {
  7555. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7556. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7557. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7558. $res = Database::query($sql);
  7559. $row = Database :: fetch_array($res);
  7560. switch ($row['item_type']) {
  7561. case 'dokeos_chapter' :
  7562. case 'dir' :
  7563. case 'asset' :
  7564. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7565. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7566. break;
  7567. case 'dokeos_module' :
  7568. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7569. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7570. break;
  7571. case TOOL_DOCUMENT :
  7572. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7573. $return .= $this->display_document_form('move', $item_id, $row);
  7574. break;
  7575. case TOOL_LINK :
  7576. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7577. $return .= $this->display_link_form('move', $item_id, $row);
  7578. break;
  7579. case TOOL_HOTPOTATOES :
  7580. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7581. $return .= $this->display_link_form('move', $item_id, $row);
  7582. break;
  7583. case TOOL_QUIZ :
  7584. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7585. $return .= $this->display_quiz_form('move', $item_id, $row);
  7586. break;
  7587. case TOOL_STUDENTPUBLICATION :
  7588. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7589. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7590. break;
  7591. case TOOL_FORUM :
  7592. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7593. $return .= $this->display_forum_form('move', $item_id, $row);
  7594. break;
  7595. case TOOL_THREAD :
  7596. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7597. $return .= $this->display_forum_form('move', $item_id, $row);
  7598. break;
  7599. }
  7600. }
  7601. return $return;
  7602. }
  7603. /**
  7604. * Displays a basic form on the overview page for changing the item title and the item description.
  7605. * @param string $item_type
  7606. * @param string $title
  7607. * @param array $data
  7608. * @return string
  7609. */
  7610. public function display_item_small_form($item_type, $title = '', $data = array())
  7611. {
  7612. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7613. $form = new FormValidator('small_form', 'post', $url);
  7614. $form->addElement('header', $title);
  7615. $form->addElement('text', 'title', get_lang('Title'));
  7616. $form->addElement('button', 'submit_button', get_lang('Save'));
  7617. $form->addElement('hidden', 'id', $data['id']);
  7618. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7619. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7620. $form->setDefaults(array('title' => $data['title']));
  7621. return $form->toHtml();
  7622. }
  7623. /**
  7624. * Return HTML form to allow prerequisites selection
  7625. * @todo use FormValidator
  7626. * @param integer Item ID
  7627. * @return string HTML form
  7628. */
  7629. public function display_item_prerequisites_form($item_id)
  7630. {
  7631. $course_id = api_get_course_int_id();
  7632. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7633. $item_id = intval($item_id);
  7634. /* Current prerequisite */
  7635. $sql = "SELECT * FROM $tbl_lp_item
  7636. WHERE c_id = $course_id AND id = " . $item_id;
  7637. $result = Database::query($sql);
  7638. $row = Database::fetch_array($result);
  7639. $prerequisiteId = $row['prerequisite'];
  7640. $return = '<legend>';
  7641. $return .= get_lang('AddEditPrerequisites');
  7642. $return .= '</legend>';
  7643. $return .= '<form method="POST">';
  7644. $return .= '<table class="data_table">';
  7645. $return .= '<tr>';
  7646. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7647. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7648. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7649. $return .= '</tr>';
  7650. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7651. $return .= '<tr >';
  7652. $return .= '<td colspan="3" class="radio">';
  7653. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7654. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7655. $return .= '</tr>';
  7656. $sql = "SELECT * FROM $tbl_lp_item
  7657. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7658. $result = Database::query($sql);
  7659. $arrLP = array();
  7660. $selectedMinScore = array();
  7661. $selectedMaxScore = array();
  7662. while ($row = Database :: fetch_array($result)) {
  7663. if ($row['id'] == $item_id) {
  7664. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7665. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7666. }
  7667. $arrLP[] = array(
  7668. 'id' => $row['id'],
  7669. 'item_type' => $row['item_type'],
  7670. 'title' => $row['title'],
  7671. 'ref' => $row['ref'],
  7672. 'description' => $row['description'],
  7673. 'parent_item_id' => $row['parent_item_id'],
  7674. 'previous_item_id' => $row['previous_item_id'],
  7675. 'next_item_id' => $row['next_item_id'],
  7676. 'max_score' => $row['max_score'],
  7677. 'min_score' => $row['min_score'],
  7678. 'mastery_score' => $row['mastery_score'],
  7679. 'prerequisite' => $row['prerequisite'],
  7680. 'next_item_id' => $row['next_item_id'],
  7681. 'display_order' => $row['display_order'],
  7682. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7683. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7684. );
  7685. }
  7686. $this->tree_array($arrLP);
  7687. $arrLP = $this->arrMenu;
  7688. unset($this->arrMenu);
  7689. for ($i = 0; $i < count($arrLP); $i++) {
  7690. $item = $arrLP[$i];
  7691. if ($item['id'] == $item_id) {
  7692. break;
  7693. }
  7694. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7695. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7696. $return .= '<tr>';
  7697. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7698. $return .= '<label for="id' . $item['id'] . '">';
  7699. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . (($item['item_type'] == 'dokeos_module' || $item['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7700. $icon_name = str_replace(' ', '', $item['item_type']);
  7701. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7702. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7703. } else {
  7704. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7705. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7706. } else {
  7707. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7708. }
  7709. }
  7710. $return .= $item['title'] . '</label>';
  7711. $return .= '</td>';
  7712. if ($item['item_type'] == TOOL_QUIZ) {
  7713. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7714. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7715. $tmp_obj_exercice = new Exercise();
  7716. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7717. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7718. $tmp_obj_lp_item->update_in_bdd();
  7719. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7720. $return .= '<td class="exercise">';
  7721. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7722. $return .= '</td>';
  7723. $return .= '<td class="exercise">';
  7724. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7725. $return .= '</td>';
  7726. }
  7727. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7728. $return .= '<td class="exercise">';
  7729. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7730. $return .= '</td>';
  7731. $return .= '<td class="exercise"">';
  7732. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7733. $return .= '</td>';
  7734. }
  7735. $return .= '</tr>';
  7736. }
  7737. $return .= '<tr>';
  7738. $return .= '</tr>';
  7739. $return .= '</table>';
  7740. $return .= '<div style="padding-top:3px;">';
  7741. $return .= '<button class="btn btn-default" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7742. $return .= '</form>';
  7743. return $return;
  7744. }
  7745. /**
  7746. * Return HTML list to allow prerequisites selection for lp
  7747. * @param integer Item ID
  7748. * @return string HTML form
  7749. */
  7750. public function display_lp_prerequisites_list()
  7751. {
  7752. $course_id = api_get_course_int_id();
  7753. $lp_id = $this->lp_id;
  7754. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7755. // get current prerequisite
  7756. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7757. $result = Database::query($sql);
  7758. $row = Database :: fetch_array($result);
  7759. $prerequisiteId = $row['prerequisite'];
  7760. $session_id = api_get_session_id();
  7761. $session_condition = api_get_session_condition($session_id);
  7762. $sql = "SELECT * FROM $tbl_lp
  7763. WHERE c_id = $course_id $session_condition
  7764. ORDER BY display_order ";
  7765. $rs = Database::query($sql);
  7766. $return = '';
  7767. $return .= '<select name="prerequisites" class="form-control">';
  7768. $return .= '<option value="0">'.get_lang('None').'</option>';
  7769. if (Database::num_rows($rs) > 0) {
  7770. while ($row = Database::fetch_array($rs)) {
  7771. if ($row['id'] == $lp_id) {
  7772. continue;
  7773. }
  7774. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7775. }
  7776. }
  7777. $return .= '</select>';
  7778. return $return;
  7779. }
  7780. /**
  7781. * Creates a list with all the documents in it
  7782. * @param bool $showInvisibleFiles
  7783. * @return string
  7784. */
  7785. public function get_documents($showInvisibleFiles = false)
  7786. {
  7787. $course_info = api_get_course_info();
  7788. $sessionId = api_get_session_id();
  7789. $document_tree = DocumentManager::get_document_preview(
  7790. $course_info,
  7791. $this->lp_id,
  7792. null,
  7793. $sessionId,
  7794. true,
  7795. null,
  7796. null,
  7797. $showInvisibleFiles,
  7798. true
  7799. );
  7800. return $document_tree;
  7801. }
  7802. /**
  7803. * Creates a list with all the exercises (quiz) in it
  7804. * @return string
  7805. */
  7806. public function get_exercises()
  7807. {
  7808. $course_id = api_get_course_int_id();
  7809. // New for hotpotatoes.
  7810. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7811. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7812. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7813. $session_id = api_get_session_id();
  7814. $condition_session = api_get_session_condition($session_id);
  7815. $sql_quiz = "SELECT * FROM $tbl_quiz
  7816. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7817. ORDER BY title ASC";
  7818. $sql_hot = "SELECT * FROM $tbl_doc
  7819. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7820. ORDER BY id ASC";
  7821. $res_quiz = Database::query($sql_quiz);
  7822. $res_hot = Database::query($sql_hot);
  7823. $return = '<ul class="lp_resource">';
  7824. $return .= '<li class="lp_resource_element">';
  7825. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7826. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7827. get_lang('NewExercise') . '</a>';
  7828. $return .= '</li>';
  7829. // Display hotpotatoes
  7830. while ($row_hot = Database :: fetch_array($res_hot)) {
  7831. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7832. $return .= '<a class="moved" href="#">';
  7833. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7834. $return .= '</a> ';
  7835. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7836. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7837. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7838. $return .= '</li>';
  7839. }
  7840. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7841. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7842. $return .= '<a class="moved" href="#">';
  7843. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7844. $return .= '</a> ';
  7845. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7846. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7847. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7848. '</a>';
  7849. $return .= '</li>';
  7850. }
  7851. $return .= '</ul>';
  7852. return $return;
  7853. }
  7854. /**
  7855. * Creates a list with all the links in it
  7856. * @return string
  7857. */
  7858. public function get_links()
  7859. {
  7860. $selfUrl = api_get_self();
  7861. $courseIdReq = api_get_cidreq();
  7862. $course = api_get_course_info();
  7863. $course_id = $course['real_id'];
  7864. $tbl_link = Database::get_course_table(TABLE_LINK);
  7865. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7866. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7867. $session_id = api_get_session_id();
  7868. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7869. $sql = "SELECT link.id as link_id,
  7870. link.title as link_title,
  7871. link.category_id as category_id,
  7872. link_category.category_title as category_title
  7873. FROM $tbl_link as link
  7874. LEFT JOIN $linkCategoryTable as link_category
  7875. ON link.category_id = link_category.id
  7876. WHERE link.c_id = ".$course_id." $condition_session
  7877. ORDER BY link_category.category_title ASC, link.title ASC";
  7878. $links = Database::query($sql);
  7879. $categorizedLinks = array();
  7880. $categories = array();
  7881. while ($link = Database :: fetch_array($links)) {
  7882. if (!$link['category_id']) {
  7883. $link['category_title'] = get_lang('Uncategorized');
  7884. }
  7885. $categories[$link['category_id']] = $link['category_title'];
  7886. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7887. }
  7888. $linksHtmlCode =
  7889. '<script>
  7890. function toggle_tool(tool, id){
  7891. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7892. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7893. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7894. } else {
  7895. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7896. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7897. }
  7898. }
  7899. </script>
  7900. <ul class="lp_resource">
  7901. <li class="lp_resource_element">
  7902. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7903. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7904. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7905. </li>';
  7906. foreach ($categorizedLinks as $categoryId => $links) {
  7907. $linkNodes = null;
  7908. foreach ($links as $key => $title) {
  7909. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7910. $linkNodes .=
  7911. '<li class="lp_resource_element" data_id="'.$key.
  7912. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7913. <a class="moved" href="#">'.
  7914. $moveEverywhereIcon.
  7915. '</a>
  7916. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7917. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7918. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7919. Security::remove_XSS($title).
  7920. '</a>
  7921. </li>';
  7922. }
  7923. }
  7924. $linksHtmlCode .=
  7925. '<li>
  7926. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7927. style="vertical-align:middle">
  7928. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7929. align="absbottom" />
  7930. </a>
  7931. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7932. </li>
  7933. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7934. }
  7935. $linksHtmlCode .= '</ul>';
  7936. return $linksHtmlCode;
  7937. }
  7938. /**
  7939. * Creates a list with all the student publications in it
  7940. * @return unknown
  7941. */
  7942. public function get_student_publications()
  7943. {
  7944. $return = '<div class="lp_resource" >';
  7945. $return .= '<div class="lp_resource_element">';
  7946. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7947. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7948. $return .= '</div>';
  7949. $return .= '</div>';
  7950. return $return;
  7951. }
  7952. /**
  7953. * Creates a list with all the forums in it
  7954. * @return string
  7955. */
  7956. public function get_forums()
  7957. {
  7958. require_once '../forum/forumfunction.inc.php';
  7959. require_once '../forum/forumconfig.inc.php';
  7960. $a_forums = get_forums();
  7961. $return = '<ul class="lp_resource">';
  7962. //First add link
  7963. $return .= '<li class="lp_resource_element">';
  7964. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7965. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&content=forum&origin=learnpath&lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7966. $return .= '</li>';
  7967. $return .= '<script>
  7968. function toggle_forum(forum_id){
  7969. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7970. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7971. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7972. } else {
  7973. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7974. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7975. }
  7976. }
  7977. </script>';
  7978. foreach ($a_forums as $forum) {
  7979. if (!empty($forum['forum_id'])) {
  7980. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7981. $return .= '<a class="moved" href="#">';
  7982. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7983. $return .= ' </a>';
  7984. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7985. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7986. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7987. </a>
  7988. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  7989. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7990. $return .= '</li>';
  7991. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7992. $a_threads = get_threads($forum['forum_id']);
  7993. if (is_array($a_threads)) {
  7994. foreach ($a_threads as $thread) {
  7995. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7996. $return .= '&nbsp;<a class="moved" href="#">';
  7997. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7998. $return .= ' </a>';
  7999. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8000. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  8001. Security :: remove_XSS($thread['thread_title']) . '</a>';
  8002. $return .= '</li>';
  8003. }
  8004. }
  8005. $return .= '</div>';
  8006. }
  8007. }
  8008. $return .= '</ul>';
  8009. return $return;
  8010. }
  8011. /**
  8012. * // TODO: The output encoding should be equal to the system encoding.
  8013. *
  8014. * Exports the learning path as a SCORM package. This is the main function that
  8015. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8016. * whole thing and returns the zip.
  8017. *
  8018. * This method needs to be called in PHP5, as it will fail with non-adequate
  8019. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8020. * you need to call it on a learnpath object.
  8021. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8022. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8023. * path has been modified, it should use the generic method here below.
  8024. * @TODO link this function with the export_lp() function in the same class
  8025. * @param string Optional name of zip file. If none, title of learnpath is
  8026. * domesticated and trailed with ".zip"
  8027. * @return string Returns the zip package string, or null if error
  8028. */
  8029. public function scorm_export()
  8030. {
  8031. $_course = api_get_course_info();
  8032. $course_id = $_course['real_id'];
  8033. // Remove memory and time limits as much as possible as this might be a long process...
  8034. if (function_exists('ini_set')) {
  8035. api_set_memory_limit('128M');
  8036. ini_set('max_execution_time', 600);
  8037. }
  8038. // Create the zip handler (this will remain available throughout the method).
  8039. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8040. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8041. $temp_dir_short = uniqid();
  8042. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8043. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8044. $zip_folder = new PclZip($temp_zip_file);
  8045. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8046. $root_path = $main_path = api_get_path(SYS_PATH);
  8047. $files_cleanup = array();
  8048. // Place to temporarily stash the zip file.
  8049. // create the temp dir if it doesn't exist
  8050. // or do a cleanup before creating the zip file.
  8051. if (!is_dir($temp_zip_dir)) {
  8052. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8053. } else {
  8054. // Cleanup: Check the temp dir for old files and delete them.
  8055. $handle = opendir($temp_zip_dir);
  8056. while (false !== ($file = readdir($handle))) {
  8057. if ($file != '.' && $file != '..') {
  8058. unlink("$temp_zip_dir/$file");
  8059. }
  8060. }
  8061. closedir($handle);
  8062. }
  8063. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8064. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8065. // Remove the possible . at the end of the path.
  8066. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8067. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8068. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8069. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  8070. }
  8071. // Build a dummy imsmanifest structure.
  8072. // Do not add to the zip yet (we still need it).
  8073. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8074. // Aggregation Model official document, section "2.3 Content Packaging".
  8075. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8076. $xmldoc = new DOMDocument('1.0');
  8077. $root = $xmldoc->createElement('manifest');
  8078. $root->setAttribute('identifier', 'SingleCourseManifest');
  8079. $root->setAttribute('version', '1.1');
  8080. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8081. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8082. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8083. $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');
  8084. // Build mandatory sub-root container elements.
  8085. $metadata = $xmldoc->createElement('metadata');
  8086. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8087. $metadata->appendChild($md_schema);
  8088. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8089. $metadata->appendChild($md_schemaversion);
  8090. $root->appendChild($metadata);
  8091. $organizations = $xmldoc->createElement('organizations');
  8092. $resources = $xmldoc->createElement('resources');
  8093. // Build the only organization we will use in building our learnpaths.
  8094. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8095. $organization = $xmldoc->createElement('organization');
  8096. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8097. // To set the title of the SCORM entity (=organization), we take the name given
  8098. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8099. // learning path charset) as it is the encoding that defines how it is stored
  8100. // in the database. Then we convert it to HTML entities again as the "&" character
  8101. // alone is not authorized in XML (must be &amp;).
  8102. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8103. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8104. $organization->appendChild($org_title);
  8105. $folder_name = 'document';
  8106. // Removes the learning_path/scorm_folder path when exporting see #4841
  8107. $path_to_remove = null;
  8108. $result = $this->generate_lp_folder($_course);
  8109. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8110. $path_to_remove = 'document'.$result['dir'];
  8111. $path_to_replace = $folder_name.'/';
  8112. }
  8113. //Fixes chamilo scorm exports
  8114. if ($this->ref == 'chamilo_scorm_export') {
  8115. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8116. }
  8117. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8118. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8119. $link_updates = array();
  8120. $links_to_create = array();
  8121. foreach ($this->items as $index => $item) {
  8122. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8123. // Get included documents from this item.
  8124. if ($item->type == 'sco') {
  8125. $inc_docs = $item->get_resources_from_source(
  8126. null,
  8127. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8128. );
  8129. } else {
  8130. $inc_docs = $item->get_resources_from_source();
  8131. }
  8132. // Give a child element <item> to the <organization> element.
  8133. $my_item_id = $item->get_id();
  8134. $my_item = $xmldoc->createElement('item');
  8135. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8136. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8137. $my_item->setAttribute('isvisible', 'true');
  8138. // Give a child element <title> to the <item> element.
  8139. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8140. $my_item->appendChild($my_title);
  8141. // Give a child element <adlcp:prerequisites> to the <item> element.
  8142. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8143. $my_prereqs->setAttribute('type', 'aicc_script');
  8144. $my_item->appendChild($my_prereqs);
  8145. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8146. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8147. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8148. //$xmldoc->createElement('adlcp:timelimitaction','');
  8149. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8150. //$xmldoc->createElement('adlcp:datafromlms','');
  8151. // Give a child element <adlcp:masteryscore> to the <item> element.
  8152. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8153. $my_item->appendChild($my_masteryscore);
  8154. // Attach this item to the organization element or hits parent if there is one.
  8155. if (!empty($item->parent) && $item->parent != 0) {
  8156. $children = $organization->childNodes;
  8157. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8158. if (is_object($possible_parent)) {
  8159. $possible_parent->appendChild($my_item);
  8160. } else {
  8161. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8162. }
  8163. } else {
  8164. if ($this->debug > 0) { error_log('No parent'); }
  8165. $organization->appendChild($my_item);
  8166. }
  8167. // Get the path of the file(s) from the course directory root.
  8168. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8169. if (!empty($path_to_remove)) {
  8170. //From docs
  8171. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8172. //From quiz
  8173. if ($this->ref == 'chamilo_scorm_export') {
  8174. $path_to_remove = 'scorm/'.$this->path.'/';
  8175. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8176. }
  8177. } else {
  8178. $my_xml_file_path = $my_file_path;
  8179. }
  8180. $my_sub_dir = dirname($my_file_path);
  8181. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8182. $my_xml_sub_dir = $my_sub_dir;
  8183. // Give a <resource> child to the <resources> element
  8184. $my_resource = $xmldoc->createElement('resource');
  8185. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8186. $my_resource->setAttribute('type', 'webcontent');
  8187. $my_resource->setAttribute('href', $my_xml_file_path);
  8188. // adlcp:scormtype can be either 'sco' or 'asset'.
  8189. if ($item->type == 'sco') {
  8190. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8191. } else {
  8192. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8193. }
  8194. // xml:base is the base directory to find the files declared in this resource.
  8195. $my_resource->setAttribute('xml:base', '');
  8196. // Give a <file> child to the <resource> element.
  8197. $my_file = $xmldoc->createElement('file');
  8198. $my_file->setAttribute('href', $my_xml_file_path);
  8199. $my_resource->appendChild($my_file);
  8200. // Dependency to other files - not yet supported.
  8201. $i = 1;
  8202. foreach ($inc_docs as $doc_info) {
  8203. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8204. $my_dep = $xmldoc->createElement('resource');
  8205. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8206. $my_dep->setAttribute('identifier', $res_id);
  8207. $my_dep->setAttribute('type', 'webcontent');
  8208. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8209. $my_dep_file = $xmldoc->createElement('file');
  8210. // Check type of URL.
  8211. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8212. if ($doc_info[1] == 'remote') {
  8213. // Remote file. Save url as is.
  8214. $my_dep_file->setAttribute('href', $doc_info[0]);
  8215. $my_dep->setAttribute('xml:base', '');
  8216. } elseif ($doc_info[1] == 'local') {
  8217. switch ($doc_info[2]) {
  8218. case 'url': // Local URL - save path as url for now, don't zip file.
  8219. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8220. $current_dir = dirname($abs_path);
  8221. $current_dir = str_replace('\\', '/', $current_dir);
  8222. $file_path = realpath($abs_path);
  8223. $file_path = str_replace('\\', '/', $file_path);
  8224. $my_dep_file->setAttribute('href', $file_path);
  8225. $my_dep->setAttribute('xml:base', '');
  8226. if (strstr($file_path, $main_path) !== false) {
  8227. // The calculated real path is really inside Chamilo's root path.
  8228. // Reduce file path to what's under the DocumentRoot.
  8229. $file_path = substr($file_path, strlen($root_path) - 1);
  8230. //echo $file_path;echo '<br /><br />';
  8231. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8232. $zip_files_abs[] = $file_path;
  8233. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8234. $my_dep_file->setAttribute('href', $file_path);
  8235. $my_dep->setAttribute('xml:base', '');
  8236. } elseif (empty($file_path)) {
  8237. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8238. if (strpos($document_root, -1) == '/') {
  8239. $document_root = substr(0, -1, $document_root);
  8240. }*/
  8241. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8242. $file_path = str_replace('//', '/', $file_path);
  8243. if (file_exists($file_path)) {
  8244. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8245. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8246. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8247. $my_dep_file->setAttribute('href', $file_path);
  8248. $my_dep->setAttribute('xml:base', '');
  8249. }
  8250. }
  8251. break;
  8252. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8253. $my_dep_file->setAttribute('href', $doc_info[0]);
  8254. $my_dep->setAttribute('xml:base', '');
  8255. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8256. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8257. $abs_img_path_without_subdir = $doc_info[0];
  8258. $relp = api_get_path(REL_PATH); // The url-append config param.
  8259. $pos = strpos($abs_img_path_without_subdir, $relp);
  8260. if ($pos === 0) {
  8261. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8262. }
  8263. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8264. $file_path = str_replace('\\', '/', $file_path);
  8265. $file_path = str_replace('//', '/', $file_path);
  8266. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8267. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8268. // Check if the current document is in that path.
  8269. if (strstr($file_path, $cur_path) !== false) {
  8270. // The document is in that path, now get the relative path
  8271. // to the containing document.
  8272. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8273. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8274. $relative_path = '';
  8275. if (strstr($file_path, $cur_path) !== false) {
  8276. //$relative_path = substr($file_path, strlen($orig_file_path));
  8277. $relative_path = str_replace($cur_path, '', $file_path);
  8278. $file_path = substr($file_path, strlen($cur_path));
  8279. } else {
  8280. // This case is still a problem as it's difficult to calculate a relative path easily
  8281. // might still generate wrong links.
  8282. //$file_path = substr($file_path,strlen($cur_path));
  8283. // Calculate the directory path to the current file (without trailing slash).
  8284. $my_relative_path = dirname($file_path);
  8285. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8286. $my_relative_file = basename($file_path);
  8287. // Calculate the directory path to the containing file (without trailing slash).
  8288. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8289. $dotdot = '';
  8290. $subdir = '';
  8291. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8292. $my_relative_path2 = dirname($my_relative_path);
  8293. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8294. $my_orig_file_path = dirname($my_orig_file_path);
  8295. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8296. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8297. $dotdot += '../';
  8298. $my_relative_path = $my_relative_path2;
  8299. }
  8300. $relative_path = $dotdot.$subdir.$my_relative_file;
  8301. }
  8302. // Put the current document in the zip (this array is the array
  8303. // that will manage documents already in the course folder - relative).
  8304. $zip_files[] = $file_path;
  8305. // Update the links to the current document in the containing document (make them relative).
  8306. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8307. $my_dep_file->setAttribute('href', $file_path);
  8308. $my_dep->setAttribute('xml:base', '');
  8309. } elseif (strstr($file_path, $main_path) !== false) {
  8310. // The calculated real path is really inside Chamilo's root path.
  8311. // Reduce file path to what's under the DocumentRoot.
  8312. $file_path = substr($file_path, strlen($root_path));
  8313. //echo $file_path;echo '<br /><br />';
  8314. //error_log('Reduced path: '.$file_path, 0);
  8315. $zip_files_abs[] = $file_path;
  8316. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8317. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8318. $my_dep->setAttribute('xml:base', '');
  8319. } elseif (empty($file_path)) {
  8320. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8321. if(strpos($document_root,-1) == '/') {
  8322. $document_root = substr(0, -1, $document_root);
  8323. }*/
  8324. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8325. $file_path = str_replace('//', '/', $file_path);
  8326. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8327. $current_dir = dirname($abs_path);
  8328. $current_dir = str_replace('\\', '/', $current_dir);
  8329. if (file_exists($file_path)) {
  8330. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8331. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8332. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8333. $my_dep_file->setAttribute('href','document/'.$file_path);
  8334. $my_dep->setAttribute('xml:base', '');
  8335. }
  8336. }
  8337. break;
  8338. case 'rel':
  8339. // Path relative to the current document.
  8340. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8341. if (substr($doc_info[0], 0, 2) == '..') {
  8342. // Relative path going up.
  8343. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8344. $current_dir = str_replace('\\', '/', $current_dir);
  8345. $file_path = realpath($current_dir.$doc_info[0]);
  8346. $file_path = str_replace('\\', '/', $file_path);
  8347. //error_log($file_path.' <-> '.$main_path,0);
  8348. if (strstr($file_path, $main_path) !== false) {
  8349. // The calculated real path is really inside Chamilo's root path.
  8350. // Reduce file path to what's under the DocumentRoot.
  8351. $file_path = substr($file_path, strlen($root_path));
  8352. //error_log('Reduced path: '.$file_path, 0);
  8353. $zip_files_abs[] = $file_path;
  8354. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8355. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8356. $my_dep->setAttribute('xml:base', '');
  8357. }
  8358. } else {
  8359. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8360. $my_dep_file->setAttribute('href', $doc_info[0]);
  8361. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8362. }
  8363. break;
  8364. default:
  8365. $my_dep_file->setAttribute('href', $doc_info[0]);
  8366. $my_dep->setAttribute('xml:base', '');
  8367. break;
  8368. }
  8369. }
  8370. $my_dep->appendChild($my_dep_file);
  8371. $resources->appendChild($my_dep);
  8372. $dependency = $xmldoc->createElement('dependency');
  8373. $dependency->setAttribute('identifierref', $res_id);
  8374. $my_resource->appendChild($dependency);
  8375. $i++;
  8376. }
  8377. $resources->appendChild($my_resource);
  8378. $zip_files[] = $my_file_path;
  8379. } else {
  8380. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8381. switch ($item->type) {
  8382. case TOOL_LINK:
  8383. $my_item = $xmldoc->createElement('item');
  8384. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8385. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8386. $my_item->setAttribute('isvisible', 'true');
  8387. // Give a child element <title> to the <item> element.
  8388. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8389. $my_item->appendChild($my_title);
  8390. // Give a child element <adlcp:prerequisites> to the <item> element.
  8391. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8392. $my_prereqs->setAttribute('type', 'aicc_script');
  8393. $my_item->appendChild($my_prereqs);
  8394. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8395. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8396. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8397. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8398. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8399. //$xmldoc->createElement('adlcp:datafromlms', '');
  8400. // Give a child element <adlcp:masteryscore> to the <item> element.
  8401. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8402. $my_item->appendChild($my_masteryscore);
  8403. // Attach this item to the organization element or its parent if there is one.
  8404. if (!empty($item->parent) && $item->parent != 0) {
  8405. $children = $organization->childNodes;
  8406. for ($i = 0; $i < $children->length; $i++) {
  8407. $item_temp = $children->item($i);
  8408. if ($item_temp -> nodeName == 'item') {
  8409. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8410. $item_temp -> appendChild($my_item);
  8411. }
  8412. }
  8413. }
  8414. } else {
  8415. $organization->appendChild($my_item);
  8416. }
  8417. $my_file_path = 'link_'.$item->get_id().'.html';
  8418. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8419. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8420. $rs = Database::query($sql);
  8421. if ($link = Database :: fetch_array($rs)) {
  8422. $url = $link['url'];
  8423. $title = stripslashes($link['title']);
  8424. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8425. $my_xml_file_path = $my_file_path;
  8426. $my_sub_dir = dirname($my_file_path);
  8427. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8428. $my_xml_sub_dir = $my_sub_dir;
  8429. // Give a <resource> child to the <resources> element.
  8430. $my_resource = $xmldoc->createElement('resource');
  8431. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8432. $my_resource->setAttribute('type', 'webcontent');
  8433. $my_resource->setAttribute('href', $my_xml_file_path);
  8434. // adlcp:scormtype can be either 'sco' or 'asset'.
  8435. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8436. // xml:base is the base directory to find the files declared in this resource.
  8437. $my_resource->setAttribute('xml:base', '');
  8438. // give a <file> child to the <resource> element.
  8439. $my_file = $xmldoc->createElement('file');
  8440. $my_file->setAttribute('href', $my_xml_file_path);
  8441. $my_resource->appendChild($my_file);
  8442. $resources->appendChild($my_resource);
  8443. }
  8444. break;
  8445. case TOOL_QUIZ:
  8446. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8447. $exe = new Exercise();
  8448. $exe->read($exe_id);
  8449. $my_item = $xmldoc->createElement('item');
  8450. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8451. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8452. $my_item->setAttribute('isvisible', 'true');
  8453. // Give a child element <title> to the <item> element.
  8454. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8455. $my_item->appendChild($my_title);
  8456. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8457. //$my_item->appendChild($my_max_score);
  8458. // Give a child element <adlcp:prerequisites> to the <item> element.
  8459. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8460. $my_prereqs->setAttribute('type','aicc_script');
  8461. $my_item->appendChild($my_prereqs);
  8462. // Give a child element <adlcp:masteryscore> to the <item> element.
  8463. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8464. $my_item->appendChild($my_masteryscore);
  8465. // Attach this item to the organization element or hits parent if there is one.
  8466. if (!empty($item->parent) && $item->parent != 0) {
  8467. $children = $organization->childNodes;
  8468. for ($i = 0; $i < $children->length; $i++) {
  8469. $item_temp = $children->item($i);
  8470. if ($item_temp -> nodeName == 'item') {
  8471. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8472. $item_temp -> appendChild($my_item);
  8473. }
  8474. }
  8475. }
  8476. } else {
  8477. $organization->appendChild($my_item);
  8478. }
  8479. // Include export scripts.
  8480. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8481. // Get the path of the file(s) from the course directory root
  8482. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8483. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8484. // Write the contents of the exported exercise into a (big) html file
  8485. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8486. $contents = export_exercise($exe_id, true);
  8487. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8488. $res = file_put_contents($tmp_file_path, $contents);
  8489. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8490. $files_cleanup[] = $tmp_file_path;
  8491. //error_log($tmp_path); die();
  8492. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8493. $my_xml_file_path = $my_file_path;
  8494. $my_sub_dir = dirname($my_file_path);
  8495. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8496. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8497. $my_xml_sub_dir = $my_sub_dir;
  8498. // Give a <resource> child to the <resources> element.
  8499. $my_resource = $xmldoc->createElement('resource');
  8500. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8501. $my_resource->setAttribute('type', 'webcontent');
  8502. $my_resource->setAttribute('href', $my_xml_file_path);
  8503. // adlcp:scormtype can be either 'sco' or 'asset'.
  8504. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8505. // xml:base is the base directory to find the files declared in this resource.
  8506. $my_resource->setAttribute('xml:base', '');
  8507. // Give a <file> child to the <resource> element.
  8508. $my_file = $xmldoc->createElement('file');
  8509. $my_file->setAttribute('href', $my_xml_file_path);
  8510. $my_resource->appendChild($my_file);
  8511. // Get included docs.
  8512. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8513. // Dependency to other files - not yet supported.
  8514. $i = 1;
  8515. foreach ($inc_docs as $doc_info) {
  8516. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8517. $my_dep = $xmldoc->createElement('resource');
  8518. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8519. $my_dep->setAttribute('identifier', $res_id);
  8520. $my_dep->setAttribute('type', 'webcontent');
  8521. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8522. $my_dep_file = $xmldoc->createElement('file');
  8523. // Check type of URL.
  8524. if ($doc_info[1] == 'remote') {
  8525. // Remote file. Save url as is.
  8526. $my_dep_file->setAttribute('href', $doc_info[0]);
  8527. $my_dep->setAttribute('xml:base', '');
  8528. } elseif ($doc_info[1] == 'local') {
  8529. switch ($doc_info[2]) {
  8530. case 'url': // Local URL - save path as url for now, don't zip file.
  8531. // Save file but as local file (retrieve from URL).
  8532. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8533. $current_dir = dirname($abs_path);
  8534. $current_dir = str_replace('\\', '/', $current_dir);
  8535. $file_path = realpath($abs_path);
  8536. $file_path = str_replace('\\', '/', $file_path);
  8537. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8538. $my_dep->setAttribute('xml:base', '');
  8539. if (strstr($file_path, $main_path) !== false) {
  8540. // The calculated real path is really inside the chamilo root path.
  8541. // Reduce file path to what's under the DocumentRoot.
  8542. $file_path = substr($file_path, strlen($root_path));
  8543. //echo $file_path;echo '<br /><br />';
  8544. //error_log('Reduced path: '.$file_path, 0);
  8545. $zip_files_abs[] = $file_path;
  8546. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8547. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8548. $my_dep->setAttribute('xml:base', '');
  8549. } elseif (empty($file_path)) {
  8550. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8551. if (strpos($document_root,-1) == '/') {
  8552. $document_root = substr(0, -1, $document_root);
  8553. }*/
  8554. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8555. $file_path = str_replace('//', '/', $file_path);
  8556. if (file_exists($file_path)) {
  8557. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8558. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8559. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8560. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8561. $my_dep->setAttribute('xml:base', '');
  8562. }
  8563. }
  8564. break;
  8565. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8566. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8567. $current_dir = str_replace('\\', '/', $current_dir);
  8568. $file_path = realpath($doc_info[0]);
  8569. $file_path = str_replace('\\', '/', $file_path);
  8570. $my_dep_file->setAttribute('href', $file_path);
  8571. $my_dep->setAttribute('xml:base', '');
  8572. if (strstr($file_path,$main_path) !== false) {
  8573. // The calculated real path is really inside the chamilo root path.
  8574. // Reduce file path to what's under the DocumentRoot.
  8575. $file_path = substr($file_path, strlen($root_path));
  8576. //echo $file_path;echo '<br /><br />';
  8577. //error_log('Reduced path: '.$file_path, 0);
  8578. $zip_files_abs[] = $file_path;
  8579. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8580. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8581. $my_dep->setAttribute('xml:base', '');
  8582. } elseif (empty($file_path)) {
  8583. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8584. if (strpos($document_root,-1) == '/') {
  8585. $document_root = substr(0, -1, $document_root);
  8586. }*/
  8587. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8588. $file_path = str_replace('//', '/', $file_path);
  8589. if (file_exists($file_path)) {
  8590. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8591. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8592. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8593. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8594. $my_dep->setAttribute('xml:base', '');
  8595. }
  8596. }
  8597. break;
  8598. 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
  8599. if (substr($doc_info[0], 0, 2) == '..') {
  8600. // Relative path going up.
  8601. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8602. $current_dir = str_replace('\\', '/', $current_dir);
  8603. $file_path = realpath($current_dir.$doc_info[0]);
  8604. $file_path = str_replace('\\', '/', $file_path);
  8605. //error_log($file_path.' <-> '.$main_path, 0);
  8606. if (strstr($file_path, $main_path) !== false) {
  8607. // The calculated real path is really inside Chamilo's root path.
  8608. // Reduce file path to what's under the DocumentRoot.
  8609. $file_path = substr($file_path, strlen($root_path));
  8610. $file_path_dest = $file_path;
  8611. // File path is courses/CHAMILO/document/....
  8612. $info_file_path = explode('/', $file_path);
  8613. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8614. $file_path_dest = 'document/'.$file_path;
  8615. }
  8616. //error_log('Reduced path: '.$file_path, 0);
  8617. $zip_files_abs[] = $file_path;
  8618. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8619. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8620. $my_dep->setAttribute('xml:base', '');
  8621. }
  8622. } else {
  8623. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8624. $my_dep_file->setAttribute('href', $doc_info[0]);
  8625. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8626. }
  8627. break;
  8628. default:
  8629. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8630. $my_dep->setAttribute('xml:base', '');
  8631. break;
  8632. }
  8633. }
  8634. $my_dep->appendChild($my_dep_file);
  8635. $resources->appendChild($my_dep);
  8636. $dependency = $xmldoc->createElement('dependency');
  8637. $dependency->setAttribute('identifierref', $res_id);
  8638. $my_resource->appendChild($dependency);
  8639. $i++;
  8640. }
  8641. $resources->appendChild($my_resource);
  8642. $zip_files[] = $my_file_path;
  8643. break;
  8644. default:
  8645. // Get the path of the file(s) from the course directory root
  8646. $my_file_path = 'non_exportable.html';
  8647. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8648. $my_xml_file_path = $my_file_path;
  8649. $my_sub_dir = dirname($my_file_path);
  8650. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8651. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8652. $my_xml_sub_dir = $my_sub_dir;
  8653. // Give a <resource> child to the <resources> element.
  8654. $my_resource = $xmldoc->createElement('resource');
  8655. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8656. $my_resource->setAttribute('type', 'webcontent');
  8657. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8658. // adlcp:scormtype can be either 'sco' or 'asset'.
  8659. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8660. // xml:base is the base directory to find the files declared in this resource.
  8661. $my_resource->setAttribute('xml:base', '');
  8662. // Give a <file> child to the <resource> element.
  8663. $my_file = $xmldoc->createElement('file');
  8664. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8665. $my_resource->appendChild($my_file);
  8666. $resources->appendChild($my_resource);
  8667. break;
  8668. }
  8669. }
  8670. }
  8671. $organizations->appendChild($organization);
  8672. $root->appendChild($organizations);
  8673. $root->appendChild($resources);
  8674. $xmldoc->appendChild($root);
  8675. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8676. // then add the file to the zip, then destroy the file (this is done automatically).
  8677. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8678. foreach ($zip_files as $file_path) {
  8679. if (empty($file_path)) {
  8680. continue;
  8681. }
  8682. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8683. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8684. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8685. }
  8686. $this->create_path($dest_file);
  8687. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8688. // Check if the file needs a link update.
  8689. if (in_array($file_path, array_keys($link_updates))) {
  8690. $string = file_get_contents($dest_file);
  8691. unlink($dest_file);
  8692. foreach ($link_updates[$file_path] as $old_new) {
  8693. // This is an ugly hack that allows .flv files to be found by the flv player that
  8694. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8695. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8696. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8697. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8698. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8699. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8700. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8701. }
  8702. //Fix to avoid problems with default_course_document
  8703. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8704. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8705. $newDestination = $old_new['dest'];
  8706. } else {
  8707. $newDestination = str_replace('document/', '', $old_new['dest']);
  8708. }
  8709. $string = str_replace($old_new['orig'], $newDestination, $string);
  8710. //Add files inside the HTMLs
  8711. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8712. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8713. if (file_exists($sys_course_path.$new_path)) {
  8714. copy($sys_course_path.$new_path, $destinationFile);
  8715. }
  8716. }
  8717. file_put_contents($dest_file, $string);
  8718. }
  8719. }
  8720. foreach ($zip_files_abs as $file_path) {
  8721. if (empty($file_path)) {
  8722. continue;
  8723. }
  8724. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8725. continue;
  8726. }
  8727. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8728. $this->create_path($dest_file);
  8729. copy($main_path.$file_path, $dest_file);
  8730. // Check if the file needs a link update.
  8731. if (in_array($file_path, array_keys($link_updates))) {
  8732. $string = file_get_contents($dest_file);
  8733. unlink($dest_file);
  8734. foreach ($link_updates[$file_path] as $old_new) {
  8735. // This is an ugly hack that allows .flv files to be found by the flv player that
  8736. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8737. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8738. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8739. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8740. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8741. }
  8742. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8743. }
  8744. file_put_contents($dest_file, $string);
  8745. }
  8746. }
  8747. if (is_array($links_to_create)) {
  8748. foreach ($links_to_create as $file => $link) {
  8749. $file_content = '<!DOCTYPE html><head>
  8750. <meta charset="'.api_get_language_isocode().'" />
  8751. <title>'.$link['title'].'</title>
  8752. </head>
  8753. <body dir="'.api_get_text_direction().'">
  8754. <div style="text-align:center">
  8755. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8756. </body>
  8757. </html>';
  8758. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8759. }
  8760. }
  8761. // Add non exportable message explanation.
  8762. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8763. $file_content = '<!DOCTYPE html><head>
  8764. <meta charset="'.api_get_language_isocode().'" />
  8765. <title>'.$lang_not_exportable.'</title>
  8766. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8767. </head>
  8768. <body dir="'.api_get_text_direction().'">';
  8769. $file_content .=
  8770. <<<EOD
  8771. <style>
  8772. .error-message {
  8773. font-family: arial, verdana, helvetica, sans-serif;
  8774. border-width: 1px;
  8775. border-style: solid;
  8776. left: 50%;
  8777. margin: 10px auto;
  8778. min-height: 30px;
  8779. padding: 5px;
  8780. right: 50%;
  8781. width: 500px;
  8782. background-color: #FFD1D1;
  8783. border-color: #FF0000;
  8784. color: #000;
  8785. }
  8786. </style>
  8787. <body>
  8788. <div class="error-message">
  8789. $lang_not_exportable
  8790. </div>
  8791. </body>
  8792. </html>
  8793. EOD;
  8794. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8795. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8796. }
  8797. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8798. // Add the extra files that go along with a SCORM package.
  8799. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8800. $extra_files = scandir($main_code_path);
  8801. foreach ($extra_files as $extra_file) {
  8802. if (strpos($extra_file, '.') === 0)
  8803. continue;
  8804. else {
  8805. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8806. $this->create_path($dest_file);
  8807. copy($main_code_path.$extra_file, $dest_file);
  8808. }
  8809. }
  8810. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8811. $manifest = @$xmldoc->saveXML();
  8812. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8813. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8814. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8815. // Clean possible temporary files.
  8816. foreach ($files_cleanup as $file) {
  8817. $res = unlink($file);
  8818. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8819. }
  8820. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8821. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8822. }
  8823. /**
  8824. * @param int $lp_id
  8825. * @return bool
  8826. */
  8827. public function scorm_export_to_pdf($lp_id)
  8828. {
  8829. $lp_id = intval($lp_id);
  8830. $files_to_export = array();
  8831. $course_data = api_get_course_info($this->cc);
  8832. if (!empty($course_data)) {
  8833. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8834. $list = self::get_flat_ordered_items_list($lp_id);
  8835. if (!empty($list)) {
  8836. foreach ($list as $item_id) {
  8837. $item = $this->items[$item_id];
  8838. switch ($item->type) {
  8839. case 'document':
  8840. //Getting documents from a LP with chamilo documents
  8841. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8842. // Try loading document from the base course.
  8843. if (empty($file_data) && !empty($sessionId)) {
  8844. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8845. }
  8846. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8847. if (file_exists($file_path)) {
  8848. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8849. }
  8850. break;
  8851. case 'asset': //commes from a scorm package generated by chamilo
  8852. case 'sco':
  8853. $file_path = $scorm_path.'/'.$item->path;
  8854. if (file_exists($file_path)) {
  8855. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8856. }
  8857. break;
  8858. case 'dokeos_chapter':
  8859. case 'dir':
  8860. case 'chapter':
  8861. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8862. break;
  8863. }
  8864. }
  8865. }
  8866. $pdf = new PDF();
  8867. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8868. return $result;
  8869. }
  8870. return false;
  8871. }
  8872. /**
  8873. * Temp function to be moved in main_api or the best place around for this.
  8874. * Creates a file path if it doesn't exist
  8875. * @param string $path
  8876. */
  8877. public function create_path($path)
  8878. {
  8879. $path_bits = explode('/', dirname($path));
  8880. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8881. $path_built = IS_WINDOWS_OS ? '' : '/';
  8882. foreach ($path_bits as $bit) {
  8883. if (!empty ($bit)) {
  8884. $new_path = $path_built . $bit;
  8885. if (is_dir($new_path)) {
  8886. $path_built = $new_path . '/';
  8887. } else {
  8888. mkdir($new_path, api_get_permissions_for_new_directories());
  8889. $path_built = $new_path . '/';
  8890. }
  8891. }
  8892. }
  8893. }
  8894. /**
  8895. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8896. * @return boolean The results of the unlink function, or false if there was no image to start with
  8897. */
  8898. public function delete_lp_image()
  8899. {
  8900. $img = $this->get_preview_image();
  8901. if ($img != '') {
  8902. $del_file = $this->get_preview_image_path(null, 'sys');
  8903. if (isset($del_file) && file_exists($del_file)) {
  8904. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8905. if (file_exists($del_file_2)) {
  8906. unlink($del_file_2);
  8907. }
  8908. $this->set_preview_image('');
  8909. return @unlink($del_file);
  8910. }
  8911. }
  8912. return false;
  8913. }
  8914. /**
  8915. * Uploads an author image to the upload/learning_path/images directory
  8916. * @param array The image array, coming from the $_FILES superglobal
  8917. * @return boolean True on success, false on error
  8918. */
  8919. public function upload_image($image_array)
  8920. {
  8921. $image_moved = false;
  8922. if (!empty ($image_array['name'])) {
  8923. $upload_ok = process_uploaded_file($image_array);
  8924. $has_attachment = true;
  8925. } else {
  8926. $image_moved = true;
  8927. }
  8928. if ($upload_ok) {
  8929. if ($has_attachment) {
  8930. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8931. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8932. $updir = $sys_course_path . $courseDir;
  8933. // Try to add an extension to the file if it hasn't one.
  8934. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8935. if (!filter_extension($new_file_name)) {
  8936. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8937. $image_moved = false;
  8938. } else {
  8939. $file_extension = explode('.', $image_array['name']);
  8940. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8941. $filename = uniqid('');
  8942. $new_file_name = $filename.'.'.$file_extension;
  8943. $new_path = $updir.'/'.$new_file_name;
  8944. // Resize the image.
  8945. $temp = new Image($image_array['tmp_name']);
  8946. $picture_infos = $temp->get_image_info();
  8947. if ($picture_infos['width'] > 104) {
  8948. $thumbwidth = 104;
  8949. } else {
  8950. $thumbwidth = $picture_infos['width'];
  8951. }
  8952. if ($picture_infos['height'] > 96) {
  8953. $new_height = 96;
  8954. } else {
  8955. $new_height = $picture_infos['height'];
  8956. }
  8957. $temp->resize($thumbwidth, $new_height, 0);
  8958. $result = $temp->send_image($new_path);
  8959. // Storing the image filename.
  8960. if ($result) {
  8961. $image_moved = true;
  8962. $this->set_preview_image($new_file_name);
  8963. //Resize to 64px to use on course homepage
  8964. $temp->resize(64, 64, 0);
  8965. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8966. return true;
  8967. }
  8968. }
  8969. }
  8970. }
  8971. return false;
  8972. }
  8973. /**
  8974. * @param int $lp_id
  8975. * @param string $status
  8976. */
  8977. public function set_autolaunch($lp_id, $status)
  8978. {
  8979. $course_id = api_get_course_int_id();
  8980. $lp_id = intval($lp_id);
  8981. $status = intval($status);
  8982. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8983. // Setting everything to autolaunch = 0
  8984. $attributes['autolaunch'] = 0;
  8985. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8986. Database::update($lp_table, $attributes, $where);
  8987. if ($status == 1) {
  8988. //Setting my lp_id to autolaunch = 1
  8989. $attributes['autolaunch'] = 1;
  8990. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8991. Database::update($lp_table, $attributes, $where );
  8992. }
  8993. }
  8994. /**
  8995. * Gets previous_item_id for the next element of the lp_item table
  8996. * @author Isaac flores paz
  8997. * @return integer Previous item ID
  8998. */
  8999. public function select_previous_item_id()
  9000. {
  9001. $course_id = api_get_course_int_id();
  9002. if ($this->debug > 0) {
  9003. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9004. }
  9005. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9006. // Get the max order of the items
  9007. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9008. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9009. $rs_max_order = Database::query($sql_max_order);
  9010. $row_max_order = Database::fetch_object($rs_max_order);
  9011. $max_order = $row_max_order->display_order;
  9012. // Get the previous item ID
  9013. $sql = "SELECT id as previous FROM $table_lp_item
  9014. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9015. $rs_max = Database::query($sql);
  9016. $row_max = Database::fetch_object($rs_max);
  9017. // Return the previous item ID
  9018. return $row_max->previous;
  9019. }
  9020. /**
  9021. * Copies an LP
  9022. */
  9023. public function copy()
  9024. {
  9025. $main_path = api_get_path(SYS_CODE_PATH);
  9026. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9027. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9028. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9029. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9030. //Course builder
  9031. $cb = new CourseBuilder();
  9032. //Setting tools that will be copied
  9033. $cb->set_tools_to_build(array('learnpaths'));
  9034. //Setting elements that will be copied
  9035. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9036. $course = $cb->build();
  9037. //Course restorer
  9038. $course_restorer = new CourseRestorer($course);
  9039. $course_restorer->set_add_text_in_items(true);
  9040. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9041. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9042. }
  9043. public function verify_document_size($s)
  9044. {
  9045. $post_max = ini_get('post_max_size');
  9046. $upl_max = ini_get('upload_max_filesize');
  9047. $documents_total_space = DocumentManager::documents_total_space();
  9048. $course_max_space = DocumentManager::get_course_quota();
  9049. $total_size = filesize($s) + $documents_total_space;
  9050. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9051. return true;
  9052. } else{
  9053. return false;
  9054. }
  9055. }
  9056. /**
  9057. * Clear LP prerequisites
  9058. */
  9059. public function clear_prerequisites()
  9060. {
  9061. $course_id = $this->get_course_int_id();
  9062. if ($this->debug > 0) {
  9063. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9064. }
  9065. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9066. $lp_id = $this->get_id();
  9067. //Cleaning prerequisites
  9068. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9069. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9070. Database::query($sql);
  9071. //Cleaning mastery score for exercises
  9072. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9073. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9074. Database::query($sql);
  9075. }
  9076. public function set_previous_step_as_prerequisite_for_all_items()
  9077. {
  9078. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9079. $course_id = $this->get_course_int_id();
  9080. $lp_id = $this->get_id();
  9081. if (!empty($this->items)) {
  9082. $previous_item_id = null;
  9083. $previous_item_max = 0;
  9084. $previous_item_type = null;
  9085. $last_item_not_chapter = null;
  9086. $last_item_not_chapter_type = null;
  9087. $last_item_not_chapter_max = null;
  9088. foreach ($this->items as $item) {
  9089. // if there was a previous item... (otherwise jump to set it)
  9090. if (!empty($previous_item_id)) {
  9091. $current_item_id = $item->get_id(); //save current id
  9092. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9093. // Current item is not a folder, so it qualifies to get a prerequisites
  9094. if ($last_item_not_chapter_type == 'quiz') {
  9095. // if previous is quiz, mark its max score as default score to be achieved
  9096. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9097. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9098. Database::query($sql);
  9099. }
  9100. // now simply update the prerequisite to set it to the last non-chapter item
  9101. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9102. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9103. Database::query($sql);
  9104. // record item as 'non-chapter' reference
  9105. $last_item_not_chapter = $item->get_id();
  9106. $last_item_not_chapter_type = $item->get_type();
  9107. $last_item_not_chapter_max = $item->get_max();
  9108. }
  9109. } else {
  9110. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9111. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9112. $last_item_not_chapter = $item->get_id();
  9113. $last_item_not_chapter_type = $item->get_type();
  9114. $last_item_not_chapter_max = $item->get_max();
  9115. }
  9116. }
  9117. // Saving the item as "previous item" for the next loop
  9118. $previous_item_id = $item->get_id();
  9119. $previous_item_max = $item->get_max();
  9120. $previous_item_type = $item->get_type();
  9121. }
  9122. }
  9123. }
  9124. /**
  9125. * @param array $params
  9126. */
  9127. public static function createCategory($params)
  9128. {
  9129. $em = Database::getManager();
  9130. $item = new CLpCategory();
  9131. $item->setName($params['name']);
  9132. $item->setCId($params['c_id']);
  9133. $em->persist($item);
  9134. $em->flush();
  9135. }
  9136. /**
  9137. * @param array $params
  9138. */
  9139. public static function updateCategory($params)
  9140. {
  9141. $em = Database::getManager();
  9142. /** @var CLpCategory $item */
  9143. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9144. if ($item) {
  9145. $item->setName($params['name']);
  9146. $em->merge($item);
  9147. $em->flush();
  9148. }
  9149. }
  9150. /**
  9151. * @param int $id
  9152. */
  9153. public static function moveUpCategory($id)
  9154. {
  9155. $em = Database::getManager();
  9156. /** @var CLpCategory $item */
  9157. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9158. if ($item) {
  9159. $position = $item->getPosition() - 1;
  9160. $item->setPosition($position);
  9161. $em->persist($item);
  9162. $em->flush();
  9163. }
  9164. }
  9165. /**
  9166. * @param int $id
  9167. */
  9168. public static function moveDownCategory($id)
  9169. {
  9170. $em = Database::getManager();
  9171. /** @var CLpCategory $item */
  9172. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9173. if ($item) {
  9174. $position = $item->getPosition() + 1;
  9175. $item->setPosition($position);
  9176. $em->persist($item);
  9177. $em->flush();
  9178. }
  9179. }
  9180. /**
  9181. * @param int $courseId
  9182. * @return int|mixed
  9183. */
  9184. public static function getCountCategories($courseId)
  9185. {
  9186. if (empty($course_id)) {
  9187. return 0;
  9188. }
  9189. $em = Database::getManager();
  9190. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9191. $query->setParameter('id', $courseId);
  9192. return $query->getSingleScalarResult();
  9193. }
  9194. /**
  9195. * @param int $courseId
  9196. *
  9197. * @return mixed
  9198. */
  9199. public static function getCategories($courseId)
  9200. {
  9201. $em = Database::getManager();
  9202. //Default behaviour
  9203. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9204. array('cId' => $course_id),
  9205. array('name' => 'ASC')
  9206. );*/
  9207. // Using doctrine extensions
  9208. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9209. array('cId' => $courseId)
  9210. )->getResult();
  9211. return $items;
  9212. }
  9213. /**
  9214. * @param int $id
  9215. *
  9216. * @return mixed
  9217. */
  9218. public static function getCategory($id)
  9219. {
  9220. $em = Database::getManager();
  9221. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9222. return $item;
  9223. }
  9224. /**
  9225. * @param int $courseId
  9226. * @return array
  9227. */
  9228. public static function getCategoryByCourse($courseId)
  9229. {
  9230. $em = Database::getManager();
  9231. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9232. return $items;
  9233. }
  9234. /**
  9235. * @param int $id
  9236. *
  9237. * @return mixed
  9238. */
  9239. public static function deleteCategory($id)
  9240. {
  9241. $em = Database::getManager();
  9242. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9243. if ($item) {
  9244. $courseId = $item->getCId();
  9245. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9246. $query->setParameter('id', $courseId);
  9247. $query->setParameter('catId', $item->getId());
  9248. $lps = $query->getResult();
  9249. // Setting category = 0.
  9250. if ($lps) {
  9251. foreach ($lps as $lpItem) {
  9252. $lpItem->setCategoryId(0);
  9253. }
  9254. }
  9255. // Removing category.
  9256. $em->remove($item);
  9257. $em->flush();
  9258. }
  9259. }
  9260. /**
  9261. * @param int $courseId
  9262. * @param bool $addSelectOption
  9263. *
  9264. * @return mixed
  9265. */
  9266. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9267. {
  9268. $items = self::getCategoryByCourse($courseId);
  9269. $cats = array();
  9270. if ($addSelectOption) {
  9271. $cats = array(get_lang('SelectACategory'));
  9272. }
  9273. if (!empty($items)) {
  9274. foreach ($items as $cat) {
  9275. $cats[$cat->getId()] = $cat->getName();
  9276. }
  9277. }
  9278. return $cats;
  9279. }
  9280. /**
  9281. * Return the scorm item type object with spaces replaced with _
  9282. * The return result is use to build a css classname like scorm_type_$return
  9283. * @param $in_type
  9284. * @return mixed
  9285. */
  9286. private static function format_scorm_type_item($in_type)
  9287. {
  9288. return str_replace(' ', '_', $in_type);
  9289. }
  9290. /**
  9291. * @return \learnpath
  9292. */
  9293. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9294. {
  9295. $lpObject = Session::read('lpobject');
  9296. $learnPath = null;
  9297. if (isset($lpObject)) {
  9298. $learnPath = unserialize($lpObject);
  9299. }
  9300. if (!is_object($learnPath)) {
  9301. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9302. }
  9303. return $learnPath;
  9304. }
  9305. /**
  9306. * @param int $itemId
  9307. * @return learnpathItem|false
  9308. */
  9309. public function getItem($itemId)
  9310. {
  9311. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9312. return $this->items[$itemId];
  9313. }
  9314. return false;
  9315. }
  9316. /**
  9317. * @return int
  9318. */
  9319. public function getCategoryId()
  9320. {
  9321. return $this->categoryId;
  9322. }
  9323. /**
  9324. * @param int $categoryId
  9325. * @return bool
  9326. */
  9327. public function setCategoryId($categoryId)
  9328. {
  9329. $this->categoryId = intval($categoryId);
  9330. $courseId = api_get_course_int_id();
  9331. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9332. $lp_id = $this->get_id();
  9333. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9334. WHERE c_id = $courseId AND id = $lp_id";
  9335. Database::query($sql);
  9336. return true;
  9337. }
  9338. /**
  9339. * Calculate the count of stars for a user
  9340. * @param int $lpId The learn path ID
  9341. * @param int $userId The user ID
  9342. * @param int $courseId The course ID
  9343. * @param int $sessionId Optional. The session ID
  9344. * @return int The count of stars
  9345. */
  9346. public function getCalculateStars()
  9347. {
  9348. $stars = 0;
  9349. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $this->lp_session_id);
  9350. if ($progress > 50) {
  9351. $stars++;
  9352. }
  9353. // Calculate stars chapters evaluation
  9354. $exercisesItems = $this->getExercisesItems();
  9355. if ($exercisesItems === false) {
  9356. return $stars;
  9357. }
  9358. $totalResult = 0;
  9359. foreach ($exercisesItems as $exerciseItem) {
  9360. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9361. $this->user_id,
  9362. $exerciseItem->ref,
  9363. $this->course_int_id,
  9364. $this->lp_session_id,
  9365. $this->lp_id,
  9366. $exerciseItem->db_id
  9367. );
  9368. $exerciseResult = 0;
  9369. foreach ($exerciseResultInfo as $result) {
  9370. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9371. }
  9372. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9373. $totalResult += $exerciseAverage;
  9374. }
  9375. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9376. if ($totalExerciseAverage >= 50) {
  9377. $stars++;
  9378. }
  9379. if ($totalExerciseAverage >= 80) {
  9380. $stars++;
  9381. }
  9382. // Calculate star for final evaluation
  9383. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9384. if ($finalEvaluationItem === false) {
  9385. return $stars;
  9386. }
  9387. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9388. $this->user_id,
  9389. $finalEvaluationItem->ref,
  9390. $this->course_int_id,
  9391. $this->lp_session_id,
  9392. $this->lp_id,
  9393. $finalEvaluationItem->db_id
  9394. );
  9395. $evaluationResult = 0;
  9396. foreach ($evaluationResultInfo as $result) {
  9397. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9398. }
  9399. $evaluationAverage = $evaluationResult / (count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1);
  9400. if ($evaluationAverage >= 80) {
  9401. $stars++;
  9402. }
  9403. return $stars;
  9404. }
  9405. /**
  9406. * Get the items of exercise type
  9407. * @return array The items. Otherwise return false
  9408. */
  9409. public function getExercisesItems()
  9410. {
  9411. $exercises = [];
  9412. foreach ($this->items as $item) {
  9413. if ($item->type != 'quiz') {
  9414. continue;
  9415. }
  9416. $exercises[] = $item;
  9417. }
  9418. array_pop($exercises);
  9419. return $exercises;
  9420. }
  9421. /**
  9422. * Get the item of exercise type (evaluation type)
  9423. * @return array The final evaluation. Otherwise return false
  9424. */
  9425. public function getFinalEvaluationItem()
  9426. {
  9427. $exercises = [];
  9428. foreach ($this->items as $item) {
  9429. if ($item->type != 'quiz') {
  9430. continue;
  9431. }
  9432. $exercises[] = $item;
  9433. }
  9434. return array_pop($exercises);
  9435. }
  9436. /**
  9437. * Calculate the total points achieved for the current user in this learning path
  9438. * @return int
  9439. */
  9440. public function getCalculateScore()
  9441. {
  9442. // Calculate stars chapters evaluation
  9443. $exercisesItems = $this->getExercisesItems();
  9444. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9445. $totalExercisesResult = 0;
  9446. $totalEvaluationResult = 0;
  9447. if ($exercisesItems !== false) {
  9448. foreach ($exercisesItems as $exerciseItem) {
  9449. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9450. $this->user_id,
  9451. $exerciseItem->ref,
  9452. $this->course_int_id,
  9453. $this->lp_session_id,
  9454. $this->lp_id,
  9455. $exerciseItem->db_id
  9456. );
  9457. $exerciseResult = 0;
  9458. foreach ($exerciseResultInfo as $result) {
  9459. $exerciseResult += $result['exe_result'];
  9460. }
  9461. $totalExercisesResult += $exerciseResult;
  9462. }
  9463. }
  9464. if ($finalEvaluationItem !== false) {
  9465. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9466. $this->user_id,
  9467. $finalEvaluationItem->ref,
  9468. $this->course_int_id,
  9469. $this->lp_session_id,
  9470. $this->lp_id,
  9471. $finalEvaluationItem->db_id
  9472. );
  9473. foreach ($evaluationResultInfo as $result) {
  9474. $totalEvaluationResult += $result['exe_result'];
  9475. }
  9476. }
  9477. return $totalExercisesResult + $totalEvaluationResult;
  9478. }
  9479. }
  9480. if (!function_exists('trim_value')) {
  9481. function trim_value(& $value) {
  9482. $value = trim($value);
  9483. }
  9484. }