learnpath.class.php 441 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387
  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 $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  202. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  203. Database::query($sql);
  204. $this->lp_view_id = Database::insert_id();
  205. if ($this->debug > 2) {
  206. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  207. }
  208. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  209. Database::query($sql);
  210. }
  211. // Initialise items.
  212. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  213. $sql = "SELECT * FROM $lp_item_table
  214. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  215. ORDER BY parent_item_id, display_order";
  216. $res = Database::query($sql);
  217. if ($this->debug > 2) {
  218. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  219. error_log('-- Start while--', 0);
  220. }
  221. $lp_item_id_list = array();
  222. while ($row = Database::fetch_array($res)) {
  223. $lp_item_id_list[] = $row['id'];
  224. switch ($this->type) {
  225. case 3: //aicc
  226. $oItem = new aiccItem('db', $row['id'], $course_id);
  227. if (is_object($oItem)) {
  228. $my_item_id = $oItem->get_id();
  229. $oItem->set_lp_view($this->lp_view_id, $course_id);
  230. $oItem->set_prevent_reinit($this->prevent_reinit);
  231. // Don't use reference here as the next loop will make the pointed object change.
  232. $this->items[$my_item_id] = $oItem;
  233. $this->refs_list[$oItem->ref] = $my_item_id;
  234. if ($this->debug > 2) {
  235. error_log(
  236. 'New LP - learnpath::__construct() - ' .
  237. 'aicc object with id ' . $my_item_id .
  238. ' set in items[]', 0);
  239. }
  240. }
  241. break;
  242. case 2:
  243. $oItem = new scormItem('db', $row['id'], $course_id);
  244. if (is_object($oItem)) {
  245. $my_item_id = $oItem->get_id();
  246. $oItem->set_lp_view($this->lp_view_id, $course_id);
  247. $oItem->set_prevent_reinit($this->prevent_reinit);
  248. // Don't use reference here as the next loop will make the pointed object change.
  249. $this->items[$my_item_id] = $oItem;
  250. $this->refs_list[$oItem->ref] = $my_item_id;
  251. if ($this->debug > 2) {
  252. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  253. }
  254. }
  255. break;
  256. case 1:
  257. default:
  258. if ($this->debug > 2) {
  259. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  260. }
  261. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  262. if ($this->debug > 2) {
  263. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  264. }
  265. if (is_object($oItem)) {
  266. $my_item_id = $oItem->get_id();
  267. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  268. $oItem->set_prevent_reinit($this->prevent_reinit);
  269. // Don't use reference here as the next loop will make the pointed object change.
  270. $this->items[$my_item_id] = $oItem;
  271. $this->refs_list[$my_item_id] = $my_item_id;
  272. if ($this->debug > 2) {
  273. error_log(
  274. 'New LP - learnpath::__construct() ' . __LINE__ .
  275. ' - object with id ' . $my_item_id . ' set in items[]',
  276. 0);
  277. }
  278. }
  279. break;
  280. }
  281. // Setting the object level with variable $this->items[$i][parent]
  282. foreach ($this->items as $itemLPObject) {
  283. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  284. $itemLPObject->level = $level;
  285. }
  286. // Setting the view in the item object.
  287. if (is_object($this->items[$row['id']])) {
  288. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  289. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  290. $this->items[$row['id']]->current_start_time = 0;
  291. $this->items[$row['id']]->current_stop_time = 0;
  292. }
  293. }
  294. }
  295. if ($this->debug > 2) {
  296. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  297. }
  298. if (!empty($lp_item_id_list)) {
  299. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  300. if (!empty($lp_item_id_list_to_string)) {
  301. // Get last viewing vars.
  302. $lp_item_view_table = Database:: get_course_table(
  303. TABLE_LP_ITEM_VIEW
  304. );
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $lp_item_view_table
  308. WHERE
  309. c_id = $course_id AND
  310. lp_view_id = ".$this->lp_view_id." AND
  311. lp_item_id IN ('".$lp_item_id_list_to_string."')
  312. ORDER BY view_count DESC ";
  313. if ($this->debug > 2) {
  314. error_log(
  315. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  316. 0
  317. );
  318. }
  319. $status_list = array();
  320. $res = Database::query($sql);
  321. while ($row = Database:: fetch_array($res)) {
  322. $status_list[$row['lp_item_id']] = $row['status'];
  323. }
  324. foreach ($lp_item_id_list as $item_id) {
  325. if (isset($status_list[$item_id])) {
  326. $status = $status_list[$item_id];
  327. if (is_object($this->items[$item_id])) {
  328. $this->items[$item_id]->set_status($status);
  329. if (empty($status)) {
  330. $this->items[$item_id]->set_status(
  331. $this->default_status
  332. );
  333. }
  334. }
  335. } else {
  336. if (!api_is_invitee()) {
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  343. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  344. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  345. if ($this->debug > 2) {
  346. error_log(
  347. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  348. 0
  349. );
  350. }
  351. $this->items[$item_id]->set_lp_view(
  352. $this->lp_view_id,
  353. $course_id
  354. );
  355. Database::query($sql);
  356. $insertId = Database::insert_id();
  357. $sql = "UPDATE $lp_item_view_table SET id = iid
  358. WHERE iid = $insertId";
  359. Database::query($sql);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. $this->ordered_items = self::get_flat_ordered_items_list(
  366. $this->get_id(),
  367. 0,
  368. $course_id
  369. );
  370. $this->max_ordered_items = 0;
  371. foreach ($this->ordered_items as $index => $dummy) {
  372. if ($index > $this->max_ordered_items && !empty($dummy)) {
  373. $this->max_ordered_items = $index;
  374. }
  375. }
  376. // TODO: Define the current item better.
  377. $this->first();
  378. if ($this->debug > 2) {
  379. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  380. }
  381. return true;
  382. }
  383. /**
  384. * @return string
  385. */
  386. public function getCourseCode()
  387. {
  388. return $this->cc;
  389. }
  390. /**
  391. * @return int
  392. */
  393. public function get_course_int_id()
  394. {
  395. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  396. }
  397. /**
  398. * @param $course_id
  399. * @return int
  400. */
  401. public function set_course_int_id($course_id)
  402. {
  403. return $this->course_int_id = intval($course_id);
  404. }
  405. /**
  406. * Get the depth level of LP item
  407. * @param $in_tab_items
  408. * @param $in_current_item_id
  409. * @return int
  410. */
  411. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  412. {
  413. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  414. if ($parent_item_id == 0) {
  415. return 0;
  416. } else {
  417. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  418. }
  419. }
  420. /**
  421. * Function rewritten based on old_add_item() from Yannick Warnier.
  422. * Due the fact that users can decide where the item should come, I had to overlook this function and
  423. * I found it better to rewrite it. Old function is still available.
  424. * Added also the possibility to add a description.
  425. *
  426. * @param int $parent
  427. * @param int $previous
  428. * @param string $type
  429. * @param int resource ID (ref)
  430. * @param string $title
  431. * @param string $description
  432. * @param int $prerequisites
  433. * @param int $max_time_allowed
  434. * @return int
  435. */
  436. public function add_item(
  437. $parent,
  438. $previous,
  439. $type = 'dokeos_chapter',
  440. $id,
  441. $title,
  442. $description,
  443. $prerequisites = 0,
  444. $max_time_allowed = 0
  445. ) {
  446. $course_id = api_get_course_int_id();
  447. if ($this->debug > 0) {
  448. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  449. }
  450. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  451. $_course = api_get_course_info();
  452. $parent = intval($parent);
  453. $previous = intval($previous);
  454. $id = intval($id);
  455. $max_time_allowed = htmlentities($max_time_allowed);
  456. if (empty($max_time_allowed)) {
  457. $max_time_allowed = 0;
  458. }
  459. $sql = "SELECT COUNT(id) AS num
  460. FROM $tbl_lp_item
  461. WHERE
  462. c_id = $course_id AND
  463. lp_id = " . $this->get_id() . " AND
  464. parent_item_id = " . $parent;
  465. $res_count = Database::query($sql);
  466. $row = Database :: fetch_array($res_count);
  467. $num = $row['num'];
  468. if ($num > 0) {
  469. if ($previous == 0) {
  470. $sql = "SELECT id, next_item_id, display_order
  471. FROM " . $tbl_lp_item . "
  472. WHERE
  473. c_id = $course_id AND
  474. lp_id = " . $this->get_id() . " AND
  475. parent_item_id = " . $parent . " AND
  476. previous_item_id = 0 OR
  477. previous_item_id=" . $parent;
  478. $result = Database::query($sql);
  479. $row = Database :: fetch_array($result);
  480. $tmp_previous = 0;
  481. $next = $row['id'];
  482. $display_order = 0;
  483. } else {
  484. $previous = (int) $previous;
  485. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  486. FROM $tbl_lp_item
  487. WHERE
  488. c_id = $course_id AND
  489. lp_id = " . $this->get_id() . " AND
  490. id = " . $previous;
  491. $result = Database::query($sql);
  492. $row = Database:: fetch_array($result);
  493. $tmp_previous = $row['id'];
  494. $next = $row['next_item_id'];
  495. $display_order = $row['display_order'];
  496. }
  497. } else {
  498. $tmp_previous = 0;
  499. $next = 0;
  500. $display_order = 0;
  501. }
  502. $id = intval($id);
  503. $typeCleaned = Database::escape_string($type);
  504. if ($type == 'quiz') {
  505. $sql = 'SELECT SUM(ponderation)
  506. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  507. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  508. ON
  509. quiz_question.id = quiz_rel_question.question_id AND
  510. quiz_question.c_id = quiz_rel_question.c_id
  511. WHERE
  512. quiz_rel_question.exercice_id = '.$id." AND
  513. quiz_question.c_id = $course_id AND
  514. quiz_rel_question.c_id = $course_id ";
  515. $rsQuiz = Database::query($sql);
  516. $max_score = Database :: result($rsQuiz, 0, 0);
  517. // Disabling the exercise if we add it inside a LP
  518. $exercise = new Exercise();
  519. $exercise->read($id);
  520. $exercise->disable();
  521. $exercise->save();
  522. } else {
  523. $max_score = 100;
  524. }
  525. $params = array(
  526. "c_id" => $course_id,
  527. "lp_id" => $this->get_id(),
  528. "item_type" =>$typeCleaned ,
  529. "ref" => '',
  530. "title" =>$title ,
  531. "description" => $description,
  532. "path" => $id,
  533. "max_score" => $max_score,
  534. "parent_item_id" => $parent,
  535. "previous_item_id" => $previous,
  536. "next_item_id" => $next,
  537. "display_order" => $display_order +1,
  538. "prerequisite" => $prerequisites,
  539. "max_time_allowed" => $max_time_allowed
  540. );
  541. if ($prerequisites != 0) {
  542. $params['prerequisite'] = $prerequisites;
  543. }
  544. $new_item_id = Database::insert($tbl_lp_item, $params);
  545. if ($this->debug > 2) {
  546. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  547. }
  548. if ($new_item_id) {
  549. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  550. Database::query($sql);
  551. // Update the item that should come after the new item.
  552. $sql = " UPDATE $tbl_lp_item SET
  553. previous_item_id = $new_item_id,
  554. next_item_id = $new_item_id,
  555. id = $new_item_id
  556. WHERE iid = $new_item_id";
  557. Database::query($sql);
  558. // Update all the items after the new item.
  559. $sql = "UPDATE " . $tbl_lp_item . "
  560. SET display_order = display_order + 1
  561. WHERE
  562. c_id = $course_id AND
  563. lp_id = " . $this->get_id() . " AND
  564. id <> " . $new_item_id . " AND
  565. parent_item_id = " . $parent . " AND
  566. display_order > " . $display_order;
  567. Database::query($sql);
  568. // Update the item that should come after the new item.
  569. $sql = "UPDATE " . $tbl_lp_item . "
  570. SET ref = " . $new_item_id . "
  571. WHERE c_id = $course_id AND id = " . $new_item_id;
  572. Database::query($sql);
  573. // Upload audio.
  574. if (!empty($_FILES['mp3']['name'])) {
  575. // Create the audio folder if it does not exist yet.
  576. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  577. if (!is_dir($filepath . 'audio')) {
  578. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  579. $audio_id = add_document(
  580. $_course,
  581. '/audio',
  582. 'folder',
  583. 0,
  584. 'audio'
  585. );
  586. api_item_property_update(
  587. $_course,
  588. TOOL_DOCUMENT,
  589. $audio_id,
  590. 'FolderCreated',
  591. api_get_user_id(),
  592. null,
  593. null,
  594. null,
  595. null,
  596. api_get_session_id()
  597. );
  598. api_item_property_update(
  599. $_course,
  600. TOOL_DOCUMENT,
  601. $audio_id,
  602. 'invisible',
  603. api_get_user_id(),
  604. null,
  605. null,
  606. null,
  607. null,
  608. api_get_session_id()
  609. );
  610. }
  611. $file_path = handle_uploaded_document(
  612. $_course,
  613. $_FILES['mp3'],
  614. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  615. '/audio',
  616. api_get_user_id(),
  617. '',
  618. '',
  619. '',
  620. '',
  621. false
  622. );
  623. // Getting the filename only.
  624. $file_components = explode('/', $file_path);
  625. $file = $file_components[count($file_components) - 1];
  626. // Store the mp3 file in the lp_item table.
  627. $sql = "UPDATE $tbl_lp_item SET
  628. audio = '" . Database::escape_string($file) . "'
  629. WHERE id = '" . intval($new_item_id) . "'";
  630. Database::query($sql);
  631. }
  632. }
  633. return $new_item_id;
  634. }
  635. /**
  636. * Static admin function allowing addition of a learnpath to a course.
  637. * @param string Course code
  638. * @param string Learnpath name
  639. * @param string Learnpath description string, if provided
  640. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  641. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  642. * @param string Zip file containing the learnpath or directory containing the learnpath
  643. * @return integer The new learnpath ID on success, 0 on failure
  644. */
  645. public static function add_lp(
  646. $course,
  647. $name,
  648. $description = '',
  649. $learnpath = 'guess',
  650. $origin = 'zip',
  651. $zipname = '',
  652. $publicated_on = '',
  653. $expired_on = '',
  654. $categoryId = 0
  655. ) {
  656. global $charset;
  657. $course_id = api_get_course_int_id();
  658. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  659. // Check course code exists.
  660. // Check lp_name doesn't exist, otherwise append something.
  661. $i = 0;
  662. $name = Database::escape_string($name);
  663. $categoryId = intval($categoryId);
  664. // Session id.
  665. $session_id = api_get_session_id();
  666. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  667. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  668. $res_name = Database::query($check_name);
  669. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  670. //by default the publication date is the same that the creation date
  671. //The behaviour above was changed due BT#2800
  672. global $_custom;
  673. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  674. $publicated_on = '';
  675. } else {
  676. $publicated_on = api_get_utc_datetime();
  677. }
  678. } else {
  679. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  680. }
  681. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  682. $expired_on = '';
  683. } else {
  684. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  685. }
  686. while (Database :: num_rows($res_name)) {
  687. // There is already one such name, update the current one a bit.
  688. $i++;
  689. $name = $name . ' - ' . $i;
  690. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  691. $res_name = Database::query($check_name);
  692. }
  693. // New name does not exist yet; keep it.
  694. // Escape description.
  695. // Kevin: added htmlentities().
  696. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  697. $type = 1;
  698. switch ($learnpath) {
  699. case 'guess':
  700. break;
  701. case 'dokeos':
  702. case 'chamilo':
  703. $type = 1;
  704. break;
  705. case 'aicc':
  706. break;
  707. }
  708. switch ($origin) {
  709. case 'zip':
  710. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  711. break;
  712. case 'manual':
  713. default:
  714. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  715. $res_max = Database::query($get_max);
  716. if (Database :: num_rows($res_max) < 1) {
  717. $dsp = 1;
  718. } else {
  719. $row = Database :: fetch_array($res_max);
  720. $dsp = $row[0] + 1;
  721. }
  722. $params = [
  723. 'c_id' => $course_id,
  724. 'lp_type' => $type,
  725. 'name' => $name,
  726. 'description' => $description,
  727. 'path' => '',
  728. 'default_view_mod' => 'embedded',
  729. 'default_encoding' => 'UTF-8',
  730. 'display_order' => $dsp,
  731. 'content_maker' => 'Chamilo',
  732. 'content_local' => 'local',
  733. 'js_lib' => '',
  734. 'session_id' => $session_id,
  735. 'created_on' => api_get_utc_datetime(),
  736. 'publicated_on' => $publicated_on,
  737. 'expired_on' => $expired_on,
  738. 'category_id' => $categoryId
  739. ];
  740. $id = Database::insert($tbl_lp, $params);
  741. if ($id > 0) {
  742. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  743. Database::query($sql);
  744. $course_info = api_get_course_info();
  745. // Insert into item_property.
  746. api_item_property_update(
  747. $course_info,
  748. TOOL_LEARNPATH,
  749. $id,
  750. 'LearnpathAdded',
  751. api_get_user_id()
  752. );
  753. api_set_default_visibility($id, TOOL_LEARNPATH);
  754. return $id;
  755. }
  756. break;
  757. }
  758. }
  759. /**
  760. * Appends a message to the message attribute
  761. * @param string $string Message to append.
  762. */
  763. public function append_message($string)
  764. {
  765. if ($this->debug > 0) {
  766. error_log('New LP - In learnpath::append_message()', 0);
  767. }
  768. $this->message .= $string;
  769. }
  770. /**
  771. * Auto completes the parents of an item in case it's been completed or passed
  772. * @param integer $item Optional ID of the item from which to look for parents
  773. */
  774. public function autocomplete_parents($item)
  775. {
  776. $debug = $this->debug;
  777. if ($debug) {
  778. error_log('Learnpath::autocomplete_parents()', 0);
  779. }
  780. if (empty($item)) {
  781. $item = $this->current;
  782. }
  783. $currentItem = $this->getItem($item);
  784. if ($currentItem) {
  785. $parent_id = $currentItem->get_parent();
  786. $parent = $this->getItem($parent_id);
  787. if ($parent) {
  788. // if $item points to an object and there is a parent.
  789. if ($debug) {
  790. error_log(
  791. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  792. 0
  793. );
  794. }
  795. // New experiment including failed and browsed in completed status.
  796. //$current_status = $currentItem->get_status();
  797. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  798. // Fixes chapter auto complete
  799. if (true) {
  800. // If the current item is completed or passes or succeeded.
  801. $updateParentStatus = true;
  802. if ($debug) {
  803. error_log('Status of current item is alright', 0);
  804. }
  805. foreach ($parent->get_children() as $childItemId) {
  806. $childItem = $this->getItem($childItemId);
  807. // If children was not set try to get the info
  808. if (empty($childItem->db_item_view_id)) {
  809. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  810. }
  811. // Check all his brothers (parent's children) for completion status.
  812. if ($childItemId != $item) {
  813. if ($debug) {
  814. error_log(
  815. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  816. 0
  817. );
  818. }
  819. // Trying completing parents of failed and browsed items as well.
  820. if ($childItem->status_is(
  821. array(
  822. 'completed',
  823. 'passed',
  824. 'succeeded',
  825. 'browsed',
  826. 'failed'
  827. )
  828. )
  829. ) {
  830. // Keep completion status to true.
  831. continue;
  832. } else {
  833. if ($debug > 2) {
  834. error_log(
  835. '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,
  836. 0
  837. );
  838. }
  839. $updateParentStatus = false;
  840. break;
  841. }
  842. }
  843. }
  844. if ($updateParentStatus) {
  845. // If all the children were completed:
  846. $parent->set_status('completed');
  847. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  848. // Force the status to "completed"
  849. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  850. $this->update_queue[$parent->get_id()] = 'completed';
  851. if ($debug) {
  852. error_log(
  853. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  854. print_r($this->update_queue, 1),
  855. 0
  856. );
  857. }
  858. // Recursive call.
  859. $this->autocomplete_parents($parent->get_id());
  860. }
  861. }
  862. } else {
  863. if ($debug) {
  864. error_log("Parent #$parent_id does not exists");
  865. }
  866. }
  867. } else {
  868. if ($debug) {
  869. error_log("#$item is an item that doesn't have parents");
  870. }
  871. }
  872. }
  873. /**
  874. * Auto saves the current results into the database for the whole learnpath
  875. */
  876. public function autosave()
  877. {
  878. if ($this->debug > 0) {
  879. error_log('New LP - In learnpath::autosave()', 0);
  880. }
  881. // TODO: Add save operations for the learnpath itself.
  882. }
  883. /**
  884. * Clears the message attribute
  885. */
  886. public function clear_message()
  887. {
  888. if ($this->debug > 0) {
  889. error_log('New LP - In learnpath::clear_message()', 0);
  890. }
  891. $this->message = '';
  892. }
  893. /**
  894. * Closes the current resource
  895. *
  896. * Stops the timer
  897. * Saves into the database if required
  898. * Clears the current resource data from this object
  899. * @return boolean True on success, false on failure
  900. */
  901. public function close()
  902. {
  903. $course_id = api_get_course_int_id();
  904. if ($this->debug > 0) {
  905. error_log('New LP - In learnpath::close()', 0);
  906. }
  907. if (empty ($this->lp_id)) {
  908. $this->error = 'Trying to close this learnpath but no ID is set';
  909. return false;
  910. }
  911. $this->current_time_stop = time();
  912. $this->ordered_items = array();
  913. $this->index = 0;
  914. unset ($this->lp_id);
  915. //unset other stuff
  916. return true;
  917. }
  918. /**
  919. * Static admin function allowing removal of a learnpath
  920. * @param string Course code
  921. * @param integer Learnpath ID
  922. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  923. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  924. */
  925. public function delete($course = null, $id = null, $delete = 'keep')
  926. {
  927. $course_id = api_get_course_int_id();
  928. // TODO: Implement a way of getting this to work when the current object is not set.
  929. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  930. // If an ID is specifically given and the current LP is not the same, prevent delete.
  931. if (!empty ($id) && ($id != $this->lp_id)) {
  932. return false;
  933. }
  934. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  935. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  936. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  937. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  938. // Delete lp item id.
  939. foreach ($this->items as $id => $dummy) {
  940. $sql = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  941. Database::query($sql);
  942. }
  943. // Proposed by Christophe (nickname: clefevre)
  944. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  945. Database::query($sql);
  946. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  947. Database::query($sql);
  948. self::toggle_publish($this->lp_id, 'i');
  949. if ($this->type == 2 || $this->type == 3) {
  950. // This is a scorm learning path, delete the files as well.
  951. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  952. $res = Database::query($sql);
  953. if (Database :: num_rows($res) > 0) {
  954. $row = Database :: fetch_array($res);
  955. $path = $row['path'];
  956. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  957. $res = Database::query($sql);
  958. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  959. if ($this->debug > 2) {
  960. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  961. }
  962. } else {
  963. // No other LP uses that directory, delete it.
  964. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  965. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  966. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  967. if ($this->debug > 2) {
  968. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  969. }
  970. // Proposed by Christophe (clefevre).
  971. if (strcmp(substr($path, -2), "/.") == 0) {
  972. $path = substr($path, 0, -1); // Remove "." at the end.
  973. }
  974. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  975. rmdirr($course_scorm_dir . $path);
  976. }
  977. }
  978. }
  979. }
  980. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  981. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  982. // Delete tools
  983. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  984. Database::query($sql);
  985. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  986. Database::query($sql);
  987. // Updates the display order of all lps.
  988. $this->update_display_order();
  989. api_item_property_update(
  990. api_get_course_info(),
  991. TOOL_LEARNPATH,
  992. $this->lp_id,
  993. 'delete',
  994. api_get_user_id()
  995. );
  996. // Delete link of gradebook tool
  997. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  998. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  999. $result = Database::query($sql);
  1000. $row = Database :: fetch_array($result, 'ASSOC');*/
  1001. // Fixing gradebook link deleted see #5229.
  1002. /*
  1003. if (!empty($row['id'])) {
  1004. $link = LinkFactory :: load($row['id']);
  1005. if ($link[0] != null) {
  1006. $link[0]->delete();
  1007. }
  1008. }*/
  1009. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1010. if ($link_info !== false) {
  1011. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1012. }
  1013. if (api_get_setting('search_enabled') == 'true') {
  1014. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1015. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1016. }
  1017. }
  1018. /**
  1019. * Removes all the children of one item - dangerous!
  1020. * @param integer $id Element ID of which children have to be removed
  1021. * @return integer Total number of children removed
  1022. */
  1023. public function delete_children_items($id)
  1024. {
  1025. $course_id = api_get_course_int_id();
  1026. if ($this->debug > 0) {
  1027. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1028. }
  1029. $num = 0;
  1030. if (empty ($id) || $id != strval(intval($id))) {
  1031. return false;
  1032. }
  1033. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1034. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1035. $res = Database::query($sql);
  1036. while ($row = Database :: fetch_array($res)) {
  1037. $num += $this->delete_children_items($row['id']);
  1038. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1039. Database::query($sql_del);
  1040. $num++;
  1041. }
  1042. return $num;
  1043. }
  1044. /**
  1045. * Removes an item from the current learnpath
  1046. * @param integer $id Elem ID (0 if first)
  1047. * @param integer $remove Whether to remove the resource/data from the
  1048. * system or leave it (default: 'keep', others 'remove')
  1049. * @return integer Number of elements moved
  1050. * @todo implement resource removal
  1051. */
  1052. public function delete_item($id, $remove = 'keep')
  1053. {
  1054. $course_id = api_get_course_int_id();
  1055. if ($this->debug > 0) {
  1056. error_log('New LP - In learnpath::delete_item()', 0);
  1057. }
  1058. // TODO: Implement the resource removal.
  1059. if (empty ($id) || $id != strval(intval($id))) {
  1060. return false;
  1061. }
  1062. // First select item to get previous, next, and display order.
  1063. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1064. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1065. $res_sel = Database::query($sql_sel);
  1066. if (Database :: num_rows($res_sel) < 1) {
  1067. return false;
  1068. }
  1069. $row = Database :: fetch_array($res_sel);
  1070. $previous = $row['previous_item_id'];
  1071. $next = $row['next_item_id'];
  1072. $display = $row['display_order'];
  1073. $parent = $row['parent_item_id'];
  1074. $lp = $row['lp_id'];
  1075. // Delete children items.
  1076. $num = $this->delete_children_items($id);
  1077. if ($this->debug > 2) {
  1078. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1079. }
  1080. // Now delete the item.
  1081. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1082. if ($this->debug > 2) {
  1083. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1084. }
  1085. Database::query($sql_del);
  1086. // Now update surrounding items.
  1087. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1088. WHERE c_id = ".$course_id." AND id = $previous";
  1089. Database::query($sql_upd);
  1090. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1091. WHERE c_id = ".$course_id." AND id = $next";
  1092. Database::query($sql_upd);
  1093. // Now update all following items with new display order.
  1094. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1095. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1096. Database::query($sql_all);
  1097. //Removing prerequisites since the item will not longer exist
  1098. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1099. Database::query($sql_all);
  1100. // Remove from search engine if enabled.
  1101. if (api_get_setting('search_enabled') == 'true') {
  1102. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1103. $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';
  1104. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1105. $res = Database::query($sql);
  1106. if (Database :: num_rows($res) > 0) {
  1107. $row2 = Database :: fetch_array($res);
  1108. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1109. $di = new ChamiloIndexer();
  1110. $di->remove_document((int) $row2['search_did']);
  1111. }
  1112. $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';
  1113. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1114. Database::query($sql);
  1115. }
  1116. }
  1117. /**
  1118. * Updates an item's content in place
  1119. * @param integer $id Element ID
  1120. * @param integer $parent Parent item ID
  1121. * @param integer $previous Previous item ID
  1122. * @param string $title Item title
  1123. * @param string $description Item description
  1124. * @param string $prerequisites Prerequisites (optional)
  1125. * @param array $audio The array resulting of the $_FILES[mp3] element
  1126. * @param int $max_time_allowed
  1127. * @param string $url
  1128. * @return boolean True on success, false on error
  1129. */
  1130. public function edit_item(
  1131. $id,
  1132. $parent,
  1133. $previous,
  1134. $title,
  1135. $description,
  1136. $prerequisites = '0',
  1137. $audio = array(),
  1138. $max_time_allowed = 0,
  1139. $url = ''
  1140. ) {
  1141. $course_id = api_get_course_int_id();
  1142. $_course = api_get_course_info();
  1143. if ($this->debug > 0) {
  1144. error_log('New LP - In learnpath::edit_item()', 0);
  1145. }
  1146. if (empty ($max_time_allowed)) {
  1147. $max_time_allowed = 0;
  1148. }
  1149. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1150. return false;
  1151. }
  1152. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1153. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1154. $res_select = Database::query($sql_select);
  1155. $row_select = Database :: fetch_array($res_select);
  1156. $audio_update_sql = '';
  1157. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1158. // Create the audio folder if it does not exist yet.
  1159. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1160. if (!is_dir($filepath . 'audio')) {
  1161. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1162. $audio_id = add_document(
  1163. $_course,
  1164. '/audio',
  1165. 'folder',
  1166. 0,
  1167. 'audio'
  1168. );
  1169. api_item_property_update(
  1170. $_course,
  1171. TOOL_DOCUMENT,
  1172. $audio_id,
  1173. 'FolderCreated',
  1174. api_get_user_id(),
  1175. null,
  1176. null,
  1177. null,
  1178. null,
  1179. api_get_session_id()
  1180. );
  1181. api_item_property_update(
  1182. $_course,
  1183. TOOL_DOCUMENT,
  1184. $audio_id,
  1185. 'invisible',
  1186. api_get_user_id(),
  1187. null,
  1188. null,
  1189. null,
  1190. null,
  1191. api_get_session_id()
  1192. );
  1193. }
  1194. // Upload file in documents.
  1195. $pi = pathinfo($audio['name']);
  1196. if ($pi['extension'] == 'mp3') {
  1197. $c_det = api_get_course_info($this->cc);
  1198. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1199. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1200. $path = substr($path, 7);
  1201. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1202. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1203. }
  1204. }
  1205. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1206. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1207. // TODO: htmlspecialchars to be checked for encoding related problems.
  1208. if ($same_parent && $same_previous) {
  1209. // Only update title and description.
  1210. $sql = "UPDATE " . $tbl_lp_item . "
  1211. SET title = '" . Database::escape_string($title) . "',
  1212. prerequisite = '" . $prerequisites . "',
  1213. description = '" . Database::escape_string($description) . "'
  1214. " . $audio_update_sql . ",
  1215. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1216. WHERE c_id = ".$course_id." AND id = " . $id;
  1217. Database::query($sql);
  1218. } else {
  1219. $old_parent = $row_select['parent_item_id'];
  1220. $old_previous = $row_select['previous_item_id'];
  1221. $old_next = $row_select['next_item_id'];
  1222. $old_order = $row_select['display_order'];
  1223. $old_prerequisite = $row_select['prerequisite'];
  1224. $old_max_time_allowed = $row_select['max_time_allowed'];
  1225. /* BEGIN -- virtually remove the current item id */
  1226. /* for the next and previous item it is like the current item doesn't exist anymore */
  1227. if ($old_previous != 0) {
  1228. // Next
  1229. $sql = "UPDATE " . $tbl_lp_item . "
  1230. SET next_item_id = " . $old_next . "
  1231. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1232. Database::query($sql);
  1233. }
  1234. if ($old_next != 0) {
  1235. // Previous
  1236. $sql = "UPDATE " . $tbl_lp_item . "
  1237. SET previous_item_id = " . $old_previous . "
  1238. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1239. Database::query($sql);
  1240. }
  1241. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1242. $sql = "UPDATE " . $tbl_lp_item . "
  1243. SET display_order = display_order - 1
  1244. WHERE
  1245. c_id = ".$course_id." AND
  1246. display_order > " . $old_order . " AND
  1247. lp_id = " . $this->lp_id . " AND
  1248. parent_item_id = " . $old_parent;
  1249. Database::query($sql);
  1250. /* END -- virtually remove the current item id */
  1251. /* BEGIN -- update the current item id to his new location */
  1252. if ($previous == 0) {
  1253. // Select the data of the item that should come after the current item.
  1254. $sql = "SELECT id, display_order
  1255. FROM " . $tbl_lp_item . "
  1256. WHERE
  1257. c_id = ".$course_id." AND
  1258. lp_id = " . $this->lp_id . " AND
  1259. parent_item_id = " . $parent . " AND
  1260. previous_item_id = " . $previous;
  1261. $res_select_old = Database::query($sql);
  1262. $row_select_old = Database::fetch_array($res_select_old);
  1263. // If the new parent didn't have children before.
  1264. if (Database :: num_rows($res_select_old) == 0) {
  1265. $new_next = 0;
  1266. $new_order = 1;
  1267. } else {
  1268. $new_next = $row_select_old['id'];
  1269. $new_order = $row_select_old['display_order'];
  1270. }
  1271. } else {
  1272. // Select the data of the item that should come before the current item.
  1273. $sql = "SELECT next_item_id, display_order
  1274. FROM " . $tbl_lp_item . "
  1275. WHERE c_id = ".$course_id." AND id = " . $previous;
  1276. $res_select_old = Database::query($sql);
  1277. $row_select_old = Database :: fetch_array($res_select_old);
  1278. $new_next = $row_select_old['next_item_id'];
  1279. $new_order = $row_select_old['display_order'] + 1;
  1280. }
  1281. // TODO: htmlspecialchars to be checked for encoding related problems.
  1282. // Update the current item with the new data.
  1283. $sql = "UPDATE " . $tbl_lp_item . "
  1284. SET
  1285. title = '" . Database::escape_string($title) . "',
  1286. description = '" . Database::escape_string($description) . "',
  1287. parent_item_id = " . $parent . ",
  1288. previous_item_id = " . $previous . ",
  1289. next_item_id = " . $new_next . ",
  1290. display_order = " . $new_order . "
  1291. " . $audio_update_sql . "
  1292. WHERE c_id = ".$course_id." AND id = " . $id;
  1293. Database::query($sql);
  1294. if ($previous != 0) {
  1295. // Update the previous item's next_item_id.
  1296. $sql = "UPDATE " . $tbl_lp_item . "
  1297. SET next_item_id = " . $id . "
  1298. WHERE c_id = ".$course_id." AND id = " . $previous;
  1299. Database::query($sql);
  1300. }
  1301. if ($new_next != 0) {
  1302. // Update the next item's previous_item_id.
  1303. $sql = "UPDATE " . $tbl_lp_item . "
  1304. SET previous_item_id = " . $id . "
  1305. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1306. Database::query($sql);
  1307. }
  1308. if ($old_prerequisite != $prerequisites) {
  1309. $sql = "UPDATE " . $tbl_lp_item . "
  1310. SET prerequisite = '" . $prerequisites . "'
  1311. WHERE c_id = ".$course_id." AND id = " . $id;
  1312. Database::query($sql);
  1313. }
  1314. if ($old_max_time_allowed != $max_time_allowed) {
  1315. // update max time allowed
  1316. $sql = "UPDATE " . $tbl_lp_item . "
  1317. SET max_time_allowed = " . $max_time_allowed . "
  1318. WHERE c_id = ".$course_id." AND id = " . $id;
  1319. Database::query($sql);
  1320. }
  1321. // Update all the items with the same or a bigger display_order than the current item.
  1322. $sql = "UPDATE " . $tbl_lp_item . "
  1323. SET display_order = display_order + 1
  1324. WHERE
  1325. c_id = ".$course_id." AND
  1326. lp_id = " . $this->get_id() . " AND
  1327. id <> " . $id . " AND
  1328. parent_item_id = " . $parent . " AND
  1329. display_order >= " . $new_order;
  1330. Database::query($sql);
  1331. }
  1332. if ($row_select['item_type'] == 'link') {
  1333. $link = new Link();
  1334. $linkId = $row_select['path'];
  1335. $link->updateLink($linkId, $url);
  1336. }
  1337. }
  1338. /**
  1339. * Updates an item's prereq in place
  1340. * @param integer $id Element ID
  1341. * @param string $prerequisite_id Prerequisite Element ID
  1342. * @param int $mastery_score Prerequisite min score
  1343. * @param int $max_score Prerequisite max score
  1344. *
  1345. * @return boolean True on success, false on error
  1346. */
  1347. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1348. {
  1349. $course_id = api_get_course_int_id();
  1350. if ($this->debug > 0) {
  1351. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1352. }
  1353. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1354. return false;
  1355. }
  1356. $prerequisite_id = intval($prerequisite_id);
  1357. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1358. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1359. $mastery_score = 0;
  1360. }
  1361. if (!is_numeric($max_score) || $max_score < 0) {
  1362. $max_score = 100;
  1363. }
  1364. /*if ($mastery_score > $max_score) {
  1365. $max_score = $mastery_score;
  1366. }*/
  1367. if (!is_numeric($prerequisite_id)) {
  1368. $prerequisite_id = 'NULL';
  1369. }
  1370. $mastery_score = floatval($mastery_score);
  1371. $max_score = floatval($max_score);
  1372. $sql = " UPDATE $tbl_lp_item
  1373. SET
  1374. prerequisite = $prerequisite_id ,
  1375. prerequisite_min_score = $mastery_score ,
  1376. prerequisite_max_score = $max_score
  1377. WHERE c_id = $course_id AND id = $id";
  1378. Database::query($sql);
  1379. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1380. // Will this be enough to ensure unicity?
  1381. /*$sql = " UPDATE $tbl_lp_item
  1382. SET mastery_score = $mastery_score
  1383. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1384. Database::query($sql);*/
  1385. }
  1386. // TODO: Update the item object (can be ignored for now because refreshed).
  1387. return true;
  1388. }
  1389. /**
  1390. * Escapes a string with the available database escape function
  1391. * @param string String to escape
  1392. * @return string String escaped
  1393. * @deprecated use Database::escape_string
  1394. */
  1395. public function escape_string($string)
  1396. {
  1397. return Database::escape_string($string);
  1398. }
  1399. /**
  1400. * Static admin function exporting a learnpath into a zip file
  1401. * @param string Export type (scorm, zip, cd)
  1402. * @param string Course code
  1403. * @param integer Learnpath ID
  1404. * @param string Zip file name
  1405. * @return string Zip file path (or false on error)
  1406. */
  1407. public function export_lp($type, $course, $id, $zipname)
  1408. {
  1409. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1410. return false;
  1411. }
  1412. $url = '';
  1413. switch ($type) {
  1414. case 'scorm':
  1415. break;
  1416. case 'zip':
  1417. break;
  1418. case 'cdrom':
  1419. break;
  1420. }
  1421. return $url;
  1422. }
  1423. /**
  1424. * Gets all the chapters belonging to the same parent as the item/chapter given
  1425. * Can also be called as abstract method
  1426. * @param integer Item ID
  1427. * @return array A list of all the "brother items" (or an empty array on failure)
  1428. */
  1429. public function get_brother_chapters($id)
  1430. {
  1431. $course_id = api_get_course_int_id();
  1432. if ($this->debug > 0) {
  1433. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1434. }
  1435. if (empty($id)|| $id != strval(intval($id))) {
  1436. return array ();
  1437. }
  1438. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1439. $sql_parent = "SELECT * FROM $lp_item
  1440. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1441. $res_parent = Database::query($sql_parent);
  1442. if (Database :: num_rows($res_parent) > 0) {
  1443. $row_parent = Database :: fetch_array($res_parent);
  1444. $parent = $row_parent['parent_item_id'];
  1445. $sql_bros = "SELECT * FROM $lp_item
  1446. WHERE
  1447. c_id = ".$course_id." AND
  1448. parent_item_id = $parent AND
  1449. id = $id AND
  1450. item_type='dokeos_chapter'
  1451. ORDER BY display_order";
  1452. $res_bros = Database::query($sql_bros);
  1453. $list = array ();
  1454. while ($row_bro = Database :: fetch_array($res_bros)) {
  1455. $list[] = $row_bro;
  1456. }
  1457. return $list;
  1458. }
  1459. return array ();
  1460. }
  1461. /**
  1462. * Gets all the items belonging to the same parent as the item given
  1463. * Can also be called as abstract method
  1464. * @param integer Item ID
  1465. * @return array A list of all the "brother items" (or an empty array on failure)
  1466. */
  1467. public function get_brother_items($id) {
  1468. $course_id = api_get_course_int_id();
  1469. if ($this->debug > 0) {
  1470. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1471. }
  1472. if (empty ($id) || $id != strval(intval($id))) {
  1473. return array ();
  1474. }
  1475. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1476. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1477. $res_parent = Database::query($sql_parent);
  1478. if (Database :: num_rows($res_parent) > 0) {
  1479. $row_parent = Database :: fetch_array($res_parent);
  1480. $parent = $row_parent['parent_item_id'];
  1481. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1482. ORDER BY display_order";
  1483. $res_bros = Database::query($sql_bros);
  1484. $list = array ();
  1485. while ($row_bro = Database :: fetch_array($res_bros)) {
  1486. $list[] = $row_bro;
  1487. }
  1488. return $list;
  1489. }
  1490. return array ();
  1491. }
  1492. /**
  1493. * Get the specific prefix index terms of this learning path
  1494. * @param string $prefix
  1495. * @return array Array of terms
  1496. */
  1497. public function get_common_index_terms_by_prefix($prefix)
  1498. {
  1499. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1500. $terms = get_specific_field_values_list_by_prefix(
  1501. $prefix,
  1502. $this->cc,
  1503. TOOL_LEARNPATH,
  1504. $this->lp_id
  1505. );
  1506. $prefix_terms = array();
  1507. if (!empty($terms)) {
  1508. foreach ($terms as $term) {
  1509. $prefix_terms[] = $term['value'];
  1510. }
  1511. }
  1512. return $prefix_terms;
  1513. }
  1514. /**
  1515. * Gets the number of items currently completed
  1516. * @return integer The number of items currently completed
  1517. */
  1518. public function get_complete_items_count()
  1519. {
  1520. if ($this->debug > 0) {
  1521. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1522. }
  1523. $i = 0;
  1524. $completedStatusList = array(
  1525. 'completed',
  1526. 'passed',
  1527. 'succeeded',
  1528. 'browsed',
  1529. 'failed'
  1530. );
  1531. foreach ($this->items as $id => $dummy) {
  1532. // Trying failed and browsed considered "progressed" as well.
  1533. if ($this->items[$id]->status_is($completedStatusList) &&
  1534. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1535. $this->items[$id]->get_type() != 'dir'
  1536. ) {
  1537. $i++;
  1538. }
  1539. }
  1540. return $i;
  1541. }
  1542. /**
  1543. * Gets the current item ID
  1544. * @return integer The current learnpath item id
  1545. */
  1546. public function get_current_item_id()
  1547. {
  1548. $current = 0;
  1549. if ($this->debug > 0) {
  1550. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1551. }
  1552. if (!empty($this->current)) {
  1553. $current = $this->current;
  1554. }
  1555. if ($this->debug > 2) {
  1556. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1557. }
  1558. return $current;
  1559. }
  1560. /**
  1561. * Force to get the first learnpath item id
  1562. * @return integer The current learnpath item id
  1563. */
  1564. public function get_first_item_id()
  1565. {
  1566. $current = 0;
  1567. if (is_array($this->ordered_items)) {
  1568. $current = $this->ordered_items[0];
  1569. }
  1570. return $current;
  1571. }
  1572. /**
  1573. * Gets the total number of items available for viewing in this SCORM
  1574. * @return integer The total number of items
  1575. */
  1576. public function get_total_items_count()
  1577. {
  1578. if ($this->debug > 0) {
  1579. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1580. }
  1581. return count($this->items);
  1582. }
  1583. /**
  1584. * Gets the total number of items available for viewing in this SCORM but without chapters
  1585. * @return integer The total no-chapters number of items
  1586. */
  1587. public function get_total_items_count_without_chapters()
  1588. {
  1589. if ($this->debug > 0) {
  1590. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1591. }
  1592. $total = 0;
  1593. $typeListNotToCount = self::getChapterTypes();
  1594. foreach ($this->items as $temp2) {
  1595. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1596. $total++;
  1597. }
  1598. }
  1599. return $total;
  1600. }
  1601. /**
  1602. * Gets the first element URL.
  1603. * @return string URL to load into the viewer
  1604. */
  1605. public function first()
  1606. {
  1607. if ($this->debug > 0) {
  1608. error_log('New LP - In learnpath::first()', 0);
  1609. error_log('$this->last_item_seen '.$this->last_item_seen);
  1610. }
  1611. // Test if the last_item_seen exists and is not a dir.
  1612. if (count($this->ordered_items) == 0) {
  1613. $this->index = 0;
  1614. }
  1615. if ($this->debug > 0) {
  1616. if (isset($this->items[$this->last_item_seen])) {
  1617. $status = $this->items[$this->last_item_seen]->get_status();
  1618. }
  1619. error_log('status '.$status);
  1620. }
  1621. if (!empty($this->last_item_seen) &&
  1622. !empty($this->items[$this->last_item_seen]) &&
  1623. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1624. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1625. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1626. //&& !$this->items[$this->last_item_seen]->is_done()
  1627. ) {
  1628. if ($this->debug > 2) {
  1629. 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);
  1630. }
  1631. $index = -1;
  1632. foreach ($this->ordered_items as $myindex => $item_id) {
  1633. if ($item_id == $this->last_item_seen) {
  1634. $index = $myindex;
  1635. break;
  1636. }
  1637. }
  1638. if ($index == -1) {
  1639. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1640. if ($this->debug > 2) {
  1641. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1642. }
  1643. return false;
  1644. } else {
  1645. $this->last = $this->last_item_seen;
  1646. $this->current = $this->last_item_seen;
  1647. $this->index = $index;
  1648. }
  1649. } else {
  1650. if ($this->debug > 2) {
  1651. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1652. }
  1653. $index = 0;
  1654. // Loop through all ordered items and stop at the first item that is
  1655. // not a directory *and* that has not been completed yet.
  1656. while ( !empty($this->ordered_items[$index]) AND
  1657. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1658. (
  1659. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1660. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1661. $this->items[$this->ordered_items[$index]]->is_done() === true
  1662. ) AND $index < $this->max_ordered_items) {
  1663. $index++;
  1664. }
  1665. $this->last = $this->current;
  1666. // current is
  1667. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1668. $this->index = $index;
  1669. if ($this->debug > 2) {
  1670. error_log('$index ' . $index);
  1671. }
  1672. if ($this->debug > 2) {
  1673. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1674. }
  1675. }
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1678. }
  1679. }
  1680. /**
  1681. * Gets the information about an item in a format usable as JavaScript to update
  1682. * the JS API by just printing this content into the <head> section of the message frame
  1683. * @param int $item_id
  1684. * @return string
  1685. */
  1686. public function get_js_info($item_id = '')
  1687. {
  1688. if ($this->debug > 0) {
  1689. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1690. }
  1691. $info = '';
  1692. $item_id = intval($item_id);
  1693. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1694. //if item is defined, return values from DB
  1695. $oItem = $this->items[$item_id];
  1696. $info .= '<script language="javascript">';
  1697. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1698. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1699. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1700. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1701. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1702. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1703. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1704. $info .= "top.set_flag_synchronized();";
  1705. $info .= '</script>';
  1706. if ($this->debug > 2) {
  1707. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1708. }
  1709. return $info;
  1710. } else {
  1711. // If item_id is empty, just update to default SCORM data.
  1712. $info .= '<script language="javascript">';
  1713. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1714. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1715. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1716. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1717. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1718. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1719. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1720. $info .= "top.set_flag_synchronized();";
  1721. $info .= '</script>';
  1722. if ($this->debug > 2) {
  1723. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1724. }
  1725. return $info;
  1726. }
  1727. }
  1728. /**
  1729. * Gets the js library from the database
  1730. * @return string The name of the javascript library to be used
  1731. */
  1732. public function get_js_lib()
  1733. {
  1734. $lib = '';
  1735. if (!empty ($this->js_lib)) {
  1736. $lib = $this->js_lib;
  1737. }
  1738. return $lib;
  1739. }
  1740. /**
  1741. * Gets the learnpath database ID
  1742. * @return integer Learnpath ID in the lp table
  1743. */
  1744. public function get_id()
  1745. {
  1746. if (!empty ($this->lp_id)) {
  1747. return $this->lp_id;
  1748. } else {
  1749. return 0;
  1750. }
  1751. }
  1752. /**
  1753. * Gets the last element URL.
  1754. * @return string URL to load into the viewer
  1755. */
  1756. public function get_last()
  1757. {
  1758. if ($this->debug > 0) {
  1759. error_log('New LP - In learnpath::get_last()', 0);
  1760. }
  1761. $this->index = count($this->ordered_items) - 1;
  1762. return $this->ordered_items[$this->index];
  1763. }
  1764. /**
  1765. * Gets the navigation bar for the learnpath display screen
  1766. * @return string The HTML string to use as a navigation bar
  1767. */
  1768. public function get_navigation_bar($idBar = null, $display=null) {
  1769. if ($this->debug > 0) {
  1770. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1771. }
  1772. if(empty($idBar)){
  1773. $idBar='control-top';
  1774. }
  1775. if(empty($display)){
  1776. $display='display:block';
  1777. }
  1778. $navbar = null;
  1779. $lp_id = $this->lp_id;
  1780. $mycurrentitemid = $this->get_current_item_id();
  1781. if ($this->mode == 'fullscreen') {
  1782. $navbar = '
  1783. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1784. <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>
  1785. <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>
  1786. <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>.
  1787. <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>
  1788. </div>';
  1789. } else {
  1790. $navbar = '
  1791. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1792. <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>
  1793. <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>
  1794. <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>
  1795. </div>';
  1796. }
  1797. return $navbar;
  1798. }
  1799. /**
  1800. * Gets the next resource in queue (url).
  1801. * @return string URL to load into the viewer
  1802. */
  1803. public function get_next_index()
  1804. {
  1805. if ($this->debug > 0) {
  1806. error_log('New LP - In learnpath::get_next_index()', 0);
  1807. }
  1808. // TODO
  1809. $index = $this->index;
  1810. $index++;
  1811. if ($this->debug > 2) {
  1812. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1813. }
  1814. 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) {
  1815. $index++;
  1816. if ($index == $this->max_ordered_items){
  1817. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1818. return $this->index;
  1819. } else {
  1820. return $index;
  1821. }
  1822. }
  1823. }
  1824. if (empty ($this->ordered_items[$index])) {
  1825. return $this->index;
  1826. }
  1827. if ($this->debug > 2) {
  1828. error_log('New LP - index is now ' . $index, 0);
  1829. }
  1830. return $index;
  1831. }
  1832. /**
  1833. * Gets item_id for the next element
  1834. * @return integer Next item (DB) ID
  1835. */
  1836. public function get_next_item_id()
  1837. {
  1838. if ($this->debug > 0) {
  1839. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1840. }
  1841. $new_index = $this->get_next_index();
  1842. if (!empty ($new_index)) {
  1843. if (isset ($this->ordered_items[$new_index])) {
  1844. if ($this->debug > 2) {
  1845. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1846. }
  1847. return $this->ordered_items[$new_index];
  1848. }
  1849. }
  1850. if ($this->debug > 2) {
  1851. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1852. }
  1853. return 0;
  1854. }
  1855. /**
  1856. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1857. *
  1858. * Generally, the package provided is in the form of a zip file, so the function
  1859. * has been written to test a zip file. If not a zip, the function will return the
  1860. * default return value: ''
  1861. * @param string the path to the file
  1862. * @param string the original name of the file
  1863. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1864. */
  1865. public static function get_package_type($file_path, $file_name)
  1866. {
  1867. // Get name of the zip file without the extension.
  1868. $file_info = pathinfo($file_name);
  1869. $filename = $file_info['basename']; // Name including extension.
  1870. $extension = $file_info['extension']; // Extension only.
  1871. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1872. 'dll',
  1873. 'exe'
  1874. ))) {
  1875. return 'oogie';
  1876. }
  1877. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1878. 'dll',
  1879. 'exe'
  1880. ))) {
  1881. return 'woogie';
  1882. }
  1883. // Filename without its extension.
  1884. $file_base_name = str_replace('.' . $extension, '', $filename);
  1885. $zipFile = new PclZip($file_path);
  1886. // Check the zip content (real size and file extension).
  1887. $zipContentArray = $zipFile->listContent();
  1888. $package_type = '';
  1889. $at_root = false;
  1890. $manifest = '';
  1891. $aicc_match_crs = 0;
  1892. $aicc_match_au = 0;
  1893. $aicc_match_des = 0;
  1894. $aicc_match_cst = 0;
  1895. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1896. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1897. foreach ($zipContentArray as $thisContent) {
  1898. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1899. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1900. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1901. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1902. $package_type = 'scorm';
  1903. break; // Exit the foreach loop.
  1904. } elseif (
  1905. preg_match('/aicc\//i', $thisContent['filename']) ||
  1906. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1907. ) {
  1908. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1909. switch ($ext) {
  1910. case 'crs':
  1911. $aicc_match_crs = 1;
  1912. break;
  1913. case 'au':
  1914. $aicc_match_au = 1;
  1915. break;
  1916. case 'des':
  1917. $aicc_match_des = 1;
  1918. break;
  1919. case 'cst':
  1920. $aicc_match_cst = 1;
  1921. break;
  1922. default:
  1923. break;
  1924. }
  1925. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1926. } else {
  1927. $package_type = '';
  1928. }
  1929. }
  1930. }
  1931. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1932. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1933. $package_type = 'aicc';
  1934. }
  1935. return $package_type;
  1936. }
  1937. /**
  1938. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1939. * @return string URL to load into the viewer
  1940. */
  1941. public function get_previous_index()
  1942. {
  1943. if ($this->debug > 0) {
  1944. error_log('New LP - In learnpath::get_previous_index()', 0);
  1945. }
  1946. $index = $this->index;
  1947. if (isset ($this->ordered_items[$index -1])) {
  1948. $index--;
  1949. 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')) {
  1950. $index--;
  1951. if ($index < 0) {
  1952. return $this->index;
  1953. }
  1954. }
  1955. } else {
  1956. if ($this->debug > 2) {
  1957. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1958. }
  1959. // There is no previous item.
  1960. }
  1961. return $index;
  1962. }
  1963. /**
  1964. * Gets item_id for the next element
  1965. * @return integer Previous item (DB) ID
  1966. */
  1967. public function get_previous_item_id()
  1968. {
  1969. if ($this->debug > 0) {
  1970. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1971. }
  1972. $new_index = $this->get_previous_index();
  1973. return $this->ordered_items[$new_index];
  1974. }
  1975. /**
  1976. * Gets the progress value from the progress_db attribute
  1977. * @return integer Current progress value
  1978. */
  1979. public function get_progress()
  1980. {
  1981. if ($this->debug > 0) {
  1982. error_log('New LP - In learnpath::get_progress()', 0);
  1983. }
  1984. if (!empty ($this->progress_db)) {
  1985. return $this->progress_db;
  1986. }
  1987. return 0;
  1988. }
  1989. /**
  1990. * Returns the HTML necessary to print a mediaplayer block inside a page
  1991. * @return string The mediaplayer HTML
  1992. */
  1993. public function get_mediaplayer($autostart = 'true')
  1994. {
  1995. $course_id = api_get_course_int_id();
  1996. $_course = api_get_course_info();
  1997. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1998. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1999. // Getting all the information about the item.
  2000. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2001. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2002. ON lp.id = lp_view.lp_item_id
  2003. WHERE
  2004. lp.id = '".$_SESSION['oLP']->current."' AND
  2005. lp.c_id = $course_id AND
  2006. lp_view.c_id = $course_id";
  2007. $result = Database::query($sql);
  2008. $row = Database::fetch_assoc($result);
  2009. $output = '';
  2010. if (!empty ($row['audio'])) {
  2011. $list = $_SESSION['oLP']->get_toc();
  2012. $type_quiz = false;
  2013. foreach($list as $toc) {
  2014. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2015. $type_quiz = true;
  2016. }
  2017. }
  2018. if ($type_quiz) {
  2019. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2020. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2021. } else {
  2022. $autostart_audio = $autostart;
  2023. }
  2024. } else {
  2025. $autostart_audio = 'true';
  2026. }
  2027. $courseInfo = api_get_course_info();
  2028. $audio = $row['audio'];
  2029. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2030. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2031. if (!file_exists($file)) {
  2032. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2033. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2034. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2035. }
  2036. $player = Display::getMediaPlayer(
  2037. $file,
  2038. array(
  2039. 'id' => 'lp_audio_media_player',
  2040. 'url' => $url,
  2041. 'autoplay' => $autostart_audio,
  2042. 'width' => '100%'
  2043. )
  2044. );
  2045. // The mp3 player.
  2046. $output = '<div id="container">';
  2047. $output .= $player;
  2048. $output .= '</div>';
  2049. }
  2050. return $output;
  2051. }
  2052. /**
  2053. * Checks if the learning path is visible for student after the progress
  2054. * of its prerequisite is completed, considering the time availability and
  2055. * the LP visibility.
  2056. * @param int $lp_id
  2057. * @param int $student_id
  2058. * @param string Course code (optional)
  2059. * @param int $sessionId
  2060. * @return bool
  2061. */
  2062. public static function is_lp_visible_for_student(
  2063. $lp_id,
  2064. $student_id,
  2065. $courseCode = null,
  2066. $sessionId = null
  2067. ) {
  2068. $lp_id = (int)$lp_id;
  2069. $courseInfo = api_get_course_info($courseCode);
  2070. $sessionId = intval($sessionId);
  2071. if (empty($sessionId)) {
  2072. $sessionId = api_get_session_id();
  2073. }
  2074. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2075. // Get current prerequisite
  2076. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2077. FROM $tbl_learnpath
  2078. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2079. $itemInfo = api_get_item_property_info(
  2080. $courseInfo['real_id'],
  2081. TOOL_LEARNPATH,
  2082. $lp_id,
  2083. $sessionId
  2084. );
  2085. // If the item was deleted.
  2086. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2087. return false;
  2088. }
  2089. $rs = Database::query($sql);
  2090. $now = time();
  2091. if (Database::num_rows($rs) > 0) {
  2092. $row = Database::fetch_array($rs, 'ASSOC');
  2093. $prerequisite = $row['prerequisite'];
  2094. $is_visible = true;
  2095. if (!empty($prerequisite)) {
  2096. $progress = self::getProgress(
  2097. $prerequisite,
  2098. $student_id,
  2099. $courseInfo['real_id'],
  2100. $sessionId
  2101. );
  2102. $progress = intval($progress);
  2103. if ($progress < 100) {
  2104. $is_visible = false;
  2105. }
  2106. }
  2107. // Also check the time availability of the LP
  2108. if ($is_visible) {
  2109. // Adding visibility restrictions
  2110. if (!empty($row['publicated_on']) &&
  2111. $row['publicated_on'] != '0000-00-00 00:00:00'
  2112. ) {
  2113. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2114. //api_not_allowed();
  2115. $is_visible = false;
  2116. }
  2117. }
  2118. // Blocking empty start times see BT#2800
  2119. global $_custom;
  2120. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2121. $_custom['lps_hidden_when_no_start_date']
  2122. ) {
  2123. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2124. //api_not_allowed();
  2125. $is_visible = false;
  2126. }
  2127. }
  2128. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2129. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2130. //api_not_allowed();
  2131. $is_visible = false;
  2132. }
  2133. }
  2134. }
  2135. // Check if the subscription users/group to a LP is ON
  2136. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2137. // Try group
  2138. $is_visible = false;
  2139. // Checking only the user visibility
  2140. $userVisibility = api_get_item_visibility(
  2141. $courseInfo,
  2142. 'learnpath',
  2143. $row['id'],
  2144. $sessionId,
  2145. $student_id,
  2146. 'LearnpathSubscription'
  2147. );
  2148. if ($userVisibility == 1) {
  2149. $is_visible = true;
  2150. } else {
  2151. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2152. if (!empty($userGroups)) {
  2153. foreach ($userGroups as $groupInfo) {
  2154. $groupId = $groupInfo['iid'];
  2155. $userVisibility = api_get_item_visibility(
  2156. $courseInfo,
  2157. 'learnpath',
  2158. $row['id'],
  2159. $sessionId,
  2160. null,
  2161. 'LearnpathSubscription',
  2162. $groupId
  2163. );
  2164. if ($userVisibility == 1) {
  2165. $is_visible = true;
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. return $is_visible;
  2173. }
  2174. return false;
  2175. }
  2176. /**
  2177. * @param int $lpId
  2178. * @param int $userId
  2179. * @param int $courseId
  2180. * @param int $sessionId
  2181. * @return int
  2182. */
  2183. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2184. {
  2185. $lpId = intval($lpId);
  2186. $userId = intval($userId);
  2187. $courseId = intval($courseId);
  2188. $sessionId = intval($sessionId);
  2189. $progress = 0;
  2190. $sessionCondition = api_get_session_condition($sessionId);
  2191. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2192. $sql = "SELECT * FROM $table
  2193. WHERE
  2194. c_id = ".$courseId." AND
  2195. lp_id = $lpId AND
  2196. user_id = $userId $sessionCondition";
  2197. $res = Database::query($sql);
  2198. if (Database :: num_rows($res) > 0) {
  2199. $row = Database:: fetch_array($res);
  2200. $progress = $row['progress'];
  2201. }
  2202. return $progress;
  2203. }
  2204. /**
  2205. * Displays a progress bar
  2206. * completed so far.
  2207. * @param integer $percentage Progress value to display
  2208. * @param string $text_add Text to display near the progress value
  2209. * @return string HTML string containing the progress bar
  2210. */
  2211. public static function get_progress_bar($percentage = -1, $text_add = '')
  2212. {
  2213. $text = $percentage . $text_add;
  2214. $output = '<div class="progress">
  2215. <div id="progress_bar_value" class="progress-bar" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2216. '. $text .'
  2217. </div>
  2218. </div>';
  2219. return $output;
  2220. }
  2221. /**
  2222. * @param string $mode can be '%' or 'abs'
  2223. * otherwise this value will be used $this->progress_bar_mode
  2224. * @return string
  2225. */
  2226. public function getProgressBar($mode = null)
  2227. {
  2228. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2229. return self::get_progress_bar($percentage, $text_add);
  2230. }
  2231. /**
  2232. * Gets the progress bar info to display inside the progress bar.
  2233. * Also used by scorm_api.php
  2234. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2235. * we display a number of completed elements per total elements
  2236. * @param integer $add Additional steps to fake as completed
  2237. * @return list array Percentage or number and symbol (% or /xx)
  2238. */
  2239. public function get_progress_bar_text($mode = '', $add = 0)
  2240. {
  2241. if ($this->debug > 0) {
  2242. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2243. }
  2244. if (empty($mode)) {
  2245. $mode = $this->progress_bar_mode;
  2246. }
  2247. $total_items = $this->get_total_items_count_without_chapters();
  2248. if ($this->debug > 2) {
  2249. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2250. }
  2251. $completeItems = $this->get_complete_items_count();
  2252. if ($this->debug > 2) {
  2253. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2254. }
  2255. if ($add != 0) {
  2256. $completeItems += $add;
  2257. if ($this->debug > 2) {
  2258. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2259. }
  2260. }
  2261. $text = '';
  2262. if ($completeItems > $total_items) {
  2263. $completeItems = $total_items;
  2264. }
  2265. $percentage = 0;
  2266. if ($mode == '%') {
  2267. if ($total_items > 0) {
  2268. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2269. } else {
  2270. $percentage = 0;
  2271. }
  2272. $percentage = number_format($percentage, 0);
  2273. $text = '%';
  2274. } elseif ($mode == 'abs') {
  2275. $percentage = $completeItems;
  2276. $text = '/' . $total_items;
  2277. }
  2278. return array(
  2279. $percentage,
  2280. $text
  2281. );
  2282. }
  2283. /**
  2284. * Gets the progress bar mode
  2285. * @return string The progress bar mode attribute
  2286. */
  2287. public function get_progress_bar_mode()
  2288. {
  2289. if ($this->debug > 0) {
  2290. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2291. }
  2292. if (!empty ($this->progress_bar_mode)) {
  2293. return $this->progress_bar_mode;
  2294. } else {
  2295. return '%';
  2296. }
  2297. }
  2298. /**
  2299. * Gets the learnpath proximity (remote or local)
  2300. * @return string Learnpath proximity
  2301. */
  2302. public function get_proximity()
  2303. {
  2304. if ($this->debug > 0) {
  2305. error_log('New LP - In learnpath::get_proximity()', 0);
  2306. }
  2307. if (!empty ($this->proximity)) {
  2308. return $this->proximity;
  2309. } else {
  2310. return '';
  2311. }
  2312. }
  2313. /**
  2314. * Gets the learnpath theme (remote or local)
  2315. * @return string Learnpath theme
  2316. */
  2317. public function get_theme()
  2318. {
  2319. if ($this->debug > 0) {
  2320. error_log('New LP - In learnpath::get_theme()', 0);
  2321. }
  2322. if (!empty ($this->theme)) {
  2323. return $this->theme;
  2324. } else {
  2325. return '';
  2326. }
  2327. }
  2328. /**
  2329. * Gets the learnpath session id
  2330. * @return string Learnpath theme
  2331. */
  2332. public function get_lp_session_id()
  2333. {
  2334. if ($this->debug > 0) {
  2335. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2336. }
  2337. if (!empty ($this->lp_session_id)) {
  2338. return $this->lp_session_id;
  2339. } else {
  2340. return 0;
  2341. }
  2342. }
  2343. /**
  2344. * Gets the learnpath image
  2345. * @return string Web URL of the LP image
  2346. */
  2347. public function get_preview_image()
  2348. {
  2349. if ($this->debug > 0) {
  2350. error_log('New LP - In learnpath::get_preview_image()', 0);
  2351. }
  2352. if (!empty($this->preview_image)) {
  2353. return $this->preview_image;
  2354. } else {
  2355. return '';
  2356. }
  2357. }
  2358. /**
  2359. * @param string $size
  2360. * @param string $path_type
  2361. * @return bool|string
  2362. */
  2363. public function get_preview_image_path($size = null, $path_type = 'web')
  2364. {
  2365. $preview_image = $this->get_preview_image();
  2366. if (isset($preview_image) && !empty($preview_image)) {
  2367. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2368. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2369. if (isset($size)) {
  2370. $info = pathinfo($preview_image);
  2371. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2372. if (file_exists($image_sys_path.$image_custom_size)) {
  2373. if ($path_type == 'web') {
  2374. return $image_path.$image_custom_size;
  2375. } else {
  2376. return $image_sys_path.$image_custom_size;
  2377. }
  2378. }
  2379. } else {
  2380. if ($path_type == 'web') {
  2381. return $image_path.$preview_image;
  2382. } else {
  2383. return $image_sys_path.$preview_image;
  2384. }
  2385. }
  2386. }
  2387. return false;
  2388. }
  2389. /**
  2390. * Gets the learnpath author
  2391. * @return string LP's author
  2392. */
  2393. public function get_author()
  2394. {
  2395. if ($this->debug > 0) {
  2396. error_log('New LP - In learnpath::get_author()', 0);
  2397. }
  2398. if (!empty ($this->author)) {
  2399. return $this->author;
  2400. } else {
  2401. return '';
  2402. }
  2403. }
  2404. /**
  2405. * Gets the learnpath author
  2406. * @return string LP's author
  2407. */
  2408. public function get_hide_toc_frame()
  2409. {
  2410. if ($this->debug > 0) {
  2411. error_log('New LP - In learnpath::get_author()', 0);
  2412. }
  2413. if (!empty ($this->hide_toc_frame)) {
  2414. return $this->hide_toc_frame;
  2415. } else {
  2416. return '';
  2417. }
  2418. }
  2419. /**
  2420. * Generate a new prerequisites string for a given item. If this item was a sco and
  2421. * its prerequisites were strings (instead of IDs), then transform those strings into
  2422. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2423. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2424. * same rule as the scorm_export() method
  2425. * @param integer Item ID
  2426. * @return string Prerequisites string ready for the export as SCORM
  2427. */
  2428. public function get_scorm_prereq_string($item_id)
  2429. {
  2430. if ($this->debug > 0) {
  2431. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2432. }
  2433. if (!is_object($this->items[$item_id])) {
  2434. return false;
  2435. }
  2436. /** @var learnpathItem $oItem */
  2437. $oItem = $this->items[$item_id];
  2438. $prereq = $oItem->get_prereq_string();
  2439. if (empty($prereq)) {
  2440. return '';
  2441. }
  2442. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2443. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2444. // then simply return it (with the ITEM_ prefix).
  2445. //return 'ITEM_' . $prereq;
  2446. return $this->items[$prereq]->ref;
  2447. } else {
  2448. if (isset($this->refs_list[$prereq])) {
  2449. // It's a simple string item from which the ID can be found in the refs list,
  2450. // so we can transform it directly to an ID for export.
  2451. return $this->items[$this->refs_list[$prereq]]->ref;
  2452. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2453. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2454. } else {
  2455. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2456. // and replace them, one by one, by the internal IDs (chamilo db)
  2457. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2458. // by a space as well.
  2459. $find = array (
  2460. '&',
  2461. '|',
  2462. '~',
  2463. '=',
  2464. '<>',
  2465. '{',
  2466. '}',
  2467. '*',
  2468. '(',
  2469. ')'
  2470. );
  2471. $replace = array (
  2472. ' ',
  2473. ' ',
  2474. ' ',
  2475. ' ',
  2476. ' ',
  2477. ' ',
  2478. ' ',
  2479. ' ',
  2480. ' ',
  2481. ' '
  2482. );
  2483. $prereq_mod = str_replace($find, $replace, $prereq);
  2484. $ids = explode(' ', $prereq_mod);
  2485. foreach ($ids as $id) {
  2486. $id = trim($id);
  2487. if (isset ($this->refs_list[$id])) {
  2488. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2489. }
  2490. }
  2491. return $prereq;
  2492. }
  2493. }
  2494. }
  2495. /**
  2496. * Returns the XML DOM document's node
  2497. * @param resource Reference to a list of objects to search for the given ITEM_*
  2498. * @param string The identifier to look for
  2499. * @return mixed The reference to the element found with that identifier. False if not found
  2500. */
  2501. public function get_scorm_xml_node(& $children, $id)
  2502. {
  2503. for ($i = 0; $i < $children->length; $i++) {
  2504. $item_temp = $children->item($i);
  2505. if ($item_temp->nodeName == 'item') {
  2506. if ($item_temp->getAttribute('identifier') == $id) {
  2507. return $item_temp;
  2508. }
  2509. }
  2510. $subchildren = $item_temp->childNodes;
  2511. if ($subchildren->length > 0) {
  2512. $val = $this->get_scorm_xml_node($subchildren, $id);
  2513. if (is_object($val)) {
  2514. return $val;
  2515. }
  2516. }
  2517. }
  2518. return false;
  2519. }
  2520. /**
  2521. * Returns a usable array of stats related to the current learnpath and user
  2522. * @return array Well-formatted array containing status for the current learnpath
  2523. */
  2524. public function get_stats()
  2525. {
  2526. if ($this->debug > 0) {
  2527. error_log('New LP - In learnpath::get_stats()', 0);
  2528. }
  2529. }
  2530. /**
  2531. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2532. * the given course (for all learnpaths and all users)
  2533. * @param string Course code
  2534. * @return array Well-formatted array containing status for the course's learnpaths
  2535. */
  2536. public function get_stats_course($course)
  2537. {
  2538. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2539. // TODO
  2540. }
  2541. /**
  2542. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2543. * the given course and learnpath (for all users)
  2544. * @param string Course code
  2545. * @param integer Learnpath ID
  2546. * @return array Well-formatted array containing status for the specified learnpath
  2547. */
  2548. public function get_stats_lp($course, $lp)
  2549. {
  2550. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2551. // TODO
  2552. }
  2553. /**
  2554. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2555. * the given course, learnpath and user.
  2556. * @param string Course code
  2557. * @param integer Learnpath ID
  2558. * @param integer User ID
  2559. * @return array Well-formatted array containing status for the specified learnpath and user
  2560. */
  2561. public function get_stats_lp_user($course, $lp, $user)
  2562. {
  2563. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2564. // TODO
  2565. }
  2566. /**
  2567. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2568. * the given course and learnpath (for all users)
  2569. * @param string Course code
  2570. * @param integer User ID
  2571. * @return array Well-formatted array containing status for the user's learnpaths
  2572. */
  2573. public function get_stats_user($course, $user) {
  2574. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2575. // TODO
  2576. }
  2577. /**
  2578. * Gets the status list for all LP's items
  2579. * @return array Array of [index] => [item ID => current status]
  2580. */
  2581. public function get_items_status_list()
  2582. {
  2583. if ($this->debug > 0) {
  2584. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2585. }
  2586. $list = array ();
  2587. foreach ($this->ordered_items as $item_id) {
  2588. $list[] = array (
  2589. $item_id => $this->items[$item_id]->get_status()
  2590. );
  2591. }
  2592. return $list;
  2593. }
  2594. /**
  2595. * Return the number of interactions for the given learnpath Item View ID.
  2596. * This method can be used as static.
  2597. * @param integer Item View ID
  2598. * @param integer course id
  2599. * @return integer Number of interactions
  2600. */
  2601. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2602. {
  2603. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2604. $lp_iv_id = intval($lp_iv_id);
  2605. $course_id = intval($course_id);
  2606. $sql = "SELECT count(*) FROM $table
  2607. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2608. $res = Database::query($sql);
  2609. $num = 0;
  2610. if (Database::num_rows($res)) {
  2611. $row = Database::fetch_array($res);
  2612. $num = $row[0];
  2613. }
  2614. return $num;
  2615. }
  2616. /**
  2617. * Return the interactions as an array for the given lp_iv_id.
  2618. * This method can be used as static.
  2619. * @param integer Learnpath Item View ID
  2620. * @return array
  2621. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2622. */
  2623. public static function get_iv_interactions_array($lp_iv_id)
  2624. {
  2625. $course_id = api_get_course_int_id();
  2626. $list = array();
  2627. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2628. if (empty($lp_iv_id)) {
  2629. return array();
  2630. }
  2631. $sql = "SELECT * FROM $table
  2632. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2633. ORDER BY order_id ASC";
  2634. $res = Database::query($sql);
  2635. $num = Database :: num_rows($res);
  2636. if ($num > 0) {
  2637. $list[] = array (
  2638. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2639. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2640. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2641. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2642. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2643. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2644. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2645. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2646. );
  2647. while ($row = Database :: fetch_array($res)) {
  2648. $list[] = array (
  2649. 'order_id' => ($row['order_id'] + 1),
  2650. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2651. 'type' => $row['interaction_type'],
  2652. 'time' => $row['completion_time'],
  2653. //'correct_responses' => $row['correct_responses'],
  2654. 'correct_responses' => '', // Hide correct responses from students.
  2655. 'student_response' => $row['student_response'],
  2656. 'result' => $row['result'],
  2657. 'latency' => $row['latency']
  2658. );
  2659. }
  2660. }
  2661. return $list;
  2662. }
  2663. /**
  2664. * Return the number of objectives for the given learnpath Item View ID.
  2665. * This method can be used as static.
  2666. * @param integer Item View ID
  2667. * @return integer Number of objectives
  2668. */
  2669. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2670. {
  2671. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2672. $course_id = intval($course_id);
  2673. $lp_iv_id = intval($lp_iv_id);
  2674. $sql = "SELECT count(*) FROM $table
  2675. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2676. //@todo seems that this always returns 0
  2677. $res = Database::query($sql);
  2678. $num = 0;
  2679. if (Database::num_rows($res)) {
  2680. $row = Database :: fetch_array($res);
  2681. $num = $row[0];
  2682. }
  2683. return $num;
  2684. }
  2685. /**
  2686. * Return the objectives as an array for the given lp_iv_id.
  2687. * This method can be used as static.
  2688. * @param integer Learnpath Item View ID
  2689. * @return array
  2690. * @todo Translate labels
  2691. */
  2692. public static function get_iv_objectives_array($lp_iv_id = 0)
  2693. {
  2694. $course_id = api_get_course_int_id();
  2695. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2696. $sql = "SELECT * FROM $table
  2697. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2698. ORDER BY order_id ASC";
  2699. $res = Database::query($sql);
  2700. $num = Database :: num_rows($res);
  2701. $list = array();
  2702. if ($num > 0) {
  2703. $list[] = array(
  2704. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2705. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2706. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2707. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2708. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2709. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2710. );
  2711. while ($row = Database :: fetch_array($res)) {
  2712. $list[] = array (
  2713. 'order_id' => ($row['order_id'] + 1),
  2714. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2715. 'score_raw' => $row['score_raw'],
  2716. 'score_max' => $row['score_max'],
  2717. 'score_min' => $row['score_min'],
  2718. 'status' => $row['status']
  2719. );
  2720. }
  2721. }
  2722. return $list;
  2723. }
  2724. /**
  2725. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2726. * used by get_html_toc() to be ready to display
  2727. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2728. */
  2729. public function get_toc()
  2730. {
  2731. if ($this->debug > 0) {
  2732. error_log('learnpath::get_toc()', 0);
  2733. }
  2734. $toc = array();
  2735. foreach ($this->ordered_items as $item_id) {
  2736. if ($this->debug > 2) {
  2737. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2738. }
  2739. // TODO: Change this link generation and use new function instead.
  2740. $toc[] = array (
  2741. 'id' => $item_id,
  2742. 'title' => $this->items[$item_id]->get_title(),
  2743. 'status' => $this->items[$item_id]->get_status(),
  2744. 'level' => $this->items[$item_id]->get_level(),
  2745. 'type' => $this->items[$item_id]->get_type(),
  2746. 'description' => $this->items[$item_id]->get_description(),
  2747. 'path' => $this->items[$item_id]->get_path(),
  2748. );
  2749. }
  2750. if ($this->debug > 2) {
  2751. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2752. }
  2753. return $toc;
  2754. }
  2755. /**
  2756. * Generate and return the table of contents for this learnpath. The JS
  2757. * table returned is used inside of scorm_api.php
  2758. * @return string A JS array vairiable construction
  2759. */
  2760. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2761. {
  2762. if ($this->debug > 0) {
  2763. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2764. }
  2765. $toc = $varname.' = new Array();';
  2766. foreach ($this->ordered_items as $item_id) {
  2767. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2768. }
  2769. if ($this->debug > 2) {
  2770. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2771. }
  2772. return $toc;
  2773. }
  2774. /**
  2775. * Gets the learning path type
  2776. * @param boolean Return the name? If false, return the ID. Default is false.
  2777. * @return mixed Type ID or name, depending on the parameter
  2778. */
  2779. public function get_type($get_name = false)
  2780. {
  2781. $res = false;
  2782. if ($this->debug > 0) {
  2783. error_log('New LP - In learnpath::get_type()', 0);
  2784. }
  2785. if (!empty ($this->type)) {
  2786. if ($get_name) {
  2787. // Get it from the lp_type table in main db.
  2788. } else {
  2789. $res = $this->type;
  2790. }
  2791. }
  2792. if ($this->debug > 2) {
  2793. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2794. }
  2795. return $res;
  2796. }
  2797. /**
  2798. * Gets the learning path type as static method
  2799. * @param boolean Return the name? If false, return the ID. Default is false.
  2800. * @return mixed Type ID or name, depending on the parameter
  2801. */
  2802. public static function get_type_static($lp_id = 0)
  2803. {
  2804. $course_id = api_get_course_int_id();
  2805. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2806. $lp_id = intval($lp_id);
  2807. $sql = "SELECT lp_type FROM $tbl_lp
  2808. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2809. $res = Database::query($sql);
  2810. if ($res === false) {
  2811. return null;
  2812. }
  2813. if (Database :: num_rows($res) <= 0) {
  2814. return null;
  2815. }
  2816. $row = Database :: fetch_array($res);
  2817. return $row['lp_type'];
  2818. }
  2819. /**
  2820. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2821. * This method can be used as abstract and is recursive
  2822. * @param integer Learnpath ID
  2823. * @param integer Parent ID of the items to look for
  2824. * @return mixed Ordered list of item IDs or false on error
  2825. */
  2826. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2827. {
  2828. if (empty($course_id)) {
  2829. $course_id = api_get_course_int_id();
  2830. } else {
  2831. $course_id = intval($course_id);
  2832. }
  2833. $list = array();
  2834. if (empty($lp)) {
  2835. return false;
  2836. }
  2837. $lp = intval($lp);
  2838. $parent = intval($parent);
  2839. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2840. $sql = "SELECT id FROM $tbl_lp_item
  2841. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2842. ORDER BY display_order";
  2843. $res = Database::query($sql);
  2844. while ($row = Database :: fetch_array($res)) {
  2845. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2846. $list[] = $row['id'];
  2847. foreach ($sublist as $item) {
  2848. $list[] = $item;
  2849. }
  2850. }
  2851. return $list;
  2852. }
  2853. /**
  2854. * @return array
  2855. */
  2856. public static function getChapterTypes()
  2857. {
  2858. return array(
  2859. 'dokeos_chapter',
  2860. 'dokeos_module',
  2861. 'chapter',
  2862. 'dir'
  2863. );
  2864. }
  2865. /**
  2866. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2867. * @return string HTML TOC ready to display
  2868. */
  2869. public function get_html_toc($toc_list = null)
  2870. {
  2871. global $_configuration;
  2872. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2873. if ($this->debug > 0) {
  2874. error_log('In learnpath::get_html_toc()', 0);
  2875. }
  2876. if (empty($toc_list)) {
  2877. $toc_list = $this->get_toc();
  2878. }
  2879. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2880. $html = '<div class="scorm-body">';
  2881. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2882. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2883. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2884. if ($this->get_lp_session_id() == api_get_session_id()) {
  2885. $html .= '<div id="actions_lp" class="actions_lp">';
  2886. $html .= '<div class="btn-group">';
  2887. $html .= "<a class='btn btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2888. $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>";
  2889. $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>';
  2890. $html .= '</div>';
  2891. $html .= '</div>';
  2892. }
  2893. }
  2894. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2895. require_once 'resourcelinker.inc.php';
  2896. // Temporary variables.
  2897. $mycurrentitemid = $this->get_current_item_id();
  2898. $color_counter = 0;
  2899. $i = 0;
  2900. foreach ($toc_list as $item) {
  2901. // TODO: Complete this
  2902. $icon_name = array (
  2903. 'not attempted' => '../img/notattempted.gif',
  2904. 'incomplete' => '../img/incomplete.png',
  2905. 'failed' => '../img/delete.png',
  2906. 'completed' => '../img/completed.png',
  2907. 'passed' => '../img/passed.png',
  2908. 'succeeded' => '../img/succeeded.png',
  2909. 'browsed' => '../img/completed.png',
  2910. );
  2911. // Style Status
  2912. $class_name = array (
  2913. 'not attempted' => 'scorm_not_attempted',
  2914. 'incomplete' => 'scorm_not_attempted',
  2915. 'failed' => 'scorm_failed',
  2916. 'completed' => 'scorm_completed',
  2917. 'passed' => 'scorm_completed',
  2918. 'succeeded' => 'scorm_completed',
  2919. 'browsed' => 'scorm_completed',
  2920. );
  2921. $scorm_color_background = 'row_odd';
  2922. $style_item = '';
  2923. if ($color_counter % 2 == 0) {
  2924. $scorm_color_background = 'row_even';
  2925. }
  2926. $dirTypes = self::getChapterTypes();
  2927. if (in_array($item['type'], $dirTypes)) {
  2928. $scorm_color_background ='scorm_item_section ';
  2929. $style_item = '';
  2930. }
  2931. if ($item['id'] == $this->current) {
  2932. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2933. } elseif (!in_array($item['type'], $dirTypes)) {
  2934. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2935. }
  2936. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2937. // Learning path title
  2938. $title = $item['title'];
  2939. if (empty ($title)) {
  2940. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2941. }
  2942. $title = Security::remove_XSS($title);
  2943. // Learning path personalization
  2944. // build the LP tree
  2945. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2946. $description = $item['description'];
  2947. if (empty($description)) {
  2948. $description = $title;
  2949. }
  2950. if (in_array($item['type'], $dirTypes)) {
  2951. // Chapters
  2952. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2953. } else {
  2954. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2955. $html .= '<a name="atoc_'.$item['id'].'" />';
  2956. }
  2957. if (in_array($item['type'], $dirTypes)) {
  2958. // Chapter
  2959. // if you want to put an image before, you should use css
  2960. $html .= stripslashes($title);
  2961. } else {
  2962. $this->get_link('http', $item['id'], $toc_list);
  2963. $html .= '<a class="items-list" href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2964. }
  2965. $html .= "</div>";
  2966. if ($scorm_color_background != '') {
  2967. $html .= '</div>';
  2968. }
  2969. $color_counter++;
  2970. }
  2971. $html .= "</div>";
  2972. $html .= "</div>";
  2973. return $html;
  2974. }
  2975. /**
  2976. * Gets the learnpath maker name - generally the editor's name
  2977. * @return string Learnpath maker name
  2978. */
  2979. public function get_maker()
  2980. {
  2981. if ($this->debug > 0) {
  2982. error_log('New LP - In learnpath::get_maker()', 0);
  2983. }
  2984. if (!empty ($this->maker)) {
  2985. return $this->maker;
  2986. } else {
  2987. return '';
  2988. }
  2989. }
  2990. /**
  2991. * Gets the user-friendly message stored in $this->message
  2992. * @return string Message
  2993. */
  2994. public function get_message()
  2995. {
  2996. if ($this->debug > 0) {
  2997. error_log('New LP - In learnpath::get_message()', 0);
  2998. }
  2999. return $this->message;
  3000. }
  3001. /**
  3002. * Gets the learnpath name/title
  3003. * @return string Learnpath name/title
  3004. */
  3005. public function get_name()
  3006. {
  3007. if ($this->debug > 0) {
  3008. error_log('New LP - In learnpath::get_name()', 0);
  3009. }
  3010. if (!empty ($this->name)) {
  3011. return $this->name;
  3012. } else {
  3013. return 'N/A';
  3014. }
  3015. }
  3016. /**
  3017. * Gets a link to the resource from the present location, depending on item ID.
  3018. * @param string $type Type of link expected
  3019. * @param integer $item_id Learnpath item ID
  3020. * @return string $provided_toc Link to the lp_item resource
  3021. */
  3022. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3023. {
  3024. $course_id = $this->get_course_int_id();
  3025. if ($this->debug > 0) {
  3026. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3027. }
  3028. if (empty($item_id)) {
  3029. if ($this->debug > 2) {
  3030. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3031. }
  3032. $item_id = $this->get_current_item_id();
  3033. }
  3034. if (empty($item_id)) {
  3035. if ($this->debug > 2) {
  3036. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3037. }
  3038. //still empty, this means there was no item_id given and we are not in an object context or
  3039. //the object property is empty, return empty link
  3040. $item_id = $this->first();
  3041. return '';
  3042. }
  3043. $file = '';
  3044. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3045. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3046. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3047. $item_id = intval($item_id);
  3048. $sql = "SELECT
  3049. l.lp_type as ltype,
  3050. l.path as lpath,
  3051. li.item_type as litype,
  3052. li.path as lipath,
  3053. li.parameters as liparams
  3054. FROM $lp_table l
  3055. INNER JOIN $lp_item_table li
  3056. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3057. WHERE li.id = $item_id ";
  3058. if ($this->debug > 2) {
  3059. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3060. }
  3061. $res = Database::query($sql);
  3062. if (Database :: num_rows($res) > 0) {
  3063. $row = Database :: fetch_array($res);
  3064. $lp_type = $row['ltype'];
  3065. $lp_path = $row['lpath'];
  3066. $lp_item_type = $row['litype'];
  3067. $lp_item_path = $row['lipath'];
  3068. $lp_item_params = $row['liparams'];
  3069. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3070. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3071. }
  3072. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3073. if ($type == 'http') {
  3074. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3075. } else {
  3076. $course_path = $sys_course_path; //system path
  3077. }
  3078. // 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.
  3079. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3080. $lp_type = 1;
  3081. }
  3082. if ($this->debug > 2) {
  3083. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3084. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3085. }
  3086. // Now go through the specific cases to get the end of the path
  3087. // @todo Use constants instead of int values.
  3088. switch ($lp_type) {
  3089. case 1 :
  3090. if ($lp_item_type == 'dokeos_chapter') {
  3091. $file = 'lp_content.php?type=dir';
  3092. } else {
  3093. require_once 'resourcelinker.inc.php';
  3094. $file = rl_get_resource_link_for_learnpath(
  3095. $course_id,
  3096. $this->get_id(),
  3097. $item_id,
  3098. $this->get_view_id()
  3099. );
  3100. if ($this->debug > 0) {
  3101. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3102. }
  3103. if ($lp_item_type == 'link') {
  3104. if (Link::is_youtube_link($file)) {
  3105. $src = Link::get_youtube_video_id($file);
  3106. $file = 'embed.php?type=youtube&source='.$src;
  3107. } elseif (Link::isVimeoLink($file)) {
  3108. $src = Link::getVimeoLinkId($file);
  3109. $file = 'embed.php?type=vimeo&source='.$src;
  3110. } else {
  3111. // If the current site is HTTPS and the link is
  3112. // HTTP, browsers will refuse opening the link
  3113. $urlId = api_get_current_access_url_id();
  3114. $url = api_get_access_url($urlId, false);
  3115. $protocol = substr($url['url'], 0, 5);
  3116. if ($protocol === 'https') {
  3117. $linkProtocol = substr($file, 0, 5);
  3118. if ($linkProtocol === 'http:') {
  3119. //this is the special intervention case
  3120. $file = 'embed.php?type=nonhttps&source=' . urlencode($file);
  3121. }
  3122. }
  3123. }
  3124. } else {
  3125. // Check how much attempts of a exercise exits in lp
  3126. $lp_item_id = $this->get_current_item_id();
  3127. $lp_view_id = $this->get_view_id();
  3128. $prevent_reinit = null;
  3129. if (isset($this->items[$this->current])) {
  3130. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3131. }
  3132. if (empty($provided_toc)) {
  3133. if ($this->debug > 0) {
  3134. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3135. }
  3136. $list = $this->get_toc();
  3137. } else {
  3138. if ($this->debug > 0) {
  3139. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3140. }
  3141. $list = $provided_toc;
  3142. }
  3143. $type_quiz = false;
  3144. foreach ($list as $toc) {
  3145. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3146. $type_quiz = true;
  3147. }
  3148. }
  3149. if ($type_quiz) {
  3150. $lp_item_id = intval($lp_item_id);
  3151. $lp_view_id = intval($lp_view_id);
  3152. $sql = "SELECT count(*) FROM $lp_item_view_table
  3153. WHERE
  3154. c_id = $course_id AND
  3155. lp_item_id='" . $lp_item_id . "' AND
  3156. lp_view_id ='" . $lp_view_id . "' AND
  3157. status='completed'";
  3158. $result = Database::query($sql);
  3159. $row_count = Database :: fetch_row($result);
  3160. $count_item_view = (int) $row_count[0];
  3161. $not_multiple_attempt = 0;
  3162. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3163. $not_multiple_attempt = 1;
  3164. }
  3165. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3166. }
  3167. $tmp_array = explode('/', $file);
  3168. $document_name = $tmp_array[count($tmp_array) - 1];
  3169. if (strpos($document_name, '_DELETED_')) {
  3170. $file = 'blank.php?error=document_deleted';
  3171. }
  3172. }
  3173. }
  3174. break;
  3175. case 2 :
  3176. if ($this->debug > 2) {
  3177. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3178. }
  3179. if ($lp_item_type != 'dir') {
  3180. // Quite complex here:
  3181. // We want to make sure 'http://' (and similar) links can
  3182. // be loaded as is (withouth the Chamilo path in front) but
  3183. // some contents use this form: resource.htm?resource=http://blablabla
  3184. // which means we have to find a protocol at the path's start, otherwise
  3185. // it should not be considered as an external URL.
  3186. //if ($this->prerequisites_match($item_id)) {
  3187. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3188. if ($this->debug > 2) {
  3189. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3190. }
  3191. // Distant url, return as is.
  3192. $file = $lp_item_path;
  3193. } else {
  3194. if ($this->debug > 2) {
  3195. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3196. }
  3197. // Prevent getting untranslatable urls.
  3198. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3199. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3200. // Prepare the path.
  3201. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3202. // TODO: Fix this for urls with protocol header.
  3203. $file = str_replace('//', '/', $file);
  3204. $file = str_replace(':/', '://', $file);
  3205. if (substr($lp_path, -1) == '/') {
  3206. $lp_path = substr($lp_path, 0, -1);
  3207. }
  3208. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3209. // if file not found.
  3210. $decoded = html_entity_decode($lp_item_path);
  3211. list ($decoded) = explode('?', $decoded);
  3212. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3213. require_once 'resourcelinker.inc.php';
  3214. $file = rl_get_resource_link_for_learnpath(
  3215. $course_id,
  3216. $this->get_id(),
  3217. $item_id,
  3218. $this->get_view_id()
  3219. );
  3220. if (empty($file)) {
  3221. $file = 'blank.php?error=document_not_found';
  3222. } else {
  3223. $tmp_array = explode('/', $file);
  3224. $document_name = $tmp_array[count($tmp_array) - 1];
  3225. if (strpos($document_name, '_DELETED_')) {
  3226. $file = 'blank.php?error=document_deleted';
  3227. } else {
  3228. $file = 'blank.php?error=document_not_found';
  3229. }
  3230. }
  3231. } else {
  3232. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3233. }
  3234. }
  3235. }
  3236. // We want to use parameters if they were defined in the imsmanifest
  3237. if (strpos($file, 'blank.php') === false) {
  3238. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3239. }
  3240. } else {
  3241. $file = 'lp_content.php?type=dir';
  3242. }
  3243. break;
  3244. case 3 :
  3245. if ($this->debug > 2) {
  3246. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3247. }
  3248. // Formatting AICC HACP append URL.
  3249. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3250. if (!empty($lp_item_params)) {
  3251. $aicc_append .= $lp_item_params . '&';
  3252. }
  3253. if ($lp_item_type != 'dir') {
  3254. // Quite complex here:
  3255. // We want to make sure 'http://' (and similar) links can
  3256. // be loaded as is (withouth the Chamilo path in front) but
  3257. // some contents use this form: resource.htm?resource=http://blablabla
  3258. // which means we have to find a protocol at the path's start, otherwise
  3259. // it should not be considered as an external URL.
  3260. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3261. if ($this->debug > 2) {
  3262. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3263. }
  3264. // Distant url, return as is.
  3265. $file = $lp_item_path;
  3266. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3267. /*
  3268. if (stristr($file,'<servername>') !== false) {
  3269. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3270. }
  3271. */
  3272. if (stripos($file, '<servername>') !== false) {
  3273. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3274. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3275. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3276. }
  3277. //
  3278. $file .= $aicc_append;
  3279. } else {
  3280. if ($this->debug > 2) {
  3281. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3282. }
  3283. // Prevent getting untranslatable urls.
  3284. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3285. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3286. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3287. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3288. // TODO: Fix this for urls with protocol header.
  3289. $file = str_replace('//', '/', $file);
  3290. $file = str_replace(':/', '://', $file);
  3291. $file .= $aicc_append;
  3292. }
  3293. } else {
  3294. $file = 'lp_content.php?type=dir';
  3295. }
  3296. break;
  3297. case 4 :
  3298. break;
  3299. default :
  3300. break;
  3301. }
  3302. // Replace &amp; by & because &amp; will break URL with params
  3303. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3304. }
  3305. if ($this->debug > 2) {
  3306. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3307. }
  3308. return $file;
  3309. }
  3310. /**
  3311. * Gets the latest usable view or generate a new one
  3312. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3313. * @return integer DB lp_view id
  3314. */
  3315. public function get_view($attempt_num = 0)
  3316. {
  3317. if ($this->debug > 0) {
  3318. error_log('New LP - In learnpath::get_view()', 0);
  3319. }
  3320. $search = '';
  3321. // Use $attempt_num to enable multi-views management (disabled so far).
  3322. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3323. $search = 'AND view_count = ' . $attempt_num;
  3324. }
  3325. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3326. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3327. $course_id = api_get_course_int_id();
  3328. $sessionId = api_get_session_id();
  3329. $sql = "SELECT id, view_count FROM $lp_view_table
  3330. WHERE
  3331. c_id = " . $course_id . " AND
  3332. lp_id = " . $this->get_id() . " AND
  3333. user_id = " . $this->get_user_id() . " AND
  3334. session_id = $sessionId
  3335. $search
  3336. ORDER BY view_count DESC";
  3337. $res = Database::query($sql);
  3338. if (Database :: num_rows($res) > 0) {
  3339. $row = Database :: fetch_array($res);
  3340. $this->lp_view_id = $row['id'];
  3341. } else if (!api_is_invitee()) {
  3342. // There is no database record, create one.
  3343. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3344. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3345. Database::query($sql);
  3346. $id = Database :: insert_id();
  3347. $this->lp_view_id = $id;
  3348. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3349. Database::query($sql);
  3350. }
  3351. return $this->lp_view_id;
  3352. }
  3353. /**
  3354. * Gets the current view id
  3355. * @return integer View ID (from lp_view)
  3356. */
  3357. public function get_view_id()
  3358. {
  3359. if ($this->debug > 0) {
  3360. error_log('New LP - In learnpath::get_view_id()', 0);
  3361. }
  3362. if (!empty ($this->lp_view_id)) {
  3363. return $this->lp_view_id;
  3364. } else {
  3365. return 0;
  3366. }
  3367. }
  3368. /**
  3369. * Gets the update queue
  3370. * @return array Array containing IDs of items to be updated by JavaScript
  3371. */
  3372. public function get_update_queue()
  3373. {
  3374. if ($this->debug > 0) {
  3375. error_log('New LP - In learnpath::get_update_queue()', 0);
  3376. }
  3377. return $this->update_queue;
  3378. }
  3379. /**
  3380. * Gets the user ID
  3381. * @return integer User ID
  3382. */
  3383. public function get_user_id()
  3384. {
  3385. if ($this->debug > 0) {
  3386. error_log('New LP - In learnpath::get_user_id()', 0);
  3387. }
  3388. if (!empty ($this->user_id)) {
  3389. return $this->user_id;
  3390. } else {
  3391. return false;
  3392. }
  3393. }
  3394. /**
  3395. * Checks if any of the items has an audio element attached
  3396. * @return bool True or false
  3397. */
  3398. public function has_audio()
  3399. {
  3400. if ($this->debug > 1) {
  3401. error_log('New LP - In learnpath::has_audio()', 0);
  3402. }
  3403. $has = false;
  3404. foreach ($this->items as $i => $item) {
  3405. if (!empty ($this->items[$i]->audio)) {
  3406. $has = true;
  3407. break;
  3408. }
  3409. }
  3410. return $has;
  3411. }
  3412. /**
  3413. * Logs a message into a file
  3414. * @param string Message to log
  3415. * @return boolean True on success, false on error or if msg empty
  3416. */
  3417. public function log($msg)
  3418. {
  3419. if ($this->debug > 0) {
  3420. error_log('New LP - In learnpath::log()', 0);
  3421. }
  3422. // TODO
  3423. $this->error .= $msg;
  3424. return true;
  3425. }
  3426. /**
  3427. * Moves an item up and down at its level
  3428. * @param integer Item to move up and down
  3429. * @param string Direction 'up' or 'down'
  3430. * @return integer New display order, or false on error
  3431. */
  3432. public function move_item($id, $direction)
  3433. {
  3434. $course_id = api_get_course_int_id();
  3435. if ($this->debug > 0) {
  3436. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3437. }
  3438. if (empty($id) || empty($direction)) {
  3439. return false;
  3440. }
  3441. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3442. $sql_sel = "SELECT *
  3443. FROM " . $tbl_lp_item . "
  3444. WHERE c_id = ".$course_id." AND id = " . $id;
  3445. $res_sel = Database::query($sql_sel);
  3446. // Check if elem exists.
  3447. if (Database :: num_rows($res_sel) < 1) {
  3448. return false;
  3449. }
  3450. // Gather data.
  3451. $row = Database :: fetch_array($res_sel);
  3452. $previous = $row['previous_item_id'];
  3453. $next = $row['next_item_id'];
  3454. $display = $row['display_order'];
  3455. $parent = $row['parent_item_id'];
  3456. $lp = $row['lp_id'];
  3457. // Update the item (switch with previous/next one).
  3458. switch ($direction) {
  3459. case 'up':
  3460. if ($this->debug > 2) {
  3461. error_log('Movement up detected', 0);
  3462. }
  3463. if ($display <= 1) { /*do nothing*/
  3464. } else {
  3465. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3466. WHERE c_id = ".$course_id." AND id = $previous";
  3467. if ($this->debug > 2) {
  3468. error_log('Selecting previous: ' . $sql_sel2, 0);
  3469. }
  3470. $res_sel2 = Database::query($sql_sel2);
  3471. if (Database :: num_rows($res_sel2) < 1) {
  3472. $previous_previous = 0;
  3473. }
  3474. // Gather data.
  3475. $row2 = Database :: fetch_array($res_sel2);
  3476. $previous_previous = $row2['previous_item_id'];
  3477. // Update previous_previous item (switch "next" with current).
  3478. if ($previous_previous != 0) {
  3479. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3480. next_item_id = $id
  3481. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3482. if ($this->debug > 2) {
  3483. error_log($sql_upd2, 0);
  3484. }
  3485. Database::query($sql_upd2);
  3486. }
  3487. // Update previous item (switch with current).
  3488. if ($previous != 0) {
  3489. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3490. next_item_id = $next,
  3491. previous_item_id = $id,
  3492. display_order = display_order +1
  3493. WHERE c_id = ".$course_id." AND id = $previous";
  3494. if ($this->debug > 2) {
  3495. error_log($sql_upd2, 0);
  3496. }
  3497. Database::query($sql_upd2);
  3498. }
  3499. // Update current item (switch with previous).
  3500. if ($id != 0) {
  3501. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3502. next_item_id = $previous,
  3503. previous_item_id = $previous_previous,
  3504. display_order = display_order-1
  3505. WHERE c_id = ".$course_id." AND id = $id";
  3506. if ($this->debug > 2) {
  3507. error_log($sql_upd2, 0);
  3508. }
  3509. Database::query($sql_upd2);
  3510. }
  3511. // Update next item (new previous item).
  3512. if ($next != 0) {
  3513. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3514. WHERE c_id = ".$course_id." AND id = $next";
  3515. if ($this->debug > 2) {
  3516. error_log($sql_upd2, 0);
  3517. }
  3518. Database::query($sql_upd2);
  3519. }
  3520. $display = $display -1;
  3521. }
  3522. break;
  3523. case 'down':
  3524. if ($this->debug > 2) {
  3525. error_log('Movement down detected', 0);
  3526. }
  3527. if ($next == 0) { /* Do nothing. */
  3528. } else {
  3529. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3530. if ($this->debug > 2) {
  3531. error_log('Selecting next: ' . $sql_sel2, 0);
  3532. }
  3533. $res_sel2 = Database::query($sql_sel2);
  3534. if (Database :: num_rows($res_sel2) < 1) {
  3535. $next_next = 0;
  3536. }
  3537. // Gather data.
  3538. $row2 = Database :: fetch_array($res_sel2);
  3539. $next_next = $row2['next_item_id'];
  3540. // Update previous item (switch with current).
  3541. if ($previous != 0) {
  3542. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3543. WHERE c_id = ".$course_id." AND id = $previous";
  3544. Database::query($sql_upd2);
  3545. }
  3546. // Update current item (switch with previous).
  3547. if ($id != 0) {
  3548. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3549. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3550. WHERE c_id = ".$course_id." AND id = $id";
  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
  3556. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3557. WHERE c_id = ".$course_id." AND id = $next";
  3558. Database::query($sql_upd2);
  3559. }
  3560. // Update next_next item (switch "previous" with current).
  3561. if ($next_next != 0) {
  3562. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3563. previous_item_id = $id
  3564. WHERE c_id = ".$course_id." AND id = $next_next";
  3565. Database::query($sql_upd2);
  3566. }
  3567. $display = $display +1;
  3568. }
  3569. break;
  3570. default :
  3571. return false;
  3572. }
  3573. return $display;
  3574. }
  3575. /**
  3576. * Move a learnpath up (display_order)
  3577. * @param integer Learnpath ID
  3578. */
  3579. public function move_up($lp_id)
  3580. {
  3581. $course_id = api_get_course_int_id();
  3582. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3583. $sql = "SELECT * FROM $lp_table
  3584. WHERE c_id = ".$course_id."
  3585. ORDER BY display_order";
  3586. $res = Database::query($sql);
  3587. if ($res === false)
  3588. return false;
  3589. $lps = array ();
  3590. $lp_order = array ();
  3591. $num = Database :: num_rows($res);
  3592. // First check the order is correct, globally (might be wrong because
  3593. // of versions < 1.8.4)
  3594. if ($num > 0) {
  3595. $i = 1;
  3596. while ($row = Database :: fetch_array($res)) {
  3597. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3598. $need_fix = true;
  3599. $sql_u = "UPDATE $lp_table SET display_order = $i
  3600. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3601. Database::query($sql_u);
  3602. }
  3603. $row['display_order'] = $i;
  3604. $lps[$row['id']] = $row;
  3605. $lp_order[$i] = $row['id'];
  3606. $i++;
  3607. }
  3608. }
  3609. if ($num > 1) { // If there's only one element, no need to sort.
  3610. $order = $lps[$lp_id]['display_order'];
  3611. if ($order > 1) { // If it's the first element, no need to move up.
  3612. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3613. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3614. Database::query($sql_u1);
  3615. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3616. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3617. Database::query($sql_u2);
  3618. }
  3619. }
  3620. }
  3621. /**
  3622. * Move a learnpath down (display_order)
  3623. * @param integer Learnpath ID
  3624. */
  3625. public function move_down($lp_id)
  3626. {
  3627. $course_id = api_get_course_int_id();
  3628. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3629. $sql = "SELECT * FROM $lp_table
  3630. WHERE c_id = ".$course_id."
  3631. ORDER BY display_order";
  3632. $res = Database::query($sql);
  3633. if ($res === false)
  3634. return false;
  3635. $lps = array ();
  3636. $lp_order = array ();
  3637. $num = Database :: num_rows($res);
  3638. $max = 0;
  3639. // First check the order is correct, globally (might be wrong because
  3640. // of versions < 1.8.4).
  3641. if ($num > 0) {
  3642. $i = 1;
  3643. while ($row = Database :: fetch_array($res)) {
  3644. $max = $i;
  3645. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3646. $need_fix = true;
  3647. $sql_u = "UPDATE $lp_table SET display_order = $i
  3648. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3649. $res_u = Database::query($sql_u);
  3650. }
  3651. $row['display_order'] = $i;
  3652. $lps[$row['id']] = $row;
  3653. $lp_order[$i] = $row['id'];
  3654. $i++;
  3655. }
  3656. }
  3657. if ($num > 1) { // If there's only one element, no need to sort.
  3658. $order = $lps[$lp_id]['display_order'];
  3659. if ($order < $max) { // If it's the first element, no need to move up.
  3660. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3661. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3662. Database::query($sql_u1);
  3663. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3664. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3665. Database::query($sql_u2);
  3666. }
  3667. }
  3668. }
  3669. /**
  3670. * Updates learnpath attributes to point to the next element
  3671. * The last part is similar to set_current_item but processing the other way around
  3672. */
  3673. public function next()
  3674. {
  3675. if ($this->debug > 0) {
  3676. error_log('New LP - In learnpath::next()', 0);
  3677. }
  3678. $this->last = $this->get_current_item_id();
  3679. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3680. $this->autocomplete_parents($this->last);
  3681. $new_index = $this->get_next_index();
  3682. if ($this->debug > 2) {
  3683. error_log('New LP - New index: ' . $new_index, 0);
  3684. }
  3685. $this->index = $new_index;
  3686. if ($this->debug > 2) {
  3687. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3688. }
  3689. $this->current = $this->ordered_items[$new_index];
  3690. if ($this->debug > 2) {
  3691. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3692. }
  3693. }
  3694. /**
  3695. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3696. * class, this might be redefined to allow several behaviours depending on the document type.
  3697. * @param integer Resource ID
  3698. * @return boolean True on success, false otherwise
  3699. */
  3700. public function open($id)
  3701. {
  3702. if ($this->debug > 0) {
  3703. error_log('New LP - In learnpath::open()', 0);
  3704. }
  3705. // TODO:
  3706. // set the current resource attribute to this resource
  3707. // switch on element type (redefine in child class?)
  3708. // set status for this item to "opened"
  3709. // start timer
  3710. // initialise score
  3711. $this->index = 0; //or = the last item seen (see $this->last)
  3712. }
  3713. /**
  3714. * Check that all prerequisites are fulfilled. Returns true and an
  3715. * empty string on succes, returns false
  3716. * and the prerequisite string on error.
  3717. * This function is based on the rules for aicc_script language as
  3718. * described in the SCORM 1.2 CAM documentation page 108.
  3719. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3720. * @return boolean True if prerequisites are matched, false otherwise -
  3721. * Empty string if true returned, prerequisites string otherwise.
  3722. */
  3723. public function prerequisites_match($itemId = null)
  3724. {
  3725. $debug = $this->debug;
  3726. if ($debug > 0) {
  3727. error_log('In learnpath::prerequisites_match()', 0);
  3728. }
  3729. if (empty($itemId)) {
  3730. $itemId = $this->current;
  3731. }
  3732. $currentItem = $this->getItem($itemId);
  3733. if ($currentItem) {
  3734. if ($this->type == 2) {
  3735. // Getting prereq from scorm
  3736. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3737. } else {
  3738. $prereq_string = $currentItem->get_prereq_string();
  3739. }
  3740. if (empty($prereq_string)) {
  3741. if ($debug > 0) {
  3742. error_log('Found prereq_string is empty return true');
  3743. }
  3744. return true;
  3745. }
  3746. // Clean spaces.
  3747. $prereq_string = str_replace(' ', '', $prereq_string);
  3748. if ($debug > 0) {
  3749. error_log('Found prereq_string: ' . $prereq_string, 0);
  3750. }
  3751. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3752. $result = $currentItem->parse_prereq(
  3753. $prereq_string,
  3754. $this->items,
  3755. $this->refs_list,
  3756. $this->get_user_id()
  3757. );
  3758. if ($result === false) {
  3759. $this->set_error_msg($currentItem->prereq_alert);
  3760. }
  3761. } else {
  3762. $result = true;
  3763. if ($debug > 1) {
  3764. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3765. }
  3766. }
  3767. if ($debug > 1) {
  3768. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3769. }
  3770. return $result;
  3771. }
  3772. /**
  3773. * Updates learnpath attributes to point to the previous element
  3774. * The last part is similar to set_current_item but processing the other way around
  3775. */
  3776. public function previous()
  3777. {
  3778. if ($this->debug > 0) {
  3779. error_log('New LP - In learnpath::previous()', 0);
  3780. }
  3781. $this->last = $this->get_current_item_id();
  3782. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3783. $this->autocomplete_parents($this->last);
  3784. $new_index = $this->get_previous_index();
  3785. $this->index = $new_index;
  3786. $this->current = $this->ordered_items[$new_index];
  3787. }
  3788. /**
  3789. * Publishes a learnpath. This basically means show or hide the learnpath
  3790. * to normal users.
  3791. * Can be used as abstract
  3792. * @param integer Learnpath ID
  3793. * @param string New visibility
  3794. */
  3795. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3796. {
  3797. $action = 'visible';
  3798. if ($set_visibility != 1) {
  3799. $action = 'invisible';
  3800. self::toggle_publish($lp_id, 'i');
  3801. }
  3802. return api_item_property_update(
  3803. api_get_course_info(),
  3804. TOOL_LEARNPATH,
  3805. $lp_id,
  3806. $action,
  3807. api_get_user_id()
  3808. );
  3809. }
  3810. /**
  3811. * Publishes a learnpath. This basically means show or hide the learnpath
  3812. * on the course homepage
  3813. * Can be used as abstract
  3814. * @param integer $lp_id Learnpath id
  3815. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3816. * @return bool
  3817. */
  3818. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3819. {
  3820. $course_id = api_get_course_int_id();
  3821. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3822. $lp_id = intval($lp_id);
  3823. $sql = "SELECT * FROM $tbl_lp
  3824. WHERE c_id = ".$course_id." AND id = $lp_id";
  3825. $result = Database::query($sql);
  3826. if (Database::num_rows($result)) {
  3827. $row = Database :: fetch_array($result);
  3828. $name = domesticate($row['name']);
  3829. if ($set_visibility == 'i') {
  3830. $s = $name . " " . get_lang('LearnpathNotPublished');
  3831. $dialogBox = $s;
  3832. $v = 0;
  3833. }
  3834. if ($set_visibility == 'v') {
  3835. $s = $name . " " . get_lang('LearnpathPublished');
  3836. $dialogBox = $s;
  3837. $v = 1;
  3838. }
  3839. } else {
  3840. return false;
  3841. }
  3842. $session_id = api_get_session_id();
  3843. $session_condition = api_get_session_condition($session_id);
  3844. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3845. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3846. $sql = "SELECT * FROM $tbl_tool
  3847. WHERE
  3848. c_id = ".$course_id." AND
  3849. link='$link' and
  3850. image='scormbuilder.gif' and
  3851. link LIKE '$link%'
  3852. $session_condition
  3853. ";
  3854. $result = Database::query($sql);
  3855. $num = Database :: num_rows($result);
  3856. if ($set_visibility == 'i' && $num > 0) {
  3857. $sql = "DELETE FROM $tbl_tool
  3858. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3859. Database::query($sql);
  3860. } elseif ($set_visibility == 'v' && $num == 0) {
  3861. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3862. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3863. Database::query($sql);
  3864. $insertId = Database::insert_id();
  3865. if ($insertId) {
  3866. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3867. Database::query($sql);
  3868. }
  3869. } elseif ($set_visibility == 'v' && $num > 0) {
  3870. $sql = "UPDATE $tbl_tool SET
  3871. c_id = $course_id,
  3872. name = '$name',
  3873. link = '$link',
  3874. image = 'scormbuilder.gif',
  3875. visibility = '$v',
  3876. admin = '0',
  3877. address = 'pastillegris.gif',
  3878. added_tool = 0,
  3879. session_id = $session_id
  3880. WHERE
  3881. c_id = ".$course_id." AND
  3882. (link='$link' and image='scormbuilder.gif' $session_condition)
  3883. ";
  3884. Database::query($sql);
  3885. } else {
  3886. // Parameter and database incompatible, do nothing, exit.
  3887. return false;
  3888. }
  3889. }
  3890. /**
  3891. * Restart the whole learnpath. Return the URL of the first element.
  3892. * Make sure the results are saved with anoter method. This method should probably be
  3893. * redefined in children classes.
  3894. * To use a similar method statically, use the create_new_attempt() method
  3895. * @return string URL to load in the viewer
  3896. */
  3897. public function restart()
  3898. {
  3899. if ($this->debug > 0) {
  3900. error_log('New LP - In learnpath::restart()', 0);
  3901. }
  3902. // TODO
  3903. // Call autosave method to save the current progress.
  3904. //$this->index = 0;
  3905. if (api_is_invitee()) {
  3906. return false;
  3907. }
  3908. $session_id = api_get_session_id();
  3909. $course_id = api_get_course_int_id();
  3910. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3911. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3912. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3913. if ($this->debug > 2) {
  3914. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3915. }
  3916. $res = Database::query($sql);
  3917. $view_id = Database::insert_id();
  3918. if ($view_id) {
  3919. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3920. Database::query($sql);
  3921. $this->lp_view_id = $view_id;
  3922. $this->attempt = $this->attempt + 1;
  3923. } else {
  3924. $this->error = 'Could not insert into item_view table...';
  3925. return false;
  3926. }
  3927. $this->autocomplete_parents($this->current);
  3928. foreach ($this->items as $index => $dummy) {
  3929. $this->items[$index]->restart();
  3930. $this->items[$index]->set_lp_view($this->lp_view_id);
  3931. }
  3932. $this->first();
  3933. return true;
  3934. }
  3935. /**
  3936. * Saves the current item
  3937. * @return boolean
  3938. */
  3939. public function save_current()
  3940. {
  3941. if ($this->debug > 0) {
  3942. error_log('learnpath::save_current()', 0);
  3943. }
  3944. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3945. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3946. if ($this->debug > 2) {
  3947. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3948. }
  3949. if ($this->debug > 2) {
  3950. error_log('' . print_r($this->items, true), 0);
  3951. }
  3952. if (isset($this->items[$this->current]) &&
  3953. is_object($this->items[$this->current])
  3954. ) {
  3955. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3956. $this->autocomplete_parents($this->current);
  3957. $status = $this->items[$this->current]->get_status();
  3958. $this->append_message('new_item_status: ' . $status);
  3959. $this->update_queue[$this->current] = $status;
  3960. return $res;
  3961. }
  3962. return false;
  3963. }
  3964. /**
  3965. * Saves the given item
  3966. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3967. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3968. * @return boolean
  3969. */
  3970. public function save_item($item_id = null, $from_outside = true)
  3971. {
  3972. $debug = $this->debug;
  3973. if ($debug) {
  3974. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3975. }
  3976. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3977. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3978. if (empty($item_id)) {
  3979. $item_id = intval($_REQUEST['id']);
  3980. }
  3981. if (empty($item_id)) {
  3982. $item_id = $this->get_current_item_id();
  3983. }
  3984. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3985. if ($debug) {
  3986. error_log('Object exists');
  3987. }
  3988. // Saving the item.
  3989. $res = $this->items[$item_id]->save(
  3990. $from_outside,
  3991. $this->prerequisites_match($item_id)
  3992. );
  3993. if ($debug) {
  3994. error_log('update_queue before:');
  3995. error_log(print_r($this->update_queue,1));
  3996. }
  3997. $this->autocomplete_parents($item_id);
  3998. $status = $this->items[$item_id]->get_status();
  3999. $this->update_queue[$item_id] = $status;
  4000. if ($debug) {
  4001. error_log('get_status(): ' . $status);
  4002. error_log('update_queue after:');
  4003. error_log(print_r($this->update_queue,1));
  4004. }
  4005. return $res;
  4006. }
  4007. return false;
  4008. }
  4009. /**
  4010. * Saves the last item seen's ID only in case
  4011. */
  4012. public function save_last()
  4013. {
  4014. $course_id = api_get_course_int_id();
  4015. if ($this->debug > 0) {
  4016. error_log('New LP - In learnpath::save_last()', 0);
  4017. }
  4018. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4019. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4020. if (isset($this->current) && !api_is_invitee()) {
  4021. if ($this->debug > 2) {
  4022. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4023. }
  4024. $sql = "UPDATE $table SET
  4025. last_item = " . intval($this->get_current_item_id()). "
  4026. WHERE
  4027. c_id = $course_id AND
  4028. lp_id = " . $this->get_id() . " AND
  4029. user_id = " . $this->get_user_id()." ".$session_condition;
  4030. if ($this->debug > 2) {
  4031. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4032. }
  4033. Database::query($sql);
  4034. }
  4035. if (!api_is_invitee()) {
  4036. // Save progress.
  4037. list($progress, $text) = $this->get_progress_bar_text('%');
  4038. if ($progress >= 0 && $progress <= 100) {
  4039. $progress = (int) $progress;
  4040. $sql = "UPDATE $table SET
  4041. progress = $progress
  4042. WHERE
  4043. c_id = ".$course_id." AND
  4044. lp_id = " . $this->get_id() . " AND
  4045. user_id = " . $this->get_user_id()." ".$session_condition;
  4046. // Ignore errors as some tables might not have the progress field just yet.
  4047. Database::query($sql);
  4048. $this->progress_db = $progress;
  4049. }
  4050. }
  4051. }
  4052. /**
  4053. * Sets the current item ID (checks if valid and authorized first)
  4054. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4055. */
  4056. public function set_current_item($item_id = null)
  4057. {
  4058. if ($this->debug > 0) {
  4059. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4060. }
  4061. if (empty ($item_id)) {
  4062. if ($this->debug > 2) {
  4063. error_log('New LP - No new current item given, ignore...', 0);
  4064. }
  4065. // Do nothing.
  4066. } else {
  4067. if ($this->debug > 2) {
  4068. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4069. }
  4070. if (is_numeric($item_id)) {
  4071. $item_id = intval($item_id);
  4072. // TODO: Check in database here.
  4073. $this->last = $this->current;
  4074. $this->current = $item_id;
  4075. // TODO: Update $this->index as well.
  4076. foreach ($this->ordered_items as $index => $item) {
  4077. if ($item == $this->current) {
  4078. $this->index = $index;
  4079. break;
  4080. }
  4081. }
  4082. if ($this->debug > 2) {
  4083. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4084. }
  4085. } else {
  4086. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4087. }
  4088. }
  4089. }
  4090. /**
  4091. * Sets the encoding
  4092. * @param string New encoding
  4093. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4094. */
  4095. public function set_encoding($enc = 'UTF-8')
  4096. {
  4097. if ($this->debug > 0) {
  4098. error_log('New LP - In learnpath::set_encoding()', 0);
  4099. }
  4100. $course_id = api_get_course_int_id();
  4101. $enc = api_refine_encoding_id($enc);
  4102. if (empty($enc)) {
  4103. $enc = api_get_system_encoding();
  4104. }
  4105. if (api_is_encoding_supported($enc)) {
  4106. $lp = $this->get_id();
  4107. if ($lp != 0) {
  4108. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4109. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4110. $res = Database::query($sql);
  4111. return $res;
  4112. }
  4113. }
  4114. return false;
  4115. }
  4116. /**
  4117. * Sets the JS lib setting in the database directly.
  4118. * This is the JavaScript library file this lp needs to load on startup
  4119. * @param string Proximity setting
  4120. * @return boolean True on update success. False otherwise.
  4121. */
  4122. public function set_jslib($lib = '')
  4123. {
  4124. if ($this->debug > 0) {
  4125. error_log('New LP - In learnpath::set_jslib()', 0);
  4126. }
  4127. $lp = $this->get_id();
  4128. $course_id = api_get_course_int_id();
  4129. if ($lp != 0) {
  4130. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4131. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4132. $res = Database::query($sql);
  4133. return $res;
  4134. } else {
  4135. return false;
  4136. }
  4137. }
  4138. /**
  4139. * Sets the name of the LP maker (publisher) (and save)
  4140. * @param string Optional string giving the new content_maker of this learnpath
  4141. * @return boolean True
  4142. */
  4143. public function set_maker($name = '')
  4144. {
  4145. if ($this->debug > 0) {
  4146. error_log('New LP - In learnpath::set_maker()', 0);
  4147. }
  4148. if (empty ($name))
  4149. return false;
  4150. $this->maker = $name;
  4151. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4152. $course_id = api_get_course_int_id();
  4153. $lp_id = $this->get_id();
  4154. $sql = "UPDATE $lp_table SET
  4155. content_maker = '" . Database::escape_string($this->maker) . "'
  4156. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4157. if ($this->debug > 2) {
  4158. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4159. }
  4160. Database::query($sql);
  4161. return true;
  4162. }
  4163. /**
  4164. * Sets the name of the current learnpath (and save)
  4165. * @param string $name Optional string giving the new name of this learnpath
  4166. * @return boolean True/False
  4167. */
  4168. public function set_name($name = null)
  4169. {
  4170. if ($this->debug > 0) {
  4171. error_log('New LP - In learnpath::set_name()', 0);
  4172. }
  4173. if (empty($name)) {
  4174. return false;
  4175. }
  4176. $this->name = $name;
  4177. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4178. $lp_id = $this->get_id();
  4179. $course_id = api_get_course_int_id();
  4180. $sql = "UPDATE $lp_table SET
  4181. name = '" . Database::escape_string($this->name). "'
  4182. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4183. if ($this->debug > 2) {
  4184. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4185. }
  4186. $result = Database::query($sql);
  4187. // If the lp is visible on the homepage, change his name there.
  4188. if (Database::affected_rows($result)) {
  4189. $session_id = api_get_session_id();
  4190. $session_condition = api_get_session_condition($session_id);
  4191. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4192. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4193. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4194. WHERE
  4195. c_id = $course_id AND
  4196. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4197. Database::query($sql);
  4198. return true;
  4199. } else {
  4200. return false;
  4201. }
  4202. }
  4203. /**
  4204. * Set index specified prefix terms for all items in this path
  4205. * @param string Comma-separated list of terms
  4206. * @param char Xapian term prefix
  4207. * @return boolean False on error, true otherwise
  4208. */
  4209. public function set_terms_by_prefix($terms_string, $prefix)
  4210. {
  4211. $course_id = api_get_course_int_id();
  4212. if (api_get_setting('search_enabled') !== 'true')
  4213. return false;
  4214. if (!extension_loaded('xapian')) {
  4215. return false;
  4216. }
  4217. $terms_string = trim($terms_string);
  4218. $terms = explode(',', $terms_string);
  4219. array_walk($terms, 'trim_value');
  4220. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4221. // Don't do anything if no change, verify only at DB, not the search engine.
  4222. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4223. return false;
  4224. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4225. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4226. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4227. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4228. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4229. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4230. $lp_id = intval($_POST['lp_id']);
  4231. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4232. $result = Database::query($sql);
  4233. $di = new ChamiloIndexer();
  4234. while ($lp_item = Database :: fetch_array($result)) {
  4235. // Get search_did.
  4236. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4237. $sql = 'SELECT * FROM %s
  4238. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4239. LIMIT 1';
  4240. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4241. //echo $sql; echo '<br>';
  4242. $res = Database::query($sql);
  4243. if (Database::num_rows($res) > 0) {
  4244. $se_ref = Database :: fetch_array($res);
  4245. // Compare terms.
  4246. $doc = $di->get_document($se_ref['search_did']);
  4247. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4248. $xterms = array();
  4249. foreach ($xapian_terms as $xapian_term) {
  4250. $xterms[] = substr($xapian_term['name'], 1);
  4251. }
  4252. $dterms = $terms;
  4253. $missing_terms = array_diff($dterms, $xterms);
  4254. $deprecated_terms = array_diff($xterms, $dterms);
  4255. // Save it to search engine.
  4256. foreach ($missing_terms as $term) {
  4257. $doc->add_term($prefix . $term, 1);
  4258. }
  4259. foreach ($deprecated_terms as $term) {
  4260. $doc->remove_term($prefix . $term);
  4261. }
  4262. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4263. $di->getDb()->flush();
  4264. } else {
  4265. //@todo What we should do here?
  4266. }
  4267. }
  4268. return true;
  4269. }
  4270. /**
  4271. * Sets the theme of the LP (local/remote) (and save)
  4272. * @param string Optional string giving the new theme of this learnpath
  4273. * @return bool Returns true if theme name is not empty
  4274. */
  4275. public function set_theme($name = '')
  4276. {
  4277. $course_id = api_get_course_int_id();
  4278. if ($this->debug > 0) {
  4279. error_log('New LP - In learnpath::set_theme()', 0);
  4280. }
  4281. $this->theme = $name;
  4282. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4283. $lp_id = $this->get_id();
  4284. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4285. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4286. if ($this->debug > 2) {
  4287. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4288. }
  4289. Database::query($sql);
  4290. return true;
  4291. }
  4292. /**
  4293. * Sets the image of an LP (and save)
  4294. * @param string Optional string giving the new image of this learnpath
  4295. * @return bool Returns true if theme name is not empty
  4296. */
  4297. public function set_preview_image($name = '')
  4298. {
  4299. $course_id = api_get_course_int_id();
  4300. if ($this->debug > 0) {
  4301. error_log('New LP - In learnpath::set_preview_image()', 0);
  4302. }
  4303. $this->preview_image = $name;
  4304. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4305. $lp_id = $this->get_id();
  4306. $sql = "UPDATE $lp_table SET
  4307. preview_image = '" . Database::escape_string($this->preview_image). "'
  4308. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4309. if ($this->debug > 2) {
  4310. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4311. }
  4312. Database::query($sql);
  4313. return true;
  4314. }
  4315. /**
  4316. * Sets the author of a LP (and save)
  4317. * @param string Optional string giving the new author of this learnpath
  4318. * @return bool Returns true if author's name is not empty
  4319. */
  4320. public function set_author($name = '')
  4321. {
  4322. $course_id = api_get_course_int_id();
  4323. if ($this->debug > 0) {
  4324. error_log('New LP - In learnpath::set_author()', 0);
  4325. }
  4326. $this->author = $name;
  4327. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4328. $lp_id = $this->get_id();
  4329. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4330. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4331. if ($this->debug > 2) {
  4332. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4333. }
  4334. Database::query($sql);
  4335. return true;
  4336. }
  4337. /**
  4338. * Sets the hide_toc_frame parameter of a LP (and save)
  4339. * @param int 1 if frame is hidden 0 then else
  4340. * @return bool Returns true if author's name is not empty
  4341. */
  4342. public function set_hide_toc_frame($hide)
  4343. {
  4344. $course_id = api_get_course_int_id();
  4345. if ($this->debug > 0) {
  4346. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4347. }
  4348. if (intval($hide) == $hide){
  4349. $this->hide_toc_frame = $hide;
  4350. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4351. $lp_id = $this->get_id();
  4352. $sql = "UPDATE $lp_table SET
  4353. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4354. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4355. if ($this->debug > 2) {
  4356. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4357. }
  4358. Database::query($sql);
  4359. return true;
  4360. } else {
  4361. return false;
  4362. }
  4363. }
  4364. /**
  4365. * Sets the prerequisite of a LP (and save)
  4366. * @param int integer giving the new prerequisite of this learnpath
  4367. * @return bool returns true if prerequisite is not empty
  4368. */
  4369. public function set_prerequisite($prerequisite)
  4370. {
  4371. $course_id = api_get_course_int_id();
  4372. if ($this->debug > 0) {
  4373. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4374. }
  4375. $this->prerequisite = intval($prerequisite);
  4376. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4377. $lp_id = $this->get_id();
  4378. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4379. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4380. if ($this->debug > 2) {
  4381. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4382. }
  4383. Database::query($sql);
  4384. return true;
  4385. }
  4386. /**
  4387. * Sets the location/proximity of the LP (local/remote) (and save)
  4388. * @param string Optional string giving the new location of this learnpath
  4389. * @return boolean True on success / False on error
  4390. */
  4391. public function set_proximity($name = '')
  4392. {
  4393. $course_id = api_get_course_int_id();
  4394. if ($this->debug > 0) {
  4395. error_log('New LP - In learnpath::set_proximity()', 0);
  4396. }
  4397. if (empty ($name))
  4398. return false;
  4399. $this->proximity = $name;
  4400. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4401. $lp_id = $this->get_id();
  4402. $sql = "UPDATE $lp_table SET
  4403. content_local = '" . Database::escape_string($name) . "'
  4404. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4405. if ($this->debug > 2) {
  4406. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4407. }
  4408. Database::query($sql);
  4409. return true;
  4410. }
  4411. /**
  4412. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4413. * @param integer DB ID of the item
  4414. */
  4415. public function set_previous_item($id)
  4416. {
  4417. if ($this->debug > 0) {
  4418. error_log('New LP - In learnpath::set_previous_item()', 0);
  4419. }
  4420. $this->last = $id;
  4421. }
  4422. /**
  4423. * Sets use_max_score
  4424. * @param string $use_max_score Optional string giving the new location of this learnpath
  4425. * @return boolean True on success / False on error
  4426. */
  4427. public function set_use_max_score($use_max_score = 1)
  4428. {
  4429. $course_id = api_get_course_int_id();
  4430. if ($this->debug > 0) {
  4431. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4432. }
  4433. $use_max_score = intval($use_max_score);
  4434. $this->use_max_score = $use_max_score;
  4435. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4436. $lp_id = $this->get_id();
  4437. $sql = "UPDATE $lp_table SET
  4438. use_max_score = '" . $this->use_max_score . "'
  4439. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4440. if ($this->debug > 2) {
  4441. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4442. }
  4443. Database::query($sql);
  4444. return true;
  4445. }
  4446. /**
  4447. * Sets and saves the expired_on date
  4448. * @param string $expired_on Optional string giving the new author of this learnpath
  4449. * @return bool Returns true if author's name is not empty
  4450. */
  4451. public function set_expired_on($expired_on)
  4452. {
  4453. $course_id = api_get_course_int_id();
  4454. if ($this->debug > 0) {
  4455. error_log('New LP - In learnpath::set_expired_on()', 0);
  4456. }
  4457. if (!empty($expired_on)) {
  4458. $this->expired_on = api_get_utc_datetime($expired_on);
  4459. } else {
  4460. $this->expired_on = '';
  4461. }
  4462. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4463. $lp_id = $this->get_id();
  4464. $sql = "UPDATE $lp_table SET
  4465. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4466. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4467. if ($this->debug > 2) {
  4468. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4469. }
  4470. Database::query($sql);
  4471. return true;
  4472. }
  4473. /**
  4474. * Sets and saves the publicated_on date
  4475. * @param string $publicated_on Optional string giving the new author of this learnpath
  4476. * @return bool Returns true if author's name is not empty
  4477. */
  4478. public function set_publicated_on($publicated_on)
  4479. {
  4480. $course_id = api_get_course_int_id();
  4481. if ($this->debug > 0) {
  4482. error_log('New LP - In learnpath::set_expired_on()', 0);
  4483. }
  4484. if (!empty($publicated_on)) {
  4485. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4486. } else {
  4487. $this->publicated_on = '';
  4488. }
  4489. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4490. $lp_id = $this->get_id();
  4491. $sql = "UPDATE $lp_table SET
  4492. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4493. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4494. if ($this->debug > 2) {
  4495. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4496. }
  4497. Database::query($sql);
  4498. return true;
  4499. }
  4500. /**
  4501. * Sets and saves the expired_on date
  4502. * @return bool Returns true if author's name is not empty
  4503. */
  4504. public function set_modified_on()
  4505. {
  4506. $course_id = api_get_course_int_id();
  4507. if ($this->debug > 0) {
  4508. error_log('New LP - In learnpath::set_expired_on()', 0);
  4509. }
  4510. $this->modified_on = api_get_utc_datetime();
  4511. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4512. $lp_id = $this->get_id();
  4513. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4514. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4515. if ($this->debug > 2) {
  4516. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4517. }
  4518. Database::query($sql);
  4519. return true;
  4520. }
  4521. /**
  4522. * Sets the object's error message
  4523. * @param string Error message. If empty, reinits the error string
  4524. * @return void
  4525. */
  4526. public function set_error_msg($error = '')
  4527. {
  4528. if ($this->debug > 0) {
  4529. error_log('New LP - In learnpath::set_error_msg()', 0);
  4530. }
  4531. if (empty ($error)) {
  4532. $this->error = '';
  4533. } else {
  4534. $this->error .= $error;
  4535. }
  4536. }
  4537. /**
  4538. * Launches the current item if not 'sco'
  4539. * (starts timer and make sure there is a record ready in the DB)
  4540. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4541. * @return boolean
  4542. */
  4543. public function start_current_item($allow_new_attempt = false)
  4544. {
  4545. if ($this->debug > 0) {
  4546. error_log('New LP - In learnpath::start_current_item()', 0);
  4547. }
  4548. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4549. $type = $this->get_type();
  4550. $item_type = $this->items[$this->current]->get_type();
  4551. if (($type == 2 && $item_type != 'sco') ||
  4552. ($type == 3 && $item_type != 'au') ||
  4553. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4554. ) {
  4555. $this->items[$this->current]->open($allow_new_attempt);
  4556. $this->autocomplete_parents($this->current);
  4557. $prereq_check = $this->prerequisites_match($this->current);
  4558. $this->items[$this->current]->save(false, $prereq_check);
  4559. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4560. } else {
  4561. // If sco, then it is supposed to have been updated by some other call.
  4562. }
  4563. if ($item_type == 'sco') {
  4564. $this->items[$this->current]->restart();
  4565. }
  4566. }
  4567. if ($this->debug > 0) {
  4568. error_log('New LP - End of learnpath::start_current_item()', 0);
  4569. }
  4570. return true;
  4571. }
  4572. /**
  4573. * Stops the processing and counters for the old item (as held in $this->last)
  4574. * @return boolean True/False
  4575. */
  4576. public function stop_previous_item()
  4577. {
  4578. if ($this->debug > 0) {
  4579. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4580. }
  4581. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4582. if ($this->debug > 2) {
  4583. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4584. }
  4585. switch ($this->get_type()) {
  4586. case '3' :
  4587. if ($this->items[$this->last]->get_type() != 'au') {
  4588. if ($this->debug > 2) {
  4589. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4590. }
  4591. $this->items[$this->last]->close();
  4592. //$this->autocomplete_parents($this->last);
  4593. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4594. } else {
  4595. if ($this->debug > 2) {
  4596. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4597. }
  4598. }
  4599. case '2' :
  4600. if ($this->items[$this->last]->get_type() != 'sco') {
  4601. if ($this->debug > 2) {
  4602. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4603. }
  4604. $this->items[$this->last]->close();
  4605. //$this->autocomplete_parents($this->last);
  4606. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4607. } else {
  4608. if ($this->debug > 2) {
  4609. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4610. }
  4611. }
  4612. break;
  4613. case '1' :
  4614. default :
  4615. if ($this->debug > 2) {
  4616. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4617. }
  4618. $this->items[$this->last]->close();
  4619. break;
  4620. }
  4621. } else {
  4622. if ($this->debug > 2) {
  4623. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4624. }
  4625. return false;
  4626. }
  4627. return true;
  4628. }
  4629. /**
  4630. * Updates the default view mode from fullscreen to embedded and inversely
  4631. * @return string The current default view mode ('fullscreen' or 'embedded')
  4632. */
  4633. public function update_default_view_mode()
  4634. {
  4635. $course_id = api_get_course_int_id();
  4636. if ($this->debug > 0) {
  4637. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4638. }
  4639. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4640. $sql = "SELECT * FROM $lp_table
  4641. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4642. $res = Database::query($sql);
  4643. if (Database :: num_rows($res) > 0) {
  4644. $row = Database :: fetch_array($res);
  4645. $default_view_mode = $row['default_view_mod'];
  4646. $view_mode = $default_view_mode;
  4647. switch ($default_view_mode) {
  4648. case 'fullscreen':
  4649. $view_mode = 'embedded';
  4650. break;
  4651. case 'embedded':
  4652. $view_mode = 'embedframe';
  4653. break;
  4654. case 'embedframe':
  4655. $view_mode = 'impress';
  4656. break;
  4657. case 'impress':
  4658. $view_mode = 'fullscreen';
  4659. break;
  4660. }
  4661. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4662. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4663. Database::query($sql);
  4664. $this->mode = $view_mode;
  4665. return $view_mode;
  4666. } else {
  4667. if ($this->debug > 2) {
  4668. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4669. }
  4670. }
  4671. return -1;
  4672. }
  4673. /**
  4674. * Updates the default behaviour about auto-commiting SCORM updates
  4675. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4676. */
  4677. public function update_default_scorm_commit()
  4678. {
  4679. $course_id = api_get_course_int_id();
  4680. if ($this->debug > 0) {
  4681. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4682. }
  4683. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4684. $sql = "SELECT * FROM $lp_table
  4685. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4686. $res = Database::query($sql);
  4687. if (Database :: num_rows($res) > 0) {
  4688. $row = Database :: fetch_array($res);
  4689. $force = $row['force_commit'];
  4690. if ($force == 1) {
  4691. $force = 0;
  4692. $force_return = false;
  4693. } elseif ($force == 0) {
  4694. $force = 1;
  4695. $force_return = true;
  4696. }
  4697. $sql = "UPDATE $lp_table SET force_commit = $force
  4698. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4699. Database::query($sql);
  4700. $this->force_commit = $force_return;
  4701. return $force_return;
  4702. } else {
  4703. if ($this->debug > 2) {
  4704. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4705. }
  4706. }
  4707. return -1;
  4708. }
  4709. /**
  4710. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4711. * @return bool True on success, false on failure
  4712. */
  4713. public function update_display_order()
  4714. {
  4715. $course_id = api_get_course_int_id();
  4716. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4717. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4718. $res = Database::query($sql);
  4719. if ($res === false)
  4720. return false;
  4721. $num = Database :: num_rows($res);
  4722. // First check the order is correct, globally (might be wrong because
  4723. // of versions < 1.8.4).
  4724. if ($num > 0) {
  4725. $i = 1;
  4726. while ($row = Database :: fetch_array($res)) {
  4727. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4728. $need_fix = true;
  4729. $sql = "UPDATE $lp_table SET display_order = $i
  4730. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4731. Database::query($sql);
  4732. }
  4733. $i++;
  4734. }
  4735. }
  4736. return true;
  4737. }
  4738. /**
  4739. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4740. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4741. */
  4742. public function update_reinit()
  4743. {
  4744. $course_id = api_get_course_int_id();
  4745. if ($this->debug > 0) {
  4746. error_log('New LP - In learnpath::update_reinit()', 0);
  4747. }
  4748. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4749. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4750. $res = Database::query($sql);
  4751. if (Database :: num_rows($res) > 0) {
  4752. $row = Database :: fetch_array($res);
  4753. $force = $row['prevent_reinit'];
  4754. if ($force == 1) {
  4755. $force = 0;
  4756. } elseif ($force == 0) {
  4757. $force = 1;
  4758. }
  4759. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4760. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4761. Database::query($sql);
  4762. $this->prevent_reinit = $force;
  4763. return $force;
  4764. } else {
  4765. if ($this->debug > 2) {
  4766. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4767. }
  4768. }
  4769. return -1;
  4770. }
  4771. /**
  4772. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4773. *
  4774. * @return string 'single', 'multi' or 'seriousgame'
  4775. * @author ndiechburg <noel@cblue.be>
  4776. **/
  4777. public function get_attempt_mode()
  4778. {
  4779. //Set default value for seriousgame_mode
  4780. if (!isset($this->seriousgame_mode)) {
  4781. $this->seriousgame_mode=0;
  4782. }
  4783. // Set default value for prevent_reinit
  4784. if (!isset($this->prevent_reinit)) {
  4785. $this->prevent_reinit =1;
  4786. }
  4787. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4788. return 'seriousgame';
  4789. }
  4790. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4791. return 'single';
  4792. }
  4793. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4794. return 'multiple';
  4795. }
  4796. return 'single';
  4797. }
  4798. /**
  4799. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4800. *
  4801. * @param string 'seriousgame', 'single' or 'multiple'
  4802. * @return boolean
  4803. * @author ndiechburg <noel@cblue.be>
  4804. **/
  4805. public function set_attempt_mode($mode)
  4806. {
  4807. $course_id = api_get_course_int_id();
  4808. switch ($mode) {
  4809. case 'seriousgame' :
  4810. $sg_mode = 1;
  4811. $prevent_reinit = 1;
  4812. break;
  4813. case 'single' :
  4814. $sg_mode = 0;
  4815. $prevent_reinit = 1;
  4816. break;
  4817. case 'multiple' :
  4818. $sg_mode = 0;
  4819. $prevent_reinit = 0;
  4820. break;
  4821. default :
  4822. $sg_mode = 0;
  4823. $prevent_reinit = 0;
  4824. break;
  4825. }
  4826. $this->prevent_reinit = $prevent_reinit;
  4827. $this->seriousgame_mode = $sg_mode;
  4828. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4829. $sql = "UPDATE $lp_table SET
  4830. prevent_reinit = $prevent_reinit ,
  4831. seriousgame_mode = $sg_mode
  4832. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4833. $res = Database::query($sql);
  4834. if ($res) {
  4835. return true;
  4836. } else {
  4837. return false;
  4838. }
  4839. }
  4840. /**
  4841. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4842. *
  4843. * @return boolean
  4844. * @author ndiechburg <noel@cblue.be>
  4845. **/
  4846. public function switch_attempt_mode()
  4847. {
  4848. if ($this->debug > 0) {
  4849. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4850. }
  4851. $mode = $this->get_attempt_mode();
  4852. switch ($mode) {
  4853. case 'single' :
  4854. $next_mode = 'multiple';
  4855. break;
  4856. case 'multiple' :
  4857. $next_mode = 'seriousgame';
  4858. break;
  4859. case 'seriousgame' :
  4860. $next_mode = 'single';
  4861. break;
  4862. default :
  4863. $next_mode = 'single';
  4864. break;
  4865. }
  4866. $this->set_attempt_mode($next_mode);
  4867. }
  4868. /**
  4869. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4870. * but possibility to do again a completed item.
  4871. *
  4872. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4873. * @author ndiechburg <noel@cblue.be>
  4874. **/
  4875. public function set_seriousgame_mode()
  4876. {
  4877. $course_id = api_get_course_int_id();
  4878. if ($this->debug > 0) {
  4879. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4880. }
  4881. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4882. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4883. $res = Database::query($sql);
  4884. if (Database :: num_rows($res) > 0) {
  4885. $row = Database :: fetch_array($res);
  4886. $force = $row['seriousgame_mode'];
  4887. if ($force == 1) {
  4888. $force = 0;
  4889. } elseif ($force == 0) {
  4890. $force = 1;
  4891. }
  4892. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4893. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4894. Database::query($sql);
  4895. $this->seriousgame_mode = $force;
  4896. return $force;
  4897. } else {
  4898. if ($this->debug > 2) {
  4899. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4900. }
  4901. }
  4902. return -1;
  4903. }
  4904. /**
  4905. * Updates the "scorm_debug" value that shows or hide the debug window
  4906. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4907. */
  4908. public function update_scorm_debug()
  4909. {
  4910. $course_id = api_get_course_int_id();
  4911. if ($this->debug > 0) {
  4912. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4913. }
  4914. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4915. $sql = "SELECT * FROM $lp_table
  4916. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4917. $res = Database::query($sql);
  4918. if (Database :: num_rows($res) > 0) {
  4919. $row = Database :: fetch_array($res);
  4920. $force = $row['debug'];
  4921. if ($force == 1) {
  4922. $force = 0;
  4923. } elseif ($force == 0) {
  4924. $force = 1;
  4925. }
  4926. $sql = "UPDATE $lp_table SET debug = $force
  4927. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4928. $res = Database::query($sql);
  4929. $this->scorm_debug = $force;
  4930. return $force;
  4931. } else {
  4932. if ($this->debug > 2) {
  4933. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4934. }
  4935. }
  4936. return -1;
  4937. }
  4938. /**
  4939. * Function that makes a call to the function sort_tree_array and create_tree_array
  4940. * @author Kevin Van Den Haute
  4941. * @param array
  4942. */
  4943. public function tree_array($array)
  4944. {
  4945. if ($this->debug > 1) {
  4946. error_log('New LP - In learnpath::tree_array()', 0);
  4947. }
  4948. $array = $this->sort_tree_array($array);
  4949. $this->create_tree_array($array);
  4950. }
  4951. /**
  4952. * Creates an array with the elements of the learning path tree in it
  4953. *
  4954. * @author Kevin Van Den Haute
  4955. * @param array $array
  4956. * @param int $parent
  4957. * @param int $depth
  4958. * @param array $tmp
  4959. */
  4960. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4961. {
  4962. if ($this->debug > 1) {
  4963. error_log('New LP - In learnpath::create_tree_array())', 0);
  4964. }
  4965. if (is_array($array)) {
  4966. for ($i = 0; $i < count($array); $i++) {
  4967. if ($array[$i]['parent_item_id'] == $parent) {
  4968. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4969. $tmp[] = $array[$i]['parent_item_id'];
  4970. $depth++;
  4971. }
  4972. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4973. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4974. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4975. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4976. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4977. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4978. $this->arrMenu[] = array(
  4979. 'id' => $array[$i]['id'],
  4980. 'ref' => $ref,
  4981. 'item_type' => $array[$i]['item_type'],
  4982. 'title' => $array[$i]['title'],
  4983. 'path' => $path,
  4984. 'description' => $array[$i]['description'],
  4985. 'parent_item_id' => $array[$i]['parent_item_id'],
  4986. 'previous_item_id' => $array[$i]['previous_item_id'],
  4987. 'next_item_id' => $array[$i]['next_item_id'],
  4988. 'min_score' => $array[$i]['min_score'],
  4989. 'max_score' => $array[$i]['max_score'],
  4990. 'mastery_score' => $array[$i]['mastery_score'],
  4991. 'display_order' => $array[$i]['display_order'],
  4992. 'prerequisite' => $preq,
  4993. 'depth' => $depth,
  4994. 'audio' => $audio,
  4995. 'prerequisite_min_score' => $prerequisiteMinScore,
  4996. 'prerequisite_max_score' => $prerequisiteMaxScore
  4997. );
  4998. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4999. }
  5000. }
  5001. }
  5002. }
  5003. /**
  5004. * Sorts a multi dimensional array by parent id and display order
  5005. * @author Kevin Van Den Haute
  5006. *
  5007. * @param array $array (array with al the learning path items in it)
  5008. *
  5009. * @return array
  5010. */
  5011. public function sort_tree_array($array) {
  5012. foreach ($array as $key => $row) {
  5013. $parent[$key] = $row['parent_item_id'];
  5014. $position[$key] = $row['display_order'];
  5015. }
  5016. if (count($array) > 0)
  5017. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5018. return $array;
  5019. }
  5020. /**
  5021. * Function that creates a html list of learning path items so that we can add audio files to them
  5022. * @author Kevin Van Den Haute
  5023. * @param int $lp_id
  5024. * @return string
  5025. */
  5026. public function overview()
  5027. {
  5028. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5029. $_course = api_get_course_info();
  5030. if ($this->debug > 0) {
  5031. error_log('New LP - In learnpath::overview()', 0);
  5032. }
  5033. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5034. $return = '';
  5035. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5036. // we need to start a form when we want to update all the mp3 files
  5037. if ($update_audio == 'true') {
  5038. $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">';
  5039. }
  5040. $return .= '<div id="message"></div>';
  5041. if (count($this->items) == 0) {
  5042. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5043. } else {
  5044. $return_audio = '<table class="data_table">';
  5045. $return_audio .= '<tr>';
  5046. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5047. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5048. $return_audio .= '</tr>';
  5049. if ($update_audio != 'true') {
  5050. $return .= '<div class="col-md-12">';
  5051. $return .= self::return_new_tree($update_audio);
  5052. $return .='</div>';
  5053. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5054. } else {
  5055. $return_audio .= self::return_new_tree($update_audio);
  5056. $return .= $return_audio.'</table>';
  5057. }
  5058. // We need to close the form when we are updating the mp3 files.
  5059. if ($update_audio == 'true') {
  5060. $return .= '<div class="footer-audio">';
  5061. $return .= Display::button('save_audio','<i class="fa fa-file-audio-o"></i> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5062. $return .= '</div>';
  5063. //$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?
  5064. }
  5065. }
  5066. // We need to close the form when we are updating the mp3 files.
  5067. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5068. $return .= '</form>';
  5069. }
  5070. return $return;
  5071. }
  5072. /**
  5073. * @param string string $update_audio
  5074. * @param bool $drop_element_here
  5075. * @return string
  5076. */
  5077. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5078. {
  5079. $return = '';
  5080. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5081. $course_id = api_get_course_int_id();
  5082. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5083. $sql = "SELECT * FROM $tbl_lp_item
  5084. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5085. $result = Database::query($sql);
  5086. $arrLP = array();
  5087. while ($row = Database :: fetch_array($result)) {
  5088. $arrLP[] = array(
  5089. 'id' => $row['id'],
  5090. 'item_type' => $row['item_type'],
  5091. 'title' => Security :: remove_XSS($row['title']),
  5092. 'path' => $row['path'],
  5093. 'description' => Security::remove_XSS($row['description']),
  5094. 'parent_item_id' => $row['parent_item_id'],
  5095. 'previous_item_id' => $row['previous_item_id'],
  5096. 'next_item_id' => $row['next_item_id'],
  5097. 'max_score' => $row['max_score'],
  5098. 'min_score' => $row['min_score'],
  5099. 'mastery_score' => $row['mastery_score'],
  5100. 'prerequisite' => $row['prerequisite'],
  5101. 'display_order' => $row['display_order'],
  5102. 'audio' => $row['audio'],
  5103. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5104. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5105. );
  5106. }
  5107. $this->tree_array($arrLP);
  5108. $arrLP = $this->arrMenu;
  5109. unset ($this->arrMenu);
  5110. $default_data = null;
  5111. $default_content = null;
  5112. $elements = array();
  5113. $return_audio = null;
  5114. for ($i = 0; $i < count($arrLP); $i++) {
  5115. $title = $arrLP[$i]['title'];
  5116. $title_cut = cut($arrLP[$i]['title'], 25);
  5117. //Link for the documents
  5118. if ($arrLP[$i]['item_type'] == 'document') {
  5119. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5120. $title_cut = Display::url(
  5121. $title_cut,
  5122. $url,
  5123. array(
  5124. 'class' => 'ajax',
  5125. 'data-title' => $title_cut
  5126. )
  5127. );
  5128. }
  5129. if (($i % 2) == 0) {
  5130. $oddClass = 'row_odd';
  5131. } else {
  5132. $oddClass = 'row_even';
  5133. }
  5134. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5135. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5136. $icon = '';
  5137. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5138. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5139. } else {
  5140. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5141. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5142. } else {
  5143. $icon = '<img src="../img/folder_document.gif" />';
  5144. }
  5145. }
  5146. // The audio column.
  5147. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5148. $audio = '';
  5149. if (!$update_audio || $update_audio <> 'true') {
  5150. if (!empty($arrLP[$i]['audio'])) {
  5151. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5152. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5153. $audio .= '<script type="text/javascript">
  5154. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5155. s1.addParam("allowscriptaccess","always");
  5156. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5157. s1.write("container' . $i . '");
  5158. </script>';*/
  5159. } else {
  5160. $audio .= '';
  5161. }
  5162. } else {
  5163. $types = self::getChapterTypes();
  5164. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5165. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5166. if (!empty ($arrLP[$i]['audio'])) {
  5167. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5168. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5169. }
  5170. }
  5171. }
  5172. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5173. $return_audio .= '</td>';
  5174. $move_icon = '';
  5175. $move_item_icon = '';
  5176. $edit_icon = '';
  5177. $delete_icon = '';
  5178. $audio_icon = '';
  5179. $prerequisities_icon = '';
  5180. if ($is_allowed_to_edit) {
  5181. if (!$update_audio || $update_audio <> 'true') {
  5182. $move_icon .= '<a class="moved" href="#">';
  5183. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5184. $move_icon .= '</a>';
  5185. }
  5186. // No edit for this item types
  5187. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5188. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5189. $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'] . '">';
  5190. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5191. $edit_icon .= '</a>';
  5192. } else {
  5193. $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'] . '">';
  5194. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5195. $edit_icon .= '</a>';
  5196. }
  5197. }
  5198. $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) . '\');">';
  5199. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5200. $delete_icon .= '</a>';
  5201. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5202. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5203. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5204. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5205. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5206. }
  5207. }
  5208. if ($update_audio != 'true') {
  5209. $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'));
  5210. } else {
  5211. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5212. }
  5213. $parent_id = $arrLP[$i]['parent_item_id'];
  5214. $default_data[$arrLP[$i]['id']] = $row;
  5215. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5216. if (empty($parent_id)) {
  5217. $elements[$arrLP[$i]['id']]['data'] = $row;
  5218. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5219. } else {
  5220. $parent_arrays = array();
  5221. if ($arrLP[$i]['depth'] > 1) {
  5222. //Getting list of parents
  5223. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5224. foreach($arrLP as $item) {
  5225. if ($item['id'] == $parent_id) {
  5226. if ($item['parent_item_id'] == 0) {
  5227. $parent_id = $item['id'];
  5228. break;
  5229. } else {
  5230. $parent_id = $item['parent_item_id'];
  5231. if (empty($parent_arrays)) {
  5232. $parent_arrays[] = intval($item['id']);
  5233. }
  5234. $parent_arrays[] = $parent_id;
  5235. break;
  5236. }
  5237. }
  5238. }
  5239. }
  5240. }
  5241. if (!empty($parent_arrays)) {
  5242. $parent_arrays = array_reverse($parent_arrays);
  5243. $val = '$elements';
  5244. $x = 0;
  5245. foreach($parent_arrays as $item) {
  5246. if ($x != count($parent_arrays) -1) {
  5247. $val .= '["'.$item.'"]["children"]';
  5248. } else {
  5249. $val .= '["'.$item.'"]["children"]';
  5250. }
  5251. $x++;
  5252. }
  5253. $val .= "";
  5254. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5255. eval($code_str);
  5256. } else {
  5257. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5258. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5259. }
  5260. }
  5261. }
  5262. $list = '<ul id="lp_item_list">';
  5263. $tree = self::print_recursive($elements, $default_data, $default_content);
  5264. if (!empty($tree)) {
  5265. $list .= $tree;
  5266. } else {
  5267. if ($drop_element_here) {
  5268. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5269. }
  5270. }
  5271. $list .= '</ul>';
  5272. $return .= Display::panel($list, $this->name);
  5273. if ($update_audio == 'true') {
  5274. $return = $return_audio;
  5275. }
  5276. return $return;
  5277. }
  5278. /**
  5279. * @param array $elements
  5280. * @param array $default_data
  5281. * @param array $default_content
  5282. * @return string
  5283. */
  5284. public function print_recursive($elements, $default_data, $default_content)
  5285. {
  5286. $return = '';
  5287. foreach ($elements as $key => $item) {
  5288. if (isset($item['load_data']) || empty($item['data'])) {
  5289. $item['data'] = $default_data[$item['load_data']];
  5290. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5291. }
  5292. $sub_list = '';
  5293. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5294. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5295. }
  5296. if (empty($item['children'])) {
  5297. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5298. $active = null;
  5299. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5300. $active = 'active';
  5301. }
  5302. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5303. } else {
  5304. //sections
  5305. if (isset($item['children'])) {
  5306. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5307. }
  5308. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5309. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5310. }
  5311. }
  5312. return $return;
  5313. }
  5314. /**
  5315. * This function builds the action menu
  5316. * @param bool $returnContent
  5317. * @return void
  5318. */
  5319. public function build_action_menu($returnContent = false)
  5320. {
  5321. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5322. $return = '<div class="actions">';
  5323. $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> ';
  5324. $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>';
  5325. $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>';
  5326. $buttons = array(
  5327. array(
  5328. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5329. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5330. ),
  5331. array(
  5332. 'title' => get_lang('ClearAllPrerequisites'),
  5333. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5334. ),
  5335. );
  5336. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5337. $return .= '</div>';
  5338. if ($returnContent) {
  5339. return $return;
  5340. }
  5341. echo $return;
  5342. }
  5343. /**
  5344. * Creates the default learning path folder
  5345. * @param array $course
  5346. * @return bool
  5347. */
  5348. public static function generate_learning_path_folder($course)
  5349. {
  5350. // Creating learning_path folder
  5351. $dir = '/learning_path';
  5352. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5353. $folder = false;
  5354. if (!is_dir($filepath.'/'.$dir)) {
  5355. $folderData = create_unexisting_directory(
  5356. $course,
  5357. api_get_user_id(),
  5358. api_get_session_id(),
  5359. 0,
  5360. 0,
  5361. $filepath,
  5362. $dir,
  5363. get_lang('LearningPaths'),
  5364. 0
  5365. );
  5366. if (!empty($folderData)) {
  5367. $folder = true;
  5368. }
  5369. } else {
  5370. $folder = true;
  5371. }
  5372. return $folder;
  5373. }
  5374. /**
  5375. * @param array $course
  5376. * @param string $lp_name
  5377. * @return array
  5378. */
  5379. public function generate_lp_folder($course, $lp_name = null)
  5380. {
  5381. $filepath = '';
  5382. $dir = '/learning_path/';
  5383. if (empty($lp_name)) {
  5384. $lp_name = $this->name;
  5385. }
  5386. $folder = self::generate_learning_path_folder($course);
  5387. // Creating LP folder
  5388. if ($folder) {
  5389. //Limits title size
  5390. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5391. $dir = $dir.$title;
  5392. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5393. if (!is_dir($filepath.'/'.$dir)) {
  5394. $folderData = create_unexisting_directory(
  5395. $course,
  5396. api_get_user_id(),
  5397. 0,
  5398. 0,
  5399. 0,
  5400. $filepath,
  5401. $dir,
  5402. $lp_name
  5403. );
  5404. if (!empty($folderData)) {
  5405. $folder = true;
  5406. }
  5407. } else {
  5408. $folder = true;
  5409. }
  5410. $dir = $dir.'/';
  5411. if ($folder) {
  5412. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5413. }
  5414. }
  5415. $array = array(
  5416. 'dir' => $dir,
  5417. 'filepath' => $filepath,
  5418. 'folder' => $folder
  5419. );
  5420. return $array;
  5421. }
  5422. /**
  5423. * Create a new document //still needs some finetuning
  5424. * @param array $_course
  5425. * @return string
  5426. */
  5427. public function create_document($_course)
  5428. {
  5429. $course_id = api_get_course_int_id();
  5430. global $charset;
  5431. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5432. if (strstr($dir, '..'))
  5433. $dir = '/';
  5434. if ($dir[0] == '.')
  5435. $dir = substr($dir, 1);
  5436. if ($dir[0] != '/')
  5437. $dir = '/' . $dir;
  5438. if ($dir[strlen($dir) - 1] != '/')
  5439. $dir .= '/';
  5440. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5441. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5442. //Generates folder
  5443. $result = $this->generate_lp_folder($_course);
  5444. $dir = $result['dir'];
  5445. $filepath = $result['filepath'];
  5446. }
  5447. if (!is_dir($filepath)) {
  5448. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5449. $dir = '/';
  5450. }
  5451. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5452. // is already escaped twice when it gets here.
  5453. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5454. $title = disable_dangerous_file($title);
  5455. $filename = $title;
  5456. $content = $_POST['content_lp'];
  5457. $tmp_filename = $filename;
  5458. $i = 0;
  5459. while (file_exists($filepath . $tmp_filename . '.html'))
  5460. $tmp_filename = $filename . '_' . ++ $i;
  5461. $filename = $tmp_filename . '.html';
  5462. $content = stripslashes($content);
  5463. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5464. // Change the path of mp3 to absolute.
  5465. // The first regexp deals with :// urls.
  5466. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5467. // The second regexp deals with audio/ urls.
  5468. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5469. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5470. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5471. if (!file_exists($filepath . $filename)) {
  5472. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5473. fputs($fp, $content);
  5474. fclose($fp);
  5475. $file_size = filesize($filepath . $filename);
  5476. $save_file_path = $dir.$filename;
  5477. $document_id = add_document(
  5478. $_course,
  5479. $save_file_path,
  5480. 'file',
  5481. $file_size,
  5482. $tmp_filename
  5483. );
  5484. if ($document_id) {
  5485. api_item_property_update(
  5486. $_course,
  5487. TOOL_DOCUMENT,
  5488. $document_id,
  5489. 'DocumentAdded',
  5490. api_get_user_id(),
  5491. null,
  5492. null,
  5493. null,
  5494. null,
  5495. api_get_session_id()
  5496. );
  5497. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5498. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5499. if ($new_comment || $new_title) {
  5500. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5501. $ct = '';
  5502. if ($new_comment)
  5503. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5504. if ($new_title)
  5505. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5506. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5507. Database::query($sql_update);
  5508. }
  5509. }
  5510. return $document_id;
  5511. }
  5512. }
  5513. }
  5514. /**
  5515. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5516. * @param array $_course array
  5517. * @return void
  5518. */
  5519. public function edit_document($_course)
  5520. {
  5521. $course_id = api_get_course_int_id();
  5522. global $_configuration;
  5523. // Please, do not modify this dirname formatting.
  5524. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5525. if (strstr($dir, '..'))
  5526. $dir = '/';
  5527. if ($dir[0] == '.')
  5528. $dir = substr($dir, 1);
  5529. if ($dir[0] != '/')
  5530. $dir = '/' . $dir;
  5531. if ($dir[strlen($dir) - 1] != '/')
  5532. $dir .= '/';
  5533. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5534. if (!is_dir($filepath)) {
  5535. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5536. $dir = '/';
  5537. }
  5538. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5539. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5540. $document_id = intval($_POST['path']);
  5541. $sql = "SELECT path FROM " . $table_doc . "
  5542. WHERE c_id = $course_id AND id = " . $document_id;
  5543. $res = Database::query($sql);
  5544. $row = Database :: fetch_array($res);
  5545. $content = stripslashes($_POST['content_lp']);
  5546. $file = $filepath . $row['path'];
  5547. if ($fp = @ fopen($file, 'w')) {
  5548. $content = text_filter($content);
  5549. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5550. // Change the path of mp3 to absolute.
  5551. // The first regexp deals with :// urls.
  5552. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5553. // The second regexp deals with audio/ urls.
  5554. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5555. fputs($fp, $content);
  5556. fclose($fp);
  5557. $sql = "UPDATE " . $table_doc ." SET
  5558. title='".Database::escape_string($_POST['title'])."'
  5559. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5560. Database::query($sql);
  5561. }
  5562. }
  5563. }
  5564. /**
  5565. * Displays the selected item, with a panel for manipulating the item
  5566. * @param int $item_id
  5567. * @param string $msg
  5568. * @return string
  5569. */
  5570. public function display_item($item_id, $msg = null, $show_actions = true)
  5571. {
  5572. $course_id = api_get_course_int_id();
  5573. $return = '';
  5574. if (is_numeric($item_id)) {
  5575. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5576. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5577. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5578. $result = Database::query($sql);
  5579. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5580. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5581. // Prevents wrong parent selection for document, see Bug#1251.
  5582. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5583. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5584. }
  5585. if ($show_actions) {
  5586. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5587. }
  5588. $return .= '<div style="padding:10px;">';
  5589. if ($msg != '')
  5590. $return .= $msg;
  5591. $return .= '<h3>'.$row['title'].'</h3>';
  5592. switch ($row['item_type']) {
  5593. case TOOL_QUIZ:
  5594. if (!empty($row['path'])) {
  5595. $exercise = new Exercise();
  5596. $exercise->read($row['path']);
  5597. $return .= $exercise->description.'<br />';
  5598. }
  5599. break;
  5600. case TOOL_DOCUMENT:
  5601. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5602. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5603. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5604. $result = Database::query($sql_doc);
  5605. $path_file = Database::result($result, 0, 0);
  5606. $path_parts = pathinfo($path_file);
  5607. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5608. if (in_array($path_parts['extension'], array(
  5609. 'html',
  5610. 'txt',
  5611. 'png',
  5612. 'jpg',
  5613. 'JPG',
  5614. 'jpeg',
  5615. 'JPEG',
  5616. 'gif',
  5617. 'swf'
  5618. ))) {
  5619. $return .= $this->display_document($row['path'], true, true);
  5620. }
  5621. break;
  5622. }
  5623. $return .= '</div>';
  5624. }
  5625. }
  5626. return $return;
  5627. }
  5628. /**
  5629. * Shows the needed forms for editing a specific item
  5630. * @param int $item_id
  5631. * @return string
  5632. */
  5633. public function display_edit_item($item_id)
  5634. {
  5635. $course_id = api_get_course_int_id();
  5636. $return = '';
  5637. if (is_numeric($item_id)) {
  5638. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5639. $sql = "SELECT * FROM $tbl_lp_item
  5640. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5641. $res = Database::query($sql);
  5642. $row = Database::fetch_array($res);
  5643. switch ($row['item_type']) {
  5644. case 'dokeos_chapter' :
  5645. case 'dir' :
  5646. case 'asset' :
  5647. case 'sco' :
  5648. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5649. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5650. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5651. } else {
  5652. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5653. }
  5654. break;
  5655. case TOOL_DOCUMENT :
  5656. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5657. $sql = "SELECT lp.*, doc.path as dir
  5658. FROM " . $tbl_lp_item . " as lp
  5659. LEFT JOIN " . $tbl_doc . " as doc
  5660. ON doc.id = lp.path
  5661. WHERE
  5662. lp.c_id = $course_id AND
  5663. doc.c_id = $course_id AND
  5664. lp.id = " . intval($item_id);
  5665. $res_step = Database::query($sql);
  5666. $row_step = Database :: fetch_array($res_step);
  5667. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5668. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5669. break;
  5670. case TOOL_LINK :
  5671. $link_id = (string) $row['path'];
  5672. if (ctype_digit($link_id)) {
  5673. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5674. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5675. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5676. $res_link = Database::query($sql_select);
  5677. $row_link = Database :: fetch_array($res_link);
  5678. if (is_array($row_link)) {
  5679. $row['url'] = $row_link['url'];
  5680. }
  5681. }
  5682. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5683. $return .= $this->display_link_form('edit', $item_id, $row);
  5684. break;
  5685. case 'dokeos_module' :
  5686. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5687. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5688. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5689. } else {
  5690. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5691. }
  5692. break;
  5693. case TOOL_QUIZ :
  5694. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5695. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5696. break;
  5697. case TOOL_HOTPOTATOES :
  5698. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5699. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5700. break;
  5701. case TOOL_STUDENTPUBLICATION :
  5702. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5703. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5704. break;
  5705. case TOOL_FORUM :
  5706. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5707. $return .= $this->display_forum_form('edit', $item_id, $row);
  5708. break;
  5709. case TOOL_THREAD :
  5710. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5711. $return .= $this->display_thread_form('edit', $item_id, $row);
  5712. break;
  5713. }
  5714. }
  5715. return $return;
  5716. }
  5717. /**
  5718. * Function that displays a list with al the resources that
  5719. * could be added to the learning path
  5720. * @return string
  5721. */
  5722. public function display_resources()
  5723. {
  5724. $course_code = api_get_course_id();
  5725. // Get all the docs.
  5726. $documents = $this->get_documents(true);
  5727. // Get all the exercises.
  5728. $exercises = $this->get_exercises();
  5729. // Get all the links.
  5730. $links = $this->get_links();
  5731. // Get al the student publications.
  5732. $works = $this->get_student_publications();
  5733. // Get al the forums.
  5734. $forums = $this->get_forums(null, $course_code);
  5735. $headers = array(
  5736. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5737. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5738. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5739. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5740. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5741. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5742. );
  5743. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5744. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5745. echo Display::tabs(
  5746. $headers,
  5747. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5748. );
  5749. return true;
  5750. }
  5751. /**
  5752. * Returns the extension of a document
  5753. * @param string filename
  5754. * @return string Extension (part after the last dot)
  5755. */
  5756. public function get_extension($filename)
  5757. {
  5758. $explode = explode('.', $filename);
  5759. return $explode[count($explode) - 1];
  5760. }
  5761. /**
  5762. * Displays a document by id
  5763. *
  5764. * @param int $id
  5765. * @return string
  5766. */
  5767. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5768. {
  5769. $_course = api_get_course_info();
  5770. $course_id = api_get_course_int_id();
  5771. $return = '';
  5772. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5773. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5774. WHERE c_id = ".$course_id." AND id = " . $id;
  5775. $res_doc = Database::query($sql_doc);
  5776. $row_doc = Database :: fetch_array($res_doc);
  5777. // TODO: Add a path filter.
  5778. if ($iframe) {
  5779. $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>';
  5780. } else {
  5781. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5782. }
  5783. return $return;
  5784. }
  5785. /**
  5786. * Return HTML form to add/edit a quiz
  5787. * @param string Action (add/edit)
  5788. * @param integer Item ID if already exists
  5789. * @param mixed Extra information (quiz ID if integer)
  5790. * @return string HTML form
  5791. */
  5792. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5793. {
  5794. $course_id = api_get_course_int_id();
  5795. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5796. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5797. if ($id != 0 && is_array($extra_info)) {
  5798. $item_title = $extra_info['title'];
  5799. $item_description = $extra_info['description'];
  5800. } elseif (is_numeric($extra_info)) {
  5801. $sql = "SELECT title, description
  5802. FROM " . $tbl_quiz . "
  5803. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5804. $result = Database::query($sql);
  5805. $row = Database::fetch_array($result);
  5806. $item_title = $row['title'];
  5807. $item_description = $row['description'];
  5808. } else {
  5809. $item_title = '';
  5810. $item_description = '';
  5811. }
  5812. $item_title = Security::remove_XSS($item_title);
  5813. $item_description = Security::remove_XSS($item_description);
  5814. $legend = '<legend>';
  5815. if ($id != 0 && is_array($extra_info))
  5816. $parent = $extra_info['parent_item_id'];
  5817. else
  5818. $parent = 0;
  5819. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5820. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5821. $result = Database::query($sql);
  5822. $arrLP = array ();
  5823. while ($row = Database :: fetch_array($result)) {
  5824. $arrLP[] = array (
  5825. 'id' => $row['id'],
  5826. 'item_type' => $row['item_type'],
  5827. 'title' => $row['title'],
  5828. 'path' => $row['path'],
  5829. 'description' => $row['description'],
  5830. 'parent_item_id' => $row['parent_item_id'],
  5831. 'previous_item_id' => $row['previous_item_id'],
  5832. 'next_item_id' => $row['next_item_id'],
  5833. 'display_order' => $row['display_order'],
  5834. 'max_score' => $row['max_score'],
  5835. 'min_score' => $row['min_score'],
  5836. 'mastery_score' => $row['mastery_score'],
  5837. 'prerequisite' => $row['prerequisite'],
  5838. 'max_time_allowed' => $row['max_time_allowed']
  5839. );
  5840. }
  5841. $this->tree_array($arrLP);
  5842. $arrLP = $this->arrMenu;
  5843. unset ($this->arrMenu);
  5844. if ($action == 'add') {
  5845. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5846. } elseif ($action == 'move') {
  5847. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5848. } else {
  5849. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5850. }
  5851. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5852. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5853. }
  5854. $legend .= '</legend>';
  5855. $return = '';
  5856. $return .= '<div class="sectioncomment">';
  5857. $return .= '<form method="POST">';
  5858. $return .= $legend;
  5859. $return .= '<table class="lp_form">';
  5860. if ($action != 'move') {
  5861. $return .= '<tr>';
  5862. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5863. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5864. $return .= '</tr>';
  5865. }
  5866. $return .= '<tr>';
  5867. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5868. $return .= '<td class="input">';
  5869. // Select for Parent item, root or chapter
  5870. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5871. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5872. $arrHide = array (
  5873. $id
  5874. );
  5875. for ($i = 0; $i < count($arrLP); $i++) {
  5876. if ($action != 'add') {
  5877. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5878. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5879. } else {
  5880. $arrHide[] = $arrLP[$i]['id'];
  5881. }
  5882. } else {
  5883. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5884. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5885. }
  5886. }
  5887. if (is_array($arrLP)) {
  5888. reset($arrLP);
  5889. }
  5890. $return .= '</select>';
  5891. $return .= '</td>';
  5892. $return .= '</tr>';
  5893. $return .= '<tr>';
  5894. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5895. $return .= '<td class="input">';
  5896. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5897. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5898. for ($i = 0; $i < count($arrLP); $i++) {
  5899. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5900. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5901. $selected = 'selected="selected" ';
  5902. elseif ($action == 'add') $selected = 'selected="selected" ';
  5903. else
  5904. $selected = '';
  5905. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5906. }
  5907. }
  5908. $return .= '</select>';
  5909. $return .= '</td>';
  5910. $return .= '</tr>';
  5911. if ($action != 'move') {
  5912. $id_prerequisite = 0;
  5913. if (is_array($arrLP)) {
  5914. foreach ($arrLP as $key => $value) {
  5915. if ($value['id'] == $id) {
  5916. $id_prerequisite = $value['prerequisite'];
  5917. break;
  5918. }
  5919. }
  5920. }
  5921. $arrHide = array ();
  5922. for ($i = 0; $i < count($arrLP); $i++) {
  5923. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5924. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5925. $s_selected_position = $arrLP[$i]['id'];
  5926. elseif ($action == 'add') $s_selected_position = 0;
  5927. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5928. }
  5929. }
  5930. /*// Commented the prerequisites, only visible in edit (exercise).
  5931. $return .= '<tr>';
  5932. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5933. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5934. foreach($arrHide as $key => $value){
  5935. if($key==$s_selected_position && $action == 'add'){
  5936. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5937. }
  5938. elseif($key==$id_prerequisite && $action == 'edit'){
  5939. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5940. }
  5941. else{
  5942. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5943. }
  5944. }
  5945. $return .= "</select></td>";
  5946. */
  5947. $return .= '</tr>';
  5948. /*$return .= '<tr>';
  5949. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5950. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5951. // Remove temporarily the test description.
  5952. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5953. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5954. $return .= '</tr>'; */
  5955. }
  5956. $return .= '<tr>';
  5957. if ($action == 'add') {
  5958. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5959. } else {
  5960. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5961. }
  5962. $return .= '</tr>';
  5963. $return .= '</table>';
  5964. if ($action == 'move') {
  5965. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5966. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5967. }
  5968. if (is_numeric($extra_info)) {
  5969. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5970. } elseif (is_array($extra_info)) {
  5971. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5972. }
  5973. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5974. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5975. $return .= '</form>';
  5976. $return .= '</div>';
  5977. return $return;
  5978. }
  5979. /**
  5980. * Addition of Hotpotatoes tests
  5981. * @param string Action
  5982. * @param integer Internal ID of the item
  5983. * @param mixed Extra information - can be an array with title and description indexes
  5984. * @return string HTML structure to display the hotpotatoes addition formular
  5985. */
  5986. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  5987. {
  5988. $course_id = api_get_course_int_id();
  5989. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5990. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5991. if ($id != 0 && is_array($extra_info)) {
  5992. $item_title = stripslashes($extra_info['title']);
  5993. $item_description = stripslashes($extra_info['description']);
  5994. } elseif (is_numeric($extra_info)) {
  5995. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5996. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  5997. WHERE
  5998. c_id = ".$course_id." AND
  5999. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6000. id = " . (int) $extra_info . "
  6001. ORDER BY id ASC";
  6002. $res_hot = Database::query($sql);
  6003. $row = Database::fetch_array($res_hot);
  6004. $item_title = $row['title'];
  6005. $item_description = $row['description'];
  6006. if (!empty ($row['comment'])) {
  6007. $item_title = $row['comment'];
  6008. }
  6009. } else {
  6010. $item_title = '';
  6011. $item_description = '';
  6012. }
  6013. if ($id != 0 && is_array($extra_info)) {
  6014. $parent = $extra_info['parent_item_id'];
  6015. } else {
  6016. $parent = 0;
  6017. }
  6018. $sql = "SELECT * FROM $tbl_lp_item
  6019. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6020. $result = Database::query($sql);
  6021. $arrLP = array ();
  6022. while ($row = Database :: fetch_array($result)) {
  6023. $arrLP[] = array (
  6024. 'id' => $row['id'],
  6025. 'item_type' => $row['item_type'],
  6026. 'title' => $row['title'],
  6027. 'path' => $row['path'],
  6028. 'description' => $row['description'],
  6029. 'parent_item_id' => $row['parent_item_id'],
  6030. 'previous_item_id' => $row['previous_item_id'],
  6031. 'next_item_id' => $row['next_item_id'],
  6032. 'display_order' => $row['display_order'],
  6033. 'max_score' => $row['max_score'],
  6034. 'min_score' => $row['min_score'],
  6035. 'mastery_score' => $row['mastery_score'],
  6036. 'prerequisite' => $row['prerequisite'],
  6037. 'max_time_allowed' => $row['max_time_allowed']
  6038. );
  6039. }
  6040. $legend = '<legend>';
  6041. if ($action == 'add')
  6042. $legend .= get_lang('CreateTheExercise');
  6043. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6044. else
  6045. $legend .= get_lang('EditCurrentExecice');
  6046. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6047. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6048. }
  6049. $legend .= '</legend>';
  6050. $return = '<form method="POST">';
  6051. $return .= $legend;
  6052. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6053. $return .= '<tr>';
  6054. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6055. $return .= '<td class="input">';
  6056. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6057. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6058. $arrHide = array (
  6059. $id
  6060. );
  6061. if (count($arrLP) > 0) {
  6062. for ($i = 0; $i < count($arrLP); $i++) {
  6063. if ($action != 'add') {
  6064. 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)) {
  6065. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6066. } else {
  6067. $arrHide[] = $arrLP[$i]['id'];
  6068. }
  6069. } else {
  6070. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6071. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6072. }
  6073. }
  6074. reset($arrLP);
  6075. }
  6076. $return .= '</select>';
  6077. $return .= '</td>';
  6078. $return .= '</tr>';
  6079. $return .= '<tr>';
  6080. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6081. $return .= '<td class="input">';
  6082. $return .= '<select id="previous" name="previous" size="1">';
  6083. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6084. for ($i = 0; $i < count($arrLP); $i++) {
  6085. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6086. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6087. $selected = 'selected="selected" ';
  6088. elseif ($action == 'add') $selected = 'selected="selected" ';
  6089. else
  6090. $selected = '';
  6091. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6092. }
  6093. }
  6094. $return .= '</select>';
  6095. $return .= '</td>';
  6096. $return .= '</tr>';
  6097. if ($action != 'move') {
  6098. $return .= '<tr>';
  6099. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6100. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6101. $return .= '</tr>';
  6102. $id_prerequisite = 0;
  6103. if (is_array($arrLP) && count($arrLP) > 0) {
  6104. foreach ($arrLP as $key => $value) {
  6105. if ($value['id'] == $id) {
  6106. $id_prerequisite = $value['prerequisite'];
  6107. break;
  6108. }
  6109. }
  6110. $arrHide = array ();
  6111. for ($i = 0; $i < count($arrLP); $i++) {
  6112. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6113. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6114. $s_selected_position = $arrLP[$i]['id'];
  6115. elseif ($action == 'add') $s_selected_position = 0;
  6116. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6117. }
  6118. }
  6119. }
  6120. }
  6121. $return .= '<tr>';
  6122. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6123. $return .= '</tr>';
  6124. $return .= '</table>';
  6125. if ($action == 'move') {
  6126. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6127. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6128. }
  6129. if (is_numeric($extra_info)) {
  6130. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6131. } elseif (is_array($extra_info)) {
  6132. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6133. }
  6134. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6135. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6136. $return .= '</form>';
  6137. return $return;
  6138. }
  6139. /**
  6140. * Return the form to display the forum edit/add option
  6141. * @param string Action (add/edit)
  6142. * @param integer ID of the lp_item if already exists
  6143. * @param mixed Forum ID or title
  6144. * @return string HTML form
  6145. */
  6146. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6147. {
  6148. $course_id = api_get_course_int_id();
  6149. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6150. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6151. if ($id != 0 && is_array($extra_info)) {
  6152. $item_title = stripslashes($extra_info['title']);
  6153. } elseif (is_numeric($extra_info)) {
  6154. $sql = "SELECT forum_title as title, forum_comment as comment
  6155. FROM " . $tbl_forum . "
  6156. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6157. $result = Database::query($sql);
  6158. $row = Database :: fetch_array($result);
  6159. $item_title = $row['title'];
  6160. $item_description = $row['comment'];
  6161. } else {
  6162. $item_title = '';
  6163. $item_description = '';
  6164. }
  6165. $legend = '<legend>';
  6166. if ($id != 0 && is_array($extra_info)) {
  6167. $parent = $extra_info['parent_item_id'];
  6168. } else {
  6169. $parent = 0;
  6170. }
  6171. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6172. WHERE
  6173. c_id = ".$course_id." AND
  6174. lp_id = " . $this->lp_id;
  6175. $result = Database::query($sql);
  6176. $arrLP = array();
  6177. while ($row = Database :: fetch_array($result)) {
  6178. $arrLP[] = array (
  6179. 'id' => $row['id'],
  6180. 'item_type' => $row['item_type'],
  6181. 'title' => $row['title'],
  6182. 'path' => $row['path'],
  6183. 'description' => $row['description'],
  6184. 'parent_item_id' => $row['parent_item_id'],
  6185. 'previous_item_id' => $row['previous_item_id'],
  6186. 'next_item_id' => $row['next_item_id'],
  6187. 'display_order' => $row['display_order'],
  6188. 'max_score' => $row['max_score'],
  6189. 'min_score' => $row['min_score'],
  6190. 'mastery_score' => $row['mastery_score'],
  6191. 'prerequisite' => $row['prerequisite']
  6192. );
  6193. }
  6194. $this->tree_array($arrLP);
  6195. $arrLP = $this->arrMenu;
  6196. unset($this->arrMenu);
  6197. if ($action == 'add')
  6198. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6199. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6200. else
  6201. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6202. $legend .= '</legend>';
  6203. $return = '<div class="sectioncomment">';
  6204. $return .= '<form method="POST">';
  6205. $return .= $legend;
  6206. $return .= '<table class="lp_form">';
  6207. if ($action != 'move') {
  6208. $return .= '<tr>';
  6209. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6210. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6211. $return .= '</tr>';
  6212. }
  6213. $return .= '<tr>';
  6214. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6215. $return .= '<td class="input">';
  6216. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6217. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6218. $arrHide = array(
  6219. $id
  6220. );
  6221. //$parent_item_id = $_SESSION['parent_item_id'];
  6222. for ($i = 0; $i < count($arrLP); $i++) {
  6223. if ($action != 'add') {
  6224. 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)) {
  6225. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6226. } else {
  6227. $arrHide[] = $arrLP[$i]['id'];
  6228. }
  6229. } else {
  6230. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6231. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6232. }
  6233. }
  6234. if (is_array($arrLP)) {
  6235. reset($arrLP);
  6236. }
  6237. $return .= '</select>';
  6238. $return .= '</td>';
  6239. $return .= '</tr>';
  6240. $return .= '<tr>';
  6241. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6242. $return .= '<td class="input">';
  6243. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6244. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6245. for ($i = 0; $i < count($arrLP); $i++) {
  6246. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6247. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6248. $selected = 'selected="selected" ';
  6249. elseif ($action == 'add') $selected = 'selected="selected" ';
  6250. else
  6251. $selected = '';
  6252. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6253. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6254. }
  6255. }
  6256. $return .= '</select>';
  6257. $return .= '</td>';
  6258. $return .= '</tr>';
  6259. if ($action != 'move') {
  6260. $return .= '<tr>';
  6261. $return .= '</tr>';
  6262. $id_prerequisite = 0;
  6263. if (is_array($arrLP)) {
  6264. foreach ($arrLP as $key => $value) {
  6265. if ($value['id'] == $id) {
  6266. $id_prerequisite = $value['prerequisite'];
  6267. break;
  6268. }
  6269. }
  6270. }
  6271. $arrHide = array();
  6272. for ($i = 0; $i < count($arrLP); $i++) {
  6273. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6274. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6275. $s_selected_position = $arrLP[$i]['id'];
  6276. elseif ($action == 'add') $s_selected_position = 0;
  6277. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6278. }
  6279. }
  6280. $return .= '</tr>';
  6281. }
  6282. $return .= '<tr>';
  6283. if ($action == 'add') {
  6284. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6285. } else {
  6286. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6287. }
  6288. $return .= '</tr>';
  6289. $return .= '</table>';
  6290. if ($action == 'move') {
  6291. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6292. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6293. }
  6294. if (is_numeric($extra_info)) {
  6295. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6296. } elseif (is_array($extra_info)) {
  6297. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6298. }
  6299. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6300. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6301. $return .= '</form>';
  6302. $return .= '</div>';
  6303. return $return;
  6304. }
  6305. /**
  6306. * Return HTML form to add/edit forum threads
  6307. * @param string Action (add/edit)
  6308. * @param integer Item ID if already exists in learning path
  6309. * @param mixed Extra information (thread ID if integer)
  6310. * @return string HTML form
  6311. */
  6312. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6313. {
  6314. $course_id = api_get_course_int_id();
  6315. if (empty($course_id)) {
  6316. return null;
  6317. }
  6318. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6319. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6320. if ($id != 0 && is_array($extra_info)) {
  6321. $item_title = stripslashes($extra_info['title']);
  6322. } elseif (is_numeric($extra_info)) {
  6323. $sql = "SELECT thread_title as title FROM $tbl_forum
  6324. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6325. $result = Database::query($sql);
  6326. $row = Database :: fetch_array($result);
  6327. $item_title = $row['title'];
  6328. $item_description = '';
  6329. } else {
  6330. $item_title = '';
  6331. $item_description = '';
  6332. }
  6333. $return = null;
  6334. if ($id != 0 && is_array($extra_info)) {
  6335. $parent = $extra_info['parent_item_id'];
  6336. } else {
  6337. $parent = 0;
  6338. }
  6339. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6340. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6341. $result = Database::query($sql);
  6342. $arrLP = array ();
  6343. while ($row = Database :: fetch_array($result)) {
  6344. $arrLP[] = array (
  6345. 'id' => $row['id'],
  6346. 'item_type' => $row['item_type'],
  6347. 'title' => $row['title'],
  6348. 'path' => $row['path'],
  6349. 'description' => $row['description'],
  6350. 'parent_item_id' => $row['parent_item_id'],
  6351. 'previous_item_id' => $row['previous_item_id'],
  6352. 'next_item_id' => $row['next_item_id'],
  6353. 'display_order' => $row['display_order'],
  6354. 'max_score' => $row['max_score'],
  6355. 'min_score' => $row['min_score'],
  6356. 'mastery_score' => $row['mastery_score'],
  6357. 'prerequisite' => $row['prerequisite']
  6358. );
  6359. }
  6360. $this->tree_array($arrLP);
  6361. $arrLP = $this->arrMenu;
  6362. unset ($this->arrMenu);
  6363. $return .= '<form method="POST">';
  6364. if ($action == 'add')
  6365. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6366. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6367. else
  6368. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6369. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6370. $return .= '<tr>';
  6371. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6372. $return .= '<td class="input">';
  6373. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6374. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6375. $arrHide = array (
  6376. $id
  6377. );
  6378. for ($i = 0; $i < count($arrLP); $i++) {
  6379. if ($action != 'add') {
  6380. 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)) {
  6381. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6382. } else {
  6383. $arrHide[] = $arrLP[$i]['id'];
  6384. }
  6385. } else {
  6386. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6387. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6388. }
  6389. }
  6390. if ($arrLP != null) {
  6391. reset($arrLP);
  6392. }
  6393. $return .= '</select>';
  6394. $return .= '</td>';
  6395. $return .= '</tr>';
  6396. $return .= '<tr>';
  6397. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6398. $return .= '<td class="input">';
  6399. $return .= '<select id="previous" name="previous" size="1">';
  6400. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6401. for ($i = 0; $i < count($arrLP); $i++) {
  6402. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6403. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6404. $selected = 'selected="selected" ';
  6405. elseif ($action == 'add') $selected = 'selected="selected" ';
  6406. else
  6407. $selected = '';
  6408. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6409. }
  6410. }
  6411. $return .= '</select>';
  6412. $return .= '</td>';
  6413. $return .= '</tr>';
  6414. if ($action != 'move') {
  6415. $return .= '<tr>';
  6416. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6417. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6418. $return .= '</tr>';
  6419. $return .= '<tr>';
  6420. $return .= '</tr>';
  6421. $id_prerequisite = 0;
  6422. if ($arrLP != null) {
  6423. foreach ($arrLP as $key => $value) {
  6424. if ($value['id'] == $id) {
  6425. $id_prerequisite = $value['prerequisite'];
  6426. break;
  6427. }
  6428. }
  6429. }
  6430. $arrHide = array();
  6431. for ($i = 0; $i < count($arrLP); $i++) {
  6432. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6433. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6434. $s_selected_position = $arrLP[$i]['id'];
  6435. elseif ($action == 'add') $s_selected_position = 0;
  6436. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6437. }
  6438. }
  6439. $return .= '<tr>';
  6440. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6441. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6442. foreach ($arrHide as $key => $value) {
  6443. if ($key == $s_selected_position && $action == 'add') {
  6444. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6445. }
  6446. elseif ($key == $id_prerequisite && $action == 'edit') {
  6447. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6448. } else {
  6449. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6450. }
  6451. }
  6452. $return .= "</select></td>";
  6453. $return .= '</tr>';
  6454. }
  6455. $return .= '<tr>';
  6456. $return .= '<td></td><td>
  6457. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6458. $return .= '</tr>';
  6459. $return .= '</table>';
  6460. if ($action == 'move') {
  6461. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6462. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6463. }
  6464. if (is_numeric($extra_info)) {
  6465. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6466. }
  6467. elseif (is_array($extra_info)) {
  6468. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6469. }
  6470. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6471. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6472. $return .= '</form>';
  6473. $return .= '</div>';
  6474. return $return;
  6475. }
  6476. /**
  6477. * Return the HTML form to display an item (generally a section/module item)
  6478. * @param string Item type (module/dokeos_module)
  6479. * @param string Title (optional, only when creating)
  6480. * @param string Action ('add'/'edit')
  6481. * @param integer lp_item ID
  6482. * @param mixed Extra info
  6483. * @return string HTML form
  6484. */
  6485. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6486. {
  6487. $course_id = api_get_course_int_id();
  6488. $_course = api_get_course_info();
  6489. global $charset;
  6490. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6491. if ($id != 0 && is_array($extra_info)) {
  6492. $item_title = $extra_info['title'];
  6493. $item_description = $extra_info['description'];
  6494. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6495. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6496. } else {
  6497. $item_title = '';
  6498. $item_description = '';
  6499. $item_path_fck = '';
  6500. }
  6501. if ($id != 0 && is_array($extra_info)) {
  6502. $parent = $extra_info['parent_item_id'];
  6503. } else {
  6504. $parent = 0;
  6505. }
  6506. $id = intval($id);
  6507. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6508. WHERE
  6509. c_id = ".$course_id." AND
  6510. lp_id = " . $this->lp_id . " AND
  6511. id != $id";
  6512. if ($item_type == 'module')
  6513. $sql .= " AND parent_item_id = 0";
  6514. $result = Database::query($sql);
  6515. $arrLP = array ();
  6516. while ($row = Database :: fetch_array($result)) {
  6517. $arrLP[] = array(
  6518. 'id' => $row['id'],
  6519. 'item_type' => $row['item_type'],
  6520. 'title' => $row['title'],
  6521. 'path' => $row['path'],
  6522. 'description' => $row['description'],
  6523. 'parent_item_id' => $row['parent_item_id'],
  6524. 'previous_item_id' => $row['previous_item_id'],
  6525. 'next_item_id' => $row['next_item_id'],
  6526. 'max_score' => $row['max_score'],
  6527. 'min_score' => $row['min_score'],
  6528. 'mastery_score' => $row['mastery_score'],
  6529. 'prerequisite' => $row['prerequisite'],
  6530. 'display_order' => $row['display_order']
  6531. );
  6532. }
  6533. $this->tree_array($arrLP);
  6534. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6535. unset ($this->arrMenu);
  6536. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6537. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6538. $form = new FormValidator('form', 'POST', $url);
  6539. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6540. $defaults['description'] = $item_description;
  6541. $form->addElement('header', $title);
  6542. //$arrHide = array($id);
  6543. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6544. $arrHide[0]['padding'] = 3;
  6545. $charset = api_get_system_encoding();
  6546. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6547. for ($i = 0; $i < count($arrLP); $i++) {
  6548. if ($action != 'add') {
  6549. 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)) {
  6550. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6551. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6552. if ($parent == $arrLP[$i]['id']) {
  6553. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6554. }
  6555. }
  6556. } else {
  6557. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6558. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6559. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6560. if ($parent == $arrLP[$i]['id']) {
  6561. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6562. }
  6563. }
  6564. }
  6565. }
  6566. if ($action != 'move') {
  6567. $form->addElement('text', 'title', get_lang('Title'));
  6568. $form->applyFilter('title', 'html_filter');
  6569. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6570. } else {
  6571. $form->addElement('hidden', 'title');
  6572. }
  6573. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6574. foreach ($arrHide as $key => $value) {
  6575. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6576. }
  6577. if (!empty($s_selected_parent)) {
  6578. $parent_select->setSelected($s_selected_parent);
  6579. }
  6580. }
  6581. if (is_array($arrLP)) {
  6582. reset($arrLP);
  6583. }
  6584. $arrHide = array();
  6585. // POSITION
  6586. for ($i = 0; $i < count($arrLP); $i++) {
  6587. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6588. //this is the same!
  6589. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6590. $s_selected_position = $arrLP[$i]['id'];
  6591. } elseif ($action == 'add') {
  6592. $s_selected_position = $arrLP[$i]['id'];
  6593. }
  6594. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6595. }
  6596. }
  6597. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6598. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6599. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6600. foreach ($arrHide as $key => $value) {
  6601. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6602. }
  6603. if (!empty ($s_selected_position)) {
  6604. $position->setSelected($s_selected_position);
  6605. }
  6606. if (is_array($arrLP)) {
  6607. reset($arrLP);
  6608. }
  6609. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6610. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6611. $form->addElement('hidden', 'parent', '0');
  6612. }
  6613. //fix in order to use the tab
  6614. if ($item_type == 'chapter') {
  6615. $form->addElement('hidden', 'type', 'chapter');
  6616. }
  6617. $extension = null;
  6618. if (!empty($item_path)) {
  6619. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6620. }
  6621. //assets can't be modified
  6622. //$item_type == 'asset' ||
  6623. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6624. if ($item_type == 'sco') {
  6625. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6626. }
  6627. $renderer = $form->defaultRenderer();
  6628. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6629. $relative_prefix = '';
  6630. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6631. 'Width' => '100%',
  6632. 'Height' => '500',
  6633. 'FullPage' => true,
  6634. 'CreateDocumentDir' => $relative_prefix,
  6635. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6636. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6637. );
  6638. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6639. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6640. //$defaults['content_lp'] = file_get_contents($item_path);
  6641. $defaults['content_lp'] = file_get_contents($content_path);
  6642. }
  6643. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6644. $form->addElement('hidden', 'post_time', time());
  6645. $form->setDefaults($defaults);
  6646. return $form->return_form();
  6647. }
  6648. /**
  6649. * Returns the form to update or create a document
  6650. * @param string Action (add/edit)
  6651. * @param integer ID of the lp_item (if already exists)
  6652. * @param mixed Integer if document ID, string if info ('new')
  6653. * @return string HTML form
  6654. */
  6655. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6656. {
  6657. $course_id = api_get_course_int_id();
  6658. $_course = api_get_course_info();
  6659. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6660. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6661. $no_display_edit_textarea = false;
  6662. //If action==edit document
  6663. //We don't display the document form if it's not an editable document (html or txt file)
  6664. if ($action == "edit") {
  6665. if (is_array($extra_info)) {
  6666. $path_parts = pathinfo($extra_info['dir']);
  6667. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6668. $no_display_edit_textarea = true;
  6669. }
  6670. }
  6671. }
  6672. $no_display_add = false;
  6673. // If action==add an existing document
  6674. // We don't display the document form if it's not an editable document (html or txt file).
  6675. if ($action == "add") {
  6676. if (is_numeric($extra_info)) {
  6677. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6678. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6679. $result = Database::query($sql_doc);
  6680. $path_file = Database :: result($result, 0, 0);
  6681. $path_parts = pathinfo($path_file);
  6682. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6683. $no_display_add = true;
  6684. }
  6685. }
  6686. }
  6687. if ($id != 0 && is_array($extra_info)) {
  6688. $item_title = stripslashes($extra_info['title']);
  6689. $item_description = stripslashes($extra_info['description']);
  6690. $item_terms = stripslashes($extra_info['terms']);
  6691. if (empty ($item_title)) {
  6692. $path_parts = pathinfo($extra_info['path']);
  6693. $item_title = stripslashes($path_parts['filename']);
  6694. }
  6695. } elseif (is_numeric($extra_info)) {
  6696. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6697. WHERE
  6698. c_id = ".$course_id." AND
  6699. id = " . intval($extra_info);
  6700. $result = Database::query($sql_doc);
  6701. $row = Database::fetch_array($result);
  6702. $item_title = $row['title'];
  6703. $item_title = str_replace('_', ' ', $item_title);
  6704. if (empty ($item_title)) {
  6705. $path_parts = pathinfo($row['path']);
  6706. $item_title = stripslashes($path_parts['filename']);
  6707. }
  6708. } else {
  6709. $item_title = '';
  6710. $item_description = '';
  6711. }
  6712. $return = '<legend>';
  6713. if ($id != 0 && is_array($extra_info)) {
  6714. $parent = $extra_info['parent_item_id'];
  6715. } else {
  6716. $parent = 0;
  6717. }
  6718. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6719. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6720. $result = Database::query($sql);
  6721. $arrLP = array ();
  6722. while ($row = Database :: fetch_array($result)) {
  6723. $arrLP[] = array(
  6724. 'id' => $row['id'],
  6725. 'item_type' => $row['item_type'],
  6726. 'title' => $row['title'],
  6727. 'path' => $row['path'],
  6728. 'description' => $row['description'],
  6729. 'parent_item_id' => $row['parent_item_id'],
  6730. 'previous_item_id' => $row['previous_item_id'],
  6731. 'next_item_id' => $row['next_item_id'],
  6732. 'display_order' => $row['display_order'],
  6733. 'max_score' => $row['max_score'],
  6734. 'min_score' => $row['min_score'],
  6735. 'mastery_score' => $row['mastery_score'],
  6736. 'prerequisite' => $row['prerequisite']
  6737. );
  6738. }
  6739. $this->tree_array($arrLP);
  6740. $arrLP = $this->arrMenu;
  6741. unset ($this->arrMenu);
  6742. if ($action == 'add') {
  6743. $return .= get_lang('CreateTheDocument');
  6744. } elseif ($action == 'move') {
  6745. $return .= get_lang('MoveTheCurrentDocument');
  6746. } else {
  6747. $return .= get_lang('EditTheCurrentDocument');
  6748. }
  6749. $return .= '</legend>';
  6750. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6751. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6752. }
  6753. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6754. $defaults['title'] = Security :: remove_XSS($item_title);
  6755. if (empty($item_title)) {
  6756. $defaults['title'] = Security::remove_XSS($item_title);
  6757. }
  6758. $defaults['description'] = $item_description;
  6759. $form->addElement('html', $return);
  6760. if ($action != 'move') {
  6761. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6762. $form->applyFilter('title', 'html_filter');
  6763. }
  6764. $arrHide[0]['value'] = $this->name;
  6765. $arrHide[0]['padding'] = 3;
  6766. for ($i = 0; $i < count($arrLP); $i++) {
  6767. if ($action != 'add') {
  6768. 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)) {
  6769. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6770. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6771. if ($parent == $arrLP[$i]['id']) {
  6772. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6773. }
  6774. }
  6775. } else {
  6776. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6777. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6778. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6779. if ($parent == $arrLP[$i]['id']) {
  6780. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6781. }
  6782. }
  6783. }
  6784. }
  6785. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6786. $my_count=0;
  6787. foreach ($arrHide as $key => $value) {
  6788. if ($my_count!=0) {
  6789. // The LP name is also the first section and is not in the same charset like the other sections.
  6790. $value['value'] = Security :: remove_XSS($value['value']);
  6791. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6792. } else {
  6793. $value['value'] = Security :: remove_XSS($value['value']);
  6794. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6795. }
  6796. $my_count++;
  6797. }
  6798. if (!empty($id)) {
  6799. $parent_select->setSelected($parent);
  6800. } else {
  6801. $parent_item_id = $_SESSION['parent_item_id'];
  6802. $parent_select->setSelected($parent_item_id);
  6803. }
  6804. if (is_array($arrLP)) {
  6805. reset($arrLP);
  6806. }
  6807. $arrHide = array();
  6808. $s_selected_position = null;
  6809. //POSITION
  6810. for ($i = 0; $i < count($arrLP); $i++) {
  6811. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6812. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6813. $s_selected_position = $arrLP[$i]['id'];
  6814. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6815. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6816. }
  6817. }
  6818. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6819. $position->addOption(get_lang('FirstPosition'), 0);
  6820. foreach ($arrHide as $key => $value) {
  6821. $padding = isset($value['padding']) ? $value['padding']: 0;
  6822. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6823. }
  6824. $position->setSelected($s_selected_position);
  6825. if (is_array($arrLP)) {
  6826. reset($arrLP);
  6827. }
  6828. if ($action != 'move') {
  6829. $id_prerequisite = 0;
  6830. if (is_array($arrLP)) {
  6831. foreach ($arrLP as $key => $value) {
  6832. if ($value['id'] == $id) {
  6833. $id_prerequisite = $value['prerequisite'];
  6834. break;
  6835. }
  6836. }
  6837. }
  6838. $arrHide = array();
  6839. for ($i = 0; $i < count($arrLP); $i++) {
  6840. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6841. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6842. $s_selected_position = $arrLP[$i]['id'];
  6843. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6844. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6845. }
  6846. }
  6847. if (!$no_display_add) {
  6848. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6849. if (isset ($_POST['content']))
  6850. $content = stripslashes($_POST['content']);
  6851. elseif (is_array($extra_info)) {
  6852. //If it's an html document or a text file
  6853. if (!$no_display_edit_textarea) {
  6854. $content = $this->display_document($extra_info['path'], false, false);
  6855. }
  6856. } elseif (is_numeric($extra_info))
  6857. $content = $this->display_document($extra_info, false, false);
  6858. else
  6859. $content = '';
  6860. if (!$no_display_edit_textarea) {
  6861. // We need to calculate here some specific settings for the online editor.
  6862. // The calculated settings work for documents in the Documents tool
  6863. // (on the root or in subfolders).
  6864. // For documents in native scorm packages it is unclear whether the
  6865. // online editor should be activated or not.
  6866. // A new document, it is in the root of the repository.
  6867. $relative_path = '';
  6868. $relative_prefix = '';
  6869. if (is_array($extra_info) && $extra_info != 'new') {
  6870. // The document already exists. Whe have to determine its relative path towards the repository root.
  6871. $relative_path = explode('/', $extra_info['dir']);
  6872. $cnt = count($relative_path) - 2;
  6873. if ($cnt < 0) {
  6874. $cnt = 0;
  6875. }
  6876. $relative_prefix = str_repeat('../', $cnt);
  6877. $relative_path = array_slice($relative_path, 1, $cnt);
  6878. $relative_path = implode('/', $relative_path);
  6879. if (strlen($relative_path) > 0) {
  6880. $relative_path = $relative_path . '/';
  6881. }
  6882. } else {
  6883. $result = $this->generate_lp_folder($_course);
  6884. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6885. $relative_prefix = '../../';
  6886. }
  6887. $editor_config = array(
  6888. 'ToolbarSet'=> 'LearningPathDocuments',
  6889. 'Width' => '100%',
  6890. 'Height' => '500',
  6891. 'FullPage' => true,
  6892. 'CreateDocumentDir' => $relative_prefix,
  6893. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6894. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6895. );
  6896. if ($_GET['action'] == 'add_item') {
  6897. $class = 'add';
  6898. $text = get_lang('LPCreateDocument');
  6899. } else {
  6900. if ($_GET['action'] == 'edit_item') {
  6901. $class = 'save';
  6902. $text = get_lang('SaveDocument');
  6903. }
  6904. }
  6905. $form->addButtonSave($text, 'submit_button');
  6906. $renderer = $form->defaultRenderer();
  6907. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  6908. $form->addElement('html', '<div class="editor-lp">');
  6909. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  6910. $form->addElement('html', '</div>');
  6911. $defaults['content_lp'] = $content;
  6912. }
  6913. } elseif (is_numeric($extra_info)) {
  6914. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6915. $return = $this->display_document($extra_info, true, true, true);
  6916. $form->addElement('html', $return);
  6917. }
  6918. }
  6919. }
  6920. if ($action == 'move') {
  6921. $form->addElement('hidden', 'title', $item_title);
  6922. $form->addElement('hidden', 'description', $item_description);
  6923. }
  6924. if (is_numeric($extra_info)) {
  6925. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6926. $form->addElement('hidden', 'path', $extra_info);
  6927. } elseif (is_array($extra_info)) {
  6928. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6929. $form->addElement('hidden', 'path', $extra_info['path']);
  6930. }
  6931. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6932. $form->addElement('hidden', 'post_time', time());
  6933. $form->setDefaults($defaults);
  6934. return $form->return_form();
  6935. }
  6936. /**
  6937. * Return HTML form to add/edit a link item
  6938. * @param string $action (add/edit)
  6939. * @param integer $id Item ID if exists
  6940. * @param mixed $extra_info
  6941. * @return string HTML form
  6942. */
  6943. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6944. {
  6945. $course_id = api_get_course_int_id();
  6946. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6947. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6948. if ($id != 0 && is_array($extra_info)) {
  6949. $item_title = stripslashes($extra_info['title']);
  6950. $item_description = stripslashes($extra_info['description']);
  6951. $item_url = stripslashes($extra_info['url']);
  6952. } elseif (is_numeric($extra_info)) {
  6953. $extra_info = intval($extra_info);
  6954. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  6955. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6956. $result = Database::query($sql);
  6957. $row = Database :: fetch_array($result);
  6958. $item_title = $row['title'];
  6959. $item_description = $row['description'];
  6960. $item_url = $row['url'];
  6961. } else {
  6962. $item_title = '';
  6963. $item_description = '';
  6964. $item_url = '';
  6965. }
  6966. $legend = '<legend>';
  6967. if ($id != 0 && is_array($extra_info)) {
  6968. $parent = $extra_info['parent_item_id'];
  6969. } else {
  6970. $parent = 0;
  6971. }
  6972. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6973. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6974. $result = Database::query($sql);
  6975. $arrLP = array();
  6976. while ($row = Database :: fetch_array($result)) {
  6977. $arrLP[] = array(
  6978. 'id' => $row['id'],
  6979. 'item_type' => $row['item_type'],
  6980. 'title' => $row['title'],
  6981. 'path' => $row['path'],
  6982. 'description' => $row['description'],
  6983. 'parent_item_id' => $row['parent_item_id'],
  6984. 'previous_item_id' => $row['previous_item_id'],
  6985. 'next_item_id' => $row['next_item_id'],
  6986. 'display_order' => $row['display_order'],
  6987. 'max_score' => $row['max_score'],
  6988. 'min_score' => $row['min_score'],
  6989. 'mastery_score' => $row['mastery_score'],
  6990. 'prerequisite' => $row['prerequisite']
  6991. );
  6992. }
  6993. $this->tree_array($arrLP);
  6994. $arrLP = $this->arrMenu;
  6995. unset ($this->arrMenu);
  6996. if ($action == 'add')
  6997. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6998. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6999. else
  7000. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7001. $legend .= '</legend>';
  7002. $return = '<div class="sectioncomment">';
  7003. $return .= '<form method="POST">';
  7004. $return .= $legend;
  7005. $return .= '<table>';
  7006. if ($action != 'move') {
  7007. $return .= '<tr>';
  7008. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7009. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7010. $return .= '</tr>';
  7011. }
  7012. $return .= '<tr>';
  7013. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7014. $return .= '<td class="input">';
  7015. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7016. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7017. $arrHide = array(
  7018. $id
  7019. );
  7020. $parent_item_id = $_SESSION['parent_item_id'];
  7021. for ($i = 0; $i < count($arrLP); $i++) {
  7022. if ($action != 'add') {
  7023. 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)) {
  7024. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7025. } else {
  7026. $arrHide[] = $arrLP[$i]['id'];
  7027. }
  7028. } else {
  7029. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7030. $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>';
  7031. }
  7032. }
  7033. if (is_array($arrLP)) {
  7034. reset($arrLP);
  7035. }
  7036. $return .= '</select>';
  7037. $return .= '</td>';
  7038. $return .= '</tr>';
  7039. $return .= '<tr>';
  7040. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7041. $return .= '<td class="input">';
  7042. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7043. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7044. for ($i = 0; $i < count($arrLP); $i++) {
  7045. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7046. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7047. $selected = 'selected="selected" ';
  7048. elseif ($action == 'add')
  7049. $selected = 'selected="selected" ';
  7050. else
  7051. $selected = '';
  7052. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7053. }
  7054. }
  7055. $return .= '</select>';
  7056. $return .= '</td>';
  7057. $return .= '</tr>';
  7058. if ($action != 'move') {
  7059. $return .= '<tr>';
  7060. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7061. $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>';
  7062. $return .= '</tr>';
  7063. $id_prerequisite = 0;
  7064. if (is_array($arrLP)) {
  7065. foreach ($arrLP as $key => $value) {
  7066. if ($value['id'] == $id) {
  7067. $id_prerequisite = $value['prerequisite'];
  7068. break;
  7069. }
  7070. }
  7071. }
  7072. $arrHide = array();
  7073. for ($i = 0; $i < count($arrLP); $i++) {
  7074. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7075. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7076. $s_selected_position = $arrLP[$i]['id'];
  7077. elseif ($action == 'add') $s_selected_position = 0;
  7078. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7079. }
  7080. }
  7081. $return .= '</tr>';
  7082. }
  7083. $return .= '<tr>';
  7084. if ($action == 'add') {
  7085. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7086. } else {
  7087. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7088. }
  7089. $return .= '</tr>';
  7090. $return .= '</table>';
  7091. if ($action == 'move') {
  7092. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7093. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7094. }
  7095. if (is_numeric($extra_info)) {
  7096. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7097. } elseif (is_array($extra_info)) {
  7098. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7099. }
  7100. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7101. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7102. $return .= '</form>';
  7103. $return .= '</div>';
  7104. return $return;
  7105. }
  7106. /**
  7107. * Return HTML form to add/edit a student publication (work)
  7108. * @param string Action (add/edit)
  7109. * @param integer Item ID if already exists
  7110. * @param mixed Extra info (work ID if integer)
  7111. * @return string HTML form
  7112. */
  7113. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7114. {
  7115. $course_id = api_get_course_int_id();
  7116. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7117. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7118. if ($id != 0 && is_array($extra_info)) {
  7119. $item_title = stripslashes($extra_info['title']);
  7120. $item_description = stripslashes($extra_info['description']);
  7121. } elseif (is_numeric($extra_info)) {
  7122. $extra_info = intval($extra_info);
  7123. $sql = "SELECT title, description
  7124. FROM " . $tbl_publication . "
  7125. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7126. $result = Database::query($sql);
  7127. $row = Database :: fetch_array($result);
  7128. $item_title = $row['title'];
  7129. } else {
  7130. $item_title = get_lang('Student_publication');
  7131. }
  7132. $legend = '<legend>';
  7133. if ($id != 0 && is_array($extra_info)) {
  7134. $parent = $extra_info['parent_item_id'];
  7135. } else {
  7136. $parent = 0;
  7137. }
  7138. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7139. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7140. $result = Database::query($sql);
  7141. $arrLP = array();
  7142. while ($row = Database :: fetch_array($result)) {
  7143. $arrLP[] = array (
  7144. 'id' => $row['id'],
  7145. 'item_type' => $row['item_type'],
  7146. 'title' => $row['title'],
  7147. 'path' => $row['path'],
  7148. 'description' => $row['description'],
  7149. 'parent_item_id' => $row['parent_item_id'],
  7150. 'previous_item_id' => $row['previous_item_id'],
  7151. 'next_item_id' => $row['next_item_id'],
  7152. 'display_order' => $row['display_order'],
  7153. 'max_score' => $row['max_score'],
  7154. 'min_score' => $row['min_score'],
  7155. 'mastery_score' => $row['mastery_score'],
  7156. 'prerequisite' => $row['prerequisite']
  7157. );
  7158. }
  7159. $this->tree_array($arrLP);
  7160. $arrLP = $this->arrMenu;
  7161. unset ($this->arrMenu);
  7162. if ($action == 'add') {
  7163. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7164. } elseif ($action == 'move') {
  7165. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7166. } else {
  7167. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7168. }
  7169. $legend .= '</legend>';
  7170. $return = '<div class="sectioncomment">';
  7171. $return .= '<form method="POST">';
  7172. $return .= $legend;
  7173. $return .= '<table class="lp_form">';
  7174. if ($action != 'move') {
  7175. $return .= '<tr>';
  7176. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7177. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7178. $return .= '</tr>';
  7179. }
  7180. $return .= '<tr>';
  7181. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7182. $return .= '<td class="input">';
  7183. $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">';
  7184. //$parent_item_id = $_SESSION['parent_item_id'];
  7185. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7186. $arrHide = array (
  7187. $id
  7188. );
  7189. for ($i = 0; $i < count($arrLP); $i++) {
  7190. if ($action != 'add') {
  7191. 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)) {
  7192. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7193. } else {
  7194. $arrHide[] = $arrLP[$i]['id'];
  7195. }
  7196. } else {
  7197. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7198. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7199. }
  7200. }
  7201. if (is_array($arrLP)) {
  7202. reset($arrLP);
  7203. }
  7204. $return .= '</select>';
  7205. $return .= '</td>';
  7206. $return .= '</tr>';
  7207. $return .= '<tr>';
  7208. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7209. $return .= '<td class="input">';
  7210. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7211. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7212. for ($i = 0; $i < count($arrLP); $i++) {
  7213. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7214. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7215. $selected = 'selected="selected" ';
  7216. elseif ($action == 'add') $selected = 'selected="selected" ';
  7217. else
  7218. $selected = '';
  7219. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7220. }
  7221. }
  7222. $return .= '</select>';
  7223. $return .= '</td>';
  7224. $return .= '</tr>';
  7225. if ($action != 'move') {
  7226. $id_prerequisite = 0;
  7227. if (is_array($arrLP)) {
  7228. foreach ($arrLP as $key => $value) {
  7229. if ($value['id'] == $id) {
  7230. $id_prerequisite = $value['prerequisite'];
  7231. break;
  7232. }
  7233. }
  7234. }
  7235. $arrHide = array ();
  7236. for ($i = 0; $i < count($arrLP); $i++) {
  7237. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7238. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7239. $s_selected_position = $arrLP[$i]['id'];
  7240. elseif ($action == 'add') $s_selected_position = 0;
  7241. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7242. }
  7243. }
  7244. // Commented the prerequisites, only visible in edit (work).
  7245. /*
  7246. $return .= '<tr>';
  7247. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7248. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7249. foreach($arrHide as $key => $value) {
  7250. if ($key == $s_selected_position && $action == 'add') {
  7251. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7252. }
  7253. elseif ($key == $id_prerequisite && $action == 'edit') {
  7254. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7255. }
  7256. else {
  7257. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7258. }
  7259. }
  7260. $return .= "</select></td>";
  7261. */
  7262. $return .= '</tr>';
  7263. }
  7264. $return .= '<tr>';
  7265. if ($action == 'add') {
  7266. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7267. } else {
  7268. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7269. }
  7270. $return .= '</tr>';
  7271. $return .= '</table>';
  7272. if ($action == 'move') {
  7273. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7274. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7275. }
  7276. if (is_numeric($extra_info)) {
  7277. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7278. } elseif (is_array($extra_info)) {
  7279. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7280. }
  7281. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7282. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7283. $return .= '</form>';
  7284. $return .= '</div>';
  7285. return $return;
  7286. }
  7287. /**
  7288. * Displays the menu for manipulating a step
  7289. *
  7290. * @param $item_id
  7291. * @param string $item_type
  7292. * @return string
  7293. */
  7294. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7295. {
  7296. $_course = api_get_course_info();
  7297. $course_id = api_get_course_int_id();
  7298. $course_code = api_get_course_id();
  7299. $return = '<div class="actions">';
  7300. switch ($item_type) {
  7301. case 'dokeos_chapter' :
  7302. case 'chapter' :
  7303. // Commented the message cause should not show it.
  7304. //$lang = get_lang('TitleManipulateChapter');
  7305. break;
  7306. case 'dokeos_module' :
  7307. case 'module' :
  7308. // Commented the message cause should not show it.
  7309. //$lang = get_lang('TitleManipulateModule');
  7310. break;
  7311. case TOOL_DOCUMENT :
  7312. // Commented the message cause should not show it.
  7313. //$lang = get_lang('TitleManipulateDocument');
  7314. break;
  7315. case TOOL_LINK :
  7316. case 'link' :
  7317. // Commented the message cause should not show it.
  7318. //$lang = get_lang('TitleManipulateLink');
  7319. break;
  7320. case TOOL_QUIZ :
  7321. // Commented the message cause should not show it.
  7322. //$lang = get_lang('TitleManipulateQuiz');
  7323. break;
  7324. case TOOL_STUDENTPUBLICATION :
  7325. // Commented the message cause should not show it.
  7326. //$lang = get_lang('TitleManipulateStudentPublication');
  7327. break;
  7328. }
  7329. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7330. $item_id = intval($item_id);
  7331. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7332. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7333. $result = Database::query($sql);
  7334. $row = Database::fetch_assoc($result);
  7335. $audio_player = null;
  7336. // We display an audio player if needed.
  7337. if (!empty($row['audio'])) {
  7338. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7339. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7340. </div>';
  7341. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7342. $audio_player .= '<script>
  7343. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7344. s1.addParam("allowscriptaccess","always");
  7345. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7346. s1.write("container");
  7347. </script>';
  7348. }
  7349. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7350. $return .= Display::url(
  7351. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7352. $url.'&action=edit_item&path_item=' . $row['path']
  7353. );
  7354. $return .= Display::url(
  7355. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7356. $url.'&action=move_item'
  7357. );
  7358. // Commented for now as prerequisites cannot be added to chapters.
  7359. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7360. $return .= Display::url(
  7361. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7362. $url.'&action=edit_item_prereq'
  7363. );
  7364. }
  7365. $return .= Display::url(
  7366. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7367. $url.'&action=delete_item'
  7368. );
  7369. if ($item_type == TOOL_HOTPOTATOES ) {
  7370. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7371. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7372. }
  7373. if ($item_type == TOOL_DOCUMENT ) {
  7374. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7375. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7376. }
  7377. $return .= '</div>';
  7378. if (!empty($audio_player)) {
  7379. $return .= '<br />'.$audio_player;
  7380. }
  7381. return $return;
  7382. }
  7383. /**
  7384. * Creates the javascript needed for filling up the checkboxes without page reload
  7385. * @return string
  7386. */
  7387. public function get_js_dropdown_array()
  7388. {
  7389. $course_id = api_get_course_int_id();
  7390. $return = 'var child_name = new Array();' . "\n";
  7391. $return .= 'var child_value = new Array();' . "\n\n";
  7392. $return .= 'child_name[0] = new Array();' . "\n";
  7393. $return .= 'child_value[0] = new Array();' . "\n\n";
  7394. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7395. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7396. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7397. ORDER BY display_order ASC";
  7398. $res_zero = Database::query($sql_zero);
  7399. $i = 0;
  7400. while ($row_zero = Database :: fetch_array($res_zero)) {
  7401. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7402. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7403. }
  7404. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7405. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7406. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7407. }
  7408. $return .= "\n";
  7409. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7410. $res = Database::query($sql);
  7411. while ($row = Database :: fetch_array($res)) {
  7412. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7413. WHERE
  7414. c_id = ".$course_id." AND
  7415. parent_item_id = " . $row['id'] . "
  7416. ORDER BY display_order ASC";
  7417. $res_parent = Database::query($sql_parent);
  7418. $i = 0;
  7419. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7420. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7421. while ($row_parent = Database :: fetch_array($res_parent)) {
  7422. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7423. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7424. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7425. }
  7426. $return .= "\n";
  7427. }
  7428. return $return;
  7429. }
  7430. /**
  7431. * Display the form to allow moving an item
  7432. * @param integer Item ID
  7433. * @return string HTML form
  7434. */
  7435. public function display_move_item($item_id)
  7436. {
  7437. $course_id = api_get_course_int_id();
  7438. $_course = api_get_course_info();
  7439. global $charset;
  7440. $return = '';
  7441. if (is_numeric($item_id)) {
  7442. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7443. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7444. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7445. $res = Database::query($sql);
  7446. $row = Database :: fetch_array($res);
  7447. switch ($row['item_type']) {
  7448. case 'dokeos_chapter' :
  7449. case 'dir' :
  7450. case 'asset' :
  7451. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7452. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7453. break;
  7454. case 'dokeos_module' :
  7455. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7456. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7457. break;
  7458. case TOOL_DOCUMENT :
  7459. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7460. $return .= $this->display_document_form('move', $item_id, $row);
  7461. break;
  7462. case TOOL_LINK :
  7463. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7464. $return .= $this->display_link_form('move', $item_id, $row);
  7465. break;
  7466. case TOOL_HOTPOTATOES :
  7467. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7468. $return .= $this->display_link_form('move', $item_id, $row);
  7469. break;
  7470. case TOOL_QUIZ :
  7471. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7472. $return .= $this->display_quiz_form('move', $item_id, $row);
  7473. break;
  7474. case TOOL_STUDENTPUBLICATION :
  7475. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7476. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7477. break;
  7478. case TOOL_FORUM :
  7479. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7480. $return .= $this->display_forum_form('move', $item_id, $row);
  7481. break;
  7482. case TOOL_THREAD :
  7483. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7484. $return .= $this->display_forum_form('move', $item_id, $row);
  7485. break;
  7486. }
  7487. }
  7488. return $return;
  7489. }
  7490. /**
  7491. * Displays a basic form on the overview page for changing the item title and the item description.
  7492. * @param string $item_type
  7493. * @param string $title
  7494. * @param array $data
  7495. * @return string
  7496. */
  7497. public function display_item_small_form($item_type, $title = '', $data = array())
  7498. {
  7499. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7500. $form = new FormValidator('small_form', 'post', $url);
  7501. $form->addElement('header', $title);
  7502. $form->addElement('text', 'title', get_lang('Title'));
  7503. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7504. $form->addElement('hidden', 'id', $data['id']);
  7505. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7506. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7507. $form->setDefaults(array('title' => $data['title']));
  7508. return $form->toHtml();
  7509. }
  7510. /**
  7511. * Return HTML form to allow prerequisites selection
  7512. * @todo use FormValidator
  7513. * @param integer Item ID
  7514. * @return string HTML form
  7515. */
  7516. public function display_item_prerequisites_form($item_id)
  7517. {
  7518. $course_id = api_get_course_int_id();
  7519. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7520. $item_id = intval($item_id);
  7521. /* Current prerequisite */
  7522. $sql = "SELECT * FROM $tbl_lp_item
  7523. WHERE c_id = $course_id AND id = " . $item_id;
  7524. $result = Database::query($sql);
  7525. $row = Database::fetch_array($result);
  7526. $prerequisiteId = $row['prerequisite'];
  7527. $return = '<legend>';
  7528. $return .= get_lang('AddEditPrerequisites');
  7529. $return .= '</legend>';
  7530. $return .= '<form method="POST">';
  7531. $return .= '<table class="data_table">';
  7532. $return .= '<tr>';
  7533. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7534. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7535. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7536. $return .= '</tr>';
  7537. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7538. $return .= '<tr >';
  7539. $return .= '<td colspan="3" class="radio">';
  7540. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7541. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7542. $return .= '</tr>';
  7543. $sql = "SELECT * FROM $tbl_lp_item
  7544. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7545. $result = Database::query($sql);
  7546. $arrLP = array();
  7547. $selectedMinScore = array();
  7548. $selectedMaxScore = array();
  7549. while ($row = Database :: fetch_array($result)) {
  7550. if ($row['id'] == $item_id) {
  7551. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7552. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7553. }
  7554. $arrLP[] = array(
  7555. 'id' => $row['id'],
  7556. 'item_type' => $row['item_type'],
  7557. 'title' => $row['title'],
  7558. 'ref' => $row['ref'],
  7559. 'description' => $row['description'],
  7560. 'parent_item_id' => $row['parent_item_id'],
  7561. 'previous_item_id' => $row['previous_item_id'],
  7562. 'next_item_id' => $row['next_item_id'],
  7563. 'max_score' => $row['max_score'],
  7564. 'min_score' => $row['min_score'],
  7565. 'mastery_score' => $row['mastery_score'],
  7566. 'prerequisite' => $row['prerequisite'],
  7567. 'next_item_id' => $row['next_item_id'],
  7568. 'display_order' => $row['display_order'],
  7569. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7570. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7571. );
  7572. }
  7573. $this->tree_array($arrLP);
  7574. $arrLP = $this->arrMenu;
  7575. unset($this->arrMenu);
  7576. for ($i = 0; $i < count($arrLP); $i++) {
  7577. $item = $arrLP[$i];
  7578. if ($item['id'] == $item_id) {
  7579. break;
  7580. }
  7581. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7582. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7583. $return .= '<tr>';
  7584. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7585. $return .= '<label for="id' . $item['id'] . '">';
  7586. $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'] . '" />';
  7587. $icon_name = str_replace(' ', '', $item['item_type']);
  7588. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7589. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7590. } else {
  7591. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7592. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7593. } else {
  7594. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7595. }
  7596. }
  7597. $return .= $item['title'] . '</label>';
  7598. $return .= '</td>';
  7599. if ($item['item_type'] == TOOL_QUIZ) {
  7600. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7601. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7602. $tmp_obj_exercice = new Exercise();
  7603. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7604. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7605. $tmp_obj_lp_item->update_in_bdd();
  7606. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7607. $return .= '<td class="exercise">';
  7608. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7609. $return .= '</td>';
  7610. $return .= '<td class="exercise">';
  7611. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7612. $return .= '</td>';
  7613. }
  7614. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7615. $return .= '<td class="exercise">';
  7616. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7617. $return .= '</td>';
  7618. $return .= '<td class="exercise"">';
  7619. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7620. $return .= '</td>';
  7621. }
  7622. $return .= '</tr>';
  7623. }
  7624. $return .= '<tr>';
  7625. $return .= '</tr>';
  7626. $return .= '</table>';
  7627. $return .= '<div style="padding-top:3px;">';
  7628. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7629. $return .= '</form>';
  7630. return $return;
  7631. }
  7632. /**
  7633. * Return HTML list to allow prerequisites selection for lp
  7634. * @param integer Item ID
  7635. * @return string HTML form
  7636. */
  7637. public function display_lp_prerequisites_list()
  7638. {
  7639. $course_id = api_get_course_int_id();
  7640. $lp_id = $this->lp_id;
  7641. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7642. // get current prerequisite
  7643. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7644. $result = Database::query($sql);
  7645. $row = Database :: fetch_array($result);
  7646. $prerequisiteId = $row['prerequisite'];
  7647. $session_id = api_get_session_id();
  7648. $session_condition = api_get_session_condition($session_id);
  7649. $sql = "SELECT * FROM $tbl_lp
  7650. WHERE c_id = $course_id $session_condition
  7651. ORDER BY display_order ";
  7652. $rs = Database::query($sql);
  7653. $return = '';
  7654. $return .= '<select name="prerequisites" class="form-control">';
  7655. $return .= '<option value="0">'.get_lang('None').'</option>';
  7656. if (Database::num_rows($rs) > 0) {
  7657. while ($row = Database::fetch_array($rs)) {
  7658. if ($row['id'] == $lp_id) {
  7659. continue;
  7660. }
  7661. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7662. }
  7663. }
  7664. $return .= '</select>';
  7665. return $return;
  7666. }
  7667. /**
  7668. * Creates a list with all the documents in it
  7669. * @param bool $showInvisibleFiles
  7670. * @return string
  7671. */
  7672. public function get_documents($showInvisibleFiles = false)
  7673. {
  7674. $course_info = api_get_course_info();
  7675. $sessionId = api_get_session_id();
  7676. $document_tree = DocumentManager::get_document_preview(
  7677. $course_info,
  7678. $this->lp_id,
  7679. null,
  7680. $sessionId,
  7681. true,
  7682. null,
  7683. null,
  7684. $showInvisibleFiles,
  7685. true
  7686. );
  7687. return $document_tree;
  7688. }
  7689. /**
  7690. * Creates a list with all the exercises (quiz) in it
  7691. * @return string
  7692. */
  7693. public function get_exercises()
  7694. {
  7695. $course_id = api_get_course_int_id();
  7696. // New for hotpotatoes.
  7697. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7698. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7699. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7700. $session_id = api_get_session_id();
  7701. $condition_session = api_get_session_condition($session_id);
  7702. $sql_quiz = "SELECT * FROM $tbl_quiz
  7703. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7704. ORDER BY title ASC";
  7705. $sql_hot = "SELECT * FROM $tbl_doc
  7706. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7707. ORDER BY id ASC";
  7708. $res_quiz = Database::query($sql_quiz);
  7709. $res_hot = Database::query($sql_hot);
  7710. $return = '<ul class="lp_resource">';
  7711. $return .= '<li class="lp_resource_element">';
  7712. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7713. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7714. get_lang('NewExercise') . '</a>';
  7715. $return .= '</li>';
  7716. // Display hotpotatoes
  7717. while ($row_hot = Database :: fetch_array($res_hot)) {
  7718. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7719. $return .= '<a class="moved" href="#">';
  7720. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7721. $return .= '</a> ';
  7722. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7723. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7724. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7725. $return .= '</li>';
  7726. }
  7727. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7728. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7729. $return .= '<a class="moved" href="#">';
  7730. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7731. $return .= '</a> ';
  7732. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7733. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7734. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7735. '</a>';
  7736. $return .= '</li>';
  7737. }
  7738. $return .= '</ul>';
  7739. return $return;
  7740. }
  7741. /**
  7742. * Creates a list with all the links in it
  7743. * @return string
  7744. */
  7745. public function get_links()
  7746. {
  7747. $selfUrl = api_get_self();
  7748. $courseIdReq = api_get_cidreq();
  7749. $course = api_get_course_info();
  7750. $course_id = $course['real_id'];
  7751. $tbl_link = Database::get_course_table(TABLE_LINK);
  7752. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7753. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7754. $session_id = api_get_session_id();
  7755. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7756. $sql = "SELECT link.id as link_id,
  7757. link.title as link_title,
  7758. link.category_id as category_id,
  7759. link_category.category_title as category_title
  7760. FROM $tbl_link as link
  7761. LEFT JOIN $linkCategoryTable as link_category
  7762. ON link.category_id = link_category.id
  7763. WHERE link.c_id = ".$course_id." $condition_session
  7764. ORDER BY link_category.category_title ASC, link.title ASC";
  7765. $links = Database::query($sql);
  7766. $categorizedLinks = array();
  7767. $categories = array();
  7768. while ($link = Database :: fetch_array($links)) {
  7769. if (!$link['category_id']) {
  7770. $link['category_title'] = get_lang('Uncategorized');
  7771. }
  7772. $categories[$link['category_id']] = $link['category_title'];
  7773. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7774. }
  7775. $linksHtmlCode =
  7776. '<script>
  7777. function toggle_tool(tool, id){
  7778. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7779. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7780. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7781. } else {
  7782. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7783. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7784. }
  7785. }
  7786. </script>
  7787. <ul class="lp_resource">
  7788. <li class="lp_resource_element">
  7789. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7790. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7791. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7792. </li>';
  7793. foreach ($categorizedLinks as $categoryId => $links) {
  7794. $linkNodes = null;
  7795. foreach ($links as $key => $title) {
  7796. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7797. $linkNodes .=
  7798. '<li class="lp_resource_element" data_id="'.$key.
  7799. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7800. <a class="moved" href="#">'.
  7801. $moveEverywhereIcon.
  7802. '</a>
  7803. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7804. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7805. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7806. Security::remove_XSS($title).
  7807. '</a>
  7808. </li>';
  7809. }
  7810. }
  7811. $linksHtmlCode .=
  7812. '<li>
  7813. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7814. style="vertical-align:middle">
  7815. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7816. align="absbottom" />
  7817. </a>
  7818. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7819. </li>
  7820. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7821. }
  7822. $linksHtmlCode .= '</ul>';
  7823. return $linksHtmlCode;
  7824. }
  7825. /**
  7826. * Creates a list with all the student publications in it
  7827. * @return unknown
  7828. */
  7829. public function get_student_publications()
  7830. {
  7831. $return = '<div class="lp_resource" >';
  7832. $return .= '<div class="lp_resource_element">';
  7833. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7834. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7835. $return .= '</div>';
  7836. $return .= '</div>';
  7837. return $return;
  7838. }
  7839. /**
  7840. * Creates a list with all the forums in it
  7841. * @return string
  7842. */
  7843. public function get_forums()
  7844. {
  7845. require_once '../forum/forumfunction.inc.php';
  7846. require_once '../forum/forumconfig.inc.php';
  7847. $a_forums = get_forums();
  7848. $return = '<ul class="lp_resource">';
  7849. //First add link
  7850. $return .= '<li class="lp_resource_element">';
  7851. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7852. $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>';
  7853. $return .= '</li>';
  7854. $return .= '<script>
  7855. function toggle_forum(forum_id){
  7856. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7857. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7858. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7859. } else {
  7860. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7861. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7862. }
  7863. }
  7864. </script>';
  7865. foreach ($a_forums as $forum) {
  7866. if (!empty($forum['forum_id'])) {
  7867. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7868. $return .= '<a class="moved" href="#">';
  7869. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7870. $return .= ' </a>';
  7871. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7872. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7873. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7874. </a>
  7875. <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">' .
  7876. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7877. $return .= '</li>';
  7878. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7879. $a_threads = get_threads($forum['forum_id']);
  7880. if (is_array($a_threads)) {
  7881. foreach ($a_threads as $thread) {
  7882. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7883. $return .= '&nbsp;<a class="moved" href="#">';
  7884. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7885. $return .= ' </a>';
  7886. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7887. $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 . '">' .
  7888. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7889. $return .= '</li>';
  7890. }
  7891. }
  7892. $return .= '</div>';
  7893. }
  7894. }
  7895. $return .= '</ul>';
  7896. return $return;
  7897. }
  7898. /**
  7899. * // TODO: The output encoding should be equal to the system encoding.
  7900. *
  7901. * Exports the learning path as a SCORM package. This is the main function that
  7902. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7903. * whole thing and returns the zip.
  7904. *
  7905. * This method needs to be called in PHP5, as it will fail with non-adequate
  7906. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7907. * you need to call it on a learnpath object.
  7908. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7909. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7910. * path has been modified, it should use the generic method here below.
  7911. * @TODO link this function with the export_lp() function in the same class
  7912. * @param string Optional name of zip file. If none, title of learnpath is
  7913. * domesticated and trailed with ".zip"
  7914. * @return string Returns the zip package string, or null if error
  7915. */
  7916. public function scorm_export()
  7917. {
  7918. $_course = api_get_course_info();
  7919. $course_id = $_course['real_id'];
  7920. // Remove memory and time limits as much as possible as this might be a long process...
  7921. if (function_exists('ini_set')) {
  7922. api_set_memory_limit('128M');
  7923. ini_set('max_execution_time', 600);
  7924. }
  7925. // Create the zip handler (this will remain available throughout the method).
  7926. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7927. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7928. $temp_dir_short = uniqid();
  7929. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7930. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7931. $zip_folder = new PclZip($temp_zip_file);
  7932. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7933. $root_path = $main_path = api_get_path(SYS_PATH);
  7934. $files_cleanup = array();
  7935. // Place to temporarily stash the zip file.
  7936. // create the temp dir if it doesn't exist
  7937. // or do a cleanup before creating the zip file.
  7938. if (!is_dir($temp_zip_dir)) {
  7939. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7940. } else {
  7941. // Cleanup: Check the temp dir for old files and delete them.
  7942. $handle = opendir($temp_zip_dir);
  7943. while (false !== ($file = readdir($handle))) {
  7944. if ($file != '.' && $file != '..') {
  7945. unlink("$temp_zip_dir/$file");
  7946. }
  7947. }
  7948. closedir($handle);
  7949. }
  7950. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7951. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7952. // Remove the possible . at the end of the path.
  7953. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7954. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7955. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7956. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7957. }
  7958. // Build a dummy imsmanifest structure.
  7959. // Do not add to the zip yet (we still need it).
  7960. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7961. // Aggregation Model official document, section "2.3 Content Packaging".
  7962. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7963. $xmldoc = new DOMDocument('1.0');
  7964. $root = $xmldoc->createElement('manifest');
  7965. $root->setAttribute('identifier', 'SingleCourseManifest');
  7966. $root->setAttribute('version', '1.1');
  7967. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7968. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7969. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7970. $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');
  7971. // Build mandatory sub-root container elements.
  7972. $metadata = $xmldoc->createElement('metadata');
  7973. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7974. $metadata->appendChild($md_schema);
  7975. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7976. $metadata->appendChild($md_schemaversion);
  7977. $root->appendChild($metadata);
  7978. $organizations = $xmldoc->createElement('organizations');
  7979. $resources = $xmldoc->createElement('resources');
  7980. // Build the only organization we will use in building our learnpaths.
  7981. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7982. $organization = $xmldoc->createElement('organization');
  7983. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7984. // To set the title of the SCORM entity (=organization), we take the name given
  7985. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7986. // learning path charset) as it is the encoding that defines how it is stored
  7987. // in the database. Then we convert it to HTML entities again as the "&" character
  7988. // alone is not authorized in XML (must be &amp;).
  7989. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7990. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7991. $organization->appendChild($org_title);
  7992. $folder_name = 'document';
  7993. // Removes the learning_path/scorm_folder path when exporting see #4841
  7994. $path_to_remove = null;
  7995. $result = $this->generate_lp_folder($_course);
  7996. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7997. $path_to_remove = 'document'.$result['dir'];
  7998. $path_to_replace = $folder_name.'/';
  7999. }
  8000. //Fixes chamilo scorm exports
  8001. if ($this->ref == 'chamilo_scorm_export') {
  8002. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8003. }
  8004. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8005. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8006. $link_updates = array();
  8007. $links_to_create = array();
  8008. foreach ($this->items as $index => $item) {
  8009. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8010. // Get included documents from this item.
  8011. if ($item->type == 'sco') {
  8012. $inc_docs = $item->get_resources_from_source(
  8013. null,
  8014. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8015. );
  8016. } else {
  8017. $inc_docs = $item->get_resources_from_source();
  8018. }
  8019. // Give a child element <item> to the <organization> element.
  8020. $my_item_id = $item->get_id();
  8021. $my_item = $xmldoc->createElement('item');
  8022. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8023. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8024. $my_item->setAttribute('isvisible', 'true');
  8025. // Give a child element <title> to the <item> element.
  8026. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8027. $my_item->appendChild($my_title);
  8028. // Give a child element <adlcp:prerequisites> to the <item> element.
  8029. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8030. $my_prereqs->setAttribute('type', 'aicc_script');
  8031. $my_item->appendChild($my_prereqs);
  8032. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8033. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8034. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8035. //$xmldoc->createElement('adlcp:timelimitaction','');
  8036. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8037. //$xmldoc->createElement('adlcp:datafromlms','');
  8038. // Give a child element <adlcp:masteryscore> to the <item> element.
  8039. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8040. $my_item->appendChild($my_masteryscore);
  8041. // Attach this item to the organization element or hits parent if there is one.
  8042. if (!empty($item->parent) && $item->parent != 0) {
  8043. $children = $organization->childNodes;
  8044. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8045. if (is_object($possible_parent)) {
  8046. $possible_parent->appendChild($my_item);
  8047. } else {
  8048. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8049. }
  8050. } else {
  8051. if ($this->debug > 0) { error_log('No parent'); }
  8052. $organization->appendChild($my_item);
  8053. }
  8054. // Get the path of the file(s) from the course directory root.
  8055. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8056. if (!empty($path_to_remove)) {
  8057. //From docs
  8058. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8059. //From quiz
  8060. if ($this->ref == 'chamilo_scorm_export') {
  8061. $path_to_remove = 'scorm/'.$this->path.'/';
  8062. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8063. }
  8064. } else {
  8065. $my_xml_file_path = $my_file_path;
  8066. }
  8067. $my_sub_dir = dirname($my_file_path);
  8068. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8069. $my_xml_sub_dir = $my_sub_dir;
  8070. // Give a <resource> child to the <resources> element
  8071. $my_resource = $xmldoc->createElement('resource');
  8072. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8073. $my_resource->setAttribute('type', 'webcontent');
  8074. $my_resource->setAttribute('href', $my_xml_file_path);
  8075. // adlcp:scormtype can be either 'sco' or 'asset'.
  8076. if ($item->type == 'sco') {
  8077. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8078. } else {
  8079. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8080. }
  8081. // xml:base is the base directory to find the files declared in this resource.
  8082. $my_resource->setAttribute('xml:base', '');
  8083. // Give a <file> child to the <resource> element.
  8084. $my_file = $xmldoc->createElement('file');
  8085. $my_file->setAttribute('href', $my_xml_file_path);
  8086. $my_resource->appendChild($my_file);
  8087. // Dependency to other files - not yet supported.
  8088. $i = 1;
  8089. foreach ($inc_docs as $doc_info) {
  8090. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8091. $my_dep = $xmldoc->createElement('resource');
  8092. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8093. $my_dep->setAttribute('identifier', $res_id);
  8094. $my_dep->setAttribute('type', 'webcontent');
  8095. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8096. $my_dep_file = $xmldoc->createElement('file');
  8097. // Check type of URL.
  8098. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8099. if ($doc_info[1] == 'remote') {
  8100. // Remote file. Save url as is.
  8101. $my_dep_file->setAttribute('href', $doc_info[0]);
  8102. $my_dep->setAttribute('xml:base', '');
  8103. } elseif ($doc_info[1] == 'local') {
  8104. switch ($doc_info[2]) {
  8105. case 'url': // Local URL - save path as url for now, don't zip file.
  8106. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8107. $current_dir = dirname($abs_path);
  8108. $current_dir = str_replace('\\', '/', $current_dir);
  8109. $file_path = realpath($abs_path);
  8110. $file_path = str_replace('\\', '/', $file_path);
  8111. $my_dep_file->setAttribute('href', $file_path);
  8112. $my_dep->setAttribute('xml:base', '');
  8113. if (strstr($file_path, $main_path) !== false) {
  8114. // The calculated real path is really inside Chamilo's root path.
  8115. // Reduce file path to what's under the DocumentRoot.
  8116. $file_path = substr($file_path, strlen($root_path) - 1);
  8117. //echo $file_path;echo '<br /><br />';
  8118. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8119. $zip_files_abs[] = $file_path;
  8120. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8121. $my_dep_file->setAttribute('href', $file_path);
  8122. $my_dep->setAttribute('xml:base', '');
  8123. } elseif (empty($file_path)) {
  8124. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8125. if (strpos($document_root, -1) == '/') {
  8126. $document_root = substr(0, -1, $document_root);
  8127. }*/
  8128. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8129. $file_path = str_replace('//', '/', $file_path);
  8130. if (file_exists($file_path)) {
  8131. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8132. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8133. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8134. $my_dep_file->setAttribute('href', $file_path);
  8135. $my_dep->setAttribute('xml:base', '');
  8136. }
  8137. }
  8138. break;
  8139. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8140. $my_dep_file->setAttribute('href', $doc_info[0]);
  8141. $my_dep->setAttribute('xml:base', '');
  8142. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8143. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8144. $abs_img_path_without_subdir = $doc_info[0];
  8145. $relp = api_get_path(REL_PATH); // The url-append config param.
  8146. $pos = strpos($abs_img_path_without_subdir, $relp);
  8147. if ($pos === 0) {
  8148. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8149. }
  8150. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8151. $file_path = str_replace('\\', '/', $file_path);
  8152. $file_path = str_replace('//', '/', $file_path);
  8153. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8154. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8155. // Check if the current document is in that path.
  8156. if (strstr($file_path, $cur_path) !== false) {
  8157. // The document is in that path, now get the relative path
  8158. // to the containing document.
  8159. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8160. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8161. $relative_path = '';
  8162. if (strstr($file_path, $cur_path) !== false) {
  8163. //$relative_path = substr($file_path, strlen($orig_file_path));
  8164. $relative_path = str_replace($cur_path, '', $file_path);
  8165. $file_path = substr($file_path, strlen($cur_path));
  8166. } else {
  8167. // This case is still a problem as it's difficult to calculate a relative path easily
  8168. // might still generate wrong links.
  8169. //$file_path = substr($file_path,strlen($cur_path));
  8170. // Calculate the directory path to the current file (without trailing slash).
  8171. $my_relative_path = dirname($file_path);
  8172. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8173. $my_relative_file = basename($file_path);
  8174. // Calculate the directory path to the containing file (without trailing slash).
  8175. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8176. $dotdot = '';
  8177. $subdir = '';
  8178. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8179. $my_relative_path2 = dirname($my_relative_path);
  8180. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8181. $my_orig_file_path = dirname($my_orig_file_path);
  8182. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8183. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8184. $dotdot += '../';
  8185. $my_relative_path = $my_relative_path2;
  8186. }
  8187. $relative_path = $dotdot.$subdir.$my_relative_file;
  8188. }
  8189. // Put the current document in the zip (this array is the array
  8190. // that will manage documents already in the course folder - relative).
  8191. $zip_files[] = $file_path;
  8192. // Update the links to the current document in the containing document (make them relative).
  8193. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8194. $my_dep_file->setAttribute('href', $file_path);
  8195. $my_dep->setAttribute('xml:base', '');
  8196. } elseif (strstr($file_path, $main_path) !== false) {
  8197. // The calculated real path is really inside Chamilo's root path.
  8198. // Reduce file path to what's under the DocumentRoot.
  8199. $file_path = substr($file_path, strlen($root_path));
  8200. //echo $file_path;echo '<br /><br />';
  8201. //error_log('Reduced path: '.$file_path, 0);
  8202. $zip_files_abs[] = $file_path;
  8203. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8204. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8205. $my_dep->setAttribute('xml:base', '');
  8206. } elseif (empty($file_path)) {
  8207. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8208. if(strpos($document_root,-1) == '/') {
  8209. $document_root = substr(0, -1, $document_root);
  8210. }*/
  8211. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8212. $file_path = str_replace('//', '/', $file_path);
  8213. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8214. $current_dir = dirname($abs_path);
  8215. $current_dir = str_replace('\\', '/', $current_dir);
  8216. if (file_exists($file_path)) {
  8217. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8218. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8219. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8220. $my_dep_file->setAttribute('href','document/'.$file_path);
  8221. $my_dep->setAttribute('xml:base', '');
  8222. }
  8223. }
  8224. break;
  8225. case 'rel':
  8226. // Path relative to the current document.
  8227. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8228. if (substr($doc_info[0], 0, 2) == '..') {
  8229. // Relative path going up.
  8230. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8231. $current_dir = str_replace('\\', '/', $current_dir);
  8232. $file_path = realpath($current_dir.$doc_info[0]);
  8233. $file_path = str_replace('\\', '/', $file_path);
  8234. //error_log($file_path.' <-> '.$main_path,0);
  8235. if (strstr($file_path, $main_path) !== false) {
  8236. // The calculated real path is really inside Chamilo's root path.
  8237. // Reduce file path to what's under the DocumentRoot.
  8238. $file_path = substr($file_path, strlen($root_path));
  8239. //error_log('Reduced path: '.$file_path, 0);
  8240. $zip_files_abs[] = $file_path;
  8241. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8242. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8243. $my_dep->setAttribute('xml:base', '');
  8244. }
  8245. } else {
  8246. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8247. $my_dep_file->setAttribute('href', $doc_info[0]);
  8248. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8249. }
  8250. break;
  8251. default:
  8252. $my_dep_file->setAttribute('href', $doc_info[0]);
  8253. $my_dep->setAttribute('xml:base', '');
  8254. break;
  8255. }
  8256. }
  8257. $my_dep->appendChild($my_dep_file);
  8258. $resources->appendChild($my_dep);
  8259. $dependency = $xmldoc->createElement('dependency');
  8260. $dependency->setAttribute('identifierref', $res_id);
  8261. $my_resource->appendChild($dependency);
  8262. $i++;
  8263. }
  8264. $resources->appendChild($my_resource);
  8265. $zip_files[] = $my_file_path;
  8266. } else {
  8267. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8268. switch ($item->type) {
  8269. case TOOL_LINK:
  8270. $my_item = $xmldoc->createElement('item');
  8271. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8272. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8273. $my_item->setAttribute('isvisible', 'true');
  8274. // Give a child element <title> to the <item> element.
  8275. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8276. $my_item->appendChild($my_title);
  8277. // Give a child element <adlcp:prerequisites> to the <item> element.
  8278. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8279. $my_prereqs->setAttribute('type', 'aicc_script');
  8280. $my_item->appendChild($my_prereqs);
  8281. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8282. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8283. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8284. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8285. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8286. //$xmldoc->createElement('adlcp:datafromlms', '');
  8287. // Give a child element <adlcp:masteryscore> to the <item> element.
  8288. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8289. $my_item->appendChild($my_masteryscore);
  8290. // Attach this item to the organization element or its parent if there is one.
  8291. if (!empty($item->parent) && $item->parent != 0) {
  8292. $children = $organization->childNodes;
  8293. for ($i = 0; $i < $children->length; $i++) {
  8294. $item_temp = $children->item($i);
  8295. if ($item_temp -> nodeName == 'item') {
  8296. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8297. $item_temp -> appendChild($my_item);
  8298. }
  8299. }
  8300. }
  8301. } else {
  8302. $organization->appendChild($my_item);
  8303. }
  8304. $my_file_path = 'link_'.$item->get_id().'.html';
  8305. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8306. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8307. $rs = Database::query($sql);
  8308. if ($link = Database :: fetch_array($rs)) {
  8309. $url = $link['url'];
  8310. $title = stripslashes($link['title']);
  8311. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8312. $my_xml_file_path = $my_file_path;
  8313. $my_sub_dir = dirname($my_file_path);
  8314. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8315. $my_xml_sub_dir = $my_sub_dir;
  8316. // Give a <resource> child to the <resources> element.
  8317. $my_resource = $xmldoc->createElement('resource');
  8318. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8319. $my_resource->setAttribute('type', 'webcontent');
  8320. $my_resource->setAttribute('href', $my_xml_file_path);
  8321. // adlcp:scormtype can be either 'sco' or 'asset'.
  8322. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8323. // xml:base is the base directory to find the files declared in this resource.
  8324. $my_resource->setAttribute('xml:base', '');
  8325. // give a <file> child to the <resource> element.
  8326. $my_file = $xmldoc->createElement('file');
  8327. $my_file->setAttribute('href', $my_xml_file_path);
  8328. $my_resource->appendChild($my_file);
  8329. $resources->appendChild($my_resource);
  8330. }
  8331. break;
  8332. case TOOL_QUIZ:
  8333. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8334. $exe = new Exercise();
  8335. $exe->read($exe_id);
  8336. $my_item = $xmldoc->createElement('item');
  8337. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8338. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8339. $my_item->setAttribute('isvisible', 'true');
  8340. // Give a child element <title> to the <item> element.
  8341. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8342. $my_item->appendChild($my_title);
  8343. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8344. //$my_item->appendChild($my_max_score);
  8345. // Give a child element <adlcp:prerequisites> to the <item> element.
  8346. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8347. $my_prereqs->setAttribute('type','aicc_script');
  8348. $my_item->appendChild($my_prereqs);
  8349. // Give a child element <adlcp:masteryscore> to the <item> element.
  8350. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8351. $my_item->appendChild($my_masteryscore);
  8352. // Attach this item to the organization element or hits parent if there is one.
  8353. if (!empty($item->parent) && $item->parent != 0) {
  8354. $children = $organization->childNodes;
  8355. for ($i = 0; $i < $children->length; $i++) {
  8356. $item_temp = $children->item($i);
  8357. if ($item_temp -> nodeName == 'item') {
  8358. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8359. $item_temp -> appendChild($my_item);
  8360. }
  8361. }
  8362. }
  8363. } else {
  8364. $organization->appendChild($my_item);
  8365. }
  8366. // Get the path of the file(s) from the course directory root
  8367. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8368. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8369. // Write the contents of the exported exercise into a (big) html file
  8370. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8371. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8372. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8373. $res = file_put_contents($tmp_file_path, $contents);
  8374. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8375. $files_cleanup[] = $tmp_file_path;
  8376. //error_log($tmp_path); die();
  8377. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8378. $my_xml_file_path = $my_file_path;
  8379. $my_sub_dir = dirname($my_file_path);
  8380. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8381. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8382. $my_xml_sub_dir = $my_sub_dir;
  8383. // Give a <resource> child to the <resources> element.
  8384. $my_resource = $xmldoc->createElement('resource');
  8385. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8386. $my_resource->setAttribute('type', 'webcontent');
  8387. $my_resource->setAttribute('href', $my_xml_file_path);
  8388. // adlcp:scormtype can be either 'sco' or 'asset'.
  8389. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8390. // xml:base is the base directory to find the files declared in this resource.
  8391. $my_resource->setAttribute('xml:base', '');
  8392. // Give a <file> child to the <resource> element.
  8393. $my_file = $xmldoc->createElement('file');
  8394. $my_file->setAttribute('href', $my_xml_file_path);
  8395. $my_resource->appendChild($my_file);
  8396. // Get included docs.
  8397. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8398. // Dependency to other files - not yet supported.
  8399. $i = 1;
  8400. foreach ($inc_docs as $doc_info) {
  8401. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8402. $my_dep = $xmldoc->createElement('resource');
  8403. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8404. $my_dep->setAttribute('identifier', $res_id);
  8405. $my_dep->setAttribute('type', 'webcontent');
  8406. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8407. $my_dep_file = $xmldoc->createElement('file');
  8408. // Check type of URL.
  8409. if ($doc_info[1] == 'remote') {
  8410. // Remote file. Save url as is.
  8411. $my_dep_file->setAttribute('href', $doc_info[0]);
  8412. $my_dep->setAttribute('xml:base', '');
  8413. } elseif ($doc_info[1] == 'local') {
  8414. switch ($doc_info[2]) {
  8415. case 'url': // Local URL - save path as url for now, don't zip file.
  8416. // Save file but as local file (retrieve from URL).
  8417. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8418. $current_dir = dirname($abs_path);
  8419. $current_dir = str_replace('\\', '/', $current_dir);
  8420. $file_path = realpath($abs_path);
  8421. $file_path = str_replace('\\', '/', $file_path);
  8422. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8423. $my_dep->setAttribute('xml:base', '');
  8424. if (strstr($file_path, $main_path) !== false) {
  8425. // The calculated real path is really inside the chamilo root path.
  8426. // Reduce file path to what's under the DocumentRoot.
  8427. $file_path = substr($file_path, strlen($root_path));
  8428. //echo $file_path;echo '<br /><br />';
  8429. //error_log('Reduced path: '.$file_path, 0);
  8430. $zip_files_abs[] = $file_path;
  8431. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8432. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8433. $my_dep->setAttribute('xml:base', '');
  8434. } elseif (empty($file_path)) {
  8435. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8436. if (strpos($document_root,-1) == '/') {
  8437. $document_root = substr(0, -1, $document_root);
  8438. }*/
  8439. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8440. $file_path = str_replace('//', '/', $file_path);
  8441. if (file_exists($file_path)) {
  8442. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8443. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8444. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8445. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8446. $my_dep->setAttribute('xml:base', '');
  8447. }
  8448. }
  8449. break;
  8450. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8451. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8452. $current_dir = str_replace('\\', '/', $current_dir);
  8453. $file_path = realpath($doc_info[0]);
  8454. $file_path = str_replace('\\', '/', $file_path);
  8455. $my_dep_file->setAttribute('href', $file_path);
  8456. $my_dep->setAttribute('xml:base', '');
  8457. if (strstr($file_path,$main_path) !== false) {
  8458. // The calculated real path is really inside the chamilo root path.
  8459. // Reduce file path to what's under the DocumentRoot.
  8460. $file_path = substr($file_path, strlen($root_path));
  8461. //echo $file_path;echo '<br /><br />';
  8462. //error_log('Reduced path: '.$file_path, 0);
  8463. $zip_files_abs[] = $file_path;
  8464. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8465. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8466. $my_dep->setAttribute('xml:base', '');
  8467. } elseif (empty($file_path)) {
  8468. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8469. if (strpos($document_root,-1) == '/') {
  8470. $document_root = substr(0, -1, $document_root);
  8471. }*/
  8472. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8473. $file_path = str_replace('//', '/', $file_path);
  8474. if (file_exists($file_path)) {
  8475. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8476. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8477. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8478. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8479. $my_dep->setAttribute('xml:base', '');
  8480. }
  8481. }
  8482. break;
  8483. 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
  8484. if (substr($doc_info[0], 0, 2) == '..') {
  8485. // Relative path going up.
  8486. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8487. $current_dir = str_replace('\\', '/', $current_dir);
  8488. $file_path = realpath($current_dir.$doc_info[0]);
  8489. $file_path = str_replace('\\', '/', $file_path);
  8490. //error_log($file_path.' <-> '.$main_path, 0);
  8491. if (strstr($file_path, $main_path) !== false) {
  8492. // The calculated real path is really inside Chamilo's root path.
  8493. // Reduce file path to what's under the DocumentRoot.
  8494. $file_path = substr($file_path, strlen($root_path));
  8495. $file_path_dest = $file_path;
  8496. // File path is courses/CHAMILO/document/....
  8497. $info_file_path = explode('/', $file_path);
  8498. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8499. $file_path_dest = 'document/'.$file_path;
  8500. }
  8501. //error_log('Reduced path: '.$file_path, 0);
  8502. $zip_files_abs[] = $file_path;
  8503. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8504. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8505. $my_dep->setAttribute('xml:base', '');
  8506. }
  8507. } else {
  8508. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8509. $my_dep_file->setAttribute('href', $doc_info[0]);
  8510. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8511. }
  8512. break;
  8513. default:
  8514. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8515. $my_dep->setAttribute('xml:base', '');
  8516. break;
  8517. }
  8518. }
  8519. $my_dep->appendChild($my_dep_file);
  8520. $resources->appendChild($my_dep);
  8521. $dependency = $xmldoc->createElement('dependency');
  8522. $dependency->setAttribute('identifierref', $res_id);
  8523. $my_resource->appendChild($dependency);
  8524. $i++;
  8525. }
  8526. $resources->appendChild($my_resource);
  8527. $zip_files[] = $my_file_path;
  8528. break;
  8529. default:
  8530. // Get the path of the file(s) from the course directory root
  8531. $my_file_path = 'non_exportable.html';
  8532. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8533. $my_xml_file_path = $my_file_path;
  8534. $my_sub_dir = dirname($my_file_path);
  8535. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8536. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8537. $my_xml_sub_dir = $my_sub_dir;
  8538. // Give a <resource> child to the <resources> element.
  8539. $my_resource = $xmldoc->createElement('resource');
  8540. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8541. $my_resource->setAttribute('type', 'webcontent');
  8542. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8543. // adlcp:scormtype can be either 'sco' or 'asset'.
  8544. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8545. // xml:base is the base directory to find the files declared in this resource.
  8546. $my_resource->setAttribute('xml:base', '');
  8547. // Give a <file> child to the <resource> element.
  8548. $my_file = $xmldoc->createElement('file');
  8549. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8550. $my_resource->appendChild($my_file);
  8551. $resources->appendChild($my_resource);
  8552. break;
  8553. }
  8554. }
  8555. }
  8556. $organizations->appendChild($organization);
  8557. $root->appendChild($organizations);
  8558. $root->appendChild($resources);
  8559. $xmldoc->appendChild($root);
  8560. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8561. // then add the file to the zip, then destroy the file (this is done automatically).
  8562. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8563. foreach ($zip_files as $file_path) {
  8564. if (empty($file_path)) {
  8565. continue;
  8566. }
  8567. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8568. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8569. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8570. }
  8571. $this->create_path($dest_file);
  8572. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8573. // Check if the file needs a link update.
  8574. if (in_array($file_path, array_keys($link_updates))) {
  8575. $string = file_get_contents($dest_file);
  8576. unlink($dest_file);
  8577. foreach ($link_updates[$file_path] as $old_new) {
  8578. // This is an ugly hack that allows .flv files to be found by the flv player that
  8579. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8580. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8581. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8582. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8583. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8584. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8585. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8586. }
  8587. //Fix to avoid problems with default_course_document
  8588. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8589. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8590. $newDestination = $old_new['dest'];
  8591. } else {
  8592. $newDestination = str_replace('document/', '', $old_new['dest']);
  8593. }
  8594. $string = str_replace($old_new['orig'], $newDestination, $string);
  8595. //Add files inside the HTMLs
  8596. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8597. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8598. if (file_exists($sys_course_path.$new_path)) {
  8599. copy($sys_course_path.$new_path, $destinationFile);
  8600. }
  8601. }
  8602. file_put_contents($dest_file, $string);
  8603. }
  8604. }
  8605. foreach ($zip_files_abs as $file_path) {
  8606. if (empty($file_path)) {
  8607. continue;
  8608. }
  8609. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8610. continue;
  8611. }
  8612. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8613. $this->create_path($dest_file);
  8614. copy($main_path.$file_path, $dest_file);
  8615. // Check if the file needs a link update.
  8616. if (in_array($file_path, array_keys($link_updates))) {
  8617. $string = file_get_contents($dest_file);
  8618. unlink($dest_file);
  8619. foreach ($link_updates[$file_path] as $old_new) {
  8620. // This is an ugly hack that allows .flv files to be found by the flv player that
  8621. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8622. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8623. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8624. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8625. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8626. }
  8627. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8628. }
  8629. file_put_contents($dest_file, $string);
  8630. }
  8631. }
  8632. if (is_array($links_to_create)) {
  8633. foreach ($links_to_create as $file => $link) {
  8634. $file_content = '<!DOCTYPE html><head>
  8635. <meta charset="'.api_get_language_isocode().'" />
  8636. <title>'.$link['title'].'</title>
  8637. </head>
  8638. <body dir="'.api_get_text_direction().'">
  8639. <div style="text-align:center">
  8640. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8641. </body>
  8642. </html>';
  8643. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8644. }
  8645. }
  8646. // Add non exportable message explanation.
  8647. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8648. $file_content = '<!DOCTYPE html><head>
  8649. <meta charset="'.api_get_language_isocode().'" />
  8650. <title>'.$lang_not_exportable.'</title>
  8651. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8652. </head>
  8653. <body dir="'.api_get_text_direction().'">';
  8654. $file_content .=
  8655. <<<EOD
  8656. <style>
  8657. .error-message {
  8658. font-family: arial, verdana, helvetica, sans-serif;
  8659. border-width: 1px;
  8660. border-style: solid;
  8661. left: 50%;
  8662. margin: 10px auto;
  8663. min-height: 30px;
  8664. padding: 5px;
  8665. right: 50%;
  8666. width: 500px;
  8667. background-color: #FFD1D1;
  8668. border-color: #FF0000;
  8669. color: #000;
  8670. }
  8671. </style>
  8672. <body>
  8673. <div class="error-message">
  8674. $lang_not_exportable
  8675. </div>
  8676. </body>
  8677. </html>
  8678. EOD;
  8679. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8680. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8681. }
  8682. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8683. // Add the extra files that go along with a SCORM package.
  8684. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8685. $extra_files = scandir($main_code_path);
  8686. foreach ($extra_files as $extra_file) {
  8687. if (strpos($extra_file, '.') === 0)
  8688. continue;
  8689. else {
  8690. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8691. $this->create_path($dest_file);
  8692. copy($main_code_path.$extra_file, $dest_file);
  8693. }
  8694. }
  8695. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8696. $manifest = @$xmldoc->saveXML();
  8697. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8698. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8699. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8700. // Clean possible temporary files.
  8701. foreach ($files_cleanup as $file) {
  8702. $res = unlink($file);
  8703. if ($res === false) {
  8704. error_log(
  8705. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8706. 0
  8707. );
  8708. }
  8709. }
  8710. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8711. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8712. }
  8713. /**
  8714. * @param int $lp_id
  8715. * @return bool
  8716. */
  8717. public function scorm_export_to_pdf($lp_id)
  8718. {
  8719. $lp_id = intval($lp_id);
  8720. $files_to_export = array();
  8721. $course_data = api_get_course_info($this->cc);
  8722. if (!empty($course_data)) {
  8723. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8724. $list = self::get_flat_ordered_items_list($lp_id);
  8725. if (!empty($list)) {
  8726. foreach ($list as $item_id) {
  8727. $item = $this->items[$item_id];
  8728. switch ($item->type) {
  8729. case 'document':
  8730. //Getting documents from a LP with chamilo documents
  8731. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8732. // Try loading document from the base course.
  8733. if (empty($file_data) && !empty($sessionId)) {
  8734. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8735. }
  8736. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8737. if (file_exists($file_path)) {
  8738. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8739. }
  8740. break;
  8741. case 'asset': //commes from a scorm package generated by chamilo
  8742. case 'sco':
  8743. $file_path = $scorm_path.'/'.$item->path;
  8744. if (file_exists($file_path)) {
  8745. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8746. }
  8747. break;
  8748. case 'dokeos_chapter':
  8749. case 'dir':
  8750. case 'chapter':
  8751. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8752. break;
  8753. }
  8754. }
  8755. }
  8756. $pdf = new PDF();
  8757. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8758. return $result;
  8759. }
  8760. return false;
  8761. }
  8762. /**
  8763. * Temp function to be moved in main_api or the best place around for this.
  8764. * Creates a file path if it doesn't exist
  8765. * @param string $path
  8766. */
  8767. public function create_path($path)
  8768. {
  8769. $path_bits = explode('/', dirname($path));
  8770. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8771. $path_built = IS_WINDOWS_OS ? '' : '/';
  8772. foreach ($path_bits as $bit) {
  8773. if (!empty ($bit)) {
  8774. $new_path = $path_built . $bit;
  8775. if (is_dir($new_path)) {
  8776. $path_built = $new_path . '/';
  8777. } else {
  8778. mkdir($new_path, api_get_permissions_for_new_directories());
  8779. $path_built = $new_path . '/';
  8780. }
  8781. }
  8782. }
  8783. }
  8784. /**
  8785. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8786. * @return boolean The results of the unlink function, or false if there was no image to start with
  8787. */
  8788. public function delete_lp_image()
  8789. {
  8790. $img = $this->get_preview_image();
  8791. if ($img != '') {
  8792. $del_file = $this->get_preview_image_path(null, 'sys');
  8793. if (isset($del_file) && file_exists($del_file)) {
  8794. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8795. if (file_exists($del_file_2)) {
  8796. unlink($del_file_2);
  8797. }
  8798. $this->set_preview_image('');
  8799. return @unlink($del_file);
  8800. }
  8801. }
  8802. return false;
  8803. }
  8804. /**
  8805. * Uploads an author image to the upload/learning_path/images directory
  8806. * @param array The image array, coming from the $_FILES superglobal
  8807. * @return boolean True on success, false on error
  8808. */
  8809. public function upload_image($image_array)
  8810. {
  8811. $image_moved = false;
  8812. if (!empty ($image_array['name'])) {
  8813. $upload_ok = process_uploaded_file($image_array);
  8814. $has_attachment = true;
  8815. } else {
  8816. $image_moved = true;
  8817. }
  8818. if ($upload_ok) {
  8819. if ($has_attachment) {
  8820. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8821. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8822. $updir = $sys_course_path . $courseDir;
  8823. // Try to add an extension to the file if it hasn't one.
  8824. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8825. if (!filter_extension($new_file_name)) {
  8826. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8827. $image_moved = false;
  8828. } else {
  8829. $file_extension = explode('.', $image_array['name']);
  8830. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8831. $filename = uniqid('');
  8832. $new_file_name = $filename.'.'.$file_extension;
  8833. $new_path = $updir.'/'.$new_file_name;
  8834. // Resize the image.
  8835. $temp = new Image($image_array['tmp_name']);
  8836. $picture_infos = $temp->get_image_info();
  8837. if ($picture_infos['width'] > 104) {
  8838. $thumbwidth = 104;
  8839. } else {
  8840. $thumbwidth = $picture_infos['width'];
  8841. }
  8842. if ($picture_infos['height'] > 96) {
  8843. $new_height = 96;
  8844. } else {
  8845. $new_height = $picture_infos['height'];
  8846. }
  8847. $temp->resize($thumbwidth, $new_height, 0);
  8848. $result = $temp->send_image($new_path);
  8849. // Storing the image filename.
  8850. if ($result) {
  8851. $image_moved = true;
  8852. $this->set_preview_image($new_file_name);
  8853. //Resize to 64px to use on course homepage
  8854. $temp->resize(64, 64, 0);
  8855. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8856. return true;
  8857. }
  8858. }
  8859. }
  8860. }
  8861. return false;
  8862. }
  8863. /**
  8864. * @param int $lp_id
  8865. * @param string $status
  8866. */
  8867. public function set_autolaunch($lp_id, $status)
  8868. {
  8869. $course_id = api_get_course_int_id();
  8870. $lp_id = intval($lp_id);
  8871. $status = intval($status);
  8872. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8873. // Setting everything to autolaunch = 0
  8874. $attributes['autolaunch'] = 0;
  8875. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8876. Database::update($lp_table, $attributes, $where);
  8877. if ($status == 1) {
  8878. //Setting my lp_id to autolaunch = 1
  8879. $attributes['autolaunch'] = 1;
  8880. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8881. Database::update($lp_table, $attributes, $where );
  8882. }
  8883. }
  8884. /**
  8885. * Gets previous_item_id for the next element of the lp_item table
  8886. * @author Isaac flores paz
  8887. * @return integer Previous item ID
  8888. */
  8889. public function select_previous_item_id()
  8890. {
  8891. $course_id = api_get_course_int_id();
  8892. if ($this->debug > 0) {
  8893. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8894. }
  8895. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8896. // Get the max order of the items
  8897. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8898. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8899. $rs_max_order = Database::query($sql_max_order);
  8900. $row_max_order = Database::fetch_object($rs_max_order);
  8901. $max_order = $row_max_order->display_order;
  8902. // Get the previous item ID
  8903. $sql = "SELECT id as previous FROM $table_lp_item
  8904. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8905. $rs_max = Database::query($sql);
  8906. $row_max = Database::fetch_object($rs_max);
  8907. // Return the previous item ID
  8908. return $row_max->previous;
  8909. }
  8910. /**
  8911. * Copies an LP
  8912. */
  8913. public function copy()
  8914. {
  8915. $main_path = api_get_path(SYS_CODE_PATH);
  8916. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8917. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8918. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8919. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8920. //Course builder
  8921. $cb = new CourseBuilder();
  8922. //Setting tools that will be copied
  8923. $cb->set_tools_to_build(array('learnpaths'));
  8924. //Setting elements that will be copied
  8925. $cb->set_tools_specific_id_list(
  8926. array('learnpaths' => array($this->lp_id))
  8927. );
  8928. $course = $cb->build();
  8929. //Course restorer
  8930. $course_restorer = new CourseRestorer($course);
  8931. $course_restorer->set_add_text_in_items(true);
  8932. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8933. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8934. }
  8935. public function verify_document_size($s)
  8936. {
  8937. $post_max = ini_get('post_max_size');
  8938. if (substr($post_max, -1, 1) == 'M') {
  8939. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  8940. } elseif (substr($post_max, -1, 1) == 'G') {
  8941. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  8942. }
  8943. $upl_max = ini_get('upload_max_filesize');
  8944. if (substr($upl_max, -1, 1) == 'M') {
  8945. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  8946. } elseif (substr($upl_max, -1, 1) == 'G') {
  8947. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  8948. }
  8949. $documents_total_space = DocumentManager::documents_total_space();
  8950. $course_max_space = DocumentManager::get_course_quota();
  8951. $total_size = filesize($s) + $documents_total_space;
  8952. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8953. return true;
  8954. } else{
  8955. return false;
  8956. }
  8957. }
  8958. /**
  8959. * Clear LP prerequisites
  8960. */
  8961. public function clear_prerequisites()
  8962. {
  8963. $course_id = $this->get_course_int_id();
  8964. if ($this->debug > 0) {
  8965. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8966. }
  8967. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8968. $lp_id = $this->get_id();
  8969. //Cleaning prerequisites
  8970. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8971. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8972. Database::query($sql);
  8973. //Cleaning mastery score for exercises
  8974. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8975. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8976. Database::query($sql);
  8977. }
  8978. public function set_previous_step_as_prerequisite_for_all_items()
  8979. {
  8980. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8981. $course_id = $this->get_course_int_id();
  8982. $lp_id = $this->get_id();
  8983. if (!empty($this->items)) {
  8984. $previous_item_id = null;
  8985. $previous_item_max = 0;
  8986. $previous_item_type = null;
  8987. $last_item_not_chapter = null;
  8988. $last_item_not_chapter_type = null;
  8989. $last_item_not_chapter_max = null;
  8990. foreach ($this->items as $item) {
  8991. // if there was a previous item... (otherwise jump to set it)
  8992. if (!empty($previous_item_id)) {
  8993. $current_item_id = $item->get_id(); //save current id
  8994. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8995. // Current item is not a folder, so it qualifies to get a prerequisites
  8996. if ($last_item_not_chapter_type == 'quiz') {
  8997. // if previous is quiz, mark its max score as default score to be achieved
  8998. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  8999. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9000. Database::query($sql);
  9001. }
  9002. // now simply update the prerequisite to set it to the last non-chapter item
  9003. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9004. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9005. Database::query($sql);
  9006. // record item as 'non-chapter' reference
  9007. $last_item_not_chapter = $item->get_id();
  9008. $last_item_not_chapter_type = $item->get_type();
  9009. $last_item_not_chapter_max = $item->get_max();
  9010. }
  9011. } else {
  9012. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9013. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9014. $last_item_not_chapter = $item->get_id();
  9015. $last_item_not_chapter_type = $item->get_type();
  9016. $last_item_not_chapter_max = $item->get_max();
  9017. }
  9018. }
  9019. // Saving the item as "previous item" for the next loop
  9020. $previous_item_id = $item->get_id();
  9021. $previous_item_max = $item->get_max();
  9022. $previous_item_type = $item->get_type();
  9023. }
  9024. }
  9025. }
  9026. /**
  9027. * @param array $params
  9028. */
  9029. public static function createCategory($params)
  9030. {
  9031. $em = Database::getManager();
  9032. $item = new CLpCategory();
  9033. $item->setName($params['name']);
  9034. $item->setCId($params['c_id']);
  9035. $em->persist($item);
  9036. $em->flush();
  9037. }
  9038. /**
  9039. * @param array $params
  9040. */
  9041. public static function updateCategory($params)
  9042. {
  9043. $em = Database::getManager();
  9044. /** @var CLpCategory $item */
  9045. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9046. if ($item) {
  9047. $item->setName($params['name']);
  9048. $em->merge($item);
  9049. $em->flush();
  9050. }
  9051. }
  9052. /**
  9053. * @param int $id
  9054. */
  9055. public static function moveUpCategory($id)
  9056. {
  9057. $em = Database::getManager();
  9058. /** @var CLpCategory $item */
  9059. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9060. if ($item) {
  9061. $position = $item->getPosition() - 1;
  9062. $item->setPosition($position);
  9063. $em->persist($item);
  9064. $em->flush();
  9065. }
  9066. }
  9067. /**
  9068. * @param int $id
  9069. */
  9070. public static function moveDownCategory($id)
  9071. {
  9072. $em = Database::getManager();
  9073. /** @var CLpCategory $item */
  9074. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9075. if ($item) {
  9076. $position = $item->getPosition() + 1;
  9077. $item->setPosition($position);
  9078. $em->persist($item);
  9079. $em->flush();
  9080. }
  9081. }
  9082. /**
  9083. * @param int $courseId
  9084. * @return int|mixed
  9085. */
  9086. public static function getCountCategories($courseId)
  9087. {
  9088. if (empty($course_id)) {
  9089. return 0;
  9090. }
  9091. $em = Database::getManager();
  9092. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9093. $query->setParameter('id', $courseId);
  9094. return $query->getSingleScalarResult();
  9095. }
  9096. /**
  9097. * @param int $courseId
  9098. *
  9099. * @return mixed
  9100. */
  9101. public static function getCategories($courseId)
  9102. {
  9103. $em = Database::getManager();
  9104. //Default behaviour
  9105. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9106. array('cId' => $course_id),
  9107. array('name' => 'ASC')
  9108. );*/
  9109. // Using doctrine extensions
  9110. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9111. array('cId' => $courseId)
  9112. )->getResult();
  9113. return $items;
  9114. }
  9115. /**
  9116. * @param int $id
  9117. *
  9118. * @return mixed
  9119. */
  9120. public static function getCategory($id)
  9121. {
  9122. $em = Database::getManager();
  9123. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9124. return $item;
  9125. }
  9126. /**
  9127. * @param int $courseId
  9128. * @return array
  9129. */
  9130. public static function getCategoryByCourse($courseId)
  9131. {
  9132. $em = Database::getManager();
  9133. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9134. return $items;
  9135. }
  9136. /**
  9137. * @param int $id
  9138. *
  9139. * @return mixed
  9140. */
  9141. public static function deleteCategory($id)
  9142. {
  9143. $em = Database::getManager();
  9144. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9145. if ($item) {
  9146. $courseId = $item->getCId();
  9147. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9148. $query->setParameter('id', $courseId);
  9149. $query->setParameter('catId', $item->getId());
  9150. $lps = $query->getResult();
  9151. // Setting category = 0.
  9152. if ($lps) {
  9153. foreach ($lps as $lpItem) {
  9154. $lpItem->setCategoryId(0);
  9155. }
  9156. }
  9157. // Removing category.
  9158. $em->remove($item);
  9159. $em->flush();
  9160. }
  9161. }
  9162. /**
  9163. * @param int $courseId
  9164. * @param bool $addSelectOption
  9165. *
  9166. * @return mixed
  9167. */
  9168. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9169. {
  9170. $items = self::getCategoryByCourse($courseId);
  9171. $cats = array();
  9172. if ($addSelectOption) {
  9173. $cats = array(get_lang('SelectACategory'));
  9174. }
  9175. if (!empty($items)) {
  9176. foreach ($items as $cat) {
  9177. $cats[$cat->getId()] = $cat->getName();
  9178. }
  9179. }
  9180. return $cats;
  9181. }
  9182. /**
  9183. * Return the scorm item type object with spaces replaced with _
  9184. * The return result is use to build a css classname like scorm_type_$return
  9185. * @param $in_type
  9186. * @return mixed
  9187. */
  9188. private static function format_scorm_type_item($in_type)
  9189. {
  9190. return str_replace(' ', '_', $in_type);
  9191. }
  9192. /**
  9193. * @return \learnpath
  9194. */
  9195. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9196. {
  9197. $lpObject = Session::read('lpobject');
  9198. $learnPath = null;
  9199. if (isset($lpObject)) {
  9200. $learnPath = unserialize($lpObject);
  9201. }
  9202. if (!is_object($learnPath)) {
  9203. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9204. }
  9205. return $learnPath;
  9206. }
  9207. /**
  9208. * @param int $itemId
  9209. * @return learnpathItem|false
  9210. */
  9211. public function getItem($itemId)
  9212. {
  9213. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9214. return $this->items[$itemId];
  9215. }
  9216. return false;
  9217. }
  9218. /**
  9219. * @return int
  9220. */
  9221. public function getCategoryId()
  9222. {
  9223. return $this->categoryId;
  9224. }
  9225. /**
  9226. * @param int $categoryId
  9227. * @return bool
  9228. */
  9229. public function setCategoryId($categoryId)
  9230. {
  9231. $this->categoryId = intval($categoryId);
  9232. $courseId = api_get_course_int_id();
  9233. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9234. $lp_id = $this->get_id();
  9235. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9236. WHERE c_id = $courseId AND id = $lp_id";
  9237. Database::query($sql);
  9238. return true;
  9239. }
  9240. /**
  9241. * Get whether this is a learning path with the possibility to subscribe
  9242. * users or not
  9243. * @return int
  9244. */
  9245. public function getSubscribeUsers()
  9246. {
  9247. return $this->subscribeUsers;
  9248. }
  9249. /**
  9250. * Set whether this is a learning path with the possibility to subscribe
  9251. * users or not
  9252. * @param int $subscribeUsers (0 = false, 1 = true)
  9253. */
  9254. public function setSubscribeUsers($value)
  9255. {
  9256. if ($this->debug > 0) {
  9257. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9258. }
  9259. $this->subscribeUsers = intval($value);;
  9260. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9261. $lp_id = $this->get_id();
  9262. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9263. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9264. Database::query($sql);
  9265. return true;
  9266. }
  9267. /**
  9268. * Calculate the count of stars for a user in this LP
  9269. * @param int $sessionId Optional. The session ID
  9270. * @return int The count of stars
  9271. */
  9272. public function getCalculateStars($sessionId = 0)
  9273. {
  9274. $stars = 0;
  9275. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9276. if ($progress > 50) {
  9277. $stars++;
  9278. }
  9279. // Calculate stars chapters evaluation
  9280. $exercisesItems = $this->getExercisesItems();
  9281. if (!empty($exercisesItems)) {
  9282. $totalResult = 0;
  9283. foreach ($exercisesItems as $exerciseItem) {
  9284. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9285. $this->user_id,
  9286. $exerciseItem->path,
  9287. $this->course_int_id,
  9288. $sessionId,
  9289. $this->lp_id,
  9290. $exerciseItem->db_id
  9291. );
  9292. $exerciseResult = 0;
  9293. foreach ($exerciseResultInfo as $result) {
  9294. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9295. }
  9296. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9297. $totalResult += $exerciseAverage;
  9298. }
  9299. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9300. if ($totalExerciseAverage >= 50) {
  9301. $stars++;
  9302. }
  9303. if ($totalExerciseAverage >= 80) {
  9304. $stars++;
  9305. }
  9306. }
  9307. // Calculate star for final evaluation
  9308. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9309. if (!empty($finalEvaluationItem)) {
  9310. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9311. $this->user_id,
  9312. $finalEvaluationItem->path,
  9313. $this->course_int_id,
  9314. $sessionId,
  9315. $this->lp_id,
  9316. $finalEvaluationItem->db_id
  9317. );
  9318. $evaluationResult = 0;
  9319. foreach ($evaluationResultInfo as $result) {
  9320. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9321. }
  9322. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9323. $evaluationAverage = $evaluationResult / $averageDivisor;
  9324. if ($evaluationAverage >= 80) {
  9325. $stars++;
  9326. }
  9327. }
  9328. return $stars;
  9329. }
  9330. /**
  9331. * Get the items of exercise type
  9332. * @return array The items. Otherwise return false
  9333. */
  9334. public function getExercisesItems()
  9335. {
  9336. $exercises = [];
  9337. foreach ($this->items as $item) {
  9338. if ($item->type != 'quiz') {
  9339. continue;
  9340. }
  9341. $exercises[] = $item;
  9342. }
  9343. array_pop($exercises);
  9344. return $exercises;
  9345. }
  9346. /**
  9347. * Get the item of exercise type (evaluation type)
  9348. * @return array The final evaluation. Otherwise return false
  9349. */
  9350. public function getFinalEvaluationItem()
  9351. {
  9352. $exercises = [];
  9353. foreach ($this->items as $item) {
  9354. if ($item->type != 'quiz') {
  9355. continue;
  9356. }
  9357. $exercises[] = $item;
  9358. }
  9359. return array_pop($exercises);
  9360. }
  9361. /**
  9362. * Calculate the total points achieved for the current user in this learning path
  9363. * @param int $sessionId Optional. The session Id
  9364. * @return int
  9365. */
  9366. public function getCalculateScore($sessionId = 0)
  9367. {
  9368. // Calculate stars chapters evaluation
  9369. $exercisesItems = $this->getExercisesItems();
  9370. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9371. $totalExercisesResult = 0;
  9372. $totalEvaluationResult = 0;
  9373. if ($exercisesItems !== false) {
  9374. foreach ($exercisesItems as $exerciseItem) {
  9375. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9376. $this->user_id,
  9377. $exerciseItem->path,
  9378. $this->course_int_id,
  9379. $sessionId,
  9380. $this->lp_id,
  9381. $exerciseItem->db_id
  9382. );
  9383. $exerciseResult = 0;
  9384. foreach ($exerciseResultInfo as $result) {
  9385. $exerciseResult += $result['exe_result'];
  9386. }
  9387. $totalExercisesResult += $exerciseResult;
  9388. }
  9389. }
  9390. if (!empty($finalEvaluationItem)) {
  9391. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9392. $this->user_id,
  9393. $finalEvaluationItem->path,
  9394. $this->course_int_id,
  9395. $sessionId,
  9396. $this->lp_id,
  9397. $finalEvaluationItem->db_id
  9398. );
  9399. foreach ($evaluationResultInfo as $result) {
  9400. $totalEvaluationResult += $result['exe_result'];
  9401. }
  9402. }
  9403. return $totalExercisesResult + $totalEvaluationResult;
  9404. }
  9405. }
  9406. if (!function_exists('trim_value')) {
  9407. function trim_value(& $value) {
  9408. $value = trim($value);
  9409. }
  9410. }