learnpath.class.php 450 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684
  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. $params = [
  202. 'c_id' => $course_id,
  203. 'lp_id' => $lp_id,
  204. 'user_id' => $user_id,
  205. 'view_count' => 1,
  206. 'session_id' => $session_id,
  207. 'last_item' => 0
  208. ];
  209. Database::insert($lp_table, $params);
  210. $this->lp_view_id = Database::insert_id();
  211. if ($this->debug > 2) {
  212. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  213. }
  214. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  215. Database::query($sql);
  216. }
  217. // Initialise items.
  218. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  219. $sql = "SELECT * FROM $lp_item_table
  220. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  221. ORDER BY parent_item_id, display_order";
  222. $res = Database::query($sql);
  223. if ($this->debug > 2) {
  224. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  225. error_log('-- Start while--', 0);
  226. }
  227. $lp_item_id_list = array();
  228. while ($row = Database::fetch_array($res)) {
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - ' .
  243. 'aicc object with id ' . $my_item_id .
  244. ' set in items[]', 0);
  245. }
  246. }
  247. break;
  248. case 2:
  249. $oItem = new scormItem('db', $row['id'], $course_id);
  250. if (is_object($oItem)) {
  251. $my_item_id = $oItem->get_id();
  252. $oItem->set_lp_view($this->lp_view_id, $course_id);
  253. $oItem->set_prevent_reinit($this->prevent_reinit);
  254. // Don't use reference here as the next loop will make the pointed object change.
  255. $this->items[$my_item_id] = $oItem;
  256. $this->refs_list[$oItem->ref] = $my_item_id;
  257. if ($this->debug > 2) {
  258. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  259. }
  260. }
  261. break;
  262. case 1:
  263. default:
  264. if ($this->debug > 2) {
  265. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  266. }
  267. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  268. if ($this->debug > 2) {
  269. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  270. }
  271. if (is_object($oItem)) {
  272. $my_item_id = $oItem->get_id();
  273. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  274. $oItem->set_prevent_reinit($this->prevent_reinit);
  275. // Don't use reference here as the next loop will make the pointed object change.
  276. $this->items[$my_item_id] = $oItem;
  277. $this->refs_list[$my_item_id] = $my_item_id;
  278. if ($this->debug > 2) {
  279. error_log(
  280. 'New LP - learnpath::__construct() ' . __LINE__ .
  281. ' - object with id ' . $my_item_id . ' set in items[]',
  282. 0);
  283. }
  284. }
  285. break;
  286. }
  287. // Setting the object level with variable $this->items[$i][parent]
  288. foreach ($this->items as $itemLPObject) {
  289. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  290. $itemLPObject->level = $level;
  291. }
  292. // Setting the view in the item object.
  293. if (is_object($this->items[$row['id']])) {
  294. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  295. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  296. $this->items[$row['id']]->current_start_time = 0;
  297. $this->items[$row['id']]->current_stop_time = 0;
  298. }
  299. }
  300. }
  301. if ($this->debug > 2) {
  302. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  303. }
  304. if (!empty($lp_item_id_list)) {
  305. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  306. if (!empty($lp_item_id_list_to_string)) {
  307. // Get last viewing vars.
  308. $lp_item_view_table = Database:: get_course_table(
  309. TABLE_LP_ITEM_VIEW
  310. );
  311. // This query should only return one or zero result.
  312. $sql = "SELECT lp_item_id, status
  313. FROM $lp_item_view_table
  314. WHERE
  315. c_id = $course_id AND
  316. lp_view_id = ".$this->lp_view_id." AND
  317. lp_item_id IN ('".$lp_item_id_list_to_string."')
  318. ORDER BY view_count DESC ";
  319. if ($this->debug > 2) {
  320. error_log(
  321. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  322. 0
  323. );
  324. }
  325. $status_list = array();
  326. $res = Database::query($sql);
  327. while ($row = Database:: fetch_array($res)) {
  328. $status_list[$row['lp_item_id']] = $row['status'];
  329. }
  330. foreach ($lp_item_id_list as $item_id) {
  331. if (isset($status_list[$item_id])) {
  332. $status = $status_list[$item_id];
  333. if (is_object($this->items[$item_id])) {
  334. $this->items[$item_id]->set_status($status);
  335. if (empty($status)) {
  336. $this->items[$item_id]->set_status(
  337. $this->default_status
  338. );
  339. }
  340. }
  341. } else {
  342. if (!api_is_invitee()) {
  343. if (is_object($this->items[$item_id])) {
  344. $this->items[$item_id]->set_status(
  345. $this->default_status
  346. );
  347. }
  348. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  349. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  350. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  351. if ($this->debug > 2) {
  352. error_log(
  353. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  354. 0
  355. );
  356. }
  357. $this->items[$item_id]->set_lp_view(
  358. $this->lp_view_id,
  359. $course_id
  360. );
  361. Database::query($sql);
  362. $insertId = Database::insert_id();
  363. $sql = "UPDATE $lp_item_view_table SET id = iid
  364. WHERE iid = $insertId";
  365. Database::query($sql);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. $this->ordered_items = self::get_flat_ordered_items_list(
  372. $this->get_id(),
  373. 0,
  374. $course_id
  375. );
  376. $this->max_ordered_items = 0;
  377. foreach ($this->ordered_items as $index => $dummy) {
  378. if ($index > $this->max_ordered_items && !empty($dummy)) {
  379. $this->max_ordered_items = $index;
  380. }
  381. }
  382. // TODO: Define the current item better.
  383. $this->first();
  384. if ($this->debug > 2) {
  385. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  386. }
  387. return true;
  388. }
  389. /**
  390. * @return string
  391. */
  392. public function getCourseCode()
  393. {
  394. return $this->cc;
  395. }
  396. /**
  397. * @return int
  398. */
  399. public function get_course_int_id()
  400. {
  401. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  402. }
  403. /**
  404. * @param $course_id
  405. * @return int
  406. */
  407. public function set_course_int_id($course_id)
  408. {
  409. return $this->course_int_id = intval($course_id);
  410. }
  411. /**
  412. * Get the depth level of LP item
  413. * @param $in_tab_items
  414. * @param $in_current_item_id
  415. * @return int
  416. */
  417. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  418. {
  419. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  420. if ($parent_item_id == 0) {
  421. return 0;
  422. } else {
  423. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  424. }
  425. }
  426. /**
  427. * Function rewritten based on old_add_item() from Yannick Warnier.
  428. * Due the fact that users can decide where the item should come, I had to overlook this function and
  429. * I found it better to rewrite it. Old function is still available.
  430. * Added also the possibility to add a description.
  431. *
  432. * @param int $parent
  433. * @param int $previous
  434. * @param string $type
  435. * @param int resource ID (ref)
  436. * @param string $title
  437. * @param string $description
  438. * @param int $prerequisites
  439. * @param int $max_time_allowed
  440. * @return int
  441. */
  442. public function add_item(
  443. $parent,
  444. $previous,
  445. $type = 'dokeos_chapter',
  446. $id,
  447. $title,
  448. $description,
  449. $prerequisites = 0,
  450. $max_time_allowed = 0
  451. ) {
  452. $course_id = $this->course_info['real_id'];
  453. if ($this->debug > 0) {
  454. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  455. }
  456. if (empty($course_id)) {
  457. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  458. $this->course_info = api_get_course_info($this->cc);
  459. $course_id = $this->course_info['real_id'];
  460. }
  461. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  462. $_course = $this->course_info;
  463. $parent = intval($parent);
  464. $previous = intval($previous);
  465. $id = intval($id);
  466. $max_time_allowed = htmlentities($max_time_allowed);
  467. if (empty($max_time_allowed)) {
  468. $max_time_allowed = 0;
  469. }
  470. $sql = "SELECT COUNT(id) AS num
  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;
  476. $res_count = Database::query($sql);
  477. $row = Database :: fetch_array($res_count);
  478. $num = $row['num'];
  479. if ($num > 0) {
  480. if ($previous == 0) {
  481. $sql = "SELECT id, next_item_id, display_order
  482. FROM " . $tbl_lp_item . "
  483. WHERE
  484. c_id = $course_id AND
  485. lp_id = " . $this->get_id() . " AND
  486. parent_item_id = " . $parent . " AND
  487. previous_item_id = 0 OR
  488. previous_item_id=" . $parent;
  489. $result = Database::query($sql);
  490. $row = Database :: fetch_array($result);
  491. $tmp_previous = 0;
  492. $next = $row['id'];
  493. $display_order = 0;
  494. } else {
  495. $previous = (int) $previous;
  496. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  497. FROM $tbl_lp_item
  498. WHERE
  499. c_id = $course_id AND
  500. lp_id = " . $this->get_id() . " AND
  501. id = " . $previous;
  502. $result = Database::query($sql);
  503. $row = Database:: fetch_array($result);
  504. $tmp_previous = $row['id'];
  505. $next = $row['next_item_id'];
  506. $display_order = $row['display_order'];
  507. }
  508. } else {
  509. $tmp_previous = 0;
  510. $next = 0;
  511. $display_order = 0;
  512. }
  513. $id = intval($id);
  514. $typeCleaned = Database::escape_string($type);
  515. if ($type == 'quiz') {
  516. $sql = 'SELECT SUM(ponderation)
  517. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  518. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  519. ON
  520. quiz_question.id = quiz_rel_question.question_id AND
  521. quiz_question.c_id = quiz_rel_question.c_id
  522. WHERE
  523. quiz_rel_question.exercice_id = '.$id." AND
  524. quiz_question.c_id = $course_id AND
  525. quiz_rel_question.c_id = $course_id ";
  526. $rsQuiz = Database::query($sql);
  527. $max_score = Database :: result($rsQuiz, 0, 0);
  528. // Disabling the exercise if we add it inside a LP
  529. $exercise = new Exercise($course_id);
  530. $exercise->read($id);
  531. $exercise->disable();
  532. $exercise->save();
  533. } else {
  534. $max_score = 100;
  535. }
  536. $params = array(
  537. "c_id" => $course_id,
  538. "lp_id" => $this->get_id(),
  539. "item_type" =>$typeCleaned ,
  540. "ref" => '',
  541. "title" =>$title ,
  542. "description" => $description,
  543. "path" => $id,
  544. "max_score" => $max_score,
  545. "parent_item_id" => $parent,
  546. "previous_item_id" => $previous,
  547. "next_item_id" => $next,
  548. "display_order" => $display_order +1,
  549. "prerequisite" => $prerequisites,
  550. "max_time_allowed" => $max_time_allowed,
  551. 'min_score' => 0,
  552. 'launch_data' => ''
  553. );
  554. if ($prerequisites != 0) {
  555. $params['prerequisite'] = $prerequisites;
  556. }
  557. $new_item_id = Database::insert($tbl_lp_item, $params);
  558. if ($this->debug > 2) {
  559. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  560. }
  561. if ($new_item_id) {
  562. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  563. Database::query($sql);
  564. // Update the item that should come after the new item.
  565. $sql = " UPDATE $tbl_lp_item SET
  566. previous_item_id = $new_item_id,
  567. next_item_id = $new_item_id,
  568. id = $new_item_id
  569. WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. // Update all the items after the new item.
  572. $sql = "UPDATE " . $tbl_lp_item . "
  573. SET display_order = display_order + 1
  574. WHERE
  575. c_id = $course_id AND
  576. lp_id = " . $this->get_id() . " AND
  577. id <> " . $new_item_id . " AND
  578. parent_item_id = " . $parent . " AND
  579. display_order > " . $display_order;
  580. Database::query($sql);
  581. // Update the item that should come after the new item.
  582. $sql = "UPDATE " . $tbl_lp_item . "
  583. SET ref = " . $new_item_id . "
  584. WHERE c_id = $course_id AND id = " . $new_item_id;
  585. Database::query($sql);
  586. // Upload audio.
  587. if (!empty($_FILES['mp3']['name'])) {
  588. // Create the audio folder if it does not exist yet.
  589. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  590. if (!is_dir($filepath . 'audio')) {
  591. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  592. $audio_id = add_document(
  593. $_course,
  594. '/audio',
  595. 'folder',
  596. 0,
  597. 'audio'
  598. );
  599. api_item_property_update(
  600. $_course,
  601. TOOL_DOCUMENT,
  602. $audio_id,
  603. 'FolderCreated',
  604. api_get_user_id(),
  605. null,
  606. null,
  607. null,
  608. null,
  609. api_get_session_id()
  610. );
  611. api_item_property_update(
  612. $_course,
  613. TOOL_DOCUMENT,
  614. $audio_id,
  615. 'invisible',
  616. api_get_user_id(),
  617. null,
  618. null,
  619. null,
  620. null,
  621. api_get_session_id()
  622. );
  623. }
  624. $file_path = handle_uploaded_document(
  625. $_course,
  626. $_FILES['mp3'],
  627. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  628. '/audio',
  629. api_get_user_id(),
  630. '',
  631. '',
  632. '',
  633. '',
  634. false
  635. );
  636. // Getting the filename only.
  637. $file_components = explode('/', $file_path);
  638. $file = $file_components[count($file_components) - 1];
  639. // Store the mp3 file in the lp_item table.
  640. $sql = "UPDATE $tbl_lp_item SET
  641. audio = '" . Database::escape_string($file) . "'
  642. WHERE id = '" . intval($new_item_id) . "'";
  643. Database::query($sql);
  644. }
  645. }
  646. return $new_item_id;
  647. }
  648. /**
  649. * Static admin function allowing addition of a learnpath to a course.
  650. * @param string Course code
  651. * @param string Learnpath name
  652. * @param string Learnpath description string, if provided
  653. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  654. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  655. * @param string Zip file containing the learnpath or directory containing the learnpath
  656. * @return integer The new learnpath ID on success, 0 on failure
  657. */
  658. public static function add_lp(
  659. $courseCode,
  660. $name,
  661. $description = '',
  662. $learnpath = 'guess',
  663. $origin = 'zip',
  664. $zipname = '',
  665. $publicated_on = '',
  666. $expired_on = '',
  667. $categoryId = 0
  668. ) {
  669. global $charset;
  670. if (!empty($courseCode)) {
  671. $courseInfo = api_get_course_info($courseCode);
  672. $course_id = $courseInfo['real_id'];
  673. } else {
  674. $course_id = api_get_course_int_id();
  675. $courseInfo = api_get_course_info();
  676. }
  677. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  678. // Check course code exists.
  679. // Check lp_name doesn't exist, otherwise append something.
  680. $i = 0;
  681. $name = Database::escape_string($name);
  682. $categoryId = intval($categoryId);
  683. // Session id.
  684. $session_id = api_get_session_id();
  685. $check_name = "SELECT * FROM $tbl_lp
  686. WHERE c_id = $course_id AND name = '$name'";
  687. $res_name = Database::query($check_name);
  688. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  689. //by default the publication date is the same that the creation date
  690. //The behaviour above was changed due BT#2800
  691. global $_custom;
  692. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  693. $publicated_on = '';
  694. } else {
  695. $publicated_on = api_get_utc_datetime();
  696. }
  697. } else {
  698. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  699. }
  700. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  701. $expired_on = '';
  702. } else {
  703. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  704. }
  705. while (Database :: num_rows($res_name)) {
  706. // There is already one such name, update the current one a bit.
  707. $i++;
  708. $name = $name . ' - ' . $i;
  709. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  710. $res_name = Database::query($check_name);
  711. }
  712. // New name does not exist yet; keep it.
  713. // Escape description.
  714. // Kevin: added htmlentities().
  715. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  716. $type = 1;
  717. switch ($learnpath) {
  718. case 'guess':
  719. break;
  720. case 'dokeos':
  721. case 'chamilo':
  722. $type = 1;
  723. break;
  724. case 'aicc':
  725. break;
  726. }
  727. switch ($origin) {
  728. case 'zip':
  729. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  730. break;
  731. case 'manual':
  732. default:
  733. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  734. $res_max = Database::query($get_max);
  735. if (Database :: num_rows($res_max) < 1) {
  736. $dsp = 1;
  737. } else {
  738. $row = Database :: fetch_array($res_max);
  739. $dsp = $row[0] + 1;
  740. }
  741. $params = [
  742. 'c_id' => $course_id,
  743. 'lp_type' => $type,
  744. 'name' => $name,
  745. 'description' => $description,
  746. 'path' => '',
  747. 'default_view_mod' => 'embedded',
  748. 'default_encoding' => 'UTF-8',
  749. 'display_order' => $dsp,
  750. 'content_maker' => 'Chamilo',
  751. 'content_local' => 'local',
  752. 'js_lib' => '',
  753. 'session_id' => $session_id,
  754. 'created_on' => api_get_utc_datetime(),
  755. 'modified_on' => api_get_utc_datetime(),
  756. 'publicated_on' => $publicated_on,
  757. 'expired_on' => $expired_on,
  758. 'category_id' => $categoryId,
  759. 'force_commit' => 0,
  760. 'content_license' => '',
  761. 'debug' => 0,
  762. 'theme' => '',
  763. 'preview_image' => '',
  764. 'author' => '',
  765. 'prerequisite' => 0,
  766. 'hide_toc_frame' => 0,
  767. 'seriousgame_mode' => 0,
  768. 'autolaunch' => 0,
  769. 'max_attempts' => 0,
  770. 'subscribe_users' => 0
  771. ];
  772. $id = Database::insert($tbl_lp, $params);
  773. if ($id > 0) {
  774. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  775. Database::query($sql);
  776. // Insert into item_property.
  777. api_item_property_update(
  778. $courseInfo,
  779. TOOL_LEARNPATH,
  780. $id,
  781. 'LearnpathAdded',
  782. api_get_user_id()
  783. );
  784. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo);
  785. return $id;
  786. }
  787. break;
  788. }
  789. }
  790. /**
  791. * Auto completes the parents of an item in case it's been completed or passed
  792. * @param integer $item Optional ID of the item from which to look for parents
  793. */
  794. public function autocomplete_parents($item)
  795. {
  796. $debug = $this->debug;
  797. if ($debug) {
  798. error_log('Learnpath::autocomplete_parents()', 0);
  799. }
  800. if (empty($item)) {
  801. $item = $this->current;
  802. }
  803. $currentItem = $this->getItem($item);
  804. if ($currentItem) {
  805. $parent_id = $currentItem->get_parent();
  806. $parent = $this->getItem($parent_id);
  807. if ($parent) {
  808. // if $item points to an object and there is a parent.
  809. if ($debug) {
  810. error_log(
  811. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  812. 0
  813. );
  814. }
  815. // New experiment including failed and browsed in completed status.
  816. //$current_status = $currentItem->get_status();
  817. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  818. // Fixes chapter auto complete
  819. if (true) {
  820. // If the current item is completed or passes or succeeded.
  821. $updateParentStatus = true;
  822. if ($debug) {
  823. error_log('Status of current item is alright', 0);
  824. }
  825. foreach ($parent->get_children() as $childItemId) {
  826. $childItem = $this->getItem($childItemId);
  827. // If children was not set try to get the info
  828. if (empty($childItem->db_item_view_id)) {
  829. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  830. }
  831. // Check all his brothers (parent's children) for completion status.
  832. if ($childItemId != $item) {
  833. if ($debug) {
  834. error_log(
  835. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  836. 0
  837. );
  838. }
  839. // Trying completing parents of failed and browsed items as well.
  840. if ($childItem->status_is(
  841. array(
  842. 'completed',
  843. 'passed',
  844. 'succeeded',
  845. 'browsed',
  846. 'failed'
  847. )
  848. )
  849. ) {
  850. // Keep completion status to true.
  851. continue;
  852. } else {
  853. if ($debug > 2) {
  854. error_log(
  855. '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,
  856. 0
  857. );
  858. }
  859. $updateParentStatus = false;
  860. break;
  861. }
  862. }
  863. }
  864. if ($updateParentStatus) {
  865. // If all the children were completed:
  866. $parent->set_status('completed');
  867. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  868. // Force the status to "completed"
  869. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  870. $this->update_queue[$parent->get_id()] = 'completed';
  871. if ($debug) {
  872. error_log(
  873. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  874. print_r($this->update_queue, 1),
  875. 0
  876. );
  877. }
  878. // Recursive call.
  879. $this->autocomplete_parents($parent->get_id());
  880. }
  881. }
  882. } else {
  883. if ($debug) {
  884. error_log("Parent #$parent_id does not exists");
  885. }
  886. }
  887. } else {
  888. if ($debug) {
  889. error_log("#$item is an item that doesn't have parents");
  890. }
  891. }
  892. }
  893. /**
  894. * Auto saves the current results into the database for the whole learnpath
  895. */
  896. public function autosave()
  897. {
  898. if ($this->debug > 0) {
  899. error_log('New LP - In learnpath::autosave()', 0);
  900. }
  901. // TODO: Add save operations for the learnpath itself.
  902. }
  903. /**
  904. * Closes the current resource
  905. *
  906. * Stops the timer
  907. * Saves into the database if required
  908. * Clears the current resource data from this object
  909. * @return boolean True on success, false on failure
  910. */
  911. public function close()
  912. {
  913. if ($this->debug > 0) {
  914. error_log('New LP - In learnpath::close()', 0);
  915. }
  916. if (empty ($this->lp_id)) {
  917. $this->error = 'Trying to close this learnpath but no ID is set';
  918. return false;
  919. }
  920. $this->current_time_stop = time();
  921. $this->ordered_items = array();
  922. $this->index = 0;
  923. unset ($this->lp_id);
  924. //unset other stuff
  925. return true;
  926. }
  927. /**
  928. * Static admin function allowing removal of a learnpath
  929. * @param array $courseInfo
  930. * @param integer Learnpath ID
  931. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  932. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  933. */
  934. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  935. {
  936. $course_id = api_get_course_int_id();
  937. if (!empty($courseInfo)) {
  938. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  939. }
  940. // TODO: Implement a way of getting this to work when the current object is not set.
  941. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  942. // If an ID is specifically given and the current LP is not the same, prevent delete.
  943. if (!empty ($id) && ($id != $this->lp_id)) {
  944. return false;
  945. }
  946. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  947. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  948. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  949. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  950. // Delete lp item id.
  951. foreach ($this->items as $id => $dummy) {
  952. $sql = "DELETE FROM $lp_item_view
  953. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  954. Database::query($sql);
  955. }
  956. // Proposed by Christophe (nickname: clefevre)
  957. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  958. Database::query($sql);
  959. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  960. Database::query($sql);
  961. self::toggle_publish($this->lp_id, 'i');
  962. if ($this->type == 2 || $this->type == 3) {
  963. // This is a scorm learning path, delete the files as well.
  964. $sql = "SELECT path FROM $lp
  965. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  966. $res = Database::query($sql);
  967. if (Database :: num_rows($res) > 0) {
  968. $row = Database :: fetch_array($res);
  969. $path = $row['path'];
  970. $sql = "SELECT id FROM $lp
  971. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  972. $res = Database::query($sql);
  973. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  974. if ($this->debug > 2) {
  975. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  976. }
  977. } else {
  978. // No other LP uses that directory, delete it.
  979. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  980. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  981. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  982. if ($this->debug > 2) {
  983. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  984. }
  985. // Proposed by Christophe (clefevre).
  986. if (strcmp(substr($path, -2), "/.") == 0) {
  987. $path = substr($path, 0, -1); // Remove "." at the end.
  988. }
  989. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  990. rmdirr($course_scorm_dir . $path);
  991. }
  992. }
  993. }
  994. }
  995. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  996. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  997. // Delete tools
  998. $sql = "DELETE FROM $tbl_tool
  999. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1000. Database::query($sql);
  1001. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1002. Database::query($sql);
  1003. // Updates the display order of all lps.
  1004. $this->update_display_order();
  1005. api_item_property_update(
  1006. api_get_course_info(),
  1007. TOOL_LEARNPATH,
  1008. $this->lp_id,
  1009. 'delete',
  1010. api_get_user_id()
  1011. );
  1012. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1013. if ($link_info !== false) {
  1014. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1015. }
  1016. if (api_get_setting('search_enabled') == 'true') {
  1017. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1018. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1019. }
  1020. }
  1021. /**
  1022. * Removes all the children of one item - dangerous!
  1023. * @param integer $id Element ID of which children have to be removed
  1024. * @return integer Total number of children removed
  1025. */
  1026. public function delete_children_items($id)
  1027. {
  1028. $course_id = $this->course_info['real_id'];
  1029. if ($this->debug > 0) {
  1030. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1031. }
  1032. $num = 0;
  1033. if (empty ($id) || $id != strval(intval($id))) {
  1034. return false;
  1035. }
  1036. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1037. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1038. $res = Database::query($sql);
  1039. while ($row = Database :: fetch_array($res)) {
  1040. $num += $this->delete_children_items($row['id']);
  1041. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1042. Database::query($sql_del);
  1043. $num++;
  1044. }
  1045. return $num;
  1046. }
  1047. /**
  1048. * Removes an item from the current learnpath
  1049. * @param integer $id Elem ID (0 if first)
  1050. * @param integer $remove Whether to remove the resource/data from the
  1051. * system or leave it (default: 'keep', others 'remove')
  1052. * @return integer Number of elements moved
  1053. * @todo implement resource removal
  1054. */
  1055. public function delete_item($id, $remove = 'keep')
  1056. {
  1057. $course_id = api_get_course_int_id();
  1058. if ($this->debug > 0) {
  1059. error_log('New LP - In learnpath::delete_item()', 0);
  1060. }
  1061. // TODO: Implement the resource removal.
  1062. if (empty ($id) || $id != strval(intval($id))) {
  1063. return false;
  1064. }
  1065. // First select item to get previous, next, and display order.
  1066. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1067. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1068. $res_sel = Database::query($sql_sel);
  1069. if (Database :: num_rows($res_sel) < 1) {
  1070. return false;
  1071. }
  1072. $row = Database :: fetch_array($res_sel);
  1073. $previous = $row['previous_item_id'];
  1074. $next = $row['next_item_id'];
  1075. $display = $row['display_order'];
  1076. $parent = $row['parent_item_id'];
  1077. $lp = $row['lp_id'];
  1078. // Delete children items.
  1079. $num = $this->delete_children_items($id);
  1080. if ($this->debug > 2) {
  1081. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1082. }
  1083. // Now delete the item.
  1084. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1085. if ($this->debug > 2) {
  1086. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1087. }
  1088. Database::query($sql_del);
  1089. // Now update surrounding items.
  1090. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1091. WHERE c_id = ".$course_id." AND id = $previous";
  1092. Database::query($sql_upd);
  1093. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1094. WHERE c_id = ".$course_id." AND id = $next";
  1095. Database::query($sql_upd);
  1096. // Now update all following items with new display order.
  1097. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1098. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1099. Database::query($sql_all);
  1100. //Removing prerequisites since the item will not longer exist
  1101. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1102. Database::query($sql_all);
  1103. // Remove from search engine if enabled.
  1104. if (api_get_setting('search_enabled') == 'true') {
  1105. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1106. $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';
  1107. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1108. $res = Database::query($sql);
  1109. if (Database :: num_rows($res) > 0) {
  1110. $row2 = Database :: fetch_array($res);
  1111. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1112. $di = new ChamiloIndexer();
  1113. $di->remove_document((int) $row2['search_did']);
  1114. }
  1115. $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';
  1116. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1117. Database::query($sql);
  1118. }
  1119. }
  1120. /**
  1121. * Updates an item's content in place
  1122. * @param integer $id Element ID
  1123. * @param integer $parent Parent item ID
  1124. * @param integer $previous Previous item ID
  1125. * @param string $title Item title
  1126. * @param string $description Item description
  1127. * @param string $prerequisites Prerequisites (optional)
  1128. * @param array $audio The array resulting of the $_FILES[mp3] element
  1129. * @param int $max_time_allowed
  1130. * @param string $url
  1131. * @return boolean True on success, false on error
  1132. */
  1133. public function edit_item(
  1134. $id,
  1135. $parent,
  1136. $previous,
  1137. $title,
  1138. $description,
  1139. $prerequisites = '0',
  1140. $audio = array(),
  1141. $max_time_allowed = 0,
  1142. $url = ''
  1143. ) {
  1144. $course_id = api_get_course_int_id();
  1145. $_course = api_get_course_info();
  1146. if ($this->debug > 0) {
  1147. error_log('New LP - In learnpath::edit_item()', 0);
  1148. }
  1149. if (empty ($max_time_allowed)) {
  1150. $max_time_allowed = 0;
  1151. }
  1152. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1153. return false;
  1154. }
  1155. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1156. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1157. $res_select = Database::query($sql_select);
  1158. $row_select = Database :: fetch_array($res_select);
  1159. $audio_update_sql = '';
  1160. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1161. // Create the audio folder if it does not exist yet.
  1162. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1163. if (!is_dir($filepath . 'audio')) {
  1164. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1165. $audio_id = add_document(
  1166. $_course,
  1167. '/audio',
  1168. 'folder',
  1169. 0,
  1170. 'audio'
  1171. );
  1172. api_item_property_update(
  1173. $_course,
  1174. TOOL_DOCUMENT,
  1175. $audio_id,
  1176. 'FolderCreated',
  1177. api_get_user_id(),
  1178. null,
  1179. null,
  1180. null,
  1181. null,
  1182. api_get_session_id()
  1183. );
  1184. api_item_property_update(
  1185. $_course,
  1186. TOOL_DOCUMENT,
  1187. $audio_id,
  1188. 'invisible',
  1189. api_get_user_id(),
  1190. null,
  1191. null,
  1192. null,
  1193. null,
  1194. api_get_session_id()
  1195. );
  1196. }
  1197. // Upload file in documents.
  1198. $pi = pathinfo($audio['name']);
  1199. if ($pi['extension'] == 'mp3') {
  1200. $c_det = api_get_course_info($this->cc);
  1201. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1202. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1203. $path = substr($path, 7);
  1204. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1205. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1206. }
  1207. }
  1208. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1209. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1210. // TODO: htmlspecialchars to be checked for encoding related problems.
  1211. if ($same_parent && $same_previous) {
  1212. // Only update title and description.
  1213. $sql = "UPDATE " . $tbl_lp_item . "
  1214. SET title = '" . Database::escape_string($title) . "',
  1215. prerequisite = '" . $prerequisites . "',
  1216. description = '" . Database::escape_string($description) . "'
  1217. " . $audio_update_sql . ",
  1218. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1219. WHERE c_id = ".$course_id." AND id = " . $id;
  1220. Database::query($sql);
  1221. } else {
  1222. $old_parent = $row_select['parent_item_id'];
  1223. $old_previous = $row_select['previous_item_id'];
  1224. $old_next = $row_select['next_item_id'];
  1225. $old_order = $row_select['display_order'];
  1226. $old_prerequisite = $row_select['prerequisite'];
  1227. $old_max_time_allowed = $row_select['max_time_allowed'];
  1228. /* BEGIN -- virtually remove the current item id */
  1229. /* for the next and previous item it is like the current item doesn't exist anymore */
  1230. if ($old_previous != 0) {
  1231. // Next
  1232. $sql = "UPDATE " . $tbl_lp_item . "
  1233. SET next_item_id = " . $old_next . "
  1234. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1235. Database::query($sql);
  1236. }
  1237. if ($old_next != 0) {
  1238. // Previous
  1239. $sql = "UPDATE " . $tbl_lp_item . "
  1240. SET previous_item_id = " . $old_previous . "
  1241. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1242. Database::query($sql);
  1243. }
  1244. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1245. $sql = "UPDATE " . $tbl_lp_item . "
  1246. SET display_order = display_order - 1
  1247. WHERE
  1248. c_id = ".$course_id." AND
  1249. display_order > " . $old_order . " AND
  1250. lp_id = " . $this->lp_id . " AND
  1251. parent_item_id = " . $old_parent;
  1252. Database::query($sql);
  1253. /* END -- virtually remove the current item id */
  1254. /* BEGIN -- update the current item id to his new location */
  1255. if ($previous == 0) {
  1256. // Select the data of the item that should come after the current item.
  1257. $sql = "SELECT id, display_order
  1258. FROM " . $tbl_lp_item . "
  1259. WHERE
  1260. c_id = ".$course_id." AND
  1261. lp_id = " . $this->lp_id . " AND
  1262. parent_item_id = " . $parent . " AND
  1263. previous_item_id = " . $previous;
  1264. $res_select_old = Database::query($sql);
  1265. $row_select_old = Database::fetch_array($res_select_old);
  1266. // If the new parent didn't have children before.
  1267. if (Database :: num_rows($res_select_old) == 0) {
  1268. $new_next = 0;
  1269. $new_order = 1;
  1270. } else {
  1271. $new_next = $row_select_old['id'];
  1272. $new_order = $row_select_old['display_order'];
  1273. }
  1274. } else {
  1275. // Select the data of the item that should come before the current item.
  1276. $sql = "SELECT next_item_id, display_order
  1277. FROM " . $tbl_lp_item . "
  1278. WHERE c_id = ".$course_id." AND id = " . $previous;
  1279. $res_select_old = Database::query($sql);
  1280. $row_select_old = Database :: fetch_array($res_select_old);
  1281. $new_next = $row_select_old['next_item_id'];
  1282. $new_order = $row_select_old['display_order'] + 1;
  1283. }
  1284. // TODO: htmlspecialchars to be checked for encoding related problems.
  1285. // Update the current item with the new data.
  1286. $sql = "UPDATE " . $tbl_lp_item . "
  1287. SET
  1288. title = '" . Database::escape_string($title) . "',
  1289. description = '" . Database::escape_string($description) . "',
  1290. parent_item_id = " . $parent . ",
  1291. previous_item_id = " . $previous . ",
  1292. next_item_id = " . $new_next . ",
  1293. display_order = " . $new_order . "
  1294. " . $audio_update_sql . "
  1295. WHERE c_id = ".$course_id." AND id = " . $id;
  1296. Database::query($sql);
  1297. if ($previous != 0) {
  1298. // Update the previous item's next_item_id.
  1299. $sql = "UPDATE " . $tbl_lp_item . "
  1300. SET next_item_id = " . $id . "
  1301. WHERE c_id = ".$course_id." AND id = " . $previous;
  1302. Database::query($sql);
  1303. }
  1304. if ($new_next != 0) {
  1305. // Update the next item's previous_item_id.
  1306. $sql = "UPDATE " . $tbl_lp_item . "
  1307. SET previous_item_id = " . $id . "
  1308. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1309. Database::query($sql);
  1310. }
  1311. if ($old_prerequisite != $prerequisites) {
  1312. $sql = "UPDATE " . $tbl_lp_item . "
  1313. SET prerequisite = '" . $prerequisites . "'
  1314. WHERE c_id = ".$course_id." AND id = " . $id;
  1315. Database::query($sql);
  1316. }
  1317. if ($old_max_time_allowed != $max_time_allowed) {
  1318. // update max time allowed
  1319. $sql = "UPDATE " . $tbl_lp_item . "
  1320. SET max_time_allowed = " . $max_time_allowed . "
  1321. WHERE c_id = ".$course_id." AND id = " . $id;
  1322. Database::query($sql);
  1323. }
  1324. // Update all the items with the same or a bigger display_order than the current item.
  1325. $sql = "UPDATE " . $tbl_lp_item . "
  1326. SET display_order = display_order + 1
  1327. WHERE
  1328. c_id = ".$course_id." AND
  1329. lp_id = " . $this->get_id() . " AND
  1330. id <> " . $id . " AND
  1331. parent_item_id = " . $parent . " AND
  1332. display_order >= " . $new_order;
  1333. Database::query($sql);
  1334. }
  1335. if ($row_select['item_type'] == 'link') {
  1336. $link = new Link();
  1337. $linkId = $row_select['path'];
  1338. $link->updateLink($linkId, $url);
  1339. }
  1340. }
  1341. /**
  1342. * Updates an item's prereq in place
  1343. * @param integer $id Element ID
  1344. * @param string $prerequisite_id Prerequisite Element ID
  1345. * @param int $mastery_score Prerequisite min score
  1346. * @param int $max_score Prerequisite max score
  1347. *
  1348. * @return boolean True on success, false on error
  1349. */
  1350. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1351. {
  1352. $course_id = api_get_course_int_id();
  1353. if ($this->debug > 0) {
  1354. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1355. }
  1356. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1357. return false;
  1358. }
  1359. $prerequisite_id = intval($prerequisite_id);
  1360. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1361. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1362. $mastery_score = 0;
  1363. }
  1364. if (!is_numeric($max_score) || $max_score < 0) {
  1365. $max_score = 100;
  1366. }
  1367. /*if ($mastery_score > $max_score) {
  1368. $max_score = $mastery_score;
  1369. }*/
  1370. if (!is_numeric($prerequisite_id)) {
  1371. $prerequisite_id = 'NULL';
  1372. }
  1373. $mastery_score = floatval($mastery_score);
  1374. $max_score = floatval($max_score);
  1375. $sql = " UPDATE $tbl_lp_item
  1376. SET
  1377. prerequisite = $prerequisite_id ,
  1378. prerequisite_min_score = $mastery_score ,
  1379. prerequisite_max_score = $max_score
  1380. WHERE c_id = $course_id AND id = $id";
  1381. Database::query($sql);
  1382. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1383. // Will this be enough to ensure unicity?
  1384. /*$sql = " UPDATE $tbl_lp_item
  1385. SET mastery_score = $mastery_score
  1386. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1387. Database::query($sql);*/
  1388. }
  1389. // TODO: Update the item object (can be ignored for now because refreshed).
  1390. return true;
  1391. }
  1392. /**
  1393. * Escapes a string with the available database escape function
  1394. * @param string String to escape
  1395. * @return string String escaped
  1396. * @deprecated use Database::escape_string
  1397. */
  1398. public function escape_string($string)
  1399. {
  1400. return Database::escape_string($string);
  1401. }
  1402. /**
  1403. * Static admin function exporting a learnpath into a zip file
  1404. * @param string Export type (scorm, zip, cd)
  1405. * @param string Course code
  1406. * @param integer Learnpath ID
  1407. * @param string Zip file name
  1408. * @return string Zip file path (or false on error)
  1409. */
  1410. public function export_lp($type, $course, $id, $zipname)
  1411. {
  1412. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1413. return false;
  1414. }
  1415. $url = '';
  1416. switch ($type) {
  1417. case 'scorm':
  1418. break;
  1419. case 'zip':
  1420. break;
  1421. case 'cdrom':
  1422. break;
  1423. }
  1424. return $url;
  1425. }
  1426. /**
  1427. * Gets all the chapters belonging to the same parent as the item/chapter given
  1428. * Can also be called as abstract method
  1429. * @param integer Item ID
  1430. * @return array A list of all the "brother items" (or an empty array on failure)
  1431. */
  1432. public function get_brother_chapters($id)
  1433. {
  1434. $course_id = api_get_course_int_id();
  1435. if ($this->debug > 0) {
  1436. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1437. }
  1438. if (empty($id)|| $id != strval(intval($id))) {
  1439. return array ();
  1440. }
  1441. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1442. $sql_parent = "SELECT * FROM $lp_item
  1443. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1444. $res_parent = Database::query($sql_parent);
  1445. if (Database :: num_rows($res_parent) > 0) {
  1446. $row_parent = Database :: fetch_array($res_parent);
  1447. $parent = $row_parent['parent_item_id'];
  1448. $sql_bros = "SELECT * FROM $lp_item
  1449. WHERE
  1450. c_id = ".$course_id." AND
  1451. parent_item_id = $parent AND
  1452. id = $id AND
  1453. item_type='dokeos_chapter'
  1454. ORDER BY display_order";
  1455. $res_bros = Database::query($sql_bros);
  1456. $list = array ();
  1457. while ($row_bro = Database :: fetch_array($res_bros)) {
  1458. $list[] = $row_bro;
  1459. }
  1460. return $list;
  1461. }
  1462. return array ();
  1463. }
  1464. /**
  1465. * Gets all the items belonging to the same parent as the item given
  1466. * Can also be called as abstract method
  1467. * @param integer Item ID
  1468. * @return array A list of all the "brother items" (or an empty array on failure)
  1469. */
  1470. public function get_brother_items($id)
  1471. {
  1472. $course_id = api_get_course_int_id();
  1473. if ($this->debug > 0) {
  1474. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1475. }
  1476. if (empty ($id) || $id != strval(intval($id))) {
  1477. return array ();
  1478. }
  1479. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1480. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1481. $res_parent = Database::query($sql_parent);
  1482. if (Database :: num_rows($res_parent) > 0) {
  1483. $row_parent = Database :: fetch_array($res_parent);
  1484. $parent = $row_parent['parent_item_id'];
  1485. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1486. ORDER BY display_order";
  1487. $res_bros = Database::query($sql_bros);
  1488. $list = array ();
  1489. while ($row_bro = Database :: fetch_array($res_bros)) {
  1490. $list[] = $row_bro;
  1491. }
  1492. return $list;
  1493. }
  1494. return array ();
  1495. }
  1496. /**
  1497. * Get the specific prefix index terms of this learning path
  1498. * @param string $prefix
  1499. * @return array Array of terms
  1500. */
  1501. public function get_common_index_terms_by_prefix($prefix)
  1502. {
  1503. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1504. $terms = get_specific_field_values_list_by_prefix(
  1505. $prefix,
  1506. $this->cc,
  1507. TOOL_LEARNPATH,
  1508. $this->lp_id
  1509. );
  1510. $prefix_terms = array();
  1511. if (!empty($terms)) {
  1512. foreach ($terms as $term) {
  1513. $prefix_terms[] = $term['value'];
  1514. }
  1515. }
  1516. return $prefix_terms;
  1517. }
  1518. /**
  1519. * Gets the number of items currently completed
  1520. * @return integer The number of items currently completed
  1521. */
  1522. public function get_complete_items_count()
  1523. {
  1524. if ($this->debug > 0) {
  1525. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1526. }
  1527. $i = 0;
  1528. $completedStatusList = array(
  1529. 'completed',
  1530. 'passed',
  1531. 'succeeded',
  1532. 'browsed',
  1533. 'failed'
  1534. );
  1535. foreach ($this->items as $id => $dummy) {
  1536. // Trying failed and browsed considered "progressed" as well.
  1537. if ($this->items[$id]->status_is($completedStatusList) &&
  1538. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1539. $this->items[$id]->get_type() != 'dir'
  1540. ) {
  1541. $i++;
  1542. }
  1543. }
  1544. return $i;
  1545. }
  1546. /**
  1547. * Gets the current item ID
  1548. * @return integer The current learnpath item id
  1549. */
  1550. public function get_current_item_id()
  1551. {
  1552. $current = 0;
  1553. if ($this->debug > 0) {
  1554. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1555. }
  1556. if (!empty($this->current)) {
  1557. $current = $this->current;
  1558. }
  1559. if ($this->debug > 2) {
  1560. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1561. }
  1562. return $current;
  1563. }
  1564. /**
  1565. * Force to get the first learnpath item id
  1566. * @return integer The current learnpath item id
  1567. */
  1568. public function get_first_item_id()
  1569. {
  1570. $current = 0;
  1571. if (is_array($this->ordered_items)) {
  1572. $current = $this->ordered_items[0];
  1573. }
  1574. return $current;
  1575. }
  1576. /**
  1577. * Gets the total number of items available for viewing in this SCORM
  1578. * @return integer The total number of items
  1579. */
  1580. public function get_total_items_count()
  1581. {
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1584. }
  1585. return count($this->items);
  1586. }
  1587. /**
  1588. * Gets the total number of items available for viewing in this SCORM but without chapters
  1589. * @return integer The total no-chapters number of items
  1590. */
  1591. public function get_total_items_count_without_chapters()
  1592. {
  1593. if ($this->debug > 0) {
  1594. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1595. }
  1596. $total = 0;
  1597. $typeListNotToCount = self::getChapterTypes();
  1598. foreach ($this->items as $temp2) {
  1599. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1600. $total++;
  1601. }
  1602. }
  1603. return $total;
  1604. }
  1605. /**
  1606. * Gets the first element URL.
  1607. * @return string URL to load into the viewer
  1608. */
  1609. public function first()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::first()', 0);
  1613. error_log('$this->last_item_seen '.$this->last_item_seen);
  1614. }
  1615. // Test if the last_item_seen exists and is not a dir.
  1616. if (count($this->ordered_items) == 0) {
  1617. $this->index = 0;
  1618. }
  1619. if ($this->debug > 0) {
  1620. if (isset($this->items[$this->last_item_seen])) {
  1621. $status = $this->items[$this->last_item_seen]->get_status();
  1622. }
  1623. error_log('status '.$status);
  1624. }
  1625. if (!empty($this->last_item_seen) &&
  1626. !empty($this->items[$this->last_item_seen]) &&
  1627. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1628. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1629. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1630. //&& !$this->items[$this->last_item_seen]->is_done()
  1631. ) {
  1632. if ($this->debug > 2) {
  1633. 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);
  1634. }
  1635. $index = -1;
  1636. foreach ($this->ordered_items as $myindex => $item_id) {
  1637. if ($item_id == $this->last_item_seen) {
  1638. $index = $myindex;
  1639. break;
  1640. }
  1641. }
  1642. if ($index == -1) {
  1643. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1644. if ($this->debug > 2) {
  1645. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1646. }
  1647. return false;
  1648. } else {
  1649. $this->last = $this->last_item_seen;
  1650. $this->current = $this->last_item_seen;
  1651. $this->index = $index;
  1652. }
  1653. } else {
  1654. if ($this->debug > 2) {
  1655. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1656. }
  1657. $index = 0;
  1658. // Loop through all ordered items and stop at the first item that is
  1659. // not a directory *and* that has not been completed yet.
  1660. while ( !empty($this->ordered_items[$index]) AND
  1661. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1662. (
  1663. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1664. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1665. $this->items[$this->ordered_items[$index]]->is_done() === true
  1666. ) AND $index < $this->max_ordered_items) {
  1667. $index++;
  1668. }
  1669. $this->last = $this->current;
  1670. // current is
  1671. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1672. $this->index = $index;
  1673. if ($this->debug > 2) {
  1674. error_log('$index ' . $index);
  1675. }
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1678. }
  1679. }
  1680. if ($this->debug > 2) {
  1681. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1682. }
  1683. }
  1684. /**
  1685. * Gets the information about an item in a format usable as JavaScript to update
  1686. * the JS API by just printing this content into the <head> section of the message frame
  1687. * @param int $item_id
  1688. * @return string
  1689. */
  1690. public function get_js_info($item_id = '')
  1691. {
  1692. if ($this->debug > 0) {
  1693. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1694. }
  1695. $info = '';
  1696. $item_id = intval($item_id);
  1697. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1698. //if item is defined, return values from DB
  1699. $oItem = $this->items[$item_id];
  1700. $info .= '<script language="javascript">';
  1701. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1702. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1703. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1704. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1705. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1706. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1707. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1708. $info .= "top.set_flag_synchronized();";
  1709. $info .= '</script>';
  1710. if ($this->debug > 2) {
  1711. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1712. }
  1713. return $info;
  1714. } else {
  1715. // If item_id is empty, just update to default SCORM data.
  1716. $info .= '<script language="javascript">';
  1717. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1718. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1719. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1720. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1721. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1722. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1723. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1724. $info .= "top.set_flag_synchronized();";
  1725. $info .= '</script>';
  1726. if ($this->debug > 2) {
  1727. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1728. }
  1729. return $info;
  1730. }
  1731. }
  1732. /**
  1733. * Gets the js library from the database
  1734. * @return string The name of the javascript library to be used
  1735. */
  1736. public function get_js_lib()
  1737. {
  1738. $lib = '';
  1739. if (!empty ($this->js_lib)) {
  1740. $lib = $this->js_lib;
  1741. }
  1742. return $lib;
  1743. }
  1744. /**
  1745. * Gets the learnpath database ID
  1746. * @return integer Learnpath ID in the lp table
  1747. */
  1748. public function get_id()
  1749. {
  1750. if (!empty ($this->lp_id)) {
  1751. return $this->lp_id;
  1752. } else {
  1753. return 0;
  1754. }
  1755. }
  1756. /**
  1757. * Gets the last element URL.
  1758. * @return string URL to load into the viewer
  1759. */
  1760. public function get_last()
  1761. {
  1762. if ($this->debug > 0) {
  1763. error_log('New LP - In learnpath::get_last()', 0);
  1764. }
  1765. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1766. if ($this->index > 0) {
  1767. $this->index = count($this->ordered_items) - 1;
  1768. return $this->ordered_items[$this->index];
  1769. }
  1770. return false;
  1771. }
  1772. /**
  1773. * Gets the navigation bar for the learnpath display screen
  1774. * @return string The HTML string to use as a navigation bar
  1775. */
  1776. public function get_navigation_bar($idBar = null, $display=null) {
  1777. if ($this->debug > 0) {
  1778. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1779. }
  1780. if(empty($idBar)){
  1781. $idBar='control-top';
  1782. }
  1783. /* if(empty($display)){
  1784. $display='display:block';
  1785. } */
  1786. $navbar = null;
  1787. $lp_id = $this->lp_id;
  1788. $mycurrentitemid = $this->get_current_item_id();
  1789. if ($this->mode == 'fullscreen') {
  1790. $navbar = '
  1791. <span id="'.$idBar.'" class="buttons">
  1792. <a class="icon-toolbar" 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">
  1793. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1794. </a>
  1795. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1796. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1797. </a>
  1798. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1799. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1800. </a>
  1801. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1802. <span class="fa fa-columns"></span><span class="sr-only">' . get_lang('ScormExitFullScreen') . '</span>
  1803. </a>
  1804. </span>';
  1805. } else {
  1806. $navbar = '
  1807. <span id="'.$idBar.'" class="buttons text-right">
  1808. <a class="icon-toolbar" 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">
  1809. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1810. </a>
  1811. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1812. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1813. </a>
  1814. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1815. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1816. </a>
  1817. </span>';
  1818. }
  1819. return $navbar;
  1820. }
  1821. /**
  1822. * Gets the next resource in queue (url).
  1823. * @return string URL to load into the viewer
  1824. */
  1825. public function get_next_index()
  1826. {
  1827. if ($this->debug > 0) {
  1828. error_log('New LP - In learnpath::get_next_index()', 0);
  1829. }
  1830. // TODO
  1831. $index = $this->index;
  1832. $index++;
  1833. if ($this->debug > 2) {
  1834. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1835. }
  1836. 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) {
  1837. $index++;
  1838. if ($index == $this->max_ordered_items){
  1839. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1840. return $this->index;
  1841. } else {
  1842. return $index;
  1843. }
  1844. }
  1845. }
  1846. if (empty ($this->ordered_items[$index])) {
  1847. return $this->index;
  1848. }
  1849. if ($this->debug > 2) {
  1850. error_log('New LP - index is now ' . $index, 0);
  1851. }
  1852. return $index;
  1853. }
  1854. /**
  1855. * Gets item_id for the next element
  1856. * @return integer Next item (DB) ID
  1857. */
  1858. public function get_next_item_id()
  1859. {
  1860. if ($this->debug > 0) {
  1861. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1862. }
  1863. $new_index = $this->get_next_index();
  1864. if (!empty ($new_index)) {
  1865. if (isset ($this->ordered_items[$new_index])) {
  1866. if ($this->debug > 2) {
  1867. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1868. }
  1869. return $this->ordered_items[$new_index];
  1870. }
  1871. }
  1872. if ($this->debug > 2) {
  1873. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1874. }
  1875. return 0;
  1876. }
  1877. /**
  1878. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1879. *
  1880. * Generally, the package provided is in the form of a zip file, so the function
  1881. * has been written to test a zip file. If not a zip, the function will return the
  1882. * default return value: ''
  1883. * @param string the path to the file
  1884. * @param string the original name of the file
  1885. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1886. */
  1887. public static function get_package_type($file_path, $file_name)
  1888. {
  1889. // Get name of the zip file without the extension.
  1890. $file_info = pathinfo($file_name);
  1891. $filename = $file_info['basename']; // Name including extension.
  1892. $extension = $file_info['extension']; // Extension only.
  1893. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1894. 'dll',
  1895. 'exe'
  1896. ))) {
  1897. return 'oogie';
  1898. }
  1899. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1900. 'dll',
  1901. 'exe'
  1902. ))) {
  1903. return 'woogie';
  1904. }
  1905. // Filename without its extension.
  1906. $file_base_name = str_replace('.' . $extension, '', $filename);
  1907. $zipFile = new PclZip($file_path);
  1908. // Check the zip content (real size and file extension).
  1909. $zipContentArray = $zipFile->listContent();
  1910. $package_type = '';
  1911. $at_root = false;
  1912. $manifest = '';
  1913. $aicc_match_crs = 0;
  1914. $aicc_match_au = 0;
  1915. $aicc_match_des = 0;
  1916. $aicc_match_cst = 0;
  1917. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1918. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1919. foreach ($zipContentArray as $thisContent) {
  1920. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1921. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1922. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1923. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1924. $package_type = 'scorm';
  1925. break; // Exit the foreach loop.
  1926. } elseif (
  1927. preg_match('/aicc\//i', $thisContent['filename']) ||
  1928. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1929. ) {
  1930. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1931. switch ($ext) {
  1932. case 'crs':
  1933. $aicc_match_crs = 1;
  1934. break;
  1935. case 'au':
  1936. $aicc_match_au = 1;
  1937. break;
  1938. case 'des':
  1939. $aicc_match_des = 1;
  1940. break;
  1941. case 'cst':
  1942. $aicc_match_cst = 1;
  1943. break;
  1944. default:
  1945. break;
  1946. }
  1947. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1948. } else {
  1949. $package_type = '';
  1950. }
  1951. }
  1952. }
  1953. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1954. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1955. $package_type = 'aicc';
  1956. }
  1957. return $package_type;
  1958. }
  1959. /**
  1960. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1961. * @return string URL to load into the viewer
  1962. */
  1963. public function get_previous_index()
  1964. {
  1965. if ($this->debug > 0) {
  1966. error_log('New LP - In learnpath::get_previous_index()', 0);
  1967. }
  1968. $index = $this->index;
  1969. if (isset ($this->ordered_items[$index -1])) {
  1970. $index--;
  1971. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1972. $index--;
  1973. if ($index < 0) {
  1974. return $this->index;
  1975. }
  1976. }
  1977. } else {
  1978. if ($this->debug > 2) {
  1979. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1980. }
  1981. // There is no previous item.
  1982. }
  1983. return $index;
  1984. }
  1985. /**
  1986. * Gets item_id for the next element
  1987. * @return integer Previous item (DB) ID
  1988. */
  1989. public function get_previous_item_id()
  1990. {
  1991. if ($this->debug > 0) {
  1992. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1993. }
  1994. $new_index = $this->get_previous_index();
  1995. return $this->ordered_items[$new_index];
  1996. }
  1997. /**
  1998. * Gets the progress value from the progress_db attribute
  1999. * @return integer Current progress value
  2000. */
  2001. public function get_progress()
  2002. {
  2003. if ($this->debug > 0) {
  2004. error_log('New LP - In learnpath::get_progress()', 0);
  2005. }
  2006. if (!empty ($this->progress_db)) {
  2007. return $this->progress_db;
  2008. }
  2009. return 0;
  2010. }
  2011. /**
  2012. * Returns the HTML necessary to print a mediaplayer block inside a page
  2013. * @return string The mediaplayer HTML
  2014. */
  2015. public function get_mediaplayer($autostart = 'true')
  2016. {
  2017. $course_id = api_get_course_int_id();
  2018. $_course = api_get_course_info();
  2019. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2020. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2021. // Getting all the information about the item.
  2022. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2023. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2024. ON lp.id = lp_view.lp_item_id
  2025. WHERE
  2026. lp.id = '".$_SESSION['oLP']->current."' AND
  2027. lp.c_id = $course_id AND
  2028. lp_view.c_id = $course_id";
  2029. $result = Database::query($sql);
  2030. $row = Database::fetch_assoc($result);
  2031. $output = '';
  2032. if (!empty ($row['audio'])) {
  2033. $list = $_SESSION['oLP']->get_toc();
  2034. $type_quiz = false;
  2035. foreach($list as $toc) {
  2036. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2037. $type_quiz = true;
  2038. }
  2039. }
  2040. if ($type_quiz) {
  2041. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2042. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2043. } else {
  2044. $autostart_audio = $autostart;
  2045. }
  2046. } else {
  2047. $autostart_audio = 'true';
  2048. }
  2049. $courseInfo = api_get_course_info();
  2050. $audio = $row['audio'];
  2051. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2052. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2053. if (!file_exists($file)) {
  2054. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2055. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2056. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2057. }
  2058. $player = Display::getMediaPlayer(
  2059. $file,
  2060. array(
  2061. 'id' => 'lp_audio_media_player',
  2062. 'url' => $url,
  2063. 'autoplay' => $autostart_audio,
  2064. 'width' => '100%'
  2065. )
  2066. );
  2067. // The mp3 player.
  2068. $output = '<div id="container">';
  2069. $output .= $player;
  2070. $output .= '</div>';
  2071. }
  2072. return $output;
  2073. }
  2074. /**
  2075. * Checks if the learning path is visible for student after the progress
  2076. * of its prerequisite is completed, considering the time availability and
  2077. * the LP visibility.
  2078. * @param int $lp_id
  2079. * @param int $student_id
  2080. * @param string Course code (optional)
  2081. * @param int $sessionId
  2082. * @return bool
  2083. */
  2084. public static function is_lp_visible_for_student(
  2085. $lp_id,
  2086. $student_id,
  2087. $courseCode = null,
  2088. $sessionId = null
  2089. ) {
  2090. $lp_id = (int)$lp_id;
  2091. $courseInfo = api_get_course_info($courseCode);
  2092. $sessionId = intval($sessionId);
  2093. if (empty($sessionId)) {
  2094. $sessionId = api_get_session_id();
  2095. }
  2096. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2097. // Get current prerequisite
  2098. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2099. FROM $tbl_learnpath
  2100. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2101. $itemInfo = api_get_item_property_info(
  2102. $courseInfo['real_id'],
  2103. TOOL_LEARNPATH,
  2104. $lp_id,
  2105. $sessionId
  2106. );
  2107. // If the item was deleted.
  2108. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2109. return false;
  2110. }
  2111. $rs = Database::query($sql);
  2112. $now = time();
  2113. if (Database::num_rows($rs) > 0) {
  2114. $row = Database::fetch_array($rs, 'ASSOC');
  2115. $prerequisite = $row['prerequisite'];
  2116. $is_visible = true;
  2117. if (!empty($prerequisite)) {
  2118. $progress = self::getProgress(
  2119. $prerequisite,
  2120. $student_id,
  2121. $courseInfo['real_id'],
  2122. $sessionId
  2123. );
  2124. $progress = intval($progress);
  2125. if ($progress < 100) {
  2126. $is_visible = false;
  2127. }
  2128. }
  2129. // Also check the time availability of the LP
  2130. if ($is_visible) {
  2131. // Adding visibility restrictions
  2132. if (!empty($row['publicated_on']) &&
  2133. $row['publicated_on'] != '0000-00-00 00:00:00'
  2134. ) {
  2135. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2136. //api_not_allowed();
  2137. $is_visible = false;
  2138. }
  2139. }
  2140. // Blocking empty start times see BT#2800
  2141. global $_custom;
  2142. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2143. $_custom['lps_hidden_when_no_start_date']
  2144. ) {
  2145. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2146. //api_not_allowed();
  2147. $is_visible = false;
  2148. }
  2149. }
  2150. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2151. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2152. //api_not_allowed();
  2153. $is_visible = false;
  2154. }
  2155. }
  2156. }
  2157. // Check if the subscription users/group to a LP is ON
  2158. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2159. // Try group
  2160. $is_visible = false;
  2161. // Checking only the user visibility
  2162. $userVisibility = api_get_item_visibility(
  2163. $courseInfo,
  2164. 'learnpath',
  2165. $row['id'],
  2166. $sessionId,
  2167. $student_id,
  2168. 'LearnpathSubscription'
  2169. );
  2170. if ($userVisibility == 1) {
  2171. $is_visible = true;
  2172. } else {
  2173. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2174. if (!empty($userGroups)) {
  2175. foreach ($userGroups as $groupInfo) {
  2176. $groupId = $groupInfo['iid'];
  2177. $userVisibility = api_get_item_visibility(
  2178. $courseInfo,
  2179. 'learnpath',
  2180. $row['id'],
  2181. $sessionId,
  2182. null,
  2183. 'LearnpathSubscription',
  2184. $groupId
  2185. );
  2186. if ($userVisibility == 1) {
  2187. $is_visible = true;
  2188. break;
  2189. }
  2190. }
  2191. }
  2192. }
  2193. }
  2194. return $is_visible;
  2195. }
  2196. return false;
  2197. }
  2198. /**
  2199. * @param int $lpId
  2200. * @param int $userId
  2201. * @param int $courseId
  2202. * @param int $sessionId
  2203. * @return int
  2204. */
  2205. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2206. {
  2207. $lpId = intval($lpId);
  2208. $userId = intval($userId);
  2209. $courseId = intval($courseId);
  2210. $sessionId = intval($sessionId);
  2211. $progress = 0;
  2212. $sessionCondition = api_get_session_condition($sessionId);
  2213. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2214. $sql = "SELECT * FROM $table
  2215. WHERE
  2216. c_id = ".$courseId." AND
  2217. lp_id = $lpId AND
  2218. user_id = $userId $sessionCondition";
  2219. $res = Database::query($sql);
  2220. if (Database :: num_rows($res) > 0) {
  2221. $row = Database:: fetch_array($res);
  2222. $progress = $row['progress'];
  2223. }
  2224. return $progress;
  2225. }
  2226. /**
  2227. * Displays a progress bar
  2228. * completed so far.
  2229. * @param integer $percentage Progress value to display
  2230. * @param string $text_add Text to display near the progress value
  2231. * @return string HTML string containing the progress bar
  2232. */
  2233. public static function get_progress_bar($percentage = -1, $text_add = '')
  2234. {
  2235. $text = $percentage . $text_add;
  2236. $output = '<div class="progress">
  2237. <div id="progress_bar_value" class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2238. '. $text .'
  2239. </div>
  2240. </div>';
  2241. return $output;
  2242. }
  2243. /**
  2244. * @param string $mode can be '%' or 'abs'
  2245. * otherwise this value will be used $this->progress_bar_mode
  2246. * @return string
  2247. */
  2248. public function getProgressBar($mode = null)
  2249. {
  2250. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2251. return self::get_progress_bar($percentage, $text_add);
  2252. }
  2253. /**
  2254. * Gets the progress bar info to display inside the progress bar.
  2255. * Also used by scorm_api.php
  2256. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2257. * we display a number of completed elements per total elements
  2258. * @param integer $add Additional steps to fake as completed
  2259. * @return list array Percentage or number and symbol (% or /xx)
  2260. */
  2261. public function get_progress_bar_text($mode = '', $add = 0)
  2262. {
  2263. if ($this->debug > 0) {
  2264. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2265. }
  2266. if (empty($mode)) {
  2267. $mode = $this->progress_bar_mode;
  2268. }
  2269. $total_items = $this->get_total_items_count_without_chapters();
  2270. if ($this->debug > 2) {
  2271. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2272. }
  2273. $completeItems = $this->get_complete_items_count();
  2274. if ($this->debug > 2) {
  2275. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2276. }
  2277. if ($add != 0) {
  2278. $completeItems += $add;
  2279. if ($this->debug > 2) {
  2280. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2281. }
  2282. }
  2283. $text = '';
  2284. if ($completeItems > $total_items) {
  2285. $completeItems = $total_items;
  2286. }
  2287. $percentage = 0;
  2288. if ($mode == '%') {
  2289. if ($total_items > 0) {
  2290. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2291. } else {
  2292. $percentage = 0;
  2293. }
  2294. $percentage = number_format($percentage, 0);
  2295. $text = '%';
  2296. } elseif ($mode == 'abs') {
  2297. $percentage = $completeItems;
  2298. $text = '/' . $total_items;
  2299. }
  2300. return array(
  2301. $percentage,
  2302. $text
  2303. );
  2304. }
  2305. /**
  2306. * Gets the progress bar mode
  2307. * @return string The progress bar mode attribute
  2308. */
  2309. public function get_progress_bar_mode()
  2310. {
  2311. if ($this->debug > 0) {
  2312. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2313. }
  2314. if (!empty ($this->progress_bar_mode)) {
  2315. return $this->progress_bar_mode;
  2316. } else {
  2317. return '%';
  2318. }
  2319. }
  2320. /**
  2321. * Gets the learnpath proximity (remote or local)
  2322. * @return string Learnpath proximity
  2323. */
  2324. public function get_proximity()
  2325. {
  2326. if ($this->debug > 0) {
  2327. error_log('New LP - In learnpath::get_proximity()', 0);
  2328. }
  2329. if (!empty ($this->proximity)) {
  2330. return $this->proximity;
  2331. } else {
  2332. return '';
  2333. }
  2334. }
  2335. /**
  2336. * Gets the learnpath theme (remote or local)
  2337. * @return string Learnpath theme
  2338. */
  2339. public function get_theme()
  2340. {
  2341. if ($this->debug > 0) {
  2342. error_log('New LP - In learnpath::get_theme()', 0);
  2343. }
  2344. if (!empty ($this->theme)) {
  2345. return $this->theme;
  2346. } else {
  2347. return '';
  2348. }
  2349. }
  2350. /**
  2351. * Gets the learnpath session id
  2352. * @return string Learnpath theme
  2353. */
  2354. public function get_lp_session_id()
  2355. {
  2356. if ($this->debug > 0) {
  2357. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2358. }
  2359. if (!empty ($this->lp_session_id)) {
  2360. return $this->lp_session_id;
  2361. } else {
  2362. return 0;
  2363. }
  2364. }
  2365. /**
  2366. * Gets the learnpath image
  2367. * @return string Web URL of the LP image
  2368. */
  2369. public function get_preview_image()
  2370. {
  2371. if ($this->debug > 0) {
  2372. error_log('New LP - In learnpath::get_preview_image()', 0);
  2373. }
  2374. if (!empty($this->preview_image)) {
  2375. return $this->preview_image;
  2376. } else {
  2377. return '';
  2378. }
  2379. }
  2380. /**
  2381. * @param string $size
  2382. * @param string $path_type
  2383. * @return bool|string
  2384. */
  2385. public function get_preview_image_path($size = null, $path_type = 'web')
  2386. {
  2387. $preview_image = $this->get_preview_image();
  2388. if (isset($preview_image) && !empty($preview_image)) {
  2389. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2390. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2391. if (isset($size)) {
  2392. $info = pathinfo($preview_image);
  2393. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2394. if (file_exists($image_sys_path.$image_custom_size)) {
  2395. if ($path_type == 'web') {
  2396. return $image_path.$image_custom_size;
  2397. } else {
  2398. return $image_sys_path.$image_custom_size;
  2399. }
  2400. }
  2401. } else {
  2402. if ($path_type == 'web') {
  2403. return $image_path.$preview_image;
  2404. } else {
  2405. return $image_sys_path.$preview_image;
  2406. }
  2407. }
  2408. }
  2409. return false;
  2410. }
  2411. /**
  2412. * Gets the learnpath author
  2413. * @return string LP's author
  2414. */
  2415. public function get_author()
  2416. {
  2417. if ($this->debug > 0) {
  2418. error_log('New LP - In learnpath::get_author()', 0);
  2419. }
  2420. if (!empty ($this->author)) {
  2421. return $this->author;
  2422. } else {
  2423. return '';
  2424. }
  2425. }
  2426. /**
  2427. * Gets the learnpath author
  2428. * @return string LP's author
  2429. */
  2430. public function get_hide_toc_frame()
  2431. {
  2432. if ($this->debug > 0) {
  2433. error_log('New LP - In learnpath::get_author()', 0);
  2434. }
  2435. if (!empty ($this->hide_toc_frame)) {
  2436. return $this->hide_toc_frame;
  2437. } else {
  2438. return '';
  2439. }
  2440. }
  2441. /**
  2442. * Generate a new prerequisites string for a given item. If this item was a sco and
  2443. * its prerequisites were strings (instead of IDs), then transform those strings into
  2444. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2445. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2446. * same rule as the scorm_export() method
  2447. * @param integer Item ID
  2448. * @return string Prerequisites string ready for the export as SCORM
  2449. */
  2450. public function get_scorm_prereq_string($item_id)
  2451. {
  2452. if ($this->debug > 0) {
  2453. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2454. }
  2455. if (!is_object($this->items[$item_id])) {
  2456. return false;
  2457. }
  2458. /** @var learnpathItem $oItem */
  2459. $oItem = $this->items[$item_id];
  2460. $prereq = $oItem->get_prereq_string();
  2461. if (empty($prereq)) {
  2462. return '';
  2463. }
  2464. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2465. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2466. // then simply return it (with the ITEM_ prefix).
  2467. //return 'ITEM_' . $prereq;
  2468. return $this->items[$prereq]->ref;
  2469. } else {
  2470. if (isset($this->refs_list[$prereq])) {
  2471. // It's a simple string item from which the ID can be found in the refs list,
  2472. // so we can transform it directly to an ID for export.
  2473. return $this->items[$this->refs_list[$prereq]]->ref;
  2474. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2475. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2476. } else {
  2477. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2478. // and replace them, one by one, by the internal IDs (chamilo db)
  2479. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2480. // by a space as well.
  2481. $find = array (
  2482. '&',
  2483. '|',
  2484. '~',
  2485. '=',
  2486. '<>',
  2487. '{',
  2488. '}',
  2489. '*',
  2490. '(',
  2491. ')'
  2492. );
  2493. $replace = array (
  2494. ' ',
  2495. ' ',
  2496. ' ',
  2497. ' ',
  2498. ' ',
  2499. ' ',
  2500. ' ',
  2501. ' ',
  2502. ' ',
  2503. ' '
  2504. );
  2505. $prereq_mod = str_replace($find, $replace, $prereq);
  2506. $ids = explode(' ', $prereq_mod);
  2507. foreach ($ids as $id) {
  2508. $id = trim($id);
  2509. if (isset ($this->refs_list[$id])) {
  2510. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2511. }
  2512. }
  2513. return $prereq;
  2514. }
  2515. }
  2516. }
  2517. /**
  2518. * Returns the XML DOM document's node
  2519. * @param resource Reference to a list of objects to search for the given ITEM_*
  2520. * @param string The identifier to look for
  2521. * @return mixed The reference to the element found with that identifier. False if not found
  2522. */
  2523. public function get_scorm_xml_node(& $children, $id)
  2524. {
  2525. for ($i = 0; $i < $children->length; $i++) {
  2526. $item_temp = $children->item($i);
  2527. if ($item_temp->nodeName == 'item') {
  2528. if ($item_temp->getAttribute('identifier') == $id) {
  2529. return $item_temp;
  2530. }
  2531. }
  2532. $subchildren = $item_temp->childNodes;
  2533. if ($subchildren->length > 0) {
  2534. $val = $this->get_scorm_xml_node($subchildren, $id);
  2535. if (is_object($val)) {
  2536. return $val;
  2537. }
  2538. }
  2539. }
  2540. return false;
  2541. }
  2542. /**
  2543. * Returns a usable array of stats related to the current learnpath and user
  2544. * @return array Well-formatted array containing status for the current learnpath
  2545. */
  2546. public function get_stats()
  2547. {
  2548. if ($this->debug > 0) {
  2549. error_log('New LP - In learnpath::get_stats()', 0);
  2550. }
  2551. }
  2552. /**
  2553. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2554. * the given course (for all learnpaths and all users)
  2555. * @param string Course code
  2556. * @return array Well-formatted array containing status for the course's learnpaths
  2557. */
  2558. public function get_stats_course($course)
  2559. {
  2560. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2561. // TODO
  2562. }
  2563. /**
  2564. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2565. * the given course and learnpath (for all users)
  2566. * @param string Course code
  2567. * @param integer Learnpath ID
  2568. * @return array Well-formatted array containing status for the specified learnpath
  2569. */
  2570. public function get_stats_lp($course, $lp)
  2571. {
  2572. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2573. // TODO
  2574. }
  2575. /**
  2576. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2577. * the given course, learnpath and user.
  2578. * @param string Course code
  2579. * @param integer Learnpath ID
  2580. * @param integer User ID
  2581. * @return array Well-formatted array containing status for the specified learnpath and user
  2582. */
  2583. public function get_stats_lp_user($course, $lp, $user)
  2584. {
  2585. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2586. // TODO
  2587. }
  2588. /**
  2589. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2590. * the given course and learnpath (for all users)
  2591. * @param string Course code
  2592. * @param integer User ID
  2593. * @return array Well-formatted array containing status for the user's learnpaths
  2594. */
  2595. public function get_stats_user($course, $user) {
  2596. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2597. // TODO
  2598. }
  2599. /**
  2600. * Gets the status list for all LP's items
  2601. * @return array Array of [index] => [item ID => current status]
  2602. */
  2603. public function get_items_status_list()
  2604. {
  2605. if ($this->debug > 0) {
  2606. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2607. }
  2608. $list = array ();
  2609. foreach ($this->ordered_items as $item_id) {
  2610. $list[] = array (
  2611. $item_id => $this->items[$item_id]->get_status()
  2612. );
  2613. }
  2614. return $list;
  2615. }
  2616. /**
  2617. * Return the number of interactions for the given learnpath Item View ID.
  2618. * This method can be used as static.
  2619. * @param integer Item View ID
  2620. * @param integer course id
  2621. * @return integer Number of interactions
  2622. */
  2623. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2624. {
  2625. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2626. $lp_iv_id = intval($lp_iv_id);
  2627. $course_id = intval($course_id);
  2628. $sql = "SELECT count(*) FROM $table
  2629. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2630. $res = Database::query($sql);
  2631. $num = 0;
  2632. if (Database::num_rows($res)) {
  2633. $row = Database::fetch_array($res);
  2634. $num = $row[0];
  2635. }
  2636. return $num;
  2637. }
  2638. /**
  2639. * Return the interactions as an array for the given lp_iv_id.
  2640. * This method can be used as static.
  2641. * @param integer Learnpath Item View ID
  2642. * @return array
  2643. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2644. */
  2645. public static function get_iv_interactions_array($lp_iv_id)
  2646. {
  2647. $course_id = api_get_course_int_id();
  2648. $list = array();
  2649. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2650. if (empty($lp_iv_id)) {
  2651. return array();
  2652. }
  2653. $sql = "SELECT * FROM $table
  2654. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2655. ORDER BY order_id ASC";
  2656. $res = Database::query($sql);
  2657. $num = Database :: num_rows($res);
  2658. if ($num > 0) {
  2659. $list[] = array (
  2660. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2661. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2662. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2663. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2664. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2665. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2666. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2667. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2668. );
  2669. while ($row = Database :: fetch_array($res)) {
  2670. $list[] = array (
  2671. 'order_id' => ($row['order_id'] + 1),
  2672. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2673. 'type' => $row['interaction_type'],
  2674. 'time' => $row['completion_time'],
  2675. //'correct_responses' => $row['correct_responses'],
  2676. 'correct_responses' => '', // Hide correct responses from students.
  2677. 'student_response' => $row['student_response'],
  2678. 'result' => $row['result'],
  2679. 'latency' => $row['latency']
  2680. );
  2681. }
  2682. }
  2683. return $list;
  2684. }
  2685. /**
  2686. * Return the number of objectives for the given learnpath Item View ID.
  2687. * This method can be used as static.
  2688. * @param integer Item View ID
  2689. * @return integer Number of objectives
  2690. */
  2691. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2692. {
  2693. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2694. $course_id = intval($course_id);
  2695. $lp_iv_id = intval($lp_iv_id);
  2696. $sql = "SELECT count(*) FROM $table
  2697. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2698. //@todo seems that this always returns 0
  2699. $res = Database::query($sql);
  2700. $num = 0;
  2701. if (Database::num_rows($res)) {
  2702. $row = Database :: fetch_array($res);
  2703. $num = $row[0];
  2704. }
  2705. return $num;
  2706. }
  2707. /**
  2708. * Return the objectives as an array for the given lp_iv_id.
  2709. * This method can be used as static.
  2710. * @param integer Learnpath Item View ID
  2711. * @return array
  2712. * @todo Translate labels
  2713. */
  2714. public static function get_iv_objectives_array($lp_iv_id = 0)
  2715. {
  2716. $course_id = api_get_course_int_id();
  2717. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2718. $sql = "SELECT * FROM $table
  2719. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2720. ORDER BY order_id ASC";
  2721. $res = Database::query($sql);
  2722. $num = Database :: num_rows($res);
  2723. $list = array();
  2724. if ($num > 0) {
  2725. $list[] = array(
  2726. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2727. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2728. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2729. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2730. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2731. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2732. );
  2733. while ($row = Database :: fetch_array($res)) {
  2734. $list[] = array (
  2735. 'order_id' => ($row['order_id'] + 1),
  2736. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2737. 'score_raw' => $row['score_raw'],
  2738. 'score_max' => $row['score_max'],
  2739. 'score_min' => $row['score_min'],
  2740. 'status' => $row['status']
  2741. );
  2742. }
  2743. }
  2744. return $list;
  2745. }
  2746. /**
  2747. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2748. * used by get_html_toc() to be ready to display
  2749. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2750. */
  2751. public function get_toc()
  2752. {
  2753. if ($this->debug > 0) {
  2754. error_log('learnpath::get_toc()', 0);
  2755. }
  2756. $toc = array();
  2757. foreach ($this->ordered_items as $item_id) {
  2758. if ($this->debug > 2) {
  2759. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2760. }
  2761. // TODO: Change this link generation and use new function instead.
  2762. $toc[] = array (
  2763. 'id' => $item_id,
  2764. 'title' => $this->items[$item_id]->get_title(),
  2765. 'status' => $this->items[$item_id]->get_status(),
  2766. 'level' => $this->items[$item_id]->get_level(),
  2767. 'type' => $this->items[$item_id]->get_type(),
  2768. 'description' => $this->items[$item_id]->get_description(),
  2769. 'path' => $this->items[$item_id]->get_path(),
  2770. );
  2771. }
  2772. if ($this->debug > 2) {
  2773. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2774. }
  2775. return $toc;
  2776. }
  2777. /**
  2778. * Generate and return the table of contents for this learnpath. The JS
  2779. * table returned is used inside of scorm_api.php
  2780. * @return string A JS array vairiable construction
  2781. */
  2782. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2783. {
  2784. if ($this->debug > 0) {
  2785. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2786. }
  2787. $toc = $varname.' = new Array();';
  2788. foreach ($this->ordered_items as $item_id) {
  2789. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2790. }
  2791. if ($this->debug > 2) {
  2792. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2793. }
  2794. return $toc;
  2795. }
  2796. /**
  2797. * Gets the learning path type
  2798. * @param boolean Return the name? If false, return the ID. Default is false.
  2799. * @return mixed Type ID or name, depending on the parameter
  2800. */
  2801. public function get_type($get_name = false)
  2802. {
  2803. $res = false;
  2804. if ($this->debug > 0) {
  2805. error_log('New LP - In learnpath::get_type()', 0);
  2806. }
  2807. if (!empty ($this->type)) {
  2808. if ($get_name) {
  2809. // Get it from the lp_type table in main db.
  2810. } else {
  2811. $res = $this->type;
  2812. }
  2813. }
  2814. if ($this->debug > 2) {
  2815. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2816. }
  2817. return $res;
  2818. }
  2819. /**
  2820. * Gets the learning path type as static method
  2821. * @param boolean Return the name? If false, return the ID. Default is false.
  2822. * @return mixed Type ID or name, depending on the parameter
  2823. */
  2824. public static function get_type_static($lp_id = 0)
  2825. {
  2826. $course_id = api_get_course_int_id();
  2827. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2828. $lp_id = intval($lp_id);
  2829. $sql = "SELECT lp_type FROM $tbl_lp
  2830. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2831. $res = Database::query($sql);
  2832. if ($res === false) {
  2833. return null;
  2834. }
  2835. if (Database :: num_rows($res) <= 0) {
  2836. return null;
  2837. }
  2838. $row = Database :: fetch_array($res);
  2839. return $row['lp_type'];
  2840. }
  2841. /**
  2842. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2843. * This method can be used as abstract and is recursive
  2844. * @param integer Learnpath ID
  2845. * @param integer Parent ID of the items to look for
  2846. * @return mixed Ordered list of item IDs or false on error
  2847. */
  2848. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2849. {
  2850. if (empty($course_id)) {
  2851. $course_id = api_get_course_int_id();
  2852. } else {
  2853. $course_id = intval($course_id);
  2854. }
  2855. $list = array();
  2856. if (empty($lp)) {
  2857. return false;
  2858. }
  2859. $lp = intval($lp);
  2860. $parent = intval($parent);
  2861. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2862. $sql = "SELECT id FROM $tbl_lp_item
  2863. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2864. ORDER BY display_order";
  2865. $res = Database::query($sql);
  2866. while ($row = Database :: fetch_array($res)) {
  2867. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2868. $list[] = $row['id'];
  2869. foreach ($sublist as $item) {
  2870. $list[] = $item;
  2871. }
  2872. }
  2873. return $list;
  2874. }
  2875. /**
  2876. * @return array
  2877. */
  2878. public static function getChapterTypes()
  2879. {
  2880. return array(
  2881. 'dokeos_chapter',
  2882. 'dokeos_module',
  2883. 'chapter',
  2884. 'dir'
  2885. );
  2886. }
  2887. /**
  2888. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2889. * @return string HTML TOC ready to display
  2890. */
  2891. public function get_html_toc($toc_list = null)
  2892. {
  2893. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2894. if ($this->debug > 0) {
  2895. error_log('In learnpath::get_html_toc()', 0);
  2896. }
  2897. if (empty($toc_list)) {
  2898. $toc_list = $this->get_toc();
  2899. }
  2900. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2901. $html = '<div class="scorm-body">';
  2902. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2903. require_once 'resourcelinker.inc.php';
  2904. // Temporary variables.
  2905. $mycurrentitemid = $this->get_current_item_id();
  2906. $color_counter = 0;
  2907. $i = 0;
  2908. foreach ($toc_list as $item) {
  2909. // TODO: Complete this
  2910. $icon_name = array (
  2911. 'not attempted' => '../img/notattempted.gif',
  2912. 'incomplete' => '../img/incomplete.png',
  2913. 'failed' => '../img/delete.png',
  2914. 'completed' => '../img/completed.png',
  2915. 'passed' => '../img/passed.png',
  2916. 'succeeded' => '../img/succeeded.png',
  2917. 'browsed' => '../img/completed.png',
  2918. );
  2919. // Style Status
  2920. $class_name = array (
  2921. 'not attempted' => 'scorm_not_attempted',
  2922. 'incomplete' => 'scorm_not_attempted',
  2923. 'failed' => 'scorm_failed',
  2924. 'completed' => 'scorm_completed',
  2925. 'passed' => 'scorm_completed',
  2926. 'succeeded' => 'scorm_completed',
  2927. 'browsed' => 'scorm_completed',
  2928. );
  2929. $scorm_color_background = 'row_odd';
  2930. $style_item = '';
  2931. if ($color_counter % 2 == 0) {
  2932. $scorm_color_background = 'row_even';
  2933. }
  2934. $dirTypes = self::getChapterTypes();
  2935. if (in_array($item['type'], $dirTypes)) {
  2936. $scorm_color_background ='scorm_item_section ';
  2937. $style_item = '';
  2938. }
  2939. if ($item['id'] == $this->current) {
  2940. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2941. } elseif (!in_array($item['type'], $dirTypes)) {
  2942. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2943. }
  2944. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2945. // Learning path title
  2946. $title = $item['title'];
  2947. if (empty ($title)) {
  2948. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2949. }
  2950. $title = Security::remove_XSS($title);
  2951. // Learning path personalization
  2952. // build the LP tree
  2953. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2954. $description = $item['description'];
  2955. if (empty($description)) {
  2956. $description = $title;
  2957. }
  2958. if (in_array($item['type'], $dirTypes)) {
  2959. // Chapters
  2960. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2961. } else {
  2962. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2963. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2964. }
  2965. if (in_array($item['type'], $dirTypes)) {
  2966. // Chapter
  2967. // if you want to put an image before, you should use css
  2968. $html .= stripslashes($title);
  2969. } else {
  2970. $this->get_link('http', $item['id'], $toc_list);
  2971. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2972. }
  2973. $html .= "</div>";
  2974. if ($scorm_color_background != '') {
  2975. $html .= '</div>';
  2976. }
  2977. $color_counter++;
  2978. }
  2979. $html .= "</div>";
  2980. $html .= "</div>";
  2981. return $html;
  2982. }
  2983. /**
  2984. * Returns an HTML-formatted string ready to display with teacher buttons
  2985. * in LP view menu
  2986. * @return string HTML TOC ready to display
  2987. */
  2988. public function get_teacher_toc_buttons()
  2989. {
  2990. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2991. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2992. $html = '';
  2993. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2994. $gradebook = '';
  2995. if (!empty($_GET['gradebook'])) {
  2996. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2997. }
  2998. if ($this->get_lp_session_id() == api_get_session_id()) {
  2999. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3000. $html .= '<div class="btn-group">';
  3001. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  3002. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  3003. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  3004. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  3005. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'&isStudentView=false">' .
  3006. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  3007. $html .= '</div>';
  3008. $html .= '</div>';
  3009. }
  3010. }
  3011. return $html;
  3012. }
  3013. /**
  3014. * Gets the learnpath maker name - generally the editor's name
  3015. * @return string Learnpath maker name
  3016. */
  3017. public function get_maker()
  3018. {
  3019. if ($this->debug > 0) {
  3020. error_log('New LP - In learnpath::get_maker()', 0);
  3021. }
  3022. if (!empty ($this->maker)) {
  3023. return $this->maker;
  3024. } else {
  3025. return '';
  3026. }
  3027. }
  3028. /**
  3029. * Gets the learnpath name/title
  3030. * @return string Learnpath name/title
  3031. */
  3032. public function get_name()
  3033. {
  3034. if ($this->debug > 0) {
  3035. error_log('New LP - In learnpath::get_name()', 0);
  3036. }
  3037. if (!empty ($this->name)) {
  3038. return $this->name;
  3039. } else {
  3040. return 'N/A';
  3041. }
  3042. }
  3043. /**
  3044. * Gets a link to the resource from the present location, depending on item ID.
  3045. * @param string $type Type of link expected
  3046. * @param integer $item_id Learnpath item ID
  3047. * @return string $provided_toc Link to the lp_item resource
  3048. */
  3049. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3050. {
  3051. $course_id = $this->get_course_int_id();
  3052. if ($this->debug > 0) {
  3053. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3054. }
  3055. if (empty($item_id)) {
  3056. if ($this->debug > 2) {
  3057. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3058. }
  3059. $item_id = $this->get_current_item_id();
  3060. }
  3061. if (empty($item_id)) {
  3062. if ($this->debug > 2) {
  3063. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3064. }
  3065. //still empty, this means there was no item_id given and we are not in an object context or
  3066. //the object property is empty, return empty link
  3067. $item_id = $this->first();
  3068. return '';
  3069. }
  3070. $file = '';
  3071. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3072. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3073. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3074. $item_id = intval($item_id);
  3075. $sql = "SELECT
  3076. l.lp_type as ltype,
  3077. l.path as lpath,
  3078. li.item_type as litype,
  3079. li.path as lipath,
  3080. li.parameters as liparams
  3081. FROM $lp_table l
  3082. INNER JOIN $lp_item_table li
  3083. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3084. WHERE li.id = $item_id ";
  3085. if ($this->debug > 2) {
  3086. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3087. }
  3088. $res = Database::query($sql);
  3089. if (Database :: num_rows($res) > 0) {
  3090. $row = Database :: fetch_array($res);
  3091. $lp_type = $row['ltype'];
  3092. $lp_path = $row['lpath'];
  3093. $lp_item_type = $row['litype'];
  3094. $lp_item_path = $row['lipath'];
  3095. $lp_item_params = $row['liparams'];
  3096. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3097. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3098. }
  3099. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3100. if ($type == 'http') {
  3101. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3102. } else {
  3103. $course_path = $sys_course_path; //system path
  3104. }
  3105. // 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.
  3106. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3107. $lp_type = 1;
  3108. }
  3109. if ($this->debug > 2) {
  3110. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3111. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3112. }
  3113. // Now go through the specific cases to get the end of the path
  3114. // @todo Use constants instead of int values.
  3115. switch ($lp_type) {
  3116. case 1 :
  3117. if ($lp_item_type == 'dokeos_chapter') {
  3118. $file = 'lp_content.php?type=dir';
  3119. } else {
  3120. require_once 'resourcelinker.inc.php';
  3121. $file = rl_get_resource_link_for_learnpath(
  3122. $course_id,
  3123. $this->get_id(),
  3124. $item_id,
  3125. $this->get_view_id()
  3126. );
  3127. if ($this->debug > 0) {
  3128. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3129. }
  3130. if ($lp_item_type == 'link') {
  3131. if (Link::is_youtube_link($file)) {
  3132. $src = Link::get_youtube_video_id($file);
  3133. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3134. } elseif (Link::isVimeoLink($file)) {
  3135. $src = Link::getVimeoLinkId($file);
  3136. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3137. } else {
  3138. // If the current site is HTTPS and the link is
  3139. // HTTP, browsers will refuse opening the link
  3140. $urlId = api_get_current_access_url_id();
  3141. $url = api_get_access_url($urlId, false);
  3142. $protocol = substr($url['url'], 0, 5);
  3143. if ($protocol === 'https') {
  3144. $linkProtocol = substr($file, 0, 5);
  3145. if ($linkProtocol === 'http:') {
  3146. //this is the special intervention case
  3147. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&source=' . urlencode($file);
  3148. }
  3149. }
  3150. }
  3151. } else {
  3152. // Check how much attempts of a exercise exits in lp
  3153. $lp_item_id = $this->get_current_item_id();
  3154. $lp_view_id = $this->get_view_id();
  3155. $prevent_reinit = null;
  3156. if (isset($this->items[$this->current])) {
  3157. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3158. }
  3159. if (empty($provided_toc)) {
  3160. if ($this->debug > 0) {
  3161. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3162. }
  3163. $list = $this->get_toc();
  3164. } else {
  3165. if ($this->debug > 0) {
  3166. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3167. }
  3168. $list = $provided_toc;
  3169. }
  3170. $type_quiz = false;
  3171. foreach ($list as $toc) {
  3172. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3173. $type_quiz = true;
  3174. }
  3175. }
  3176. if ($type_quiz) {
  3177. $lp_item_id = intval($lp_item_id);
  3178. $lp_view_id = intval($lp_view_id);
  3179. $sql = "SELECT count(*) FROM $lp_item_view_table
  3180. WHERE
  3181. c_id = $course_id AND
  3182. lp_item_id='" . $lp_item_id . "' AND
  3183. lp_view_id ='" . $lp_view_id . "' AND
  3184. status='completed'";
  3185. $result = Database::query($sql);
  3186. $row_count = Database :: fetch_row($result);
  3187. $count_item_view = (int) $row_count[0];
  3188. $not_multiple_attempt = 0;
  3189. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3190. $not_multiple_attempt = 1;
  3191. }
  3192. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3193. }
  3194. $tmp_array = explode('/', $file);
  3195. $document_name = $tmp_array[count($tmp_array) - 1];
  3196. if (strpos($document_name, '_DELETED_')) {
  3197. $file = 'blank.php?error=document_deleted';
  3198. }
  3199. }
  3200. }
  3201. break;
  3202. case 2 :
  3203. if ($this->debug > 2) {
  3204. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3205. }
  3206. if ($lp_item_type != 'dir') {
  3207. // Quite complex here:
  3208. // We want to make sure 'http://' (and similar) links can
  3209. // be loaded as is (withouth the Chamilo path in front) but
  3210. // some contents use this form: resource.htm?resource=http://blablabla
  3211. // which means we have to find a protocol at the path's start, otherwise
  3212. // it should not be considered as an external URL.
  3213. //if ($this->prerequisites_match($item_id)) {
  3214. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3215. if ($this->debug > 2) {
  3216. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3217. }
  3218. // Distant url, return as is.
  3219. $file = $lp_item_path;
  3220. } else {
  3221. if ($this->debug > 2) {
  3222. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3223. }
  3224. // Prevent getting untranslatable urls.
  3225. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3226. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3227. // Prepare the path.
  3228. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3229. // TODO: Fix this for urls with protocol header.
  3230. $file = str_replace('//', '/', $file);
  3231. $file = str_replace(':/', '://', $file);
  3232. if (substr($lp_path, -1) == '/') {
  3233. $lp_path = substr($lp_path, 0, -1);
  3234. }
  3235. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3236. // if file not found.
  3237. $decoded = html_entity_decode($lp_item_path);
  3238. list ($decoded) = explode('?', $decoded);
  3239. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3240. require_once 'resourcelinker.inc.php';
  3241. $file = rl_get_resource_link_for_learnpath(
  3242. $course_id,
  3243. $this->get_id(),
  3244. $item_id,
  3245. $this->get_view_id()
  3246. );
  3247. if (empty($file)) {
  3248. $file = 'blank.php?error=document_not_found';
  3249. } else {
  3250. $tmp_array = explode('/', $file);
  3251. $document_name = $tmp_array[count($tmp_array) - 1];
  3252. if (strpos($document_name, '_DELETED_')) {
  3253. $file = 'blank.php?error=document_deleted';
  3254. } else {
  3255. $file = 'blank.php?error=document_not_found';
  3256. }
  3257. }
  3258. } else {
  3259. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3260. }
  3261. }
  3262. }
  3263. // We want to use parameters if they were defined in the imsmanifest
  3264. if (strpos($file, 'blank.php') === false) {
  3265. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3266. }
  3267. } else {
  3268. $file = 'lp_content.php?type=dir';
  3269. }
  3270. break;
  3271. case 3 :
  3272. if ($this->debug > 2) {
  3273. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3274. }
  3275. // Formatting AICC HACP append URL.
  3276. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3277. if (!empty($lp_item_params)) {
  3278. $aicc_append .= $lp_item_params . '&';
  3279. }
  3280. if ($lp_item_type != 'dir') {
  3281. // Quite complex here:
  3282. // We want to make sure 'http://' (and similar) links can
  3283. // be loaded as is (withouth the Chamilo path in front) but
  3284. // some contents use this form: resource.htm?resource=http://blablabla
  3285. // which means we have to find a protocol at the path's start, otherwise
  3286. // it should not be considered as an external URL.
  3287. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3288. if ($this->debug > 2) {
  3289. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3290. }
  3291. // Distant url, return as is.
  3292. $file = $lp_item_path;
  3293. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3294. /*
  3295. if (stristr($file,'<servername>') !== false) {
  3296. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3297. }
  3298. */
  3299. if (stripos($file, '<servername>') !== false) {
  3300. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3301. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3302. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3303. }
  3304. //
  3305. $file .= $aicc_append;
  3306. } else {
  3307. if ($this->debug > 2) {
  3308. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3309. }
  3310. // Prevent getting untranslatable urls.
  3311. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3312. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3313. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3314. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3315. // TODO: Fix this for urls with protocol header.
  3316. $file = str_replace('//', '/', $file);
  3317. $file = str_replace(':/', '://', $file);
  3318. $file .= $aicc_append;
  3319. }
  3320. } else {
  3321. $file = 'lp_content.php?type=dir';
  3322. }
  3323. break;
  3324. case 4 :
  3325. break;
  3326. default :
  3327. break;
  3328. }
  3329. // Replace &amp; by & because &amp; will break URL with params
  3330. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3331. }
  3332. if ($this->debug > 2) {
  3333. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3334. }
  3335. return $file;
  3336. }
  3337. /**
  3338. * Gets the latest usable view or generate a new one
  3339. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3340. * @return integer DB lp_view id
  3341. */
  3342. public function get_view($attempt_num = 0)
  3343. {
  3344. if ($this->debug > 0) {
  3345. error_log('New LP - In learnpath::get_view()', 0);
  3346. }
  3347. $search = '';
  3348. // Use $attempt_num to enable multi-views management (disabled so far).
  3349. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3350. $search = 'AND view_count = ' . $attempt_num;
  3351. }
  3352. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3353. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3354. $course_id = api_get_course_int_id();
  3355. $sessionId = api_get_session_id();
  3356. $sql = "SELECT id, view_count FROM $lp_view_table
  3357. WHERE
  3358. c_id = " . $course_id . " AND
  3359. lp_id = " . $this->get_id() . " AND
  3360. user_id = " . $this->get_user_id() . " AND
  3361. session_id = $sessionId
  3362. $search
  3363. ORDER BY view_count DESC";
  3364. $res = Database::query($sql);
  3365. if (Database :: num_rows($res) > 0) {
  3366. $row = Database :: fetch_array($res);
  3367. $this->lp_view_id = $row['id'];
  3368. } else if (!api_is_invitee()) {
  3369. // There is no database record, create one.
  3370. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3371. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3372. Database::query($sql);
  3373. $id = Database :: insert_id();
  3374. $this->lp_view_id = $id;
  3375. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3376. Database::query($sql);
  3377. }
  3378. return $this->lp_view_id;
  3379. }
  3380. /**
  3381. * Gets the current view id
  3382. * @return integer View ID (from lp_view)
  3383. */
  3384. public function get_view_id()
  3385. {
  3386. if ($this->debug > 0) {
  3387. error_log('New LP - In learnpath::get_view_id()', 0);
  3388. }
  3389. if (!empty ($this->lp_view_id)) {
  3390. return $this->lp_view_id;
  3391. } else {
  3392. return 0;
  3393. }
  3394. }
  3395. /**
  3396. * Gets the update queue
  3397. * @return array Array containing IDs of items to be updated by JavaScript
  3398. */
  3399. public function get_update_queue()
  3400. {
  3401. if ($this->debug > 0) {
  3402. error_log('New LP - In learnpath::get_update_queue()', 0);
  3403. }
  3404. return $this->update_queue;
  3405. }
  3406. /**
  3407. * Gets the user ID
  3408. * @return integer User ID
  3409. */
  3410. public function get_user_id()
  3411. {
  3412. if ($this->debug > 0) {
  3413. error_log('New LP - In learnpath::get_user_id()', 0);
  3414. }
  3415. if (!empty ($this->user_id)) {
  3416. return $this->user_id;
  3417. } else {
  3418. return false;
  3419. }
  3420. }
  3421. /**
  3422. * Checks if any of the items has an audio element attached
  3423. * @return bool True or false
  3424. */
  3425. public function has_audio()
  3426. {
  3427. if ($this->debug > 1) {
  3428. error_log('New LP - In learnpath::has_audio()', 0);
  3429. }
  3430. $has = false;
  3431. foreach ($this->items as $i => $item) {
  3432. if (!empty ($this->items[$i]->audio)) {
  3433. $has = true;
  3434. break;
  3435. }
  3436. }
  3437. return $has;
  3438. }
  3439. /**
  3440. * Logs a message into a file
  3441. * @param string Message to log
  3442. * @return boolean True on success, false on error or if msg empty
  3443. */
  3444. public function log($msg)
  3445. {
  3446. if ($this->debug > 0) {
  3447. error_log('New LP - In learnpath::log()', 0);
  3448. }
  3449. // TODO
  3450. $this->error .= $msg;
  3451. return true;
  3452. }
  3453. /**
  3454. * Moves an item up and down at its level
  3455. * @param integer Item to move up and down
  3456. * @param string Direction 'up' or 'down'
  3457. * @return integer New display order, or false on error
  3458. */
  3459. public function move_item($id, $direction)
  3460. {
  3461. $course_id = api_get_course_int_id();
  3462. if ($this->debug > 0) {
  3463. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3464. }
  3465. if (empty($id) || empty($direction)) {
  3466. return false;
  3467. }
  3468. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3469. $sql_sel = "SELECT *
  3470. FROM " . $tbl_lp_item . "
  3471. WHERE c_id = ".$course_id." AND id = " . $id;
  3472. $res_sel = Database::query($sql_sel);
  3473. // Check if elem exists.
  3474. if (Database :: num_rows($res_sel) < 1) {
  3475. return false;
  3476. }
  3477. // Gather data.
  3478. $row = Database :: fetch_array($res_sel);
  3479. $previous = $row['previous_item_id'];
  3480. $next = $row['next_item_id'];
  3481. $display = $row['display_order'];
  3482. $parent = $row['parent_item_id'];
  3483. $lp = $row['lp_id'];
  3484. // Update the item (switch with previous/next one).
  3485. switch ($direction) {
  3486. case 'up':
  3487. if ($this->debug > 2) {
  3488. error_log('Movement up detected', 0);
  3489. }
  3490. if ($display <= 1) { /*do nothing*/
  3491. } else {
  3492. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3493. WHERE c_id = ".$course_id." AND id = $previous";
  3494. if ($this->debug > 2) {
  3495. error_log('Selecting previous: ' . $sql_sel2, 0);
  3496. }
  3497. $res_sel2 = Database::query($sql_sel2);
  3498. if (Database :: num_rows($res_sel2) < 1) {
  3499. $previous_previous = 0;
  3500. }
  3501. // Gather data.
  3502. $row2 = Database :: fetch_array($res_sel2);
  3503. $previous_previous = $row2['previous_item_id'];
  3504. // Update previous_previous item (switch "next" with current).
  3505. if ($previous_previous != 0) {
  3506. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3507. next_item_id = $id
  3508. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3509. if ($this->debug > 2) {
  3510. error_log($sql_upd2, 0);
  3511. }
  3512. Database::query($sql_upd2);
  3513. }
  3514. // Update previous item (switch with current).
  3515. if ($previous != 0) {
  3516. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3517. next_item_id = $next,
  3518. previous_item_id = $id,
  3519. display_order = display_order +1
  3520. WHERE c_id = ".$course_id." AND id = $previous";
  3521. if ($this->debug > 2) {
  3522. error_log($sql_upd2, 0);
  3523. }
  3524. Database::query($sql_upd2);
  3525. }
  3526. // Update current item (switch with previous).
  3527. if ($id != 0) {
  3528. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3529. next_item_id = $previous,
  3530. previous_item_id = $previous_previous,
  3531. display_order = display_order-1
  3532. WHERE c_id = ".$course_id." AND id = $id";
  3533. if ($this->debug > 2) {
  3534. error_log($sql_upd2, 0);
  3535. }
  3536. Database::query($sql_upd2);
  3537. }
  3538. // Update next item (new previous item).
  3539. if ($next != 0) {
  3540. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3541. WHERE c_id = ".$course_id." AND id = $next";
  3542. if ($this->debug > 2) {
  3543. error_log($sql_upd2, 0);
  3544. }
  3545. Database::query($sql_upd2);
  3546. }
  3547. $display = $display -1;
  3548. }
  3549. break;
  3550. case 'down':
  3551. if ($this->debug > 2) {
  3552. error_log('Movement down detected', 0);
  3553. }
  3554. if ($next == 0) { /* Do nothing. */
  3555. } else {
  3556. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3557. if ($this->debug > 2) {
  3558. error_log('Selecting next: ' . $sql_sel2, 0);
  3559. }
  3560. $res_sel2 = Database::query($sql_sel2);
  3561. if (Database :: num_rows($res_sel2) < 1) {
  3562. $next_next = 0;
  3563. }
  3564. // Gather data.
  3565. $row2 = Database :: fetch_array($res_sel2);
  3566. $next_next = $row2['next_item_id'];
  3567. // Update previous item (switch with current).
  3568. if ($previous != 0) {
  3569. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3570. WHERE c_id = ".$course_id." AND id = $previous";
  3571. Database::query($sql_upd2);
  3572. }
  3573. // Update current item (switch with previous).
  3574. if ($id != 0) {
  3575. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3576. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3577. WHERE c_id = ".$course_id." AND id = $id";
  3578. Database::query($sql_upd2);
  3579. }
  3580. // Update next item (new previous item).
  3581. if ($next != 0) {
  3582. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3583. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3584. WHERE c_id = ".$course_id." AND id = $next";
  3585. Database::query($sql_upd2);
  3586. }
  3587. // Update next_next item (switch "previous" with current).
  3588. if ($next_next != 0) {
  3589. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3590. previous_item_id = $id
  3591. WHERE c_id = ".$course_id." AND id = $next_next";
  3592. Database::query($sql_upd2);
  3593. }
  3594. $display = $display +1;
  3595. }
  3596. break;
  3597. default :
  3598. return false;
  3599. }
  3600. return $display;
  3601. }
  3602. /**
  3603. * Move a learnpath up (display_order)
  3604. * @param integer $lp_id Learnpath ID
  3605. */
  3606. public static function move_up($lp_id)
  3607. {
  3608. $course_id = api_get_course_int_id();
  3609. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3610. $sql = "SELECT * FROM $lp_table
  3611. WHERE c_id = ".$course_id."
  3612. ORDER BY display_order";
  3613. $res = Database::query($sql);
  3614. if ($res === false)
  3615. return false;
  3616. $lps = array ();
  3617. $lp_order = array ();
  3618. $num = Database :: num_rows($res);
  3619. // First check the order is correct, globally (might be wrong because
  3620. // of versions < 1.8.4)
  3621. if ($num > 0) {
  3622. $i = 1;
  3623. while ($row = Database :: fetch_array($res)) {
  3624. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3625. $need_fix = true;
  3626. $sql_u = "UPDATE $lp_table SET display_order = $i
  3627. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3628. Database::query($sql_u);
  3629. }
  3630. $row['display_order'] = $i;
  3631. $lps[$row['id']] = $row;
  3632. $lp_order[$i] = $row['id'];
  3633. $i++;
  3634. }
  3635. }
  3636. if ($num > 1) { // If there's only one element, no need to sort.
  3637. $order = $lps[$lp_id]['display_order'];
  3638. if ($order > 1) { // If it's the first element, no need to move up.
  3639. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3640. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3641. Database::query($sql_u1);
  3642. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3643. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3644. Database::query($sql_u2);
  3645. }
  3646. }
  3647. }
  3648. /**
  3649. * Move a learnpath down (display_order)
  3650. * @param integer $lp_id Learnpath ID
  3651. */
  3652. public static function move_down($lp_id)
  3653. {
  3654. $course_id = api_get_course_int_id();
  3655. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3656. $sql = "SELECT * FROM $lp_table
  3657. WHERE c_id = ".$course_id."
  3658. ORDER BY display_order";
  3659. $res = Database::query($sql);
  3660. if ($res === false) {
  3661. return false;
  3662. }
  3663. $lps = array ();
  3664. $lp_order = array ();
  3665. $num = Database :: num_rows($res);
  3666. $max = 0;
  3667. // First check the order is correct, globally (might be wrong because
  3668. // of versions < 1.8.4).
  3669. if ($num > 0) {
  3670. $i = 1;
  3671. while ($row = Database :: fetch_array($res)) {
  3672. $max = $i;
  3673. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3674. $need_fix = true;
  3675. $sql_u = "UPDATE $lp_table SET display_order = $i
  3676. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3677. Database::query($sql_u);
  3678. }
  3679. $row['display_order'] = $i;
  3680. $lps[$row['id']] = $row;
  3681. $lp_order[$i] = $row['id'];
  3682. $i++;
  3683. }
  3684. }
  3685. if ($num > 1) { // If there's only one element, no need to sort.
  3686. $order = $lps[$lp_id]['display_order'];
  3687. if ($order < $max) { // If it's the first element, no need to move up.
  3688. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3689. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3690. Database::query($sql_u1);
  3691. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3692. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3693. Database::query($sql_u2);
  3694. }
  3695. }
  3696. }
  3697. /**
  3698. * Updates learnpath attributes to point to the next element
  3699. * The last part is similar to set_current_item but processing the other way around
  3700. */
  3701. public function next()
  3702. {
  3703. if ($this->debug > 0) {
  3704. error_log('New LP - In learnpath::next()', 0);
  3705. }
  3706. $this->last = $this->get_current_item_id();
  3707. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3708. $this->autocomplete_parents($this->last);
  3709. $new_index = $this->get_next_index();
  3710. if ($this->debug > 2) {
  3711. error_log('New LP - New index: ' . $new_index, 0);
  3712. }
  3713. $this->index = $new_index;
  3714. if ($this->debug > 2) {
  3715. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3716. }
  3717. $this->current = $this->ordered_items[$new_index];
  3718. if ($this->debug > 2) {
  3719. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3720. }
  3721. }
  3722. /**
  3723. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3724. * class, this might be redefined to allow several behaviours depending on the document type.
  3725. * @param integer Resource ID
  3726. * @return boolean True on success, false otherwise
  3727. */
  3728. public function open($id)
  3729. {
  3730. if ($this->debug > 0) {
  3731. error_log('New LP - In learnpath::open()', 0);
  3732. }
  3733. // TODO:
  3734. // set the current resource attribute to this resource
  3735. // switch on element type (redefine in child class?)
  3736. // set status for this item to "opened"
  3737. // start timer
  3738. // initialise score
  3739. $this->index = 0; //or = the last item seen (see $this->last)
  3740. }
  3741. /**
  3742. * Check that all prerequisites are fulfilled. Returns true and an
  3743. * empty string on succes, returns false
  3744. * and the prerequisite string on error.
  3745. * This function is based on the rules for aicc_script language as
  3746. * described in the SCORM 1.2 CAM documentation page 108.
  3747. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3748. * @return boolean True if prerequisites are matched, false otherwise -
  3749. * Empty string if true returned, prerequisites string otherwise.
  3750. */
  3751. public function prerequisites_match($itemId = null)
  3752. {
  3753. $debug = $this->debug;
  3754. if ($debug > 0) {
  3755. error_log('In learnpath::prerequisites_match()', 0);
  3756. }
  3757. if (empty($itemId)) {
  3758. $itemId = $this->current;
  3759. }
  3760. $currentItem = $this->getItem($itemId);
  3761. if ($currentItem) {
  3762. if ($this->type == 2) {
  3763. // Getting prereq from scorm
  3764. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3765. } else {
  3766. $prereq_string = $currentItem->get_prereq_string();
  3767. }
  3768. if (empty($prereq_string)) {
  3769. if ($debug > 0) {
  3770. error_log('Found prereq_string is empty return true');
  3771. }
  3772. return true;
  3773. }
  3774. // Clean spaces.
  3775. $prereq_string = str_replace(' ', '', $prereq_string);
  3776. if ($debug > 0) {
  3777. error_log('Found prereq_string: ' . $prereq_string, 0);
  3778. }
  3779. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3780. $result = $currentItem->parse_prereq(
  3781. $prereq_string,
  3782. $this->items,
  3783. $this->refs_list,
  3784. $this->get_user_id()
  3785. );
  3786. if ($result === false) {
  3787. $this->set_error_msg($currentItem->prereq_alert);
  3788. }
  3789. } else {
  3790. $result = true;
  3791. if ($debug > 1) {
  3792. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3793. }
  3794. }
  3795. if ($debug > 1) {
  3796. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3797. }
  3798. return $result;
  3799. }
  3800. /**
  3801. * Updates learnpath attributes to point to the previous element
  3802. * The last part is similar to set_current_item but processing the other way around
  3803. */
  3804. public function previous()
  3805. {
  3806. if ($this->debug > 0) {
  3807. error_log('New LP - In learnpath::previous()', 0);
  3808. }
  3809. $this->last = $this->get_current_item_id();
  3810. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3811. $this->autocomplete_parents($this->last);
  3812. $new_index = $this->get_previous_index();
  3813. $this->index = $new_index;
  3814. $this->current = $this->ordered_items[$new_index];
  3815. }
  3816. /**
  3817. * Publishes a learnpath. This basically means show or hide the learnpath
  3818. * to normal users.
  3819. * Can be used as abstract
  3820. * @param integer Learnpath ID
  3821. * @param string New visibility
  3822. */
  3823. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3824. {
  3825. $action = 'visible';
  3826. if ($set_visibility != 1) {
  3827. $action = 'invisible';
  3828. self::toggle_publish($lp_id, 'i');
  3829. }
  3830. return api_item_property_update(
  3831. api_get_course_info(),
  3832. TOOL_LEARNPATH,
  3833. $lp_id,
  3834. $action,
  3835. api_get_user_id()
  3836. );
  3837. }
  3838. /**
  3839. * Publishes a learnpath. This basically means show or hide the learnpath
  3840. * on the course homepage
  3841. * Can be used as abstract
  3842. * @param integer $lp_id Learnpath id
  3843. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3844. * @return bool
  3845. */
  3846. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3847. {
  3848. $course_id = api_get_course_int_id();
  3849. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3850. $lp_id = intval($lp_id);
  3851. $sql = "SELECT * FROM $tbl_lp
  3852. WHERE c_id = ".$course_id." AND id = $lp_id";
  3853. $result = Database::query($sql);
  3854. if (Database::num_rows($result)) {
  3855. $row = Database :: fetch_array($result);
  3856. $name = domesticate($row['name']);
  3857. if ($set_visibility == 'i') {
  3858. $s = $name . " " . get_lang('LearnpathNotPublished');
  3859. $dialogBox = $s;
  3860. $v = 0;
  3861. }
  3862. if ($set_visibility == 'v') {
  3863. $s = $name . " " . get_lang('LearnpathPublished');
  3864. $dialogBox = $s;
  3865. $v = 1;
  3866. }
  3867. } else {
  3868. return false;
  3869. }
  3870. $session_id = api_get_session_id();
  3871. $session_condition = api_get_session_condition($session_id);
  3872. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3873. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3874. $sql = "SELECT * FROM $tbl_tool
  3875. WHERE
  3876. c_id = ".$course_id." AND
  3877. link='$link' and
  3878. image='scormbuilder.gif' and
  3879. link LIKE '$link%'
  3880. $session_condition
  3881. ";
  3882. $result = Database::query($sql);
  3883. $num = Database :: num_rows($result);
  3884. if ($set_visibility == 'i' && $num > 0) {
  3885. $sql = "DELETE FROM $tbl_tool
  3886. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3887. Database::query($sql);
  3888. } elseif ($set_visibility == 'v' && $num == 0) {
  3889. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3890. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3891. Database::query($sql);
  3892. $insertId = Database::insert_id();
  3893. if ($insertId) {
  3894. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3895. Database::query($sql);
  3896. }
  3897. } elseif ($set_visibility == 'v' && $num > 0) {
  3898. $sql = "UPDATE $tbl_tool SET
  3899. c_id = $course_id,
  3900. name = '$name',
  3901. link = '$link',
  3902. image = 'scormbuilder.gif',
  3903. visibility = '$v',
  3904. admin = '0',
  3905. address = 'pastillegris.gif',
  3906. added_tool = 0,
  3907. session_id = $session_id
  3908. WHERE
  3909. c_id = ".$course_id." AND
  3910. (link='$link' and image='scormbuilder.gif' $session_condition)
  3911. ";
  3912. Database::query($sql);
  3913. } else {
  3914. // Parameter and database incompatible, do nothing, exit.
  3915. return false;
  3916. }
  3917. }
  3918. /**
  3919. * Restart the whole learnpath. Return the URL of the first element.
  3920. * Make sure the results are saved with anoter method. This method should probably be
  3921. * redefined in children classes.
  3922. * To use a similar method statically, use the create_new_attempt() method
  3923. * @return string URL to load in the viewer
  3924. */
  3925. public function restart()
  3926. {
  3927. if ($this->debug > 0) {
  3928. error_log('New LP - In learnpath::restart()', 0);
  3929. }
  3930. // TODO
  3931. // Call autosave method to save the current progress.
  3932. //$this->index = 0;
  3933. if (api_is_invitee()) {
  3934. return false;
  3935. }
  3936. $session_id = api_get_session_id();
  3937. $course_id = api_get_course_int_id();
  3938. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3939. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3940. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3941. if ($this->debug > 2) {
  3942. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3943. }
  3944. $res = Database::query($sql);
  3945. $view_id = Database::insert_id();
  3946. if ($view_id) {
  3947. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3948. Database::query($sql);
  3949. $this->lp_view_id = $view_id;
  3950. $this->attempt = $this->attempt + 1;
  3951. } else {
  3952. $this->error = 'Could not insert into item_view table...';
  3953. return false;
  3954. }
  3955. $this->autocomplete_parents($this->current);
  3956. foreach ($this->items as $index => $dummy) {
  3957. $this->items[$index]->restart();
  3958. $this->items[$index]->set_lp_view($this->lp_view_id);
  3959. }
  3960. $this->first();
  3961. return true;
  3962. }
  3963. /**
  3964. * Saves the current item
  3965. * @return boolean
  3966. */
  3967. public function save_current()
  3968. {
  3969. if ($this->debug > 0) {
  3970. error_log('learnpath::save_current()', 0);
  3971. }
  3972. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3973. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3974. if ($this->debug > 2) {
  3975. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3976. }
  3977. if ($this->debug > 2) {
  3978. error_log('' . print_r($this->items, true), 0);
  3979. }
  3980. if (isset($this->items[$this->current]) &&
  3981. is_object($this->items[$this->current])
  3982. ) {
  3983. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3984. $this->autocomplete_parents($this->current);
  3985. $status = $this->items[$this->current]->get_status();
  3986. $this->update_queue[$this->current] = $status;
  3987. return $res;
  3988. }
  3989. return false;
  3990. }
  3991. /**
  3992. * Saves the given item
  3993. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3994. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3995. * @return boolean
  3996. */
  3997. public function save_item($item_id = null, $from_outside = true)
  3998. {
  3999. $debug = $this->debug;
  4000. if ($debug) {
  4001. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4002. }
  4003. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4004. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4005. if (empty($item_id)) {
  4006. $item_id = intval($_REQUEST['id']);
  4007. }
  4008. if (empty($item_id)) {
  4009. $item_id = $this->get_current_item_id();
  4010. }
  4011. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4012. if ($debug) {
  4013. error_log('Object exists');
  4014. }
  4015. // Saving the item.
  4016. $res = $this->items[$item_id]->save(
  4017. $from_outside,
  4018. $this->prerequisites_match($item_id)
  4019. );
  4020. if ($debug) {
  4021. error_log('update_queue before:');
  4022. error_log(print_r($this->update_queue,1));
  4023. }
  4024. $this->autocomplete_parents($item_id);
  4025. $status = $this->items[$item_id]->get_status();
  4026. $this->update_queue[$item_id] = $status;
  4027. if ($debug) {
  4028. error_log('get_status(): ' . $status);
  4029. error_log('update_queue after:');
  4030. error_log(print_r($this->update_queue,1));
  4031. }
  4032. return $res;
  4033. }
  4034. return false;
  4035. }
  4036. /**
  4037. * Saves the last item seen's ID only in case
  4038. */
  4039. public function save_last()
  4040. {
  4041. $course_id = api_get_course_int_id();
  4042. if ($this->debug > 0) {
  4043. error_log('New LP - In learnpath::save_last()', 0);
  4044. }
  4045. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4046. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4047. if (isset($this->current) && !api_is_invitee()) {
  4048. if ($this->debug > 2) {
  4049. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4050. }
  4051. $sql = "UPDATE $table SET
  4052. last_item = " . intval($this->get_current_item_id()). "
  4053. WHERE
  4054. c_id = $course_id AND
  4055. lp_id = " . $this->get_id() . " AND
  4056. user_id = " . $this->get_user_id()." ".$session_condition;
  4057. if ($this->debug > 2) {
  4058. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4059. }
  4060. Database::query($sql);
  4061. }
  4062. if (!api_is_invitee()) {
  4063. // Save progress.
  4064. list($progress, $text) = $this->get_progress_bar_text('%');
  4065. if ($progress >= 0 && $progress <= 100) {
  4066. $progress = (int) $progress;
  4067. $sql = "UPDATE $table SET
  4068. progress = $progress
  4069. WHERE
  4070. c_id = ".$course_id." AND
  4071. lp_id = " . $this->get_id() . " AND
  4072. user_id = " . $this->get_user_id()." ".$session_condition;
  4073. // Ignore errors as some tables might not have the progress field just yet.
  4074. Database::query($sql);
  4075. $this->progress_db = $progress;
  4076. }
  4077. }
  4078. }
  4079. /**
  4080. * Sets the current item ID (checks if valid and authorized first)
  4081. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4082. */
  4083. public function set_current_item($item_id = null)
  4084. {
  4085. if ($this->debug > 0) {
  4086. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4087. }
  4088. if (empty ($item_id)) {
  4089. if ($this->debug > 2) {
  4090. error_log('New LP - No new current item given, ignore...', 0);
  4091. }
  4092. // Do nothing.
  4093. } else {
  4094. if ($this->debug > 2) {
  4095. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4096. }
  4097. if (is_numeric($item_id)) {
  4098. $item_id = intval($item_id);
  4099. // TODO: Check in database here.
  4100. $this->last = $this->current;
  4101. $this->current = $item_id;
  4102. // TODO: Update $this->index as well.
  4103. foreach ($this->ordered_items as $index => $item) {
  4104. if ($item == $this->current) {
  4105. $this->index = $index;
  4106. break;
  4107. }
  4108. }
  4109. if ($this->debug > 2) {
  4110. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4111. }
  4112. } else {
  4113. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4114. }
  4115. }
  4116. }
  4117. /**
  4118. * Sets the encoding
  4119. * @param string New encoding
  4120. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4121. */
  4122. public function set_encoding($enc = 'UTF-8')
  4123. {
  4124. if ($this->debug > 0) {
  4125. error_log('New LP - In learnpath::set_encoding()', 0);
  4126. }
  4127. $course_id = api_get_course_int_id();
  4128. $enc = api_refine_encoding_id($enc);
  4129. if (empty($enc)) {
  4130. $enc = api_get_system_encoding();
  4131. }
  4132. if (api_is_encoding_supported($enc)) {
  4133. $lp = $this->get_id();
  4134. if ($lp != 0) {
  4135. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4136. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4137. $res = Database::query($sql);
  4138. return $res;
  4139. }
  4140. }
  4141. return false;
  4142. }
  4143. /**
  4144. * Sets the JS lib setting in the database directly.
  4145. * This is the JavaScript library file this lp needs to load on startup
  4146. * @param string Proximity setting
  4147. * @return boolean True on update success. False otherwise.
  4148. */
  4149. public function set_jslib($lib = '')
  4150. {
  4151. if ($this->debug > 0) {
  4152. error_log('New LP - In learnpath::set_jslib()', 0);
  4153. }
  4154. $lp = $this->get_id();
  4155. $course_id = api_get_course_int_id();
  4156. if ($lp != 0) {
  4157. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4158. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4159. $res = Database::query($sql);
  4160. return $res;
  4161. } else {
  4162. return false;
  4163. }
  4164. }
  4165. /**
  4166. * Sets the name of the LP maker (publisher) (and save)
  4167. * @param string Optional string giving the new content_maker of this learnpath
  4168. * @return boolean True
  4169. */
  4170. public function set_maker($name = '')
  4171. {
  4172. if ($this->debug > 0) {
  4173. error_log('New LP - In learnpath::set_maker()', 0);
  4174. }
  4175. if (empty ($name))
  4176. return false;
  4177. $this->maker = $name;
  4178. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4179. $course_id = api_get_course_int_id();
  4180. $lp_id = $this->get_id();
  4181. $sql = "UPDATE $lp_table SET
  4182. content_maker = '" . Database::escape_string($this->maker) . "'
  4183. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4184. if ($this->debug > 2) {
  4185. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4186. }
  4187. Database::query($sql);
  4188. return true;
  4189. }
  4190. /**
  4191. * Sets the name of the current learnpath (and save)
  4192. * @param string $name Optional string giving the new name of this learnpath
  4193. * @return boolean True/False
  4194. */
  4195. public function set_name($name = null)
  4196. {
  4197. if ($this->debug > 0) {
  4198. error_log('New LP - In learnpath::set_name()', 0);
  4199. }
  4200. if (empty($name)) {
  4201. return false;
  4202. }
  4203. $this->name = Database::escape_string($name);
  4204. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4205. $lp_id = $this->get_id();
  4206. $course_id = $this->course_info['real_id'];
  4207. $sql = "UPDATE $lp_table SET
  4208. name = '" . Database::escape_string($this->name). "'
  4209. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4210. if ($this->debug > 2) {
  4211. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4212. }
  4213. $result = Database::query($sql);
  4214. // If the lp is visible on the homepage, change his name there.
  4215. if (Database::affected_rows($result)) {
  4216. $session_id = api_get_session_id();
  4217. $session_condition = api_get_session_condition($session_id);
  4218. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4219. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4220. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4221. WHERE
  4222. c_id = $course_id AND
  4223. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4224. Database::query($sql);
  4225. return true;
  4226. } else {
  4227. return false;
  4228. }
  4229. }
  4230. /**
  4231. * Set index specified prefix terms for all items in this path
  4232. * @param string Comma-separated list of terms
  4233. * @param char Xapian term prefix
  4234. * @return boolean False on error, true otherwise
  4235. */
  4236. public function set_terms_by_prefix($terms_string, $prefix)
  4237. {
  4238. $course_id = api_get_course_int_id();
  4239. if (api_get_setting('search_enabled') !== 'true')
  4240. return false;
  4241. if (!extension_loaded('xapian')) {
  4242. return false;
  4243. }
  4244. $terms_string = trim($terms_string);
  4245. $terms = explode(',', $terms_string);
  4246. array_walk($terms, 'trim_value');
  4247. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4248. // Don't do anything if no change, verify only at DB, not the search engine.
  4249. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4250. return false;
  4251. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4252. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4253. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4254. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4255. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4256. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4257. $lp_id = intval($_POST['lp_id']);
  4258. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4259. $result = Database::query($sql);
  4260. $di = new ChamiloIndexer();
  4261. while ($lp_item = Database :: fetch_array($result)) {
  4262. // Get search_did.
  4263. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4264. $sql = 'SELECT * FROM %s
  4265. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4266. LIMIT 1';
  4267. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4268. //echo $sql; echo '<br>';
  4269. $res = Database::query($sql);
  4270. if (Database::num_rows($res) > 0) {
  4271. $se_ref = Database :: fetch_array($res);
  4272. // Compare terms.
  4273. $doc = $di->get_document($se_ref['search_did']);
  4274. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4275. $xterms = array();
  4276. foreach ($xapian_terms as $xapian_term) {
  4277. $xterms[] = substr($xapian_term['name'], 1);
  4278. }
  4279. $dterms = $terms;
  4280. $missing_terms = array_diff($dterms, $xterms);
  4281. $deprecated_terms = array_diff($xterms, $dterms);
  4282. // Save it to search engine.
  4283. foreach ($missing_terms as $term) {
  4284. $doc->add_term($prefix . $term, 1);
  4285. }
  4286. foreach ($deprecated_terms as $term) {
  4287. $doc->remove_term($prefix . $term);
  4288. }
  4289. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4290. $di->getDb()->flush();
  4291. } else {
  4292. //@todo What we should do here?
  4293. }
  4294. }
  4295. return true;
  4296. }
  4297. /**
  4298. * Sets the theme of the LP (local/remote) (and save)
  4299. * @param string Optional string giving the new theme of this learnpath
  4300. * @return bool Returns true if theme name is not empty
  4301. */
  4302. public function set_theme($name = '')
  4303. {
  4304. $course_id = api_get_course_int_id();
  4305. if ($this->debug > 0) {
  4306. error_log('New LP - In learnpath::set_theme()', 0);
  4307. }
  4308. $this->theme = $name;
  4309. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4310. $lp_id = $this->get_id();
  4311. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4312. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4313. if ($this->debug > 2) {
  4314. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4315. }
  4316. Database::query($sql);
  4317. return true;
  4318. }
  4319. /**
  4320. * Sets the image of an LP (and save)
  4321. * @param string Optional string giving the new image of this learnpath
  4322. * @return bool Returns true if theme name is not empty
  4323. */
  4324. public function set_preview_image($name = '')
  4325. {
  4326. $course_id = api_get_course_int_id();
  4327. if ($this->debug > 0) {
  4328. error_log('New LP - In learnpath::set_preview_image()', 0);
  4329. }
  4330. $this->preview_image = $name;
  4331. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4332. $lp_id = $this->get_id();
  4333. $sql = "UPDATE $lp_table SET
  4334. preview_image = '" . Database::escape_string($this->preview_image). "'
  4335. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4336. if ($this->debug > 2) {
  4337. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4338. }
  4339. Database::query($sql);
  4340. return true;
  4341. }
  4342. /**
  4343. * Sets the author of a LP (and save)
  4344. * @param string Optional string giving the new author of this learnpath
  4345. * @return bool Returns true if author's name is not empty
  4346. */
  4347. public function set_author($name = '')
  4348. {
  4349. $course_id = api_get_course_int_id();
  4350. if ($this->debug > 0) {
  4351. error_log('New LP - In learnpath::set_author()', 0);
  4352. }
  4353. $this->author = $name;
  4354. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4355. $lp_id = $this->get_id();
  4356. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4357. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4358. if ($this->debug > 2) {
  4359. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4360. }
  4361. Database::query($sql);
  4362. return true;
  4363. }
  4364. /**
  4365. * Sets the hide_toc_frame parameter of a LP (and save)
  4366. * @param int 1 if frame is hidden 0 then else
  4367. * @return bool Returns true if author's name is not empty
  4368. */
  4369. public function set_hide_toc_frame($hide)
  4370. {
  4371. $course_id = api_get_course_int_id();
  4372. if ($this->debug > 0) {
  4373. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4374. }
  4375. if (intval($hide) == $hide){
  4376. $this->hide_toc_frame = $hide;
  4377. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4378. $lp_id = $this->get_id();
  4379. $sql = "UPDATE $lp_table SET
  4380. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4381. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4382. if ($this->debug > 2) {
  4383. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4384. }
  4385. Database::query($sql);
  4386. return true;
  4387. } else {
  4388. return false;
  4389. }
  4390. }
  4391. /**
  4392. * Sets the prerequisite of a LP (and save)
  4393. * @param int integer giving the new prerequisite of this learnpath
  4394. * @return bool returns true if prerequisite is not empty
  4395. */
  4396. public function set_prerequisite($prerequisite)
  4397. {
  4398. $course_id = api_get_course_int_id();
  4399. if ($this->debug > 0) {
  4400. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4401. }
  4402. $this->prerequisite = intval($prerequisite);
  4403. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4404. $lp_id = $this->get_id();
  4405. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4406. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4407. if ($this->debug > 2) {
  4408. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4409. }
  4410. Database::query($sql);
  4411. return true;
  4412. }
  4413. /**
  4414. * Sets the location/proximity of the LP (local/remote) (and save)
  4415. * @param string Optional string giving the new location of this learnpath
  4416. * @return boolean True on success / False on error
  4417. */
  4418. public function set_proximity($name = '')
  4419. {
  4420. $course_id = api_get_course_int_id();
  4421. if ($this->debug > 0) {
  4422. error_log('New LP - In learnpath::set_proximity()', 0);
  4423. }
  4424. if (empty ($name))
  4425. return false;
  4426. $this->proximity = $name;
  4427. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4428. $lp_id = $this->get_id();
  4429. $sql = "UPDATE $lp_table SET
  4430. content_local = '" . Database::escape_string($name) . "'
  4431. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4432. if ($this->debug > 2) {
  4433. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4434. }
  4435. Database::query($sql);
  4436. return true;
  4437. }
  4438. /**
  4439. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4440. * @param integer DB ID of the item
  4441. */
  4442. public function set_previous_item($id)
  4443. {
  4444. if ($this->debug > 0) {
  4445. error_log('New LP - In learnpath::set_previous_item()', 0);
  4446. }
  4447. $this->last = $id;
  4448. }
  4449. /**
  4450. * Sets use_max_score
  4451. * @param string $use_max_score Optional string giving the new location of this learnpath
  4452. * @return boolean True on success / False on error
  4453. */
  4454. public function set_use_max_score($use_max_score = 1)
  4455. {
  4456. $course_id = api_get_course_int_id();
  4457. if ($this->debug > 0) {
  4458. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4459. }
  4460. $use_max_score = intval($use_max_score);
  4461. $this->use_max_score = $use_max_score;
  4462. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4463. $lp_id = $this->get_id();
  4464. $sql = "UPDATE $lp_table SET
  4465. use_max_score = '" . $this->use_max_score . "'
  4466. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4467. if ($this->debug > 2) {
  4468. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4469. }
  4470. Database::query($sql);
  4471. return true;
  4472. }
  4473. /**
  4474. * Sets and saves the expired_on date
  4475. * @param string $expired_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_expired_on($expired_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($expired_on)) {
  4485. $this->expired_on = api_get_utc_datetime($expired_on);
  4486. } else {
  4487. $this->expired_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. expired_on = '" . Database::escape_string($this->expired_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 expired_on : ' . $this->expired_on, 0);
  4496. }
  4497. Database::query($sql);
  4498. return true;
  4499. }
  4500. /**
  4501. * Sets and saves the publicated_on date
  4502. * @param string $publicated_on Optional string giving the new author of this learnpath
  4503. * @return bool Returns true if author's name is not empty
  4504. */
  4505. public function set_publicated_on($publicated_on)
  4506. {
  4507. $course_id = api_get_course_int_id();
  4508. if ($this->debug > 0) {
  4509. error_log('New LP - In learnpath::set_expired_on()', 0);
  4510. }
  4511. if (!empty($publicated_on)) {
  4512. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4513. } else {
  4514. $this->publicated_on = '';
  4515. }
  4516. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4517. $lp_id = $this->get_id();
  4518. $sql = "UPDATE $lp_table SET
  4519. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4520. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4521. if ($this->debug > 2) {
  4522. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4523. }
  4524. Database::query($sql);
  4525. return true;
  4526. }
  4527. /**
  4528. * Sets and saves the expired_on date
  4529. * @return bool Returns true if author's name is not empty
  4530. */
  4531. public function set_modified_on()
  4532. {
  4533. $course_id = api_get_course_int_id();
  4534. if ($this->debug > 0) {
  4535. error_log('New LP - In learnpath::set_expired_on()', 0);
  4536. }
  4537. $this->modified_on = api_get_utc_datetime();
  4538. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4539. $lp_id = $this->get_id();
  4540. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4541. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4542. if ($this->debug > 2) {
  4543. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4544. }
  4545. Database::query($sql);
  4546. return true;
  4547. }
  4548. /**
  4549. * Sets the object's error message
  4550. * @param string Error message. If empty, reinits the error string
  4551. * @return void
  4552. */
  4553. public function set_error_msg($error = '')
  4554. {
  4555. if ($this->debug > 0) {
  4556. error_log('New LP - In learnpath::set_error_msg()', 0);
  4557. }
  4558. if (empty ($error)) {
  4559. $this->error = '';
  4560. } else {
  4561. $this->error .= $error;
  4562. }
  4563. }
  4564. /**
  4565. * Launches the current item if not 'sco'
  4566. * (starts timer and make sure there is a record ready in the DB)
  4567. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4568. * @return boolean
  4569. */
  4570. public function start_current_item($allow_new_attempt = false)
  4571. {
  4572. if ($this->debug > 0) {
  4573. error_log('New LP - In learnpath::start_current_item()', 0);
  4574. }
  4575. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4576. $type = $this->get_type();
  4577. $item_type = $this->items[$this->current]->get_type();
  4578. if (($type == 2 && $item_type != 'sco') ||
  4579. ($type == 3 && $item_type != 'au') ||
  4580. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4581. ) {
  4582. $this->items[$this->current]->open($allow_new_attempt);
  4583. $this->autocomplete_parents($this->current);
  4584. $prereq_check = $this->prerequisites_match($this->current);
  4585. $this->items[$this->current]->save(false, $prereq_check);
  4586. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4587. } else {
  4588. // If sco, then it is supposed to have been updated by some other call.
  4589. }
  4590. if ($item_type == 'sco') {
  4591. $this->items[$this->current]->restart();
  4592. }
  4593. }
  4594. if ($this->debug > 0) {
  4595. error_log('New LP - End of learnpath::start_current_item()', 0);
  4596. }
  4597. return true;
  4598. }
  4599. /**
  4600. * Stops the processing and counters for the old item (as held in $this->last)
  4601. * @return boolean True/False
  4602. */
  4603. public function stop_previous_item()
  4604. {
  4605. if ($this->debug > 0) {
  4606. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4607. }
  4608. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4609. if ($this->debug > 2) {
  4610. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4611. }
  4612. switch ($this->get_type()) {
  4613. case '3' :
  4614. if ($this->items[$this->last]->get_type() != 'au') {
  4615. if ($this->debug > 2) {
  4616. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4617. }
  4618. $this->items[$this->last]->close();
  4619. //$this->autocomplete_parents($this->last);
  4620. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4621. } else {
  4622. if ($this->debug > 2) {
  4623. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4624. }
  4625. }
  4626. case '2' :
  4627. if ($this->items[$this->last]->get_type() != 'sco') {
  4628. if ($this->debug > 2) {
  4629. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4630. }
  4631. $this->items[$this->last]->close();
  4632. //$this->autocomplete_parents($this->last);
  4633. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4634. } else {
  4635. if ($this->debug > 2) {
  4636. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4637. }
  4638. }
  4639. break;
  4640. case '1' :
  4641. default :
  4642. if ($this->debug > 2) {
  4643. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4644. }
  4645. $this->items[$this->last]->close();
  4646. break;
  4647. }
  4648. } else {
  4649. if ($this->debug > 2) {
  4650. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4651. }
  4652. return false;
  4653. }
  4654. return true;
  4655. }
  4656. /**
  4657. * Updates the default view mode from fullscreen to embedded and inversely
  4658. * @return string The current default view mode ('fullscreen' or 'embedded')
  4659. */
  4660. public function update_default_view_mode()
  4661. {
  4662. $course_id = api_get_course_int_id();
  4663. if ($this->debug > 0) {
  4664. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4665. }
  4666. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4667. $sql = "SELECT * FROM $lp_table
  4668. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4669. $res = Database::query($sql);
  4670. if (Database :: num_rows($res) > 0) {
  4671. $row = Database :: fetch_array($res);
  4672. $default_view_mode = $row['default_view_mod'];
  4673. $view_mode = $default_view_mode;
  4674. switch ($default_view_mode) {
  4675. case 'fullscreen': // default with popup
  4676. $view_mode = 'embedded';
  4677. break;
  4678. case 'embedded': // default view with left menu
  4679. $view_mode = 'embedframe';
  4680. break;
  4681. case 'embedframe': //folded menu
  4682. $view_mode = 'impress';
  4683. break;
  4684. case 'impress':
  4685. $view_mode = 'fullscreen';
  4686. break;
  4687. }
  4688. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4689. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4690. Database::query($sql);
  4691. $this->mode = $view_mode;
  4692. return $view_mode;
  4693. } else {
  4694. if ($this->debug > 2) {
  4695. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4696. }
  4697. }
  4698. return -1;
  4699. }
  4700. /**
  4701. * Updates the default behaviour about auto-commiting SCORM updates
  4702. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4703. */
  4704. public function update_default_scorm_commit()
  4705. {
  4706. $course_id = api_get_course_int_id();
  4707. if ($this->debug > 0) {
  4708. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4709. }
  4710. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4711. $sql = "SELECT * FROM $lp_table
  4712. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4713. $res = Database::query($sql);
  4714. if (Database :: num_rows($res) > 0) {
  4715. $row = Database :: fetch_array($res);
  4716. $force = $row['force_commit'];
  4717. if ($force == 1) {
  4718. $force = 0;
  4719. $force_return = false;
  4720. } elseif ($force == 0) {
  4721. $force = 1;
  4722. $force_return = true;
  4723. }
  4724. $sql = "UPDATE $lp_table SET force_commit = $force
  4725. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4726. Database::query($sql);
  4727. $this->force_commit = $force_return;
  4728. return $force_return;
  4729. } else {
  4730. if ($this->debug > 2) {
  4731. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4732. }
  4733. }
  4734. return -1;
  4735. }
  4736. /**
  4737. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4738. * @return bool True on success, false on failure
  4739. */
  4740. public function update_display_order()
  4741. {
  4742. $course_id = api_get_course_int_id();
  4743. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4744. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4745. $res = Database::query($sql);
  4746. if ($res === false)
  4747. return false;
  4748. $num = Database :: num_rows($res);
  4749. // First check the order is correct, globally (might be wrong because
  4750. // of versions < 1.8.4).
  4751. if ($num > 0) {
  4752. $i = 1;
  4753. while ($row = Database :: fetch_array($res)) {
  4754. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4755. $need_fix = true;
  4756. $sql = "UPDATE $lp_table SET display_order = $i
  4757. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4758. Database::query($sql);
  4759. }
  4760. $i++;
  4761. }
  4762. }
  4763. return true;
  4764. }
  4765. /**
  4766. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4767. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4768. */
  4769. public function update_reinit()
  4770. {
  4771. $course_id = api_get_course_int_id();
  4772. if ($this->debug > 0) {
  4773. error_log('New LP - In learnpath::update_reinit()', 0);
  4774. }
  4775. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4776. $sql = "SELECT * FROM $lp_table
  4777. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4778. $res = Database::query($sql);
  4779. if (Database :: num_rows($res) > 0) {
  4780. $row = Database :: fetch_array($res);
  4781. $force = $row['prevent_reinit'];
  4782. if ($force == 1) {
  4783. $force = 0;
  4784. } elseif ($force == 0) {
  4785. $force = 1;
  4786. }
  4787. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4788. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4789. Database::query($sql);
  4790. $this->prevent_reinit = $force;
  4791. return $force;
  4792. } else {
  4793. if ($this->debug > 2) {
  4794. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4795. }
  4796. }
  4797. return -1;
  4798. }
  4799. /**
  4800. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4801. *
  4802. * @return string 'single', 'multi' or 'seriousgame'
  4803. * @author ndiechburg <noel@cblue.be>
  4804. **/
  4805. public function get_attempt_mode()
  4806. {
  4807. //Set default value for seriousgame_mode
  4808. if (!isset($this->seriousgame_mode)) {
  4809. $this->seriousgame_mode=0;
  4810. }
  4811. // Set default value for prevent_reinit
  4812. if (!isset($this->prevent_reinit)) {
  4813. $this->prevent_reinit =1;
  4814. }
  4815. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4816. return 'seriousgame';
  4817. }
  4818. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4819. return 'single';
  4820. }
  4821. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4822. return 'multiple';
  4823. }
  4824. return 'single';
  4825. }
  4826. /**
  4827. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4828. *
  4829. * @param string 'seriousgame', 'single' or 'multiple'
  4830. * @return boolean
  4831. * @author ndiechburg <noel@cblue.be>
  4832. **/
  4833. public function set_attempt_mode($mode)
  4834. {
  4835. $course_id = api_get_course_int_id();
  4836. switch ($mode) {
  4837. case 'seriousgame' :
  4838. $sg_mode = 1;
  4839. $prevent_reinit = 1;
  4840. break;
  4841. case 'single' :
  4842. $sg_mode = 0;
  4843. $prevent_reinit = 1;
  4844. break;
  4845. case 'multiple' :
  4846. $sg_mode = 0;
  4847. $prevent_reinit = 0;
  4848. break;
  4849. default :
  4850. $sg_mode = 0;
  4851. $prevent_reinit = 0;
  4852. break;
  4853. }
  4854. $this->prevent_reinit = $prevent_reinit;
  4855. $this->seriousgame_mode = $sg_mode;
  4856. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4857. $sql = "UPDATE $lp_table SET
  4858. prevent_reinit = $prevent_reinit ,
  4859. seriousgame_mode = $sg_mode
  4860. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4861. $res = Database::query($sql);
  4862. if ($res) {
  4863. return true;
  4864. } else {
  4865. return false;
  4866. }
  4867. }
  4868. /**
  4869. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4870. *
  4871. * @return boolean
  4872. * @author ndiechburg <noel@cblue.be>
  4873. **/
  4874. public function switch_attempt_mode()
  4875. {
  4876. if ($this->debug > 0) {
  4877. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4878. }
  4879. $mode = $this->get_attempt_mode();
  4880. switch ($mode) {
  4881. case 'single' :
  4882. $next_mode = 'multiple';
  4883. break;
  4884. case 'multiple' :
  4885. $next_mode = 'seriousgame';
  4886. break;
  4887. case 'seriousgame' :
  4888. $next_mode = 'single';
  4889. break;
  4890. default :
  4891. $next_mode = 'single';
  4892. break;
  4893. }
  4894. $this->set_attempt_mode($next_mode);
  4895. }
  4896. /**
  4897. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4898. * but possibility to do again a completed item.
  4899. *
  4900. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4901. * @author ndiechburg <noel@cblue.be>
  4902. **/
  4903. public function set_seriousgame_mode()
  4904. {
  4905. $course_id = api_get_course_int_id();
  4906. if ($this->debug > 0) {
  4907. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4908. }
  4909. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4910. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4911. $res = Database::query($sql);
  4912. if (Database :: num_rows($res) > 0) {
  4913. $row = Database :: fetch_array($res);
  4914. $force = $row['seriousgame_mode'];
  4915. if ($force == 1) {
  4916. $force = 0;
  4917. } elseif ($force == 0) {
  4918. $force = 1;
  4919. }
  4920. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4921. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4922. Database::query($sql);
  4923. $this->seriousgame_mode = $force;
  4924. return $force;
  4925. } else {
  4926. if ($this->debug > 2) {
  4927. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4928. }
  4929. }
  4930. return -1;
  4931. }
  4932. /**
  4933. * Updates the "scorm_debug" value that shows or hide the debug window
  4934. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4935. */
  4936. public function update_scorm_debug()
  4937. {
  4938. $course_id = api_get_course_int_id();
  4939. if ($this->debug > 0) {
  4940. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4941. }
  4942. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4943. $sql = "SELECT * FROM $lp_table
  4944. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4945. $res = Database::query($sql);
  4946. if (Database :: num_rows($res) > 0) {
  4947. $row = Database :: fetch_array($res);
  4948. $force = $row['debug'];
  4949. if ($force == 1) {
  4950. $force = 0;
  4951. } elseif ($force == 0) {
  4952. $force = 1;
  4953. }
  4954. $sql = "UPDATE $lp_table SET debug = $force
  4955. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4956. $res = Database::query($sql);
  4957. $this->scorm_debug = $force;
  4958. return $force;
  4959. } else {
  4960. if ($this->debug > 2) {
  4961. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4962. }
  4963. }
  4964. return -1;
  4965. }
  4966. /**
  4967. * Function that makes a call to the function sort_tree_array and create_tree_array
  4968. * @author Kevin Van Den Haute
  4969. * @param array
  4970. */
  4971. public function tree_array($array)
  4972. {
  4973. if ($this->debug > 1) {
  4974. error_log('New LP - In learnpath::tree_array()', 0);
  4975. }
  4976. $array = $this->sort_tree_array($array);
  4977. $this->create_tree_array($array);
  4978. }
  4979. /**
  4980. * Creates an array with the elements of the learning path tree in it
  4981. *
  4982. * @author Kevin Van Den Haute
  4983. * @param array $array
  4984. * @param int $parent
  4985. * @param int $depth
  4986. * @param array $tmp
  4987. */
  4988. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4989. {
  4990. if ($this->debug > 1) {
  4991. error_log('New LP - In learnpath::create_tree_array())', 0);
  4992. }
  4993. if (is_array($array)) {
  4994. for ($i = 0; $i < count($array); $i++) {
  4995. if ($array[$i]['parent_item_id'] == $parent) {
  4996. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4997. $tmp[] = $array[$i]['parent_item_id'];
  4998. $depth++;
  4999. }
  5000. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5001. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5002. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5003. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5004. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5005. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5006. $this->arrMenu[] = array(
  5007. 'id' => $array[$i]['id'],
  5008. 'ref' => $ref,
  5009. 'item_type' => $array[$i]['item_type'],
  5010. 'title' => $array[$i]['title'],
  5011. 'path' => $path,
  5012. 'description' => $array[$i]['description'],
  5013. 'parent_item_id' => $array[$i]['parent_item_id'],
  5014. 'previous_item_id' => $array[$i]['previous_item_id'],
  5015. 'next_item_id' => $array[$i]['next_item_id'],
  5016. 'min_score' => $array[$i]['min_score'],
  5017. 'max_score' => $array[$i]['max_score'],
  5018. 'mastery_score' => $array[$i]['mastery_score'],
  5019. 'display_order' => $array[$i]['display_order'],
  5020. 'prerequisite' => $preq,
  5021. 'depth' => $depth,
  5022. 'audio' => $audio,
  5023. 'prerequisite_min_score' => $prerequisiteMinScore,
  5024. 'prerequisite_max_score' => $prerequisiteMaxScore
  5025. );
  5026. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5027. }
  5028. }
  5029. }
  5030. }
  5031. /**
  5032. * Sorts a multi dimensional array by parent id and display order
  5033. * @author Kevin Van Den Haute
  5034. *
  5035. * @param array $array (array with al the learning path items in it)
  5036. *
  5037. * @return array
  5038. */
  5039. public function sort_tree_array($array) {
  5040. foreach ($array as $key => $row) {
  5041. $parent[$key] = $row['parent_item_id'];
  5042. $position[$key] = $row['display_order'];
  5043. }
  5044. if (count($array) > 0)
  5045. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5046. return $array;
  5047. }
  5048. /**
  5049. * Function that creates a html list of learning path items so that we can add audio files to them
  5050. * @author Kevin Van Den Haute
  5051. * @param int $lp_id
  5052. * @return string
  5053. */
  5054. public function overview()
  5055. {
  5056. if ($this->debug > 0) {
  5057. error_log('New LP - In learnpath::overview()', 0);
  5058. }
  5059. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5060. $return = '';
  5061. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5062. // we need to start a form when we want to update all the mp3 files
  5063. if ($update_audio == 'true') {
  5064. $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">';
  5065. }
  5066. $return .= '<div id="message"></div>';
  5067. if (count($this->items) == 0) {
  5068. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5069. } else {
  5070. $return_audio = '<table class="data_table">';
  5071. $return_audio .= '<tr>';
  5072. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5073. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5074. $return_audio .= '</tr>';
  5075. if ($update_audio != 'true') {
  5076. $return .= '<div class="col-md-12">';
  5077. $return .= self::return_new_tree($update_audio);
  5078. $return .='</div>';
  5079. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5080. } else {
  5081. $return_audio .= self::return_new_tree($update_audio);
  5082. $return .= $return_audio.'</table>';
  5083. }
  5084. // We need to close the form when we are updating the mp3 files.
  5085. if ($update_audio == 'true') {
  5086. $return .= '<div class="footer-audio">';
  5087. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5088. $return .= '</div>';
  5089. //$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?
  5090. }
  5091. }
  5092. // We need to close the form when we are updating the mp3 files.
  5093. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5094. $return .= '</form>';
  5095. }
  5096. return $return;
  5097. }
  5098. /**
  5099. * @param string string $update_audio
  5100. * @param bool $drop_element_here
  5101. * @return string
  5102. */
  5103. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5104. {
  5105. $return = '';
  5106. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5107. $course_id = api_get_course_int_id();
  5108. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5109. $sql = "SELECT * FROM $tbl_lp_item
  5110. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5111. $result = Database::query($sql);
  5112. $arrLP = array();
  5113. while ($row = Database :: fetch_array($result)) {
  5114. $arrLP[] = array(
  5115. 'id' => $row['id'],
  5116. 'item_type' => $row['item_type'],
  5117. 'title' => Security :: remove_XSS($row['title']),
  5118. 'path' => $row['path'],
  5119. 'description' => Security::remove_XSS($row['description']),
  5120. 'parent_item_id' => $row['parent_item_id'],
  5121. 'previous_item_id' => $row['previous_item_id'],
  5122. 'next_item_id' => $row['next_item_id'],
  5123. 'max_score' => $row['max_score'],
  5124. 'min_score' => $row['min_score'],
  5125. 'mastery_score' => $row['mastery_score'],
  5126. 'prerequisite' => $row['prerequisite'],
  5127. 'display_order' => $row['display_order'],
  5128. 'audio' => $row['audio'],
  5129. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5130. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5131. );
  5132. }
  5133. $this->tree_array($arrLP);
  5134. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5135. unset ($this->arrMenu);
  5136. $default_data = null;
  5137. $default_content = null;
  5138. $elements = array();
  5139. $return_audio = null;
  5140. for ($i = 0; $i < count($arrLP); $i++) {
  5141. $title = $arrLP[$i]['title'];
  5142. $title_cut = cut($arrLP[$i]['title'], 25);
  5143. //Link for the documents
  5144. if ($arrLP[$i]['item_type'] == 'document') {
  5145. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5146. $title_cut = Display::url(
  5147. $title_cut,
  5148. $url,
  5149. array(
  5150. 'class' => 'ajax',
  5151. 'data-title' => $title_cut
  5152. )
  5153. );
  5154. }
  5155. if (($i % 2) == 0) {
  5156. $oddClass = 'row_odd';
  5157. } else {
  5158. $oddClass = 'row_even';
  5159. }
  5160. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5161. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5162. $icon = '';
  5163. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5164. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5165. } else {
  5166. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5167. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5168. } else {
  5169. $icon = '<img src="../img/folder_document.gif" />';
  5170. }
  5171. }
  5172. // The audio column.
  5173. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5174. $audio = '';
  5175. if (!$update_audio || $update_audio <> 'true') {
  5176. if (!empty($arrLP[$i]['audio'])) {
  5177. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5178. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5179. $audio .= '<script type="text/javascript">
  5180. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5181. s1.addParam("allowscriptaccess","always");
  5182. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5183. s1.write("container' . $i . '");
  5184. </script>';*/
  5185. } else {
  5186. $audio .= '';
  5187. }
  5188. } else {
  5189. $types = self::getChapterTypes();
  5190. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5191. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5192. if (!empty ($arrLP[$i]['audio'])) {
  5193. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5194. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5195. }
  5196. }
  5197. }
  5198. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5199. $return_audio .= '</td>';
  5200. $move_icon = '';
  5201. $move_item_icon = '';
  5202. $edit_icon = '';
  5203. $delete_icon = '';
  5204. $audio_icon = '';
  5205. $prerequisities_icon = '';
  5206. $forumIcon = '';
  5207. if ($is_allowed_to_edit) {
  5208. if (!$update_audio || $update_audio <> 'true') {
  5209. $move_icon .= '<a class="moved" href="#">';
  5210. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5211. $move_icon .= '</a>';
  5212. }
  5213. // No edit for this item types
  5214. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5215. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5216. $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'] . '" class="btn btn-default">';
  5217. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5218. $edit_icon .= '</a>';
  5219. if (
  5220. !in_array($arrLP[$i]['item_type'], ['forum', 'thread'])
  5221. ) {
  5222. if (
  5223. $this->items[$arrLP[$i]['id']]->getForumThread(
  5224. $this->course_int_id,
  5225. $this->lp_session_id
  5226. )
  5227. ) {
  5228. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5229. 'action' => 'dissociate_forum',
  5230. 'id' => $arrLP[$i]['id'],
  5231. 'lp_id' => $this->lp_id
  5232. ]);
  5233. $forumIcon = Display::url(
  5234. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5235. $forumIconUrl,
  5236. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5237. );
  5238. } else {
  5239. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5240. 'action' => 'create_forum',
  5241. 'id' => $arrLP[$i]['id'],
  5242. 'lp_id' => $this->lp_id
  5243. ]);
  5244. $forumIcon = Display::url(
  5245. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5246. $forumIconUrl,
  5247. ['class' => "btn btn-default lp-btn-associate-forum"]
  5248. );
  5249. }
  5250. }
  5251. } else {
  5252. $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'] . '" class="btn btn-default">';
  5253. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5254. $edit_icon .= '</a>';
  5255. }
  5256. }
  5257. $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) . '\');" class="btn btn-default">';
  5258. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5259. $delete_icon .= '</a>';
  5260. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5261. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5262. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']);
  5263. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item', ['class' => 'btn btn-default']);
  5264. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio', ['class' => 'btn btn-default']);
  5265. }
  5266. }
  5267. if ($update_audio != 'true') {
  5268. $row = $move_icon . ' ' . $icon .
  5269. Display::span($title_cut) .
  5270. Display::tag(
  5271. 'div',
  5272. "<div class=\"btn-group btn-group-xs\">$audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5273. array('class'=>'btn-toolbar button_actions')
  5274. );
  5275. } else {
  5276. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5277. }
  5278. $parent_id = $arrLP[$i]['parent_item_id'];
  5279. $default_data[$arrLP[$i]['id']] = $row;
  5280. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5281. if (empty($parent_id)) {
  5282. $elements[$arrLP[$i]['id']]['data'] = $row;
  5283. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5284. } else {
  5285. $parent_arrays = array();
  5286. if ($arrLP[$i]['depth'] > 1) {
  5287. //Getting list of parents
  5288. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5289. foreach($arrLP as $item) {
  5290. if ($item['id'] == $parent_id) {
  5291. if ($item['parent_item_id'] == 0) {
  5292. $parent_id = $item['id'];
  5293. break;
  5294. } else {
  5295. $parent_id = $item['parent_item_id'];
  5296. if (empty($parent_arrays)) {
  5297. $parent_arrays[] = intval($item['id']);
  5298. }
  5299. $parent_arrays[] = $parent_id;
  5300. break;
  5301. }
  5302. }
  5303. }
  5304. }
  5305. }
  5306. if (!empty($parent_arrays)) {
  5307. $parent_arrays = array_reverse($parent_arrays);
  5308. $val = '$elements';
  5309. $x = 0;
  5310. foreach($parent_arrays as $item) {
  5311. if ($x != count($parent_arrays) -1) {
  5312. $val .= '["'.$item.'"]["children"]';
  5313. } else {
  5314. $val .= '["'.$item.'"]["children"]';
  5315. }
  5316. $x++;
  5317. }
  5318. $val .= "";
  5319. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5320. eval($code_str);
  5321. } else {
  5322. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5323. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5324. }
  5325. }
  5326. }
  5327. $list = '<ul id="lp_item_list">';
  5328. $tree = self::print_recursive($elements, $default_data, $default_content);
  5329. if (!empty($tree)) {
  5330. $list .= $tree;
  5331. } else {
  5332. if ($drop_element_here) {
  5333. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5334. }
  5335. }
  5336. $list .= '</ul>';
  5337. //$return .= Display::panel($list, $this->name);
  5338. $return .= Display::panelCollapse($this->name, $list, 'scorm-list', null, 'scorm-list-accordion', 'scorm-list-collapse');
  5339. if ($update_audio == 'true') {
  5340. $return = $return_audio;
  5341. }
  5342. return $return;
  5343. }
  5344. /**
  5345. * @param array $elements
  5346. * @param array $default_data
  5347. * @param array $default_content
  5348. * @return string
  5349. */
  5350. public function print_recursive($elements, $default_data, $default_content)
  5351. {
  5352. $return = '';
  5353. foreach ($elements as $key => $item) {
  5354. if (isset($item['load_data']) || empty($item['data'])) {
  5355. $item['data'] = $default_data[$item['load_data']];
  5356. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5357. }
  5358. $sub_list = '';
  5359. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5360. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5361. }
  5362. if (empty($item['children'])) {
  5363. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5364. $active = null;
  5365. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5366. $active = 'active';
  5367. }
  5368. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5369. } else {
  5370. //sections
  5371. if (isset($item['children'])) {
  5372. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5373. }
  5374. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5375. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5376. }
  5377. }
  5378. return $return;
  5379. }
  5380. /**
  5381. * This function builds the action menu
  5382. * @param bool $returnContent
  5383. * @return void
  5384. */
  5385. public function build_action_menu($returnContent = false)
  5386. {
  5387. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5388. $return = '<div class="actions">';
  5389. $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> ';
  5390. $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>';
  5391. $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>';
  5392. $buttons = array(
  5393. array(
  5394. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5395. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5396. ),
  5397. array(
  5398. 'title' => get_lang('ClearAllPrerequisites'),
  5399. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5400. ),
  5401. );
  5402. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5403. $return .= '</div>';
  5404. if ($returnContent) {
  5405. return $return;
  5406. }
  5407. echo $return;
  5408. }
  5409. /**
  5410. * Creates the default learning path folder
  5411. * @param array $course
  5412. * @return bool
  5413. */
  5414. public static function generate_learning_path_folder($course)
  5415. {
  5416. // Creating learning_path folder
  5417. $dir = '/learning_path';
  5418. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5419. $folder = false;
  5420. if (!is_dir($filepath.'/'.$dir)) {
  5421. $folderData = create_unexisting_directory(
  5422. $course,
  5423. api_get_user_id(),
  5424. api_get_session_id(),
  5425. 0,
  5426. 0,
  5427. $filepath,
  5428. $dir,
  5429. get_lang('LearningPaths'),
  5430. 0
  5431. );
  5432. if (!empty($folderData)) {
  5433. $folder = true;
  5434. }
  5435. } else {
  5436. $folder = true;
  5437. }
  5438. return $folder;
  5439. }
  5440. /**
  5441. * @param array $course
  5442. * @param string $lp_name
  5443. * @return array
  5444. */
  5445. public function generate_lp_folder($course, $lp_name = null)
  5446. {
  5447. $filepath = '';
  5448. $dir = '/learning_path/';
  5449. if (empty($lp_name)) {
  5450. $lp_name = $this->name;
  5451. }
  5452. $folder = self::generate_learning_path_folder($course);
  5453. // Creating LP folder
  5454. if ($folder) {
  5455. //Limits title size
  5456. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5457. $dir = $dir.$title;
  5458. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5459. if (!is_dir($filepath.'/'.$dir)) {
  5460. $folderData = create_unexisting_directory(
  5461. $course,
  5462. api_get_user_id(),
  5463. 0,
  5464. 0,
  5465. 0,
  5466. $filepath,
  5467. $dir,
  5468. $lp_name
  5469. );
  5470. if (!empty($folderData)) {
  5471. $folder = true;
  5472. }
  5473. } else {
  5474. $folder = true;
  5475. }
  5476. $dir = $dir.'/';
  5477. if ($folder) {
  5478. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5479. }
  5480. }
  5481. $array = array(
  5482. 'dir' => $dir,
  5483. 'filepath' => $filepath,
  5484. 'folder' => $folder
  5485. );
  5486. return $array;
  5487. }
  5488. /**
  5489. * Create a new document //still needs some finetuning
  5490. * @param array $courseInfo
  5491. * @param string $content
  5492. * @param string $title
  5493. * @param string $extension
  5494. *
  5495. * @return string
  5496. */
  5497. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html')
  5498. {
  5499. if (!empty($courseInfo)) {
  5500. $course_id = $courseInfo['real_id'];
  5501. } else {
  5502. $course_id = api_get_course_int_id();
  5503. }
  5504. global $charset;
  5505. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5506. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5507. // Please, do not modify this dirname formatting.
  5508. if (strstr($dir, '..')) {
  5509. $dir = '/';
  5510. }
  5511. if (!empty($dir[0]) && $dir[0] == '.') {
  5512. $dir = substr($dir, 1);
  5513. }
  5514. if (!empty($dir[0]) && $dir[0] != '/') {
  5515. $dir = '/' . $dir;
  5516. }
  5517. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5518. $dir .= '/';
  5519. }
  5520. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document' . $dir;
  5521. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5522. //Generates folder
  5523. $result = $this->generate_lp_folder($courseInfo);
  5524. $dir = $result['dir'];
  5525. $filepath = $result['filepath'];
  5526. }
  5527. if (!is_dir($filepath)) {
  5528. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/';
  5529. $dir = '/';
  5530. }
  5531. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5532. // is already escaped twice when it gets here.
  5533. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5534. if (!empty($title)) {
  5535. $title = api_replace_dangerous_char(stripslashes($title));
  5536. } else {
  5537. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5538. }
  5539. $title = disable_dangerous_file($title);
  5540. $filename = $title;
  5541. $content = !empty($content) ? $content : $_POST['content_lp'];
  5542. $tmp_filename = $filename;
  5543. $i = 0;
  5544. while (file_exists($filepath . $tmp_filename . '.'.$extension))
  5545. $tmp_filename = $filename . '_' . ++ $i;
  5546. $filename = $tmp_filename . '.'.$extension;
  5547. if ($extension == 'html') {
  5548. $content = stripslashes($content);
  5549. $content = str_replace(
  5550. api_get_path(WEB_COURSE_PATH),
  5551. api_get_path(REL_PATH).'courses/',
  5552. $content
  5553. );
  5554. // Change the path of mp3 to absolute.
  5555. // The first regexp deals with :// urls.
  5556. $content = preg_replace(
  5557. "|(flashvars=\"file=)([^:/]+)/|",
  5558. "$1".api_get_path(
  5559. REL_COURSE_PATH
  5560. ).$courseInfo['path'].'/document/',
  5561. $content
  5562. );
  5563. // The second regexp deals with audio/ urls.
  5564. $content = preg_replace(
  5565. "|(flashvars=\"file=)([^/]+)/|",
  5566. "$1".api_get_path(
  5567. REL_COURSE_PATH
  5568. ).$courseInfo['path'].'/document/$2/',
  5569. $content
  5570. );
  5571. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5572. $content = str_replace(
  5573. '</body>',
  5574. '<style type="text/css">body{}</style></body>',
  5575. $content
  5576. );
  5577. }
  5578. if (!file_exists($filepath . $filename)) {
  5579. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5580. fputs($fp, $content);
  5581. fclose($fp);
  5582. $file_size = filesize($filepath . $filename);
  5583. $save_file_path = $dir.$filename;
  5584. $document_id = add_document(
  5585. $courseInfo,
  5586. $save_file_path,
  5587. 'file',
  5588. $file_size,
  5589. $tmp_filename
  5590. );
  5591. if ($document_id) {
  5592. api_item_property_update(
  5593. $courseInfo,
  5594. TOOL_DOCUMENT,
  5595. $document_id,
  5596. 'DocumentAdded',
  5597. api_get_user_id(),
  5598. null,
  5599. null,
  5600. null,
  5601. null,
  5602. api_get_session_id()
  5603. );
  5604. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5605. $new_title = $originalTitle;
  5606. if ($new_comment || $new_title) {
  5607. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5608. $ct = '';
  5609. if ($new_comment)
  5610. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5611. if ($new_title)
  5612. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5613. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5614. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5615. Database::query($sql);
  5616. }
  5617. }
  5618. return $document_id;
  5619. }
  5620. }
  5621. }
  5622. /**
  5623. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5624. * @param array $_course array
  5625. * @return void
  5626. */
  5627. public function edit_document($_course)
  5628. {
  5629. $course_id = api_get_course_int_id();
  5630. global $_configuration;
  5631. // Please, do not modify this dirname formatting.
  5632. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5633. if (strstr($dir, '..'))
  5634. $dir = '/';
  5635. if ($dir[0] == '.')
  5636. $dir = substr($dir, 1);
  5637. if ($dir[0] != '/')
  5638. $dir = '/' . $dir;
  5639. if ($dir[strlen($dir) - 1] != '/')
  5640. $dir .= '/';
  5641. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5642. if (!is_dir($filepath)) {
  5643. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5644. $dir = '/';
  5645. }
  5646. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5647. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5648. $document_id = intval($_POST['path']);
  5649. $sql = "SELECT path FROM " . $table_doc . "
  5650. WHERE c_id = $course_id AND id = " . $document_id;
  5651. $res = Database::query($sql);
  5652. $row = Database :: fetch_array($res);
  5653. $content = stripslashes($_POST['content_lp']);
  5654. $file = $filepath . $row['path'];
  5655. if ($fp = @ fopen($file, 'w')) {
  5656. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5657. // Change the path of mp3 to absolute.
  5658. // The first regexp deals with :// urls.
  5659. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5660. // The second regexp deals with audio/ urls.
  5661. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5662. fputs($fp, $content);
  5663. fclose($fp);
  5664. $sql = "UPDATE " . $table_doc ." SET
  5665. title='".Database::escape_string($_POST['title'])."'
  5666. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5667. Database::query($sql);
  5668. }
  5669. }
  5670. }
  5671. /**
  5672. * Displays the selected item, with a panel for manipulating the item
  5673. * @param int $item_id
  5674. * @param string $msg
  5675. * @return string
  5676. */
  5677. public function display_item($item_id, $msg = null, $show_actions = true)
  5678. {
  5679. $course_id = api_get_course_int_id();
  5680. $return = '';
  5681. if (is_numeric($item_id)) {
  5682. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5683. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5684. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5685. $result = Database::query($sql);
  5686. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5687. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5688. // Prevents wrong parent selection for document, see Bug#1251.
  5689. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5690. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5691. }
  5692. if ($show_actions) {
  5693. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5694. }
  5695. $return .= '<div style="padding:10px;">';
  5696. if ($msg != '')
  5697. $return .= $msg;
  5698. $return .= '<h3>'.$row['title'].'</h3>';
  5699. switch ($row['item_type']) {
  5700. case TOOL_QUIZ:
  5701. if (!empty($row['path'])) {
  5702. $exercise = new Exercise();
  5703. $exercise->read($row['path']);
  5704. $return .= $exercise->description.'<br />';
  5705. }
  5706. break;
  5707. case TOOL_DOCUMENT:
  5708. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5709. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5710. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5711. $result = Database::query($sql_doc);
  5712. $path_file = Database::result($result, 0, 0);
  5713. $path_parts = pathinfo($path_file);
  5714. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5715. if (in_array($path_parts['extension'], array(
  5716. 'html',
  5717. 'txt',
  5718. 'png',
  5719. 'jpg',
  5720. 'JPG',
  5721. 'jpeg',
  5722. 'JPEG',
  5723. 'gif',
  5724. 'swf'
  5725. ))) {
  5726. $return .= $this->display_document($row['path'], true, true);
  5727. }
  5728. break;
  5729. }
  5730. $return .= '</div>';
  5731. }
  5732. }
  5733. return $return;
  5734. }
  5735. /**
  5736. * Shows the needed forms for editing a specific item
  5737. * @param int $item_id
  5738. * @return string
  5739. */
  5740. public function display_edit_item($item_id)
  5741. {
  5742. $course_id = api_get_course_int_id();
  5743. $return = '';
  5744. if (is_numeric($item_id)) {
  5745. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5746. $sql = "SELECT * FROM $tbl_lp_item
  5747. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5748. $res = Database::query($sql);
  5749. $row = Database::fetch_array($res);
  5750. switch ($row['item_type']) {
  5751. case 'dokeos_chapter' :
  5752. case 'dir' :
  5753. case 'asset' :
  5754. case 'sco' :
  5755. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5756. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5757. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5758. } else {
  5759. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5760. }
  5761. break;
  5762. case TOOL_DOCUMENT :
  5763. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5764. $sql = "SELECT lp.*, doc.path as dir
  5765. FROM " . $tbl_lp_item . " as lp
  5766. LEFT JOIN " . $tbl_doc . " as doc
  5767. ON doc.id = lp.path
  5768. WHERE
  5769. lp.c_id = $course_id AND
  5770. doc.c_id = $course_id AND
  5771. lp.id = " . intval($item_id);
  5772. $res_step = Database::query($sql);
  5773. $row_step = Database :: fetch_array($res_step);
  5774. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5775. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5776. break;
  5777. case TOOL_LINK :
  5778. $link_id = (string) $row['path'];
  5779. if (ctype_digit($link_id)) {
  5780. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5781. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5782. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5783. $res_link = Database::query($sql_select);
  5784. $row_link = Database :: fetch_array($res_link);
  5785. if (is_array($row_link)) {
  5786. $row['url'] = $row_link['url'];
  5787. }
  5788. }
  5789. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5790. $return .= $this->display_link_form('edit', $item_id, $row);
  5791. break;
  5792. case 'dokeos_module' :
  5793. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5794. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5795. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5796. } else {
  5797. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5798. }
  5799. break;
  5800. case TOOL_QUIZ :
  5801. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5802. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5803. break;
  5804. case TOOL_HOTPOTATOES :
  5805. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5806. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5807. break;
  5808. case TOOL_STUDENTPUBLICATION :
  5809. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5810. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5811. break;
  5812. case TOOL_FORUM :
  5813. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5814. $return .= $this->display_forum_form('edit', $item_id, $row);
  5815. break;
  5816. case TOOL_THREAD :
  5817. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5818. $return .= $this->display_thread_form('edit', $item_id, $row);
  5819. break;
  5820. }
  5821. }
  5822. return $return;
  5823. }
  5824. /**
  5825. * Function that displays a list with al the resources that
  5826. * could be added to the learning path
  5827. * @return string
  5828. */
  5829. public function display_resources()
  5830. {
  5831. $course_code = api_get_course_id();
  5832. // Get all the docs.
  5833. $documents = $this->get_documents(true);
  5834. // Get all the exercises.
  5835. $exercises = $this->get_exercises();
  5836. // Get all the links.
  5837. $links = $this->get_links();
  5838. // Get al the student publications.
  5839. $works = $this->get_student_publications();
  5840. // Get al the forums.
  5841. $forums = $this->get_forums(null, $course_code);
  5842. $headers = array(
  5843. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5844. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5845. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5846. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5847. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5848. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5849. );
  5850. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5851. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5852. echo Display::tabs(
  5853. $headers,
  5854. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5855. );
  5856. return true;
  5857. }
  5858. /**
  5859. * Returns the extension of a document
  5860. * @param string filename
  5861. * @return string Extension (part after the last dot)
  5862. */
  5863. public function get_extension($filename)
  5864. {
  5865. $explode = explode('.', $filename);
  5866. return $explode[count($explode) - 1];
  5867. }
  5868. /**
  5869. * Displays a document by id
  5870. *
  5871. * @param int $id
  5872. * @return string
  5873. */
  5874. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5875. {
  5876. $_course = api_get_course_info();
  5877. $course_id = api_get_course_int_id();
  5878. $return = '';
  5879. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5880. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5881. WHERE c_id = ".$course_id." AND id = " . $id;
  5882. $res_doc = Database::query($sql_doc);
  5883. $row_doc = Database :: fetch_array($res_doc);
  5884. // TODO: Add a path filter.
  5885. if ($iframe) {
  5886. $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>';
  5887. } else {
  5888. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5889. }
  5890. return $return;
  5891. }
  5892. /**
  5893. * Return HTML form to add/edit a quiz
  5894. * @param string Action (add/edit)
  5895. * @param integer Item ID if already exists
  5896. * @param mixed Extra information (quiz ID if integer)
  5897. * @return string HTML form
  5898. */
  5899. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5900. {
  5901. $course_id = api_get_course_int_id();
  5902. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5903. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5904. if ($id != 0 && is_array($extra_info)) {
  5905. $item_title = $extra_info['title'];
  5906. $item_description = $extra_info['description'];
  5907. } elseif (is_numeric($extra_info)) {
  5908. $sql = "SELECT title, description
  5909. FROM " . $tbl_quiz . "
  5910. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5911. $result = Database::query($sql);
  5912. $row = Database::fetch_array($result);
  5913. $item_title = $row['title'];
  5914. $item_description = $row['description'];
  5915. } else {
  5916. $item_title = '';
  5917. $item_description = '';
  5918. }
  5919. $item_title = Security::remove_XSS($item_title);
  5920. $item_description = Security::remove_XSS($item_description);
  5921. $legend = '<legend>';
  5922. if ($id != 0 && is_array($extra_info))
  5923. $parent = $extra_info['parent_item_id'];
  5924. else
  5925. $parent = 0;
  5926. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5927. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5928. $result = Database::query($sql);
  5929. $arrLP = array ();
  5930. while ($row = Database :: fetch_array($result)) {
  5931. $arrLP[] = array (
  5932. 'id' => $row['id'],
  5933. 'item_type' => $row['item_type'],
  5934. 'title' => $row['title'],
  5935. 'path' => $row['path'],
  5936. 'description' => $row['description'],
  5937. 'parent_item_id' => $row['parent_item_id'],
  5938. 'previous_item_id' => $row['previous_item_id'],
  5939. 'next_item_id' => $row['next_item_id'],
  5940. 'display_order' => $row['display_order'],
  5941. 'max_score' => $row['max_score'],
  5942. 'min_score' => $row['min_score'],
  5943. 'mastery_score' => $row['mastery_score'],
  5944. 'prerequisite' => $row['prerequisite'],
  5945. 'max_time_allowed' => $row['max_time_allowed']
  5946. );
  5947. }
  5948. $this->tree_array($arrLP);
  5949. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5950. unset ($this->arrMenu);
  5951. if ($action == 'add') {
  5952. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5953. } elseif ($action == 'move') {
  5954. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5955. } else {
  5956. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5957. }
  5958. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5959. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5960. }
  5961. $legend .= '</legend>';
  5962. $return = '';
  5963. $return .= '<div class="sectioncomment">';
  5964. $return .= '<form method="POST">';
  5965. $return .= $legend;
  5966. $return .= '<table class="lp_form">';
  5967. if ($action != 'move') {
  5968. $return .= '<tr>';
  5969. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5970. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5971. $return .= '</tr>';
  5972. }
  5973. $return .= '<tr>';
  5974. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5975. $return .= '<td class="input">';
  5976. // Select for Parent item, root or chapter
  5977. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5978. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5979. $arrHide = array (
  5980. $id
  5981. );
  5982. for ($i = 0; $i < count($arrLP); $i++) {
  5983. if ($action != 'add') {
  5984. 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)) {
  5985. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5986. } else {
  5987. $arrHide[] = $arrLP[$i]['id'];
  5988. }
  5989. } else {
  5990. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5991. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5992. }
  5993. }
  5994. if (is_array($arrLP)) {
  5995. reset($arrLP);
  5996. }
  5997. $return .= '</select>';
  5998. $return .= '</td>';
  5999. $return .= '</tr>';
  6000. $return .= '<tr>';
  6001. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6002. $return .= '<td class="input">';
  6003. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6004. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6005. for ($i = 0; $i < count($arrLP); $i++) {
  6006. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6007. if (is_array($extra_info)) {
  6008. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6009. $selected = 'selected="selected" ';
  6010. }
  6011. } elseif ($action == 'add') {
  6012. $selected = 'selected="selected" ';
  6013. } else {
  6014. $selected = '';
  6015. }
  6016. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6017. }
  6018. }
  6019. $return .= '</select>';
  6020. $return .= '</td>';
  6021. $return .= '</tr>';
  6022. if ($action != 'move') {
  6023. $id_prerequisite = 0;
  6024. if (is_array($arrLP)) {
  6025. foreach ($arrLP as $key => $value) {
  6026. if ($value['id'] == $id) {
  6027. $id_prerequisite = $value['prerequisite'];
  6028. break;
  6029. }
  6030. }
  6031. }
  6032. $arrHide = array ();
  6033. for ($i = 0; $i < count($arrLP); $i++) {
  6034. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6035. if (is_array($extra_info)) {
  6036. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6037. $s_selected_position = $arrLP[$i]['id'];
  6038. }
  6039. } elseif ($action == 'add') {
  6040. $s_selected_position = 0;
  6041. }
  6042. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6043. }
  6044. }
  6045. /*// Commented the prerequisites, only visible in edit (exercise).
  6046. $return .= '<tr>';
  6047. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6048. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6049. foreach($arrHide as $key => $value){
  6050. if($key==$s_selected_position && $action == 'add'){
  6051. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6052. }
  6053. elseif($key==$id_prerequisite && $action == 'edit'){
  6054. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6055. }
  6056. else{
  6057. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6058. }
  6059. }
  6060. $return .= "</select></td>";
  6061. */
  6062. $return .= '</tr>';
  6063. /*$return .= '<tr>';
  6064. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6065. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6066. // Remove temporarily the test description.
  6067. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6068. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6069. $return .= '</tr>'; */
  6070. }
  6071. $return .= '<tr>';
  6072. if ($action == 'add') {
  6073. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  6074. } else {
  6075. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  6076. }
  6077. $return .= '</tr>';
  6078. $return .= '</table>';
  6079. if ($action == 'move') {
  6080. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6081. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6082. }
  6083. if (is_numeric($extra_info)) {
  6084. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6085. } elseif (is_array($extra_info)) {
  6086. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6087. }
  6088. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  6089. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6090. $return .= '</form>';
  6091. $return .= '</div>';
  6092. return $return;
  6093. }
  6094. /**
  6095. * Addition of Hotpotatoes tests
  6096. * @param string Action
  6097. * @param integer Internal ID of the item
  6098. * @param mixed Extra information - can be an array with title and description indexes
  6099. * @return string HTML structure to display the hotpotatoes addition formular
  6100. */
  6101. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6102. {
  6103. $course_id = api_get_course_int_id();
  6104. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6105. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6106. if ($id != 0 && is_array($extra_info)) {
  6107. $item_title = stripslashes($extra_info['title']);
  6108. $item_description = stripslashes($extra_info['description']);
  6109. } elseif (is_numeric($extra_info)) {
  6110. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6111. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6112. WHERE
  6113. c_id = ".$course_id." AND
  6114. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6115. id = " . (int) $extra_info . "
  6116. ORDER BY id ASC";
  6117. $res_hot = Database::query($sql);
  6118. $row = Database::fetch_array($res_hot);
  6119. $item_title = $row['title'];
  6120. $item_description = $row['description'];
  6121. if (!empty ($row['comment'])) {
  6122. $item_title = $row['comment'];
  6123. }
  6124. } else {
  6125. $item_title = '';
  6126. $item_description = '';
  6127. }
  6128. if ($id != 0 && is_array($extra_info)) {
  6129. $parent = $extra_info['parent_item_id'];
  6130. } else {
  6131. $parent = 0;
  6132. }
  6133. $sql = "SELECT * FROM $tbl_lp_item
  6134. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6135. $result = Database::query($sql);
  6136. $arrLP = array ();
  6137. while ($row = Database :: fetch_array($result)) {
  6138. $arrLP[] = array (
  6139. 'id' => $row['id'],
  6140. 'item_type' => $row['item_type'],
  6141. 'title' => $row['title'],
  6142. 'path' => $row['path'],
  6143. 'description' => $row['description'],
  6144. 'parent_item_id' => $row['parent_item_id'],
  6145. 'previous_item_id' => $row['previous_item_id'],
  6146. 'next_item_id' => $row['next_item_id'],
  6147. 'display_order' => $row['display_order'],
  6148. 'max_score' => $row['max_score'],
  6149. 'min_score' => $row['min_score'],
  6150. 'mastery_score' => $row['mastery_score'],
  6151. 'prerequisite' => $row['prerequisite'],
  6152. 'max_time_allowed' => $row['max_time_allowed']
  6153. );
  6154. }
  6155. $legend = '<legend>';
  6156. if ($action == 'add')
  6157. $legend .= get_lang('CreateTheExercise');
  6158. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6159. else
  6160. $legend .= get_lang('EditCurrentExecice');
  6161. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6162. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6163. }
  6164. $legend .= '</legend>';
  6165. $return = '<form method="POST">';
  6166. $return .= $legend;
  6167. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6168. $return .= '<tr>';
  6169. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6170. $return .= '<td class="input">';
  6171. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6172. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6173. $arrHide = array (
  6174. $id
  6175. );
  6176. if (count($arrLP) > 0) {
  6177. for ($i = 0; $i < count($arrLP); $i++) {
  6178. if ($action != 'add') {
  6179. 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)) {
  6180. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6181. } else {
  6182. $arrHide[] = $arrLP[$i]['id'];
  6183. }
  6184. } else {
  6185. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6186. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6187. }
  6188. }
  6189. reset($arrLP);
  6190. }
  6191. $return .= '</select>';
  6192. $return .= '</td>';
  6193. $return .= '</tr>';
  6194. $return .= '<tr>';
  6195. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6196. $return .= '<td class="input">';
  6197. $return .= '<select id="previous" name="previous" size="1">';
  6198. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6199. for ($i = 0; $i < count($arrLP); $i++) {
  6200. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6201. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6202. $selected = 'selected="selected" ';
  6203. elseif ($action == 'add') $selected = 'selected="selected" ';
  6204. else
  6205. $selected = '';
  6206. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6207. }
  6208. }
  6209. $return .= '</select>';
  6210. $return .= '</td>';
  6211. $return .= '</tr>';
  6212. if ($action != 'move') {
  6213. $return .= '<tr>';
  6214. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6215. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6216. $return .= '</tr>';
  6217. $id_prerequisite = 0;
  6218. if (is_array($arrLP) && count($arrLP) > 0) {
  6219. foreach ($arrLP as $key => $value) {
  6220. if ($value['id'] == $id) {
  6221. $id_prerequisite = $value['prerequisite'];
  6222. break;
  6223. }
  6224. }
  6225. $arrHide = array ();
  6226. for ($i = 0; $i < count($arrLP); $i++) {
  6227. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6228. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6229. $s_selected_position = $arrLP[$i]['id'];
  6230. elseif ($action == 'add') $s_selected_position = 0;
  6231. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6232. }
  6233. }
  6234. }
  6235. }
  6236. $return .= '<tr>';
  6237. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6238. $return .= '</tr>';
  6239. $return .= '</table>';
  6240. if ($action == 'move') {
  6241. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6242. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6243. }
  6244. if (is_numeric($extra_info)) {
  6245. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6246. } elseif (is_array($extra_info)) {
  6247. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6248. }
  6249. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6250. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6251. $return .= '</form>';
  6252. return $return;
  6253. }
  6254. /**
  6255. * Return the form to display the forum edit/add option
  6256. * @param string Action (add/edit)
  6257. * @param integer ID of the lp_item if already exists
  6258. * @param mixed Forum ID or title
  6259. * @return string HTML form
  6260. */
  6261. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6262. {
  6263. $course_id = api_get_course_int_id();
  6264. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6265. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6266. if ($id != 0 && is_array($extra_info)) {
  6267. $item_title = stripslashes($extra_info['title']);
  6268. } elseif (is_numeric($extra_info)) {
  6269. $sql = "SELECT forum_title as title, forum_comment as comment
  6270. FROM " . $tbl_forum . "
  6271. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6272. $result = Database::query($sql);
  6273. $row = Database :: fetch_array($result);
  6274. $item_title = $row['title'];
  6275. $item_description = $row['comment'];
  6276. } else {
  6277. $item_title = '';
  6278. $item_description = '';
  6279. }
  6280. $legend = '<legend>';
  6281. if ($id != 0 && is_array($extra_info)) {
  6282. $parent = $extra_info['parent_item_id'];
  6283. } else {
  6284. $parent = 0;
  6285. }
  6286. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6287. WHERE
  6288. c_id = ".$course_id." AND
  6289. lp_id = " . $this->lp_id;
  6290. $result = Database::query($sql);
  6291. $arrLP = array();
  6292. while ($row = Database :: fetch_array($result)) {
  6293. $arrLP[] = array (
  6294. 'id' => $row['id'],
  6295. 'item_type' => $row['item_type'],
  6296. 'title' => $row['title'],
  6297. 'path' => $row['path'],
  6298. 'description' => $row['description'],
  6299. 'parent_item_id' => $row['parent_item_id'],
  6300. 'previous_item_id' => $row['previous_item_id'],
  6301. 'next_item_id' => $row['next_item_id'],
  6302. 'display_order' => $row['display_order'],
  6303. 'max_score' => $row['max_score'],
  6304. 'min_score' => $row['min_score'],
  6305. 'mastery_score' => $row['mastery_score'],
  6306. 'prerequisite' => $row['prerequisite']
  6307. );
  6308. }
  6309. $this->tree_array($arrLP);
  6310. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6311. unset($this->arrMenu);
  6312. if ($action == 'add')
  6313. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6314. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6315. else
  6316. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6317. $legend .= '</legend>';
  6318. $return = '<div class="sectioncomment">';
  6319. $return .= '<form method="POST">';
  6320. $return .= $legend;
  6321. $return .= '<table class="lp_form">';
  6322. if ($action != 'move') {
  6323. $return .= '<tr>';
  6324. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6325. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6326. $return .= '</tr>';
  6327. }
  6328. $return .= '<tr>';
  6329. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6330. $return .= '<td class="input">';
  6331. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6332. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6333. $arrHide = array(
  6334. $id
  6335. );
  6336. //$parent_item_id = $_SESSION['parent_item_id'];
  6337. for ($i = 0; $i < count($arrLP); $i++) {
  6338. if ($action != 'add') {
  6339. 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)) {
  6340. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6341. } else {
  6342. $arrHide[] = $arrLP[$i]['id'];
  6343. }
  6344. } else {
  6345. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6346. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6347. }
  6348. }
  6349. if (is_array($arrLP)) {
  6350. reset($arrLP);
  6351. }
  6352. $return .= '</select>';
  6353. $return .= '</td>';
  6354. $return .= '</tr>';
  6355. $return .= '<tr>';
  6356. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6357. $return .= '<td class="input">';
  6358. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6359. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6360. for ($i = 0; $i < count($arrLP); $i++) {
  6361. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6362. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6363. $selected = 'selected="selected" ';
  6364. elseif ($action == 'add') $selected = 'selected="selected" ';
  6365. else
  6366. $selected = '';
  6367. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6368. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6369. }
  6370. }
  6371. $return .= '</select>';
  6372. $return .= '</td>';
  6373. $return .= '</tr>';
  6374. if ($action != 'move') {
  6375. $return .= '<tr>';
  6376. $return .= '</tr>';
  6377. $id_prerequisite = 0;
  6378. if (is_array($arrLP)) {
  6379. foreach ($arrLP as $key => $value) {
  6380. if ($value['id'] == $id) {
  6381. $id_prerequisite = $value['prerequisite'];
  6382. break;
  6383. }
  6384. }
  6385. }
  6386. $arrHide = array();
  6387. for ($i = 0; $i < count($arrLP); $i++) {
  6388. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6389. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6390. $s_selected_position = $arrLP[$i]['id'];
  6391. elseif ($action == 'add') $s_selected_position = 0;
  6392. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6393. }
  6394. }
  6395. $return .= '</tr>';
  6396. }
  6397. $return .= '<tr>';
  6398. if ($action == 'add') {
  6399. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6400. } else {
  6401. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6402. }
  6403. $return .= '</tr>';
  6404. $return .= '</table>';
  6405. if ($action == 'move') {
  6406. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6407. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6408. }
  6409. if (is_numeric($extra_info)) {
  6410. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6411. } elseif (is_array($extra_info)) {
  6412. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6413. }
  6414. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6415. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6416. $return .= '</form>';
  6417. $return .= '</div>';
  6418. return $return;
  6419. }
  6420. /**
  6421. * Return HTML form to add/edit forum threads
  6422. * @param string Action (add/edit)
  6423. * @param integer Item ID if already exists in learning path
  6424. * @param mixed Extra information (thread ID if integer)
  6425. * @return string HTML form
  6426. */
  6427. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6428. {
  6429. $course_id = api_get_course_int_id();
  6430. if (empty($course_id)) {
  6431. return null;
  6432. }
  6433. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6434. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6435. if ($id != 0 && is_array($extra_info)) {
  6436. $item_title = stripslashes($extra_info['title']);
  6437. } elseif (is_numeric($extra_info)) {
  6438. $sql = "SELECT thread_title as title FROM $tbl_forum
  6439. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6440. $result = Database::query($sql);
  6441. $row = Database :: fetch_array($result);
  6442. $item_title = $row['title'];
  6443. $item_description = '';
  6444. } else {
  6445. $item_title = '';
  6446. $item_description = '';
  6447. }
  6448. $return = null;
  6449. if ($id != 0 && is_array($extra_info)) {
  6450. $parent = $extra_info['parent_item_id'];
  6451. } else {
  6452. $parent = 0;
  6453. }
  6454. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6455. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6456. $result = Database::query($sql);
  6457. $arrLP = array ();
  6458. while ($row = Database :: fetch_array($result)) {
  6459. $arrLP[] = array (
  6460. 'id' => $row['id'],
  6461. 'item_type' => $row['item_type'],
  6462. 'title' => $row['title'],
  6463. 'path' => $row['path'],
  6464. 'description' => $row['description'],
  6465. 'parent_item_id' => $row['parent_item_id'],
  6466. 'previous_item_id' => $row['previous_item_id'],
  6467. 'next_item_id' => $row['next_item_id'],
  6468. 'display_order' => $row['display_order'],
  6469. 'max_score' => $row['max_score'],
  6470. 'min_score' => $row['min_score'],
  6471. 'mastery_score' => $row['mastery_score'],
  6472. 'prerequisite' => $row['prerequisite']
  6473. );
  6474. }
  6475. $this->tree_array($arrLP);
  6476. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6477. unset ($this->arrMenu);
  6478. $return .= '<form method="POST">';
  6479. if ($action == 'add')
  6480. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6481. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6482. else
  6483. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6484. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6485. $return .= '<tr>';
  6486. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6487. $return .= '<td class="input">';
  6488. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6489. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6490. $arrHide = array (
  6491. $id
  6492. );
  6493. for ($i = 0; $i < count($arrLP); $i++) {
  6494. if ($action != 'add') {
  6495. 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)) {
  6496. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6497. } else {
  6498. $arrHide[] = $arrLP[$i]['id'];
  6499. }
  6500. } else {
  6501. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6502. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6503. }
  6504. }
  6505. if ($arrLP != null) {
  6506. reset($arrLP);
  6507. }
  6508. $return .= '</select>';
  6509. $return .= '</td>';
  6510. $return .= '</tr>';
  6511. $return .= '<tr>';
  6512. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6513. $return .= '<td class="input">';
  6514. $return .= '<select id="previous" name="previous" size="1">';
  6515. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6516. for ($i = 0; $i < count($arrLP); $i++) {
  6517. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6518. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6519. $selected = 'selected="selected" ';
  6520. elseif ($action == 'add') $selected = 'selected="selected" ';
  6521. else
  6522. $selected = '';
  6523. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6524. }
  6525. }
  6526. $return .= '</select>';
  6527. $return .= '</td>';
  6528. $return .= '</tr>';
  6529. if ($action != 'move') {
  6530. $return .= '<tr>';
  6531. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6532. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6533. $return .= '</tr>';
  6534. $return .= '<tr>';
  6535. $return .= '</tr>';
  6536. $id_prerequisite = 0;
  6537. if ($arrLP != null) {
  6538. foreach ($arrLP as $key => $value) {
  6539. if ($value['id'] == $id) {
  6540. $id_prerequisite = $value['prerequisite'];
  6541. break;
  6542. }
  6543. }
  6544. }
  6545. $arrHide = array();
  6546. for ($i = 0; $i < count($arrLP); $i++) {
  6547. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6548. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6549. $s_selected_position = $arrLP[$i]['id'];
  6550. elseif ($action == 'add') $s_selected_position = 0;
  6551. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6552. }
  6553. }
  6554. $return .= '<tr>';
  6555. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6556. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6557. foreach ($arrHide as $key => $value) {
  6558. if ($key == $s_selected_position && $action == 'add') {
  6559. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6560. }
  6561. elseif ($key == $id_prerequisite && $action == 'edit') {
  6562. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6563. } else {
  6564. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6565. }
  6566. }
  6567. $return .= "</select></td>";
  6568. $return .= '</tr>';
  6569. }
  6570. $return .= '<tr>';
  6571. $return .= '<td></td><td>
  6572. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6573. $return .= '</tr>';
  6574. $return .= '</table>';
  6575. if ($action == 'move') {
  6576. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6577. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6578. }
  6579. if (is_numeric($extra_info)) {
  6580. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6581. }
  6582. elseif (is_array($extra_info)) {
  6583. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6584. }
  6585. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6586. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6587. $return .= '</form>';
  6588. $return .= '</div>';
  6589. return $return;
  6590. }
  6591. /**
  6592. * Return the HTML form to display an item (generally a section/module item)
  6593. * @param string Item type (module/dokeos_module)
  6594. * @param string Title (optional, only when creating)
  6595. * @param string Action ('add'/'edit')
  6596. * @param integer lp_item ID
  6597. * @param mixed Extra info
  6598. * @return string HTML form
  6599. */
  6600. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6601. {
  6602. $course_id = api_get_course_int_id();
  6603. $_course = api_get_course_info();
  6604. global $charset;
  6605. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6606. if ($id != 0 && is_array($extra_info)) {
  6607. $item_title = $extra_info['title'];
  6608. $item_description = $extra_info['description'];
  6609. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6610. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6611. } else {
  6612. $item_title = '';
  6613. $item_description = '';
  6614. $item_path_fck = '';
  6615. }
  6616. if ($id != 0 && is_array($extra_info)) {
  6617. $parent = $extra_info['parent_item_id'];
  6618. } else {
  6619. $parent = 0;
  6620. }
  6621. $id = intval($id);
  6622. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6623. WHERE
  6624. c_id = ".$course_id." AND
  6625. lp_id = " . $this->lp_id . " AND
  6626. id != $id";
  6627. if ($item_type == 'module')
  6628. $sql .= " AND parent_item_id = 0";
  6629. $result = Database::query($sql);
  6630. $arrLP = array ();
  6631. while ($row = Database :: fetch_array($result)) {
  6632. $arrLP[] = array(
  6633. 'id' => $row['id'],
  6634. 'item_type' => $row['item_type'],
  6635. 'title' => $row['title'],
  6636. 'path' => $row['path'],
  6637. 'description' => $row['description'],
  6638. 'parent_item_id' => $row['parent_item_id'],
  6639. 'previous_item_id' => $row['previous_item_id'],
  6640. 'next_item_id' => $row['next_item_id'],
  6641. 'max_score' => $row['max_score'],
  6642. 'min_score' => $row['min_score'],
  6643. 'mastery_score' => $row['mastery_score'],
  6644. 'prerequisite' => $row['prerequisite'],
  6645. 'display_order' => $row['display_order']
  6646. );
  6647. }
  6648. $this->tree_array($arrLP);
  6649. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6650. unset ($this->arrMenu);
  6651. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6652. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6653. $form = new FormValidator('form', 'POST', $url);
  6654. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6655. $defaults['description'] = $item_description;
  6656. $form->addElement('header', $title);
  6657. //$arrHide = array($id);
  6658. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6659. $arrHide[0]['padding'] = 3;
  6660. $charset = api_get_system_encoding();
  6661. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6662. for ($i = 0; $i < count($arrLP); $i++) {
  6663. if ($action != 'add') {
  6664. 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)) {
  6665. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6666. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6667. if ($parent == $arrLP[$i]['id']) {
  6668. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6669. }
  6670. }
  6671. } else {
  6672. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6673. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6674. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6675. if ($parent == $arrLP[$i]['id']) {
  6676. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6677. }
  6678. }
  6679. }
  6680. }
  6681. if ($action != 'move') {
  6682. $form->addElement('text', 'title', get_lang('Title'));
  6683. $form->applyFilter('title', 'html_filter');
  6684. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6685. } else {
  6686. $form->addElement('hidden', 'title');
  6687. }
  6688. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6689. foreach ($arrHide as $key => $value) {
  6690. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6691. }
  6692. if (!empty($s_selected_parent)) {
  6693. $parent_select->setSelected($s_selected_parent);
  6694. }
  6695. }
  6696. if (is_array($arrLP)) {
  6697. reset($arrLP);
  6698. }
  6699. $arrHide = array();
  6700. // POSITION
  6701. for ($i = 0; $i < count($arrLP); $i++) {
  6702. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6703. //this is the same!
  6704. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6705. $s_selected_position = $arrLP[$i]['id'];
  6706. } elseif ($action == 'add') {
  6707. $s_selected_position = $arrLP[$i]['id'];
  6708. }
  6709. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6710. }
  6711. }
  6712. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6713. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6714. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6715. foreach ($arrHide as $key => $value) {
  6716. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6717. }
  6718. if (!empty ($s_selected_position)) {
  6719. $position->setSelected($s_selected_position);
  6720. }
  6721. if (is_array($arrLP)) {
  6722. reset($arrLP);
  6723. }
  6724. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6725. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6726. $form->addElement('hidden', 'parent', '0');
  6727. }
  6728. //fix in order to use the tab
  6729. if ($item_type == 'chapter') {
  6730. $form->addElement('hidden', 'type', 'chapter');
  6731. }
  6732. $extension = null;
  6733. if (!empty($item_path)) {
  6734. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6735. }
  6736. //assets can't be modified
  6737. //$item_type == 'asset' ||
  6738. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6739. if ($item_type == 'sco') {
  6740. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6741. }
  6742. $renderer = $form->defaultRenderer();
  6743. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6744. $relative_prefix = '';
  6745. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6746. 'Width' => '100%',
  6747. 'Height' => '500',
  6748. 'FullPage' => true,
  6749. 'CreateDocumentDir' => $relative_prefix,
  6750. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6751. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6752. );
  6753. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6754. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6755. //$defaults['content_lp'] = file_get_contents($item_path);
  6756. $defaults['content_lp'] = file_get_contents($content_path);
  6757. }
  6758. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6759. $form->addElement('hidden', 'post_time', time());
  6760. $form->setDefaults($defaults);
  6761. return $form->return_form();
  6762. }
  6763. /**
  6764. * Returns the form to update or create a document
  6765. * @param string Action (add/edit)
  6766. * @param integer ID of the lp_item (if already exists)
  6767. * @param mixed Integer if document ID, string if info ('new')
  6768. * @return string HTML form
  6769. */
  6770. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6771. {
  6772. $course_id = api_get_course_int_id();
  6773. $_course = api_get_course_info();
  6774. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6775. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6776. $no_display_edit_textarea = false;
  6777. $item_description = '';
  6778. //If action==edit document
  6779. //We don't display the document form if it's not an editable document (html or txt file)
  6780. if ($action == "edit") {
  6781. if (is_array($extra_info)) {
  6782. $path_parts = pathinfo($extra_info['dir']);
  6783. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6784. $no_display_edit_textarea = true;
  6785. }
  6786. }
  6787. }
  6788. $no_display_add = false;
  6789. // If action==add an existing document
  6790. // We don't display the document form if it's not an editable document (html or txt file).
  6791. if ($action == "add") {
  6792. if (is_numeric($extra_info)) {
  6793. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6794. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6795. $result = Database::query($sql_doc);
  6796. $path_file = Database :: result($result, 0, 0);
  6797. $path_parts = pathinfo($path_file);
  6798. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6799. $no_display_add = true;
  6800. }
  6801. }
  6802. }
  6803. if ($id != 0 && is_array($extra_info)) {
  6804. $item_title = stripslashes($extra_info['title']);
  6805. $item_description = stripslashes($extra_info['description']);
  6806. $item_terms = stripslashes($extra_info['terms']);
  6807. if (empty ($item_title)) {
  6808. $path_parts = pathinfo($extra_info['path']);
  6809. $item_title = stripslashes($path_parts['filename']);
  6810. }
  6811. } elseif (is_numeric($extra_info)) {
  6812. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6813. WHERE
  6814. c_id = ".$course_id." AND
  6815. id = " . intval($extra_info);
  6816. $result = Database::query($sql_doc);
  6817. $row = Database::fetch_array($result);
  6818. $item_title = $row['title'];
  6819. $item_title = str_replace('_', ' ', $item_title);
  6820. if (empty ($item_title)) {
  6821. $path_parts = pathinfo($row['path']);
  6822. $item_title = stripslashes($path_parts['filename']);
  6823. }
  6824. } else {
  6825. $item_title = '';
  6826. $item_description = '';
  6827. }
  6828. $return = '<legend>';
  6829. if ($id != 0 && is_array($extra_info)) {
  6830. $parent = $extra_info['parent_item_id'];
  6831. } else {
  6832. $parent = 0;
  6833. }
  6834. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6835. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6836. $result = Database::query($sql);
  6837. $arrLP = array ();
  6838. while ($row = Database :: fetch_array($result)) {
  6839. $arrLP[] = array(
  6840. 'id' => $row['id'],
  6841. 'item_type' => $row['item_type'],
  6842. 'title' => $row['title'],
  6843. 'path' => $row['path'],
  6844. 'description' => $row['description'],
  6845. 'parent_item_id' => $row['parent_item_id'],
  6846. 'previous_item_id' => $row['previous_item_id'],
  6847. 'next_item_id' => $row['next_item_id'],
  6848. 'display_order' => $row['display_order'],
  6849. 'max_score' => $row['max_score'],
  6850. 'min_score' => $row['min_score'],
  6851. 'mastery_score' => $row['mastery_score'],
  6852. 'prerequisite' => $row['prerequisite']
  6853. );
  6854. }
  6855. $this->tree_array($arrLP);
  6856. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6857. unset ($this->arrMenu);
  6858. if ($action == 'add') {
  6859. $return .= get_lang('CreateTheDocument');
  6860. } elseif ($action == 'move') {
  6861. $return .= get_lang('MoveTheCurrentDocument');
  6862. } else {
  6863. $return .= get_lang('EditTheCurrentDocument');
  6864. }
  6865. $return .= '</legend>';
  6866. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6867. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6868. }
  6869. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6870. $defaults['title'] = Security :: remove_XSS($item_title);
  6871. if (empty($item_title)) {
  6872. $defaults['title'] = Security::remove_XSS($item_title);
  6873. }
  6874. $defaults['description'] = $item_description;
  6875. $form->addElement('html', $return);
  6876. if ($action != 'move') {
  6877. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6878. $form->applyFilter('title', 'html_filter');
  6879. }
  6880. $arrHide[0]['value'] = $this->name;
  6881. $arrHide[0]['padding'] = 3;
  6882. for ($i = 0; $i < count($arrLP); $i++) {
  6883. if ($action != 'add') {
  6884. 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)) {
  6885. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6886. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6887. if ($parent == $arrLP[$i]['id']) {
  6888. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6889. }
  6890. }
  6891. } else {
  6892. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6893. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6894. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6895. if ($parent == $arrLP[$i]['id']) {
  6896. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6897. }
  6898. }
  6899. }
  6900. }
  6901. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="form-control" id="idParent" " onchange="javascript: load_cbo(this.value);"');
  6902. $my_count=0;
  6903. foreach ($arrHide as $key => $value) {
  6904. if ($my_count!=0) {
  6905. // The LP name is also the first section and is not in the same charset like the other sections.
  6906. $value['value'] = Security :: remove_XSS($value['value']);
  6907. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6908. } else {
  6909. $value['value'] = Security :: remove_XSS($value['value']);
  6910. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6911. }
  6912. $my_count++;
  6913. }
  6914. if (!empty($id)) {
  6915. $parent_select->setSelected($parent);
  6916. } else {
  6917. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  6918. $parent_select->setSelected($parent_item_id);
  6919. }
  6920. if (is_array($arrLP)) {
  6921. reset($arrLP);
  6922. }
  6923. $arrHide = array();
  6924. $s_selected_position = null;
  6925. //POSITION
  6926. for ($i = 0; $i < count($arrLP); $i++) {
  6927. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6928. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6929. $s_selected_position = $arrLP[$i]['id'];
  6930. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6931. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6932. }
  6933. }
  6934. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="form-control"');
  6935. $position->addOption(get_lang('FirstPosition'), 0);
  6936. foreach ($arrHide as $key => $value) {
  6937. $padding = isset($value['padding']) ? $value['padding']: 0;
  6938. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6939. }
  6940. $position->setSelected($s_selected_position);
  6941. if (is_array($arrLP)) {
  6942. reset($arrLP);
  6943. }
  6944. if ($action != 'move') {
  6945. $id_prerequisite = 0;
  6946. if (is_array($arrLP)) {
  6947. foreach ($arrLP as $key => $value) {
  6948. if ($value['id'] == $id) {
  6949. $id_prerequisite = $value['prerequisite'];
  6950. break;
  6951. }
  6952. }
  6953. }
  6954. $arrHide = array();
  6955. for ($i = 0; $i < count($arrLP); $i++) {
  6956. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6957. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6958. $s_selected_position = $arrLP[$i]['id'];
  6959. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6960. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6961. }
  6962. }
  6963. if (!$no_display_add) {
  6964. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  6965. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  6966. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $edit == 'true')) {
  6967. if (isset ($_POST['content']))
  6968. $content = stripslashes($_POST['content']);
  6969. elseif (is_array($extra_info)) {
  6970. //If it's an html document or a text file
  6971. if (!$no_display_edit_textarea) {
  6972. $content = $this->display_document($extra_info['path'], false, false);
  6973. }
  6974. } elseif (is_numeric($extra_info))
  6975. $content = $this->display_document($extra_info, false, false);
  6976. else
  6977. $content = '';
  6978. if (!$no_display_edit_textarea) {
  6979. // We need to calculate here some specific settings for the online editor.
  6980. // The calculated settings work for documents in the Documents tool
  6981. // (on the root or in subfolders).
  6982. // For documents in native scorm packages it is unclear whether the
  6983. // online editor should be activated or not.
  6984. // A new document, it is in the root of the repository.
  6985. $relative_path = '';
  6986. $relative_prefix = '';
  6987. if (is_array($extra_info) && $extra_info != 'new') {
  6988. // The document already exists. Whe have to determine its relative path towards the repository root.
  6989. $relative_path = explode('/', $extra_info['dir']);
  6990. $cnt = count($relative_path) - 2;
  6991. if ($cnt < 0) {
  6992. $cnt = 0;
  6993. }
  6994. $relative_prefix = str_repeat('../', $cnt);
  6995. $relative_path = array_slice($relative_path, 1, $cnt);
  6996. $relative_path = implode('/', $relative_path);
  6997. if (strlen($relative_path) > 0) {
  6998. $relative_path = $relative_path . '/';
  6999. }
  7000. } else {
  7001. $result = $this->generate_lp_folder($_course);
  7002. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7003. $relative_prefix = '../../';
  7004. }
  7005. $editor_config = array(
  7006. 'ToolbarSet'=> 'LearningPathDocuments',
  7007. 'Width' => '100%',
  7008. 'Height' => '500',
  7009. 'FullPage' => true,
  7010. 'CreateDocumentDir' => $relative_prefix,
  7011. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  7012. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  7013. );
  7014. if ($_GET['action'] == 'add_item') {
  7015. $class = 'add';
  7016. $text = get_lang('LPCreateDocument');
  7017. } else {
  7018. if ($_GET['action'] == 'edit_item') {
  7019. $class = 'save';
  7020. $text = get_lang('SaveDocument');
  7021. }
  7022. }
  7023. $form->addButtonSave($text, 'submit_button');
  7024. $renderer = $form->defaultRenderer();
  7025. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7026. $form->addElement('html', '<div class="editor-lp">');
  7027. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7028. $form->addElement('html', '</div>');
  7029. $defaults['content_lp'] = $content;
  7030. }
  7031. } elseif (is_numeric($extra_info)) {
  7032. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7033. $return = $this->display_document($extra_info, true, true, true);
  7034. $form->addElement('html', $return);
  7035. }
  7036. }
  7037. }
  7038. if ($action == 'move') {
  7039. $form->addElement('hidden', 'title', $item_title);
  7040. $form->addElement('hidden', 'description', $item_description);
  7041. }
  7042. if (is_numeric($extra_info)) {
  7043. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7044. $form->addElement('hidden', 'path', $extra_info);
  7045. } elseif (is_array($extra_info)) {
  7046. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7047. $form->addElement('hidden', 'path', $extra_info['path']);
  7048. }
  7049. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7050. $form->addElement('hidden', 'post_time', time());
  7051. $form->setDefaults($defaults);
  7052. return $form->return_form();
  7053. }
  7054. /**
  7055. * Return HTML form to add/edit a link item
  7056. * @param string $action (add/edit)
  7057. * @param integer $id Item ID if exists
  7058. * @param mixed $extra_info
  7059. * @return string HTML form
  7060. */
  7061. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7062. {
  7063. $course_id = api_get_course_int_id();
  7064. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7065. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7066. if ($id != 0 && is_array($extra_info)) {
  7067. $item_title = stripslashes($extra_info['title']);
  7068. $item_description = stripslashes($extra_info['description']);
  7069. $item_url = stripslashes($extra_info['url']);
  7070. } elseif (is_numeric($extra_info)) {
  7071. $extra_info = intval($extra_info);
  7072. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7073. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7074. $result = Database::query($sql);
  7075. $row = Database :: fetch_array($result);
  7076. $item_title = $row['title'];
  7077. $item_description = $row['description'];
  7078. $item_url = $row['url'];
  7079. } else {
  7080. $item_title = '';
  7081. $item_description = '';
  7082. $item_url = '';
  7083. }
  7084. $legend = '<legend>';
  7085. if ($id != 0 && is_array($extra_info)) {
  7086. $parent = $extra_info['parent_item_id'];
  7087. } else {
  7088. $parent = 0;
  7089. }
  7090. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7091. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7092. $result = Database::query($sql);
  7093. $arrLP = array();
  7094. while ($row = Database :: fetch_array($result)) {
  7095. $arrLP[] = array(
  7096. 'id' => $row['id'],
  7097. 'item_type' => $row['item_type'],
  7098. 'title' => $row['title'],
  7099. 'path' => $row['path'],
  7100. 'description' => $row['description'],
  7101. 'parent_item_id' => $row['parent_item_id'],
  7102. 'previous_item_id' => $row['previous_item_id'],
  7103. 'next_item_id' => $row['next_item_id'],
  7104. 'display_order' => $row['display_order'],
  7105. 'max_score' => $row['max_score'],
  7106. 'min_score' => $row['min_score'],
  7107. 'mastery_score' => $row['mastery_score'],
  7108. 'prerequisite' => $row['prerequisite']
  7109. );
  7110. }
  7111. $this->tree_array($arrLP);
  7112. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7113. unset ($this->arrMenu);
  7114. if ($action == 'add')
  7115. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  7116. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  7117. else
  7118. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7119. $legend .= '</legend>';
  7120. $return = '<div class="sectioncomment">';
  7121. $return .= '<form method="POST">';
  7122. $return .= $legend;
  7123. $return .= '<table>';
  7124. if ($action != 'move') {
  7125. $return .= '<tr>';
  7126. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7127. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7128. $return .= '</tr>';
  7129. }
  7130. $return .= '<tr>';
  7131. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7132. $return .= '<td class="input">';
  7133. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7134. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7135. $arrHide = array(
  7136. $id
  7137. );
  7138. $parent_item_id = $_SESSION['parent_item_id'];
  7139. for ($i = 0; $i < count($arrLP); $i++) {
  7140. if ($action != 'add') {
  7141. 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)) {
  7142. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7143. } else {
  7144. $arrHide[] = $arrLP[$i]['id'];
  7145. }
  7146. } else {
  7147. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7148. $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>';
  7149. }
  7150. }
  7151. if (is_array($arrLP)) {
  7152. reset($arrLP);
  7153. }
  7154. $return .= '</select>';
  7155. $return .= '</td>';
  7156. $return .= '</tr>';
  7157. $return .= '<tr>';
  7158. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7159. $return .= '<td class="input">';
  7160. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7161. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7162. for ($i = 0; $i < count($arrLP); $i++) {
  7163. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7164. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7165. $selected = 'selected="selected" ';
  7166. elseif ($action == 'add')
  7167. $selected = 'selected="selected" ';
  7168. else
  7169. $selected = '';
  7170. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7171. }
  7172. }
  7173. $return .= '</select>';
  7174. $return .= '</td>';
  7175. $return .= '</tr>';
  7176. if ($action != 'move') {
  7177. $return .= '<tr>';
  7178. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7179. $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>';
  7180. $return .= '</tr>';
  7181. $id_prerequisite = 0;
  7182. if (is_array($arrLP)) {
  7183. foreach ($arrLP as $key => $value) {
  7184. if ($value['id'] == $id) {
  7185. $id_prerequisite = $value['prerequisite'];
  7186. break;
  7187. }
  7188. }
  7189. }
  7190. $arrHide = array();
  7191. for ($i = 0; $i < count($arrLP); $i++) {
  7192. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7193. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7194. $s_selected_position = $arrLP[$i]['id'];
  7195. elseif ($action == 'add') $s_selected_position = 0;
  7196. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7197. }
  7198. }
  7199. $return .= '</tr>';
  7200. }
  7201. $return .= '<tr>';
  7202. if ($action == 'add') {
  7203. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7204. } else {
  7205. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7206. }
  7207. $return .= '</tr>';
  7208. $return .= '</table>';
  7209. if ($action == 'move') {
  7210. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7211. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7212. }
  7213. if (is_numeric($extra_info)) {
  7214. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7215. } elseif (is_array($extra_info)) {
  7216. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7217. }
  7218. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7219. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7220. $return .= '</form>';
  7221. $return .= '</div>';
  7222. return $return;
  7223. }
  7224. /**
  7225. * Return HTML form to add/edit a student publication (work)
  7226. * @param string Action (add/edit)
  7227. * @param integer Item ID if already exists
  7228. * @param mixed Extra info (work ID if integer)
  7229. * @return string HTML form
  7230. */
  7231. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7232. {
  7233. $course_id = api_get_course_int_id();
  7234. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7235. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7236. if ($id != 0 && is_array($extra_info)) {
  7237. $item_title = stripslashes($extra_info['title']);
  7238. $item_description = stripslashes($extra_info['description']);
  7239. } elseif (is_numeric($extra_info)) {
  7240. $extra_info = intval($extra_info);
  7241. $sql = "SELECT title, description
  7242. FROM " . $tbl_publication . "
  7243. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7244. $result = Database::query($sql);
  7245. $row = Database :: fetch_array($result);
  7246. $item_title = $row['title'];
  7247. } else {
  7248. $item_title = get_lang('Student_publication');
  7249. }
  7250. if ($id != 0 && is_array($extra_info)) {
  7251. $parent = $extra_info['parent_item_id'];
  7252. } else {
  7253. $parent = 0;
  7254. }
  7255. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7256. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7257. $result = Database::query($sql);
  7258. $arrLP = array();
  7259. while ($row = Database :: fetch_array($result)) {
  7260. $arrLP[] = array (
  7261. 'id' => $row['id'],
  7262. 'item_type' => $row['item_type'],
  7263. 'title' => $row['title'],
  7264. 'path' => $row['path'],
  7265. 'description' => $row['description'],
  7266. 'parent_item_id' => $row['parent_item_id'],
  7267. 'previous_item_id' => $row['previous_item_id'],
  7268. 'next_item_id' => $row['next_item_id'],
  7269. 'display_order' => $row['display_order'],
  7270. 'max_score' => $row['max_score'],
  7271. 'min_score' => $row['min_score'],
  7272. 'mastery_score' => $row['mastery_score'],
  7273. 'prerequisite' => $row['prerequisite']
  7274. );
  7275. }
  7276. $this->tree_array($arrLP);
  7277. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7278. unset ($this->arrMenu);
  7279. $form = new FormValidator('frm_student_publication', 'post', '#');
  7280. if ($action == 'add') {
  7281. $form->addHeader(get_lang('Student_publication'));
  7282. } elseif ($action == 'move') {
  7283. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7284. } else {
  7285. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7286. }
  7287. if ($action != 'move') {
  7288. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7289. }
  7290. $parentSelect = $form->addSelect(
  7291. 'parent',
  7292. get_lang('Parent'),
  7293. ['0' => $this->name],
  7294. [
  7295. 'onchange' => 'javascript: load_cbo(this.value);',
  7296. 'class' => 'learnpath_item_form',
  7297. 'id' => 'idParent'
  7298. ]
  7299. );
  7300. $arrHide = array (
  7301. $id
  7302. );
  7303. for ($i = 0; $i < count($arrLP); $i++) {
  7304. if ($action != 'add') {
  7305. if (
  7306. (
  7307. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7308. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7309. $arrLP[$i]['item_type'] == 'dir'
  7310. ) &&
  7311. !in_array($arrLP[$i]['id'], $arrHide) &&
  7312. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7313. ) {
  7314. $parentSelect->addOption(
  7315. $arrLP[$i]['title'],
  7316. $arrLP[$i]['id'],
  7317. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7318. );
  7319. if ($parent == $arrLP[$i]['id']) {
  7320. $parentSelect->setSelected($arrLP[$i]['id']);
  7321. }
  7322. } else {
  7323. $arrHide[] = $arrLP[$i]['id'];
  7324. }
  7325. } else {
  7326. if (
  7327. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7328. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7329. ) {
  7330. $parentSelect->addOption(
  7331. $arrLP[$i]['title'],
  7332. $arrLP[$i]['id'],
  7333. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7334. );
  7335. if ($parent == $arrLP[$i]['id']) {
  7336. $parentSelect->setSelected($arrLP[$i]['id']);
  7337. }
  7338. }
  7339. }
  7340. }
  7341. if (is_array($arrLP)) {
  7342. reset($arrLP);
  7343. }
  7344. $previousSelect = $form->addSelect(
  7345. 'previous',
  7346. get_lang('Position'),
  7347. ['0' => get_lang('FirstPosition')],
  7348. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7349. );
  7350. for ($i = 0; $i < count($arrLP); $i++) {
  7351. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7352. $previousSelect->addOption(
  7353. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7354. $arrLP[$i]['id']
  7355. );
  7356. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7357. $previousSelect->setSelected($arrLP[$i]['id']);
  7358. } elseif ($action == 'add') {
  7359. $previousSelect->setSelected($arrLP[$i]['id']);
  7360. }
  7361. }
  7362. }
  7363. if ($action != 'move') {
  7364. $id_prerequisite = 0;
  7365. if (is_array($arrLP)) {
  7366. foreach ($arrLP as $key => $value) {
  7367. if ($value['id'] == $id) {
  7368. $id_prerequisite = $value['prerequisite'];
  7369. break;
  7370. }
  7371. }
  7372. }
  7373. $arrHide = array ();
  7374. for ($i = 0; $i < count($arrLP); $i++) {
  7375. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7376. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7377. $s_selected_position = $arrLP[$i]['id'];
  7378. elseif ($action == 'add') $s_selected_position = 0;
  7379. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7380. }
  7381. }
  7382. }
  7383. if ($action == 'add') {
  7384. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7385. } else {
  7386. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7387. }
  7388. if ($action == 'move') {
  7389. $form->addHidden('title', $item_title);
  7390. $form->addHidden('description', $item_description);
  7391. }
  7392. if (is_numeric($extra_info)) {
  7393. $form->addHidden('path', $extra_info);
  7394. } elseif (is_array($extra_info)) {
  7395. $form->addHidden('path', $extra_info['path']);
  7396. }
  7397. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7398. $form->addHidden('post_time', time());
  7399. $form->setDefaults(['title' => $item_title]);
  7400. $return = '<div class="sectioncomment">';
  7401. $return .= $form->returnForm();
  7402. $return .= '</div>';
  7403. return $return;
  7404. }
  7405. /**
  7406. * Displays the menu for manipulating a step
  7407. *
  7408. * @param $item_id
  7409. * @param string $item_type
  7410. * @return string
  7411. */
  7412. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7413. {
  7414. $_course = api_get_course_info();
  7415. $course_id = api_get_course_int_id();
  7416. $course_code = api_get_course_id();
  7417. $return = '<div class="actions">';
  7418. switch ($item_type) {
  7419. case 'dokeos_chapter' :
  7420. case 'chapter' :
  7421. // Commented the message cause should not show it.
  7422. //$lang = get_lang('TitleManipulateChapter');
  7423. break;
  7424. case 'dokeos_module' :
  7425. case 'module' :
  7426. // Commented the message cause should not show it.
  7427. //$lang = get_lang('TitleManipulateModule');
  7428. break;
  7429. case TOOL_DOCUMENT :
  7430. // Commented the message cause should not show it.
  7431. //$lang = get_lang('TitleManipulateDocument');
  7432. break;
  7433. case TOOL_LINK :
  7434. case 'link' :
  7435. // Commented the message cause should not show it.
  7436. //$lang = get_lang('TitleManipulateLink');
  7437. break;
  7438. case TOOL_QUIZ :
  7439. // Commented the message cause should not show it.
  7440. //$lang = get_lang('TitleManipulateQuiz');
  7441. break;
  7442. case TOOL_STUDENTPUBLICATION :
  7443. // Commented the message cause should not show it.
  7444. //$lang = get_lang('TitleManipulateStudentPublication');
  7445. break;
  7446. }
  7447. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7448. $item_id = intval($item_id);
  7449. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7450. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7451. $result = Database::query($sql);
  7452. $row = Database::fetch_assoc($result);
  7453. $audio_player = null;
  7454. // We display an audio player if needed.
  7455. if (!empty($row['audio'])) {
  7456. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7457. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7458. </div>';
  7459. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7460. $audio_player .= '<script>
  7461. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7462. s1.addParam("allowscriptaccess","always");
  7463. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7464. s1.write("container");
  7465. </script>';
  7466. }
  7467. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7468. $return .= Display::url(
  7469. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7470. $url.'&action=edit_item&path_item=' . $row['path']
  7471. );
  7472. $return .= Display::url(
  7473. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7474. $url.'&action=move_item'
  7475. );
  7476. // Commented for now as prerequisites cannot be added to chapters.
  7477. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7478. $return .= Display::url(
  7479. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7480. $url.'&action=edit_item_prereq'
  7481. );
  7482. }
  7483. $return .= Display::url(
  7484. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7485. $url.'&action=delete_item'
  7486. );
  7487. if ($item_type == TOOL_HOTPOTATOES ) {
  7488. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7489. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7490. }
  7491. if ($item_type == TOOL_DOCUMENT ) {
  7492. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7493. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7494. }
  7495. $return .= '</div>';
  7496. if (!empty($audio_player)) {
  7497. $return .= '<br />'.$audio_player;
  7498. }
  7499. return $return;
  7500. }
  7501. /**
  7502. * Creates the javascript needed for filling up the checkboxes without page reload
  7503. * @return string
  7504. */
  7505. public function get_js_dropdown_array()
  7506. {
  7507. $course_id = api_get_course_int_id();
  7508. $return = 'var child_name = new Array();' . "\n";
  7509. $return .= 'var child_value = new Array();' . "\n\n";
  7510. $return .= 'child_name[0] = new Array();' . "\n";
  7511. $return .= 'child_value[0] = new Array();' . "\n\n";
  7512. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7513. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7514. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7515. ORDER BY display_order ASC";
  7516. $res_zero = Database::query($sql_zero);
  7517. $i = 0;
  7518. while ($row_zero = Database :: fetch_array($res_zero)) {
  7519. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7520. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7521. }
  7522. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7523. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7524. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7525. }
  7526. $return .= "\n";
  7527. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7528. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7529. $res = Database::query($sql);
  7530. while ($row = Database :: fetch_array($res)) {
  7531. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7532. WHERE
  7533. c_id = ".$course_id." AND
  7534. parent_item_id = " . $row['id'] . "
  7535. ORDER BY display_order ASC";
  7536. $res_parent = Database::query($sql_parent);
  7537. $i = 0;
  7538. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7539. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7540. while ($row_parent = Database :: fetch_array($res_parent)) {
  7541. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7542. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7543. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7544. }
  7545. $return .= "\n";
  7546. }
  7547. return $return;
  7548. }
  7549. /**
  7550. * Display the form to allow moving an item
  7551. * @param integer Item ID
  7552. * @return string HTML form
  7553. */
  7554. public function display_move_item($item_id)
  7555. {
  7556. $course_id = api_get_course_int_id();
  7557. $return = '';
  7558. if (is_numeric($item_id)) {
  7559. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7560. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7561. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7562. $res = Database::query($sql);
  7563. $row = Database :: fetch_array($res);
  7564. switch ($row['item_type']) {
  7565. case 'dokeos_chapter' :
  7566. case 'dir' :
  7567. case 'asset' :
  7568. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7569. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7570. break;
  7571. case 'dokeos_module' :
  7572. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7573. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7574. break;
  7575. case TOOL_DOCUMENT :
  7576. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7577. $return .= $this->display_document_form('move', $item_id, $row);
  7578. break;
  7579. case TOOL_LINK :
  7580. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7581. $return .= $this->display_link_form('move', $item_id, $row);
  7582. break;
  7583. case TOOL_HOTPOTATOES :
  7584. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7585. $return .= $this->display_link_form('move', $item_id, $row);
  7586. break;
  7587. case TOOL_QUIZ :
  7588. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7589. $return .= $this->display_quiz_form('move', $item_id, $row);
  7590. break;
  7591. case TOOL_STUDENTPUBLICATION :
  7592. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7593. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7594. break;
  7595. case TOOL_FORUM :
  7596. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7597. $return .= $this->display_forum_form('move', $item_id, $row);
  7598. break;
  7599. case TOOL_THREAD :
  7600. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7601. $return .= $this->display_forum_form('move', $item_id, $row);
  7602. break;
  7603. }
  7604. }
  7605. return $return;
  7606. }
  7607. /**
  7608. * Displays a basic form on the overview page for changing the item title and the item description.
  7609. * @param string $item_type
  7610. * @param string $title
  7611. * @param array $data
  7612. * @return string
  7613. */
  7614. public function display_item_small_form($item_type, $title = '', $data = array())
  7615. {
  7616. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7617. $form = new FormValidator('small_form', 'post', $url);
  7618. $form->addElement('header', $title);
  7619. $form->addElement('text', 'title', get_lang('Title'));
  7620. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7621. $form->addElement('hidden', 'id', $data['id']);
  7622. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7623. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7624. $form->setDefaults(array('title' => $data['title']));
  7625. return $form->toHtml();
  7626. }
  7627. /**
  7628. * Return HTML form to allow prerequisites selection
  7629. * @todo use FormValidator
  7630. * @param integer Item ID
  7631. * @return string HTML form
  7632. */
  7633. public function display_item_prerequisites_form($item_id)
  7634. {
  7635. $course_id = api_get_course_int_id();
  7636. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7637. $item_id = intval($item_id);
  7638. /* Current prerequisite */
  7639. $sql = "SELECT * FROM $tbl_lp_item
  7640. WHERE c_id = $course_id AND id = " . $item_id;
  7641. $result = Database::query($sql);
  7642. $row = Database::fetch_array($result);
  7643. $prerequisiteId = $row['prerequisite'];
  7644. $return = '<legend>';
  7645. $return .= get_lang('AddEditPrerequisites');
  7646. $return .= '</legend>';
  7647. $return .= '<form method="POST">';
  7648. $return .= '<table class="data_table">';
  7649. $return .= '<tr>';
  7650. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7651. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7652. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7653. $return .= '</tr>';
  7654. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7655. $return .= '<tr >';
  7656. $return .= '<td colspan="3" class="radio">';
  7657. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7658. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7659. $return .= '</tr>';
  7660. $sql = "SELECT * FROM $tbl_lp_item
  7661. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7662. $result = Database::query($sql);
  7663. $arrLP = array();
  7664. $selectedMinScore = array();
  7665. $selectedMaxScore = array();
  7666. while ($row = Database :: fetch_array($result)) {
  7667. if ($row['id'] == $item_id) {
  7668. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7669. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7670. }
  7671. $arrLP[] = array(
  7672. 'id' => $row['id'],
  7673. 'item_type' => $row['item_type'],
  7674. 'title' => $row['title'],
  7675. 'ref' => $row['ref'],
  7676. 'description' => $row['description'],
  7677. 'parent_item_id' => $row['parent_item_id'],
  7678. 'previous_item_id' => $row['previous_item_id'],
  7679. 'next_item_id' => $row['next_item_id'],
  7680. 'max_score' => $row['max_score'],
  7681. 'min_score' => $row['min_score'],
  7682. 'mastery_score' => $row['mastery_score'],
  7683. 'prerequisite' => $row['prerequisite'],
  7684. 'next_item_id' => $row['next_item_id'],
  7685. 'display_order' => $row['display_order'],
  7686. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7687. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7688. );
  7689. }
  7690. $this->tree_array($arrLP);
  7691. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7692. unset($this->arrMenu);
  7693. for ($i = 0; $i < count($arrLP); $i++) {
  7694. $item = $arrLP[$i];
  7695. if ($item['id'] == $item_id) {
  7696. break;
  7697. }
  7698. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7699. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7700. $return .= '<tr>';
  7701. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7702. $return .= '<label for="id' . $item['id'] . '">';
  7703. $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'] . '" />';
  7704. $icon_name = str_replace(' ', '', $item['item_type']);
  7705. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7706. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7707. } else {
  7708. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7709. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7710. } else {
  7711. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7712. }
  7713. }
  7714. $return .= $item['title'] . '</label>';
  7715. $return .= '</td>';
  7716. if ($item['item_type'] == TOOL_QUIZ) {
  7717. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7718. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7719. $tmp_obj_exercice = new Exercise();
  7720. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7721. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7722. $tmp_obj_lp_item->update_in_bdd();
  7723. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7724. $return .= '<td class="exercise">';
  7725. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7726. $return .= '</td>';
  7727. $return .= '<td class="exercise">';
  7728. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7729. $return .= '</td>';
  7730. }
  7731. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7732. $return .= '<td class="exercise">';
  7733. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7734. $return .= '</td>';
  7735. $return .= '<td class="exercise"">';
  7736. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7737. $return .= '</td>';
  7738. }
  7739. $return .= '</tr>';
  7740. }
  7741. $return .= '<tr>';
  7742. $return .= '</tr>';
  7743. $return .= '</table>';
  7744. $return .= '<div style="padding-top:3px;">';
  7745. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7746. $return .= '</form>';
  7747. return $return;
  7748. }
  7749. /**
  7750. * Return HTML list to allow prerequisites selection for lp
  7751. * @param integer Item ID
  7752. * @return string HTML form
  7753. */
  7754. public function display_lp_prerequisites_list()
  7755. {
  7756. $course_id = api_get_course_int_id();
  7757. $lp_id = $this->lp_id;
  7758. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7759. // get current prerequisite
  7760. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7761. $result = Database::query($sql);
  7762. $row = Database :: fetch_array($result);
  7763. $prerequisiteId = $row['prerequisite'];
  7764. $session_id = api_get_session_id();
  7765. $session_condition = api_get_session_condition($session_id);
  7766. $sql = "SELECT * FROM $tbl_lp
  7767. WHERE c_id = $course_id $session_condition
  7768. ORDER BY display_order ";
  7769. $rs = Database::query($sql);
  7770. $return = '';
  7771. $return .= '<select name="prerequisites" class="form-control">';
  7772. $return .= '<option value="0">'.get_lang('None').'</option>';
  7773. if (Database::num_rows($rs) > 0) {
  7774. while ($row = Database::fetch_array($rs)) {
  7775. if ($row['id'] == $lp_id) {
  7776. continue;
  7777. }
  7778. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7779. }
  7780. }
  7781. $return .= '</select>';
  7782. return $return;
  7783. }
  7784. /**
  7785. * Creates a list with all the documents in it
  7786. * @param bool $showInvisibleFiles
  7787. * @return string
  7788. */
  7789. public function get_documents($showInvisibleFiles = false)
  7790. {
  7791. $course_info = api_get_course_info();
  7792. $sessionId = api_get_session_id();
  7793. $document_tree = DocumentManager::get_document_preview(
  7794. $course_info,
  7795. $this->lp_id,
  7796. null,
  7797. $sessionId,
  7798. true,
  7799. null,
  7800. null,
  7801. $showInvisibleFiles,
  7802. true
  7803. );
  7804. return $document_tree;
  7805. }
  7806. /**
  7807. * Creates a list with all the exercises (quiz) in it
  7808. * @return string
  7809. */
  7810. public function get_exercises()
  7811. {
  7812. $course_id = api_get_course_int_id();
  7813. // New for hotpotatoes.
  7814. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7815. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7816. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7817. $session_id = api_get_session_id();
  7818. $condition_session = api_get_session_condition($session_id);
  7819. $sql_quiz = "SELECT * FROM $tbl_quiz
  7820. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7821. ORDER BY title ASC";
  7822. $sql_hot = "SELECT * FROM $tbl_doc
  7823. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7824. ORDER BY id ASC";
  7825. $res_quiz = Database::query($sql_quiz);
  7826. $res_hot = Database::query($sql_hot);
  7827. $return = '<ul class="lp_resource">';
  7828. $return .= '<li class="lp_resource_element">';
  7829. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7830. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7831. get_lang('NewExercise') . '</a>';
  7832. $return .= '</li>';
  7833. // Display hotpotatoes
  7834. while ($row_hot = Database :: fetch_array($res_hot)) {
  7835. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7836. $return .= '<a class="moved" href="#">';
  7837. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7838. $return .= '</a> ';
  7839. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7840. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7841. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7842. $return .= '</li>';
  7843. }
  7844. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7845. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7846. $return .= '<a class="moved" href="#">';
  7847. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7848. $return .= '</a> ';
  7849. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7850. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7851. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7852. '</a>';
  7853. $return .= '</li>';
  7854. }
  7855. $return .= '</ul>';
  7856. return $return;
  7857. }
  7858. /**
  7859. * Creates a list with all the links in it
  7860. * @return string
  7861. */
  7862. public function get_links()
  7863. {
  7864. $selfUrl = api_get_self();
  7865. $courseIdReq = api_get_cidreq();
  7866. $course = api_get_course_info();
  7867. $course_id = $course['real_id'];
  7868. $tbl_link = Database::get_course_table(TABLE_LINK);
  7869. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7870. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7871. $session_id = api_get_session_id();
  7872. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7873. $sql = "SELECT link.id as link_id,
  7874. link.title as link_title,
  7875. link.category_id as category_id,
  7876. link_category.category_title as category_title
  7877. FROM $tbl_link as link
  7878. LEFT JOIN $linkCategoryTable as link_category
  7879. ON link.category_id = link_category.id
  7880. WHERE link.c_id = ".$course_id." $condition_session
  7881. ORDER BY link_category.category_title ASC, link.title ASC";
  7882. $links = Database::query($sql);
  7883. $categorizedLinks = array();
  7884. $categories = array();
  7885. while ($link = Database :: fetch_array($links)) {
  7886. if (!$link['category_id']) {
  7887. $link['category_title'] = get_lang('Uncategorized');
  7888. }
  7889. $categories[$link['category_id']] = $link['category_title'];
  7890. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7891. }
  7892. $linksHtmlCode =
  7893. '<script>
  7894. function toggle_tool(tool, id){
  7895. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7896. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7897. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7898. } else {
  7899. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7900. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7901. }
  7902. }
  7903. </script>
  7904. <ul class="lp_resource">
  7905. <li class="lp_resource_element">
  7906. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7907. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7908. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7909. </li>';
  7910. foreach ($categorizedLinks as $categoryId => $links) {
  7911. $linkNodes = null;
  7912. foreach ($links as $key => $title) {
  7913. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7914. $linkNodes .=
  7915. '<li class="lp_resource_element" data_id="'.$key.
  7916. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7917. <a class="moved" href="#">'.
  7918. $moveEverywhereIcon.
  7919. '</a>
  7920. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7921. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7922. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7923. Security::remove_XSS($title).
  7924. '</a>
  7925. </li>';
  7926. }
  7927. }
  7928. $linksHtmlCode .=
  7929. '<li>
  7930. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7931. style="vertical-align:middle">
  7932. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7933. align="absbottom" />
  7934. </a>
  7935. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7936. </li>
  7937. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7938. }
  7939. $linksHtmlCode .= '</ul>';
  7940. return $linksHtmlCode;
  7941. }
  7942. /**
  7943. * Creates a list with all the student publications in it
  7944. * @return unknown
  7945. */
  7946. public function get_student_publications()
  7947. {
  7948. $return = '<div class="lp_resource" >';
  7949. $return .= '<div class="lp_resource_element">';
  7950. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7951. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7952. $return .= '</div>';
  7953. $return .= '</div>';
  7954. return $return;
  7955. }
  7956. /**
  7957. * Creates a list with all the forums in it
  7958. * @return string
  7959. */
  7960. public function get_forums()
  7961. {
  7962. require_once '../forum/forumfunction.inc.php';
  7963. require_once '../forum/forumconfig.inc.php';
  7964. $a_forums = get_forums();
  7965. $return = '<ul class="lp_resource">';
  7966. //First add link
  7967. $return .= '<li class="lp_resource_element">';
  7968. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7969. $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>';
  7970. $return .= '</li>';
  7971. $return .= '<script>
  7972. function toggle_forum(forum_id){
  7973. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7974. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7975. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7976. } else {
  7977. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7978. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7979. }
  7980. }
  7981. </script>';
  7982. foreach ($a_forums as $forum) {
  7983. if (!empty($forum['forum_id'])) {
  7984. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7985. $return .= '<a class="moved" href="#">';
  7986. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7987. $return .= ' </a>';
  7988. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7989. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7990. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7991. </a>
  7992. <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">' .
  7993. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7994. $return .= '</li>';
  7995. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7996. $a_threads = get_threads($forum['forum_id']);
  7997. if (is_array($a_threads)) {
  7998. foreach ($a_threads as $thread) {
  7999. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8000. $return .= '&nbsp;<a class="moved" href="#">';
  8001. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8002. $return .= ' </a>';
  8003. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8004. $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 . '">' .
  8005. Security :: remove_XSS($thread['thread_title']) . '</a>';
  8006. $return .= '</li>';
  8007. }
  8008. }
  8009. $return .= '</div>';
  8010. }
  8011. }
  8012. $return .= '</ul>';
  8013. return $return;
  8014. }
  8015. /**
  8016. * // TODO: The output encoding should be equal to the system encoding.
  8017. *
  8018. * Exports the learning path as a SCORM package. This is the main function that
  8019. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8020. * whole thing and returns the zip.
  8021. *
  8022. * This method needs to be called in PHP5, as it will fail with non-adequate
  8023. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8024. * you need to call it on a learnpath object.
  8025. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8026. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8027. * path has been modified, it should use the generic method here below.
  8028. * @TODO link this function with the export_lp() function in the same class
  8029. * @param string Optional name of zip file. If none, title of learnpath is
  8030. * domesticated and trailed with ".zip"
  8031. * @return string Returns the zip package string, or null if error
  8032. */
  8033. public function scorm_export()
  8034. {
  8035. $_course = api_get_course_info();
  8036. $course_id = $_course['real_id'];
  8037. // Remove memory and time limits as much as possible as this might be a long process...
  8038. if (function_exists('ini_set')) {
  8039. api_set_memory_limit('128M');
  8040. ini_set('max_execution_time', 600);
  8041. }
  8042. // Create the zip handler (this will remain available throughout the method).
  8043. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8044. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8045. $temp_dir_short = uniqid();
  8046. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8047. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8048. $zip_folder = new PclZip($temp_zip_file);
  8049. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8050. $root_path = $main_path = api_get_path(SYS_PATH);
  8051. $files_cleanup = array();
  8052. // Place to temporarily stash the zip file.
  8053. // create the temp dir if it doesn't exist
  8054. // or do a cleanup before creating the zip file.
  8055. if (!is_dir($temp_zip_dir)) {
  8056. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8057. } else {
  8058. // Cleanup: Check the temp dir for old files and delete them.
  8059. $handle = opendir($temp_zip_dir);
  8060. while (false !== ($file = readdir($handle))) {
  8061. if ($file != '.' && $file != '..') {
  8062. unlink("$temp_zip_dir/$file");
  8063. }
  8064. }
  8065. closedir($handle);
  8066. }
  8067. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8068. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8069. // Remove the possible . at the end of the path.
  8070. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8071. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8072. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8073. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  8074. }
  8075. // Build a dummy imsmanifest structure.
  8076. // Do not add to the zip yet (we still need it).
  8077. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8078. // Aggregation Model official document, section "2.3 Content Packaging".
  8079. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8080. $xmldoc = new DOMDocument('1.0');
  8081. $root = $xmldoc->createElement('manifest');
  8082. $root->setAttribute('identifier', 'SingleCourseManifest');
  8083. $root->setAttribute('version', '1.1');
  8084. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8085. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8086. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8087. $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');
  8088. // Build mandatory sub-root container elements.
  8089. $metadata = $xmldoc->createElement('metadata');
  8090. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8091. $metadata->appendChild($md_schema);
  8092. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8093. $metadata->appendChild($md_schemaversion);
  8094. $root->appendChild($metadata);
  8095. $organizations = $xmldoc->createElement('organizations');
  8096. $resources = $xmldoc->createElement('resources');
  8097. // Build the only organization we will use in building our learnpaths.
  8098. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8099. $organization = $xmldoc->createElement('organization');
  8100. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8101. // To set the title of the SCORM entity (=organization), we take the name given
  8102. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8103. // learning path charset) as it is the encoding that defines how it is stored
  8104. // in the database. Then we convert it to HTML entities again as the "&" character
  8105. // alone is not authorized in XML (must be &amp;).
  8106. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8107. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8108. $organization->appendChild($org_title);
  8109. $folder_name = 'document';
  8110. // Removes the learning_path/scorm_folder path when exporting see #4841
  8111. $path_to_remove = null;
  8112. $result = $this->generate_lp_folder($_course);
  8113. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8114. $path_to_remove = 'document'.$result['dir'];
  8115. $path_to_replace = $folder_name.'/';
  8116. }
  8117. //Fixes chamilo scorm exports
  8118. if ($this->ref == 'chamilo_scorm_export') {
  8119. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8120. }
  8121. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8122. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8123. $link_updates = array();
  8124. $links_to_create = array();
  8125. foreach ($this->items as $index => $item) {
  8126. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8127. // Get included documents from this item.
  8128. if ($item->type == 'sco') {
  8129. $inc_docs = $item->get_resources_from_source(
  8130. null,
  8131. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8132. );
  8133. } else {
  8134. $inc_docs = $item->get_resources_from_source();
  8135. }
  8136. // Give a child element <item> to the <organization> element.
  8137. $my_item_id = $item->get_id();
  8138. $my_item = $xmldoc->createElement('item');
  8139. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8140. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8141. $my_item->setAttribute('isvisible', 'true');
  8142. // Give a child element <title> to the <item> element.
  8143. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8144. $my_item->appendChild($my_title);
  8145. // Give a child element <adlcp:prerequisites> to the <item> element.
  8146. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8147. $my_prereqs->setAttribute('type', 'aicc_script');
  8148. $my_item->appendChild($my_prereqs);
  8149. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8150. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8151. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8152. //$xmldoc->createElement('adlcp:timelimitaction','');
  8153. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8154. //$xmldoc->createElement('adlcp:datafromlms','');
  8155. // Give a child element <adlcp:masteryscore> to the <item> element.
  8156. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8157. $my_item->appendChild($my_masteryscore);
  8158. // Attach this item to the organization element or hits parent if there is one.
  8159. if (!empty($item->parent) && $item->parent != 0) {
  8160. $children = $organization->childNodes;
  8161. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8162. if (is_object($possible_parent)) {
  8163. $possible_parent->appendChild($my_item);
  8164. } else {
  8165. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8166. }
  8167. } else {
  8168. if ($this->debug > 0) { error_log('No parent'); }
  8169. $organization->appendChild($my_item);
  8170. }
  8171. // Get the path of the file(s) from the course directory root.
  8172. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8173. if (!empty($path_to_remove)) {
  8174. //From docs
  8175. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8176. //From quiz
  8177. if ($this->ref == 'chamilo_scorm_export') {
  8178. $path_to_remove = 'scorm/'.$this->path.'/';
  8179. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8180. }
  8181. } else {
  8182. $my_xml_file_path = $my_file_path;
  8183. }
  8184. $my_sub_dir = dirname($my_file_path);
  8185. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8186. $my_xml_sub_dir = $my_sub_dir;
  8187. // Give a <resource> child to the <resources> element
  8188. $my_resource = $xmldoc->createElement('resource');
  8189. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8190. $my_resource->setAttribute('type', 'webcontent');
  8191. $my_resource->setAttribute('href', $my_xml_file_path);
  8192. // adlcp:scormtype can be either 'sco' or 'asset'.
  8193. if ($item->type == 'sco') {
  8194. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8195. } else {
  8196. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8197. }
  8198. // xml:base is the base directory to find the files declared in this resource.
  8199. $my_resource->setAttribute('xml:base', '');
  8200. // Give a <file> child to the <resource> element.
  8201. $my_file = $xmldoc->createElement('file');
  8202. $my_file->setAttribute('href', $my_xml_file_path);
  8203. $my_resource->appendChild($my_file);
  8204. // Dependency to other files - not yet supported.
  8205. $i = 1;
  8206. foreach ($inc_docs as $doc_info) {
  8207. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8208. $my_dep = $xmldoc->createElement('resource');
  8209. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8210. $my_dep->setAttribute('identifier', $res_id);
  8211. $my_dep->setAttribute('type', 'webcontent');
  8212. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8213. $my_dep_file = $xmldoc->createElement('file');
  8214. // Check type of URL.
  8215. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8216. if ($doc_info[1] == 'remote') {
  8217. // Remote file. Save url as is.
  8218. $my_dep_file->setAttribute('href', $doc_info[0]);
  8219. $my_dep->setAttribute('xml:base', '');
  8220. } elseif ($doc_info[1] == 'local') {
  8221. switch ($doc_info[2]) {
  8222. case 'url': // Local URL - save path as url for now, don't zip file.
  8223. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8224. $current_dir = dirname($abs_path);
  8225. $current_dir = str_replace('\\', '/', $current_dir);
  8226. $file_path = realpath($abs_path);
  8227. $file_path = str_replace('\\', '/', $file_path);
  8228. $my_dep_file->setAttribute('href', $file_path);
  8229. $my_dep->setAttribute('xml:base', '');
  8230. if (strstr($file_path, $main_path) !== false) {
  8231. // The calculated real path is really inside Chamilo's root path.
  8232. // Reduce file path to what's under the DocumentRoot.
  8233. $file_path = substr($file_path, strlen($root_path) - 1);
  8234. //echo $file_path;echo '<br /><br />';
  8235. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8236. $zip_files_abs[] = $file_path;
  8237. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8238. $my_dep_file->setAttribute('href', $file_path);
  8239. $my_dep->setAttribute('xml:base', '');
  8240. } elseif (empty($file_path)) {
  8241. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8242. if (strpos($document_root, -1) == '/') {
  8243. $document_root = substr(0, -1, $document_root);
  8244. }*/
  8245. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8246. $file_path = str_replace('//', '/', $file_path);
  8247. if (file_exists($file_path)) {
  8248. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8249. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8250. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8251. $my_dep_file->setAttribute('href', $file_path);
  8252. $my_dep->setAttribute('xml:base', '');
  8253. }
  8254. }
  8255. break;
  8256. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8257. $my_dep_file->setAttribute('href', $doc_info[0]);
  8258. $my_dep->setAttribute('xml:base', '');
  8259. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8260. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8261. $abs_img_path_without_subdir = $doc_info[0];
  8262. $relp = api_get_path(REL_PATH); // The url-append config param.
  8263. $pos = strpos($abs_img_path_without_subdir, $relp);
  8264. if ($pos === 0) {
  8265. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8266. }
  8267. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8268. $file_path = str_replace('\\', '/', $file_path);
  8269. $file_path = str_replace('//', '/', $file_path);
  8270. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8271. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8272. // Check if the current document is in that path.
  8273. if (strstr($file_path, $cur_path) !== false) {
  8274. // The document is in that path, now get the relative path
  8275. // to the containing document.
  8276. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8277. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8278. $relative_path = '';
  8279. if (strstr($file_path, $cur_path) !== false) {
  8280. //$relative_path = substr($file_path, strlen($orig_file_path));
  8281. $relative_path = str_replace($cur_path, '', $file_path);
  8282. $file_path = substr($file_path, strlen($cur_path));
  8283. } else {
  8284. // This case is still a problem as it's difficult to calculate a relative path easily
  8285. // might still generate wrong links.
  8286. //$file_path = substr($file_path,strlen($cur_path));
  8287. // Calculate the directory path to the current file (without trailing slash).
  8288. $my_relative_path = dirname($file_path);
  8289. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8290. $my_relative_file = basename($file_path);
  8291. // Calculate the directory path to the containing file (without trailing slash).
  8292. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8293. $dotdot = '';
  8294. $subdir = '';
  8295. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8296. $my_relative_path2 = dirname($my_relative_path);
  8297. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8298. $my_orig_file_path = dirname($my_orig_file_path);
  8299. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8300. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8301. $dotdot += '../';
  8302. $my_relative_path = $my_relative_path2;
  8303. }
  8304. $relative_path = $dotdot.$subdir.$my_relative_file;
  8305. }
  8306. // Put the current document in the zip (this array is the array
  8307. // that will manage documents already in the course folder - relative).
  8308. $zip_files[] = $file_path;
  8309. // Update the links to the current document in the containing document (make them relative).
  8310. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8311. $my_dep_file->setAttribute('href', $file_path);
  8312. $my_dep->setAttribute('xml:base', '');
  8313. } elseif (strstr($file_path, $main_path) !== false) {
  8314. // The calculated real path is really inside Chamilo's root path.
  8315. // Reduce file path to what's under the DocumentRoot.
  8316. $file_path = substr($file_path, strlen($root_path));
  8317. //echo $file_path;echo '<br /><br />';
  8318. //error_log('Reduced path: '.$file_path, 0);
  8319. $zip_files_abs[] = $file_path;
  8320. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8321. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8322. $my_dep->setAttribute('xml:base', '');
  8323. } elseif (empty($file_path)) {
  8324. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8325. if(strpos($document_root,-1) == '/') {
  8326. $document_root = substr(0, -1, $document_root);
  8327. }*/
  8328. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8329. $file_path = str_replace('//', '/', $file_path);
  8330. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8331. $current_dir = dirname($abs_path);
  8332. $current_dir = str_replace('\\', '/', $current_dir);
  8333. if (file_exists($file_path)) {
  8334. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8335. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8336. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8337. $my_dep_file->setAttribute('href','document/'.$file_path);
  8338. $my_dep->setAttribute('xml:base', '');
  8339. }
  8340. }
  8341. break;
  8342. case 'rel':
  8343. // Path relative to the current document.
  8344. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8345. if (substr($doc_info[0], 0, 2) == '..') {
  8346. // Relative path going up.
  8347. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8348. $current_dir = str_replace('\\', '/', $current_dir);
  8349. $file_path = realpath($current_dir.$doc_info[0]);
  8350. $file_path = str_replace('\\', '/', $file_path);
  8351. //error_log($file_path.' <-> '.$main_path,0);
  8352. if (strstr($file_path, $main_path) !== false) {
  8353. // The calculated real path is really inside Chamilo's root path.
  8354. // Reduce file path to what's under the DocumentRoot.
  8355. $file_path = substr($file_path, strlen($root_path));
  8356. //error_log('Reduced path: '.$file_path, 0);
  8357. $zip_files_abs[] = $file_path;
  8358. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8359. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8360. $my_dep->setAttribute('xml:base', '');
  8361. }
  8362. } else {
  8363. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8364. $my_dep_file->setAttribute('href', $doc_info[0]);
  8365. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8366. }
  8367. break;
  8368. default:
  8369. $my_dep_file->setAttribute('href', $doc_info[0]);
  8370. $my_dep->setAttribute('xml:base', '');
  8371. break;
  8372. }
  8373. }
  8374. $my_dep->appendChild($my_dep_file);
  8375. $resources->appendChild($my_dep);
  8376. $dependency = $xmldoc->createElement('dependency');
  8377. $dependency->setAttribute('identifierref', $res_id);
  8378. $my_resource->appendChild($dependency);
  8379. $i++;
  8380. }
  8381. $resources->appendChild($my_resource);
  8382. $zip_files[] = $my_file_path;
  8383. } else {
  8384. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8385. switch ($item->type) {
  8386. case TOOL_LINK:
  8387. $my_item = $xmldoc->createElement('item');
  8388. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8389. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8390. $my_item->setAttribute('isvisible', 'true');
  8391. // Give a child element <title> to the <item> element.
  8392. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8393. $my_item->appendChild($my_title);
  8394. // Give a child element <adlcp:prerequisites> to the <item> element.
  8395. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8396. $my_prereqs->setAttribute('type', 'aicc_script');
  8397. $my_item->appendChild($my_prereqs);
  8398. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8399. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8400. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8401. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8402. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8403. //$xmldoc->createElement('adlcp:datafromlms', '');
  8404. // Give a child element <adlcp:masteryscore> to the <item> element.
  8405. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8406. $my_item->appendChild($my_masteryscore);
  8407. // Attach this item to the organization element or its parent if there is one.
  8408. if (!empty($item->parent) && $item->parent != 0) {
  8409. $children = $organization->childNodes;
  8410. for ($i = 0; $i < $children->length; $i++) {
  8411. $item_temp = $children->item($i);
  8412. if ($item_temp -> nodeName == 'item') {
  8413. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8414. $item_temp -> appendChild($my_item);
  8415. }
  8416. }
  8417. }
  8418. } else {
  8419. $organization->appendChild($my_item);
  8420. }
  8421. $my_file_path = 'link_'.$item->get_id().'.html';
  8422. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8423. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8424. $rs = Database::query($sql);
  8425. if ($link = Database :: fetch_array($rs)) {
  8426. $url = $link['url'];
  8427. $title = stripslashes($link['title']);
  8428. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8429. $my_xml_file_path = $my_file_path;
  8430. $my_sub_dir = dirname($my_file_path);
  8431. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8432. $my_xml_sub_dir = $my_sub_dir;
  8433. // Give a <resource> child to the <resources> element.
  8434. $my_resource = $xmldoc->createElement('resource');
  8435. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8436. $my_resource->setAttribute('type', 'webcontent');
  8437. $my_resource->setAttribute('href', $my_xml_file_path);
  8438. // adlcp:scormtype can be either 'sco' or 'asset'.
  8439. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8440. // xml:base is the base directory to find the files declared in this resource.
  8441. $my_resource->setAttribute('xml:base', '');
  8442. // give a <file> child to the <resource> element.
  8443. $my_file = $xmldoc->createElement('file');
  8444. $my_file->setAttribute('href', $my_xml_file_path);
  8445. $my_resource->appendChild($my_file);
  8446. $resources->appendChild($my_resource);
  8447. }
  8448. break;
  8449. case TOOL_QUIZ:
  8450. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8451. $exe = new Exercise();
  8452. $exe->read($exe_id);
  8453. $my_item = $xmldoc->createElement('item');
  8454. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8455. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8456. $my_item->setAttribute('isvisible', 'true');
  8457. // Give a child element <title> to the <item> element.
  8458. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8459. $my_item->appendChild($my_title);
  8460. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8461. //$my_item->appendChild($my_max_score);
  8462. // Give a child element <adlcp:prerequisites> to the <item> element.
  8463. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8464. $my_prereqs->setAttribute('type','aicc_script');
  8465. $my_item->appendChild($my_prereqs);
  8466. // Give a child element <adlcp:masteryscore> to the <item> element.
  8467. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8468. $my_item->appendChild($my_masteryscore);
  8469. // Attach this item to the organization element or hits parent if there is one.
  8470. if (!empty($item->parent) && $item->parent != 0) {
  8471. $children = $organization->childNodes;
  8472. for ($i = 0; $i < $children->length; $i++) {
  8473. $item_temp = $children->item($i);
  8474. if ($item_temp -> nodeName == 'item') {
  8475. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8476. $item_temp -> appendChild($my_item);
  8477. }
  8478. }
  8479. }
  8480. } else {
  8481. $organization->appendChild($my_item);
  8482. }
  8483. // Get the path of the file(s) from the course directory root
  8484. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8485. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8486. // Write the contents of the exported exercise into a (big) html file
  8487. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8488. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8489. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8490. $res = file_put_contents($tmp_file_path, $contents);
  8491. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8492. $files_cleanup[] = $tmp_file_path;
  8493. //error_log($tmp_path); die();
  8494. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8495. $my_xml_file_path = $my_file_path;
  8496. $my_sub_dir = dirname($my_file_path);
  8497. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8498. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8499. $my_xml_sub_dir = $my_sub_dir;
  8500. // Give a <resource> child to the <resources> element.
  8501. $my_resource = $xmldoc->createElement('resource');
  8502. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8503. $my_resource->setAttribute('type', 'webcontent');
  8504. $my_resource->setAttribute('href', $my_xml_file_path);
  8505. // adlcp:scormtype can be either 'sco' or 'asset'.
  8506. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8507. // xml:base is the base directory to find the files declared in this resource.
  8508. $my_resource->setAttribute('xml:base', '');
  8509. // Give a <file> child to the <resource> element.
  8510. $my_file = $xmldoc->createElement('file');
  8511. $my_file->setAttribute('href', $my_xml_file_path);
  8512. $my_resource->appendChild($my_file);
  8513. // Get included docs.
  8514. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8515. // Dependency to other files - not yet supported.
  8516. $i = 1;
  8517. foreach ($inc_docs as $doc_info) {
  8518. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8519. $my_dep = $xmldoc->createElement('resource');
  8520. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8521. $my_dep->setAttribute('identifier', $res_id);
  8522. $my_dep->setAttribute('type', 'webcontent');
  8523. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8524. $my_dep_file = $xmldoc->createElement('file');
  8525. // Check type of URL.
  8526. if ($doc_info[1] == 'remote') {
  8527. // Remote file. Save url as is.
  8528. $my_dep_file->setAttribute('href', $doc_info[0]);
  8529. $my_dep->setAttribute('xml:base', '');
  8530. } elseif ($doc_info[1] == 'local') {
  8531. switch ($doc_info[2]) {
  8532. case 'url': // Local URL - save path as url for now, don't zip file.
  8533. // Save file but as local file (retrieve from URL).
  8534. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8535. $current_dir = dirname($abs_path);
  8536. $current_dir = str_replace('\\', '/', $current_dir);
  8537. $file_path = realpath($abs_path);
  8538. $file_path = str_replace('\\', '/', $file_path);
  8539. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8540. $my_dep->setAttribute('xml:base', '');
  8541. if (strstr($file_path, $main_path) !== false) {
  8542. // The calculated real path is really inside the chamilo root path.
  8543. // Reduce file path to what's under the DocumentRoot.
  8544. $file_path = substr($file_path, strlen($root_path));
  8545. //echo $file_path;echo '<br /><br />';
  8546. //error_log('Reduced path: '.$file_path, 0);
  8547. $zip_files_abs[] = $file_path;
  8548. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8549. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8550. $my_dep->setAttribute('xml:base', '');
  8551. } elseif (empty($file_path)) {
  8552. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8553. if (strpos($document_root,-1) == '/') {
  8554. $document_root = substr(0, -1, $document_root);
  8555. }*/
  8556. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8557. $file_path = str_replace('//', '/', $file_path);
  8558. if (file_exists($file_path)) {
  8559. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8560. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8561. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8562. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8563. $my_dep->setAttribute('xml:base', '');
  8564. }
  8565. }
  8566. break;
  8567. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8568. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8569. $current_dir = str_replace('\\', '/', $current_dir);
  8570. $file_path = realpath($doc_info[0]);
  8571. $file_path = str_replace('\\', '/', $file_path);
  8572. $my_dep_file->setAttribute('href', $file_path);
  8573. $my_dep->setAttribute('xml:base', '');
  8574. if (strstr($file_path,$main_path) !== false) {
  8575. // The calculated real path is really inside the chamilo root path.
  8576. // Reduce file path to what's under the DocumentRoot.
  8577. $file_path = substr($file_path, strlen($root_path));
  8578. //echo $file_path;echo '<br /><br />';
  8579. //error_log('Reduced path: '.$file_path, 0);
  8580. $zip_files_abs[] = $file_path;
  8581. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8582. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8583. $my_dep->setAttribute('xml:base', '');
  8584. } elseif (empty($file_path)) {
  8585. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8586. if (strpos($document_root,-1) == '/') {
  8587. $document_root = substr(0, -1, $document_root);
  8588. }*/
  8589. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8590. $file_path = str_replace('//', '/', $file_path);
  8591. if (file_exists($file_path)) {
  8592. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8593. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8594. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8595. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8596. $my_dep->setAttribute('xml:base', '');
  8597. }
  8598. }
  8599. break;
  8600. 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
  8601. if (substr($doc_info[0], 0, 2) == '..') {
  8602. // Relative path going up.
  8603. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8604. $current_dir = str_replace('\\', '/', $current_dir);
  8605. $file_path = realpath($current_dir.$doc_info[0]);
  8606. $file_path = str_replace('\\', '/', $file_path);
  8607. //error_log($file_path.' <-> '.$main_path, 0);
  8608. if (strstr($file_path, $main_path) !== false) {
  8609. // The calculated real path is really inside Chamilo's root path.
  8610. // Reduce file path to what's under the DocumentRoot.
  8611. $file_path = substr($file_path, strlen($root_path));
  8612. $file_path_dest = $file_path;
  8613. // File path is courses/CHAMILO/document/....
  8614. $info_file_path = explode('/', $file_path);
  8615. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8616. $file_path_dest = 'document/'.$file_path;
  8617. }
  8618. //error_log('Reduced path: '.$file_path, 0);
  8619. $zip_files_abs[] = $file_path;
  8620. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8621. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8622. $my_dep->setAttribute('xml:base', '');
  8623. }
  8624. } else {
  8625. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8626. $my_dep_file->setAttribute('href', $doc_info[0]);
  8627. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8628. }
  8629. break;
  8630. default:
  8631. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8632. $my_dep->setAttribute('xml:base', '');
  8633. break;
  8634. }
  8635. }
  8636. $my_dep->appendChild($my_dep_file);
  8637. $resources->appendChild($my_dep);
  8638. $dependency = $xmldoc->createElement('dependency');
  8639. $dependency->setAttribute('identifierref', $res_id);
  8640. $my_resource->appendChild($dependency);
  8641. $i++;
  8642. }
  8643. $resources->appendChild($my_resource);
  8644. $zip_files[] = $my_file_path;
  8645. break;
  8646. default:
  8647. // Get the path of the file(s) from the course directory root
  8648. $my_file_path = 'non_exportable.html';
  8649. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8650. $my_xml_file_path = $my_file_path;
  8651. $my_sub_dir = dirname($my_file_path);
  8652. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8653. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8654. $my_xml_sub_dir = $my_sub_dir;
  8655. // Give a <resource> child to the <resources> element.
  8656. $my_resource = $xmldoc->createElement('resource');
  8657. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8658. $my_resource->setAttribute('type', 'webcontent');
  8659. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8660. // adlcp:scormtype can be either 'sco' or 'asset'.
  8661. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8662. // xml:base is the base directory to find the files declared in this resource.
  8663. $my_resource->setAttribute('xml:base', '');
  8664. // Give a <file> child to the <resource> element.
  8665. $my_file = $xmldoc->createElement('file');
  8666. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8667. $my_resource->appendChild($my_file);
  8668. $resources->appendChild($my_resource);
  8669. break;
  8670. }
  8671. }
  8672. }
  8673. $organizations->appendChild($organization);
  8674. $root->appendChild($organizations);
  8675. $root->appendChild($resources);
  8676. $xmldoc->appendChild($root);
  8677. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8678. // then add the file to the zip, then destroy the file (this is done automatically).
  8679. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8680. foreach ($zip_files as $file_path) {
  8681. if (empty($file_path)) {
  8682. continue;
  8683. }
  8684. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8685. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8686. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8687. }
  8688. $this->create_path($dest_file);
  8689. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8690. // Check if the file needs a link update.
  8691. if (in_array($file_path, array_keys($link_updates))) {
  8692. $string = file_get_contents($dest_file);
  8693. unlink($dest_file);
  8694. foreach ($link_updates[$file_path] as $old_new) {
  8695. // This is an ugly hack that allows .flv files to be found by the flv player that
  8696. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8697. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8698. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8699. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8700. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8701. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8702. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8703. }
  8704. //Fix to avoid problems with default_course_document
  8705. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8706. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8707. $newDestination = $old_new['dest'];
  8708. } else {
  8709. $newDestination = str_replace('document/', '', $old_new['dest']);
  8710. }
  8711. $string = str_replace($old_new['orig'], $newDestination, $string);
  8712. //Add files inside the HTMLs
  8713. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8714. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8715. if (file_exists($sys_course_path.$new_path)) {
  8716. copy($sys_course_path.$new_path, $destinationFile);
  8717. }
  8718. }
  8719. file_put_contents($dest_file, $string);
  8720. }
  8721. }
  8722. foreach ($zip_files_abs as $file_path) {
  8723. if (empty($file_path)) {
  8724. continue;
  8725. }
  8726. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8727. continue;
  8728. }
  8729. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8730. $this->create_path($dest_file);
  8731. copy($main_path.$file_path, $dest_file);
  8732. // Check if the file needs a link update.
  8733. if (in_array($file_path, array_keys($link_updates))) {
  8734. $string = file_get_contents($dest_file);
  8735. unlink($dest_file);
  8736. foreach ($link_updates[$file_path] as $old_new) {
  8737. // This is an ugly hack that allows .flv files to be found by the flv player that
  8738. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8739. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8740. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8741. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8742. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8743. }
  8744. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8745. }
  8746. file_put_contents($dest_file, $string);
  8747. }
  8748. }
  8749. if (is_array($links_to_create)) {
  8750. foreach ($links_to_create as $file => $link) {
  8751. $file_content = '<!DOCTYPE html><head>
  8752. <meta charset="'.api_get_language_isocode().'" />
  8753. <title>'.$link['title'].'</title>
  8754. </head>
  8755. <body dir="'.api_get_text_direction().'">
  8756. <div style="text-align:center">
  8757. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8758. </body>
  8759. </html>';
  8760. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8761. }
  8762. }
  8763. // Add non exportable message explanation.
  8764. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8765. $file_content = '<!DOCTYPE html><head>
  8766. <meta charset="'.api_get_language_isocode().'" />
  8767. <title>'.$lang_not_exportable.'</title>
  8768. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8769. </head>
  8770. <body dir="'.api_get_text_direction().'">';
  8771. $file_content .=
  8772. <<<EOD
  8773. <style>
  8774. .error-message {
  8775. font-family: arial, verdana, helvetica, sans-serif;
  8776. border-width: 1px;
  8777. border-style: solid;
  8778. left: 50%;
  8779. margin: 10px auto;
  8780. min-height: 30px;
  8781. padding: 5px;
  8782. right: 50%;
  8783. width: 500px;
  8784. background-color: #FFD1D1;
  8785. border-color: #FF0000;
  8786. color: #000;
  8787. }
  8788. </style>
  8789. <body>
  8790. <div class="error-message">
  8791. $lang_not_exportable
  8792. </div>
  8793. </body>
  8794. </html>
  8795. EOD;
  8796. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8797. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8798. }
  8799. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8800. // Add the extra files that go along with a SCORM package.
  8801. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8802. $extra_files = scandir($main_code_path);
  8803. foreach ($extra_files as $extra_file) {
  8804. if (strpos($extra_file, '.') === 0)
  8805. continue;
  8806. else {
  8807. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8808. $this->create_path($dest_file);
  8809. copy($main_code_path.$extra_file, $dest_file);
  8810. }
  8811. }
  8812. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8813. $manifest = @$xmldoc->saveXML();
  8814. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8815. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8816. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8817. // Clean possible temporary files.
  8818. foreach ($files_cleanup as $file) {
  8819. $res = unlink($file);
  8820. if ($res === false) {
  8821. error_log(
  8822. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8823. 0
  8824. );
  8825. }
  8826. }
  8827. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8828. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8829. }
  8830. /**
  8831. * @param int $lp_id
  8832. * @return bool
  8833. */
  8834. public function scorm_export_to_pdf($lp_id)
  8835. {
  8836. $lp_id = intval($lp_id);
  8837. $files_to_export = array();
  8838. $course_data = api_get_course_info($this->cc);
  8839. if (!empty($course_data)) {
  8840. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8841. $list = self::get_flat_ordered_items_list($lp_id);
  8842. if (!empty($list)) {
  8843. foreach ($list as $item_id) {
  8844. $item = $this->items[$item_id];
  8845. switch ($item->type) {
  8846. case 'document':
  8847. //Getting documents from a LP with chamilo documents
  8848. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8849. // Try loading document from the base course.
  8850. if (empty($file_data) && !empty($sessionId)) {
  8851. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8852. }
  8853. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8854. if (file_exists($file_path)) {
  8855. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8856. }
  8857. break;
  8858. case 'asset': //commes from a scorm package generated by chamilo
  8859. case 'sco':
  8860. $file_path = $scorm_path.'/'.$item->path;
  8861. if (file_exists($file_path)) {
  8862. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8863. }
  8864. break;
  8865. case 'dokeos_chapter':
  8866. case 'dir':
  8867. case 'chapter':
  8868. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8869. break;
  8870. }
  8871. }
  8872. }
  8873. $pdf = new PDF();
  8874. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8875. return $result;
  8876. }
  8877. return false;
  8878. }
  8879. /**
  8880. * Temp function to be moved in main_api or the best place around for this.
  8881. * Creates a file path if it doesn't exist
  8882. * @param string $path
  8883. */
  8884. public function create_path($path)
  8885. {
  8886. $path_bits = explode('/', dirname($path));
  8887. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8888. $path_built = IS_WINDOWS_OS ? '' : '/';
  8889. foreach ($path_bits as $bit) {
  8890. if (!empty ($bit)) {
  8891. $new_path = $path_built . $bit;
  8892. if (is_dir($new_path)) {
  8893. $path_built = $new_path . '/';
  8894. } else {
  8895. mkdir($new_path, api_get_permissions_for_new_directories());
  8896. $path_built = $new_path . '/';
  8897. }
  8898. }
  8899. }
  8900. }
  8901. /**
  8902. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8903. * @return boolean The results of the unlink function, or false if there was no image to start with
  8904. */
  8905. public function delete_lp_image()
  8906. {
  8907. $img = $this->get_preview_image();
  8908. if ($img != '') {
  8909. $del_file = $this->get_preview_image_path(null, 'sys');
  8910. if (isset($del_file) && file_exists($del_file)) {
  8911. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8912. if (file_exists($del_file_2)) {
  8913. unlink($del_file_2);
  8914. }
  8915. $this->set_preview_image('');
  8916. return @unlink($del_file);
  8917. }
  8918. }
  8919. return false;
  8920. }
  8921. /**
  8922. * Uploads an author image to the upload/learning_path/images directory
  8923. * @param array The image array, coming from the $_FILES superglobal
  8924. * @return boolean True on success, false on error
  8925. */
  8926. public function upload_image($image_array)
  8927. {
  8928. $image_moved = false;
  8929. if (!empty ($image_array['name'])) {
  8930. $upload_ok = process_uploaded_file($image_array);
  8931. $has_attachment = true;
  8932. } else {
  8933. $image_moved = true;
  8934. }
  8935. if ($upload_ok) {
  8936. if ($has_attachment) {
  8937. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8938. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8939. $updir = $sys_course_path . $courseDir;
  8940. // Try to add an extension to the file if it hasn't one.
  8941. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8942. if (!filter_extension($new_file_name)) {
  8943. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8944. $image_moved = false;
  8945. } else {
  8946. $file_extension = explode('.', $image_array['name']);
  8947. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8948. $filename = uniqid('');
  8949. $new_file_name = $filename.'.'.$file_extension;
  8950. $new_path = $updir.'/'.$new_file_name;
  8951. // Resize the image.
  8952. $temp = new Image($image_array['tmp_name']);
  8953. $temp->resize(104);
  8954. $result = $temp->send_image($new_path);
  8955. // Storing the image filename.
  8956. if ($result) {
  8957. $image_moved = true;
  8958. $this->set_preview_image($new_file_name);
  8959. //Resize to 64px to use on course homepage
  8960. $temp->resize(64);
  8961. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8962. return true;
  8963. }
  8964. }
  8965. }
  8966. }
  8967. return false;
  8968. }
  8969. /**
  8970. * @param int $lp_id
  8971. * @param string $status
  8972. */
  8973. public function set_autolaunch($lp_id, $status)
  8974. {
  8975. $course_id = api_get_course_int_id();
  8976. $lp_id = intval($lp_id);
  8977. $status = intval($status);
  8978. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8979. // Setting everything to autolaunch = 0
  8980. $attributes['autolaunch'] = 0;
  8981. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8982. Database::update($lp_table, $attributes, $where);
  8983. if ($status == 1) {
  8984. //Setting my lp_id to autolaunch = 1
  8985. $attributes['autolaunch'] = 1;
  8986. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8987. Database::update($lp_table, $attributes, $where );
  8988. }
  8989. }
  8990. /**
  8991. * Gets previous_item_id for the next element of the lp_item table
  8992. * @author Isaac flores paz
  8993. * @return integer Previous item ID
  8994. */
  8995. public function select_previous_item_id()
  8996. {
  8997. $course_id = api_get_course_int_id();
  8998. if ($this->debug > 0) {
  8999. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9000. }
  9001. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9002. // Get the max order of the items
  9003. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9004. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9005. $rs_max_order = Database::query($sql_max_order);
  9006. $row_max_order = Database::fetch_object($rs_max_order);
  9007. $max_order = $row_max_order->display_order;
  9008. // Get the previous item ID
  9009. $sql = "SELECT id as previous FROM $table_lp_item
  9010. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9011. $rs_max = Database::query($sql);
  9012. $row_max = Database::fetch_object($rs_max);
  9013. // Return the previous item ID
  9014. return $row_max->previous;
  9015. }
  9016. /**
  9017. * Copies an LP
  9018. */
  9019. public function copy()
  9020. {
  9021. $main_path = api_get_path(SYS_CODE_PATH);
  9022. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9023. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9024. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9025. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9026. //Course builder
  9027. $cb = new CourseBuilder();
  9028. //Setting tools that will be copied
  9029. $cb->set_tools_to_build(array('learnpaths'));
  9030. //Setting elements that will be copied
  9031. $cb->set_tools_specific_id_list(
  9032. array('learnpaths' => array($this->lp_id))
  9033. );
  9034. $course = $cb->build();
  9035. //Course restorer
  9036. $course_restorer = new CourseRestorer($course);
  9037. $course_restorer->set_add_text_in_items(true);
  9038. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9039. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9040. }
  9041. public function verify_document_size($s)
  9042. {
  9043. $post_max = ini_get('post_max_size');
  9044. if (substr($post_max, -1, 1) == 'M') {
  9045. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9046. } elseif (substr($post_max, -1, 1) == 'G') {
  9047. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9048. }
  9049. $upl_max = ini_get('upload_max_filesize');
  9050. if (substr($upl_max, -1, 1) == 'M') {
  9051. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9052. } elseif (substr($upl_max, -1, 1) == 'G') {
  9053. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9054. }
  9055. $documents_total_space = DocumentManager::documents_total_space();
  9056. $course_max_space = DocumentManager::get_course_quota();
  9057. $total_size = filesize($s) + $documents_total_space;
  9058. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9059. return true;
  9060. } else{
  9061. return false;
  9062. }
  9063. }
  9064. /**
  9065. * Clear LP prerequisites
  9066. */
  9067. public function clear_prerequisites()
  9068. {
  9069. $course_id = $this->get_course_int_id();
  9070. if ($this->debug > 0) {
  9071. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9072. }
  9073. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9074. $lp_id = $this->get_id();
  9075. //Cleaning prerequisites
  9076. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9077. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9078. Database::query($sql);
  9079. //Cleaning mastery score for exercises
  9080. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9081. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9082. Database::query($sql);
  9083. }
  9084. public function set_previous_step_as_prerequisite_for_all_items()
  9085. {
  9086. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9087. $course_id = $this->get_course_int_id();
  9088. $lp_id = $this->get_id();
  9089. if (!empty($this->items)) {
  9090. $previous_item_id = null;
  9091. $previous_item_max = 0;
  9092. $previous_item_type = null;
  9093. $last_item_not_chapter = null;
  9094. $last_item_not_chapter_type = null;
  9095. $last_item_not_chapter_max = null;
  9096. foreach ($this->items as $item) {
  9097. // if there was a previous item... (otherwise jump to set it)
  9098. if (!empty($previous_item_id)) {
  9099. $current_item_id = $item->get_id(); //save current id
  9100. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9101. // Current item is not a folder, so it qualifies to get a prerequisites
  9102. if ($last_item_not_chapter_type == 'quiz') {
  9103. // if previous is quiz, mark its max score as default score to be achieved
  9104. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9105. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9106. Database::query($sql);
  9107. }
  9108. // now simply update the prerequisite to set it to the last non-chapter item
  9109. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9110. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9111. Database::query($sql);
  9112. // record item as 'non-chapter' reference
  9113. $last_item_not_chapter = $item->get_id();
  9114. $last_item_not_chapter_type = $item->get_type();
  9115. $last_item_not_chapter_max = $item->get_max();
  9116. }
  9117. } else {
  9118. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9119. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9120. $last_item_not_chapter = $item->get_id();
  9121. $last_item_not_chapter_type = $item->get_type();
  9122. $last_item_not_chapter_max = $item->get_max();
  9123. }
  9124. }
  9125. // Saving the item as "previous item" for the next loop
  9126. $previous_item_id = $item->get_id();
  9127. $previous_item_max = $item->get_max();
  9128. $previous_item_type = $item->get_type();
  9129. }
  9130. }
  9131. }
  9132. /**
  9133. * @param array $params
  9134. */
  9135. public static function createCategory($params)
  9136. {
  9137. $em = Database::getManager();
  9138. $item = new CLpCategory();
  9139. $item->setName($params['name']);
  9140. $item->setCId($params['c_id']);
  9141. $em->persist($item);
  9142. $em->flush();
  9143. }
  9144. /**
  9145. * @param array $params
  9146. */
  9147. public static function updateCategory($params)
  9148. {
  9149. $em = Database::getManager();
  9150. /** @var CLpCategory $item */
  9151. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9152. if ($item) {
  9153. $item->setName($params['name']);
  9154. $em->merge($item);
  9155. $em->flush();
  9156. }
  9157. }
  9158. /**
  9159. * @param int $id
  9160. */
  9161. public static function moveUpCategory($id)
  9162. {
  9163. $em = Database::getManager();
  9164. /** @var CLpCategory $item */
  9165. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9166. if ($item) {
  9167. $position = $item->getPosition() - 1;
  9168. $item->setPosition($position);
  9169. $em->persist($item);
  9170. $em->flush();
  9171. }
  9172. }
  9173. /**
  9174. * @param int $id
  9175. */
  9176. public static function moveDownCategory($id)
  9177. {
  9178. $em = Database::getManager();
  9179. /** @var CLpCategory $item */
  9180. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9181. if ($item) {
  9182. $position = $item->getPosition() + 1;
  9183. $item->setPosition($position);
  9184. $em->persist($item);
  9185. $em->flush();
  9186. }
  9187. }
  9188. /**
  9189. * @param int $courseId
  9190. * @return int|mixed
  9191. */
  9192. public static function getCountCategories($courseId)
  9193. {
  9194. if (empty($course_id)) {
  9195. return 0;
  9196. }
  9197. $em = Database::getManager();
  9198. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9199. $query->setParameter('id', $courseId);
  9200. return $query->getSingleScalarResult();
  9201. }
  9202. /**
  9203. * @param int $courseId
  9204. *
  9205. * @return mixed
  9206. */
  9207. public static function getCategories($courseId)
  9208. {
  9209. $em = Database::getManager();
  9210. //Default behaviour
  9211. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9212. array('cId' => $course_id),
  9213. array('name' => 'ASC')
  9214. );*/
  9215. // Using doctrine extensions
  9216. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9217. array('cId' => $courseId)
  9218. )->getResult();
  9219. return $items;
  9220. }
  9221. /**
  9222. * @param int $id
  9223. *
  9224. * @return mixed
  9225. */
  9226. public static function getCategory($id)
  9227. {
  9228. $em = Database::getManager();
  9229. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9230. return $item;
  9231. }
  9232. /**
  9233. * @param int $courseId
  9234. * @return array
  9235. */
  9236. public static function getCategoryByCourse($courseId)
  9237. {
  9238. $em = Database::getManager();
  9239. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9240. return $items;
  9241. }
  9242. /**
  9243. * @param int $id
  9244. *
  9245. * @return mixed
  9246. */
  9247. public static function deleteCategory($id)
  9248. {
  9249. $em = Database::getManager();
  9250. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9251. if ($item) {
  9252. $courseId = $item->getCId();
  9253. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9254. $query->setParameter('id', $courseId);
  9255. $query->setParameter('catId', $item->getId());
  9256. $lps = $query->getResult();
  9257. // Setting category = 0.
  9258. if ($lps) {
  9259. foreach ($lps as $lpItem) {
  9260. $lpItem->setCategoryId(0);
  9261. }
  9262. }
  9263. // Removing category.
  9264. $em->remove($item);
  9265. $em->flush();
  9266. }
  9267. }
  9268. /**
  9269. * @param int $courseId
  9270. * @param bool $addSelectOption
  9271. *
  9272. * @return mixed
  9273. */
  9274. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9275. {
  9276. $items = self::getCategoryByCourse($courseId);
  9277. $cats = array();
  9278. if ($addSelectOption) {
  9279. $cats = array(get_lang('SelectACategory'));
  9280. }
  9281. if (!empty($items)) {
  9282. foreach ($items as $cat) {
  9283. $cats[$cat->getId()] = $cat->getName();
  9284. }
  9285. }
  9286. return $cats;
  9287. }
  9288. /**
  9289. * Return the scorm item type object with spaces replaced with _
  9290. * The return result is use to build a css classname like scorm_type_$return
  9291. * @param $in_type
  9292. * @return mixed
  9293. */
  9294. private static function format_scorm_type_item($in_type)
  9295. {
  9296. return str_replace(' ', '_', $in_type);
  9297. }
  9298. /**
  9299. * @return \learnpath
  9300. */
  9301. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9302. {
  9303. $lpObject = Session::read('lpobject');
  9304. $learnPath = null;
  9305. if (isset($lpObject)) {
  9306. $learnPath = unserialize($lpObject);
  9307. }
  9308. if (!is_object($learnPath)) {
  9309. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9310. }
  9311. return $learnPath;
  9312. }
  9313. /**
  9314. * @param int $itemId
  9315. * @return learnpathItem|false
  9316. */
  9317. public function getItem($itemId)
  9318. {
  9319. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9320. return $this->items[$itemId];
  9321. }
  9322. return false;
  9323. }
  9324. /**
  9325. * @return int
  9326. */
  9327. public function getCategoryId()
  9328. {
  9329. return $this->categoryId;
  9330. }
  9331. /**
  9332. * @param int $categoryId
  9333. * @return bool
  9334. */
  9335. public function setCategoryId($categoryId)
  9336. {
  9337. $this->categoryId = intval($categoryId);
  9338. $courseId = api_get_course_int_id();
  9339. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9340. $lp_id = $this->get_id();
  9341. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9342. WHERE c_id = $courseId AND id = $lp_id";
  9343. Database::query($sql);
  9344. return true;
  9345. }
  9346. /**
  9347. * Get whether this is a learning path with the possibility to subscribe
  9348. * users or not
  9349. * @return int
  9350. */
  9351. public function getSubscribeUsers()
  9352. {
  9353. return $this->subscribeUsers;
  9354. }
  9355. /**
  9356. * Set whether this is a learning path with the possibility to subscribe
  9357. * users or not
  9358. * @param int $subscribeUsers (0 = false, 1 = true)
  9359. */
  9360. public function setSubscribeUsers($value)
  9361. {
  9362. if ($this->debug > 0) {
  9363. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9364. }
  9365. $this->subscribeUsers = intval($value);;
  9366. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9367. $lp_id = $this->get_id();
  9368. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9369. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9370. Database::query($sql);
  9371. return true;
  9372. }
  9373. /**
  9374. * Calculate the count of stars for a user in this LP
  9375. * This calculation is based on the following rules:
  9376. * - the student gets one star when he gets to 50% of the learning path
  9377. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9378. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9379. * - the student gets the final star when the score for the *last* test is >= 80%
  9380. * @param int $sessionId Optional. The session ID
  9381. * @return int The count of stars
  9382. */
  9383. public function getCalculateStars($sessionId = 0)
  9384. {
  9385. $stars = 0;
  9386. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9387. if ($progress >= 50) {
  9388. $stars++;
  9389. }
  9390. // Calculate stars chapters evaluation
  9391. $exercisesItems = $this->getExercisesItems();
  9392. if (!empty($exercisesItems)) {
  9393. $totalResult = 0;
  9394. foreach ($exercisesItems as $exerciseItem) {
  9395. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9396. $this->user_id,
  9397. $exerciseItem->path,
  9398. $this->course_int_id,
  9399. $sessionId,
  9400. $this->lp_id,
  9401. $exerciseItem->db_id
  9402. );
  9403. $exerciseResultInfo = end($exerciseResultInfo);
  9404. if (!$exerciseResultInfo) {
  9405. continue;
  9406. }
  9407. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9408. $totalResult += $exerciseResult;
  9409. }
  9410. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9411. if ($totalExerciseAverage >= 50) {
  9412. $stars++;
  9413. }
  9414. if ($totalExerciseAverage >= 80) {
  9415. $stars++;
  9416. }
  9417. }
  9418. // Calculate star for final evaluation
  9419. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9420. if (!empty($finalEvaluationItem)) {
  9421. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9422. $this->user_id,
  9423. $finalEvaluationItem->path,
  9424. $this->course_int_id,
  9425. $sessionId,
  9426. $this->lp_id,
  9427. $finalEvaluationItem->db_id
  9428. );
  9429. $evaluationResultInfo = end($evaluationResultInfo);
  9430. if ($evaluationResultInfo) {
  9431. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9432. if ($evaluationResult >= 80) {
  9433. $stars++;
  9434. }
  9435. }
  9436. }
  9437. return $stars;
  9438. }
  9439. /**
  9440. * Get the items of exercise type
  9441. * @return array The items. Otherwise return false
  9442. */
  9443. public function getExercisesItems()
  9444. {
  9445. $exercises = [];
  9446. foreach ($this->items as $item) {
  9447. if ($item->type != 'quiz') {
  9448. continue;
  9449. }
  9450. $exercises[] = $item;
  9451. }
  9452. array_pop($exercises);
  9453. return $exercises;
  9454. }
  9455. /**
  9456. * Get the item of exercise type (evaluation type)
  9457. * @return array The final evaluation. Otherwise return false
  9458. */
  9459. public function getFinalEvaluationItem()
  9460. {
  9461. $exercises = [];
  9462. foreach ($this->items as $item) {
  9463. if ($item->type != 'quiz') {
  9464. continue;
  9465. }
  9466. $exercises[] = $item;
  9467. }
  9468. return array_pop($exercises);
  9469. }
  9470. /**
  9471. * Calculate the total points achieved for the current user in this learning path
  9472. * @param int $sessionId Optional. The session Id
  9473. * @return int
  9474. */
  9475. public function getCalculateScore($sessionId = 0)
  9476. {
  9477. // Calculate stars chapters evaluation
  9478. $exercisesItems = $this->getExercisesItems();
  9479. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9480. $totalExercisesResult = 0;
  9481. $totalEvaluationResult = 0;
  9482. if ($exercisesItems !== false) {
  9483. foreach ($exercisesItems as $exerciseItem) {
  9484. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9485. $this->user_id,
  9486. $exerciseItem->path,
  9487. $this->course_int_id,
  9488. $sessionId,
  9489. $this->lp_id,
  9490. $exerciseItem->db_id
  9491. );
  9492. $exerciseResultInfo = end($exerciseResultInfo);
  9493. if (!$exerciseResultInfo) {
  9494. continue;
  9495. }
  9496. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  9497. }
  9498. }
  9499. if (!empty($finalEvaluationItem)) {
  9500. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9501. $this->user_id,
  9502. $finalEvaluationItem->path,
  9503. $this->course_int_id,
  9504. $sessionId,
  9505. $this->lp_id,
  9506. $finalEvaluationItem->db_id
  9507. );
  9508. $evaluationResultInfo = end($evaluationResultInfo);
  9509. if ($evaluationResultInfo) {
  9510. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  9511. }
  9512. }
  9513. return $totalExercisesResult + $totalEvaluationResult;
  9514. }
  9515. /**
  9516. * Check if URL is not allowed to be show in a iframe
  9517. * @param string $src
  9518. *
  9519. * @return string
  9520. */
  9521. public function fixBlockedLinks($src)
  9522. {
  9523. $urlInfo = parse_url($src);
  9524. //$platformProtocol = api_get_protocol();
  9525. $platformProtocol = 'https';
  9526. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9527. $platformProtocol = 'http';
  9528. }
  9529. $protocolFixApplied = false;
  9530. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9531. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9532. if ($platformProtocol != $scheme) {
  9533. $_SESSION['x_frame_source'] = $src;
  9534. $src = 'blank.php?error=x_frames_options';
  9535. $protocolFixApplied = true;
  9536. }
  9537. if ($protocolFixApplied == false) {
  9538. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9539. // Check X-Frame-Options
  9540. $ch = curl_init();
  9541. $options = array(
  9542. CURLOPT_URL => $src,
  9543. CURLOPT_RETURNTRANSFER => true,
  9544. CURLOPT_HEADER => true,
  9545. CURLOPT_FOLLOWLOCATION => true,
  9546. CURLOPT_ENCODING => "",
  9547. CURLOPT_AUTOREFERER => true,
  9548. CURLOPT_CONNECTTIMEOUT => 120,
  9549. CURLOPT_TIMEOUT => 120,
  9550. CURLOPT_MAXREDIRS => 10,
  9551. );
  9552. curl_setopt_array($ch, $options);
  9553. $response = curl_exec($ch);
  9554. $httpCode = curl_getinfo($ch);
  9555. $headers = substr($response, 0, $httpCode['header_size']);
  9556. $error = false;
  9557. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9558. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9559. ) {
  9560. $error = true;
  9561. }
  9562. if ($error) {
  9563. $_SESSION['x_frame_source'] = $src;
  9564. $src = 'blank.php?error=x_frames_options';
  9565. }
  9566. }
  9567. }
  9568. return $src;
  9569. }
  9570. /**
  9571. * Check if this LP has a created forum in the basis course
  9572. * @return boolean
  9573. */
  9574. public function lpHasForum()
  9575. {
  9576. $forumTable = Database::get_course_table(TABLE_FORUM);
  9577. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9578. $fakeFrom = "
  9579. $forumTable f
  9580. INNER JOIN $itemProperty ip
  9581. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9582. ";
  9583. $resultData = Database::select(
  9584. 'COUNT(f.iid) AS qty',
  9585. $fakeFrom,
  9586. [
  9587. 'where' => [
  9588. 'ip.visibility != ? AND ' => 2,
  9589. 'ip.tool = ? AND ' => TOOL_FORUM,
  9590. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9591. 'f.lp_id = ?' => intval($this->lp_id)
  9592. ]
  9593. ],
  9594. 'first'
  9595. );
  9596. if ($resultData['qty'] > 0) {
  9597. return true;
  9598. }
  9599. return false;
  9600. }
  9601. /**
  9602. * Get the forum for this learning path
  9603. * @return boolean
  9604. */
  9605. public function getForum($sessionId = 0)
  9606. {
  9607. $forumTable = Database::get_course_table(TABLE_FORUM);
  9608. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9609. $fakeFrom = "$forumTable f
  9610. INNER JOIN $itemProperty ip ";
  9611. if ($this->lp_session_id == 0) {
  9612. $fakeFrom .= "
  9613. ON (
  9614. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9615. f.session_id = ip.session_id OR ip.session_id IS NULL
  9616. )
  9617. )
  9618. ";
  9619. } else {
  9620. $fakeFrom .= "
  9621. ON (
  9622. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9623. )
  9624. ";
  9625. }
  9626. $resultData = Database::select(
  9627. 'f.*',
  9628. $fakeFrom,
  9629. [
  9630. 'where' => [
  9631. 'ip.visibility != ? AND ' => 2,
  9632. 'ip.tool = ? AND ' => TOOL_FORUM,
  9633. 'f.session_id = ? AND ' => $sessionId,
  9634. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9635. 'f.lp_id = ?' => intval($this->lp_id)
  9636. ]
  9637. ],
  9638. 'first'
  9639. );
  9640. if (empty($resultData)) {
  9641. return false;
  9642. }
  9643. return $resultData;
  9644. }
  9645. /**
  9646. * Create a forum for this learning path
  9647. * @param type $forumCategoryId
  9648. * @return int The forum ID if was created. Otherwise return false
  9649. */
  9650. public function createForum($forumCategoryId)
  9651. {
  9652. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9653. $forumId = store_forum(
  9654. [
  9655. 'lp_id' => $this->lp_id,
  9656. 'forum_title' => $this->name,
  9657. 'forum_comment' => null,
  9658. 'forum_category' => intval($forumCategoryId),
  9659. 'students_can_edit_group' => ['students_can_edit' => 0],
  9660. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9661. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9662. 'group_forum' => 0,
  9663. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9664. ],
  9665. [],
  9666. true
  9667. );
  9668. return $forumId;
  9669. }
  9670. }
  9671. if (!function_exists('trim_value')) {
  9672. function trim_value(& $value) {
  9673. $value = trim($value);
  9674. }
  9675. }