learnpath.class.php 483 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Finder\Finder;
  10. use Chamilo\CourseBundle\Entity\CLp;
  11. use Chamilo\CourseBundle\Entity\CTool;
  12. use Chamilo\UserBundle\Entity\User;
  13. /**
  14. * Class learnpath
  15. * This class defines the parent attributes and methods for Chamilo learnpaths
  16. * and SCORM learnpaths. It is used by the scorm class.
  17. *
  18. * @package chamilo.learnpath
  19. * @author Yannick Warnier <ywarnier@beeznest.org>
  20. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  21. */
  22. class learnpath
  23. {
  24. public $attempt = 0; // The number for the current ID view.
  25. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  26. public $current; // Id of the current item the user is viewing.
  27. public $current_score; // The score of the current item.
  28. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  29. public $current_time_stop; // The time the user closed this resource.
  30. public $default_status = 'not attempted';
  31. public $encoding = 'UTF-8';
  32. public $error = '';
  33. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  34. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  35. public $index; // The index of the active learnpath_item in $ordered_items array.
  36. public $items = array();
  37. public $last; // item_id of last item viewed in the learning path.
  38. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  39. public $license; // Which license this course has been given - not used yet on 20060522.
  40. public $lp_id; // DB ID for this learnpath.
  41. public $lp_view_id; // DB ID for lp_view
  42. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  43. public $message = '';
  44. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  45. public $name; // Learnpath name (they generally have one).
  46. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  47. public $path = ''; // Path inside the scorm directory (if scorm).
  48. public $theme; // The current theme of the learning path.
  49. public $preview_image; // The current image of the learning path.
  50. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  51. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  52. public $prevent_reinit = 1;
  53. // Describes the mode of progress bar display.
  54. public $seriousgame_mode = 0;
  55. public $progress_bar_mode = '%';
  56. // Percentage progress as saved in the db.
  57. public $progress_db = 0;
  58. public $proximity; // Wether the content is distant or local or unknown.
  59. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  60. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  61. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  62. public $type; //type of learnpath. Could be 'chamilo', 'scorm', 'scorm2004', 'aicc', ...
  63. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  64. public $user_id; //ID of the user that is viewing/using the course
  65. public $update_queue = array();
  66. public $scorm_debug = 0;
  67. public $arrMenu = array(); // Array for the menu items.
  68. public $debug = 0; // Logging level.
  69. public $lp_session_id = 0;
  70. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  71. public $prerequisite = 0;
  72. public $use_max_score = 1; // 1 or 0
  73. public $subscribeUsers = 0; // Subscribe users or not
  74. public $created_on = '';
  75. public $modified_on = '';
  76. public $publicated_on = '';
  77. public $expired_on = '';
  78. public $ref = null;
  79. public $course_int_id;
  80. public $course_info = array();
  81. public $categoryId;
  82. /**
  83. * Constructor.
  84. * Needs a database handler, a course code and a learnpath id from the database.
  85. * Also builds the list of items into $this->items.
  86. * @param string $course Course code
  87. * @param integer $lp_id
  88. * @param integer $user_id
  89. */
  90. public function __construct($course, $lp_id, $user_id)
  91. {
  92. $this->encoding = api_get_system_encoding();
  93. if ($this->debug > 0) {
  94. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  95. }
  96. if (empty($course)) {
  97. $course = api_get_course_id();
  98. }
  99. $course_info = api_get_course_info($course);
  100. if (!empty($course_info)) {
  101. $this->cc = $course_info['code'];
  102. $this->course_info = $course_info;
  103. $course_id = $course_info['real_id'];
  104. } else {
  105. $this->error = 'Course code does not exist in database.';
  106. }
  107. $this->set_course_int_id($course_id);
  108. $lp_id = (int) $lp_id;
  109. // Check learnpath ID.
  110. if (empty($lp_id)) {
  111. $this->error = 'Learnpath ID is empty';
  112. } else {
  113. // TODO: Make it flexible to use any course_code (still using env course code here).
  114. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  115. $sql = "SELECT * FROM $lp_table
  116. WHERE id = '$lp_id' AND c_id = $course_id";
  117. if ($this->debug > 2) {
  118. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  119. }
  120. $res = Database::query($sql);
  121. if (Database::num_rows($res) > 0) {
  122. $this->lp_id = $lp_id;
  123. $row = Database::fetch_array($res);
  124. $this->type = $row['lp_type'];
  125. $this->name = stripslashes($row['name']);
  126. $this->proximity = $row['content_local'];
  127. $this->theme = $row['theme'];
  128. $this->maker = $row['content_maker'];
  129. $this->prevent_reinit = $row['prevent_reinit'];
  130. $this->seriousgame_mode = $row['seriousgame_mode'];
  131. $this->license = $row['content_license'];
  132. $this->scorm_debug = $row['debug'];
  133. $this->js_lib = $row['js_lib'];
  134. $this->path = $row['path'];
  135. $this->preview_image = $row['preview_image'];
  136. $this->author = $row['author'];
  137. $this->hide_toc_frame = $row['hide_toc_frame'];
  138. $this->lp_session_id = $row['session_id'];
  139. $this->use_max_score = $row['use_max_score'];
  140. $this->subscribeUsers = $row['subscribe_users'];
  141. $this->created_on = $row['created_on'];
  142. $this->modified_on = $row['modified_on'];
  143. $this->ref = $row['ref'];
  144. $this->categoryId = $row['category_id'];
  145. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  146. if (!empty($row['publicated_on'])) {
  147. $this->publicated_on = $row['publicated_on'];
  148. }
  149. if (!empty($row['expired_on'])) {
  150. $this->expired_on = $row['expired_on'];
  151. }
  152. if ($this->type == 2) {
  153. if ($row['force_commit'] == 1) {
  154. $this->force_commit = true;
  155. }
  156. }
  157. $this->mode = $row['default_view_mod'];
  158. // Check user ID.
  159. if (empty($user_id)) {
  160. $this->error = 'User ID is empty';
  161. } else {
  162. $user_info = api_get_user_info($user_id);
  163. if (!empty($user_info)) {
  164. $this->user_id = $user_info['user_id'];
  165. } else {
  166. $this->error = 'User ID does not exist in database ('.$sql.')';
  167. }
  168. }
  169. // End of variables checking.
  170. $session_id = api_get_session_id();
  171. // Get the session condition for learning paths of the base + session.
  172. $session = api_get_session_condition($session_id);
  173. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  174. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  175. // Selecting by view_count descending allows to get the highest view_count first.
  176. $sql = "SELECT * FROM $lp_table
  177. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  178. ORDER BY view_count DESC";
  179. $res = Database::query($sql);
  180. if ($this->debug > 2) {
  181. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  182. }
  183. if (Database::num_rows($res) > 0) {
  184. if ($this->debug > 2) {
  185. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  186. }
  187. $row = Database::fetch_array($res);
  188. $this->attempt = $row['view_count'];
  189. $this->lp_view_id = $row['id'];
  190. $this->last_item_seen = $row['last_item'];
  191. $this->progress_db = $row['progress'];
  192. $this->lp_view_session_id = $row['session_id'];
  193. } else if (!api_is_invitee()) {
  194. if ($this->debug > 2) {
  195. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  196. }
  197. $this->attempt = 1;
  198. $params = [
  199. 'c_id' => $course_id,
  200. 'lp_id' => $lp_id,
  201. 'user_id' => $user_id,
  202. 'view_count' => 1,
  203. 'session_id' => $session_id,
  204. 'last_item' => 0
  205. ];
  206. $this->lp_view_id = Database::insert($lp_table, $params);
  207. if (!empty($this->lp_view_id)) {
  208. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  209. Database::query($sql);
  210. }
  211. }
  212. // Initialise items.
  213. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  214. $sql = "SELECT * FROM $lp_item_table
  215. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  216. ORDER BY parent_item_id, display_order";
  217. $res = Database::query($sql);
  218. if ($this->debug > 2) {
  219. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  220. error_log('-- Start while--', 0);
  221. }
  222. $lp_item_id_list = array();
  223. while ($row = Database::fetch_array($res)) {
  224. $lp_item_id_list[] = $row['id'];
  225. switch ($this->type) {
  226. case 3: //aicc
  227. $oItem = new aiccItem('db', $row['id'], $course_id);
  228. if (is_object($oItem)) {
  229. $my_item_id = $oItem->get_id();
  230. $oItem->set_lp_view($this->lp_view_id, $course_id);
  231. $oItem->set_prevent_reinit($this->prevent_reinit);
  232. // Don't use reference here as the next loop will make the pointed object change.
  233. $this->items[$my_item_id] = $oItem;
  234. $this->refs_list[$oItem->ref] = $my_item_id;
  235. if ($this->debug > 2) {
  236. error_log(
  237. 'New LP - learnpath::__construct() - '.
  238. 'aicc object with id '.$my_item_id.
  239. ' set in items[]',
  240. 0
  241. );
  242. }
  243. }
  244. break;
  245. case 2:
  246. $oItem = new scormItem('db', $row['id'], $course_id);
  247. if (is_object($oItem)) {
  248. $my_item_id = $oItem->get_id();
  249. $oItem->set_lp_view($this->lp_view_id, $course_id);
  250. $oItem->set_prevent_reinit($this->prevent_reinit);
  251. // Don't use reference here as the next loop will make the pointed object change.
  252. $this->items[$my_item_id] = $oItem;
  253. $this->refs_list[$oItem->ref] = $my_item_id;
  254. if ($this->debug > 2) {
  255. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  256. }
  257. }
  258. break;
  259. case 1:
  260. default:
  261. if ($this->debug > 2) {
  262. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  263. }
  264. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  265. if ($this->debug > 2) {
  266. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  267. }
  268. if (is_object($oItem)) {
  269. $my_item_id = $oItem->get_id();
  270. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  271. $oItem->set_prevent_reinit($this->prevent_reinit);
  272. // Don't use reference here as the next loop will make the pointed object change.
  273. $this->items[$my_item_id] = $oItem;
  274. $this->refs_list[$my_item_id] = $my_item_id;
  275. if ($this->debug > 2) {
  276. error_log(
  277. 'New LP - learnpath::__construct() '.__LINE__.
  278. ' - object with id '.$my_item_id.' set in items[]',
  279. 0
  280. );
  281. }
  282. }
  283. break;
  284. }
  285. // Setting the object level with variable $this->items[$i][parent]
  286. foreach ($this->items as $itemLPObject) {
  287. $level = self::get_level_for_item($this->items, $itemLPObject->db_id);
  288. $itemLPObject->level = $level;
  289. }
  290. // Setting the view in the item object.
  291. if (is_object($this->items[$row['id']])) {
  292. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  293. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  294. $this->items[$row['id']]->current_start_time = 0;
  295. $this->items[$row['id']]->current_stop_time = 0;
  296. }
  297. }
  298. }
  299. if ($this->debug > 2) {
  300. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  301. }
  302. if (!empty($lp_item_id_list)) {
  303. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  304. if (!empty($lp_item_id_list_to_string)) {
  305. // Get last viewing vars.
  306. $itemViewTable = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  307. // This query should only return one or zero result.
  308. $sql = "SELECT lp_item_id, status
  309. FROM $itemViewTable
  310. WHERE
  311. c_id = $course_id AND
  312. lp_view_id = ".$this->lp_view_id." AND
  313. lp_item_id IN ('".$lp_item_id_list_to_string."')
  314. ORDER BY view_count DESC ";
  315. if ($this->debug > 2) {
  316. error_log(
  317. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  318. 0
  319. );
  320. }
  321. $status_list = array();
  322. $res = Database::query($sql);
  323. while ($row = Database:: fetch_array($res)) {
  324. $status_list[$row['lp_item_id']] = $row['status'];
  325. }
  326. foreach ($lp_item_id_list as $item_id) {
  327. if (isset($status_list[$item_id])) {
  328. $status = $status_list[$item_id];
  329. if (is_object($this->items[$item_id])) {
  330. $this->items[$item_id]->set_status($status);
  331. if (empty($status)) {
  332. $this->items[$item_id]->set_status(
  333. $this->default_status
  334. );
  335. }
  336. }
  337. } else {
  338. if (!api_is_invitee()) {
  339. if (is_object($this->items[$item_id])) {
  340. $this->items[$item_id]->set_status(
  341. $this->default_status
  342. );
  343. }
  344. if (!empty($this->lp_view_id)) {
  345. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  346. $params = [
  347. 'c_id' => $course_id,
  348. 'lp_item_id' => $course_id,
  349. 'lp_view_id' => $this->lp_view_id,
  350. 'view_count' => 1,
  351. 'status' => 'not attempted',
  352. 'start_time' => api_get_utc_datetime(),
  353. 'total_time' => 0,
  354. 'score' => 0
  355. ];
  356. $insertId = Database::insert($itemViewTable, $params);
  357. if ($insertId) {
  358. $sql = "UPDATE $itemViewTable SET id = iid
  359. WHERE iid = $insertId";
  360. Database::query($sql);
  361. }
  362. $this->items[$item_id]->set_lp_view(
  363. $this->lp_view_id,
  364. $course_id
  365. );
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. $this->ordered_items = self::get_flat_ordered_items_list(
  373. $this->get_id(),
  374. 0,
  375. $course_id
  376. );
  377. $this->max_ordered_items = 0;
  378. foreach ($this->ordered_items as $index => $dummy) {
  379. if ($index > $this->max_ordered_items && !empty($dummy)) {
  380. $this->max_ordered_items = $index;
  381. }
  382. }
  383. // TODO: Define the current item better.
  384. $this->first();
  385. if ($this->debug > 2) {
  386. error_log('New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(), 0);
  387. }
  388. } else {
  389. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  390. }
  391. }
  392. }
  393. /**
  394. * @return string
  395. */
  396. public function getCourseCode()
  397. {
  398. return $this->cc;
  399. }
  400. /**
  401. * @return int
  402. */
  403. public function get_course_int_id()
  404. {
  405. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  406. }
  407. /**
  408. * @param $course_id
  409. * @return int
  410. */
  411. public function set_course_int_id($course_id)
  412. {
  413. return $this->course_int_id = (int) $course_id;
  414. }
  415. /**
  416. * Get the depth level of LP item
  417. * @param array $items
  418. * @param int $currentItemId
  419. * @return int
  420. */
  421. private static function get_level_for_item($items, $currentItemId)
  422. {
  423. $parentItemId = $items[$currentItemId]->parent;
  424. if ($parentItemId == 0) {
  425. return 0;
  426. } else {
  427. return self::get_level_for_item($items, $parentItemId) + 1;
  428. }
  429. }
  430. /**
  431. * Function rewritten based on old_add_item() from Yannick Warnier.
  432. * Due the fact that users can decide where the item should come, I had to overlook this function and
  433. * I found it better to rewrite it. Old function is still available.
  434. * Added also the possibility to add a description.
  435. *
  436. * @param int $parent
  437. * @param int $previous
  438. * @param string $type
  439. * @param int $id resource ID (ref)
  440. * @param string $title
  441. * @param string $description
  442. * @param int $prerequisites
  443. * @param int $max_time_allowed
  444. * @param int $userId
  445. *
  446. * @return int
  447. */
  448. public function add_item(
  449. $parent,
  450. $previous,
  451. $type = 'dir',
  452. $id,
  453. $title,
  454. $description,
  455. $prerequisites = 0,
  456. $max_time_allowed = 0,
  457. $userId = 0
  458. ) {
  459. $course_id = $this->course_info['real_id'];
  460. if ($this->debug > 0) {
  461. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  462. }
  463. if (empty($course_id)) {
  464. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  465. $this->course_info = api_get_course_info($this->cc);
  466. $course_id = $this->course_info['real_id'];
  467. }
  468. $userId = empty($userId) ? api_get_user_id() : $userId;
  469. $sessionId = api_get_session_id();
  470. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  471. $_course = $this->course_info;
  472. $parent = intval($parent);
  473. $previous = intval($previous);
  474. $id = intval($id);
  475. $max_time_allowed = htmlentities($max_time_allowed);
  476. if (empty($max_time_allowed)) {
  477. $max_time_allowed = 0;
  478. }
  479. $sql = "SELECT COUNT(id) AS num
  480. FROM $tbl_lp_item
  481. WHERE
  482. c_id = $course_id AND
  483. lp_id = ".$this->get_id()." AND
  484. parent_item_id = " . $parent;
  485. $res_count = Database::query($sql);
  486. $row = Database::fetch_array($res_count);
  487. $num = $row['num'];
  488. if ($num > 0) {
  489. if (empty($previous)) {
  490. $sql = "SELECT id, next_item_id, display_order
  491. FROM " . $tbl_lp_item."
  492. WHERE
  493. c_id = $course_id AND
  494. lp_id = ".$this->get_id()." AND
  495. parent_item_id = " . $parent." AND
  496. previous_item_id = 0 OR
  497. previous_item_id=" . $parent;
  498. $result = Database::query($sql);
  499. $row = Database::fetch_array($result);
  500. $tmp_previous = 0;
  501. $next = $row['id'];
  502. $display_order = 0;
  503. } else {
  504. $previous = (int) $previous;
  505. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  506. FROM $tbl_lp_item
  507. WHERE
  508. c_id = $course_id AND
  509. lp_id = ".$this->get_id()." AND
  510. id = " . $previous;
  511. $result = Database::query($sql);
  512. $row = Database:: fetch_array($result);
  513. $tmp_previous = $row['id'];
  514. $next = $row['next_item_id'];
  515. $display_order = $row['display_order'];
  516. }
  517. } else {
  518. $tmp_previous = 0;
  519. $next = 0;
  520. $display_order = 0;
  521. }
  522. $id = intval($id);
  523. $typeCleaned = Database::escape_string($type);
  524. if ($type == 'quiz') {
  525. $sql = 'SELECT SUM(ponderation)
  526. FROM ' . Database::get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  527. INNER JOIN ' . Database::get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  528. ON
  529. quiz_question.id = quiz_rel_question.question_id AND
  530. quiz_question.c_id = quiz_rel_question.c_id
  531. WHERE
  532. quiz_rel_question.exercice_id = '.$id." AND
  533. quiz_question.c_id = $course_id AND
  534. quiz_rel_question.c_id = $course_id ";
  535. $rsQuiz = Database::query($sql);
  536. $max_score = Database::result($rsQuiz, 0, 0);
  537. // Disabling the exercise if we add it inside a LP
  538. $exercise = new Exercise($course_id);
  539. $exercise->read($id);
  540. $exercise->disable();
  541. $exercise->save();
  542. } else {
  543. $max_score = 100;
  544. }
  545. $params = array(
  546. "c_id" => $course_id,
  547. "lp_id" => $this->get_id(),
  548. "item_type" => $typeCleaned,
  549. "ref" => '',
  550. "title" => $title,
  551. "description" => $description,
  552. "path" => $id,
  553. "max_score" => $max_score,
  554. "parent_item_id" => $parent,
  555. "previous_item_id" => $previous,
  556. "next_item_id" => intval($next),
  557. "display_order" => $display_order + 1,
  558. "prerequisite" => $prerequisites,
  559. "max_time_allowed" => $max_time_allowed,
  560. 'min_score' => 0,
  561. 'launch_data' => ''
  562. );
  563. if ($prerequisites != 0) {
  564. $params['prerequisite'] = $prerequisites;
  565. }
  566. $new_item_id = Database::insert($tbl_lp_item, $params);
  567. if ($this->debug > 2) {
  568. error_log('New LP - Inserting dir/chapter: '.$new_item_id, 0);
  569. }
  570. if ($new_item_id) {
  571. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  572. Database::query($sql);
  573. $sql = "UPDATE $tbl_lp_item
  574. SET previous_item_id = $new_item_id
  575. WHERE c_id = $course_id AND id = $next";
  576. Database::query($sql);
  577. // Update the item that should be before the new item.
  578. $sql = "UPDATE $tbl_lp_item
  579. SET next_item_id = $new_item_id
  580. WHERE c_id = $course_id AND id = $tmp_previous";
  581. Database::query($sql);
  582. // Update all the items after the new item.
  583. $sql = "UPDATE ".$tbl_lp_item."
  584. SET display_order = display_order + 1
  585. WHERE
  586. c_id = $course_id AND
  587. lp_id = ".$this->get_id()." AND
  588. id <> " . $new_item_id." AND
  589. parent_item_id = " . $parent." AND
  590. display_order > " . $display_order;
  591. Database::query($sql);
  592. // Update the item that should come after the new item.
  593. $sql = "UPDATE ".$tbl_lp_item."
  594. SET ref = " . $new_item_id."
  595. WHERE c_id = $course_id AND id = ".$new_item_id;
  596. Database::query($sql);
  597. // Upload audio.
  598. if (!empty($_FILES['mp3']['name'])) {
  599. // Create the audio folder if it does not exist yet.
  600. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  601. if (!is_dir($filepath.'audio')) {
  602. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  603. $audio_id = add_document(
  604. $_course,
  605. '/audio',
  606. 'folder',
  607. 0,
  608. 'audio',
  609. '',
  610. 0,
  611. true,
  612. null,
  613. $sessionId,
  614. $userId
  615. );
  616. api_item_property_update(
  617. $_course,
  618. TOOL_DOCUMENT,
  619. $audio_id,
  620. 'FolderCreated',
  621. $userId,
  622. null,
  623. null,
  624. null,
  625. null,
  626. $sessionId
  627. );
  628. api_item_property_update(
  629. $_course,
  630. TOOL_DOCUMENT,
  631. $audio_id,
  632. 'invisible',
  633. $userId,
  634. null,
  635. null,
  636. null,
  637. null,
  638. $sessionId
  639. );
  640. }
  641. $file_path = handle_uploaded_document(
  642. $_course,
  643. $_FILES['mp3'],
  644. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  645. '/audio',
  646. $userId,
  647. '',
  648. '',
  649. '',
  650. '',
  651. false
  652. );
  653. // Getting the filename only.
  654. $file_components = explode('/', $file_path);
  655. $file = $file_components[count($file_components) - 1];
  656. // Store the mp3 file in the lp_item table.
  657. $sql = "UPDATE $tbl_lp_item SET
  658. audio = '".Database::escape_string($file)."'
  659. WHERE id = '" . intval($new_item_id)."'";
  660. Database::query($sql);
  661. }
  662. }
  663. return $new_item_id;
  664. }
  665. /**
  666. * Static admin function allowing addition of a learnpath to a course.
  667. * @param string Course code
  668. * @param string Learnpath name
  669. * @param string Learnpath description string, if provided
  670. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  671. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  672. * @param string $zipname Zip file containing the learnpath or directory containing the learnpath
  673. * @param string $publicated_on
  674. * @param string $expired_on
  675. * @param int $categoryId
  676. * @param int $userId
  677. * @return integer The new learnpath ID on success, 0 on failure
  678. */
  679. public static function add_lp(
  680. $courseCode,
  681. $name,
  682. $description = '',
  683. $learnpath = 'guess',
  684. $origin = 'zip',
  685. $zipname = '',
  686. $publicated_on = '',
  687. $expired_on = '',
  688. $categoryId = 0,
  689. $userId = 0
  690. ) {
  691. global $charset;
  692. if (!empty($courseCode)) {
  693. $courseInfo = api_get_course_info($courseCode);
  694. $course_id = $courseInfo['real_id'];
  695. } else {
  696. $course_id = api_get_course_int_id();
  697. $courseInfo = api_get_course_info();
  698. }
  699. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  700. // Check course code exists.
  701. // Check lp_name doesn't exist, otherwise append something.
  702. $i = 0;
  703. $name = Database::escape_string($name);
  704. $categoryId = intval($categoryId);
  705. // Session id.
  706. $session_id = api_get_session_id();
  707. $userId = empty($userId) ? api_get_user_id() : $userId;
  708. $check_name = "SELECT * FROM $tbl_lp
  709. WHERE c_id = $course_id AND name = '$name'";
  710. $res_name = Database::query($check_name);
  711. if (empty($publicated_on)) {
  712. $publicated_on = null;
  713. } else {
  714. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  715. }
  716. if (empty($expired_on)) {
  717. $expired_on = null;
  718. } else {
  719. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  720. }
  721. while (Database::num_rows($res_name)) {
  722. // There is already one such name, update the current one a bit.
  723. $i++;
  724. $name = $name.' - '.$i;
  725. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  726. $res_name = Database::query($check_name);
  727. }
  728. // New name does not exist yet; keep it.
  729. // Escape description.
  730. // Kevin: added htmlentities().
  731. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  732. $type = 1;
  733. switch ($learnpath) {
  734. case 'guess':
  735. break;
  736. case 'dokeos':
  737. case 'chamilo':
  738. $type = 1;
  739. break;
  740. case 'aicc':
  741. break;
  742. }
  743. switch ($origin) {
  744. case 'zip':
  745. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  746. break;
  747. case 'manual':
  748. default:
  749. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  750. $res_max = Database::query($get_max);
  751. if (Database::num_rows($res_max) < 1) {
  752. $dsp = 1;
  753. } else {
  754. $row = Database::fetch_array($res_max);
  755. $dsp = $row[0] + 1;
  756. }
  757. $params = [
  758. 'c_id' => $course_id,
  759. 'lp_type' => $type,
  760. 'name' => $name,
  761. 'description' => $description,
  762. 'path' => '',
  763. 'default_view_mod' => 'embedded',
  764. 'default_encoding' => 'UTF-8',
  765. 'display_order' => $dsp,
  766. 'content_maker' => 'Chamilo',
  767. 'content_local' => 'local',
  768. 'js_lib' => '',
  769. 'session_id' => $session_id,
  770. 'created_on' => api_get_utc_datetime(),
  771. 'modified_on' => api_get_utc_datetime(),
  772. 'publicated_on' => $publicated_on,
  773. 'expired_on' => $expired_on,
  774. 'category_id' => $categoryId,
  775. 'force_commit' => 0,
  776. 'content_license' => '',
  777. 'debug' => 0,
  778. 'theme' => '',
  779. 'preview_image' => '',
  780. 'author' => '',
  781. 'prerequisite' => 0,
  782. 'hide_toc_frame' => 0,
  783. 'seriousgame_mode' => 0,
  784. 'autolaunch' => 0,
  785. 'max_attempts' => 0,
  786. 'subscribe_users' => 0,
  787. 'accumulate_scorm_time' => 1
  788. ];
  789. $id = Database::insert($tbl_lp, $params);
  790. if ($id > 0) {
  791. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  792. Database::query($sql);
  793. // Insert into item_property.
  794. api_item_property_update(
  795. $courseInfo,
  796. TOOL_LEARNPATH,
  797. $id,
  798. 'LearnpathAdded',
  799. $userId
  800. );
  801. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  802. return $id;
  803. }
  804. break;
  805. }
  806. }
  807. /**
  808. * Auto completes the parents of an item in case it's been completed or passed
  809. * @param int $item Optional ID of the item from which to look for parents
  810. */
  811. public function autocomplete_parents($item)
  812. {
  813. $debug = $this->debug;
  814. if ($debug) {
  815. error_log('Learnpath::autocomplete_parents()', 0);
  816. }
  817. if (empty($item)) {
  818. $item = $this->current;
  819. }
  820. $currentItem = $this->getItem($item);
  821. if ($currentItem) {
  822. $parent_id = $currentItem->get_parent();
  823. $parent = $this->getItem($parent_id);
  824. if ($parent) {
  825. // if $item points to an object and there is a parent.
  826. if ($debug) {
  827. error_log(
  828. 'Autocompleting parent of item '.$item.' "'.$currentItem->get_title().'" (item '.$parent_id.' "'.$parent->get_title().'") ',
  829. 0
  830. );
  831. }
  832. // New experiment including failed and browsed in completed status.
  833. //$current_status = $currentItem->get_status();
  834. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  835. // Fixes chapter auto complete
  836. if (true) {
  837. // If the current item is completed or passes or succeeded.
  838. $updateParentStatus = true;
  839. if ($debug) {
  840. error_log('Status of current item is alright', 0);
  841. }
  842. foreach ($parent->get_children() as $childItemId) {
  843. $childItem = $this->getItem($childItemId);
  844. // If children was not set try to get the info
  845. if (empty($childItem->db_item_view_id)) {
  846. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  847. }
  848. // Check all his brothers (parent's children) for completion status.
  849. if ($childItemId != $item) {
  850. if ($debug) {
  851. error_log(
  852. 'Looking at brother #'.$childItemId.' "'.$childItem->get_title().'", status is '.$childItem->get_status(),
  853. 0
  854. );
  855. }
  856. // Trying completing parents of failed and browsed items as well.
  857. if ($childItem->status_is(
  858. array(
  859. 'completed',
  860. 'passed',
  861. 'succeeded',
  862. 'browsed',
  863. 'failed'
  864. )
  865. )
  866. ) {
  867. // Keep completion status to true.
  868. continue;
  869. } else {
  870. if ($debug > 2) {
  871. error_log(
  872. '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,
  873. 0
  874. );
  875. }
  876. $updateParentStatus = false;
  877. break;
  878. }
  879. }
  880. }
  881. if ($updateParentStatus) {
  882. // If all the children were completed:
  883. $parent->set_status('completed');
  884. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  885. // Force the status to "completed"
  886. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  887. $this->update_queue[$parent->get_id()] = 'completed';
  888. if ($debug) {
  889. error_log(
  890. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  891. print_r($this->update_queue, 1),
  892. 0
  893. );
  894. }
  895. // Recursive call.
  896. $this->autocomplete_parents($parent->get_id());
  897. }
  898. }
  899. } else {
  900. if ($debug) {
  901. error_log("Parent #$parent_id does not exists");
  902. }
  903. }
  904. } else {
  905. if ($debug) {
  906. error_log("#$item is an item that doesn't have parents");
  907. }
  908. }
  909. }
  910. /**
  911. * Auto saves the current results into the database for the whole learnpath
  912. * @todo: Add save operations for the learnpath itself.
  913. */
  914. public function autosave()
  915. {
  916. if ($this->debug > 0) {
  917. error_log('New LP - In learnpath::autosave()', 0);
  918. }
  919. }
  920. /**
  921. * Closes the current resource
  922. *
  923. * Stops the timer
  924. * Saves into the database if required
  925. * Clears the current resource data from this object
  926. * @return boolean True on success, false on failure
  927. */
  928. public function close()
  929. {
  930. if ($this->debug > 0) {
  931. error_log('New LP - In learnpath::close()', 0);
  932. }
  933. if (empty($this->lp_id)) {
  934. $this->error = 'Trying to close this learnpath but no ID is set';
  935. return false;
  936. }
  937. $this->current_time_stop = time();
  938. $this->ordered_items = array();
  939. $this->index = 0;
  940. unset($this->lp_id);
  941. //unset other stuff
  942. return true;
  943. }
  944. /**
  945. * Static admin function allowing removal of a learnpath
  946. * @param array $courseInfo
  947. * @param integer Learnpath ID
  948. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  949. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  950. */
  951. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  952. {
  953. $course_id = api_get_course_int_id();
  954. if (!empty($courseInfo)) {
  955. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  956. }
  957. // TODO: Implement a way of getting this to work when the current object is not set.
  958. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  959. // If an ID is specifically given and the current LP is not the same, prevent delete.
  960. if (!empty($id) && ($id != $this->lp_id)) {
  961. return false;
  962. }
  963. $lp = Database::get_course_table(TABLE_LP_MAIN);
  964. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  965. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
  966. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  967. // Delete lp item id.
  968. foreach ($this->items as $id => $dummy) {
  969. $sql = "DELETE FROM $lp_item_view
  970. WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  971. Database::query($sql);
  972. }
  973. // Proposed by Christophe (nickname: clefevre)
  974. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  975. Database::query($sql);
  976. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  977. Database::query($sql);
  978. self::toggle_publish($this->lp_id, 'i');
  979. if ($this->type == 2 || $this->type == 3) {
  980. // This is a scorm learning path, delete the files as well.
  981. $sql = "SELECT path FROM $lp
  982. WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  983. $res = Database::query($sql);
  984. if (Database::num_rows($res) > 0) {
  985. $row = Database::fetch_array($res);
  986. $path = $row['path'];
  987. $sql = "SELECT id FROM $lp
  988. WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  989. $res = Database::query($sql);
  990. if (Database::num_rows($res) > 0) {
  991. // Another learning path uses this directory, so don't delete it.
  992. if ($this->debug > 2) {
  993. error_log('New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory', 0);
  994. }
  995. } else {
  996. // No other LP uses that directory, delete it.
  997. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  998. $course_scorm_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir; // The absolute system path for this course.
  999. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) && !empty ($course_scorm_dir)) {
  1000. if ($this->debug > 2) {
  1001. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path, 0);
  1002. }
  1003. // Proposed by Christophe (clefevre).
  1004. if (strcmp(substr($path, -2), "/.") == 0) {
  1005. $path = substr($path, 0, -1); // Remove "." at the end.
  1006. }
  1007. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1008. rmdirr($course_scorm_dir.$path);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1014. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1015. // Delete tools
  1016. $sql = "DELETE FROM $tbl_tool
  1017. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1018. Database::query($sql);
  1019. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  1020. Database::query($sql);
  1021. // Updates the display order of all lps.
  1022. $this->update_display_order();
  1023. api_item_property_update(
  1024. api_get_course_info(),
  1025. TOOL_LEARNPATH,
  1026. $this->lp_id,
  1027. 'delete',
  1028. api_get_user_id()
  1029. );
  1030. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1031. api_get_course_id(),
  1032. 4,
  1033. $id,
  1034. api_get_session_id()
  1035. );
  1036. if ($link_info !== false) {
  1037. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1038. }
  1039. if (api_get_setting('search_enabled') == 'true') {
  1040. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1041. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1042. }
  1043. }
  1044. /**
  1045. * Removes all the children of one item - dangerous!
  1046. * @param integer $id Element ID of which children have to be removed
  1047. * @return integer Total number of children removed
  1048. */
  1049. public function delete_children_items($id)
  1050. {
  1051. $course_id = $this->course_info['real_id'];
  1052. if ($this->debug > 0) {
  1053. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  1054. }
  1055. $num = 0;
  1056. if (empty($id) || $id != strval(intval($id))) {
  1057. return false;
  1058. }
  1059. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1060. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1061. $res = Database::query($sql);
  1062. while ($row = Database::fetch_array($res)) {
  1063. $num += $this->delete_children_items($row['id']);
  1064. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1065. Database::query($sql_del);
  1066. $num++;
  1067. }
  1068. return $num;
  1069. }
  1070. /**
  1071. * Removes an item from the current learnpath
  1072. * @param integer $id Elem ID (0 if first)
  1073. * @param integer $remove Whether to remove the resource/data from the
  1074. * system or leave it (default: 'keep', others 'remove')
  1075. * @return integer Number of elements moved
  1076. * @todo implement resource removal
  1077. */
  1078. public function delete_item($id, $remove = 'keep')
  1079. {
  1080. $course_id = api_get_course_int_id();
  1081. if ($this->debug > 0) {
  1082. error_log('New LP - In learnpath::delete_item()', 0);
  1083. }
  1084. // TODO: Implement the resource removal.
  1085. if (empty($id) || $id != strval(intval($id))) {
  1086. return false;
  1087. }
  1088. // First select item to get previous, next, and display order.
  1089. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1090. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1091. $res_sel = Database::query($sql_sel);
  1092. if (Database::num_rows($res_sel) < 1) {
  1093. return false;
  1094. }
  1095. $row = Database::fetch_array($res_sel);
  1096. $previous = $row['previous_item_id'];
  1097. $next = $row['next_item_id'];
  1098. $display = $row['display_order'];
  1099. $parent = $row['parent_item_id'];
  1100. $lp = $row['lp_id'];
  1101. // Delete children items.
  1102. $num = $this->delete_children_items($id);
  1103. if ($this->debug > 2) {
  1104. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1105. }
  1106. // Now delete the item.
  1107. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1108. if ($this->debug > 2) {
  1109. error_log('New LP - Deleting item: '.$sql_del, 0);
  1110. }
  1111. Database::query($sql_del);
  1112. // Now update surrounding items.
  1113. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1114. WHERE c_id = ".$course_id." AND id = $previous";
  1115. Database::query($sql_upd);
  1116. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1117. WHERE c_id = ".$course_id." AND id = $next";
  1118. Database::query($sql_upd);
  1119. // Now update all following items with new display order.
  1120. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1121. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1122. Database::query($sql_all);
  1123. //Removing prerequisites since the item will not longer exist
  1124. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1125. Database::query($sql_all);
  1126. // Remove from search engine if enabled.
  1127. if (api_get_setting('search_enabled') == 'true') {
  1128. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1129. $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';
  1130. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1131. $res = Database::query($sql);
  1132. if (Database::num_rows($res) > 0) {
  1133. $row2 = Database::fetch_array($res);
  1134. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1135. $di = new ChamiloIndexer();
  1136. $di->remove_document((int) $row2['search_did']);
  1137. }
  1138. $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';
  1139. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1140. Database::query($sql);
  1141. }
  1142. }
  1143. /**
  1144. * Updates an item's content in place
  1145. * @param integer $id Element ID
  1146. * @param integer $parent Parent item ID
  1147. * @param integer $previous Previous item ID
  1148. * @param string $title Item title
  1149. * @param string $description Item description
  1150. * @param string $prerequisites Prerequisites (optional)
  1151. * @param array $audio The array resulting of the $_FILES[mp3] element
  1152. * @param int $max_time_allowed
  1153. * @param string $url
  1154. * @return boolean True on success, false on error
  1155. */
  1156. public function edit_item(
  1157. $id,
  1158. $parent,
  1159. $previous,
  1160. $title,
  1161. $description,
  1162. $prerequisites = '0',
  1163. $audio = array(),
  1164. $max_time_allowed = 0,
  1165. $url = ''
  1166. ) {
  1167. $course_id = api_get_course_int_id();
  1168. $_course = api_get_course_info();
  1169. if ($this->debug > 0) {
  1170. error_log('New LP - In learnpath::edit_item()', 0);
  1171. }
  1172. if (empty($max_time_allowed)) {
  1173. $max_time_allowed = 0;
  1174. }
  1175. if (empty($id) || ($id != strval(intval($id))) || empty($title)) {
  1176. return false;
  1177. }
  1178. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1179. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1180. $res_select = Database::query($sql_select);
  1181. $row_select = Database::fetch_array($res_select);
  1182. $audio_update_sql = '';
  1183. if (is_array($audio) && !empty($audio['tmp_name']) && $audio['error'] === 0) {
  1184. // Create the audio folder if it does not exist yet.
  1185. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1186. if (!is_dir($filepath.'audio')) {
  1187. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1188. $audio_id = add_document(
  1189. $_course,
  1190. '/audio',
  1191. 'folder',
  1192. 0,
  1193. 'audio'
  1194. );
  1195. api_item_property_update(
  1196. $_course,
  1197. TOOL_DOCUMENT,
  1198. $audio_id,
  1199. 'FolderCreated',
  1200. api_get_user_id(),
  1201. null,
  1202. null,
  1203. null,
  1204. null,
  1205. api_get_session_id()
  1206. );
  1207. api_item_property_update(
  1208. $_course,
  1209. TOOL_DOCUMENT,
  1210. $audio_id,
  1211. 'invisible',
  1212. api_get_user_id(),
  1213. null,
  1214. null,
  1215. null,
  1216. null,
  1217. api_get_session_id()
  1218. );
  1219. }
  1220. // Upload file in documents.
  1221. $pi = pathinfo($audio['name']);
  1222. if ($pi['extension'] == 'mp3') {
  1223. $c_det = api_get_course_info($this->cc);
  1224. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1225. $path = handle_uploaded_document(
  1226. $c_det,
  1227. $audio,
  1228. $bp,
  1229. '/audio',
  1230. api_get_user_id(),
  1231. 0,
  1232. null,
  1233. 0,
  1234. 'rename',
  1235. false,
  1236. 0
  1237. );
  1238. $path = substr($path, 7);
  1239. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1240. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1241. }
  1242. }
  1243. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1244. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1245. // TODO: htmlspecialchars to be checked for encoding related problems.
  1246. if ($same_parent && $same_previous) {
  1247. // Only update title and description.
  1248. $sql = "UPDATE ".$tbl_lp_item."
  1249. SET title = '" . Database::escape_string($title)."',
  1250. prerequisite = '" . $prerequisites."',
  1251. description = '" . Database::escape_string($description)."'
  1252. " . $audio_update_sql.",
  1253. max_time_allowed = '" . Database::escape_string($max_time_allowed)."'
  1254. WHERE c_id = ".$course_id." AND id = ".$id;
  1255. Database::query($sql);
  1256. } else {
  1257. $old_parent = $row_select['parent_item_id'];
  1258. $old_previous = $row_select['previous_item_id'];
  1259. $old_next = $row_select['next_item_id'];
  1260. $old_order = $row_select['display_order'];
  1261. $old_prerequisite = $row_select['prerequisite'];
  1262. $old_max_time_allowed = $row_select['max_time_allowed'];
  1263. /* BEGIN -- virtually remove the current item id */
  1264. /* for the next and previous item it is like the current item doesn't exist anymore */
  1265. if ($old_previous != 0) {
  1266. // Next
  1267. $sql = "UPDATE ".$tbl_lp_item."
  1268. SET next_item_id = " . $old_next."
  1269. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1270. Database::query($sql);
  1271. }
  1272. if ($old_next != 0) {
  1273. // Previous
  1274. $sql = "UPDATE ".$tbl_lp_item."
  1275. SET previous_item_id = " . $old_previous."
  1276. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1277. Database::query($sql);
  1278. }
  1279. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1280. $sql = "UPDATE ".$tbl_lp_item."
  1281. SET display_order = display_order - 1
  1282. WHERE
  1283. c_id = ".$course_id." AND
  1284. display_order > " . $old_order." AND
  1285. lp_id = " . $this->lp_id." AND
  1286. parent_item_id = " . $old_parent;
  1287. Database::query($sql);
  1288. /* END -- virtually remove the current item id */
  1289. /* BEGIN -- update the current item id to his new location */
  1290. if ($previous == 0) {
  1291. // Select the data of the item that should come after the current item.
  1292. $sql = "SELECT id, display_order
  1293. FROM " . $tbl_lp_item."
  1294. WHERE
  1295. c_id = ".$course_id." AND
  1296. lp_id = " . $this->lp_id." AND
  1297. parent_item_id = " . $parent." AND
  1298. previous_item_id = " . $previous;
  1299. $res_select_old = Database::query($sql);
  1300. $row_select_old = Database::fetch_array($res_select_old);
  1301. // If the new parent didn't have children before.
  1302. if (Database::num_rows($res_select_old) == 0) {
  1303. $new_next = 0;
  1304. $new_order = 1;
  1305. } else {
  1306. $new_next = $row_select_old['id'];
  1307. $new_order = $row_select_old['display_order'];
  1308. }
  1309. } else {
  1310. // Select the data of the item that should come before the current item.
  1311. $sql = "SELECT next_item_id, display_order
  1312. FROM " . $tbl_lp_item."
  1313. WHERE c_id = ".$course_id." AND id = ".$previous;
  1314. $res_select_old = Database::query($sql);
  1315. $row_select_old = Database::fetch_array($res_select_old);
  1316. $new_next = $row_select_old['next_item_id'];
  1317. $new_order = $row_select_old['display_order'] + 1;
  1318. }
  1319. // TODO: htmlspecialchars to be checked for encoding related problems.
  1320. // Update the current item with the new data.
  1321. $sql = "UPDATE $tbl_lp_item
  1322. SET
  1323. title = '".Database::escape_string($title)."',
  1324. description = '" . Database::escape_string($description)."',
  1325. parent_item_id = " . $parent.",
  1326. previous_item_id = " . $previous.",
  1327. next_item_id = " . $new_next.",
  1328. display_order = " . $new_order."
  1329. " . $audio_update_sql."
  1330. WHERE c_id = ".$course_id." AND id = ".$id;
  1331. Database::query($sql);
  1332. if ($previous != 0) {
  1333. // Update the previous item's next_item_id.
  1334. $sql = "UPDATE ".$tbl_lp_item."
  1335. SET next_item_id = " . $id."
  1336. WHERE c_id = ".$course_id." AND id = ".$previous;
  1337. Database::query($sql);
  1338. }
  1339. if ($new_next != 0) {
  1340. // Update the next item's previous_item_id.
  1341. $sql = "UPDATE ".$tbl_lp_item."
  1342. SET previous_item_id = " . $id."
  1343. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1344. Database::query($sql);
  1345. }
  1346. if ($old_prerequisite != $prerequisites) {
  1347. $sql = "UPDATE ".$tbl_lp_item."
  1348. SET prerequisite = '" . $prerequisites."'
  1349. WHERE c_id = ".$course_id." AND id = ".$id;
  1350. Database::query($sql);
  1351. }
  1352. if ($old_max_time_allowed != $max_time_allowed) {
  1353. // update max time allowed
  1354. $sql = "UPDATE ".$tbl_lp_item."
  1355. SET max_time_allowed = " . $max_time_allowed."
  1356. WHERE c_id = ".$course_id." AND id = ".$id;
  1357. Database::query($sql);
  1358. }
  1359. // Update all the items with the same or a bigger display_order than the current item.
  1360. $sql = "UPDATE ".$tbl_lp_item."
  1361. SET display_order = display_order + 1
  1362. WHERE
  1363. c_id = ".$course_id." AND
  1364. lp_id = " . $this->get_id()." AND
  1365. id <> " . $id." AND
  1366. parent_item_id = " . $parent." AND
  1367. display_order >= " . $new_order;
  1368. Database::query($sql);
  1369. }
  1370. if ($row_select['item_type'] == 'link') {
  1371. $link = new Link();
  1372. $linkId = $row_select['path'];
  1373. $link->updateLink($linkId, $url);
  1374. }
  1375. }
  1376. /**
  1377. * Updates an item's prereq in place
  1378. * @param integer $id Element ID
  1379. * @param string $prerequisite_id Prerequisite Element ID
  1380. * @param int $mastery_score Prerequisite min score
  1381. * @param int $max_score Prerequisite max score
  1382. *
  1383. * @return boolean True on success, false on error
  1384. */
  1385. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1386. {
  1387. $course_id = api_get_course_int_id();
  1388. if ($this->debug > 0) {
  1389. error_log('New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')', 0);
  1390. }
  1391. if (empty($id) || ($id != strval(intval($id))) || empty($prerequisite_id)) {
  1392. return false;
  1393. }
  1394. $prerequisite_id = intval($prerequisite_id);
  1395. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1396. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1397. $mastery_score = 0;
  1398. }
  1399. if (!is_numeric($max_score) || $max_score < 0) {
  1400. $max_score = 100;
  1401. }
  1402. /*if ($mastery_score > $max_score) {
  1403. $max_score = $mastery_score;
  1404. }*/
  1405. if (!is_numeric($prerequisite_id)) {
  1406. $prerequisite_id = 'NULL';
  1407. }
  1408. $mastery_score = floatval($mastery_score);
  1409. $max_score = floatval($max_score);
  1410. $sql = " UPDATE $tbl_lp_item
  1411. SET
  1412. prerequisite = $prerequisite_id ,
  1413. prerequisite_min_score = $mastery_score ,
  1414. prerequisite_max_score = $max_score
  1415. WHERE c_id = $course_id AND id = $id";
  1416. Database::query($sql);
  1417. // TODO: Update the item object (can be ignored for now because refreshed).
  1418. return true;
  1419. }
  1420. /**
  1421. * Static admin function exporting a learnpath into a zip file
  1422. * @param string Export type (scorm, zip, cd)
  1423. * @param string Course code
  1424. * @param integer Learnpath ID
  1425. * @param string Zip file name
  1426. * @return string Zip file path (or false on error)
  1427. */
  1428. public function export_lp($type, $course, $id, $zipname)
  1429. {
  1430. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1431. return false;
  1432. }
  1433. $url = '';
  1434. switch ($type) {
  1435. case 'scorm':
  1436. break;
  1437. case 'zip':
  1438. break;
  1439. case 'cdrom':
  1440. break;
  1441. }
  1442. return $url;
  1443. }
  1444. /**
  1445. * Gets all the chapters belonging to the same parent as the item/chapter given
  1446. * Can also be called as abstract method
  1447. * @param integer $id Item ID
  1448. * @return array A list of all the "brother items" (or an empty array on failure)
  1449. */
  1450. public function getSiblingDirectories($id)
  1451. {
  1452. $course_id = api_get_course_int_id();
  1453. if ($this->debug > 0) {
  1454. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1455. }
  1456. if (empty($id) || $id != strval(intval($id))) {
  1457. return array();
  1458. }
  1459. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1460. $sql_parent = "SELECT * FROM $lp_item
  1461. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1462. $res_parent = Database::query($sql_parent);
  1463. if (Database::num_rows($res_parent) > 0) {
  1464. $row_parent = Database::fetch_array($res_parent);
  1465. $parent = $row_parent['parent_item_id'];
  1466. $sql = "SELECT * FROM $lp_item
  1467. WHERE
  1468. c_id = ".$course_id." AND
  1469. parent_item_id = $parent AND
  1470. id = $id AND
  1471. item_type='dir'
  1472. ORDER BY display_order";
  1473. $res_bros = Database::query($sql);
  1474. $list = array();
  1475. while ($row_bro = Database::fetch_array($res_bros)) {
  1476. $list[] = $row_bro;
  1477. }
  1478. return $list;
  1479. }
  1480. return array();
  1481. }
  1482. /**
  1483. * Gets all the items belonging to the same parent as the item given
  1484. * Can also be called as abstract method
  1485. * @param integer $id Item ID
  1486. * @return array A list of all the "brother items" (or an empty array on failure)
  1487. */
  1488. public function get_brother_items($id)
  1489. {
  1490. $course_id = api_get_course_int_id();
  1491. if ($this->debug > 0) {
  1492. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1493. }
  1494. if (empty($id) || $id != strval(intval($id))) {
  1495. return array();
  1496. }
  1497. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1498. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1499. $res_parent = Database::query($sql_parent);
  1500. if (Database::num_rows($res_parent) > 0) {
  1501. $row_parent = Database::fetch_array($res_parent);
  1502. $parent = $row_parent['parent_item_id'];
  1503. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1504. ORDER BY display_order";
  1505. $res_bros = Database::query($sql_bros);
  1506. $list = [];
  1507. while ($row_bro = Database::fetch_array($res_bros)) {
  1508. $list[] = $row_bro;
  1509. }
  1510. return $list;
  1511. }
  1512. return [];
  1513. }
  1514. /**
  1515. * Get the specific prefix index terms of this learning path
  1516. * @param string $prefix
  1517. * @return array Array of terms
  1518. */
  1519. public function get_common_index_terms_by_prefix($prefix)
  1520. {
  1521. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1522. $terms = get_specific_field_values_list_by_prefix(
  1523. $prefix,
  1524. $this->cc,
  1525. TOOL_LEARNPATH,
  1526. $this->lp_id
  1527. );
  1528. $prefix_terms = array();
  1529. if (!empty($terms)) {
  1530. foreach ($terms as $term) {
  1531. $prefix_terms[] = $term['value'];
  1532. }
  1533. }
  1534. return $prefix_terms;
  1535. }
  1536. /**
  1537. * Gets the number of items currently completed
  1538. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1539. * @return integer The number of items currently completed
  1540. */
  1541. public function get_complete_items_count($failedStatusException = false)
  1542. {
  1543. if ($this->debug > 0) {
  1544. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1545. }
  1546. $i = 0;
  1547. $completedStatusList = array(
  1548. 'completed',
  1549. 'passed',
  1550. 'succeeded',
  1551. 'browsed'
  1552. );
  1553. if (!$failedStatusException) {
  1554. $completedStatusList[] = 'failed';
  1555. }
  1556. foreach ($this->items as $id => $dummy) {
  1557. // Trying failed and browsed considered "progressed" as well.
  1558. if ($this->items[$id]->status_is($completedStatusList) &&
  1559. $this->items[$id]->get_type() != 'dir'
  1560. ) {
  1561. $i++;
  1562. }
  1563. }
  1564. return $i;
  1565. }
  1566. /**
  1567. * Gets the current item ID
  1568. * @return integer The current learnpath item id
  1569. */
  1570. public function get_current_item_id()
  1571. {
  1572. $current = 0;
  1573. if ($this->debug > 0) {
  1574. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1575. }
  1576. if (!empty($this->current)) {
  1577. $current = $this->current;
  1578. }
  1579. if ($this->debug > 2) {
  1580. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1581. }
  1582. return $current;
  1583. }
  1584. /**
  1585. * Force to get the first learnpath item id
  1586. * @return integer The current learnpath item id
  1587. */
  1588. public function get_first_item_id()
  1589. {
  1590. $current = 0;
  1591. if (is_array($this->ordered_items)) {
  1592. $current = $this->ordered_items[0];
  1593. }
  1594. return $current;
  1595. }
  1596. /**
  1597. * Gets the total number of items available for viewing in this SCORM
  1598. * @return integer The total number of items
  1599. */
  1600. public function get_total_items_count()
  1601. {
  1602. if ($this->debug > 0) {
  1603. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1604. }
  1605. return count($this->items);
  1606. }
  1607. /**
  1608. * Gets the total number of items available for viewing in this SCORM but without chapters
  1609. * @return integer The total no-chapters number of items
  1610. */
  1611. public function getTotalItemsCountWithoutDirs()
  1612. {
  1613. if ($this->debug > 0) {
  1614. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1615. }
  1616. $total = 0;
  1617. $typeListNotToCount = self::getChapterTypes();
  1618. foreach ($this->items as $temp2) {
  1619. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1620. $total++;
  1621. }
  1622. }
  1623. return $total;
  1624. }
  1625. /**
  1626. * Gets the first element URL.
  1627. * @return string URL to load into the viewer
  1628. */
  1629. public function first()
  1630. {
  1631. if ($this->debug > 0) {
  1632. error_log('New LP - In learnpath::first()', 0);
  1633. error_log('$this->last_item_seen '.$this->last_item_seen);
  1634. }
  1635. // Test if the last_item_seen exists and is not a dir.
  1636. if (count($this->ordered_items) == 0) {
  1637. $this->index = 0;
  1638. }
  1639. if ($this->debug > 0) {
  1640. if (isset($this->items[$this->last_item_seen])) {
  1641. $status = $this->items[$this->last_item_seen]->get_status();
  1642. }
  1643. error_log('status '.$status);
  1644. }
  1645. if (!empty($this->last_item_seen) &&
  1646. !empty($this->items[$this->last_item_seen]) &&
  1647. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1648. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1649. //&& !$this->items[$this->last_item_seen]->is_done()
  1650. ) {
  1651. if ($this->debug > 2) {
  1652. 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);
  1653. }
  1654. $index = -1;
  1655. foreach ($this->ordered_items as $myindex => $item_id) {
  1656. if ($item_id == $this->last_item_seen) {
  1657. $index = $myindex;
  1658. break;
  1659. }
  1660. }
  1661. if ($index == -1) {
  1662. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1663. if ($this->debug > 2) {
  1664. error_log('New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!', 0);
  1665. }
  1666. return false;
  1667. } else {
  1668. $this->last = $this->last_item_seen;
  1669. $this->current = $this->last_item_seen;
  1670. $this->index = $index;
  1671. }
  1672. } else {
  1673. if ($this->debug > 2) {
  1674. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1675. }
  1676. $index = 0;
  1677. // Loop through all ordered items and stop at the first item that is
  1678. // not a directory *and* that has not been completed yet.
  1679. while (!empty($this->ordered_items[$index]) AND
  1680. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1681. (
  1682. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1683. $this->items[$this->ordered_items[$index]]->is_done() === true
  1684. ) AND $index < $this->max_ordered_items) {
  1685. $index++;
  1686. }
  1687. $this->last = $this->current;
  1688. // current is
  1689. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1690. $this->index = $index;
  1691. if ($this->debug > 2) {
  1692. error_log('$index '.$index);
  1693. }
  1694. if ($this->debug > 2) {
  1695. error_log('New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')', 0);
  1696. }
  1697. }
  1698. if ($this->debug > 2) {
  1699. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1700. }
  1701. }
  1702. /**
  1703. * Gets the information about an item in a format usable as JavaScript to update
  1704. * the JS API by just printing this content into the <head> section of the message frame
  1705. * @param int $item_id
  1706. * @return string
  1707. */
  1708. public function get_js_info($item_id = 0)
  1709. {
  1710. if ($this->debug > 0) {
  1711. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1712. }
  1713. $info = '';
  1714. $item_id = intval($item_id);
  1715. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1716. //if item is defined, return values from DB
  1717. $oItem = $this->items[$item_id];
  1718. $info .= '<script language="javascript">';
  1719. $info .= "top.set_score(".$oItem->get_score().");\n";
  1720. $info .= "top.set_max(".$oItem->get_max().");\n";
  1721. $info .= "top.set_min(".$oItem->get_min().");\n";
  1722. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1723. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1724. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1725. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1726. $info .= "top.set_flag_synchronized();";
  1727. $info .= '</script>';
  1728. if ($this->debug > 2) {
  1729. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1730. }
  1731. return $info;
  1732. } else {
  1733. // If item_id is empty, just update to default SCORM data.
  1734. $info .= '<script language="javascript">';
  1735. $info .= "top.set_score(".learnpathItem::get_score().");\n";
  1736. $info .= "top.set_max(".learnpathItem::get_max().");\n";
  1737. $info .= "top.set_min(".learnpathItem::get_min().");\n";
  1738. $info .= "top.set_lesson_status('".learnpathItem::get_status()."');";
  1739. $info .= "top.set_session_time('".learnpathItem::getScormTimeFromParameter('js')."');";
  1740. $info .= "top.set_suspend_data('".learnpathItem::get_suspend_data()."');";
  1741. $info .= "top.set_saved_lesson_status('".learnpathItem::get_status()."');";
  1742. $info .= "top.set_flag_synchronized();";
  1743. $info .= '</script>';
  1744. if ($this->debug > 2) {
  1745. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1746. }
  1747. return $info;
  1748. }
  1749. }
  1750. /**
  1751. * Gets the js library from the database
  1752. * @return string The name of the javascript library to be used
  1753. */
  1754. public function get_js_lib()
  1755. {
  1756. $lib = '';
  1757. if (!empty ($this->js_lib)) {
  1758. $lib = $this->js_lib;
  1759. }
  1760. return $lib;
  1761. }
  1762. /**
  1763. * Gets the learnpath database ID
  1764. * @return integer Learnpath ID in the lp table
  1765. */
  1766. public function get_id()
  1767. {
  1768. if (!empty ($this->lp_id)) {
  1769. return $this->lp_id;
  1770. } else {
  1771. return 0;
  1772. }
  1773. }
  1774. /**
  1775. * Gets the last element URL.
  1776. * @return string URL to load into the viewer
  1777. */
  1778. public function get_last()
  1779. {
  1780. if ($this->debug > 0) {
  1781. error_log('New LP - In learnpath::get_last()', 0);
  1782. }
  1783. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1784. if (count($this->ordered_items) > 0) {
  1785. $this->index = count($this->ordered_items) - 1;
  1786. return $this->ordered_items[$this->index];
  1787. }
  1788. return false;
  1789. }
  1790. /**
  1791. * Gets the navigation bar for the learnpath display screen
  1792. * @return string The HTML string to use as a navigation bar
  1793. */
  1794. public function get_navigation_bar($idBar = null, $display = null)
  1795. {
  1796. if ($this->debug > 0) {
  1797. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1798. }
  1799. if (empty($idBar)) {
  1800. $idBar = 'control-top';
  1801. }
  1802. $navbar = null;
  1803. $lp_id = $this->lp_id;
  1804. $mycurrentitemid = $this->get_current_item_id();
  1805. $reportingText = get_lang('Reporting');
  1806. $previousText = get_lang('ScormPrevious');
  1807. $nextText = get_lang('ScormNext');
  1808. $fullScreenText = get_lang('ScormExitFullScreen');
  1809. if ($this->mode == 'fullscreen') {
  1810. $navbar = '
  1811. <span id="'.$idBar.'" class="buttons">
  1812. <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="'.$reportingText.'" id="stats_link">
  1813. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText.'</span>
  1814. </a>
  1815. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid.',\'previous\');return false;" title="'.$previousText.'">
  1816. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText.'</span>
  1817. </a>
  1818. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid.',\'next\');return false;" title="'.$nextText.'">
  1819. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText.'</span>
  1820. </a>
  1821. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1822. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText.'</span>
  1823. </a>
  1824. </span>';
  1825. } else {
  1826. $navbar = '
  1827. <span id="'.$idBar.'" class="buttons text-right">
  1828. <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="'.$reportingText.'" id="stats_link">
  1829. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText.'</span>
  1830. </a>
  1831. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid.',\'previous\');return false;" title="'.$previousText.'">
  1832. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText.'</span>
  1833. </a>
  1834. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid.',\'next\');return false;" title="'.$nextText.'">
  1835. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText.'</span>
  1836. </a>
  1837. </span>';
  1838. }
  1839. return $navbar;
  1840. }
  1841. /**
  1842. * Gets the next resource in queue (url).
  1843. * @return string URL to load into the viewer
  1844. */
  1845. public function get_next_index()
  1846. {
  1847. if ($this->debug > 0) {
  1848. error_log('New LP - In learnpath::get_next_index()', 0);
  1849. }
  1850. // TODO
  1851. $index = $this->index;
  1852. $index++;
  1853. if ($this->debug > 2) {
  1854. error_log('New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type, 0);
  1855. }
  1856. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1857. $index++;
  1858. if ($index == $this->max_ordered_items) {
  1859. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1860. return $this->index;
  1861. } else {
  1862. return $index;
  1863. }
  1864. }
  1865. }
  1866. if (empty ($this->ordered_items[$index])) {
  1867. return $this->index;
  1868. }
  1869. if ($this->debug > 2) {
  1870. error_log('New LP - index is now '.$index, 0);
  1871. }
  1872. return $index;
  1873. }
  1874. /**
  1875. * Gets item_id for the next element
  1876. * @return integer Next item (DB) ID
  1877. */
  1878. public function get_next_item_id()
  1879. {
  1880. if ($this->debug > 0) {
  1881. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1882. }
  1883. $new_index = $this->get_next_index();
  1884. if (!empty ($new_index)) {
  1885. if (isset ($this->ordered_items[$new_index])) {
  1886. if ($this->debug > 2) {
  1887. error_log('New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index], 0);
  1888. }
  1889. return $this->ordered_items[$new_index];
  1890. }
  1891. }
  1892. if ($this->debug > 2) {
  1893. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1894. }
  1895. return 0;
  1896. }
  1897. /**
  1898. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1899. *
  1900. * Generally, the package provided is in the form of a zip file, so the function
  1901. * has been written to test a zip file. If not a zip, the function will return the
  1902. * default return value: ''
  1903. * @param string the path to the file
  1904. * @param string the original name of the file
  1905. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1906. */
  1907. public static function get_package_type($file_path, $file_name)
  1908. {
  1909. // Get name of the zip file without the extension.
  1910. $file_info = pathinfo($file_name);
  1911. $filename = $file_info['basename']; // Name including extension.
  1912. $extension = $file_info['extension']; // Extension only.
  1913. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1914. 'dll',
  1915. 'exe'
  1916. ))) {
  1917. return 'oogie';
  1918. }
  1919. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1920. 'dll',
  1921. 'exe'
  1922. ))) {
  1923. return 'woogie';
  1924. }
  1925. $zipFile = new PclZip($file_path);
  1926. // Check the zip content (real size and file extension).
  1927. $zipContentArray = $zipFile->listContent();
  1928. $package_type = '';
  1929. $at_root = false;
  1930. $manifest = '';
  1931. $aicc_match_crs = 0;
  1932. $aicc_match_au = 0;
  1933. $aicc_match_des = 0;
  1934. $aicc_match_cst = 0;
  1935. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1936. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1937. foreach ($zipContentArray as $thisContent) {
  1938. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1939. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1940. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1941. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1942. $package_type = 'scorm';
  1943. break; // Exit the foreach loop.
  1944. } elseif (
  1945. preg_match('/aicc\//i', $thisContent['filename']) ||
  1946. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array('crs', 'au', 'des', 'cst'))
  1947. ) {
  1948. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1949. switch ($ext) {
  1950. case 'crs':
  1951. $aicc_match_crs = 1;
  1952. break;
  1953. case 'au':
  1954. $aicc_match_au = 1;
  1955. break;
  1956. case 'des':
  1957. $aicc_match_des = 1;
  1958. break;
  1959. case 'cst':
  1960. $aicc_match_cst = 1;
  1961. break;
  1962. default:
  1963. break;
  1964. }
  1965. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1966. } else {
  1967. $package_type = '';
  1968. }
  1969. }
  1970. }
  1971. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1972. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1973. $package_type = 'aicc';
  1974. }
  1975. return $package_type;
  1976. }
  1977. /**
  1978. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1979. * @return string URL to load into the viewer
  1980. */
  1981. public function get_previous_index()
  1982. {
  1983. if ($this->debug > 0) {
  1984. error_log('New LP - In learnpath::get_previous_index()', 0);
  1985. }
  1986. $index = $this->index;
  1987. if (isset ($this->ordered_items[$index - 1])) {
  1988. $index--;
  1989. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1990. $index--;
  1991. if ($index < 0) {
  1992. return $this->index;
  1993. }
  1994. }
  1995. } else {
  1996. if ($this->debug > 2) {
  1997. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1998. }
  1999. // There is no previous item.
  2000. }
  2001. return $index;
  2002. }
  2003. /**
  2004. * Gets item_id for the next element
  2005. * @return integer Previous item (DB) ID
  2006. */
  2007. public function get_previous_item_id()
  2008. {
  2009. if ($this->debug > 0) {
  2010. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2011. }
  2012. $new_index = $this->get_previous_index();
  2013. return $this->ordered_items[$new_index];
  2014. }
  2015. /**
  2016. * Gets the progress value from the progress_db attribute
  2017. * @return integer Current progress value
  2018. * @deprecated This method does not seem to be used as of 20170514
  2019. */
  2020. public function get_progress()
  2021. {
  2022. if ($this->debug > 0) {
  2023. error_log('New LP - In learnpath::get_progress()', 0);
  2024. }
  2025. if (!empty ($this->progress_db)) {
  2026. return $this->progress_db;
  2027. }
  2028. return 0;
  2029. }
  2030. /**
  2031. * Returns the HTML necessary to print a mediaplayer block inside a page
  2032. * @return string The mediaplayer HTML
  2033. */
  2034. public function get_mediaplayer($lpItemId, $autostart = 'true')
  2035. {
  2036. $course_id = api_get_course_int_id();
  2037. $_course = api_get_course_info();
  2038. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2039. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2040. // Getting all the information about the item.
  2041. $sql = "SELECT * FROM $tbl_lp_item as lp
  2042. INNER JOIN $tbl_lp_item_view as lp_view
  2043. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2044. WHERE
  2045. lp.id = '$lpItemId' AND
  2046. lp.c_id = $course_id AND
  2047. lp_view.c_id = $course_id";
  2048. $result = Database::query($sql);
  2049. $row = Database::fetch_assoc($result);
  2050. $output = '';
  2051. if (!empty ($row['audio'])) {
  2052. $list = $_SESSION['oLP']->get_toc();
  2053. $type_quiz = false;
  2054. foreach ($list as $toc) {
  2055. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2056. $type_quiz = true;
  2057. }
  2058. }
  2059. if ($type_quiz) {
  2060. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2061. $autostart_audio = $row['status'] === 'completed' ? 'false' : 'true';
  2062. } else {
  2063. $autostart_audio = $autostart;
  2064. }
  2065. } else {
  2066. $autostart_audio = 'true';
  2067. }
  2068. $courseInfo = api_get_course_info();
  2069. $audio = $row['audio'];
  2070. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2071. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2072. if (!file_exists($file)) {
  2073. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2074. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2075. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2076. }
  2077. $player = Display::getMediaPlayer(
  2078. $file,
  2079. array(
  2080. 'id' => 'lp_audio_media_player',
  2081. 'url' => $url,
  2082. 'autoplay' => $autostart_audio,
  2083. 'width' => '100%'
  2084. )
  2085. );
  2086. // The mp3 player.
  2087. $output = '<div id="container">';
  2088. $output .= $player;
  2089. $output .= '</div>';
  2090. }
  2091. return $output;
  2092. }
  2093. /**
  2094. * @param int $studentId
  2095. * @param int $prerequisite
  2096. * @param array $courseInfo
  2097. * @param int $sessionId
  2098. *
  2099. * @return bool
  2100. *
  2101. */
  2102. public static function isBlockedByPrerequisite(
  2103. $studentId,
  2104. $prerequisite,
  2105. $courseInfo,
  2106. $sessionId
  2107. ) {
  2108. $isBlocked = false;
  2109. if (!empty($prerequisite)) {
  2110. $progress = self::getProgress(
  2111. $prerequisite,
  2112. $studentId,
  2113. $courseInfo['real_id'],
  2114. $sessionId
  2115. );
  2116. if ($progress < 100) {
  2117. $isBlocked = true;
  2118. }
  2119. }
  2120. return $isBlocked;
  2121. }
  2122. /**
  2123. * Checks if the learning path is visible for student after the progress
  2124. * of its prerequisite is completed, considering the time availability and
  2125. * the LP visibility.
  2126. * @param int $lp_id
  2127. * @param int $student_id
  2128. * @param string Course code (optional)
  2129. * @param int $sessionId
  2130. * @return bool
  2131. */
  2132. public static function is_lp_visible_for_student(
  2133. $lp_id,
  2134. $student_id,
  2135. $courseCode = null,
  2136. $sessionId = 0
  2137. ) {
  2138. $courseInfo = api_get_course_info($courseCode);
  2139. $lp_id = (int) $lp_id;
  2140. $sessionId = (int) $sessionId;
  2141. if (empty($sessionId)) {
  2142. $sessionId = api_get_session_id();
  2143. }
  2144. if (empty($courseInfo)) {
  2145. return false;
  2146. }
  2147. $itemInfo = api_get_item_property_info(
  2148. $courseInfo['real_id'],
  2149. TOOL_LEARNPATH,
  2150. $lp_id,
  2151. $sessionId
  2152. );
  2153. // If the item was deleted.
  2154. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2155. return false;
  2156. }
  2157. // @todo remove this query and load the row info as a parameter
  2158. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2159. // Get current prerequisite
  2160. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2161. FROM $tbl_learnpath
  2162. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2163. $rs = Database::query($sql);
  2164. $now = time();
  2165. if (Database::num_rows($rs) > 0) {
  2166. $row = Database::fetch_array($rs, 'ASSOC');
  2167. $prerequisite = $row['prerequisite'];
  2168. $is_visible = true;
  2169. $isBlocked = self::isBlockedByPrerequisite(
  2170. $student_id,
  2171. $prerequisite,
  2172. $courseInfo,
  2173. $sessionId
  2174. );
  2175. if ($isBlocked) {
  2176. $is_visible = false;
  2177. }
  2178. // Also check the time availability of the LP
  2179. if ($is_visible) {
  2180. // Adding visibility restrictions
  2181. if (!empty($row['publicated_on'])) {
  2182. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2183. $is_visible = false;
  2184. }
  2185. }
  2186. // Blocking empty start times see BT#2800
  2187. global $_custom;
  2188. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2189. $_custom['lps_hidden_when_no_start_date']
  2190. ) {
  2191. if (empty($row['publicated_on'])) {
  2192. $is_visible = false;
  2193. }
  2194. }
  2195. if (!empty($row['expired_on'])) {
  2196. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2197. $is_visible = false;
  2198. }
  2199. }
  2200. }
  2201. // Check if the subscription users/group to a LP is ON
  2202. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2203. // Try group
  2204. $is_visible = false;
  2205. // Checking only the user visibility
  2206. $userVisibility = api_get_item_visibility(
  2207. $courseInfo,
  2208. 'learnpath',
  2209. $row['id'],
  2210. $sessionId,
  2211. $student_id,
  2212. 'LearnpathSubscription'
  2213. );
  2214. if ($userVisibility == 1) {
  2215. $is_visible = true;
  2216. } else {
  2217. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2218. if (!empty($userGroups)) {
  2219. foreach ($userGroups as $groupInfo) {
  2220. $groupId = $groupInfo['iid'];
  2221. $userVisibility = api_get_item_visibility(
  2222. $courseInfo,
  2223. 'learnpath',
  2224. $row['id'],
  2225. $sessionId,
  2226. null,
  2227. 'LearnpathSubscription',
  2228. $groupId
  2229. );
  2230. if ($userVisibility == 1) {
  2231. $is_visible = true;
  2232. break;
  2233. }
  2234. }
  2235. }
  2236. }
  2237. }
  2238. return $is_visible;
  2239. }
  2240. return false;
  2241. }
  2242. /**
  2243. * @param int $lpId
  2244. * @param int $userId
  2245. * @param int $courseId
  2246. * @param int $sessionId
  2247. * @return int
  2248. */
  2249. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2250. {
  2251. $lpId = intval($lpId);
  2252. $userId = intval($userId);
  2253. $courseId = intval($courseId);
  2254. $sessionId = intval($sessionId);
  2255. $progress = 0;
  2256. $sessionCondition = api_get_session_condition($sessionId);
  2257. $table = Database::get_course_table(TABLE_LP_VIEW);
  2258. $sql = "SELECT * FROM $table
  2259. WHERE
  2260. c_id = ".$courseId." AND
  2261. lp_id = $lpId AND
  2262. user_id = $userId $sessionCondition";
  2263. $res = Database::query($sql);
  2264. if (Database::num_rows($res) > 0) {
  2265. $row = Database:: fetch_array($res);
  2266. $progress = $row['progress'];
  2267. }
  2268. return (int) $progress;
  2269. }
  2270. /**
  2271. * Displays a progress bar
  2272. * completed so far.
  2273. * @param integer $percentage Progress value to display
  2274. * @param string $text_add Text to display near the progress value
  2275. * @return string HTML string containing the progress bar
  2276. */
  2277. public static function get_progress_bar($percentage = -1, $text_add = '')
  2278. {
  2279. $text = $percentage.$text_add;
  2280. $output = '<div class="progress">
  2281. <div id="progress_bar_value" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' .$percentage.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2282. '. $text.'
  2283. </div>
  2284. </div>';
  2285. return $output;
  2286. }
  2287. /**
  2288. * @param string $mode can be '%' or 'abs'
  2289. * otherwise this value will be used $this->progress_bar_mode
  2290. * @return string
  2291. */
  2292. public function getProgressBar($mode = null)
  2293. {
  2294. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2295. return self::get_progress_bar($percentage, $text_add);
  2296. }
  2297. /**
  2298. * Gets the progress bar info to display inside the progress bar.
  2299. * Also used by scorm_api.php
  2300. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2301. * we display a number of completed elements per total elements
  2302. * @param integer $add Additional steps to fake as completed
  2303. * @return list array Percentage or number and symbol (% or /xx)
  2304. */
  2305. public function get_progress_bar_text($mode = '', $add = 0)
  2306. {
  2307. if ($this->debug > 0) {
  2308. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2309. }
  2310. if (empty($mode)) {
  2311. $mode = $this->progress_bar_mode;
  2312. }
  2313. $total_items = $this->getTotalItemsCountWithoutDirs();
  2314. if ($this->debug > 2) {
  2315. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2316. }
  2317. $completeItems = $this->get_complete_items_count();
  2318. if ($this->debug > 2) {
  2319. error_log('New LP - Items completed so far: '.$completeItems, 0);
  2320. }
  2321. if ($add != 0) {
  2322. $completeItems += $add;
  2323. if ($this->debug > 2) {
  2324. error_log('New LP - Items completed so far (+modifier): '.$completeItems, 0);
  2325. }
  2326. }
  2327. $text = '';
  2328. if ($completeItems > $total_items) {
  2329. $completeItems = $total_items;
  2330. }
  2331. $percentage = 0;
  2332. if ($mode == '%') {
  2333. if ($total_items > 0) {
  2334. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2335. } else {
  2336. $percentage = 0;
  2337. }
  2338. $percentage = number_format($percentage, 0);
  2339. $text = '%';
  2340. } elseif ($mode == 'abs') {
  2341. $percentage = $completeItems;
  2342. $text = '/'.$total_items;
  2343. }
  2344. return array(
  2345. $percentage,
  2346. $text
  2347. );
  2348. }
  2349. /**
  2350. * Gets the progress bar mode
  2351. * @return string The progress bar mode attribute
  2352. */
  2353. public function get_progress_bar_mode()
  2354. {
  2355. if ($this->debug > 0) {
  2356. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2357. }
  2358. if (!empty ($this->progress_bar_mode)) {
  2359. return $this->progress_bar_mode;
  2360. } else {
  2361. return '%';
  2362. }
  2363. }
  2364. /**
  2365. * Gets the learnpath proximity (remote or local)
  2366. * @return string Learnpath proximity
  2367. */
  2368. public function get_proximity()
  2369. {
  2370. if ($this->debug > 0) {
  2371. error_log('New LP - In learnpath::get_proximity()', 0);
  2372. }
  2373. if (!empty ($this->proximity)) {
  2374. return $this->proximity;
  2375. } else {
  2376. return '';
  2377. }
  2378. }
  2379. /**
  2380. * Gets the learnpath theme (remote or local)
  2381. * @return string Learnpath theme
  2382. */
  2383. public function get_theme()
  2384. {
  2385. if ($this->debug > 0) {
  2386. error_log('New LP - In learnpath::get_theme()', 0);
  2387. }
  2388. if (!empty ($this->theme)) {
  2389. return $this->theme;
  2390. } else {
  2391. return '';
  2392. }
  2393. }
  2394. /**
  2395. * Gets the learnpath session id
  2396. * @return int
  2397. */
  2398. public function get_lp_session_id()
  2399. {
  2400. if ($this->debug > 0) {
  2401. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2402. }
  2403. if (!empty ($this->lp_session_id)) {
  2404. return (int) $this->lp_session_id;
  2405. } else {
  2406. return 0;
  2407. }
  2408. }
  2409. /**
  2410. * Gets the learnpath image
  2411. * @return string Web URL of the LP image
  2412. */
  2413. public function get_preview_image()
  2414. {
  2415. if ($this->debug > 0) {
  2416. error_log('New LP - In learnpath::get_preview_image()', 0);
  2417. }
  2418. if (!empty($this->preview_image)) {
  2419. return $this->preview_image;
  2420. } else {
  2421. return '';
  2422. }
  2423. }
  2424. /**
  2425. * @param string $size
  2426. * @param string $path_type
  2427. * @return bool|string
  2428. */
  2429. public function get_preview_image_path($size = null, $path_type = 'web')
  2430. {
  2431. $preview_image = $this->get_preview_image();
  2432. if (isset($preview_image) && !empty($preview_image)) {
  2433. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2434. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2435. if (isset($size)) {
  2436. $info = pathinfo($preview_image);
  2437. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2438. if (file_exists($image_sys_path.$image_custom_size)) {
  2439. if ($path_type == 'web') {
  2440. return $image_path.$image_custom_size;
  2441. } else {
  2442. return $image_sys_path.$image_custom_size;
  2443. }
  2444. }
  2445. } else {
  2446. if ($path_type == 'web') {
  2447. return $image_path.$preview_image;
  2448. } else {
  2449. return $image_sys_path.$preview_image;
  2450. }
  2451. }
  2452. }
  2453. return false;
  2454. }
  2455. /**
  2456. * Gets the learnpath author
  2457. * @return string LP's author
  2458. */
  2459. public function get_author()
  2460. {
  2461. if ($this->debug > 0) {
  2462. error_log('New LP - In learnpath::get_author()', 0);
  2463. }
  2464. if (!empty ($this->author)) {
  2465. return $this->author;
  2466. } else {
  2467. return '';
  2468. }
  2469. }
  2470. /**
  2471. * Gets the learnpath author
  2472. * @return string LP's author
  2473. */
  2474. public function get_hide_toc_frame()
  2475. {
  2476. if ($this->debug > 0) {
  2477. error_log('New LP - In learnpath::get_author()', 0);
  2478. }
  2479. if (!empty ($this->hide_toc_frame)) {
  2480. return $this->hide_toc_frame;
  2481. } else {
  2482. return '';
  2483. }
  2484. }
  2485. /**
  2486. * Generate a new prerequisites string for a given item. If this item was a sco and
  2487. * its prerequisites were strings (instead of IDs), then transform those strings into
  2488. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2489. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2490. * same rule as the scorm_export() method
  2491. * @param integer Item ID
  2492. * @return string Prerequisites string ready for the export as SCORM
  2493. */
  2494. public function get_scorm_prereq_string($item_id)
  2495. {
  2496. if ($this->debug > 0) {
  2497. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2498. }
  2499. if (!is_object($this->items[$item_id])) {
  2500. return false;
  2501. }
  2502. /** @var learnpathItem $oItem */
  2503. $oItem = $this->items[$item_id];
  2504. $prereq = $oItem->get_prereq_string();
  2505. if (empty($prereq)) {
  2506. return '';
  2507. }
  2508. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2509. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2510. // then simply return it (with the ITEM_ prefix).
  2511. //return 'ITEM_' . $prereq;
  2512. return $this->items[$prereq]->ref;
  2513. } else {
  2514. if (isset($this->refs_list[$prereq])) {
  2515. // It's a simple string item from which the ID can be found in the refs list,
  2516. // so we can transform it directly to an ID for export.
  2517. return $this->items[$this->refs_list[$prereq]]->ref;
  2518. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2519. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2520. } else {
  2521. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2522. // and replace them, one by one, by the internal IDs (chamilo db)
  2523. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2524. // by a space as well.
  2525. $find = array(
  2526. '&',
  2527. '|',
  2528. '~',
  2529. '=',
  2530. '<>',
  2531. '{',
  2532. '}',
  2533. '*',
  2534. '(',
  2535. ')'
  2536. );
  2537. $replace = array(
  2538. ' ',
  2539. ' ',
  2540. ' ',
  2541. ' ',
  2542. ' ',
  2543. ' ',
  2544. ' ',
  2545. ' ',
  2546. ' ',
  2547. ' '
  2548. );
  2549. $prereq_mod = str_replace($find, $replace, $prereq);
  2550. $ids = explode(' ', $prereq_mod);
  2551. foreach ($ids as $id) {
  2552. $id = trim($id);
  2553. if (isset ($this->refs_list[$id])) {
  2554. $prereq = preg_replace('/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/', 'ITEM_'.$this->refs_list[$id], $prereq);
  2555. }
  2556. }
  2557. return $prereq;
  2558. }
  2559. }
  2560. }
  2561. /**
  2562. * Returns the XML DOM document's node
  2563. * @param resource Reference to a list of objects to search for the given ITEM_*
  2564. * @param string The identifier to look for
  2565. * @return mixed The reference to the element found with that identifier. False if not found
  2566. */
  2567. public function get_scorm_xml_node(& $children, $id)
  2568. {
  2569. for ($i = 0; $i < $children->length; $i++) {
  2570. $item_temp = $children->item($i);
  2571. if ($item_temp->nodeName == 'item') {
  2572. if ($item_temp->getAttribute('identifier') == $id) {
  2573. return $item_temp;
  2574. }
  2575. }
  2576. $subchildren = $item_temp->childNodes;
  2577. if ($subchildren && $subchildren->length > 0) {
  2578. $val = $this->get_scorm_xml_node($subchildren, $id);
  2579. if (is_object($val)) {
  2580. return $val;
  2581. }
  2582. }
  2583. }
  2584. return false;
  2585. }
  2586. /**
  2587. * Gets the status list for all LP's items
  2588. * @return array Array of [index] => [item ID => current status]
  2589. */
  2590. public function get_items_status_list()
  2591. {
  2592. if ($this->debug > 0) {
  2593. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2594. }
  2595. $list = array();
  2596. foreach ($this->ordered_items as $item_id) {
  2597. $list[] = array(
  2598. $item_id => $this->items[$item_id]->get_status()
  2599. );
  2600. }
  2601. return $list;
  2602. }
  2603. /**
  2604. * Return the number of interactions for the given learnpath Item View ID.
  2605. * This method can be used as static.
  2606. * @param integer Item View ID
  2607. * @param integer course id
  2608. * @return integer Number of interactions
  2609. */
  2610. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2611. {
  2612. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2613. $lp_iv_id = intval($lp_iv_id);
  2614. $course_id = intval($course_id);
  2615. $sql = "SELECT count(*) FROM $table
  2616. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2617. $res = Database::query($sql);
  2618. $num = 0;
  2619. if (Database::num_rows($res)) {
  2620. $row = Database::fetch_array($res);
  2621. $num = $row[0];
  2622. }
  2623. return $num;
  2624. }
  2625. /**
  2626. * Return the interactions as an array for the given lp_iv_id.
  2627. * This method can be used as static.
  2628. * @param integer Learnpath Item View ID
  2629. * @return array
  2630. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2631. */
  2632. public static function get_iv_interactions_array($lp_iv_id)
  2633. {
  2634. $course_id = api_get_course_int_id();
  2635. $list = array();
  2636. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2637. if (empty($lp_iv_id)) {
  2638. return array();
  2639. }
  2640. $sql = "SELECT * FROM $table
  2641. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2642. ORDER BY order_id ASC";
  2643. $res = Database::query($sql);
  2644. $num = Database::num_rows($res);
  2645. if ($num > 0) {
  2646. $list[] = array(
  2647. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2648. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2649. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2650. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2651. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2652. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2653. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2654. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2655. );
  2656. while ($row = Database::fetch_array($res)) {
  2657. $list[] = array(
  2658. 'order_id' => ($row['order_id'] + 1),
  2659. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2660. 'type' => $row['interaction_type'],
  2661. 'time' => $row['completion_time'],
  2662. //'correct_responses' => $row['correct_responses'],
  2663. 'correct_responses' => '', // Hide correct responses from students.
  2664. 'student_response' => $row['student_response'],
  2665. 'result' => $row['result'],
  2666. 'latency' => $row['latency']
  2667. );
  2668. }
  2669. }
  2670. return $list;
  2671. }
  2672. /**
  2673. * Return the number of objectives for the given learnpath Item View ID.
  2674. * This method can be used as static.
  2675. * @param integer Item View ID
  2676. * @return integer Number of objectives
  2677. */
  2678. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2679. {
  2680. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2681. $course_id = intval($course_id);
  2682. $lp_iv_id = intval($lp_iv_id);
  2683. $sql = "SELECT count(*) FROM $table
  2684. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2685. //@todo seems that this always returns 0
  2686. $res = Database::query($sql);
  2687. $num = 0;
  2688. if (Database::num_rows($res)) {
  2689. $row = Database::fetch_array($res);
  2690. $num = $row[0];
  2691. }
  2692. return $num;
  2693. }
  2694. /**
  2695. * Return the objectives as an array for the given lp_iv_id.
  2696. * This method can be used as static.
  2697. * @param integer Learnpath Item View ID
  2698. * @return array
  2699. * @todo Translate labels
  2700. */
  2701. public static function get_iv_objectives_array($lp_iv_id = 0)
  2702. {
  2703. $course_id = api_get_course_int_id();
  2704. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2705. $sql = "SELECT * FROM $table
  2706. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2707. ORDER BY order_id ASC";
  2708. $res = Database::query($sql);
  2709. $num = Database::num_rows($res);
  2710. $list = array();
  2711. if ($num > 0) {
  2712. $list[] = array(
  2713. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2714. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2715. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2716. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2717. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2718. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2719. );
  2720. while ($row = Database::fetch_array($res)) {
  2721. $list[] = array(
  2722. 'order_id' => ($row['order_id'] + 1),
  2723. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2724. 'score_raw' => $row['score_raw'],
  2725. 'score_max' => $row['score_max'],
  2726. 'score_min' => $row['score_min'],
  2727. 'status' => $row['status']
  2728. );
  2729. }
  2730. }
  2731. return $list;
  2732. }
  2733. /**
  2734. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2735. * used by get_html_toc() to be ready to display
  2736. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2737. */
  2738. public function get_toc()
  2739. {
  2740. if ($this->debug > 0) {
  2741. error_log('learnpath::get_toc()', 0);
  2742. }
  2743. $toc = array();
  2744. foreach ($this->ordered_items as $item_id) {
  2745. if ($this->debug > 2) {
  2746. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2747. }
  2748. // TODO: Change this link generation and use new function instead.
  2749. $toc[] = array(
  2750. 'id' => $item_id,
  2751. 'title' => $this->items[$item_id]->get_title(),
  2752. 'status' => $this->items[$item_id]->get_status(),
  2753. 'level' => $this->items[$item_id]->get_level(),
  2754. 'type' => $this->items[$item_id]->get_type(),
  2755. 'description' => $this->items[$item_id]->get_description(),
  2756. 'path' => $this->items[$item_id]->get_path(),
  2757. );
  2758. }
  2759. if ($this->debug > 2) {
  2760. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2761. }
  2762. return $toc;
  2763. }
  2764. /**
  2765. * Generate and return the table of contents for this learnpath. The JS
  2766. * table returned is used inside of scorm_api.php
  2767. * @return string A JS array vairiable construction
  2768. */
  2769. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2770. {
  2771. if ($this->debug > 0) {
  2772. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2773. }
  2774. $toc = $varname.' = new Array();';
  2775. foreach ($this->ordered_items as $item_id) {
  2776. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2777. }
  2778. if ($this->debug > 2) {
  2779. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2780. }
  2781. return $toc;
  2782. }
  2783. /**
  2784. * Gets the learning path type
  2785. * @param boolean Return the name? If false, return the ID. Default is false.
  2786. * @return mixed Type ID or name, depending on the parameter
  2787. */
  2788. public function get_type($get_name = false)
  2789. {
  2790. $res = false;
  2791. if ($this->debug > 0) {
  2792. error_log('New LP - In learnpath::get_type()', 0);
  2793. }
  2794. if (!empty ($this->type) && (!$get_name)) {
  2795. $res = $this->type;
  2796. }
  2797. if ($this->debug > 2) {
  2798. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2799. }
  2800. return $res;
  2801. }
  2802. /**
  2803. * Gets the learning path type as static method
  2804. * @param boolean Return the name? If false, return the ID. Default is false.
  2805. * @return mixed Type ID or name, depending on the parameter
  2806. */
  2807. public static function get_type_static($lp_id = 0)
  2808. {
  2809. $course_id = api_get_course_int_id();
  2810. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2811. $lp_id = intval($lp_id);
  2812. $sql = "SELECT lp_type FROM $tbl_lp
  2813. WHERE c_id = $course_id AND id = '".$lp_id."'";
  2814. $res = Database::query($sql);
  2815. if ($res === false) {
  2816. return null;
  2817. }
  2818. if (Database::num_rows($res) <= 0) {
  2819. return null;
  2820. }
  2821. $row = Database::fetch_array($res);
  2822. return $row['lp_type'];
  2823. }
  2824. /**
  2825. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2826. * This method can be used as abstract and is recursive
  2827. * @param integer Learnpath ID
  2828. * @param integer Parent ID of the items to look for
  2829. * @return array Ordered list of item IDs (empty array on error)
  2830. */
  2831. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2832. {
  2833. if (empty($course_id)) {
  2834. $course_id = api_get_course_int_id();
  2835. } else {
  2836. $course_id = intval($course_id);
  2837. }
  2838. $list = array();
  2839. if (empty($lp)) {
  2840. return $list;
  2841. }
  2842. $lp = intval($lp);
  2843. $parent = intval($parent);
  2844. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2845. $sql = "SELECT id FROM $tbl_lp_item
  2846. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2847. ORDER BY display_order";
  2848. $res = Database::query($sql);
  2849. while ($row = Database::fetch_array($res)) {
  2850. $sublist = self::get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2851. $list[] = $row['id'];
  2852. foreach ($sublist as $item) {
  2853. $list[] = $item;
  2854. }
  2855. }
  2856. return $list;
  2857. }
  2858. /**
  2859. * @return array
  2860. */
  2861. public static function getChapterTypes()
  2862. {
  2863. return array(
  2864. 'dir'
  2865. );
  2866. }
  2867. /**
  2868. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2869. * @return string HTML TOC ready to display
  2870. */
  2871. public function getListArrayToc($toc_list = null)
  2872. {
  2873. if ($this->debug > 0) {
  2874. error_log('In learnpath::get_html_toc()', 0);
  2875. }
  2876. if (empty($toc_list)) {
  2877. $toc_list = $this->get_toc();
  2878. }
  2879. // Temporary variables.
  2880. $mycurrentitemid = $this->get_current_item_id();
  2881. $list = [];
  2882. $arrayList = [];
  2883. $classStatus = [
  2884. 'not attempted' => 'scorm_not_attempted',
  2885. 'incomplete' => 'scorm_not_attempted',
  2886. 'failed' => 'scorm_failed',
  2887. 'completed' => 'scorm_completed',
  2888. 'passed' => 'scorm_completed',
  2889. 'succeeded' => 'scorm_completed',
  2890. 'browsed' => 'scorm_completed',
  2891. ];
  2892. foreach ($toc_list as $item) {
  2893. $list['id'] = $item['id'];
  2894. $list['status'] = $item['status'];
  2895. $cssStatus = null;
  2896. if (isset($classStatus[$item['status']])) {
  2897. $cssStatus = $classStatus[$item['status']];
  2898. }
  2899. $classStyle = ' ';
  2900. $dirTypes = self::getChapterTypes();
  2901. if (in_array($item['type'], $dirTypes)) {
  2902. $classStyle = 'scorm_item_section ';
  2903. }
  2904. if ($item['id'] == $this->current) {
  2905. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  2906. } elseif (!in_array($item['type'], $dirTypes)) {
  2907. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  2908. }
  2909. $title = $item['title'];
  2910. if (empty($title)) {
  2911. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2912. }
  2913. $title = Security::remove_XSS($item['title']);
  2914. if (empty($item['description'])) {
  2915. $list['description'] = $title;
  2916. } else {
  2917. $list['description'] = $item['description'];
  2918. }
  2919. $list['class'] = $classStyle.' '.$cssStatus;
  2920. $list['level'] = $item['level'];
  2921. $list['type'] = $item['type'];
  2922. if (in_array($item['type'], $dirTypes)) {
  2923. $list['css_level'] = 'level_'.$item['level'];
  2924. } else {
  2925. $list['css_level'] = 'level_'.$item['level']
  2926. .' scorm_type_'
  2927. .learnpath::format_scorm_type_item($item['type']);
  2928. }
  2929. if (in_array($item['type'], $dirTypes)) {
  2930. $list['title'] = stripslashes($title);
  2931. } else {
  2932. $list['title'] = stripslashes($title);
  2933. $list['url'] = $this->get_link('http', $item['id'], $toc_list);
  2934. $list['current_id'] = $mycurrentitemid;
  2935. }
  2936. $arrayList[] = $list;
  2937. }
  2938. return $arrayList;
  2939. }
  2940. /**
  2941. * Returns an HTML-formatted string ready to display with teacher buttons
  2942. * in LP view menu
  2943. * @return string HTML TOC ready to display
  2944. */
  2945. public function get_teacher_toc_buttons()
  2946. {
  2947. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2948. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2949. $html = '';
  2950. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2951. $gradebook = '';
  2952. if (!empty($_GET['gradebook'])) {
  2953. $gradebook = Security::remove_XSS($_GET['gradebook']);
  2954. }
  2955. if ($this->get_lp_session_id() == api_get_session_id()) {
  2956. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2957. $html .= '<div class="btn-group">';
  2958. $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'>".
  2959. Display::returnFontAwesomeIcon('street-view').get_lang('Overview')."</a>";
  2960. $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'>".
  2961. Display::returnFontAwesomeIcon('pencil').get_lang('Edit')."</a>";
  2962. $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">'.
  2963. Display::returnFontAwesomeIcon('cog').get_lang('Settings').'</a>';
  2964. $html .= '</div>';
  2965. $html .= '</div>';
  2966. }
  2967. }
  2968. return $html;
  2969. }
  2970. /**
  2971. * Gets the learnpath maker name - generally the editor's name
  2972. * @return string Learnpath maker name
  2973. */
  2974. public function get_maker()
  2975. {
  2976. if ($this->debug > 0) {
  2977. error_log('New LP - In learnpath::get_maker()', 0);
  2978. }
  2979. if (!empty ($this->maker)) {
  2980. return $this->maker;
  2981. } else {
  2982. return '';
  2983. }
  2984. }
  2985. /**
  2986. * Gets the learnpath name/title
  2987. * @return string Learnpath name/title
  2988. */
  2989. public function get_name()
  2990. {
  2991. if ($this->debug > 0) {
  2992. error_log('New LP - In learnpath::get_name()', 0);
  2993. }
  2994. if (!empty ($this->name)) {
  2995. return $this->name;
  2996. } else {
  2997. return 'N/A';
  2998. }
  2999. }
  3000. /**
  3001. * Gets a link to the resource from the present location, depending on item ID.
  3002. * @param string $type Type of link expected
  3003. * @param integer $item_id Learnpath item ID
  3004. * @return string $provided_toc Link to the lp_item resource
  3005. */
  3006. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3007. {
  3008. $course_id = $this->get_course_int_id();
  3009. if ($this->debug > 0) {
  3010. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3011. }
  3012. if (empty($item_id)) {
  3013. if ($this->debug > 2) {
  3014. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(), 0);
  3015. }
  3016. $item_id = $this->get_current_item_id();
  3017. }
  3018. if (empty($item_id)) {
  3019. if ($this->debug > 2) {
  3020. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3021. }
  3022. //still empty, this means there was no item_id given and we are not in an object context or
  3023. //the object property is empty, return empty link
  3024. $item_id = $this->first();
  3025. return '';
  3026. }
  3027. $file = '';
  3028. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3029. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3030. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3031. $item_id = intval($item_id);
  3032. $sql = "SELECT
  3033. l.lp_type as ltype,
  3034. l.path as lpath,
  3035. li.item_type as litype,
  3036. li.path as lipath,
  3037. li.parameters as liparams
  3038. FROM $lp_table l
  3039. INNER JOIN $lp_item_table li
  3040. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3041. WHERE li.id = $item_id ";
  3042. if ($this->debug > 2) {
  3043. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3044. }
  3045. $res = Database::query($sql);
  3046. if (Database::num_rows($res) > 0) {
  3047. $row = Database::fetch_array($res);
  3048. $lp_type = $row['ltype'];
  3049. $lp_path = $row['lpath'];
  3050. $lp_item_type = $row['litype'];
  3051. $lp_item_path = $row['lipath'];
  3052. $lp_item_params = $row['liparams'];
  3053. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3054. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3055. }
  3056. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3057. if ($type == 'http') {
  3058. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3059. } else {
  3060. $course_path = $sys_course_path; //system path
  3061. }
  3062. // 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.
  3063. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3064. $lp_type = 1;
  3065. }
  3066. if ($this->debug > 2) {
  3067. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3068. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3069. }
  3070. // Now go through the specific cases to get the end of the path
  3071. // @todo Use constants instead of int values.
  3072. switch ($lp_type) {
  3073. case 1 :
  3074. $file = self::rl_get_resource_link_for_learnpath(
  3075. $course_id,
  3076. $this->get_id(),
  3077. $item_id,
  3078. $this->get_view_id()
  3079. );
  3080. if ($this->debug > 0) {
  3081. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3082. }
  3083. switch ($lp_item_type) {
  3084. case 'dir':
  3085. $file = 'lp_content.php?type=dir';
  3086. break;
  3087. case 'link':
  3088. if (Link::is_youtube_link($file)) {
  3089. $src = Link::get_youtube_video_id($file);
  3090. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=youtube&source='.$src;
  3091. } elseif (Link::isVimeoLink($file)) {
  3092. $src = Link::getVimeoLinkId($file);
  3093. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=vimeo&source='.$src;
  3094. } else {
  3095. // If the current site is HTTPS and the link is
  3096. // HTTP, browsers will refuse opening the link
  3097. $urlId = api_get_current_access_url_id();
  3098. $url = api_get_access_url($urlId, false);
  3099. $protocol = substr($url['url'], 0, 5);
  3100. if ($protocol === 'https') {
  3101. $linkProtocol = substr($file, 0, 5);
  3102. if ($linkProtocol === 'http:') {
  3103. //this is the special intervention case
  3104. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3105. }
  3106. }
  3107. }
  3108. break;
  3109. case 'quiz':
  3110. // Check how much attempts of a exercise exits in lp
  3111. $lp_item_id = $this->get_current_item_id();
  3112. $lp_view_id = $this->get_view_id();
  3113. $prevent_reinit = null;
  3114. if (isset($this->items[$this->current])) {
  3115. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3116. }
  3117. if (empty($provided_toc)) {
  3118. if ($this->debug > 0) {
  3119. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3120. }
  3121. $list = $this->get_toc();
  3122. } else {
  3123. if ($this->debug > 0) {
  3124. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3125. }
  3126. $list = $provided_toc;
  3127. }
  3128. $type_quiz = false;
  3129. foreach ($list as $toc) {
  3130. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3131. $type_quiz = true;
  3132. }
  3133. }
  3134. if ($type_quiz) {
  3135. $lp_item_id = intval($lp_item_id);
  3136. $lp_view_id = intval($lp_view_id);
  3137. $sql = "SELECT count(*) FROM $lp_item_view_table
  3138. WHERE
  3139. c_id = $course_id AND
  3140. lp_item_id='".$lp_item_id."' AND
  3141. lp_view_id ='".$lp_view_id."' AND
  3142. status='completed'";
  3143. $result = Database::query($sql);
  3144. $row_count = Database:: fetch_row($result);
  3145. $count_item_view = (int) $row_count[0];
  3146. $not_multiple_attempt = 0;
  3147. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3148. $not_multiple_attempt = 1;
  3149. }
  3150. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3151. }
  3152. break;
  3153. }
  3154. $tmp_array = explode('/', $file);
  3155. $document_name = $tmp_array[count($tmp_array) - 1];
  3156. if (strpos($document_name, '_DELETED_')) {
  3157. $file = 'blank.php?error=document_deleted';
  3158. }
  3159. break;
  3160. case 2 :
  3161. if ($this->debug > 2) {
  3162. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3163. }
  3164. if ($lp_item_type != 'dir') {
  3165. // Quite complex here:
  3166. // We want to make sure 'http://' (and similar) links can
  3167. // be loaded as is (withouth the Chamilo path in front) but
  3168. // some contents use this form: resource.htm?resource=http://blablabla
  3169. // which means we have to find a protocol at the path's start, otherwise
  3170. // it should not be considered as an external URL.
  3171. //if ($this->prerequisites_match($item_id)) {
  3172. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3173. if ($this->debug > 2) {
  3174. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3175. }
  3176. // Distant url, return as is.
  3177. $file = $lp_item_path;
  3178. } else {
  3179. if ($this->debug > 2) {
  3180. error_log('New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3181. }
  3182. // Prevent getting untranslatable urls.
  3183. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3184. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3185. // Prepare the path.
  3186. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3187. // TODO: Fix this for urls with protocol header.
  3188. $file = str_replace('//', '/', $file);
  3189. $file = str_replace(':/', '://', $file);
  3190. if (substr($lp_path, -1) == '/') {
  3191. $lp_path = substr($lp_path, 0, -1);
  3192. }
  3193. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3194. // if file not found.
  3195. $decoded = html_entity_decode($lp_item_path);
  3196. list ($decoded) = explode('?', $decoded);
  3197. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3198. $file = self::rl_get_resource_link_for_learnpath(
  3199. $course_id,
  3200. $this->get_id(),
  3201. $item_id,
  3202. $this->get_view_id()
  3203. );
  3204. if (empty($file)) {
  3205. $file = 'blank.php?error=document_not_found';
  3206. } else {
  3207. $tmp_array = explode('/', $file);
  3208. $document_name = $tmp_array[count($tmp_array) - 1];
  3209. if (strpos($document_name, '_DELETED_')) {
  3210. $file = 'blank.php?error=document_deleted';
  3211. } else {
  3212. $file = 'blank.php?error=document_not_found';
  3213. }
  3214. }
  3215. } else {
  3216. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3217. }
  3218. }
  3219. }
  3220. // We want to use parameters if they were defined in the imsmanifest
  3221. if (strpos($file, 'blank.php') === false) {
  3222. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3223. }
  3224. } else {
  3225. $file = 'lp_content.php?type=dir';
  3226. }
  3227. break;
  3228. case 3 :
  3229. if ($this->debug > 2) {
  3230. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3231. }
  3232. // Formatting AICC HACP append URL.
  3233. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(api_get_path(WEB_CODE_PATH).'lp/aicc_hacp.php').'&';
  3234. if (!empty($lp_item_params)) {
  3235. $aicc_append .= $lp_item_params.'&';
  3236. }
  3237. if ($lp_item_type != 'dir') {
  3238. // Quite complex here:
  3239. // We want to make sure 'http://' (and similar) links can
  3240. // be loaded as is (withouth the Chamilo path in front) but
  3241. // some contents use this form: resource.htm?resource=http://blablabla
  3242. // which means we have to find a protocol at the path's start, otherwise
  3243. // it should not be considered as an external URL.
  3244. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3245. if ($this->debug > 2) {
  3246. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3247. }
  3248. // Distant url, return as is.
  3249. $file = $lp_item_path;
  3250. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3251. /*
  3252. if (stristr($file,'<servername>') !== false) {
  3253. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3254. }
  3255. */
  3256. if (stripos($file, '<servername>') !== false) {
  3257. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3258. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3259. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3260. }
  3261. //
  3262. $file .= $aicc_append;
  3263. } else {
  3264. if ($this->debug > 2) {
  3265. error_log('New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3266. }
  3267. // Prevent getting untranslatable urls.
  3268. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3269. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3270. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3271. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3272. // TODO: Fix this for urls with protocol header.
  3273. $file = str_replace('//', '/', $file);
  3274. $file = str_replace(':/', '://', $file);
  3275. $file .= $aicc_append;
  3276. }
  3277. } else {
  3278. $file = 'lp_content.php?type=dir';
  3279. }
  3280. break;
  3281. case 4 :
  3282. break;
  3283. default:
  3284. break;
  3285. }
  3286. // Replace &amp; by & because &amp; will break URL with params
  3287. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3288. }
  3289. if ($this->debug > 2) {
  3290. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3291. }
  3292. return $file;
  3293. }
  3294. /**
  3295. * Gets the latest usable view or generate a new one
  3296. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3297. * @return integer DB lp_view id
  3298. */
  3299. public function get_view($attempt_num = 0)
  3300. {
  3301. if ($this->debug > 0) {
  3302. error_log('New LP - In learnpath::get_view()', 0);
  3303. }
  3304. $search = '';
  3305. // Use $attempt_num to enable multi-views management (disabled so far).
  3306. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3307. $search = 'AND view_count = '.$attempt_num;
  3308. }
  3309. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3310. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3311. $course_id = api_get_course_int_id();
  3312. $sessionId = api_get_session_id();
  3313. $sql = "SELECT id, view_count FROM $lp_view_table
  3314. WHERE
  3315. c_id = ".$course_id." AND
  3316. lp_id = " . $this->get_id()." AND
  3317. user_id = " . $this->get_user_id()." AND
  3318. session_id = $sessionId
  3319. $search
  3320. ORDER BY view_count DESC";
  3321. $res = Database::query($sql);
  3322. if (Database::num_rows($res) > 0) {
  3323. $row = Database::fetch_array($res);
  3324. $this->lp_view_id = $row['id'];
  3325. } else if (!api_is_invitee()) {
  3326. // There is no database record, create one.
  3327. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3328. ($course_id, ".$this->get_id().",".$this->get_user_id().", 1, $sessionId)";
  3329. Database::query($sql);
  3330. $id = Database::insert_id();
  3331. $this->lp_view_id = $id;
  3332. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3333. Database::query($sql);
  3334. }
  3335. return $this->lp_view_id;
  3336. }
  3337. /**
  3338. * Gets the current view id
  3339. * @return integer View ID (from lp_view)
  3340. */
  3341. public function get_view_id()
  3342. {
  3343. if ($this->debug > 0) {
  3344. error_log('New LP - In learnpath::get_view_id()', 0);
  3345. }
  3346. if (!empty ($this->lp_view_id)) {
  3347. return $this->lp_view_id;
  3348. } else {
  3349. return 0;
  3350. }
  3351. }
  3352. /**
  3353. * Gets the update queue
  3354. * @return array Array containing IDs of items to be updated by JavaScript
  3355. */
  3356. public function get_update_queue()
  3357. {
  3358. if ($this->debug > 0) {
  3359. error_log('New LP - In learnpath::get_update_queue()', 0);
  3360. }
  3361. return $this->update_queue;
  3362. }
  3363. /**
  3364. * Gets the user ID
  3365. * @return integer User ID
  3366. */
  3367. public function get_user_id()
  3368. {
  3369. if ($this->debug > 0) {
  3370. error_log('New LP - In learnpath::get_user_id()', 0);
  3371. }
  3372. if (!empty ($this->user_id)) {
  3373. return $this->user_id;
  3374. } else {
  3375. return false;
  3376. }
  3377. }
  3378. /**
  3379. * Checks if any of the items has an audio element attached
  3380. * @return bool True or false
  3381. */
  3382. public function has_audio()
  3383. {
  3384. if ($this->debug > 1) {
  3385. error_log('New LP - In learnpath::has_audio()', 0);
  3386. }
  3387. $has = false;
  3388. foreach ($this->items as $i => $item) {
  3389. if (!empty ($this->items[$i]->audio)) {
  3390. $has = true;
  3391. break;
  3392. }
  3393. }
  3394. return $has;
  3395. }
  3396. /**
  3397. * Logs a message into a file
  3398. * @param string Message to log
  3399. * @return boolean True on success, false on error or if msg empty
  3400. */
  3401. public function log($msg)
  3402. {
  3403. if ($this->debug > 0) {
  3404. error_log('New LP - In learnpath::log()', 0);
  3405. }
  3406. // TODO
  3407. $this->error .= $msg;
  3408. return true;
  3409. }
  3410. /**
  3411. * Moves an item up and down at its level
  3412. * @param integer Item to move up and down
  3413. * @param string Direction 'up' or 'down'
  3414. * @return integer New display order, or false on error
  3415. */
  3416. public function move_item($id, $direction)
  3417. {
  3418. $course_id = api_get_course_int_id();
  3419. if ($this->debug > 0) {
  3420. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3421. }
  3422. if (empty($id) || empty($direction)) {
  3423. return false;
  3424. }
  3425. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3426. $sql_sel = "SELECT *
  3427. FROM " . $tbl_lp_item."
  3428. WHERE c_id = ".$course_id." AND id = ".$id;
  3429. $res_sel = Database::query($sql_sel);
  3430. // Check if elem exists.
  3431. if (Database::num_rows($res_sel) < 1) {
  3432. return false;
  3433. }
  3434. // Gather data.
  3435. $row = Database::fetch_array($res_sel);
  3436. $previous = $row['previous_item_id'];
  3437. $next = $row['next_item_id'];
  3438. $display = $row['display_order'];
  3439. $parent = $row['parent_item_id'];
  3440. $lp = $row['lp_id'];
  3441. // Update the item (switch with previous/next one).
  3442. switch ($direction) {
  3443. case 'up':
  3444. if ($this->debug > 2) {
  3445. error_log('Movement up detected', 0);
  3446. }
  3447. if ($display > 1) {
  3448. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3449. WHERE c_id = ".$course_id." AND id = $previous";
  3450. if ($this->debug > 2) {
  3451. error_log('Selecting previous: '.$sql_sel2, 0);
  3452. }
  3453. $res_sel2 = Database::query($sql_sel2);
  3454. if (Database::num_rows($res_sel2) < 1) {
  3455. $previous_previous = 0;
  3456. }
  3457. // Gather data.
  3458. $row2 = Database::fetch_array($res_sel2);
  3459. $previous_previous = $row2['previous_item_id'];
  3460. // Update previous_previous item (switch "next" with current).
  3461. if ($previous_previous != 0) {
  3462. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3463. next_item_id = $id
  3464. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3465. if ($this->debug > 2) {
  3466. error_log($sql_upd2, 0);
  3467. }
  3468. Database::query($sql_upd2);
  3469. }
  3470. // Update previous item (switch with current).
  3471. if ($previous != 0) {
  3472. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3473. next_item_id = $next,
  3474. previous_item_id = $id,
  3475. display_order = display_order +1
  3476. WHERE c_id = ".$course_id." AND id = $previous";
  3477. if ($this->debug > 2) {
  3478. error_log($sql_upd2, 0);
  3479. }
  3480. Database::query($sql_upd2);
  3481. }
  3482. // Update current item (switch with previous).
  3483. if ($id != 0) {
  3484. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3485. next_item_id = $previous,
  3486. previous_item_id = $previous_previous,
  3487. display_order = display_order-1
  3488. WHERE c_id = ".$course_id." AND id = $id";
  3489. if ($this->debug > 2) {
  3490. error_log($sql_upd2, 0);
  3491. }
  3492. Database::query($sql_upd2);
  3493. }
  3494. // Update next item (new previous item).
  3495. if ($next != 0) {
  3496. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3497. WHERE c_id = ".$course_id." AND id = $next";
  3498. if ($this->debug > 2) {
  3499. error_log($sql_upd2, 0);
  3500. }
  3501. Database::query($sql_upd2);
  3502. }
  3503. $display = $display - 1;
  3504. }
  3505. break;
  3506. case 'down':
  3507. if ($this->debug > 2) {
  3508. error_log('Movement down detected', 0);
  3509. }
  3510. if ($next != 0) {
  3511. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3512. if ($this->debug > 2) {
  3513. error_log('Selecting next: '.$sql_sel2, 0);
  3514. }
  3515. $res_sel2 = Database::query($sql_sel2);
  3516. if (Database::num_rows($res_sel2) < 1) {
  3517. $next_next = 0;
  3518. }
  3519. // Gather data.
  3520. $row2 = Database::fetch_array($res_sel2);
  3521. $next_next = $row2['next_item_id'];
  3522. // Update previous item (switch with current).
  3523. if ($previous != 0) {
  3524. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3525. WHERE c_id = ".$course_id." AND id = $previous";
  3526. Database::query($sql_upd2);
  3527. }
  3528. // Update current item (switch with previous).
  3529. if ($id != 0) {
  3530. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3531. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3532. WHERE c_id = ".$course_id." AND id = $id";
  3533. Database::query($sql_upd2);
  3534. }
  3535. // Update next item (new previous item).
  3536. if ($next != 0) {
  3537. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3538. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3539. WHERE c_id = ".$course_id." AND id = $next";
  3540. Database::query($sql_upd2);
  3541. }
  3542. // Update next_next item (switch "previous" with current).
  3543. if ($next_next != 0) {
  3544. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3545. previous_item_id = $id
  3546. WHERE c_id = ".$course_id." AND id = $next_next";
  3547. Database::query($sql_upd2);
  3548. }
  3549. $display = $display + 1;
  3550. }
  3551. break;
  3552. default:
  3553. return false;
  3554. }
  3555. return $display;
  3556. }
  3557. /**
  3558. * Move a LP up (display_order)
  3559. * @param int $lp_id Learnpath ID
  3560. * @param int $categoryId
  3561. * @return bool
  3562. */
  3563. public static function move_up($lp_id, $categoryId = 0)
  3564. {
  3565. $courseId = api_get_course_int_id();
  3566. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3567. $categoryCondition = '';
  3568. if (!empty($categoryId)) {
  3569. $categoryId = (int) $categoryId;
  3570. $categoryCondition = " AND category_id = $categoryId";
  3571. }
  3572. $sql = "SELECT * FROM $lp_table
  3573. WHERE c_id = $courseId
  3574. $categoryCondition
  3575. ORDER BY display_order";
  3576. $res = Database::query($sql);
  3577. if ($res === false) {
  3578. return false;
  3579. }
  3580. $lps = [];
  3581. $lp_order = [];
  3582. $num = Database::num_rows($res);
  3583. // First check the order is correct, globally (might be wrong because
  3584. // of versions < 1.8.4)
  3585. if ($num > 0) {
  3586. $i = 1;
  3587. while ($row = Database::fetch_array($res)) {
  3588. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3589. $sql = "UPDATE $lp_table SET display_order = $i
  3590. WHERE c_id = $courseId AND id = ".$row['id'];
  3591. Database::query($sql);
  3592. }
  3593. $row['display_order'] = $i;
  3594. $lps[$row['id']] = $row;
  3595. $lp_order[$i] = $row['id'];
  3596. $i++;
  3597. }
  3598. }
  3599. if ($num > 1) { // If there's only one element, no need to sort.
  3600. $order = $lps[$lp_id]['display_order'];
  3601. if ($order > 1) { // If it's the first element, no need to move up.
  3602. $sql = "UPDATE $lp_table SET display_order = $order
  3603. WHERE c_id = $courseId AND id = ".$lp_order[$order - 1];
  3604. Database::query($sql);
  3605. $sql = "UPDATE $lp_table SET display_order = ".($order - 1)."
  3606. WHERE c_id = $courseId AND id = ".$lp_id;
  3607. Database::query($sql);
  3608. }
  3609. }
  3610. return true;
  3611. }
  3612. /**
  3613. * Move a learnpath down (display_order)
  3614. * @param int $lp_id Learnpath ID
  3615. * @param int $categoryId
  3616. * @return bool
  3617. */
  3618. public static function move_down($lp_id, $categoryId = 0)
  3619. {
  3620. $courseId = api_get_course_int_id();
  3621. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3622. $categoryCondition = '';
  3623. if (!empty($categoryId)) {
  3624. $categoryId = (int) $categoryId;
  3625. $categoryCondition = " AND category_id = $categoryId";
  3626. }
  3627. $sql = "SELECT * FROM $lp_table
  3628. WHERE c_id = $courseId
  3629. $categoryCondition
  3630. ORDER BY display_order";
  3631. $res = Database::query($sql);
  3632. if ($res === false) {
  3633. return false;
  3634. }
  3635. $lps = [];
  3636. $lp_order = [];
  3637. $num = Database::num_rows($res);
  3638. $max = 0;
  3639. // First check the order is correct, globally (might be wrong because
  3640. // of versions < 1.8.4).
  3641. if ($num > 0) {
  3642. $i = 1;
  3643. while ($row = Database::fetch_array($res)) {
  3644. $max = $i;
  3645. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3646. $need_fix = true;
  3647. $sql_u = "UPDATE $lp_table SET display_order = $i
  3648. WHERE c_id = ".$courseId." AND id = ".$row['id'];
  3649. Database::query($sql_u);
  3650. }
  3651. $row['display_order'] = $i;
  3652. $lps[$row['id']] = $row;
  3653. $lp_order[$i] = $row['id'];
  3654. $i++;
  3655. }
  3656. }
  3657. if ($num > 1) { // If there's only one element, no need to sort.
  3658. $order = $lps[$lp_id]['display_order'];
  3659. if ($order < $max) { // If it's the first element, no need to move up.
  3660. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3661. WHERE c_id = ".$courseId." AND id = ".$lp_order[$order + 1];
  3662. Database::query($sql_u1);
  3663. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3664. WHERE c_id = ".$courseId." AND id = ".$lp_id;
  3665. Database::query($sql_u2);
  3666. }
  3667. }
  3668. return true;
  3669. }
  3670. /**
  3671. * Updates learnpath attributes to point to the next element
  3672. * The last part is similar to set_current_item but processing the other way around
  3673. */
  3674. public function next()
  3675. {
  3676. if ($this->debug > 0) {
  3677. error_log('New LP - In learnpath::next()', 0);
  3678. }
  3679. $this->last = $this->get_current_item_id();
  3680. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3681. $this->autocomplete_parents($this->last);
  3682. $new_index = $this->get_next_index();
  3683. if ($this->debug > 2) {
  3684. error_log('New LP - New index: '.$new_index, 0);
  3685. }
  3686. $this->index = $new_index;
  3687. if ($this->debug > 2) {
  3688. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3689. }
  3690. $this->current = $this->ordered_items[$new_index];
  3691. if ($this->debug > 2) {
  3692. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3693. }
  3694. }
  3695. /**
  3696. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3697. * class, this might be redefined to allow several behaviours depending on the document type.
  3698. * @param integer Resource ID
  3699. * @return boolean True on success, false otherwise
  3700. */
  3701. public function open($id)
  3702. {
  3703. if ($this->debug > 0) {
  3704. error_log('New LP - In learnpath::open()', 0);
  3705. }
  3706. // TODO:
  3707. // set the current resource attribute to this resource
  3708. // switch on element type (redefine in child class?)
  3709. // set status for this item to "opened"
  3710. // start timer
  3711. // initialise score
  3712. $this->index = 0; //or = the last item seen (see $this->last)
  3713. }
  3714. /**
  3715. * Check that all prerequisites are fulfilled. Returns true and an
  3716. * empty string on success, returns false
  3717. * and the prerequisite string on error.
  3718. * This function is based on the rules for aicc_script language as
  3719. * described in the SCORM 1.2 CAM documentation page 108.
  3720. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3721. * @return boolean True if prerequisites are matched, false otherwise -
  3722. * Empty string if true returned, prerequisites string otherwise.
  3723. */
  3724. public function prerequisites_match($itemId = null)
  3725. {
  3726. $debug = $this->debug;
  3727. if ($debug > 0) {
  3728. error_log('In learnpath::prerequisites_match()', 0);
  3729. }
  3730. if (empty($itemId)) {
  3731. $itemId = $this->current;
  3732. }
  3733. $currentItem = $this->getItem($itemId);
  3734. if ($currentItem) {
  3735. if ($this->type == 2) {
  3736. // Getting prereq from scorm
  3737. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3738. } else {
  3739. $prereq_string = $currentItem->get_prereq_string();
  3740. }
  3741. if (empty($prereq_string)) {
  3742. if ($debug > 0) {
  3743. error_log('Found prereq_string is empty return true');
  3744. }
  3745. return true;
  3746. }
  3747. // Clean spaces.
  3748. $prereq_string = str_replace(' ', '', $prereq_string);
  3749. if ($debug > 0) {
  3750. error_log('Found prereq_string: '.$prereq_string, 0);
  3751. }
  3752. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3753. $result = $currentItem->parse_prereq(
  3754. $prereq_string,
  3755. $this->items,
  3756. $this->refs_list,
  3757. $this->get_user_id()
  3758. );
  3759. if ($result === false) {
  3760. $this->set_error_msg($currentItem->prereq_alert);
  3761. }
  3762. } else {
  3763. $result = true;
  3764. if ($debug > 1) {
  3765. error_log('$this->items['.$itemId.'] was not an object', 0);
  3766. }
  3767. }
  3768. if ($debug > 1) {
  3769. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3770. }
  3771. return $result;
  3772. }
  3773. /**
  3774. * Updates learnpath attributes to point to the previous element
  3775. * The last part is similar to set_current_item but processing the other way around
  3776. */
  3777. public function previous()
  3778. {
  3779. if ($this->debug > 0) {
  3780. error_log('New LP - In learnpath::previous()', 0);
  3781. }
  3782. $this->last = $this->get_current_item_id();
  3783. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3784. $this->autocomplete_parents($this->last);
  3785. $new_index = $this->get_previous_index();
  3786. $this->index = $new_index;
  3787. $this->current = $this->ordered_items[$new_index];
  3788. }
  3789. /**
  3790. * Publishes a learnpath. This basically means show or hide the learnpath
  3791. * to normal users.
  3792. * Can be used as abstract
  3793. * @param integer Learnpath ID
  3794. * @param string New visibility
  3795. */
  3796. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3797. {
  3798. $action = 'visible';
  3799. if ($set_visibility != 1) {
  3800. $action = 'invisible';
  3801. self::toggle_publish($lp_id, 'i');
  3802. }
  3803. return api_item_property_update(
  3804. api_get_course_info(),
  3805. TOOL_LEARNPATH,
  3806. $lp_id,
  3807. $action,
  3808. api_get_user_id()
  3809. );
  3810. }
  3811. /**
  3812. * Publishes a learnpath category.
  3813. * This basically means show or hide the learnpath category to normal users.
  3814. * @param int $id
  3815. * @param int $visibility
  3816. * @return bool
  3817. */
  3818. public static function toggleCategoryVisibility($id, $visibility = 1)
  3819. {
  3820. $action = 'visible';
  3821. if ($visibility != 1) {
  3822. $action = 'invisible';
  3823. $list = new LearnpathList(api_get_user_id(), null, null, null, false, $id);
  3824. $learPaths = $list->get_flat_list();
  3825. foreach ($learPaths as $lp) {
  3826. learnpath::toggle_visibility($lp['iid'], 0);
  3827. }
  3828. learnpath::toggleCategoryPublish($id, 0);
  3829. }
  3830. return api_item_property_update(
  3831. api_get_course_info(),
  3832. TOOL_LEARNPATH_CATEGORY,
  3833. $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 = Database::escape_string($row['name']);
  3857. if ($set_visibility == 'i') {
  3858. $v = 0;
  3859. }
  3860. if ($set_visibility == 'v') {
  3861. $v = 1;
  3862. }
  3863. $session_id = api_get_session_id();
  3864. $session_condition = api_get_session_condition($session_id);
  3865. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  3866. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3867. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3868. $sql = "SELECT * FROM $tbl_tool
  3869. WHERE
  3870. c_id = $course_id AND
  3871. (link = '$link' OR link = '$oldLink') AND
  3872. image = 'scormbuilder.gif' AND
  3873. (
  3874. link LIKE '$link%' OR
  3875. link LIKE '$oldLink%'
  3876. )
  3877. $session_condition
  3878. ";
  3879. $result = Database::query($sql);
  3880. $num = Database::num_rows($result);
  3881. if ($set_visibility == 'i' && $num > 0) {
  3882. $sql = "DELETE FROM $tbl_tool
  3883. WHERE
  3884. c_id = $course_id AND
  3885. (link = '$link' OR link = '$oldLink') AND
  3886. image='scormbuilder.gif'
  3887. $session_condition";
  3888. Database::query($sql);
  3889. } elseif ($set_visibility == 'v' && $num == 0) {
  3890. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3891. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3892. Database::query($sql);
  3893. $insertId = Database::insert_id();
  3894. if ($insertId) {
  3895. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3896. Database::query($sql);
  3897. }
  3898. } elseif ($set_visibility == 'v' && $num > 0) {
  3899. $sql = "UPDATE $tbl_tool SET
  3900. c_id = $course_id,
  3901. name = '$name',
  3902. link = '$link',
  3903. image = 'scormbuilder.gif',
  3904. visibility = '$v',
  3905. admin = '0',
  3906. address = 'pastillegris.gif',
  3907. added_tool = 0,
  3908. session_id = $session_id
  3909. WHERE
  3910. c_id = ".$course_id." AND
  3911. (link = '$link' OR link = '$oldLink') AND
  3912. image='scormbuilder.gif'
  3913. $session_condition
  3914. ";
  3915. Database::query($sql);
  3916. } else {
  3917. // Parameter and database incompatible, do nothing, exit.
  3918. return false;
  3919. }
  3920. } else {
  3921. return false;
  3922. }
  3923. }
  3924. /**
  3925. * Generate the link for a learnpath category as course tool
  3926. * @param int $categoryId
  3927. * @return string
  3928. */
  3929. private static function getCategoryLinkForTool($categoryId)
  3930. {
  3931. $link = 'lp/lp_controller.php?'.api_get_cidreq().'&'
  3932. .http_build_query(
  3933. [
  3934. 'action' => 'view_category',
  3935. 'id' => $categoryId
  3936. ]
  3937. );
  3938. return $link;
  3939. }
  3940. /**
  3941. * Publishes a learnpath.
  3942. * Show or hide the learnpath category on the course homepage
  3943. * @param int $id
  3944. * @param int $setVisibility
  3945. * @return bool
  3946. */
  3947. public static function toggleCategoryPublish($id, $setVisibility = 1)
  3948. {
  3949. $courseId = api_get_course_int_id();
  3950. $sessionId = api_get_session_id();
  3951. $sessionCondition = api_get_session_condition($sessionId, true, false, 't.sessionId');
  3952. $em = Database::getManager();
  3953. /** @var CLpCategory $category */
  3954. $category = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  3955. if (!$category) {
  3956. return false;
  3957. }
  3958. $link = self::getCategoryLinkForTool($id);
  3959. /** @var CTool $tool */
  3960. $tool = $em
  3961. ->createQuery("
  3962. SELECT t FROM ChamiloCourseBundle:CTool t
  3963. WHERE
  3964. t.cId = :course AND
  3965. t.link = :link1 AND
  3966. t.image = 'lp_category.gif' AND
  3967. t.link LIKE :link2
  3968. $sessionCondition
  3969. ")
  3970. ->setParameters([
  3971. 'course' => (int) $courseId,
  3972. 'link1' => $link,
  3973. 'link2' => "$link%"
  3974. ])
  3975. ->getOneOrNullResult();
  3976. if ($setVisibility == 0 && $tool) {
  3977. $em->remove($tool);
  3978. $em->flush();
  3979. return true;
  3980. }
  3981. if ($setVisibility == 1 && !$tool) {
  3982. $tool = new CTool();
  3983. $tool
  3984. ->setCategory('authoring')
  3985. ->setCId($courseId)
  3986. ->setName(strip_tags($category->getName()))
  3987. ->setLink($link)
  3988. ->setImage('lp_category.gif')
  3989. ->setVisibility(1)
  3990. ->setAdmin(0)
  3991. ->setAddress('pastillegris.gif')
  3992. ->setAddedTool(0)
  3993. ->setSessionId($sessionId)
  3994. ->setTarget('_self');
  3995. $em->persist($tool);
  3996. $em->flush();
  3997. $tool->setId($tool->getIid());
  3998. $em->persist($tool);
  3999. $em->flush();
  4000. return true;
  4001. }
  4002. if ($setVisibility == 1 && $tool) {
  4003. $tool
  4004. ->setName(strip_tags($category->getName()))
  4005. ->setVisibility(1);
  4006. $em->persist($tool);
  4007. $em->flush();
  4008. return true;
  4009. }
  4010. return false;
  4011. }
  4012. /**
  4013. * Check if the learnpath category is visible for a user
  4014. * @param CLpCategory $category
  4015. * @param User $user
  4016. * @return bool
  4017. */
  4018. public static function categoryIsVisibleForStudent(
  4019. CLpCategory $category,
  4020. User $user
  4021. )
  4022. {
  4023. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  4024. if ($isAllowedToEdit) {
  4025. return true;
  4026. }
  4027. $users = $category->getUsers();
  4028. if (empty($users) || !$users->count()) {
  4029. return true;
  4030. }
  4031. if ($category->hasUserAdded($user)) {
  4032. return true;
  4033. }
  4034. return false;
  4035. }
  4036. /**
  4037. * Check if a learnpath category is published as course tool
  4038. * @param CLpCategory $category
  4039. * @param int $courseId
  4040. * @return bool
  4041. */
  4042. public static function categoryIsPusblished(
  4043. CLpCategory $category,
  4044. $courseId
  4045. )
  4046. {
  4047. $link = self::getCategoryLinkForTool($category->getId());
  4048. $em = Database::getManager();
  4049. $tools = $em
  4050. ->createQuery("
  4051. SELECT t FROM ChamiloCourseBundle:CTool t
  4052. WHERE t.cId = :course AND
  4053. t.name = :name AND
  4054. t.image = 'lp_category.gif' AND
  4055. t.link LIKE :link
  4056. ")
  4057. ->setParameters([
  4058. 'course' => $courseId,
  4059. 'name' => strip_tags($category->getName()),
  4060. 'link' => "$link%"
  4061. ])
  4062. ->getResult();
  4063. /** @var CTool $tool */
  4064. $tool = current($tools);
  4065. return $tool ? $tool->getVisibility() : false;
  4066. }
  4067. /**
  4068. * Restart the whole learnpath. Return the URL of the first element.
  4069. * Make sure the results are saved with anoter method. This method should probably be
  4070. * redefined in children classes.
  4071. * To use a similar method statically, use the create_new_attempt() method
  4072. * @return string URL to load in the viewer
  4073. */
  4074. public function restart()
  4075. {
  4076. if ($this->debug > 0) {
  4077. error_log('New LP - In learnpath::restart()', 0);
  4078. }
  4079. // TODO
  4080. // Call autosave method to save the current progress.
  4081. //$this->index = 0;
  4082. if (api_is_invitee()) {
  4083. return false;
  4084. }
  4085. $session_id = api_get_session_id();
  4086. $course_id = api_get_course_int_id();
  4087. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  4088. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  4089. VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  4090. if ($this->debug > 2) {
  4091. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  4092. }
  4093. Database::query($sql);
  4094. $view_id = Database::insert_id();
  4095. if ($view_id) {
  4096. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  4097. Database::query($sql);
  4098. $this->lp_view_id = $view_id;
  4099. $this->attempt = $this->attempt + 1;
  4100. } else {
  4101. $this->error = 'Could not insert into item_view table...';
  4102. return false;
  4103. }
  4104. $this->autocomplete_parents($this->current);
  4105. foreach ($this->items as $index => $dummy) {
  4106. $this->items[$index]->restart();
  4107. $this->items[$index]->set_lp_view($this->lp_view_id);
  4108. }
  4109. $this->first();
  4110. return true;
  4111. }
  4112. /**
  4113. * Saves the current item
  4114. * @return boolean
  4115. */
  4116. public function save_current()
  4117. {
  4118. if ($this->debug > 0) {
  4119. error_log('learnpath::save_current()', 0);
  4120. }
  4121. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4122. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4123. if ($this->debug > 2) {
  4124. error_log('New LP - save_current() saving item '.$this->current, 0);
  4125. }
  4126. if ($this->debug > 2) {
  4127. error_log(''.print_r($this->items, true), 0);
  4128. }
  4129. if (isset($this->items[$this->current]) &&
  4130. is_object($this->items[$this->current])
  4131. ) {
  4132. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  4133. $this->autocomplete_parents($this->current);
  4134. $status = $this->items[$this->current]->get_status();
  4135. $this->update_queue[$this->current] = $status;
  4136. return $res;
  4137. }
  4138. return false;
  4139. }
  4140. /**
  4141. * Saves the given item
  4142. * @param integer $item_id Optional (will take from $_REQUEST if null)
  4143. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4144. * @return boolean
  4145. */
  4146. public function save_item($item_id = null, $from_outside = true)
  4147. {
  4148. $debug = $this->debug;
  4149. if ($debug) {
  4150. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  4151. }
  4152. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4153. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4154. if (empty($item_id)) {
  4155. $item_id = intval($_REQUEST['id']);
  4156. }
  4157. if (empty($item_id)) {
  4158. $item_id = $this->get_current_item_id();
  4159. }
  4160. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4161. if ($debug) {
  4162. error_log('Object exists');
  4163. }
  4164. // Saving the item.
  4165. $res = $this->items[$item_id]->save(
  4166. $from_outside,
  4167. $this->prerequisites_match($item_id)
  4168. );
  4169. if ($debug) {
  4170. error_log('update_queue before:');
  4171. error_log(print_r($this->update_queue, 1));
  4172. }
  4173. $this->autocomplete_parents($item_id);
  4174. $status = $this->items[$item_id]->get_status();
  4175. $this->update_queue[$item_id] = $status;
  4176. if ($debug) {
  4177. error_log('get_status(): '.$status);
  4178. error_log('update_queue after:');
  4179. error_log(print_r($this->update_queue, 1));
  4180. }
  4181. return $res;
  4182. }
  4183. return false;
  4184. }
  4185. /**
  4186. * Saves the last item seen's ID only in case
  4187. */
  4188. public function save_last()
  4189. {
  4190. $course_id = api_get_course_int_id();
  4191. if ($this->debug > 0) {
  4192. error_log('New LP - In learnpath::save_last()', 0);
  4193. }
  4194. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4195. $table = Database::get_course_table(TABLE_LP_VIEW);
  4196. if (isset($this->current) && !api_is_invitee()) {
  4197. if ($this->debug > 2) {
  4198. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  4199. }
  4200. $sql = "UPDATE $table SET
  4201. last_item = ".intval($this->get_current_item_id())."
  4202. WHERE
  4203. c_id = $course_id AND
  4204. lp_id = ".$this->get_id()." AND
  4205. user_id = " . $this->get_user_id()." ".$session_condition;
  4206. if ($this->debug > 2) {
  4207. error_log('New LP - Saving last item seen : '.$sql, 0);
  4208. }
  4209. Database::query($sql);
  4210. }
  4211. if (!api_is_invitee()) {
  4212. // Save progress.
  4213. list($progress,) = $this->get_progress_bar_text('%');
  4214. if ($progress >= 0 && $progress <= 100) {
  4215. $progress = (int) $progress;
  4216. $sql = "UPDATE $table SET
  4217. progress = $progress
  4218. WHERE
  4219. c_id = ".$course_id." AND
  4220. lp_id = " . $this->get_id()." AND
  4221. user_id = " . $this->get_user_id()." ".$session_condition;
  4222. // Ignore errors as some tables might not have the progress field just yet.
  4223. Database::query($sql);
  4224. $this->progress_db = $progress;
  4225. }
  4226. }
  4227. }
  4228. /**
  4229. * Sets the current item ID (checks if valid and authorized first)
  4230. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4231. */
  4232. public function set_current_item($item_id = null)
  4233. {
  4234. if ($this->debug > 0) {
  4235. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  4236. }
  4237. if (empty ($item_id)) {
  4238. if ($this->debug > 2) {
  4239. error_log('New LP - No new current item given, ignore...', 0);
  4240. }
  4241. // Do nothing.
  4242. } else {
  4243. if ($this->debug > 2) {
  4244. error_log('New LP - New current item given is '.$item_id.'...', 0);
  4245. }
  4246. if (is_numeric($item_id)) {
  4247. $item_id = intval($item_id);
  4248. // TODO: Check in database here.
  4249. $this->last = $this->current;
  4250. $this->current = $item_id;
  4251. // TODO: Update $this->index as well.
  4252. foreach ($this->ordered_items as $index => $item) {
  4253. if ($item == $this->current) {
  4254. $this->index = $index;
  4255. break;
  4256. }
  4257. }
  4258. if ($this->debug > 2) {
  4259. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  4260. }
  4261. } else {
  4262. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  4263. }
  4264. }
  4265. }
  4266. /**
  4267. * Sets the encoding
  4268. * @param string New encoding
  4269. * @return bool
  4270. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4271. */
  4272. public function set_encoding($enc = 'UTF-8')
  4273. {
  4274. if ($this->debug > 0) {
  4275. error_log('New LP - In learnpath::set_encoding()', 0);
  4276. }
  4277. $course_id = api_get_course_int_id();
  4278. $enc = api_refine_encoding_id($enc);
  4279. if (empty($enc)) {
  4280. $enc = api_get_system_encoding();
  4281. }
  4282. if (api_is_encoding_supported($enc)) {
  4283. $lp = $this->get_id();
  4284. if ($lp != 0) {
  4285. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4286. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4287. WHERE c_id = ".$course_id." AND id = ".$lp;
  4288. $res = Database::query($sql);
  4289. return $res;
  4290. }
  4291. }
  4292. return false;
  4293. }
  4294. /**
  4295. * Sets the JS lib setting in the database directly.
  4296. * This is the JavaScript library file this lp needs to load on startup
  4297. * @param string Proximity setting
  4298. * @return boolean True on update success. False otherwise.
  4299. */
  4300. public function set_jslib($lib = '')
  4301. {
  4302. if ($this->debug > 0) {
  4303. error_log('New LP - In learnpath::set_jslib()', 0);
  4304. }
  4305. $lp = $this->get_id();
  4306. $course_id = api_get_course_int_id();
  4307. if ($lp != 0) {
  4308. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4309. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4310. WHERE c_id = ".$course_id." AND id = ".$lp;
  4311. $res = Database::query($sql);
  4312. return $res;
  4313. } else {
  4314. return false;
  4315. }
  4316. }
  4317. /**
  4318. * Sets the name of the LP maker (publisher) (and save)
  4319. * @param string Optional string giving the new content_maker of this learnpath
  4320. * @return boolean True
  4321. */
  4322. public function set_maker($name = '')
  4323. {
  4324. if ($this->debug > 0) {
  4325. error_log('New LP - In learnpath::set_maker()', 0);
  4326. }
  4327. if (empty ($name))
  4328. return false;
  4329. $this->maker = $name;
  4330. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4331. $course_id = api_get_course_int_id();
  4332. $lp_id = $this->get_id();
  4333. $sql = "UPDATE $lp_table SET
  4334. content_maker = '".Database::escape_string($this->maker)."'
  4335. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4336. if ($this->debug > 2) {
  4337. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4338. }
  4339. Database::query($sql);
  4340. return true;
  4341. }
  4342. /**
  4343. * Sets the name of the current learnpath (and save)
  4344. * @param string $name Optional string giving the new name of this learnpath
  4345. * @return boolean True/False
  4346. */
  4347. public function set_name($name = null)
  4348. {
  4349. if ($this->debug > 0) {
  4350. error_log('New LP - In learnpath::set_name()', 0);
  4351. }
  4352. if (empty($name)) {
  4353. return false;
  4354. }
  4355. $this->name = Database::escape_string($name);
  4356. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4357. $lp_id = $this->get_id();
  4358. $course_id = $this->course_info['real_id'];
  4359. $sql = "UPDATE $lp_table SET
  4360. name = '".Database::escape_string($this->name)."'
  4361. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4362. if ($this->debug > 2) {
  4363. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4364. }
  4365. $result = Database::query($sql);
  4366. // If the lp is visible on the homepage, change his name there.
  4367. if (Database::affected_rows($result)) {
  4368. $session_id = api_get_session_id();
  4369. $session_condition = api_get_session_condition($session_id);
  4370. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  4371. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4372. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4373. WHERE
  4374. c_id = $course_id AND
  4375. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4376. Database::query($sql);
  4377. return true;
  4378. } else {
  4379. return false;
  4380. }
  4381. }
  4382. /**
  4383. * Set index specified prefix terms for all items in this path
  4384. * @param string Comma-separated list of terms
  4385. * @param char Xapian term prefix
  4386. * @return boolean False on error, true otherwise
  4387. */
  4388. public function set_terms_by_prefix($terms_string, $prefix)
  4389. {
  4390. $course_id = api_get_course_int_id();
  4391. if (api_get_setting('search_enabled') !== 'true')
  4392. return false;
  4393. if (!extension_loaded('xapian')) {
  4394. return false;
  4395. }
  4396. $terms_string = trim($terms_string);
  4397. $terms = explode(',', $terms_string);
  4398. array_walk($terms, 'trim_value');
  4399. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4400. // Don't do anything if no change, verify only at DB, not the search engine.
  4401. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4402. return false;
  4403. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4404. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4405. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4406. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4407. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  4408. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4409. $lp_id = intval($_POST['lp_id']);
  4410. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4411. $result = Database::query($sql);
  4412. $di = new ChamiloIndexer();
  4413. while ($lp_item = Database::fetch_array($result)) {
  4414. // Get search_did.
  4415. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4416. $sql = 'SELECT * FROM %s
  4417. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4418. LIMIT 1';
  4419. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4420. //echo $sql; echo '<br>';
  4421. $res = Database::query($sql);
  4422. if (Database::num_rows($res) > 0) {
  4423. $se_ref = Database::fetch_array($res);
  4424. // Compare terms.
  4425. $doc = $di->get_document($se_ref['search_did']);
  4426. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4427. $xterms = array();
  4428. foreach ($xapian_terms as $xapian_term) {
  4429. $xterms[] = substr($xapian_term['name'], 1);
  4430. }
  4431. $dterms = $terms;
  4432. $missing_terms = array_diff($dterms, $xterms);
  4433. $deprecated_terms = array_diff($xterms, $dterms);
  4434. // Save it to search engine.
  4435. foreach ($missing_terms as $term) {
  4436. $doc->add_term($prefix.$term, 1);
  4437. }
  4438. foreach ($deprecated_terms as $term) {
  4439. $doc->remove_term($prefix.$term);
  4440. }
  4441. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4442. $di->getDb()->flush();
  4443. }
  4444. }
  4445. return true;
  4446. }
  4447. /**
  4448. * Sets the theme of the LP (local/remote) (and save)
  4449. * @param string Optional string giving the new theme of this learnpath
  4450. * @return bool Returns true if theme name is not empty
  4451. */
  4452. public function set_theme($name = '')
  4453. {
  4454. $course_id = api_get_course_int_id();
  4455. if ($this->debug > 0) {
  4456. error_log('New LP - In learnpath::set_theme()', 0);
  4457. }
  4458. $this->theme = $name;
  4459. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4460. $lp_id = $this->get_id();
  4461. $sql = "UPDATE $lp_table SET theme = '".Database::escape_string($this->theme)."'
  4462. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4463. if ($this->debug > 2) {
  4464. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4465. }
  4466. Database::query($sql);
  4467. return true;
  4468. }
  4469. /**
  4470. * Sets the image of an LP (and save)
  4471. * @param string Optional string giving the new image of this learnpath
  4472. * @return bool Returns true if theme name is not empty
  4473. */
  4474. public function set_preview_image($name = '')
  4475. {
  4476. $course_id = api_get_course_int_id();
  4477. if ($this->debug > 0) {
  4478. error_log('New LP - In learnpath::set_preview_image()', 0);
  4479. }
  4480. $this->preview_image = $name;
  4481. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4482. $lp_id = $this->get_id();
  4483. $sql = "UPDATE $lp_table SET
  4484. preview_image = '".Database::escape_string($this->preview_image)."'
  4485. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4486. if ($this->debug > 2) {
  4487. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4488. }
  4489. Database::query($sql);
  4490. return true;
  4491. }
  4492. /**
  4493. * Sets the author of a LP (and save)
  4494. * @param string Optional string giving the new author of this learnpath
  4495. * @return bool Returns true if author's name is not empty
  4496. */
  4497. public function set_author($name = '')
  4498. {
  4499. $course_id = api_get_course_int_id();
  4500. if ($this->debug > 0) {
  4501. error_log('New LP - In learnpath::set_author()', 0);
  4502. }
  4503. $this->author = $name;
  4504. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4505. $lp_id = $this->get_id();
  4506. $sql = "UPDATE $lp_table SET author = '".Database::escape_string($name)."'
  4507. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4508. if ($this->debug > 2) {
  4509. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4510. }
  4511. Database::query($sql);
  4512. return true;
  4513. }
  4514. /**
  4515. * Sets the hide_toc_frame parameter of a LP (and save)
  4516. * @param int 1 if frame is hidden 0 then else
  4517. * @return bool Returns true if author's name is not empty
  4518. */
  4519. public function set_hide_toc_frame($hide)
  4520. {
  4521. $course_id = api_get_course_int_id();
  4522. if ($this->debug > 0) {
  4523. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4524. }
  4525. if (intval($hide) == $hide) {
  4526. $this->hide_toc_frame = $hide;
  4527. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4528. $lp_id = $this->get_id();
  4529. $sql = "UPDATE $lp_table SET
  4530. hide_toc_frame = '".(int) $this->hide_toc_frame."'
  4531. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4532. if ($this->debug > 2) {
  4533. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4534. }
  4535. Database::query($sql);
  4536. return true;
  4537. } else {
  4538. return false;
  4539. }
  4540. }
  4541. /**
  4542. * Sets the prerequisite of a LP (and save)
  4543. * @param int integer giving the new prerequisite of this learnpath
  4544. * @return bool returns true if prerequisite is not empty
  4545. */
  4546. public function set_prerequisite($prerequisite)
  4547. {
  4548. $course_id = api_get_course_int_id();
  4549. if ($this->debug > 0) {
  4550. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4551. }
  4552. $this->prerequisite = intval($prerequisite);
  4553. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4554. $lp_id = $this->get_id();
  4555. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4556. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4557. if ($this->debug > 2) {
  4558. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4559. }
  4560. Database::query($sql);
  4561. return true;
  4562. }
  4563. /**
  4564. * Sets the location/proximity of the LP (local/remote) (and save)
  4565. * @param string Optional string giving the new location of this learnpath
  4566. * @return boolean True on success / False on error
  4567. */
  4568. public function set_proximity($name = '')
  4569. {
  4570. $course_id = api_get_course_int_id();
  4571. if ($this->debug > 0) {
  4572. error_log('New LP - In learnpath::set_proximity()', 0);
  4573. }
  4574. if (empty ($name))
  4575. return false;
  4576. $this->proximity = $name;
  4577. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4578. $lp_id = $this->get_id();
  4579. $sql = "UPDATE $lp_table SET
  4580. content_local = '".Database::escape_string($name)."'
  4581. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4582. if ($this->debug > 2) {
  4583. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4584. }
  4585. Database::query($sql);
  4586. return true;
  4587. }
  4588. /**
  4589. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4590. * @param integer DB ID of the item
  4591. */
  4592. public function set_previous_item($id)
  4593. {
  4594. if ($this->debug > 0) {
  4595. error_log('New LP - In learnpath::set_previous_item()', 0);
  4596. }
  4597. $this->last = $id;
  4598. }
  4599. /**
  4600. * Sets use_max_score
  4601. * @param string $use_max_score Optional string giving the new location of this learnpath
  4602. * @return boolean True on success / False on error
  4603. */
  4604. public function set_use_max_score($use_max_score = 1)
  4605. {
  4606. $course_id = api_get_course_int_id();
  4607. if ($this->debug > 0) {
  4608. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4609. }
  4610. $use_max_score = intval($use_max_score);
  4611. $this->use_max_score = $use_max_score;
  4612. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4613. $lp_id = $this->get_id();
  4614. $sql = "UPDATE $lp_table SET
  4615. use_max_score = '".$this->use_max_score."'
  4616. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4617. if ($this->debug > 2) {
  4618. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4619. }
  4620. Database::query($sql);
  4621. return true;
  4622. }
  4623. /**
  4624. * Sets and saves the expired_on date
  4625. * @param string $expired_on Optional string giving the new author of this learnpath
  4626. * @return bool Returns true if author's name is not empty
  4627. */
  4628. public function set_expired_on($expired_on)
  4629. {
  4630. if ($this->debug > 0) {
  4631. error_log('New LP - In learnpath::set_expired_on()', 0);
  4632. }
  4633. $em = Database::getManager();
  4634. /** @var CLp $lp */
  4635. $lp = $em
  4636. ->getRepository('ChamiloCourseBundle:CLp')
  4637. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4638. if (!$lp) {
  4639. return false;
  4640. }
  4641. $this->expired_on = !empty($expired_on) ? api_get_utc_datetime($expired_on, false, true) : null;
  4642. $lp->setExpiredOn($this->expired_on);
  4643. $em->persist($lp);
  4644. $em->flush();
  4645. if ($this->debug > 2) {
  4646. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4647. }
  4648. return true;
  4649. }
  4650. /**
  4651. * Sets and saves the publicated_on date
  4652. * @param string $publicated_on Optional string giving the new author of this learnpath
  4653. * @return bool Returns true if author's name is not empty
  4654. */
  4655. public function set_publicated_on($publicated_on)
  4656. {
  4657. if ($this->debug > 0) {
  4658. error_log('New LP - In learnpath::set_expired_on()', 0);
  4659. }
  4660. $em = Database::getManager();
  4661. /** @var CLp $lp */
  4662. $lp = $em
  4663. ->getRepository('ChamiloCourseBundle:CLp')
  4664. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4665. if (!$lp) {
  4666. return false;
  4667. }
  4668. $this->publicated_on = !empty($publicated_on) ? api_get_utc_datetime($publicated_on, false, true) : null;
  4669. $lp->setPublicatedOn($this->publicated_on);
  4670. $em->persist($lp);
  4671. $em->flush();
  4672. if ($this->debug > 2) {
  4673. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4674. }
  4675. return true;
  4676. }
  4677. /**
  4678. * Sets and saves the expired_on date
  4679. * @return bool Returns true if author's name is not empty
  4680. */
  4681. public function set_modified_on()
  4682. {
  4683. $course_id = api_get_course_int_id();
  4684. if ($this->debug > 0) {
  4685. error_log('New LP - In learnpath::set_expired_on()', 0);
  4686. }
  4687. $this->modified_on = api_get_utc_datetime();
  4688. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4689. $lp_id = $this->get_id();
  4690. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."'
  4691. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4692. if ($this->debug > 2) {
  4693. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4694. }
  4695. Database::query($sql);
  4696. return true;
  4697. }
  4698. /**
  4699. * Sets the object's error message
  4700. * @param string Error message. If empty, reinits the error string
  4701. * @return void
  4702. */
  4703. public function set_error_msg($error = '')
  4704. {
  4705. if ($this->debug > 0) {
  4706. error_log('New LP - In learnpath::set_error_msg()', 0);
  4707. }
  4708. if (empty ($error)) {
  4709. $this->error = '';
  4710. } else {
  4711. $this->error .= $error;
  4712. }
  4713. }
  4714. /**
  4715. * Launches the current item if not 'sco'
  4716. * (starts timer and make sure there is a record ready in the DB)
  4717. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4718. * @return boolean
  4719. */
  4720. public function start_current_item($allow_new_attempt = false)
  4721. {
  4722. if ($this->debug > 0) {
  4723. error_log('New LP - In learnpath::start_current_item()', 0);
  4724. }
  4725. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4726. $type = $this->get_type();
  4727. $item_type = $this->items[$this->current]->get_type();
  4728. if (($type == 2 && $item_type != 'sco') ||
  4729. ($type == 3 && $item_type != 'au') ||
  4730. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4731. ) {
  4732. $this->items[$this->current]->open($allow_new_attempt);
  4733. $this->autocomplete_parents($this->current);
  4734. $prereq_check = $this->prerequisites_match($this->current);
  4735. $this->items[$this->current]->save(false, $prereq_check);
  4736. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4737. }
  4738. // If sco, then it is supposed to have been updated by some other call.
  4739. if ($item_type == 'sco') {
  4740. $this->items[$this->current]->restart();
  4741. }
  4742. }
  4743. if ($this->debug > 0) {
  4744. error_log('New LP - End of learnpath::start_current_item()', 0);
  4745. }
  4746. return true;
  4747. }
  4748. /**
  4749. * Stops the processing and counters for the old item (as held in $this->last)
  4750. * @return boolean True/False
  4751. */
  4752. public function stop_previous_item()
  4753. {
  4754. if ($this->debug > 0) {
  4755. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4756. }
  4757. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4758. if ($this->debug > 2) {
  4759. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4760. }
  4761. switch ($this->get_type()) {
  4762. case '3':
  4763. if ($this->items[$this->last]->get_type() != 'au') {
  4764. if ($this->debug > 2) {
  4765. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au', 0);
  4766. }
  4767. $this->items[$this->last]->close();
  4768. //$this->autocomplete_parents($this->last);
  4769. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4770. } else {
  4771. if ($this->debug > 2) {
  4772. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4773. }
  4774. }
  4775. break;
  4776. case '2':
  4777. if ($this->items[$this->last]->get_type() != 'sco') {
  4778. if ($this->debug > 2) {
  4779. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco', 0);
  4780. }
  4781. $this->items[$this->last]->close();
  4782. //$this->autocomplete_parents($this->last);
  4783. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4784. } else {
  4785. if ($this->debug > 2) {
  4786. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4787. }
  4788. }
  4789. break;
  4790. case '1':
  4791. default:
  4792. if ($this->debug > 2) {
  4793. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset', 0);
  4794. }
  4795. $this->items[$this->last]->close();
  4796. break;
  4797. }
  4798. } else {
  4799. if ($this->debug > 2) {
  4800. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4801. }
  4802. return false;
  4803. }
  4804. return true;
  4805. }
  4806. /**
  4807. * Updates the default view mode from fullscreen to embedded and inversely
  4808. * @return string The current default view mode ('fullscreen' or 'embedded')
  4809. */
  4810. public function update_default_view_mode()
  4811. {
  4812. $course_id = api_get_course_int_id();
  4813. if ($this->debug > 0) {
  4814. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4815. }
  4816. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4817. $sql = "SELECT * FROM $lp_table
  4818. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4819. $res = Database::query($sql);
  4820. if (Database::num_rows($res) > 0) {
  4821. $row = Database::fetch_array($res);
  4822. $default_view_mode = $row['default_view_mod'];
  4823. $view_mode = $default_view_mode;
  4824. switch ($default_view_mode) {
  4825. case 'fullscreen': // default with popup
  4826. $view_mode = 'embedded';
  4827. break;
  4828. case 'embedded': // default view with left menu
  4829. $view_mode = 'embedframe';
  4830. break;
  4831. case 'embedframe': //folded menu
  4832. $view_mode = 'impress';
  4833. break;
  4834. case 'impress':
  4835. $view_mode = 'fullscreen';
  4836. break;
  4837. }
  4838. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4839. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4840. Database::query($sql);
  4841. $this->mode = $view_mode;
  4842. return $view_mode;
  4843. } else {
  4844. if ($this->debug > 2) {
  4845. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4846. }
  4847. }
  4848. return -1;
  4849. }
  4850. /**
  4851. * Updates the default behaviour about auto-commiting SCORM updates
  4852. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4853. */
  4854. public function update_default_scorm_commit()
  4855. {
  4856. $course_id = api_get_course_int_id();
  4857. if ($this->debug > 0) {
  4858. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4859. }
  4860. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4861. $sql = "SELECT * FROM $lp_table
  4862. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4863. $res = Database::query($sql);
  4864. if (Database::num_rows($res) > 0) {
  4865. $row = Database::fetch_array($res);
  4866. $force = $row['force_commit'];
  4867. if ($force == 1) {
  4868. $force = 0;
  4869. $force_return = false;
  4870. } elseif ($force == 0) {
  4871. $force = 1;
  4872. $force_return = true;
  4873. }
  4874. $sql = "UPDATE $lp_table SET force_commit = $force
  4875. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4876. Database::query($sql);
  4877. $this->force_commit = $force_return;
  4878. return $force_return;
  4879. } else {
  4880. if ($this->debug > 2) {
  4881. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB', 0);
  4882. }
  4883. }
  4884. return -1;
  4885. }
  4886. /**
  4887. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4888. * @return bool True on success, false on failure
  4889. */
  4890. public function update_display_order()
  4891. {
  4892. $course_id = api_get_course_int_id();
  4893. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4894. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4895. $res = Database::query($sql);
  4896. if ($res === false) {
  4897. return false;
  4898. }
  4899. $num = Database::num_rows($res);
  4900. // First check the order is correct, globally (might be wrong because
  4901. // of versions < 1.8.4).
  4902. if ($num > 0) {
  4903. $i = 1;
  4904. while ($row = Database::fetch_array($res)) {
  4905. if ($row['display_order'] != $i) {
  4906. // If we find a gap in the order, we need to fix it.
  4907. $sql = "UPDATE $lp_table SET display_order = $i
  4908. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4909. Database::query($sql);
  4910. }
  4911. $i++;
  4912. }
  4913. }
  4914. return true;
  4915. }
  4916. /**
  4917. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4918. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4919. */
  4920. public function update_reinit()
  4921. {
  4922. $course_id = api_get_course_int_id();
  4923. if ($this->debug > 0) {
  4924. error_log('New LP - In learnpath::update_reinit()', 0);
  4925. }
  4926. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4927. $sql = "SELECT * FROM $lp_table
  4928. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4929. $res = Database::query($sql);
  4930. if (Database::num_rows($res) > 0) {
  4931. $row = Database::fetch_array($res);
  4932. $force = $row['prevent_reinit'];
  4933. if ($force == 1) {
  4934. $force = 0;
  4935. } elseif ($force == 0) {
  4936. $force = 1;
  4937. }
  4938. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4939. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4940. Database::query($sql);
  4941. $this->prevent_reinit = $force;
  4942. return $force;
  4943. } else {
  4944. if ($this->debug > 2) {
  4945. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4946. }
  4947. }
  4948. return -1;
  4949. }
  4950. /**
  4951. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4952. *
  4953. * @return string 'single', 'multi' or 'seriousgame'
  4954. * @author ndiechburg <noel@cblue.be>
  4955. **/
  4956. public function get_attempt_mode()
  4957. {
  4958. //Set default value for seriousgame_mode
  4959. if (!isset($this->seriousgame_mode)) {
  4960. $this->seriousgame_mode = 0;
  4961. }
  4962. // Set default value for prevent_reinit
  4963. if (!isset($this->prevent_reinit)) {
  4964. $this->prevent_reinit = 1;
  4965. }
  4966. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4967. return 'seriousgame';
  4968. }
  4969. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4970. return 'single';
  4971. }
  4972. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4973. return 'multiple';
  4974. }
  4975. return 'single';
  4976. }
  4977. /**
  4978. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4979. *
  4980. * @param string 'seriousgame', 'single' or 'multiple'
  4981. * @return boolean
  4982. * @author ndiechburg <noel@cblue.be>
  4983. **/
  4984. public function set_attempt_mode($mode)
  4985. {
  4986. $course_id = api_get_course_int_id();
  4987. switch ($mode) {
  4988. case 'seriousgame':
  4989. $sg_mode = 1;
  4990. $prevent_reinit = 1;
  4991. break;
  4992. case 'single':
  4993. $sg_mode = 0;
  4994. $prevent_reinit = 1;
  4995. break;
  4996. case 'multiple':
  4997. $sg_mode = 0;
  4998. $prevent_reinit = 0;
  4999. break;
  5000. default:
  5001. $sg_mode = 0;
  5002. $prevent_reinit = 0;
  5003. break;
  5004. }
  5005. $this->prevent_reinit = $prevent_reinit;
  5006. $this->seriousgame_mode = $sg_mode;
  5007. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5008. $sql = "UPDATE $lp_table SET
  5009. prevent_reinit = $prevent_reinit ,
  5010. seriousgame_mode = $sg_mode
  5011. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  5012. $res = Database::query($sql);
  5013. if ($res) {
  5014. return true;
  5015. } else {
  5016. return false;
  5017. }
  5018. }
  5019. /**
  5020. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  5021. *
  5022. * @return boolean
  5023. * @author ndiechburg <noel@cblue.be>
  5024. **/
  5025. public function switch_attempt_mode()
  5026. {
  5027. if ($this->debug > 0) {
  5028. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  5029. }
  5030. $mode = $this->get_attempt_mode();
  5031. switch ($mode) {
  5032. case 'single':
  5033. $next_mode = 'multiple';
  5034. break;
  5035. case 'multiple':
  5036. $next_mode = 'seriousgame';
  5037. break;
  5038. case 'seriousgame':
  5039. $next_mode = 'single';
  5040. break;
  5041. default:
  5042. $next_mode = 'single';
  5043. break;
  5044. }
  5045. $this->set_attempt_mode($next_mode);
  5046. }
  5047. /**
  5048. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  5049. * but possibility to do again a completed item.
  5050. *
  5051. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  5052. * @author ndiechburg <noel@cblue.be>
  5053. **/
  5054. public function set_seriousgame_mode()
  5055. {
  5056. $course_id = api_get_course_int_id();
  5057. if ($this->debug > 0) {
  5058. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  5059. }
  5060. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5061. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  5062. $res = Database::query($sql);
  5063. if (Database::num_rows($res) > 0) {
  5064. $row = Database::fetch_array($res);
  5065. $force = $row['seriousgame_mode'];
  5066. if ($force == 1) {
  5067. $force = 0;
  5068. } elseif ($force == 0) {
  5069. $force = 1;
  5070. }
  5071. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  5072. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  5073. Database::query($sql);
  5074. $this->seriousgame_mode = $force;
  5075. return $force;
  5076. } else {
  5077. if ($this->debug > 2) {
  5078. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB', 0);
  5079. }
  5080. }
  5081. return -1;
  5082. }
  5083. /**
  5084. * Updates the "scorm_debug" value that shows or hide the debug window
  5085. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  5086. */
  5087. public function update_scorm_debug()
  5088. {
  5089. $course_id = api_get_course_int_id();
  5090. if ($this->debug > 0) {
  5091. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  5092. }
  5093. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5094. $sql = "SELECT * FROM $lp_table
  5095. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  5096. $res = Database::query($sql);
  5097. if (Database::num_rows($res) > 0) {
  5098. $row = Database::fetch_array($res);
  5099. $force = $row['debug'];
  5100. if ($force == 1) {
  5101. $force = 0;
  5102. } elseif ($force == 0) {
  5103. $force = 1;
  5104. }
  5105. $sql = "UPDATE $lp_table SET debug = $force
  5106. WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  5107. Database::query($sql);
  5108. $this->scorm_debug = $force;
  5109. return $force;
  5110. } else {
  5111. if ($this->debug > 2) {
  5112. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  5113. }
  5114. }
  5115. return -1;
  5116. }
  5117. /**
  5118. * Function that makes a call to the function sort_tree_array and create_tree_array
  5119. * @author Kevin Van Den Haute
  5120. * @param array
  5121. */
  5122. public function tree_array($array)
  5123. {
  5124. if ($this->debug > 1) {
  5125. error_log('New LP - In learnpath::tree_array()', 0);
  5126. }
  5127. $array = $this->sort_tree_array($array);
  5128. $this->create_tree_array($array);
  5129. }
  5130. /**
  5131. * Creates an array with the elements of the learning path tree in it
  5132. *
  5133. * @author Kevin Van Den Haute
  5134. * @param array $array
  5135. * @param int $parent
  5136. * @param int $depth
  5137. * @param array $tmp
  5138. */
  5139. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  5140. {
  5141. if ($this->debug > 1) {
  5142. error_log('New LP - In learnpath::create_tree_array())', 0);
  5143. }
  5144. if (is_array($array)) {
  5145. for ($i = 0; $i < count($array); $i++) {
  5146. if ($array[$i]['parent_item_id'] == $parent) {
  5147. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5148. $tmp[] = $array[$i]['parent_item_id'];
  5149. $depth++;
  5150. }
  5151. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5152. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5153. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5154. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5155. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5156. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5157. $this->arrMenu[] = array(
  5158. 'id' => $array[$i]['id'],
  5159. 'ref' => $ref,
  5160. 'item_type' => $array[$i]['item_type'],
  5161. 'title' => $array[$i]['title'],
  5162. 'path' => $path,
  5163. 'description' => $array[$i]['description'],
  5164. 'parent_item_id' => $array[$i]['parent_item_id'],
  5165. 'previous_item_id' => $array[$i]['previous_item_id'],
  5166. 'next_item_id' => $array[$i]['next_item_id'],
  5167. 'min_score' => $array[$i]['min_score'],
  5168. 'max_score' => $array[$i]['max_score'],
  5169. 'mastery_score' => $array[$i]['mastery_score'],
  5170. 'display_order' => $array[$i]['display_order'],
  5171. 'prerequisite' => $preq,
  5172. 'depth' => $depth,
  5173. 'audio' => $audio,
  5174. 'prerequisite_min_score' => $prerequisiteMinScore,
  5175. 'prerequisite_max_score' => $prerequisiteMaxScore
  5176. );
  5177. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5178. }
  5179. }
  5180. }
  5181. }
  5182. /**
  5183. * Sorts a multi dimensional array by parent id and display order
  5184. * @author Kevin Van Den Haute
  5185. *
  5186. * @param array $array (array with al the learning path items in it)
  5187. *
  5188. * @return array
  5189. */
  5190. public function sort_tree_array($array)
  5191. {
  5192. foreach ($array as $key => $row) {
  5193. $parent[$key] = $row['parent_item_id'];
  5194. $position[$key] = $row['display_order'];
  5195. }
  5196. if (count($array) > 0) {
  5197. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5198. }
  5199. return $array;
  5200. }
  5201. /**
  5202. * Function that creates a html list of learning path items so that we can add audio files to them
  5203. * @author Kevin Van Den Haute
  5204. * @return string
  5205. */
  5206. public function overview()
  5207. {
  5208. if ($this->debug > 0) {
  5209. error_log('New LP - In learnpath::overview()', 0);
  5210. }
  5211. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5212. $return = '';
  5213. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5214. // we need to start a form when we want to update all the mp3 files
  5215. if ($update_audio == 'true') {
  5216. $return .= '<form action="'.api_get_self().'?'.api_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">';
  5217. }
  5218. $return .= '<div id="message"></div>';
  5219. if (count($this->items) == 0) {
  5220. $return .= Display::return_message(get_lang('YouShouldAddItemsBeforeAttachAudio'), 'normal');
  5221. } else {
  5222. $return_audio = '<table class="data_table">';
  5223. $return_audio .= '<tr>';
  5224. $return_audio .= '<th width="40%">'.get_lang('Title').'</th>';
  5225. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5226. $return_audio .= '</tr>';
  5227. if ($update_audio != 'true') {
  5228. $return .= '<div class="col-md-12">';
  5229. $return .= self::return_new_tree($update_audio);
  5230. $return .= '</div>';
  5231. $return .= Display::div(
  5232. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5233. array('style' => 'float:left; margin-top:15px;width:100%')
  5234. );
  5235. } else {
  5236. $return_audio .= self::return_new_tree($update_audio);
  5237. $return .= $return_audio.'</table>';
  5238. }
  5239. // We need to close the form when we are updating the mp3 files.
  5240. if ($update_audio == 'true') {
  5241. $return .= '<div class="footer-audio">';
  5242. $return .= Display::button(
  5243. 'save_audio',
  5244. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5245. array('class' => 'btn btn-primary', 'type' => 'submit')
  5246. );
  5247. $return .= '</div>';
  5248. }
  5249. }
  5250. // We need to close the form when we are updating the mp3 files.
  5251. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5252. $return .= '</form>';
  5253. }
  5254. return $return;
  5255. }
  5256. /**
  5257. * @param string string $update_audio
  5258. * @param bool $drop_element_here
  5259. * @return string
  5260. */
  5261. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5262. {
  5263. $return = '';
  5264. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5265. $course_id = api_get_course_int_id();
  5266. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5267. $sql = "SELECT * FROM $tbl_lp_item
  5268. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5269. $result = Database::query($sql);
  5270. $arrLP = array();
  5271. while ($row = Database::fetch_array($result)) {
  5272. $arrLP[] = array(
  5273. 'id' => $row['id'],
  5274. 'item_type' => $row['item_type'],
  5275. 'title' => Security::remove_XSS($row['title']),
  5276. 'path' => $row['path'],
  5277. 'description' => Security::remove_XSS($row['description']),
  5278. 'parent_item_id' => $row['parent_item_id'],
  5279. 'previous_item_id' => $row['previous_item_id'],
  5280. 'next_item_id' => $row['next_item_id'],
  5281. 'max_score' => $row['max_score'],
  5282. 'min_score' => $row['min_score'],
  5283. 'mastery_score' => $row['mastery_score'],
  5284. 'prerequisite' => $row['prerequisite'],
  5285. 'display_order' => $row['display_order'],
  5286. 'audio' => $row['audio'],
  5287. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5288. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5289. );
  5290. }
  5291. $this->tree_array($arrLP);
  5292. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5293. unset($this->arrMenu);
  5294. $default_data = null;
  5295. $default_content = null;
  5296. $elements = array();
  5297. $return_audio = null;
  5298. for ($i = 0; $i < count($arrLP); $i++) {
  5299. $title = $arrLP[$i]['title'];
  5300. $title_cut = cut($arrLP[$i]['title'], 25);
  5301. // Link for the documents
  5302. if ($arrLP[$i]['item_type'] == 'document') {
  5303. $url = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5304. $title_cut = Display::url(
  5305. $title_cut,
  5306. $url,
  5307. array(
  5308. 'class' => 'ajax moved',
  5309. 'data-title' => $title_cut
  5310. )
  5311. );
  5312. }
  5313. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5314. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5315. Session::write('pathItem', $arrLP[$i]['path']);
  5316. }
  5317. if (($i % 2) == 0) {
  5318. $oddClass = 'row_odd';
  5319. } else {
  5320. $oddClass = 'row_even';
  5321. }
  5322. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddClass.'">';
  5323. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5324. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5325. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5326. } else {
  5327. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5328. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5329. } else {
  5330. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5331. $icon = Display::return_icon('certificate.png');
  5332. } else {
  5333. $icon = Display::return_icon('folder_document.gif');
  5334. }
  5335. }
  5336. }
  5337. // The audio column.
  5338. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5339. $audio = '';
  5340. if (!$update_audio || $update_audio <> 'true') {
  5341. if (empty($arrLP[$i]['audio'])) {
  5342. $audio .= '';
  5343. }
  5344. } else {
  5345. $types = self::getChapterTypes();
  5346. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5347. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5348. if (!empty ($arrLP[$i]['audio'])) {
  5349. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5350. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang('RemoveAudio');
  5351. }
  5352. }
  5353. }
  5354. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5355. $return_audio .= '</td>';
  5356. $move_icon = '';
  5357. $move_item_icon = '';
  5358. $edit_icon = '';
  5359. $delete_icon = '';
  5360. $audio_icon = '';
  5361. $prerequisities_icon = '';
  5362. $forumIcon = '';
  5363. $previewIcon = '';
  5364. if ($is_allowed_to_edit) {
  5365. if (!$update_audio || $update_audio <> 'true') {
  5366. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5367. $move_icon .= '<a class="moved" href="#">';
  5368. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5369. $move_icon .= '</a>';
  5370. }
  5371. }
  5372. // No edit for this item types
  5373. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5374. if ($arrLP[$i]['item_type'] != 'dir') {
  5375. $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">';
  5376. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5377. $edit_icon .= '</a>';
  5378. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5379. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5380. $this->course_int_id,
  5381. $this->lp_session_id
  5382. );
  5383. if ($forumThread) {
  5384. $forumIconUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  5385. 'action' => 'dissociate_forum',
  5386. 'id' => $arrLP[$i]['id'],
  5387. 'lp_id' => $this->lp_id
  5388. ]);
  5389. $forumIcon = Display::url(
  5390. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5391. $forumIconUrl,
  5392. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5393. );
  5394. } else {
  5395. $forumIconUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  5396. 'action' => 'create_forum',
  5397. 'id' => $arrLP[$i]['id'],
  5398. 'lp_id' => $this->lp_id
  5399. ]);
  5400. $forumIcon = Display::url(
  5401. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5402. $forumIconUrl,
  5403. ['class' => "btn btn-default lp-btn-associate-forum"]
  5404. );
  5405. }
  5406. }
  5407. } else {
  5408. $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">';
  5409. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5410. $edit_icon .= '</a>';
  5411. }
  5412. } else {
  5413. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5414. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id.'" class="btn btn-default">';
  5415. $edit_icon .= Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_TINY);
  5416. $edit_icon .= '</a>';
  5417. }
  5418. }
  5419. $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">';
  5420. $delete_icon .= Display::return_icon(
  5421. 'delete.png',
  5422. get_lang('LearnpathDeleteModule'),
  5423. [],
  5424. ICON_SIZE_TINY
  5425. );
  5426. $delete_icon .= '</a>';
  5427. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5428. $previewImage = Display::return_icon(
  5429. 'preview_view.png',
  5430. get_lang('Preview'),
  5431. [],
  5432. ICON_SIZE_TINY
  5433. );
  5434. switch ($arrLP[$i]['item_type']) {
  5435. case TOOL_DOCUMENT:
  5436. case TOOL_LP_FINAL_ITEM:
  5437. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5438. $previewIcon = Display::url(
  5439. $previewImage,
  5440. $urlPreviewLink,
  5441. array(
  5442. 'target' => '_blank',
  5443. 'class' => 'btn btn-default',
  5444. 'data-title' => $arrLP[$i]['title']
  5445. )
  5446. );
  5447. break;
  5448. case TOOL_THREAD:
  5449. case TOOL_FORUM:
  5450. case TOOL_QUIZ:
  5451. case TOOL_STUDENTPUBLICATION:
  5452. case TOOL_LP_FINAL_ITEM:
  5453. case TOOL_LINK:
  5454. //$target = '';
  5455. //$class = 'btn btn-default ajax';
  5456. //if ($arrLP[$i]['item_type'] == TOOL_LINK) {
  5457. $class = 'btn btn-default';
  5458. $target = '_blank';
  5459. //}
  5460. $link = self::rl_get_resource_link_for_learnpath(
  5461. $this->course_int_id,
  5462. $this->lp_id,
  5463. $arrLP[$i]['id'],
  5464. 0,
  5465. ''
  5466. );
  5467. $previewIcon = Display::url(
  5468. $previewImage,
  5469. $link,
  5470. [
  5471. 'class' => $class,
  5472. 'data-title' => $arrLP[$i]['title'],
  5473. 'target' => $target
  5474. ]
  5475. );
  5476. break;
  5477. default:
  5478. $previewIcon = Display::url(
  5479. $previewImage,
  5480. $url.'&action=view_item',
  5481. ['class' => 'btn btn-default', 'target' => '_blank']
  5482. );
  5483. break;
  5484. }
  5485. if ($arrLP[$i]['item_type'] != 'dir') {
  5486. $prerequisities_icon = Display::url(
  5487. Display::return_icon(
  5488. 'accept.png',
  5489. get_lang('LearnpathPrerequisites'),
  5490. array(),
  5491. ICON_SIZE_TINY
  5492. ),
  5493. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5494. );
  5495. if ($arrLP[$i]['item_type'] != 'final_item') {
  5496. $move_item_icon = Display::url(
  5497. Display::return_icon(
  5498. 'move.png',
  5499. get_lang('Move'),
  5500. array(),
  5501. ICON_SIZE_TINY
  5502. ),
  5503. $url.'&action=move_item',
  5504. ['class' => 'btn btn-default']
  5505. );
  5506. }
  5507. $audio_icon = Display::url(
  5508. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5509. $url.'&action=add_audio',
  5510. ['class' => 'btn btn-default']
  5511. );
  5512. }
  5513. }
  5514. if ($update_audio != 'true') {
  5515. $row = $move_icon.' '.$icon.
  5516. Display::span($title_cut).
  5517. Display::tag(
  5518. 'div',
  5519. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5520. array('class'=>'btn-toolbar button_actions')
  5521. );
  5522. } else {
  5523. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5524. }
  5525. $parent_id = $arrLP[$i]['parent_item_id'];
  5526. $default_data[$arrLP[$i]['id']] = $row;
  5527. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5528. if (empty($parent_id)) {
  5529. $elements[$arrLP[$i]['id']]['data'] = $row;
  5530. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5531. } else {
  5532. $parent_arrays = array();
  5533. if ($arrLP[$i]['depth'] > 1) {
  5534. //Getting list of parents
  5535. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5536. foreach ($arrLP as $item) {
  5537. if ($item['id'] == $parent_id) {
  5538. if ($item['parent_item_id'] == 0) {
  5539. $parent_id = $item['id'];
  5540. break;
  5541. } else {
  5542. $parent_id = $item['parent_item_id'];
  5543. if (empty($parent_arrays)) {
  5544. $parent_arrays[] = intval($item['id']);
  5545. }
  5546. $parent_arrays[] = $parent_id;
  5547. break;
  5548. }
  5549. }
  5550. }
  5551. }
  5552. }
  5553. if (!empty($parent_arrays)) {
  5554. $parent_arrays = array_reverse($parent_arrays);
  5555. $val = '$elements';
  5556. $x = 0;
  5557. foreach ($parent_arrays as $item) {
  5558. if ($x != count($parent_arrays) - 1) {
  5559. $val .= '["'.$item.'"]["children"]';
  5560. } else {
  5561. $val .= '["'.$item.'"]["children"]';
  5562. }
  5563. $x++;
  5564. }
  5565. $val .= "";
  5566. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5567. eval($code_str);
  5568. } else {
  5569. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5570. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5571. }
  5572. }
  5573. }
  5574. $list = '<ul id="lp_item_list">';
  5575. $tree = self::print_recursive($elements, $default_data, $default_content);
  5576. if (!empty($tree)) {
  5577. $list .= $tree;
  5578. } else {
  5579. if ($drop_element_here) {
  5580. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5581. }
  5582. }
  5583. $list .= '</ul>';
  5584. $return .= Display::panelCollapse(
  5585. $this->name,
  5586. $list,
  5587. 'scorm-list',
  5588. null,
  5589. 'scorm-list-accordion',
  5590. 'scorm-list-collapse'
  5591. );
  5592. if ($update_audio == 'true') {
  5593. $return = $return_audio;
  5594. }
  5595. return $return;
  5596. }
  5597. /**
  5598. * @param array $elements
  5599. * @param array $default_data
  5600. * @param array $default_content
  5601. * @return string
  5602. */
  5603. public function print_recursive($elements, $default_data, $default_content)
  5604. {
  5605. $return = '';
  5606. foreach ($elements as $key => $item) {
  5607. if (isset($item['load_data']) || empty($item['data'])) {
  5608. $item['data'] = $default_data[$item['load_data']];
  5609. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5610. }
  5611. $sub_list = '';
  5612. if (isset($item['type']) && $item['type'] == 'dir') {
  5613. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5614. }
  5615. if (empty($item['children'])) {
  5616. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5617. $active = null;
  5618. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5619. $active = 'active';
  5620. }
  5621. $return .= Display::tag(
  5622. 'li',
  5623. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5624. array('id'=>$key, 'class'=>'record li_container')
  5625. );
  5626. } else {
  5627. // Sections
  5628. if (isset($item['children'])) {
  5629. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5630. }
  5631. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5632. $return .= Display::tag(
  5633. 'li',
  5634. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5635. array('id'=>$key, 'class'=>'record li_container')
  5636. );
  5637. }
  5638. }
  5639. return $return;
  5640. }
  5641. /**
  5642. * This function builds the action menu
  5643. * @param bool $returnContent Optional
  5644. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5645. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5646. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5647. * @return string
  5648. */
  5649. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5650. {
  5651. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5652. $actionsLeft = '';
  5653. $actionsRight = '';
  5654. $actionsLeft .= Display::url(
  5655. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5656. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5657. 'gradebook' => $gradebook,
  5658. 'action' => 'view',
  5659. 'lp_id' => $_SESSION['oLP']->lp_id,
  5660. 'isStudentView' => 'true'
  5661. ])
  5662. );
  5663. $actionsLeft .= Display::url(
  5664. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5665. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5666. 'action' => 'admin_view',
  5667. 'lp_id' => $_SESSION['oLP']->lp_id,
  5668. 'updateaudio' => 'true'
  5669. ])
  5670. );
  5671. if (!$isConfigPage) {
  5672. $actionsLeft .= Display::url(
  5673. Display::return_icon('settings.png', get_lang('CourseSettings'), '', ICON_SIZE_MEDIUM),
  5674. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5675. 'action' => 'edit',
  5676. 'lp_id' => $_SESSION['oLP']->lp_id
  5677. ])
  5678. );
  5679. } else {
  5680. $actionsLeft .= Display::url(
  5681. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5682. 'lp_controller.php?'.http_build_query([
  5683. 'action' => 'build',
  5684. 'lp_id' => $_SESSION['oLP']->lp_id
  5685. ]).'&'.api_get_cidreq()
  5686. );
  5687. }
  5688. if ($allowExpand) {
  5689. $actionsLeft .= Display::url(
  5690. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM).
  5691. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5692. '#',
  5693. ['role' => 'button', 'id' => 'hide_bar_template']
  5694. );
  5695. }
  5696. if ($showRequirementButtons) {
  5697. $buttons = array(
  5698. array(
  5699. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5700. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5701. 'action' => 'set_previous_step_as_prerequisite',
  5702. 'lp_id' => $_SESSION['oLP']->lp_id
  5703. ])
  5704. ),
  5705. array(
  5706. 'title' => get_lang('ClearAllPrerequisites'),
  5707. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5708. 'action' => 'clear_prerequisites',
  5709. 'lp_id' => $_SESSION['oLP']->lp_id
  5710. ])
  5711. ),
  5712. );
  5713. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons, true);
  5714. }
  5715. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5716. if ($returnContent) {
  5717. return $toolbar;
  5718. }
  5719. echo $toolbar;
  5720. }
  5721. /**
  5722. * Creates the default learning path folder
  5723. * @param array $course
  5724. * @param int $creatorId
  5725. *
  5726. * @return bool
  5727. */
  5728. public static function generate_learning_path_folder($course, $creatorId = 0)
  5729. {
  5730. // Creating learning_path folder
  5731. $dir = '/learning_path';
  5732. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5733. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5734. $folder = false;
  5735. if (!is_dir($filepath.'/'.$dir)) {
  5736. $folderData = create_unexisting_directory(
  5737. $course,
  5738. $creatorId,
  5739. 0,
  5740. 0,
  5741. 0,
  5742. $filepath,
  5743. $dir,
  5744. get_lang('LearningPaths'),
  5745. 0
  5746. );
  5747. if (!empty($folderData)) {
  5748. $folder = true;
  5749. }
  5750. } else {
  5751. $folder = true;
  5752. }
  5753. return $folder;
  5754. }
  5755. /**
  5756. * @param array $course
  5757. * @param string $lp_name
  5758. * @param int $creatorId
  5759. *
  5760. * @return array
  5761. */
  5762. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5763. {
  5764. $filepath = '';
  5765. $dir = '/learning_path/';
  5766. if (empty($lp_name)) {
  5767. $lp_name = $this->name;
  5768. }
  5769. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5770. $folder = self::generate_learning_path_folder($course, $creatorId);
  5771. // Limits title size
  5772. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5773. $dir = $dir.$title;
  5774. // Creating LP folder
  5775. $documentId = null;
  5776. if ($folder) {
  5777. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5778. if (!is_dir($filepath.'/'.$dir)) {
  5779. $folderData = create_unexisting_directory(
  5780. $course,
  5781. $creatorId,
  5782. 0,
  5783. 0,
  5784. 0,
  5785. $filepath,
  5786. $dir,
  5787. $lp_name
  5788. );
  5789. if (!empty($folderData)) {
  5790. $folder = true;
  5791. }
  5792. $documentId = $folderData['id'];
  5793. } else {
  5794. $folder = true;
  5795. }
  5796. $dir = $dir.'/';
  5797. if ($folder) {
  5798. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5799. }
  5800. }
  5801. if (empty($documentId)) {
  5802. $dir = api_remove_trailing_slash($dir);
  5803. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5804. }
  5805. $array = array(
  5806. 'dir' => $dir,
  5807. 'filepath' => $filepath,
  5808. 'folder' => $folder,
  5809. 'id' => $documentId
  5810. );
  5811. return $array;
  5812. }
  5813. /**
  5814. * Create a new document //still needs some finetuning
  5815. * @param array $courseInfo
  5816. * @param string $content
  5817. * @param string $title
  5818. * @param string $extension
  5819. * @param int $parentId
  5820. * @param int $creatorId creator id
  5821. *
  5822. * @return string
  5823. */
  5824. public function create_document(
  5825. $courseInfo,
  5826. $content = '',
  5827. $title = '',
  5828. $extension = 'html',
  5829. $parentId = 0,
  5830. $creatorId = 0
  5831. ) {
  5832. if (!empty($courseInfo)) {
  5833. $course_id = $courseInfo['real_id'];
  5834. } else {
  5835. $course_id = api_get_course_int_id();
  5836. }
  5837. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5838. $sessionId = api_get_session_id();
  5839. // Generates folder
  5840. $result = $this->generate_lp_folder($courseInfo);
  5841. $dir = $result['dir'];
  5842. if (empty($parentId) || $parentId == '/') {
  5843. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  5844. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5845. if ($parentId === '/') {
  5846. $dir = '/';
  5847. }
  5848. // Please, do not modify this dirname formatting.
  5849. if (strstr($dir, '..')) {
  5850. $dir = '/';
  5851. }
  5852. if (!empty($dir[0]) && $dir[0] == '.') {
  5853. $dir = substr($dir, 1);
  5854. }
  5855. if (!empty($dir[0]) && $dir[0] != '/') {
  5856. $dir = '/'.$dir;
  5857. }
  5858. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5859. $dir .= '/';
  5860. }
  5861. } else {
  5862. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5863. if (!empty($parentInfo)) {
  5864. $dir = $parentInfo['path'].'/';
  5865. }
  5866. }
  5867. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  5868. if (!is_dir($filepath)) {
  5869. $dir = '/';
  5870. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  5871. }
  5872. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5873. // is already escaped twice when it gets here.
  5874. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5875. if (!empty($title)) {
  5876. $title = api_replace_dangerous_char(stripslashes($title));
  5877. } else {
  5878. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5879. }
  5880. $title = disable_dangerous_file($title);
  5881. $filename = $title;
  5882. $content = !empty($content) ? $content : $_POST['content_lp'];
  5883. $tmp_filename = $filename;
  5884. $i = 0;
  5885. while (file_exists($filepath.$tmp_filename.'.'.$extension)) {
  5886. $tmp_filename = $filename.'_'.++ $i;
  5887. }
  5888. $filename = $tmp_filename.'.'.$extension;
  5889. if ($extension == 'html') {
  5890. $content = stripslashes($content);
  5891. $content = str_replace(
  5892. api_get_path(WEB_COURSE_PATH),
  5893. api_get_path(REL_PATH).'courses/',
  5894. $content
  5895. );
  5896. // Change the path of mp3 to absolute.
  5897. // The first regexp deals with :// urls.
  5898. $content = preg_replace(
  5899. "|(flashvars=\"file=)([^:/]+)/|",
  5900. "$1".api_get_path(
  5901. REL_COURSE_PATH
  5902. ).$courseInfo['path'].'/document/',
  5903. $content
  5904. );
  5905. // The second regexp deals with audio/ urls.
  5906. $content = preg_replace(
  5907. "|(flashvars=\"file=)([^/]+)/|",
  5908. "$1".api_get_path(
  5909. REL_COURSE_PATH
  5910. ).$courseInfo['path'].'/document/$2/',
  5911. $content
  5912. );
  5913. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5914. $content = str_replace(
  5915. '</body>',
  5916. '<style type="text/css">body{}</style></body>',
  5917. $content
  5918. );
  5919. }
  5920. if (!file_exists($filepath.$filename)) {
  5921. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5922. fputs($fp, $content);
  5923. fclose($fp);
  5924. $file_size = filesize($filepath.$filename);
  5925. $save_file_path = $dir.$filename;
  5926. $document_id = add_document(
  5927. $courseInfo,
  5928. $save_file_path,
  5929. 'file',
  5930. $file_size,
  5931. $tmp_filename,
  5932. '',
  5933. 0, //readonly
  5934. true,
  5935. null,
  5936. $sessionId,
  5937. $creatorId
  5938. );
  5939. if ($document_id) {
  5940. api_item_property_update(
  5941. $courseInfo,
  5942. TOOL_DOCUMENT,
  5943. $document_id,
  5944. 'DocumentAdded',
  5945. $creatorId,
  5946. null,
  5947. null,
  5948. null,
  5949. null,
  5950. $sessionId
  5951. );
  5952. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5953. $new_title = $originalTitle;
  5954. if ($new_comment || $new_title) {
  5955. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5956. $ct = '';
  5957. if ($new_comment)
  5958. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5959. if ($new_title)
  5960. $ct .= ", title='".Database::escape_string($new_title)."' ";
  5961. $sql = "UPDATE ".$tbl_doc." SET ".substr($ct, 1)."
  5962. WHERE c_id = ".$course_id." AND id = ".$document_id;
  5963. Database::query($sql);
  5964. }
  5965. }
  5966. return $document_id;
  5967. }
  5968. }
  5969. }
  5970. /**
  5971. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5972. * @param array $_course array
  5973. * @return void
  5974. */
  5975. public function edit_document($_course)
  5976. {
  5977. $course_id = api_get_course_int_id();
  5978. $urlAppend = api_get_configuration_value('url_append');
  5979. // Please, do not modify this dirname formatting.
  5980. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5981. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  5982. if (strstr($dir, '..')) {
  5983. $dir = '/';
  5984. }
  5985. if (isset($dir[0]) && $dir[0] == '.') {
  5986. $dir = substr($dir, 1);
  5987. }
  5988. if (isset($dir[0]) && $dir[0] != '/') {
  5989. $dir = '/'.$dir;
  5990. }
  5991. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5992. $dir .= '/';
  5993. }
  5994. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5995. if (!is_dir($filepath)) {
  5996. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5997. }
  5998. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  5999. if (isset($_POST['path']) && !empty($_POST['path'])) {
  6000. $document_id = intval($_POST['path']);
  6001. $sql = "SELECT path FROM ".$table_doc."
  6002. WHERE c_id = $course_id AND id = ".$document_id;
  6003. $res = Database::query($sql);
  6004. $row = Database::fetch_array($res);
  6005. $content = stripslashes($_POST['content_lp']);
  6006. $file = $filepath.$row['path'];
  6007. if ($fp = @ fopen($file, 'w')) {
  6008. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  6009. // Change the path of mp3 to absolute.
  6010. // The first regexp deals with :// urls.
  6011. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/', $content);
  6012. // The second regexp deals with audio/ urls.
  6013. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/', $content);
  6014. fputs($fp, $content);
  6015. fclose($fp);
  6016. $sql = "UPDATE ".$table_doc." SET
  6017. title='".Database::escape_string($_POST['title'])."'
  6018. WHERE c_id = ".$course_id." AND id = ".$document_id;
  6019. Database::query($sql);
  6020. }
  6021. }
  6022. }
  6023. /**
  6024. * Displays the selected item, with a panel for manipulating the item
  6025. * @param int $item_id
  6026. * @param string $msg
  6027. * @return string
  6028. */
  6029. public function display_item($item_id, $msg = null, $show_actions = true)
  6030. {
  6031. $course_id = api_get_course_int_id();
  6032. $return = '';
  6033. if (is_numeric($item_id)) {
  6034. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6035. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  6036. WHERE
  6037. c_id = $course_id AND
  6038. lp.id = ".intval($item_id);
  6039. $result = Database::query($sql);
  6040. while ($row = Database::fetch_array($result, 'ASSOC')) {
  6041. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  6042. // Prevents wrong parent selection for document, see Bug#1251.
  6043. if ($row['item_type'] != 'dir') {
  6044. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  6045. }
  6046. if ($show_actions) {
  6047. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6048. }
  6049. $return .= '<div style="padding:10px;">';
  6050. if ($msg != '') {
  6051. $return .= $msg;
  6052. }
  6053. $return .= '<h3>'.$row['title'].'</h3>';
  6054. switch ($row['item_type']) {
  6055. case TOOL_THREAD:
  6056. $link = $this->rl_get_resource_link_for_learnpath(
  6057. $course_id,
  6058. $row['lp_id'],
  6059. $item_id,
  6060. 0
  6061. );
  6062. $return .= Display::url(
  6063. get_lang('GoToThread'),
  6064. $link,
  6065. ['class' => 'btn btn-primary']
  6066. );
  6067. break;
  6068. case TOOL_FORUM:
  6069. $return .= Display::url(
  6070. get_lang('GoToForum'),
  6071. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$row['path'],
  6072. ['class' => 'btn btn-primary']
  6073. );
  6074. break;
  6075. case TOOL_QUIZ:
  6076. if (!empty($row['path'])) {
  6077. $exercise = new Exercise();
  6078. $exercise->read($row['path']);
  6079. $return .= $exercise->description.'<br />';
  6080. $return .= Display::url(
  6081. get_lang('GoToExercise'),
  6082. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$exercise->id,
  6083. ['class' => 'btn btn-primary']
  6084. );
  6085. }
  6086. break;
  6087. case TOOL_LP_FINAL_ITEM:
  6088. $return .= $this->getSavedFinalItem();
  6089. break;
  6090. case TOOL_DOCUMENT:
  6091. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6092. $sql_doc = "SELECT path FROM ".$tbl_doc."
  6093. WHERE c_id = ".$course_id." AND id = ".intval($row['path']);
  6094. $result = Database::query($sql_doc);
  6095. $path_file = Database::result($result, 0, 0);
  6096. $path_parts = pathinfo($path_file);
  6097. // TODO: Correct the following naive comparisons.
  6098. if (in_array($path_parts['extension'], array(
  6099. 'html',
  6100. 'txt',
  6101. 'png',
  6102. 'jpg',
  6103. 'JPG',
  6104. 'jpeg',
  6105. 'JPEG',
  6106. 'gif',
  6107. 'swf',
  6108. 'pdf',
  6109. 'htm'
  6110. ))) {
  6111. $return .= $this->display_document($row['path'], true, true);
  6112. }
  6113. break;
  6114. case TOOL_HOTPOTATOES:
  6115. $return .= $this->display_document($row['path'], false, true);
  6116. break;
  6117. }
  6118. $return .= '</div>';
  6119. }
  6120. }
  6121. return $return;
  6122. }
  6123. /**
  6124. * Shows the needed forms for editing a specific item
  6125. * @param int $item_id
  6126. * @return string
  6127. */
  6128. public function display_edit_item($item_id)
  6129. {
  6130. $course_id = api_get_course_int_id();
  6131. $return = '';
  6132. if (is_numeric($item_id)) {
  6133. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6134. $sql = "SELECT * FROM $tbl_lp_item
  6135. WHERE c_id = ".$course_id." AND id = ".intval($item_id);
  6136. $res = Database::query($sql);
  6137. $row = Database::fetch_array($res);
  6138. switch ($row['item_type']) {
  6139. case 'dir':
  6140. case 'asset':
  6141. case 'sco':
  6142. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  6143. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6144. $return .= $this->display_item_form(
  6145. $row['item_type'],
  6146. get_lang('EditCurrentChapter').' :',
  6147. 'edit',
  6148. $item_id,
  6149. $row
  6150. );
  6151. } else {
  6152. $return .= $this->display_item_small_form(
  6153. $row['item_type'],
  6154. get_lang('EditCurrentChapter').' :',
  6155. $row
  6156. );
  6157. }
  6158. break;
  6159. case TOOL_DOCUMENT:
  6160. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6161. $sql = "SELECT lp.*, doc.path as dir
  6162. FROM $tbl_lp_item as lp
  6163. LEFT JOIN $tbl_doc as doc
  6164. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6165. WHERE
  6166. lp.c_id = $course_id AND
  6167. doc.c_id = $course_id AND
  6168. lp.id = ".intval($item_id);
  6169. $res_step = Database::query($sql);
  6170. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6171. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6172. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6173. break;
  6174. case TOOL_LINK:
  6175. $link_id = (string) $row['path'];
  6176. if (ctype_digit($link_id)) {
  6177. $tbl_link = Database::get_course_table(TABLE_LINK);
  6178. $sql_select = 'SELECT url FROM '.$tbl_link.'
  6179. WHERE c_id = '.$course_id.' AND id = '.intval($link_id);
  6180. $res_link = Database::query($sql_select);
  6181. $row_link = Database::fetch_array($res_link);
  6182. if (is_array($row_link)) {
  6183. $row['url'] = $row_link['url'];
  6184. }
  6185. }
  6186. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6187. $return .= $this->display_link_form('edit', $item_id, $row);
  6188. break;
  6189. case TOOL_LP_FINAL_ITEM:
  6190. Session::write('finalItem', true);
  6191. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6192. $sql = "SELECT lp.*, doc.path as dir
  6193. FROM $tbl_lp_item as lp
  6194. LEFT JOIN $tbl_doc as doc
  6195. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6196. WHERE
  6197. lp.c_id = $course_id AND
  6198. doc.c_id = $course_id AND
  6199. lp.id = ".intval($item_id);
  6200. $res_step = Database::query($sql);
  6201. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6202. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6203. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6204. break;
  6205. case TOOL_QUIZ:
  6206. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6207. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6208. break;
  6209. case TOOL_HOTPOTATOES:
  6210. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6211. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6212. break;
  6213. case TOOL_STUDENTPUBLICATION:
  6214. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6215. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6216. break;
  6217. case TOOL_FORUM:
  6218. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6219. $return .= $this->display_forum_form('edit', $item_id, $row);
  6220. break;
  6221. case TOOL_THREAD:
  6222. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6223. $return .= $this->display_thread_form('edit', $item_id, $row);
  6224. break;
  6225. }
  6226. }
  6227. return $return;
  6228. }
  6229. /**
  6230. * Function that displays a list with al the resources that
  6231. * could be added to the learning path
  6232. * @return string
  6233. */
  6234. public function display_resources()
  6235. {
  6236. $course_code = api_get_course_id();
  6237. // Get all the docs.
  6238. $documents = $this->get_documents(true);
  6239. // Get all the exercises.
  6240. $exercises = $this->get_exercises();
  6241. // Get all the links.
  6242. $links = $this->get_links();
  6243. // Get all the student publications.
  6244. $works = $this->get_student_publications();
  6245. // Get all the forums.
  6246. $forums = $this->get_forums(null, $course_code);
  6247. // Get the final item form (see BT#11048) .
  6248. $finish = $this->getFinalItemForm();
  6249. $headers = array(
  6250. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  6251. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  6252. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  6253. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  6254. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  6255. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  6256. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6257. );
  6258. echo Display::return_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'), 'normal');
  6259. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6260. echo Display::tabs(
  6261. $headers,
  6262. array(
  6263. $documents,
  6264. $exercises,
  6265. $links,
  6266. $works,
  6267. $forums,
  6268. $dir,
  6269. $finish,
  6270. ),
  6271. 'resource_tab'
  6272. );
  6273. return true;
  6274. }
  6275. /**
  6276. * Returns the extension of a document
  6277. * @param string $filename
  6278. * @return string Extension (part after the last dot)
  6279. */
  6280. public function get_extension($filename)
  6281. {
  6282. $explode = explode('.', $filename);
  6283. return $explode[count($explode) - 1];
  6284. }
  6285. /**
  6286. * Displays a document by id
  6287. *
  6288. * @param int $id
  6289. * @return string
  6290. */
  6291. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6292. {
  6293. $_course = api_get_course_info();
  6294. $course_id = api_get_course_int_id();
  6295. $return = '';
  6296. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6297. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6298. WHERE c_id = ".$course_id." AND id = ".$id;
  6299. $res_doc = Database::query($sql_doc);
  6300. $row_doc = Database::fetch_array($res_doc);
  6301. // TODO: Add a path filter.
  6302. if ($iframe) {
  6303. $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>';
  6304. } else {
  6305. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/'.$row_doc['path']);
  6306. }
  6307. return $return;
  6308. }
  6309. /**
  6310. * Return HTML form to add/edit a quiz
  6311. * @param string $action Action (add/edit)
  6312. * @param integer $id Item ID if already exists
  6313. * @param mixed $extra_info Extra information (quiz ID if integer)
  6314. * @return string HTML form
  6315. */
  6316. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6317. {
  6318. $course_id = api_get_course_int_id();
  6319. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6320. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6321. if ($id != 0 && is_array($extra_info)) {
  6322. $item_title = $extra_info['title'];
  6323. $item_description = $extra_info['description'];
  6324. } elseif (is_numeric($extra_info)) {
  6325. $sql = "SELECT title, description
  6326. FROM $tbl_quiz
  6327. WHERE c_id = $course_id AND id = ".$extra_info;
  6328. $result = Database::query($sql);
  6329. $row = Database::fetch_array($result);
  6330. $item_title = $row['title'];
  6331. $item_description = $row['description'];
  6332. } else {
  6333. $item_title = '';
  6334. $item_description = '';
  6335. }
  6336. $item_title = Security::remove_XSS($item_title);
  6337. $item_description = Security::remove_XSS($item_description);
  6338. if ($id != 0 && is_array($extra_info)) {
  6339. $parent = $extra_info['parent_item_id'];
  6340. } else {
  6341. $parent = 0;
  6342. }
  6343. $sql = "SELECT * FROM $tbl_lp_item
  6344. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  6345. $result = Database::query($sql);
  6346. $arrLP = array();
  6347. while ($row = Database::fetch_array($result)) {
  6348. $arrLP[] = array(
  6349. 'id' => $row['id'],
  6350. 'item_type' => $row['item_type'],
  6351. 'title' => $row['title'],
  6352. 'path' => $row['path'],
  6353. 'description' => $row['description'],
  6354. 'parent_item_id' => $row['parent_item_id'],
  6355. 'previous_item_id' => $row['previous_item_id'],
  6356. 'next_item_id' => $row['next_item_id'],
  6357. 'display_order' => $row['display_order'],
  6358. 'max_score' => $row['max_score'],
  6359. 'min_score' => $row['min_score'],
  6360. 'mastery_score' => $row['mastery_score'],
  6361. 'prerequisite' => $row['prerequisite'],
  6362. 'max_time_allowed' => $row['max_time_allowed']
  6363. );
  6364. }
  6365. $this->tree_array($arrLP);
  6366. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6367. unset($this->arrMenu);
  6368. $form = new FormValidator(
  6369. 'quiz_form',
  6370. 'POST',
  6371. $this->getCurrentBuildingModeURL()
  6372. );
  6373. $defaults = [];
  6374. if ($action == 'add') {
  6375. $legend = get_lang('CreateTheExercise');
  6376. } elseif ($action == 'move') {
  6377. $legend = get_lang('MoveTheCurrentExercise');
  6378. } else {
  6379. $legend = get_lang('EditCurrentExecice');
  6380. }
  6381. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6382. $legend .= Display::return_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6383. }
  6384. $form->addHeader($legend);
  6385. if ($action != 'move') {
  6386. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6387. $defaults['title'] = $item_title;
  6388. }
  6389. // Select for Parent item, root or chapter
  6390. $selectParent = $form->addSelect(
  6391. 'parent',
  6392. get_lang('Parent'),
  6393. [],
  6394. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6395. );
  6396. $selectParent->addOption($this->name, 0);
  6397. $arrHide = array(
  6398. $id
  6399. );
  6400. for ($i = 0; $i < count($arrLP); $i++) {
  6401. if ($action != 'add') {
  6402. if (
  6403. ($arrLP[$i]['item_type'] == 'dir') &&
  6404. !in_array($arrLP[$i]['id'], $arrHide) &&
  6405. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6406. ) {
  6407. $selectParent->addOption(
  6408. $arrLP[$i]['title'],
  6409. $arrLP[$i]['id'],
  6410. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6411. );
  6412. if ($parent == $arrLP[$i]['id']) {
  6413. $selectParent->setSelected($arrLP[$i]['id']);
  6414. }
  6415. } else {
  6416. $arrHide[] = $arrLP[$i]['id'];
  6417. }
  6418. } else {
  6419. if ($arrLP[$i]['item_type'] == 'dir') {
  6420. $selectParent->addOption(
  6421. $arrLP[$i]['title'],
  6422. $arrLP[$i]['id'], ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6423. );
  6424. if ($parent == $arrLP[$i]['id']) {
  6425. $selectParent->setSelected($arrLP[$i]['id']);
  6426. }
  6427. }
  6428. }
  6429. }
  6430. if (is_array($arrLP)) {
  6431. reset($arrLP);
  6432. }
  6433. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6434. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6435. for ($i = 0; $i < count($arrLP); $i++) {
  6436. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6437. $selectPrevious->addOption(get_lang('After').' "'.$arrLP[$i]['title'].'"', $arrLP[$i]['id']);
  6438. if (is_array($extra_info)) {
  6439. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6440. $selectPrevious->setSelected($arrLP[$i]['id']);
  6441. }
  6442. } elseif ($action == 'add') {
  6443. $selectPrevious->setSelected($arrLP[$i]['id']);
  6444. }
  6445. }
  6446. }
  6447. if ($action != 'move') {
  6448. $id_prerequisite = 0;
  6449. if (is_array($arrLP)) {
  6450. foreach ($arrLP as $key => $value) {
  6451. if ($value['id'] == $id) {
  6452. $id_prerequisite = $value['prerequisite'];
  6453. break;
  6454. }
  6455. }
  6456. }
  6457. $arrHide = array();
  6458. for ($i = 0; $i < count($arrLP); $i++) {
  6459. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6460. if (is_array($extra_info)) {
  6461. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6462. $s_selected_position = $arrLP[$i]['id'];
  6463. }
  6464. } elseif ($action == 'add') {
  6465. $s_selected_position = 0;
  6466. }
  6467. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6468. }
  6469. }
  6470. }
  6471. if ($action == 'add') {
  6472. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6473. } else {
  6474. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6475. }
  6476. if ($action == 'move') {
  6477. $form->addHidden('title', $item_title);
  6478. $form->addHidden('description', $item_description);
  6479. }
  6480. if (is_numeric($extra_info)) {
  6481. $form->addHidden('path', $extra_info);
  6482. } elseif (is_array($extra_info)) {
  6483. $form->addHidden('path', $extra_info['path']);
  6484. }
  6485. $form->addHidden('type', TOOL_QUIZ);
  6486. $form->addHidden('post_time', time());
  6487. $form->setDefaults($defaults);
  6488. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  6489. }
  6490. /**
  6491. * Addition of Hotpotatoes tests
  6492. * @param string Action
  6493. * @param integer Internal ID of the item
  6494. * @param mixed Extra information - can be an array with title and description indexes
  6495. * @return string HTML structure to display the hotpotatoes addition formular
  6496. */
  6497. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6498. {
  6499. $course_id = api_get_course_int_id();
  6500. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6501. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6502. if ($id != 0 && is_array($extra_info)) {
  6503. $item_title = stripslashes($extra_info['title']);
  6504. $item_description = stripslashes($extra_info['description']);
  6505. } elseif (is_numeric($extra_info)) {
  6506. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6507. $sql = "SELECT * FROM ".$TBL_DOCUMENT."
  6508. WHERE
  6509. c_id = ".$course_id." AND
  6510. path LIKE '" . $uploadPath."/%/%htm%' AND
  6511. id = " . (int) $extra_info."
  6512. ORDER BY id ASC";
  6513. $res_hot = Database::query($sql);
  6514. $row = Database::fetch_array($res_hot);
  6515. $item_title = $row['title'];
  6516. $item_description = $row['description'];
  6517. if (!empty ($row['comment'])) {
  6518. $item_title = $row['comment'];
  6519. }
  6520. } else {
  6521. $item_title = '';
  6522. $item_description = '';
  6523. }
  6524. if ($id != 0 && is_array($extra_info)) {
  6525. $parent = $extra_info['parent_item_id'];
  6526. } else {
  6527. $parent = 0;
  6528. }
  6529. $sql = "SELECT * FROM $tbl_lp_item
  6530. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6531. $result = Database::query($sql);
  6532. $arrLP = array();
  6533. while ($row = Database::fetch_array($result)) {
  6534. $arrLP[] = array(
  6535. 'id' => $row['id'],
  6536. 'item_type' => $row['item_type'],
  6537. 'title' => $row['title'],
  6538. 'path' => $row['path'],
  6539. 'description' => $row['description'],
  6540. 'parent_item_id' => $row['parent_item_id'],
  6541. 'previous_item_id' => $row['previous_item_id'],
  6542. 'next_item_id' => $row['next_item_id'],
  6543. 'display_order' => $row['display_order'],
  6544. 'max_score' => $row['max_score'],
  6545. 'min_score' => $row['min_score'],
  6546. 'mastery_score' => $row['mastery_score'],
  6547. 'prerequisite' => $row['prerequisite'],
  6548. 'max_time_allowed' => $row['max_time_allowed']
  6549. );
  6550. }
  6551. $legend = '<legend>';
  6552. if ($action == 'add') {
  6553. $legend .= get_lang('CreateTheExercise');
  6554. } elseif ($action == 'move') {
  6555. $legend .= get_lang('MoveTheCurrentExercise');
  6556. } else {
  6557. $legend .= get_lang('EditCurrentExecice');
  6558. }
  6559. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6560. $legend .= Display:: return_message(
  6561. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6562. );
  6563. }
  6564. $legend .= '</legend>';
  6565. $return = '<form method="POST">';
  6566. $return .= $legend;
  6567. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6568. $return .= '<tr>';
  6569. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6570. $return .= '<td class="input">';
  6571. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6572. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6573. $arrHide = array(
  6574. $id
  6575. );
  6576. if (count($arrLP) > 0) {
  6577. for ($i = 0; $i < count($arrLP); $i++) {
  6578. if ($action != 'add') {
  6579. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6580. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6581. } else {
  6582. $arrHide[] = $arrLP[$i]['id'];
  6583. }
  6584. } else {
  6585. if ($arrLP[$i]['item_type'] == 'dir')
  6586. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6587. }
  6588. }
  6589. reset($arrLP);
  6590. }
  6591. $return .= '</select>';
  6592. $return .= '</td>';
  6593. $return .= '</tr>';
  6594. $return .= '<tr>';
  6595. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6596. $return .= '<td class="input">';
  6597. $return .= '<select id="previous" name="previous" size="1">';
  6598. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6599. for ($i = 0; $i < count($arrLP); $i++) {
  6600. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6601. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6602. $selected = 'selected="selected" ';
  6603. elseif ($action == 'add') $selected = 'selected="selected" ';
  6604. else
  6605. $selected = '';
  6606. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang('After').' "'.$arrLP[$i]['title'].'"</option>';
  6607. }
  6608. }
  6609. $return .= '</select>';
  6610. $return .= '</td>';
  6611. $return .= '</tr>';
  6612. if ($action != 'move') {
  6613. $return .= '<tr>';
  6614. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6615. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6616. $return .= '</tr>';
  6617. $id_prerequisite = 0;
  6618. if (is_array($arrLP) && count($arrLP) > 0) {
  6619. foreach ($arrLP as $key => $value) {
  6620. if ($value['id'] == $id) {
  6621. $id_prerequisite = $value['prerequisite'];
  6622. break;
  6623. }
  6624. }
  6625. $arrHide = array();
  6626. for ($i = 0; $i < count($arrLP); $i++) {
  6627. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6628. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6629. $s_selected_position = $arrLP[$i]['id'];
  6630. elseif ($action == 'add') $s_selected_position = 0;
  6631. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6632. }
  6633. }
  6634. }
  6635. }
  6636. $return .= '<tr>';
  6637. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang('SaveHotpotatoes').'</button></td>';
  6638. $return .= '</tr>';
  6639. $return .= '</table>';
  6640. if ($action == 'move') {
  6641. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6642. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6643. }
  6644. if (is_numeric($extra_info)) {
  6645. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6646. } elseif (is_array($extra_info)) {
  6647. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6648. }
  6649. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6650. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6651. $return .= '</form>';
  6652. return $return;
  6653. }
  6654. /**
  6655. * Return the form to display the forum edit/add option
  6656. * @param string Action (add/edit)
  6657. * @param integer ID of the lp_item if already exists
  6658. * @param mixed Forum ID or title
  6659. * @return string HTML form
  6660. */
  6661. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6662. {
  6663. $course_id = api_get_course_int_id();
  6664. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6665. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6666. if ($id != 0 && is_array($extra_info)) {
  6667. $item_title = stripslashes($extra_info['title']);
  6668. } elseif (is_numeric($extra_info)) {
  6669. $sql = "SELECT forum_title as title, forum_comment as comment
  6670. FROM " . $tbl_forum."
  6671. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6672. $result = Database::query($sql);
  6673. $row = Database::fetch_array($result);
  6674. $item_title = $row['title'];
  6675. $item_description = $row['comment'];
  6676. } else {
  6677. $item_title = '';
  6678. $item_description = '';
  6679. }
  6680. if ($id != 0 && is_array($extra_info)) {
  6681. $parent = $extra_info['parent_item_id'];
  6682. } else {
  6683. $parent = 0;
  6684. }
  6685. $sql = "SELECT * FROM ".$tbl_lp_item."
  6686. WHERE
  6687. c_id = ".$course_id." AND
  6688. lp_id = " . $this->lp_id;
  6689. $result = Database::query($sql);
  6690. $arrLP = array();
  6691. while ($row = Database::fetch_array($result)) {
  6692. $arrLP[] = array(
  6693. 'id' => $row['id'],
  6694. 'item_type' => $row['item_type'],
  6695. 'title' => $row['title'],
  6696. 'path' => $row['path'],
  6697. 'description' => $row['description'],
  6698. 'parent_item_id' => $row['parent_item_id'],
  6699. 'previous_item_id' => $row['previous_item_id'],
  6700. 'next_item_id' => $row['next_item_id'],
  6701. 'display_order' => $row['display_order'],
  6702. 'max_score' => $row['max_score'],
  6703. 'min_score' => $row['min_score'],
  6704. 'mastery_score' => $row['mastery_score'],
  6705. 'prerequisite' => $row['prerequisite']
  6706. );
  6707. }
  6708. $this->tree_array($arrLP);
  6709. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6710. unset($this->arrMenu);
  6711. if ($action == 'add') {
  6712. $legend = get_lang('CreateTheForum');
  6713. } elseif ($action == 'move') {
  6714. $legend = get_lang('MoveTheCurrentForum');
  6715. } else {
  6716. $legend = get_lang('EditCurrentForum');
  6717. }
  6718. $form = new FormValidator(
  6719. 'forum_form',
  6720. 'POST',
  6721. $this->getCurrentBuildingModeURL()
  6722. );
  6723. $defaults = [];
  6724. $form->addHeader($legend);
  6725. if ($action != 'move') {
  6726. $form->addText(
  6727. 'title',
  6728. get_lang('Title'),
  6729. true,
  6730. ['id' => 'idTitle', 'class' => 'learnpath_item_form']
  6731. );
  6732. $defaults['title'] = $item_title;
  6733. }
  6734. $selectParent = $form->addSelect(
  6735. 'parent',
  6736. get_lang('Parent'),
  6737. [],
  6738. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6739. );
  6740. $selectParent->addOption($this->name, 0);
  6741. $arrHide = array(
  6742. $id
  6743. );
  6744. //$parent_item_id = $_SESSION['parent_item_id'];
  6745. for ($i = 0; $i < count($arrLP); $i++) {
  6746. if ($action != 'add') {
  6747. if ($arrLP[$i]['item_type'] == 'dir' &&
  6748. !in_array($arrLP[$i]['id'], $arrHide) &&
  6749. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6750. ) {
  6751. $selectParent->addOption(
  6752. $arrLP[$i]['title'],
  6753. $arrLP[$i]['id'],
  6754. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6755. );
  6756. if ($parent == $arrLP[$i]['id']) {
  6757. $selectParent->setSelected($arrLP[$i]['id']);
  6758. }
  6759. } else {
  6760. $arrHide[] = $arrLP[$i]['id'];
  6761. }
  6762. } else {
  6763. if ($arrLP[$i]['item_type'] == 'dir') {
  6764. $selectParent->addOption(
  6765. $arrLP[$i]['title'],
  6766. $arrLP[$i]['id'],
  6767. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6768. );
  6769. if ($parent == $arrLP[$i]['id']) {
  6770. $selectParent->setSelected($arrLP[$i]['id']);
  6771. }
  6772. }
  6773. }
  6774. }
  6775. if (is_array($arrLP)) {
  6776. reset($arrLP);
  6777. }
  6778. $selectPrevious = $form->addSelect(
  6779. 'previous',
  6780. get_lang('Position'),
  6781. [],
  6782. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6783. );
  6784. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6785. for ($i = 0; $i < count($arrLP); $i++) {
  6786. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6787. $selectPrevious->addOption(get_lang('After').' "'.$arrLP[$i]['title'].'"', $arrLP[$i]['id']);
  6788. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6789. $selectPrevious->setSelected($arrLP[$i]['id']);
  6790. } elseif ($action == 'add') {
  6791. $selectPrevious->setSelected($arrLP[$i]['id']);
  6792. }
  6793. }
  6794. }
  6795. if ($action != 'move') {
  6796. $id_prerequisite = 0;
  6797. if (is_array($arrLP)) {
  6798. foreach ($arrLP as $key => $value) {
  6799. if ($value['id'] == $id) {
  6800. $id_prerequisite = $value['prerequisite'];
  6801. break;
  6802. }
  6803. }
  6804. }
  6805. $arrHide = array();
  6806. for ($i = 0; $i < count($arrLP); $i++) {
  6807. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6808. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6809. $s_selected_position = $arrLP[$i]['id'];
  6810. elseif ($action == 'add') $s_selected_position = 0;
  6811. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6812. }
  6813. }
  6814. }
  6815. if ($action == 'add') {
  6816. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6817. } else {
  6818. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6819. }
  6820. if ($action == 'move') {
  6821. $form->addHidden('title', $item_title);
  6822. $form->addHidden('description', $item_description);
  6823. }
  6824. if (is_numeric($extra_info)) {
  6825. $form->addHidden('path', $extra_info);
  6826. } elseif (is_array($extra_info)) {
  6827. $form->addHidden('path', $extra_info['path']);
  6828. }
  6829. $form->addHidden('type', TOOL_FORUM);
  6830. $form->addHidden('post_time', time());
  6831. $form->setDefaults($defaults);
  6832. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  6833. }
  6834. /**
  6835. * Return HTML form to add/edit forum threads
  6836. * @param string Action (add/edit)
  6837. * @param integer Item ID if already exists in learning path
  6838. * @param mixed Extra information (thread ID if integer)
  6839. * @return string HTML form
  6840. */
  6841. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6842. {
  6843. $course_id = api_get_course_int_id();
  6844. if (empty($course_id)) {
  6845. return null;
  6846. }
  6847. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6848. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  6849. if ($id != 0 && is_array($extra_info)) {
  6850. $item_title = stripslashes($extra_info['title']);
  6851. } elseif (is_numeric($extra_info)) {
  6852. $sql = "SELECT thread_title as title FROM $tbl_forum
  6853. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6854. $result = Database::query($sql);
  6855. $row = Database::fetch_array($result);
  6856. $item_title = $row['title'];
  6857. $item_description = '';
  6858. } else {
  6859. $item_title = '';
  6860. $item_description = '';
  6861. }
  6862. if ($id != 0 && is_array($extra_info)) {
  6863. $parent = $extra_info['parent_item_id'];
  6864. } else {
  6865. $parent = 0;
  6866. }
  6867. $sql = "SELECT * FROM ".$tbl_lp_item."
  6868. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6869. $result = Database::query($sql);
  6870. $arrLP = array();
  6871. while ($row = Database::fetch_array($result)) {
  6872. $arrLP[] = array(
  6873. 'id' => $row['id'],
  6874. 'item_type' => $row['item_type'],
  6875. 'title' => $row['title'],
  6876. 'path' => $row['path'],
  6877. 'description' => $row['description'],
  6878. 'parent_item_id' => $row['parent_item_id'],
  6879. 'previous_item_id' => $row['previous_item_id'],
  6880. 'next_item_id' => $row['next_item_id'],
  6881. 'display_order' => $row['display_order'],
  6882. 'max_score' => $row['max_score'],
  6883. 'min_score' => $row['min_score'],
  6884. 'mastery_score' => $row['mastery_score'],
  6885. 'prerequisite' => $row['prerequisite']
  6886. );
  6887. }
  6888. $this->tree_array($arrLP);
  6889. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6890. unset($this->arrMenu);
  6891. $form = new FormValidator(
  6892. 'thread_form',
  6893. 'POST',
  6894. $this->getCurrentBuildingModeURL()
  6895. );
  6896. $defaults = [];
  6897. if ($action == 'add') {
  6898. $legend = get_lang('CreateTheForum');
  6899. } elseif ($action == 'move') {
  6900. $legend = get_lang('MoveTheCurrentForum');
  6901. } else {
  6902. $legend = get_lang('EditCurrentForum');
  6903. }
  6904. $form->addHeader($legend);
  6905. $selectParent = $form->addSelect(
  6906. 'parent',
  6907. get_lang('Parent'),
  6908. [],
  6909. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6910. );
  6911. $selectParent->addOption($this->name, 0);
  6912. $arrHide = array(
  6913. $id
  6914. );
  6915. for ($i = 0; $i < count($arrLP); $i++) {
  6916. if ($action != 'add') {
  6917. if (
  6918. ($arrLP[$i]['item_type'] == 'dir') &&
  6919. !in_array($arrLP[$i]['id'], $arrHide) &&
  6920. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6921. ) {
  6922. $selectParent->addOption(
  6923. $arrLP[$i]['title'],
  6924. $arrLP[$i]['id'],
  6925. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6926. );
  6927. if ($parent == $arrLP[$i]['id']) {
  6928. $selectParent->setSelected($arrLP[$i]['id']);
  6929. }
  6930. } else {
  6931. $arrHide[] = $arrLP[$i]['id'];
  6932. }
  6933. } else {
  6934. if ($arrLP[$i]['item_type'] == 'dir') {
  6935. $selectParent->addOption(
  6936. $arrLP[$i]['title'],
  6937. $arrLP[$i]['id'],
  6938. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6939. );
  6940. if ($parent == $arrLP[$i]['id']) {
  6941. $selectParent->setSelected($arrLP[$i]['id']);
  6942. }
  6943. }
  6944. }
  6945. }
  6946. if ($arrLP != null) {
  6947. reset($arrLP);
  6948. }
  6949. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6950. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6951. for ($i = 0; $i < count($arrLP); $i++) {
  6952. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6953. $selectPrevious->addOption(
  6954. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  6955. $arrLP[$i]['id']
  6956. );
  6957. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6958. $selectPrevious->setSelected($arrLP[$i]['id']);
  6959. } elseif ($action == 'add') {
  6960. $selectPrevious->setSelected($arrLP[$i]['id']);
  6961. }
  6962. }
  6963. }
  6964. if ($action != 'move') {
  6965. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6966. $defaults['title'] = $item_title;
  6967. $id_prerequisite = 0;
  6968. if ($arrLP != null) {
  6969. foreach ($arrLP as $key => $value) {
  6970. if ($value['id'] == $id) {
  6971. $id_prerequisite = $value['prerequisite'];
  6972. break;
  6973. }
  6974. }
  6975. }
  6976. $arrHide = array();
  6977. $s_selected_position = 0;
  6978. for ($i = 0; $i < count($arrLP); $i++) {
  6979. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6980. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6981. $s_selected_position = $arrLP[$i]['id'];
  6982. elseif ($action == 'add') $s_selected_position = 0;
  6983. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6984. }
  6985. }
  6986. $selectPrerequisites = $form->addSelect(
  6987. 'prerequisites',
  6988. get_lang('LearnpathPrerequisites'),
  6989. [],
  6990. ['id' => 'prerequisites']
  6991. );
  6992. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6993. foreach ($arrHide as $key => $value) {
  6994. $selectPrerequisites->addOption($value['value'], $key);
  6995. if ($key == $s_selected_position && $action == 'add') {
  6996. $selectPrerequisites->setSelected($key);
  6997. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6998. $selectPrerequisites->setSelected($key);
  6999. }
  7000. }
  7001. }
  7002. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  7003. if ($action == 'move') {
  7004. $form->addHidden('title', $item_title);
  7005. $form->addHidden('description', $item_description);
  7006. }
  7007. if (is_numeric($extra_info)) {
  7008. $form->addHidden('path', $extra_info);
  7009. }
  7010. elseif (is_array($extra_info)) {
  7011. $form->addHidden('path', $extra_info['path']);
  7012. }
  7013. $form->addHidden('type', TOOL_THREAD);
  7014. $form->addHidden('post_time', time());
  7015. $form->setDefaults($defaults);
  7016. return $form->returnForm();
  7017. }
  7018. /**
  7019. * Return the HTML form to display an item (generally a dir item)
  7020. * @param string Item type (dir)
  7021. * @param string Title (optional, only when creating)
  7022. * @param string Action ('add'/'edit')
  7023. * @param integer lp_item ID
  7024. * @param mixed Extra info
  7025. * @return string HTML form
  7026. */
  7027. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  7028. {
  7029. $course_id = api_get_course_int_id();
  7030. $_course = api_get_course_info();
  7031. global $charset;
  7032. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7033. if ($id != 0 && is_array($extra_info)) {
  7034. $item_title = $extra_info['title'];
  7035. $item_description = $extra_info['description'];
  7036. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  7037. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  7038. } else {
  7039. $item_title = '';
  7040. $item_description = '';
  7041. $item_path_fck = '';
  7042. }
  7043. if ($id != 0 && is_array($extra_info)) {
  7044. $parent = $extra_info['parent_item_id'];
  7045. } else {
  7046. $parent = 0;
  7047. }
  7048. $id = intval($id);
  7049. $sql = "SELECT * FROM ".$tbl_lp_item."
  7050. WHERE
  7051. c_id = ".$course_id." AND
  7052. lp_id = " . $this->lp_id." AND
  7053. id != $id";
  7054. if ($item_type == 'dir') {
  7055. $sql .= " AND parent_item_id = 0";
  7056. }
  7057. $result = Database::query($sql);
  7058. $arrLP = [];
  7059. while ($row = Database::fetch_array($result)) {
  7060. $arrLP[] = array(
  7061. 'id' => $row['id'],
  7062. 'item_type' => $row['item_type'],
  7063. 'title' => $row['title'],
  7064. 'path' => $row['path'],
  7065. 'description' => $row['description'],
  7066. 'parent_item_id' => $row['parent_item_id'],
  7067. 'previous_item_id' => $row['previous_item_id'],
  7068. 'next_item_id' => $row['next_item_id'],
  7069. 'max_score' => $row['max_score'],
  7070. 'min_score' => $row['min_score'],
  7071. 'mastery_score' => $row['mastery_score'],
  7072. 'prerequisite' => $row['prerequisite'],
  7073. 'display_order' => $row['display_order']
  7074. );
  7075. }
  7076. $this->tree_array($arrLP);
  7077. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7078. unset($this->arrMenu);
  7079. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  7080. $url = api_get_self().'?'.api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  7081. $form = new FormValidator('form', 'POST', $url);
  7082. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  7083. $defaults['description'] = $item_description;
  7084. $form->addElement('header', $title);
  7085. //$arrHide = array($id);
  7086. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  7087. $arrHide[0]['padding'] = 20;
  7088. $charset = api_get_system_encoding();
  7089. for ($i = 0; $i < count($arrLP); $i++) {
  7090. if ($action != 'add') {
  7091. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7092. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7093. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7094. if ($parent == $arrLP[$i]['id']) {
  7095. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7096. }
  7097. }
  7098. } else {
  7099. if ($arrLP[$i]['item_type'] == 'dir') {
  7100. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7101. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7102. if ($parent == $arrLP[$i]['id']) {
  7103. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7104. }
  7105. }
  7106. }
  7107. }
  7108. if ($action != 'move') {
  7109. $form->addElement('text', 'title', get_lang('Title'));
  7110. $form->applyFilter('title', 'html_filter');
  7111. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  7112. } else {
  7113. $form->addElement('hidden', 'title');
  7114. }
  7115. $parent_select = $form->addElement(
  7116. 'select',
  7117. 'parent',
  7118. get_lang('Parent'),
  7119. '',
  7120. array(
  7121. 'id' => 'idParent',
  7122. 'onchange' => "javascript: load_cbo(this.value);",
  7123. )
  7124. );
  7125. foreach ($arrHide as $key => $value) {
  7126. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7127. }
  7128. if (!empty($s_selected_parent)) {
  7129. $parent_select->setSelected($s_selected_parent);
  7130. }
  7131. if (is_array($arrLP)) {
  7132. reset($arrLP);
  7133. }
  7134. $arrHide = array();
  7135. // POSITION
  7136. for ($i = 0; $i < count($arrLP); $i++) {
  7137. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7138. //this is the same!
  7139. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7140. $s_selected_position = $arrLP[$i]['id'];
  7141. } elseif ($action == 'add') {
  7142. $s_selected_position = $arrLP[$i]['id'];
  7143. }
  7144. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7145. }
  7146. }
  7147. $position = $form->addElement(
  7148. 'select',
  7149. 'previous',
  7150. get_lang('Position'),
  7151. '',
  7152. array('id' => 'previous')
  7153. );
  7154. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7155. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  7156. foreach ($arrHide as $key => $value) {
  7157. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  7158. }
  7159. if (!empty ($s_selected_position)) {
  7160. $position->setSelected($s_selected_position);
  7161. }
  7162. if (is_array($arrLP)) {
  7163. reset($arrLP);
  7164. }
  7165. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  7166. //fix in order to use the tab
  7167. if ($item_type == 'dir') {
  7168. $form->addElement('hidden', 'type', 'dir');
  7169. }
  7170. $extension = null;
  7171. if (!empty($item_path)) {
  7172. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7173. }
  7174. //assets can't be modified
  7175. //$item_type == 'asset' ||
  7176. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7177. if ($item_type == 'sco') {
  7178. $form->addElement('html', '<script>alert("'.get_lang('WarningWhenEditingScorm').'")</script>');
  7179. }
  7180. $renderer = $form->defaultRenderer();
  7181. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7182. $relative_prefix = '';
  7183. $editor_config = array(
  7184. 'ToolbarSet' => 'LearningPathDocuments',
  7185. 'Width' => '100%',
  7186. 'Height' => '500',
  7187. 'FullPage' => true,
  7188. 'CreateDocumentDir' => $relative_prefix,
  7189. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7190. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7191. );
  7192. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7193. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7194. $defaults['content_lp'] = file_get_contents($content_path);
  7195. }
  7196. $form->addElement('hidden', 'type', $item_type);
  7197. $form->addElement('hidden', 'post_time', time());
  7198. $form->setDefaults($defaults);
  7199. return $form->return_form();
  7200. }
  7201. /**
  7202. * @return string
  7203. */
  7204. public function getCurrentBuildingModeURL()
  7205. {
  7206. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7207. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7208. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7209. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7210. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7211. return $currentUrl;
  7212. }
  7213. /**
  7214. * Returns the form to update or create a document
  7215. * @param string $action (add/edit)
  7216. * @param integer $id ID of the lp_item (if already exists)
  7217. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7218. * @return string HTML form
  7219. */
  7220. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7221. {
  7222. $course_id = api_get_course_int_id();
  7223. $_course = api_get_course_info();
  7224. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7225. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7226. $no_display_edit_textarea = false;
  7227. $item_description = '';
  7228. //If action==edit document
  7229. //We don't display the document form if it's not an editable document (html or txt file)
  7230. if ($action == 'edit') {
  7231. if (is_array($extra_info)) {
  7232. $path_parts = pathinfo($extra_info['dir']);
  7233. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7234. $no_display_edit_textarea = true;
  7235. }
  7236. }
  7237. }
  7238. $no_display_add = false;
  7239. // If action==add an existing document
  7240. // We don't display the document form if it's not an editable document (html or txt file).
  7241. if ($action == 'add') {
  7242. if (is_numeric($extra_info)) {
  7243. $sql_doc = "SELECT path FROM $tbl_doc
  7244. WHERE c_id = $course_id AND id = ".intval($extra_info);
  7245. $result = Database::query($sql_doc);
  7246. $path_file = Database::result($result, 0, 0);
  7247. $path_parts = pathinfo($path_file);
  7248. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7249. $no_display_add = true;
  7250. }
  7251. }
  7252. }
  7253. if ($id != 0 && is_array($extra_info)) {
  7254. $item_title = stripslashes($extra_info['title']);
  7255. $item_description = stripslashes($extra_info['description']);
  7256. $item_terms = stripslashes($extra_info['terms']);
  7257. if (empty ($item_title)) {
  7258. $path_parts = pathinfo($extra_info['path']);
  7259. $item_title = stripslashes($path_parts['filename']);
  7260. }
  7261. } elseif (is_numeric($extra_info)) {
  7262. $sql = "SELECT path, title FROM $tbl_doc
  7263. WHERE
  7264. c_id = ".$course_id." AND
  7265. id = " . intval($extra_info);
  7266. $result = Database::query($sql);
  7267. $row = Database::fetch_array($result);
  7268. $item_title = $row['title'];
  7269. $item_title = str_replace('_', ' ', $item_title);
  7270. if (empty ($item_title)) {
  7271. $path_parts = pathinfo($row['path']);
  7272. $item_title = stripslashes($path_parts['filename']);
  7273. }
  7274. } else {
  7275. $item_title = '';
  7276. $item_description = '';
  7277. }
  7278. $return = '<legend>';
  7279. if ($id != 0 && is_array($extra_info)) {
  7280. $parent = $extra_info['parent_item_id'];
  7281. } else {
  7282. $parent = 0;
  7283. }
  7284. $sql = "SELECT * FROM $tbl_lp_item
  7285. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7286. $result = Database::query($sql);
  7287. $arrLP = array();
  7288. while ($row = Database::fetch_array($result)) {
  7289. $arrLP[] = array(
  7290. 'id' => $row['id'],
  7291. 'item_type' => $row['item_type'],
  7292. 'title' => $row['title'],
  7293. 'path' => $row['path'],
  7294. 'description' => $row['description'],
  7295. 'parent_item_id' => $row['parent_item_id'],
  7296. 'previous_item_id' => $row['previous_item_id'],
  7297. 'next_item_id' => $row['next_item_id'],
  7298. 'display_order' => $row['display_order'],
  7299. 'max_score' => $row['max_score'],
  7300. 'min_score' => $row['min_score'],
  7301. 'mastery_score' => $row['mastery_score'],
  7302. 'prerequisite' => $row['prerequisite']
  7303. );
  7304. }
  7305. $this->tree_array($arrLP);
  7306. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7307. unset($this->arrMenu);
  7308. if ($action == 'add') {
  7309. $return .= get_lang('CreateTheDocument');
  7310. } elseif ($action == 'move') {
  7311. $return .= get_lang('MoveTheCurrentDocument');
  7312. } else {
  7313. $return .= get_lang('EditTheCurrentDocument');
  7314. }
  7315. $return .= '</legend>';
  7316. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7317. $return .= Display::return_message(
  7318. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7319. false
  7320. );
  7321. }
  7322. $form = new FormValidator(
  7323. 'form',
  7324. 'POST',
  7325. $this->getCurrentBuildingModeURL(),
  7326. '',
  7327. array('enctype' => 'multipart/form-data')
  7328. );
  7329. $defaults['title'] = Security::remove_XSS($item_title);
  7330. if (empty($item_title)) {
  7331. $defaults['title'] = Security::remove_XSS($item_title);
  7332. }
  7333. $defaults['description'] = $item_description;
  7334. $form->addElement('html', $return);
  7335. if ($action != 'move') {
  7336. $data = $this->generate_lp_folder($_course);
  7337. if ($action != 'edit') {
  7338. $folders = DocumentManager::get_all_document_folders(
  7339. $_course,
  7340. 0,
  7341. true
  7342. );
  7343. DocumentManager::build_directory_selector(
  7344. $folders,
  7345. '',
  7346. array(),
  7347. true,
  7348. $form,
  7349. 'directory_parent_id'
  7350. );
  7351. }
  7352. if (isset($data['id'])) {
  7353. $defaults['directory_parent_id'] = $data['id'];
  7354. }
  7355. $form->addElement(
  7356. 'text',
  7357. 'title',
  7358. get_lang('Title'),
  7359. array('id' => 'idTitle', 'class' => 'col-md-4')
  7360. );
  7361. $form->applyFilter('title', 'html_filter');
  7362. }
  7363. $arrHide[0]['value'] = $this->name;
  7364. $arrHide[0]['padding'] = 20;
  7365. for ($i = 0; $i < count($arrLP); $i++) {
  7366. if ($action != 'add') {
  7367. if ($arrLP[$i]['item_type'] == 'dir' &&
  7368. !in_array($arrLP[$i]['id'], $arrHide) &&
  7369. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7370. ) {
  7371. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7372. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7373. if ($parent == $arrLP[$i]['id']) {
  7374. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7375. }
  7376. }
  7377. } else {
  7378. if ($arrLP[$i]['item_type'] == 'dir') {
  7379. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7380. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7381. if ($parent == $arrLP[$i]['id']) {
  7382. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7383. }
  7384. }
  7385. }
  7386. }
  7387. $parent_select = $form->addSelect(
  7388. 'parent',
  7389. get_lang('Parent'),
  7390. [],
  7391. [
  7392. 'id' => 'idParent',
  7393. 'onchange' => 'javascript: load_cbo(this.value);',
  7394. ]
  7395. );
  7396. $my_count = 0;
  7397. foreach ($arrHide as $key => $value) {
  7398. if ($my_count != 0) {
  7399. // The LP name is also the first section and is not in the same charset like the other sections.
  7400. $value['value'] = Security::remove_XSS($value['value']);
  7401. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7402. } else {
  7403. $value['value'] = Security::remove_XSS($value['value']);
  7404. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7405. }
  7406. $my_count++;
  7407. }
  7408. if (!empty($id)) {
  7409. $parent_select->setSelected($parent);
  7410. } else {
  7411. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7412. $parent_select->setSelected($parent_item_id);
  7413. }
  7414. if (is_array($arrLP)) {
  7415. reset($arrLP);
  7416. }
  7417. $arrHide = array();
  7418. $s_selected_position = null;
  7419. // POSITION
  7420. $lastPosition = null;
  7421. for ($i = 0; $i < count($arrLP); $i++) {
  7422. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7423. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7424. ) {
  7425. if ((isset($extra_info['previous_item_id']) &&
  7426. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  7427. ) {
  7428. $s_selected_position = $arrLP[$i]['id'];
  7429. }
  7430. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7431. }
  7432. $lastPosition = $arrLP[$i]['id'];
  7433. }
  7434. if (empty($s_selected_position)) {
  7435. $s_selected_position = $lastPosition;
  7436. }
  7437. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7438. $position->addOption(get_lang('FirstPosition'), 0);
  7439. foreach ($arrHide as $key => $value) {
  7440. $padding = isset($value['padding']) ? $value['padding'] : 20;
  7441. $position->addOption(
  7442. $value['value'],
  7443. $key,
  7444. 'style="padding-left:'.$padding.'px;"'
  7445. );
  7446. }
  7447. $position->setSelected($s_selected_position);
  7448. if (is_array($arrLP)) {
  7449. reset($arrLP);
  7450. }
  7451. if ($action != 'move') {
  7452. $id_prerequisite = 0;
  7453. if (is_array($arrLP)) {
  7454. foreach ($arrLP as $key => $value) {
  7455. if ($value['id'] == $id) {
  7456. $id_prerequisite = $value['prerequisite'];
  7457. break;
  7458. }
  7459. }
  7460. }
  7461. $arrHide = array();
  7462. for ($i = 0; $i < count($arrLP); $i++) {
  7463. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7464. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7465. $s_selected_position = $arrLP[$i]['id'];
  7466. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7467. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7468. }
  7469. }
  7470. if (!$no_display_add) {
  7471. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7472. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7473. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true') {
  7474. if (isset ($_POST['content'])) {
  7475. $content = stripslashes($_POST['content']);
  7476. } elseif (is_array($extra_info)) {
  7477. //If it's an html document or a text file
  7478. if (!$no_display_edit_textarea) {
  7479. $content = $this->display_document($extra_info['path'], false, false);
  7480. }
  7481. } elseif (is_numeric($extra_info)) {
  7482. $content = $this->display_document(
  7483. $extra_info,
  7484. false,
  7485. false
  7486. );
  7487. } else {
  7488. $content = '';
  7489. }
  7490. if (!$no_display_edit_textarea) {
  7491. // We need to calculate here some specific settings for the online editor.
  7492. // The calculated settings work for documents in the Documents tool
  7493. // (on the root or in subfolders).
  7494. // For documents in native scorm packages it is unclear whether the
  7495. // online editor should be activated or not.
  7496. // A new document, it is in the root of the repository.
  7497. $relative_path = '';
  7498. $relative_prefix = '';
  7499. if (is_array($extra_info) && $extra_info != 'new') {
  7500. // The document already exists. Whe have to determine its relative path towards the repository root.
  7501. $relative_path = explode('/', $extra_info['dir']);
  7502. $cnt = count($relative_path) - 2;
  7503. if ($cnt < 0) {
  7504. $cnt = 0;
  7505. }
  7506. $relative_prefix = str_repeat('../', $cnt);
  7507. $relative_path = array_slice($relative_path, 1, $cnt);
  7508. $relative_path = implode('/', $relative_path);
  7509. if (strlen($relative_path) > 0) {
  7510. $relative_path = $relative_path.'/';
  7511. }
  7512. } else {
  7513. $result = $this->generate_lp_folder($_course);
  7514. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7515. $relative_prefix = '../../';
  7516. }
  7517. $editor_config = array(
  7518. 'ToolbarSet' => 'LearningPathDocuments',
  7519. 'Width' => '100%',
  7520. 'Height' => '500',
  7521. 'FullPage' => true,
  7522. 'CreateDocumentDir' => $relative_prefix,
  7523. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7524. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7525. );
  7526. if ($_GET['action'] == 'add_item') {
  7527. $class = 'add';
  7528. $text = get_lang('LPCreateDocument');
  7529. } else {
  7530. if ($_GET['action'] == 'edit_item') {
  7531. $class = 'save';
  7532. $text = get_lang('SaveDocument');
  7533. }
  7534. }
  7535. $form->addButtonSave($text, 'submit_button');
  7536. $renderer = $form->defaultRenderer();
  7537. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7538. $form->addElement('html', '<div class="editor-lp">');
  7539. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7540. $form->addElement('html', '</div>');
  7541. $defaults['content_lp'] = $content;
  7542. }
  7543. } elseif (is_numeric($extra_info)) {
  7544. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7545. $return = $this->display_document($extra_info, true, true, true);
  7546. $form->addElement('html', $return);
  7547. }
  7548. }
  7549. }
  7550. if (isset($extra_info['item_type']) &&
  7551. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7552. ) {
  7553. $parent_select->freeze();
  7554. $position->freeze();
  7555. }
  7556. if ($action == 'move') {
  7557. $form->addElement('hidden', 'title', $item_title);
  7558. $form->addElement('hidden', 'description', $item_description);
  7559. }
  7560. if (is_numeric($extra_info)) {
  7561. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7562. $form->addElement('hidden', 'path', $extra_info);
  7563. } elseif (is_array($extra_info)) {
  7564. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7565. $form->addElement('hidden', 'path', $extra_info['path']);
  7566. }
  7567. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7568. $form->addElement('hidden', 'post_time', time());
  7569. $form->setDefaults($defaults);
  7570. return $form->returnForm();
  7571. }
  7572. /**
  7573. * Return HTML form to add/edit a link item
  7574. * @param string $action (add/edit)
  7575. * @param integer $id Item ID if exists
  7576. * @param mixed $extra_info
  7577. * @return string HTML form
  7578. */
  7579. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7580. {
  7581. $course_id = api_get_course_int_id();
  7582. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7583. $tbl_link = Database::get_course_table(TABLE_LINK);
  7584. if ($id != 0 && is_array($extra_info)) {
  7585. $item_title = stripslashes($extra_info['title']);
  7586. $item_description = stripslashes($extra_info['description']);
  7587. $item_url = stripslashes($extra_info['url']);
  7588. } elseif (is_numeric($extra_info)) {
  7589. $extra_info = intval($extra_info);
  7590. $sql = "SELECT title, description, url FROM ".$tbl_link."
  7591. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7592. $result = Database::query($sql);
  7593. $row = Database::fetch_array($result);
  7594. $item_title = $row['title'];
  7595. $item_description = $row['description'];
  7596. $item_url = $row['url'];
  7597. } else {
  7598. $item_title = '';
  7599. $item_description = '';
  7600. $item_url = '';
  7601. }
  7602. $form = new FormValidator(
  7603. 'edit_link',
  7604. 'POST',
  7605. $this->getCurrentBuildingModeURL()
  7606. );
  7607. $defaults = [];
  7608. if ($id != 0 && is_array($extra_info)) {
  7609. $parent = $extra_info['parent_item_id'];
  7610. } else {
  7611. $parent = 0;
  7612. }
  7613. $sql = "SELECT * FROM ".$tbl_lp_item."
  7614. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7615. $result = Database::query($sql);
  7616. $arrLP = array();
  7617. while ($row = Database::fetch_array($result)) {
  7618. $arrLP[] = array(
  7619. 'id' => $row['id'],
  7620. 'item_type' => $row['item_type'],
  7621. 'title' => $row['title'],
  7622. 'path' => $row['path'],
  7623. 'description' => $row['description'],
  7624. 'parent_item_id' => $row['parent_item_id'],
  7625. 'previous_item_id' => $row['previous_item_id'],
  7626. 'next_item_id' => $row['next_item_id'],
  7627. 'display_order' => $row['display_order'],
  7628. 'max_score' => $row['max_score'],
  7629. 'min_score' => $row['min_score'],
  7630. 'mastery_score' => $row['mastery_score'],
  7631. 'prerequisite' => $row['prerequisite']
  7632. );
  7633. }
  7634. $this->tree_array($arrLP);
  7635. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7636. unset($this->arrMenu);
  7637. if ($action == 'add') {
  7638. $legend = get_lang('CreateTheLink');
  7639. } elseif ($action == 'move') {
  7640. $legend = get_lang('MoveCurrentLink');
  7641. } else {
  7642. $legend = get_lang('EditCurrentLink');
  7643. }
  7644. $form->addHeader($legend);
  7645. if ($action != 'move') {
  7646. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7647. $defaults['title'] = $item_title;
  7648. }
  7649. $selectParent = $form->addSelect(
  7650. 'parent',
  7651. get_lang('Parent'),
  7652. [],
  7653. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7654. );
  7655. $selectParent->addOption($this->name, 0);
  7656. $arrHide = array(
  7657. $id
  7658. );
  7659. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7660. for ($i = 0; $i < count($arrLP); $i++) {
  7661. if ($action != 'add') {
  7662. if (
  7663. ($arrLP[$i]['item_type'] == 'dir') &&
  7664. !in_array($arrLP[$i]['id'], $arrHide) &&
  7665. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7666. ) {
  7667. $selectParent->addOption(
  7668. $arrLP[$i]['title'],
  7669. $arrLP[$i]['id'],
  7670. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px;']
  7671. );
  7672. if ($parent == $arrLP[$i]['id']) {
  7673. $selectParent->setSelected($arrLP[$i]['id']);
  7674. }
  7675. } else {
  7676. $arrHide[] = $arrLP[$i]['id'];
  7677. }
  7678. } else {
  7679. if ($arrLP[$i]['item_type'] == 'dir') {
  7680. $selectParent->addOption(
  7681. $arrLP[$i]['title'],
  7682. $arrLP[$i]['id'],
  7683. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7684. );
  7685. if ($parent_item_id == $arrLP[$i]['id']) {
  7686. $selectParent->setSelected($arrLP[$i]['id']);
  7687. }
  7688. }
  7689. }
  7690. }
  7691. if (is_array($arrLP)) {
  7692. reset($arrLP);
  7693. }
  7694. $selectPrevious = $form->addSelect(
  7695. 'previous',
  7696. get_lang('Position'),
  7697. [],
  7698. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7699. );
  7700. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7701. for ($i = 0; $i < count($arrLP); $i++) {
  7702. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7703. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7704. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7705. $selectPrevious->setSelected($arrLP[$i]['id']);
  7706. } elseif ($action == 'add') {
  7707. $selectPrevious->setSelected($arrLP[$i]['id']);
  7708. }
  7709. }
  7710. }
  7711. if ($action != 'move') {
  7712. $urlAttributes = ['class' => 'learnpath_item_form'];
  7713. if (is_numeric($extra_info)) {
  7714. $urlAttributes['disabled'] = 'disabled';
  7715. }
  7716. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7717. $defaults['url'] = $item_url;
  7718. $id_prerequisite = 0;
  7719. if (is_array($arrLP)) {
  7720. foreach ($arrLP as $key => $value) {
  7721. if ($value['id'] == $id) {
  7722. $id_prerequisite = $value['prerequisite'];
  7723. break;
  7724. }
  7725. }
  7726. }
  7727. $arrHide = array();
  7728. for ($i = 0; $i < count($arrLP); $i++) {
  7729. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7730. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7731. $s_selected_position = $arrLP[$i]['id'];
  7732. elseif ($action == 'add') $s_selected_position = 0;
  7733. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7734. }
  7735. }
  7736. }
  7737. if ($action == 'add') {
  7738. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7739. } else {
  7740. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7741. }
  7742. if ($action == 'move') {
  7743. $form->addHidden('title', $item_title);
  7744. $form->addHidden('description', $item_description);
  7745. }
  7746. if (is_numeric($extra_info)) {
  7747. $form->addHidden('path', $extra_info);
  7748. } elseif (is_array($extra_info)) {
  7749. $form->addHidden('path', $extra_info['path']);
  7750. }
  7751. $form->addHidden('type', TOOL_LINK);
  7752. $form->addHidden('post_time', time());
  7753. $form->setDefaults($defaults);
  7754. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  7755. }
  7756. /**
  7757. * Return HTML form to add/edit a student publication (work)
  7758. * @param string Action (add/edit)
  7759. * @param integer Item ID if already exists
  7760. * @param mixed Extra info (work ID if integer)
  7761. * @return string HTML form
  7762. */
  7763. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7764. {
  7765. $course_id = api_get_course_int_id();
  7766. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7767. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  7768. if ($id != 0 && is_array($extra_info)) {
  7769. $item_title = stripslashes($extra_info['title']);
  7770. $item_description = stripslashes($extra_info['description']);
  7771. } elseif (is_numeric($extra_info)) {
  7772. $extra_info = intval($extra_info);
  7773. $sql = "SELECT title, description
  7774. FROM $tbl_publication
  7775. WHERE c_id = $course_id AND id = ".$extra_info;
  7776. $result = Database::query($sql);
  7777. $row = Database::fetch_array($result);
  7778. $item_title = $row['title'];
  7779. } else {
  7780. $item_title = get_lang('Student_publication');
  7781. }
  7782. if ($id != 0 && is_array($extra_info)) {
  7783. $parent = $extra_info['parent_item_id'];
  7784. } else {
  7785. $parent = 0;
  7786. }
  7787. $sql = "SELECT * FROM $tbl_lp_item
  7788. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7789. $result = Database::query($sql);
  7790. $arrLP = array();
  7791. while ($row = Database::fetch_array($result)) {
  7792. $arrLP[] = array(
  7793. 'id' => $row['id'],
  7794. 'item_type' => $row['item_type'],
  7795. 'title' => $row['title'],
  7796. 'path' => $row['path'],
  7797. 'description' => $row['description'],
  7798. 'parent_item_id' => $row['parent_item_id'],
  7799. 'previous_item_id' => $row['previous_item_id'],
  7800. 'next_item_id' => $row['next_item_id'],
  7801. 'display_order' => $row['display_order'],
  7802. 'max_score' => $row['max_score'],
  7803. 'min_score' => $row['min_score'],
  7804. 'mastery_score' => $row['mastery_score'],
  7805. 'prerequisite' => $row['prerequisite']
  7806. );
  7807. }
  7808. $this->tree_array($arrLP);
  7809. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7810. unset($this->arrMenu);
  7811. $form = new FormValidator('frm_student_publication', 'post', '#');
  7812. if ($action == 'add') {
  7813. $form->addHeader(get_lang('Student_publication'));
  7814. } elseif ($action == 'move') {
  7815. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7816. } else {
  7817. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7818. }
  7819. if ($action != 'move') {
  7820. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7821. }
  7822. $parentSelect = $form->addSelect(
  7823. 'parent',
  7824. get_lang('Parent'),
  7825. ['0' => $this->name],
  7826. [
  7827. 'onchange' => 'javascript: load_cbo(this.value);',
  7828. 'class' => 'learnpath_item_form',
  7829. 'id' => 'idParent'
  7830. ]
  7831. );
  7832. $arrHide = array($id);
  7833. for ($i = 0; $i < count($arrLP); $i++) {
  7834. if ($action != 'add') {
  7835. if (
  7836. ($arrLP[$i]['item_type'] == 'dir') &&
  7837. !in_array($arrLP[$i]['id'], $arrHide) &&
  7838. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7839. ) {
  7840. $parentSelect->addOption(
  7841. $arrLP[$i]['title'],
  7842. $arrLP[$i]['id'],
  7843. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  7844. );
  7845. if ($parent == $arrLP[$i]['id']) {
  7846. $parentSelect->setSelected($arrLP[$i]['id']);
  7847. }
  7848. } else {
  7849. $arrHide[] = $arrLP[$i]['id'];
  7850. }
  7851. } else {
  7852. if ($arrLP[$i]['item_type'] == 'dir') {
  7853. $parentSelect->addOption(
  7854. $arrLP[$i]['title'],
  7855. $arrLP[$i]['id'],
  7856. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  7857. );
  7858. if ($parent == $arrLP[$i]['id']) {
  7859. $parentSelect->setSelected($arrLP[$i]['id']);
  7860. }
  7861. }
  7862. }
  7863. }
  7864. if (is_array($arrLP)) {
  7865. reset($arrLP);
  7866. }
  7867. $previousSelect = $form->addSelect(
  7868. 'previous',
  7869. get_lang('Position'),
  7870. ['0' => get_lang('FirstPosition')],
  7871. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7872. );
  7873. for ($i = 0; $i < count($arrLP); $i++) {
  7874. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7875. $previousSelect->addOption(
  7876. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  7877. $arrLP[$i]['id']
  7878. );
  7879. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7880. $previousSelect->setSelected($arrLP[$i]['id']);
  7881. } elseif ($action == 'add') {
  7882. $previousSelect->setSelected($arrLP[$i]['id']);
  7883. }
  7884. }
  7885. }
  7886. if ($action != 'move') {
  7887. $id_prerequisite = 0;
  7888. if (is_array($arrLP)) {
  7889. foreach ($arrLP as $key => $value) {
  7890. if ($value['id'] == $id) {
  7891. $id_prerequisite = $value['prerequisite'];
  7892. break;
  7893. }
  7894. }
  7895. }
  7896. $arrHide = array();
  7897. for ($i = 0; $i < count($arrLP); $i++) {
  7898. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7899. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7900. $s_selected_position = $arrLP[$i]['id'];
  7901. elseif ($action == 'add') $s_selected_position = 0;
  7902. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7903. }
  7904. }
  7905. }
  7906. if ($action == 'add') {
  7907. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7908. } else {
  7909. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7910. }
  7911. if ($action == 'move') {
  7912. $form->addHidden('title', $item_title);
  7913. $form->addHidden('description', $item_description);
  7914. }
  7915. if (is_numeric($extra_info)) {
  7916. $form->addHidden('path', $extra_info);
  7917. } elseif (is_array($extra_info)) {
  7918. $form->addHidden('path', $extra_info['path']);
  7919. }
  7920. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7921. $form->addHidden('post_time', time());
  7922. $form->setDefaults(['title' => $item_title]);
  7923. $return = '<div class="sectioncomment">';
  7924. $return .= $form->returnForm();
  7925. $return .= '</div>';
  7926. return $return;
  7927. }
  7928. /**
  7929. * Displays the menu for manipulating a step
  7930. *
  7931. * @param $item_id
  7932. * @param string $item_type
  7933. * @return string
  7934. */
  7935. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7936. {
  7937. $_course = api_get_course_info();
  7938. $course_id = api_get_course_int_id();
  7939. $course_code = api_get_course_id();
  7940. $return = '<div class="actions">';
  7941. switch ($item_type) {
  7942. case 'dir':
  7943. // Commented the message cause should not show it.
  7944. //$lang = get_lang('TitleManipulateChapter');
  7945. break;
  7946. case TOOL_LP_FINAL_ITEM:
  7947. case TOOL_DOCUMENT:
  7948. // Commented the message cause should not show it.
  7949. //$lang = get_lang('TitleManipulateDocument');
  7950. break;
  7951. case TOOL_LINK:
  7952. case 'link' :
  7953. // Commented the message cause should not show it.
  7954. //$lang = get_lang('TitleManipulateLink');
  7955. break;
  7956. case TOOL_QUIZ:
  7957. // Commented the message cause should not show it.
  7958. //$lang = get_lang('TitleManipulateQuiz');
  7959. break;
  7960. case TOOL_STUDENTPUBLICATION:
  7961. // Commented the message cause should not show it.
  7962. //$lang = get_lang('TitleManipulateStudentPublication');
  7963. break;
  7964. }
  7965. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7966. $item_id = intval($item_id);
  7967. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  7968. WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7969. $result = Database::query($sql);
  7970. $row = Database::fetch_assoc($result);
  7971. $audio_player = null;
  7972. // We display an audio player if needed.
  7973. if (!empty($row['audio'])) {
  7974. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7975. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7976. </div>';
  7977. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7978. $audio_player .= '<script>
  7979. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7980. s1.addParam("allowscriptaccess","always");
  7981. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7982. s1.write("container");
  7983. </script>';
  7984. }
  7985. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7986. if ($item_type != TOOL_LP_FINAL_ITEM) {
  7987. $return .= Display::url(
  7988. Display::return_icon(
  7989. 'edit.png',
  7990. get_lang('Edit'),
  7991. array(),
  7992. ICON_SIZE_SMALL
  7993. ),
  7994. $url.'&action=edit_item&path_item='.$row['path']
  7995. );
  7996. $return .= Display::url(
  7997. Display::return_icon(
  7998. 'move.png',
  7999. get_lang('Move'),
  8000. array(),
  8001. ICON_SIZE_SMALL
  8002. ),
  8003. $url.'&action=move_item'
  8004. );
  8005. }
  8006. // Commented for now as prerequisites cannot be added to chapters.
  8007. if ($item_type != 'dir') {
  8008. $return .= Display::url(
  8009. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  8010. $url.'&action=edit_item_prereq'
  8011. );
  8012. }
  8013. $return .= Display::url(
  8014. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  8015. $url.'&action=delete_item'
  8016. );
  8017. if ($item_type == TOOL_HOTPOTATOES) {
  8018. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  8019. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  8020. }
  8021. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  8022. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  8023. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  8024. }
  8025. $return .= '</div>';
  8026. if (!empty($audio_player)) {
  8027. $return .= '<br />'.$audio_player;
  8028. }
  8029. return $return;
  8030. }
  8031. /**
  8032. * Creates the javascript needed for filling up the checkboxes without page reload
  8033. * @return string
  8034. */
  8035. public function get_js_dropdown_array()
  8036. {
  8037. $course_id = api_get_course_int_id();
  8038. $return = 'var child_name = new Array();'."\n";
  8039. $return .= 'var child_value = new Array();'."\n\n";
  8040. $return .= 'child_name[0] = new Array();'."\n";
  8041. $return .= 'child_value[0] = new Array();'."\n\n";
  8042. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8043. $sql = "SELECT * FROM ".$tbl_lp_item."
  8044. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  8045. ORDER BY display_order ASC";
  8046. $res_zero = Database::query($sql);
  8047. $i = 0;
  8048. while ($row_zero = Database::fetch_array($res_zero)) {
  8049. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  8050. if ($row_zero['item_type'] == TOOL_QUIZ) {
  8051. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  8052. }
  8053. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  8054. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  8055. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  8056. }
  8057. }
  8058. $return .= "\n";
  8059. $sql = "SELECT * FROM ".$tbl_lp_item."
  8060. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  8061. $res = Database::query($sql);
  8062. while ($row = Database::fetch_array($res)) {
  8063. $sql_parent = "SELECT * FROM ".$tbl_lp_item."
  8064. WHERE
  8065. c_id = ".$course_id." AND
  8066. parent_item_id = " . $row['id']."
  8067. ORDER BY display_order ASC";
  8068. $res_parent = Database::query($sql_parent);
  8069. $i = 0;
  8070. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  8071. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  8072. while ($row_parent = Database::fetch_array($res_parent)) {
  8073. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  8074. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  8075. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  8076. }
  8077. $return .= "\n";
  8078. }
  8079. return $return;
  8080. }
  8081. /**
  8082. * Display the form to allow moving an item
  8083. * @param integer $item_id Item ID
  8084. * @return string HTML form
  8085. */
  8086. public function display_move_item($item_id)
  8087. {
  8088. $course_id = api_get_course_int_id();
  8089. $return = '';
  8090. if (is_numeric($item_id)) {
  8091. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8092. $sql = "SELECT * FROM ".$tbl_lp_item."
  8093. WHERE c_id = ".$course_id." AND id = ".$item_id;
  8094. $res = Database::query($sql);
  8095. $row = Database::fetch_array($res);
  8096. switch ($row['item_type']) {
  8097. case 'dir':
  8098. case 'asset':
  8099. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8100. $return .= $this->display_item_form(
  8101. $row['item_type'],
  8102. get_lang('MoveCurrentChapter'),
  8103. 'move',
  8104. $item_id,
  8105. $row
  8106. );
  8107. break;
  8108. case TOOL_DOCUMENT:
  8109. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8110. $return .= $this->display_document_form('move', $item_id, $row);
  8111. break;
  8112. case TOOL_LINK:
  8113. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8114. $return .= $this->display_link_form('move', $item_id, $row);
  8115. break;
  8116. case TOOL_HOTPOTATOES:
  8117. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8118. $return .= $this->display_link_form('move', $item_id, $row);
  8119. break;
  8120. case TOOL_QUIZ:
  8121. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8122. $return .= $this->display_quiz_form('move', $item_id, $row);
  8123. break;
  8124. case TOOL_STUDENTPUBLICATION:
  8125. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8126. $return .= $this->display_student_publication_form('move', $item_id, $row);
  8127. break;
  8128. case TOOL_FORUM:
  8129. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8130. $return .= $this->display_forum_form('move', $item_id, $row);
  8131. break;
  8132. case TOOL_THREAD:
  8133. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8134. $return .= $this->display_forum_form('move', $item_id, $row);
  8135. break;
  8136. }
  8137. }
  8138. return $return;
  8139. }
  8140. /**
  8141. * Displays a basic form on the overview page for changing the item title and the item description.
  8142. * @param string $item_type
  8143. * @param string $title
  8144. * @param array $data
  8145. * @return string
  8146. */
  8147. public function display_item_small_form($item_type, $title = '', $data = array())
  8148. {
  8149. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  8150. $form = new FormValidator('small_form', 'post', $url);
  8151. $form->addElement('header', $title);
  8152. $form->addElement('text', 'title', get_lang('Title'));
  8153. $form->addButtonSave(get_lang('Save'), 'submit_button');
  8154. $form->addElement('hidden', 'id', $data['id']);
  8155. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  8156. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  8157. $form->setDefaults(array('title' => $data['title']));
  8158. return $form->toHtml();
  8159. }
  8160. /**
  8161. * Return HTML form to allow prerequisites selection
  8162. * @todo use FormValidator
  8163. * @param integer Item ID
  8164. * @return string HTML form
  8165. */
  8166. public function display_item_prerequisites_form($item_id)
  8167. {
  8168. $course_id = api_get_course_int_id();
  8169. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8170. $item_id = intval($item_id);
  8171. /* Current prerequisite */
  8172. $sql = "SELECT * FROM $tbl_lp_item
  8173. WHERE c_id = $course_id AND id = ".$item_id;
  8174. $result = Database::query($sql);
  8175. $row = Database::fetch_array($result);
  8176. $prerequisiteId = $row['prerequisite'];
  8177. $return = '<legend>';
  8178. $return .= get_lang('AddEditPrerequisites');
  8179. $return .= '</legend>';
  8180. $return .= '<form method="POST">';
  8181. $return .= '<div class="table-responsive">';
  8182. $return .= '<table class="table table-hover">';
  8183. $return .= '<tr>';
  8184. $return .= '<th>'.get_lang('LearnpathPrerequisites').'</th>';
  8185. $return .= '<th width="140" >'.get_lang('Minimum').'</th>';
  8186. $return .= '<th width="140">'.get_lang('Maximum').'</th>';
  8187. $return .= '</tr>';
  8188. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8189. $return .= '<tr>';
  8190. $return .= '<td colspan="3">';
  8191. $return .= '<div class="radio learnpath"><label for="idNone">';
  8192. $return .= '<input checked="checked" id="idNone" name="prerequisites" type="radio" />';
  8193. $return .= get_lang('None').'</label>';
  8194. $return .= '</div>';
  8195. $return .= '</tr>';
  8196. $sql = "SELECT * FROM $tbl_lp_item
  8197. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8198. $result = Database::query($sql);
  8199. $arrLP = array();
  8200. $selectedMinScore = array();
  8201. $selectedMaxScore = array();
  8202. while ($row = Database::fetch_array($result)) {
  8203. if ($row['id'] == $item_id) {
  8204. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8205. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8206. }
  8207. $arrLP[] = array(
  8208. 'id' => $row['id'],
  8209. 'item_type' => $row['item_type'],
  8210. 'title' => $row['title'],
  8211. 'ref' => $row['ref'],
  8212. 'description' => $row['description'],
  8213. 'parent_item_id' => $row['parent_item_id'],
  8214. 'previous_item_id' => $row['previous_item_id'],
  8215. 'next_item_id' => $row['next_item_id'],
  8216. 'max_score' => $row['max_score'],
  8217. 'min_score' => $row['min_score'],
  8218. 'mastery_score' => $row['mastery_score'],
  8219. 'prerequisite' => $row['prerequisite'],
  8220. 'next_item_id' => $row['next_item_id'],
  8221. 'display_order' => $row['display_order'],
  8222. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  8223. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  8224. );
  8225. }
  8226. $this->tree_array($arrLP);
  8227. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  8228. unset($this->arrMenu);
  8229. for ($i = 0; $i < count($arrLP); $i++) {
  8230. $item = $arrLP[$i];
  8231. if ($item['id'] == $item_id) {
  8232. break;
  8233. }
  8234. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8235. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']] : 0;
  8236. $return .= '<tr>';
  8237. $return .= '<td '.(($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8238. $return .= '<div style="margin-left:'.($item['depth'] * 20).'px;" class="radio learnpath">';
  8239. $return .= '<label for="id'.$item['id'].'">';
  8240. $return .= '<input'.(in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '').($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ').'id="id'.$item['id'].'" name="prerequisites" type="radio" value="'.$item['id'].'" />';
  8241. $icon_name = str_replace(' ', '', $item['item_type']);
  8242. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8243. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8244. } else {
  8245. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8246. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8247. } else {
  8248. $return .= Display::return_icon('folder_document.png');
  8249. }
  8250. }
  8251. $return .= $item['title'].'</label>';
  8252. $return .= '</div>';
  8253. $return .= '</td>';
  8254. if ($item['item_type'] == TOOL_QUIZ) {
  8255. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8256. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  8257. $tmp_obj_exercice = new Exercise();
  8258. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  8259. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  8260. $tmp_obj_lp_item->update_in_bdd();
  8261. $item['max_score'] = $tmp_obj_lp_item->max_score;
  8262. $return .= '<td>';
  8263. $return .= '<input class="form-control" size="4" maxlength="3" name="min_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMinScoreValue.'" />';
  8264. $return .= '</td>';
  8265. $return .= '<td>';
  8266. $return .= '<input class="form-control" size="4" maxlength="3" name="max_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue.'" />';
  8267. $return .= '</td>';
  8268. }
  8269. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8270. $return .= '<td>';
  8271. $return .= '<center><input size="4" maxlength="3" name="min_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMinScoreValue.'" /></center>';
  8272. $return .= '</td>';
  8273. $return .= '<td>';
  8274. $return .= '<center><input size="4" maxlength="3" name="max_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue.'" /></center>';
  8275. $return .= '</td>';
  8276. }
  8277. $return .= '</tr>';
  8278. }
  8279. $return .= '<tr>';
  8280. $return .= '</tr>';
  8281. $return .= '</table>';
  8282. $return .= '</div>';
  8283. $return .= '<div class="form-group">';
  8284. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">'.get_lang('ModifyPrerequisites').'</button>';
  8285. $return .= '</form>';
  8286. return $return;
  8287. }
  8288. /**
  8289. * Return HTML list to allow prerequisites selection for lp
  8290. * @param integer Item ID
  8291. * @return string HTML form
  8292. */
  8293. public function display_lp_prerequisites_list()
  8294. {
  8295. $course_id = api_get_course_int_id();
  8296. $lp_id = $this->lp_id;
  8297. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8298. // get current prerequisite
  8299. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8300. $result = Database::query($sql);
  8301. $row = Database::fetch_array($result);
  8302. $prerequisiteId = $row['prerequisite'];
  8303. $session_id = api_get_session_id();
  8304. $session_condition = api_get_session_condition($session_id, true, true);
  8305. $sql = "SELECT * FROM $tbl_lp
  8306. WHERE c_id = $course_id $session_condition
  8307. ORDER BY display_order ";
  8308. $rs = Database::query($sql);
  8309. $return = '';
  8310. $return .= '<select name="prerequisites" class="form-control">';
  8311. $return .= '<option value="0">'.get_lang('None').'</option>';
  8312. if (Database::num_rows($rs) > 0) {
  8313. while ($row = Database::fetch_array($rs)) {
  8314. if ($row['id'] == $lp_id) {
  8315. continue;
  8316. }
  8317. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $prerequisiteId) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8318. }
  8319. }
  8320. $return .= '</select>';
  8321. return $return;
  8322. }
  8323. /**
  8324. * Creates a list with all the documents in it
  8325. * @param bool $showInvisibleFiles
  8326. * @return string
  8327. */
  8328. public function get_documents($showInvisibleFiles = false)
  8329. {
  8330. $course_info = api_get_course_info();
  8331. $sessionId = api_get_session_id();
  8332. $documentTree = DocumentManager::get_document_preview(
  8333. $course_info,
  8334. $this->lp_id,
  8335. null,
  8336. $sessionId,
  8337. true,
  8338. null,
  8339. null,
  8340. $showInvisibleFiles,
  8341. true
  8342. );
  8343. $headers = array(
  8344. get_lang('Files'),
  8345. get_lang('CreateTheDocument'),
  8346. get_lang('Upload')
  8347. );
  8348. $form = new FormValidator(
  8349. 'form_upload',
  8350. 'POST',
  8351. $this->getCurrentBuildingModeURL(),
  8352. '',
  8353. array('enctype' => 'multipart/form-data')
  8354. );
  8355. $folders = DocumentManager::get_all_document_folders(
  8356. api_get_course_info(),
  8357. 0,
  8358. true
  8359. );
  8360. DocumentManager::build_directory_selector(
  8361. $folders,
  8362. '',
  8363. array(),
  8364. true,
  8365. $form,
  8366. 'directory_parent_id'
  8367. );
  8368. $group = array(
  8369. $form->createElement(
  8370. 'radio',
  8371. 'if_exists',
  8372. get_lang("UplWhatIfFileExists"),
  8373. get_lang('UplDoNothing'),
  8374. 'nothing'
  8375. ),
  8376. $form->createElement(
  8377. 'radio',
  8378. 'if_exists',
  8379. null,
  8380. get_lang('UplOverwriteLong'),
  8381. 'overwrite'
  8382. ),
  8383. $form->createElement(
  8384. 'radio',
  8385. 'if_exists',
  8386. null,
  8387. get_lang('UplRenameLong'),
  8388. 'rename'
  8389. )
  8390. );
  8391. $form->addGroup($group, null, get_lang('UplWhatIfFileExists'));
  8392. /*$form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8393. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8394. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');*/
  8395. $form->setDefaults(['if_exists' => 'rename']);
  8396. // Check box options
  8397. $form->addElement(
  8398. 'checkbox',
  8399. 'unzip',
  8400. get_lang('Options'),
  8401. get_lang('Uncompress')
  8402. );
  8403. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8404. $form->addMultipleUpload($url);
  8405. $new = $this->display_document_form('add', 0);
  8406. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8407. return $tabs;
  8408. }
  8409. /**
  8410. * Creates a list with all the exercises (quiz) in it
  8411. * @return string
  8412. */
  8413. public function get_exercises()
  8414. {
  8415. $course_id = api_get_course_int_id();
  8416. $session_id = api_get_session_id();
  8417. $userInfo = api_get_user_info();
  8418. // New for hotpotatoes.
  8419. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8420. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  8421. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  8422. $condition_session = api_get_session_condition($session_id, true, true);
  8423. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8424. $activeCondition = ' active <> -1 ';
  8425. if ($setting) {
  8426. $activeCondition = ' active = 1 ';
  8427. }
  8428. $sql_quiz = "SELECT * FROM $tbl_quiz
  8429. WHERE c_id = $course_id AND $activeCondition $condition_session
  8430. ORDER BY title ASC";
  8431. $sql_hot = "SELECT * FROM $tbl_doc
  8432. WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session
  8433. ORDER BY id ASC";
  8434. $res_quiz = Database::query($sql_quiz);
  8435. $res_hot = Database::query($sql_hot);
  8436. $return = '<ul class="lp_resource">';
  8437. $return .= '<li class="lp_resource_element">';
  8438. $return .= Display::return_icon('new_exercice.png');
  8439. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id='.$this->lp_id.'">'.
  8440. get_lang('NewExercise').'</a>';
  8441. $return .= '</li>';
  8442. $previewIcon = Display::return_icon('preview_view.png', get_lang('Preview'));
  8443. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  8444. // Display hotpotatoes
  8445. while ($row_hot = Database::fetch_array($res_hot)) {
  8446. $link = Display::url(
  8447. $previewIcon,
  8448. $exerciseUrl.'&file='.$row_hot['path'],
  8449. ['target' => '_blank']
  8450. );
  8451. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8452. $return .= '<a class="moved" href="#">';
  8453. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8454. $return .= '</a> ';
  8455. $return .= Display::return_icon('hotpotatoes_s.png');
  8456. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_HOTPOTATOES.'&file='.$row_hot['id'].'&lp_id='.$this->lp_id.'">'.
  8457. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])).$link.'</a>';
  8458. $return .= '</li>';
  8459. }
  8460. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  8461. while ($row_quiz = Database::fetch_array($res_quiz)) {
  8462. $title = strip_tags(
  8463. api_html_entity_decode($row_quiz['title'])
  8464. );
  8465. $link = Display::url(
  8466. $previewIcon,
  8467. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  8468. ['target' => '_blank']
  8469. );
  8470. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$title.'" >';
  8471. $return .= '<a class="moved" href="#">';
  8472. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8473. $return .= '</a> ';
  8474. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8475. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  8476. $return .= '<a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_QUIZ.'&file='.$row_quiz['id'].'&lp_id='.$this->lp_id.'">'.
  8477. Security::remove_XSS(cut($title, 80)).$link.$sessionStar.
  8478. '</a>';
  8479. $return .= '</li>';
  8480. }
  8481. $return .= '</ul>';
  8482. return $return;
  8483. }
  8484. /**
  8485. * Creates a list with all the links in it
  8486. * @return string
  8487. */
  8488. public function get_links()
  8489. {
  8490. $selfUrl = api_get_self();
  8491. $courseIdReq = api_get_cidreq();
  8492. $course = api_get_course_info();
  8493. $userInfo = api_get_user_info();
  8494. $course_id = $course['real_id'];
  8495. $tbl_link = Database::get_course_table(TABLE_LINK);
  8496. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8497. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8498. $session_id = api_get_session_id();
  8499. $condition_session = api_get_session_condition(
  8500. $session_id,
  8501. true,
  8502. true,
  8503. "link.session_id"
  8504. );
  8505. $sql = "SELECT
  8506. link.id as link_id,
  8507. link.title as link_title,
  8508. link.session_id as link_session_id,
  8509. link.category_id as category_id,
  8510. link_category.category_title as category_title
  8511. FROM $tbl_link as link
  8512. LEFT JOIN $linkCategoryTable as link_category
  8513. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8514. WHERE link.c_id = ".$course_id." $condition_session
  8515. ORDER BY link_category.category_title ASC, link.title ASC";
  8516. $result = Database::query($sql);
  8517. $categorizedLinks = array();
  8518. $categories = array();
  8519. while ($link = Database::fetch_array($result)) {
  8520. if (!$link['category_id']) {
  8521. $link['category_title'] = get_lang('Uncategorized');
  8522. }
  8523. $categories[$link['category_id']] = $link['category_title'];
  8524. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  8525. }
  8526. $linksHtmlCode =
  8527. '<script>
  8528. function toggle_tool(tool, id){
  8529. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8530. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8531. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8532. } else {
  8533. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8534. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8535. }
  8536. }
  8537. </script>
  8538. <ul class="lp_resource">
  8539. <li class="lp_resource_element">
  8540. '.Display::return_icon('linksnew.gif').'
  8541. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8542. get_lang('LinkAdd').'
  8543. </a>
  8544. </li>';
  8545. foreach ($categorizedLinks as $categoryId => $links) {
  8546. $linkNodes = null;
  8547. foreach ($links as $key => $linkInfo) {
  8548. $title = $linkInfo['link_title'];
  8549. $linkSessionId = $linkInfo['link_session_id'];
  8550. $link = Display::url(
  8551. Display::return_icon('preview_view.png', get_lang('Preview')),
  8552. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8553. ['target' => '_blank']
  8554. );
  8555. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8556. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8557. $linkNodes .=
  8558. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8559. <a class="moved" href="#">'.
  8560. $moveEverywhereIcon.
  8561. '</a>
  8562. '.Display::return_icon('lp_link.png').'
  8563. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8564. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8565. Security::remove_XSS($title).$sessionStar.$link.
  8566. '</a>
  8567. </li>';
  8568. }
  8569. }
  8570. $linksHtmlCode .=
  8571. '<li>
  8572. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8573. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8574. align="absbottom" />
  8575. </a>
  8576. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8577. </li>
  8578. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8579. }
  8580. $linksHtmlCode .= '</ul>';
  8581. return $linksHtmlCode;
  8582. }
  8583. /**
  8584. * Creates a list with all the student publications in it
  8585. * @return string
  8586. */
  8587. public function get_student_publications()
  8588. {
  8589. $return = '<ul class="lp_resource">';
  8590. $return .= '<li class="lp_resource_element">';
  8591. $return .= Display::return_icon('works_new.gif');
  8592. $return .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_STUDENTPUBLICATION.'&lp_id='.$this->lp_id.'">'.
  8593. get_lang('AddAssignmentPage').'</a>';
  8594. $return .= '</li>';
  8595. $sessionId = api_get_session_id();
  8596. if (empty($sessionId)) {
  8597. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8598. $works = getWorkListTeacher(0, 100, null, null, null);
  8599. if (!empty($works)) {
  8600. foreach ($works as $work) {
  8601. $link = Display::url(
  8602. Display::return_icon('preview_view.png', get_lang('Preview')),
  8603. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8604. ['target' => '_blank']
  8605. );
  8606. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8607. $return .= '<a class="moved" href="#">';
  8608. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8609. $return .= '</a> ';
  8610. $return .= Display::return_icon('works.gif');
  8611. $return .= ' <a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_STUDENTPUBLICATION.'&file='.$work['iid'].'&lp_id='.$this->lp_id.'">'.
  8612. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8613. </a>';
  8614. $return .= '</li>';
  8615. }
  8616. }
  8617. }
  8618. $return .= '</ul>';
  8619. return $return;
  8620. }
  8621. /**
  8622. * Creates a list with all the forums in it
  8623. * @return string
  8624. */
  8625. public function get_forums()
  8626. {
  8627. require_once '../forum/forumfunction.inc.php';
  8628. require_once '../forum/forumconfig.inc.php';
  8629. $forumCategories = get_forum_categories();
  8630. $forumsInNoCategory = get_forums_in_category(0);
  8631. if (!empty($forumsInNoCategory)) {
  8632. $forumCategories = array_merge(
  8633. $forumCategories,
  8634. array(
  8635. array(
  8636. 'cat_id' => 0,
  8637. 'session_id' => 0,
  8638. 'visibility' => 1,
  8639. 'cat_comment' => null,
  8640. ),
  8641. )
  8642. );
  8643. }
  8644. $forumList = get_forums();
  8645. $a_forums = [];
  8646. foreach ($forumCategories as $forumCategory) {
  8647. // The forums in this category.
  8648. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8649. if (!empty($forumsInCategory)) {
  8650. foreach ($forumList as $forum) {
  8651. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8652. $a_forums[] = $forum;
  8653. }
  8654. }
  8655. }
  8656. }
  8657. $return = '<ul class="lp_resource">';
  8658. // First add link
  8659. $return .= '<li class="lp_resource_element">';
  8660. $return .= Display::return_icon('new_forum.png');
  8661. $return .= Display::url(
  8662. get_lang('CreateANewForum'),
  8663. api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&'.http_build_query([
  8664. 'action' => 'add',
  8665. 'content' => 'forum',
  8666. 'lp_id' => $this->lp_id
  8667. ]),
  8668. ['title' => get_lang('CreateANewForum')]
  8669. );
  8670. $return .= '</li>';
  8671. $return .= '<script>
  8672. function toggle_forum(forum_id) {
  8673. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8674. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8675. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8676. } else {
  8677. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8678. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8679. }
  8680. }
  8681. </script>';
  8682. foreach ($a_forums as $forum) {
  8683. if (!empty($forum['forum_id'])) {
  8684. $link = Display::url(
  8685. Display::return_icon('preview_view.png', get_lang('Preview')),
  8686. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8687. ['target' => '_blank']
  8688. );
  8689. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8690. $return .= '<a class="moved" href="#">';
  8691. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8692. $return .= ' </a>';
  8693. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8694. $return .= '<a onclick="javascript:toggle_forum('.$forum['forum_id'].');" style="cursor:hand; vertical-align:middle">
  8695. <img src="' . Display::returnIconPath('add.gif').'" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8696. </a>
  8697. <a class="moved" 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">'.
  8698. Security::remove_XSS($forum['forum_title']).' '.$link.'</a>';
  8699. $return .= '</li>';
  8700. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8701. $a_threads = get_threads($forum['forum_id']);
  8702. if (is_array($a_threads)) {
  8703. foreach ($a_threads as $thread) {
  8704. $link = Display::url(
  8705. Display::return_icon('preview_view.png', get_lang('Preview')),
  8706. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8707. ['target' => '_blank']
  8708. );
  8709. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8710. $return .= '&nbsp;<a class="moved" href="#">';
  8711. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8712. $return .= ' </a>';
  8713. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8714. $return .= '<a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_THREAD.'&thread_id='.$thread['thread_id'].'&lp_id='.$this->lp_id.'">'.
  8715. Security::remove_XSS($thread['thread_title']).' '.$link.'</a>';
  8716. $return .= '</li>';
  8717. }
  8718. }
  8719. $return .= '</div>';
  8720. }
  8721. }
  8722. $return .= '</ul>';
  8723. return $return;
  8724. }
  8725. /**
  8726. * // TODO: The output encoding should be equal to the system encoding.
  8727. *
  8728. * Exports the learning path as a SCORM package. This is the main function that
  8729. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8730. * whole thing and returns the zip.
  8731. *
  8732. * This method needs to be called in PHP5, as it will fail with non-adequate
  8733. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8734. * you need to call it on a learnpath object.
  8735. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8736. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8737. * path has been modified, it should use the generic method here below.
  8738. * @TODO link this function with the export_lp() function in the same class
  8739. * @param string Optional name of zip file. If none, title of learnpath is
  8740. * domesticated and trailed with ".zip"
  8741. * @return string Returns the zip package string, or null if error
  8742. */
  8743. public function scorm_export()
  8744. {
  8745. $_course = api_get_course_info();
  8746. $course_id = $_course['real_id'];
  8747. // Remove memory and time limits as much as possible as this might be a long process...
  8748. if (function_exists('ini_set')) {
  8749. api_set_memory_limit('256M');
  8750. ini_set('max_execution_time', 600);
  8751. }
  8752. // Create the zip handler (this will remain available throughout the method).
  8753. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8754. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8755. $temp_dir_short = uniqid();
  8756. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8757. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8758. $zip_folder = new PclZip($temp_zip_file);
  8759. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8760. $root_path = $main_path = api_get_path(SYS_PATH);
  8761. $files_cleanup = array();
  8762. // Place to temporarily stash the zip file.
  8763. // create the temp dir if it doesn't exist
  8764. // or do a cleanup before creating the zip file.
  8765. if (!is_dir($temp_zip_dir)) {
  8766. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8767. } else {
  8768. // Cleanup: Check the temp dir for old files and delete them.
  8769. $handle = opendir($temp_zip_dir);
  8770. while (false !== ($file = readdir($handle))) {
  8771. if ($file != '.' && $file != '..') {
  8772. unlink("$temp_zip_dir/$file");
  8773. }
  8774. }
  8775. closedir($handle);
  8776. }
  8777. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8778. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8779. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8780. ) {
  8781. // Remove the possible . at the end of the path.
  8782. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8783. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8784. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8785. copyr(
  8786. $current_course_path.'/scorm/'.$this->path,
  8787. $dest_path_to_scorm_folder,
  8788. array('imsmanifest'),
  8789. $zip_files
  8790. );
  8791. }
  8792. // Build a dummy imsmanifest structure.
  8793. // Do not add to the zip yet (we still need it).
  8794. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8795. // Aggregation Model official document, section "2.3 Content Packaging".
  8796. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8797. $xmldoc = new DOMDocument('1.0');
  8798. $root = $xmldoc->createElement('manifest');
  8799. $root->setAttribute('identifier', 'SingleCourseManifest');
  8800. $root->setAttribute('version', '1.1');
  8801. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8802. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8803. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8804. $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');
  8805. // Build mandatory sub-root container elements.
  8806. $metadata = $xmldoc->createElement('metadata');
  8807. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8808. $metadata->appendChild($md_schema);
  8809. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8810. $metadata->appendChild($md_schemaversion);
  8811. $root->appendChild($metadata);
  8812. $organizations = $xmldoc->createElement('organizations');
  8813. $resources = $xmldoc->createElement('resources');
  8814. // Build the only organization we will use in building our learnpaths.
  8815. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8816. $organization = $xmldoc->createElement('organization');
  8817. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8818. // To set the title of the SCORM entity (=organization), we take the name given
  8819. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8820. // learning path charset) as it is the encoding that defines how it is stored
  8821. // in the database. Then we convert it to HTML entities again as the "&" character
  8822. // alone is not authorized in XML (must be &amp;).
  8823. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8824. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8825. $organization->appendChild($org_title);
  8826. $folder_name = 'document';
  8827. // Removes the learning_path/scorm_folder path when exporting see #4841
  8828. $path_to_remove = null;
  8829. $result = $this->generate_lp_folder($_course);
  8830. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8831. $path_to_remove = 'document'.$result['dir'];
  8832. $path_to_replace = $folder_name.'/';
  8833. }
  8834. // Fixes chamilo scorm exports
  8835. if ($this->ref === 'chamilo_scorm_export') {
  8836. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8837. }
  8838. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8839. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8840. $link_updates = array();
  8841. $links_to_create = array();
  8842. //foreach ($this->items as $index => $item) {
  8843. foreach ($this->ordered_items as $index => $itemId) {
  8844. $item = $this->items[$itemId];
  8845. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8846. // Get included documents from this item.
  8847. if ($item->type === 'sco') {
  8848. $inc_docs = $item->get_resources_from_source(
  8849. null,
  8850. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8851. );
  8852. } else {
  8853. $inc_docs = $item->get_resources_from_source();
  8854. }
  8855. // Give a child element <item> to the <organization> element.
  8856. $my_item_id = $item->get_id();
  8857. $my_item = $xmldoc->createElement('item');
  8858. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8859. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8860. $my_item->setAttribute('isvisible', 'true');
  8861. // Give a child element <title> to the <item> element.
  8862. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8863. $my_item->appendChild($my_title);
  8864. // Give a child element <adlcp:prerequisites> to the <item> element.
  8865. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8866. $my_prereqs->setAttribute('type', 'aicc_script');
  8867. $my_item->appendChild($my_prereqs);
  8868. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8869. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8870. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8871. //$xmldoc->createElement('adlcp:timelimitaction','');
  8872. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8873. //$xmldoc->createElement('adlcp:datafromlms','');
  8874. // Give a child element <adlcp:masteryscore> to the <item> element.
  8875. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8876. $my_item->appendChild($my_masteryscore);
  8877. // Attach this item to the organization element or hits parent if there is one.
  8878. if (!empty($item->parent) && $item->parent != 0) {
  8879. $children = $organization->childNodes;
  8880. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8881. if (is_object($possible_parent)) {
  8882. $possible_parent->appendChild($my_item);
  8883. } else {
  8884. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8885. }
  8886. } else {
  8887. if ($this->debug > 0) { error_log('No parent'); }
  8888. $organization->appendChild($my_item);
  8889. }
  8890. // Get the path of the file(s) from the course directory root.
  8891. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8892. if (!empty($path_to_remove)) {
  8893. //From docs
  8894. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8895. //From quiz
  8896. if ($this->ref === 'chamilo_scorm_export') {
  8897. $path_to_remove = 'scorm/'.$this->path.'/';
  8898. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8899. }
  8900. } else {
  8901. $my_xml_file_path = $my_file_path;
  8902. }
  8903. $my_sub_dir = dirname($my_file_path);
  8904. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8905. $my_xml_sub_dir = $my_sub_dir;
  8906. // Give a <resource> child to the <resources> element
  8907. $my_resource = $xmldoc->createElement('resource');
  8908. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8909. $my_resource->setAttribute('type', 'webcontent');
  8910. $my_resource->setAttribute('href', $my_xml_file_path);
  8911. // adlcp:scormtype can be either 'sco' or 'asset'.
  8912. if ($item->type === 'sco') {
  8913. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8914. } else {
  8915. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8916. }
  8917. // xml:base is the base directory to find the files declared in this resource.
  8918. $my_resource->setAttribute('xml:base', '');
  8919. // Give a <file> child to the <resource> element.
  8920. $my_file = $xmldoc->createElement('file');
  8921. $my_file->setAttribute('href', $my_xml_file_path);
  8922. $my_resource->appendChild($my_file);
  8923. // Dependency to other files - not yet supported.
  8924. $i = 1;
  8925. if ($inc_docs)
  8926. foreach ($inc_docs as $doc_info) {
  8927. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8928. continue;
  8929. }
  8930. $my_dep = $xmldoc->createElement('resource');
  8931. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8932. $my_dep->setAttribute('identifier', $res_id);
  8933. $my_dep->setAttribute('type', 'webcontent');
  8934. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8935. $my_dep_file = $xmldoc->createElement('file');
  8936. // Check type of URL.
  8937. if ($doc_info[1] == 'remote') {
  8938. // Remote file. Save url as is.
  8939. $my_dep_file->setAttribute('href', $doc_info[0]);
  8940. $my_dep->setAttribute('xml:base', '');
  8941. } elseif ($doc_info[1] === 'local') {
  8942. switch ($doc_info[2]) {
  8943. case 'url': // Local URL - save path as url for now, don't zip file.
  8944. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8945. $current_dir = dirname($abs_path);
  8946. $current_dir = str_replace('\\', '/', $current_dir);
  8947. $file_path = realpath($abs_path);
  8948. $file_path = str_replace('\\', '/', $file_path);
  8949. $my_dep_file->setAttribute('href', $file_path);
  8950. $my_dep->setAttribute('xml:base', '');
  8951. if (strstr($file_path, $main_path) !== false) {
  8952. // The calculated real path is really inside Chamilo's root path.
  8953. // Reduce file path to what's under the DocumentRoot.
  8954. $file_path = substr($file_path, strlen($root_path) - 1);
  8955. //echo $file_path;echo '<br /><br />';
  8956. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8957. $zip_files_abs[] = $file_path;
  8958. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8959. $my_dep_file->setAttribute('href', $file_path);
  8960. $my_dep->setAttribute('xml:base', '');
  8961. } elseif (empty($file_path)) {
  8962. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8963. if (strpos($document_root, -1) == '/') {
  8964. $document_root = substr(0, -1, $document_root);
  8965. }*/
  8966. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8967. $file_path = str_replace('//', '/', $file_path);
  8968. if (file_exists($file_path)) {
  8969. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8970. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8971. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8972. $my_dep_file->setAttribute('href', $file_path);
  8973. $my_dep->setAttribute('xml:base', '');
  8974. }
  8975. }
  8976. break;
  8977. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8978. $my_dep_file->setAttribute('href', $doc_info[0]);
  8979. $my_dep->setAttribute('xml:base', '');
  8980. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8981. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8982. $abs_img_path_without_subdir = $doc_info[0];
  8983. $relp = api_get_path(REL_PATH); // The url-append config param.
  8984. $pos = strpos($abs_img_path_without_subdir, $relp);
  8985. if ($pos === 0) {
  8986. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8987. }
  8988. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8989. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8990. $file_path = str_replace('\\', '/', $file_path);
  8991. $file_path = str_replace('//', '/', $file_path);
  8992. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8993. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8994. // Check if the current document is in that path.
  8995. if (strstr($file_path, $cur_path) !== false) {
  8996. // The document is in that path, now get the relative path
  8997. // to the containing document.
  8998. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8999. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  9000. $relative_path = '';
  9001. if (strstr($file_path, $cur_path) !== false) {
  9002. //$relative_path = substr($file_path, strlen($orig_file_path));
  9003. $relative_path = str_replace($cur_path, '', $file_path);
  9004. $file_path = substr($file_path, strlen($cur_path));
  9005. } else {
  9006. // This case is still a problem as it's difficult to calculate a relative path easily
  9007. // might still generate wrong links.
  9008. //$file_path = substr($file_path,strlen($cur_path));
  9009. // Calculate the directory path to the current file (without trailing slash).
  9010. $my_relative_path = dirname($file_path);
  9011. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  9012. $my_relative_file = basename($file_path);
  9013. // Calculate the directory path to the containing file (without trailing slash).
  9014. $my_orig_file_path = substr($orig_file_path, 0, -1);
  9015. $dotdot = '';
  9016. $subdir = '';
  9017. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  9018. $my_relative_path2 = dirname($my_relative_path);
  9019. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  9020. $my_orig_file_path = dirname($my_orig_file_path);
  9021. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  9022. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  9023. $dotdot += '../';
  9024. $my_relative_path = $my_relative_path2;
  9025. }
  9026. $relative_path = $dotdot.$subdir.$my_relative_file;
  9027. }
  9028. // Put the current document in the zip (this array is the array
  9029. // that will manage documents already in the course folder - relative).
  9030. $zip_files[] = $file_path;
  9031. // Update the links to the current document in the containing document (make them relative).
  9032. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  9033. $my_dep_file->setAttribute('href', $file_path);
  9034. $my_dep->setAttribute('xml:base', '');
  9035. } elseif (strstr($file_path, $main_path) !== false) {
  9036. // The calculated real path is really inside Chamilo's root path.
  9037. // Reduce file path to what's under the DocumentRoot.
  9038. $file_path = substr($file_path, strlen($root_path));
  9039. //echo $file_path;echo '<br /><br />';
  9040. //error_log('Reduced path: '.$file_path, 0);
  9041. $zip_files_abs[] = $file_path;
  9042. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9043. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9044. $my_dep->setAttribute('xml:base', '');
  9045. } elseif (empty($file_path)) {
  9046. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9047. if(strpos($document_root,-1) == '/') {
  9048. $document_root = substr(0, -1, $document_root);
  9049. }*/
  9050. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9051. $file_path = str_replace('//', '/', $file_path);
  9052. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9053. $current_dir = dirname($abs_path);
  9054. $current_dir = str_replace('\\', '/', $current_dir);
  9055. if (file_exists($file_path)) {
  9056. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9057. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9058. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9059. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9060. $my_dep->setAttribute('xml:base', '');
  9061. }
  9062. }
  9063. break;
  9064. case 'rel':
  9065. // Path relative to the current document.
  9066. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  9067. if (substr($doc_info[0], 0, 2) == '..') {
  9068. // Relative path going up.
  9069. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9070. $current_dir = str_replace('\\', '/', $current_dir);
  9071. $file_path = realpath($current_dir.$doc_info[0]);
  9072. $file_path = str_replace('\\', '/', $file_path);
  9073. //error_log($file_path.' <-> '.$main_path,0);
  9074. if (strstr($file_path, $main_path) !== false) {
  9075. // The calculated real path is really inside Chamilo's root path.
  9076. // Reduce file path to what's under the DocumentRoot.
  9077. $file_path = substr($file_path, strlen($root_path));
  9078. //error_log('Reduced path: '.$file_path, 0);
  9079. $zip_files_abs[] = $file_path;
  9080. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9081. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9082. $my_dep->setAttribute('xml:base', '');
  9083. }
  9084. } else {
  9085. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9086. $my_dep_file->setAttribute('href', $doc_info[0]);
  9087. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9088. }
  9089. break;
  9090. default:
  9091. $my_dep_file->setAttribute('href', $doc_info[0]);
  9092. $my_dep->setAttribute('xml:base', '');
  9093. break;
  9094. }
  9095. }
  9096. $my_dep->appendChild($my_dep_file);
  9097. $resources->appendChild($my_dep);
  9098. $dependency = $xmldoc->createElement('dependency');
  9099. $dependency->setAttribute('identifierref', $res_id);
  9100. $my_resource->appendChild($dependency);
  9101. $i++;
  9102. }
  9103. $resources->appendChild($my_resource);
  9104. $zip_files[] = $my_file_path;
  9105. } else {
  9106. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  9107. switch ($item->type) {
  9108. case TOOL_LINK:
  9109. $my_item = $xmldoc->createElement('item');
  9110. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9111. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9112. $my_item->setAttribute('isvisible', 'true');
  9113. // Give a child element <title> to the <item> element.
  9114. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  9115. $my_item->appendChild($my_title);
  9116. // Give a child element <adlcp:prerequisites> to the <item> element.
  9117. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9118. $my_prereqs->setAttribute('type', 'aicc_script');
  9119. $my_item->appendChild($my_prereqs);
  9120. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  9121. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  9122. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  9123. //$xmldoc->createElement('adlcp:timelimitaction', '');
  9124. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  9125. //$xmldoc->createElement('adlcp:datafromlms', '');
  9126. // Give a child element <adlcp:masteryscore> to the <item> element.
  9127. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9128. $my_item->appendChild($my_masteryscore);
  9129. // Attach this item to the organization element or its parent if there is one.
  9130. if (!empty($item->parent) && $item->parent != 0) {
  9131. $children = $organization->childNodes;
  9132. for ($i = 0; $i < $children->length; $i++) {
  9133. $item_temp = $children->item($i);
  9134. if ($item_temp -> nodeName == 'item') {
  9135. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9136. $item_temp -> appendChild($my_item);
  9137. }
  9138. }
  9139. }
  9140. } else {
  9141. $organization->appendChild($my_item);
  9142. }
  9143. $my_file_path = 'link_'.$item->get_id().'.html';
  9144. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  9145. WHERE c_id = '.$course_id.' AND id='.$item->path;
  9146. $rs = Database::query($sql);
  9147. if ($link = Database::fetch_array($rs)) {
  9148. $url = $link['url'];
  9149. $title = stripslashes($link['title']);
  9150. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  9151. $my_xml_file_path = $my_file_path;
  9152. $my_sub_dir = dirname($my_file_path);
  9153. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9154. $my_xml_sub_dir = $my_sub_dir;
  9155. // Give a <resource> child to the <resources> element.
  9156. $my_resource = $xmldoc->createElement('resource');
  9157. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9158. $my_resource->setAttribute('type', 'webcontent');
  9159. $my_resource->setAttribute('href', $my_xml_file_path);
  9160. // adlcp:scormtype can be either 'sco' or 'asset'.
  9161. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9162. // xml:base is the base directory to find the files declared in this resource.
  9163. $my_resource->setAttribute('xml:base', '');
  9164. // give a <file> child to the <resource> element.
  9165. $my_file = $xmldoc->createElement('file');
  9166. $my_file->setAttribute('href', $my_xml_file_path);
  9167. $my_resource->appendChild($my_file);
  9168. $resources->appendChild($my_resource);
  9169. }
  9170. break;
  9171. case TOOL_QUIZ:
  9172. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  9173. $exe = new Exercise();
  9174. $exe->read($exe_id);
  9175. $my_item = $xmldoc->createElement('item');
  9176. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9177. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9178. $my_item->setAttribute('isvisible', 'true');
  9179. // Give a child element <title> to the <item> element.
  9180. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  9181. $my_item->appendChild($my_title);
  9182. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  9183. //$my_item->appendChild($my_max_score);
  9184. // Give a child element <adlcp:prerequisites> to the <item> element.
  9185. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9186. $my_prereqs->setAttribute('type', 'aicc_script');
  9187. $my_item->appendChild($my_prereqs);
  9188. // Give a child element <adlcp:masteryscore> to the <item> element.
  9189. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9190. $my_item->appendChild($my_masteryscore);
  9191. // Attach this item to the organization element or hits parent if there is one.
  9192. if (!empty($item->parent) && $item->parent != 0) {
  9193. $children = $organization->childNodes;
  9194. /*for ($i = 0; $i < $children->length; $i++) {
  9195. $item_temp = $children->item($i);
  9196. if ($exe_id == 81) {
  9197. error_log($item_temp->nodeName );
  9198. error_log($item_temp->getAttribute('identifier'));
  9199. }
  9200. if ($item_temp->nodeName == 'item') {
  9201. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9202. $item_temp->appendChild($my_item);
  9203. }
  9204. }
  9205. }*/
  9206. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9207. if ($possible_parent) {
  9208. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9209. $possible_parent->appendChild($my_item);
  9210. }
  9211. }
  9212. } else {
  9213. $organization->appendChild($my_item);
  9214. }
  9215. // Get the path of the file(s) from the course directory root
  9216. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9217. $my_file_path = 'quiz_'.$item->get_id().'.html';
  9218. // Write the contents of the exported exercise into a (big) html file
  9219. // to later pack it into the exported SCORM. The file will be removed afterwards.
  9220. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  9221. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  9222. $res = file_put_contents($tmp_file_path, $contents);
  9223. if ($res === false) {
  9224. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  9225. }
  9226. $files_cleanup[] = $tmp_file_path;
  9227. $my_xml_file_path = $my_file_path;
  9228. $my_sub_dir = dirname($my_file_path);
  9229. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9230. $my_xml_sub_dir = $my_sub_dir;
  9231. // Give a <resource> child to the <resources> element.
  9232. $my_resource = $xmldoc->createElement('resource');
  9233. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9234. $my_resource->setAttribute('type', 'webcontent');
  9235. $my_resource->setAttribute('href', $my_xml_file_path);
  9236. // adlcp:scormtype can be either 'sco' or 'asset'.
  9237. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9238. // xml:base is the base directory to find the files declared in this resource.
  9239. $my_resource->setAttribute('xml:base', '');
  9240. // Give a <file> child to the <resource> element.
  9241. $my_file = $xmldoc->createElement('file');
  9242. $my_file->setAttribute('href', $my_xml_file_path);
  9243. $my_resource->appendChild($my_file);
  9244. // Get included docs.
  9245. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  9246. // Dependency to other files - not yet supported.
  9247. $i = 1;
  9248. foreach ($inc_docs as $doc_info) {
  9249. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  9250. $my_dep = $xmldoc->createElement('resource');
  9251. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9252. $my_dep->setAttribute('identifier', $res_id);
  9253. $my_dep->setAttribute('type', 'webcontent');
  9254. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9255. $my_dep_file = $xmldoc->createElement('file');
  9256. // Check type of URL.
  9257. if ($doc_info[1] == 'remote') {
  9258. // Remote file. Save url as is.
  9259. $my_dep_file->setAttribute('href', $doc_info[0]);
  9260. $my_dep->setAttribute('xml:base', '');
  9261. } elseif ($doc_info[1] == 'local') {
  9262. switch ($doc_info[2]) {
  9263. case 'url': // Local URL - save path as url for now, don't zip file.
  9264. // Save file but as local file (retrieve from URL).
  9265. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9266. $current_dir = dirname($abs_path);
  9267. $current_dir = str_replace('\\', '/', $current_dir);
  9268. $file_path = realpath($abs_path);
  9269. $file_path = str_replace('\\', '/', $file_path);
  9270. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9271. $my_dep->setAttribute('xml:base', '');
  9272. if (strstr($file_path, $main_path) !== false) {
  9273. // The calculated real path is really inside the chamilo root path.
  9274. // Reduce file path to what's under the DocumentRoot.
  9275. $file_path = substr($file_path, strlen($root_path));
  9276. $zip_files_abs[] = $file_path;
  9277. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9278. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9279. $my_dep->setAttribute('xml:base', '');
  9280. } elseif (empty($file_path)) {
  9281. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  9282. if (strpos($document_root,-1) == '/') {
  9283. $document_root = substr(0, -1, $document_root);
  9284. }*/
  9285. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9286. $file_path = str_replace('//', '/', $file_path);
  9287. if (file_exists($file_path)) {
  9288. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9289. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9290. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9291. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9292. $my_dep->setAttribute('xml:base', '');
  9293. }
  9294. }
  9295. break;
  9296. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9297. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9298. $current_dir = str_replace('\\', '/', $current_dir);
  9299. $file_path = realpath($doc_info[0]);
  9300. $file_path = str_replace('\\', '/', $file_path);
  9301. $my_dep_file->setAttribute('href', $file_path);
  9302. $my_dep->setAttribute('xml:base', '');
  9303. if (strstr($file_path, $main_path) !== false) {
  9304. // The calculated real path is really inside the chamilo root path.
  9305. // Reduce file path to what's under the DocumentRoot.
  9306. $file_path = substr($file_path, strlen($root_path));
  9307. //echo $file_path;echo '<br /><br />';
  9308. //error_log('Reduced path: '.$file_path, 0);
  9309. $zip_files_abs[] = $file_path;
  9310. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9311. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9312. $my_dep->setAttribute('xml:base', '');
  9313. } elseif (empty($file_path)) {
  9314. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9315. if (strpos($document_root,-1) == '/') {
  9316. $document_root = substr(0, -1, $document_root);
  9317. }*/
  9318. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9319. $file_path = str_replace('//', '/', $file_path);
  9320. if (file_exists($file_path)) {
  9321. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9322. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9323. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9324. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9325. $my_dep->setAttribute('xml:base', '');
  9326. }
  9327. }
  9328. break;
  9329. 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
  9330. if (substr($doc_info[0], 0, 2) == '..') {
  9331. // Relative path going up.
  9332. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9333. $current_dir = str_replace('\\', '/', $current_dir);
  9334. $file_path = realpath($current_dir.$doc_info[0]);
  9335. $file_path = str_replace('\\', '/', $file_path);
  9336. //error_log($file_path.' <-> '.$main_path, 0);
  9337. if (strstr($file_path, $main_path) !== false) {
  9338. // The calculated real path is really inside Chamilo's root path.
  9339. // Reduce file path to what's under the DocumentRoot.
  9340. $file_path = substr($file_path, strlen($root_path));
  9341. $file_path_dest = $file_path;
  9342. // File path is courses/CHAMILO/document/....
  9343. $info_file_path = explode('/', $file_path);
  9344. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9345. $file_path_dest = 'document/'.$file_path;
  9346. }
  9347. //error_log('Reduced path: '.$file_path, 0);
  9348. $zip_files_abs[] = $file_path;
  9349. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  9350. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9351. $my_dep->setAttribute('xml:base', '');
  9352. }
  9353. } else {
  9354. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9355. $my_dep_file->setAttribute('href', $doc_info[0]);
  9356. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9357. }
  9358. break;
  9359. default:
  9360. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9361. $my_dep->setAttribute('xml:base', '');
  9362. break;
  9363. }
  9364. }
  9365. $my_dep->appendChild($my_dep_file);
  9366. $resources->appendChild($my_dep);
  9367. $dependency = $xmldoc->createElement('dependency');
  9368. $dependency->setAttribute('identifierref', $res_id);
  9369. $my_resource->appendChild($dependency);
  9370. $i++;
  9371. }
  9372. $resources->appendChild($my_resource);
  9373. $zip_files[] = $my_file_path;
  9374. break;
  9375. default:
  9376. // Get the path of the file(s) from the course directory root
  9377. $my_file_path = 'non_exportable.html';
  9378. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9379. $my_xml_file_path = $my_file_path;
  9380. $my_sub_dir = dirname($my_file_path);
  9381. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9382. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9383. $my_xml_sub_dir = $my_sub_dir;
  9384. // Give a <resource> child to the <resources> element.
  9385. $my_resource = $xmldoc->createElement('resource');
  9386. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9387. $my_resource->setAttribute('type', 'webcontent');
  9388. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9389. // adlcp:scormtype can be either 'sco' or 'asset'.
  9390. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9391. // xml:base is the base directory to find the files declared in this resource.
  9392. $my_resource->setAttribute('xml:base', '');
  9393. // Give a <file> child to the <resource> element.
  9394. $my_file = $xmldoc->createElement('file');
  9395. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9396. $my_resource->appendChild($my_file);
  9397. $resources->appendChild($my_resource);
  9398. break;
  9399. }
  9400. }
  9401. }
  9402. $organizations->appendChild($organization);
  9403. $root->appendChild($organizations);
  9404. $root->appendChild($resources);
  9405. $xmldoc->appendChild($root);
  9406. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  9407. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9408. // then add the file to the zip, then destroy the file (this is done automatically).
  9409. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9410. foreach ($zip_files as $file_path) {
  9411. if (empty($file_path)) {
  9412. continue;
  9413. }
  9414. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  9415. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9416. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9417. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9418. }
  9419. $this->create_path($dest_file);
  9420. @copy($filePath, $dest_file);
  9421. // Check if the file needs a link update.
  9422. if (in_array($file_path, array_keys($link_updates))) {
  9423. $string = file_get_contents($dest_file);
  9424. unlink($dest_file);
  9425. foreach ($link_updates[$file_path] as $old_new) {
  9426. // This is an ugly hack that allows .flv files to be found by the flv player that
  9427. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9428. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9429. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9430. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9431. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9432. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9433. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9434. }
  9435. //Fix to avoid problems with default_course_document
  9436. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9437. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9438. $newDestination = $old_new['dest'];
  9439. } else {
  9440. $newDestination = str_replace('document/', '', $old_new['dest']);
  9441. }
  9442. $string = str_replace($old_new['orig'], $newDestination, $string);
  9443. // Add files inside the HTMLs
  9444. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9445. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9446. if (file_exists($sys_course_path.$new_path)) {
  9447. copy($sys_course_path.$new_path, $destinationFile);
  9448. }
  9449. }
  9450. file_put_contents($dest_file, $string);
  9451. }
  9452. if (file_exists($filePath) && $copyAll) {
  9453. $extension = $this->get_extension($filePath);
  9454. if (in_array($extension, ['html', 'html'])) {
  9455. $containerOrigin = dirname($filePath);
  9456. $containerDestination = dirname($dest_file);
  9457. $finder = new Finder();
  9458. $finder->files()->in($containerOrigin)
  9459. ->notName('*_DELETED_*')
  9460. ->exclude('share_folder')
  9461. ->exclude('chat_files')
  9462. ->exclude('certificates')
  9463. ;
  9464. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9465. $fs = new Filesystem();
  9466. $fs->mirror(
  9467. $containerOrigin,
  9468. $containerDestination,
  9469. $finder
  9470. );
  9471. }
  9472. }
  9473. }
  9474. }
  9475. foreach ($zip_files_abs as $file_path) {
  9476. if (empty($file_path)) {
  9477. continue;
  9478. }
  9479. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9480. continue;
  9481. }
  9482. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9483. $this->create_path($dest_file);
  9484. copy($main_path.$file_path, $dest_file);
  9485. // Check if the file needs a link update.
  9486. if (in_array($file_path, array_keys($link_updates))) {
  9487. $string = file_get_contents($dest_file);
  9488. unlink($dest_file);
  9489. foreach ($link_updates[$file_path] as $old_new) {
  9490. // This is an ugly hack that allows .flv files to be found by the flv player that
  9491. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9492. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9493. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9494. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9495. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9496. }
  9497. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9498. }
  9499. file_put_contents($dest_file, $string);
  9500. }
  9501. }
  9502. if (is_array($links_to_create)) {
  9503. foreach ($links_to_create as $file => $link) {
  9504. $file_content = '<!DOCTYPE html><head>
  9505. <meta charset="'.api_get_language_isocode().'" />
  9506. <title>'.$link['title'].'</title>
  9507. </head>
  9508. <body dir="'.api_get_text_direction().'">
  9509. <div style="text-align:center">
  9510. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9511. </body>
  9512. </html>';
  9513. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9514. }
  9515. }
  9516. // Add non exportable message explanation.
  9517. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9518. $file_content = '<!DOCTYPE html><head>
  9519. <meta charset="'.api_get_language_isocode().'" />
  9520. <title>'.$lang_not_exportable.'</title>
  9521. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9522. </head>
  9523. <body dir="'.api_get_text_direction().'">';
  9524. $file_content .=
  9525. <<<EOD
  9526. <style>
  9527. .error-message {
  9528. font-family: arial, verdana, helvetica, sans-serif;
  9529. border-width: 1px;
  9530. border-style: solid;
  9531. left: 50%;
  9532. margin: 10px auto;
  9533. min-height: 30px;
  9534. padding: 5px;
  9535. right: 50%;
  9536. width: 500px;
  9537. background-color: #FFD1D1;
  9538. border-color: #FF0000;
  9539. color: #000;
  9540. }
  9541. </style>
  9542. <body>
  9543. <div class="error-message">
  9544. $lang_not_exportable
  9545. </div>
  9546. </body>
  9547. </html>
  9548. EOD;
  9549. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9550. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9551. }
  9552. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9553. // Add the extra files that go along with a SCORM package.
  9554. $main_code_path = api_get_path(SYS_CODE_PATH).'lp/packaging/';
  9555. $fs = new Filesystem();
  9556. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9557. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9558. $manifest = @$xmldoc->saveXML();
  9559. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9560. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9561. $zip_folder->add(
  9562. $archive_path.'/'.$temp_dir_short,
  9563. PCLZIP_OPT_REMOVE_PATH,
  9564. $archive_path.'/'.$temp_dir_short.'/'
  9565. );
  9566. // Clean possible temporary files.
  9567. foreach ($files_cleanup as $file) {
  9568. $res = unlink($file);
  9569. if ($res === false) {
  9570. error_log(
  9571. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9572. 0
  9573. );
  9574. }
  9575. }
  9576. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9577. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9578. }
  9579. /**
  9580. * @param int $lp_id
  9581. * @return bool
  9582. */
  9583. public function scorm_export_to_pdf($lp_id)
  9584. {
  9585. $lp_id = intval($lp_id);
  9586. $files_to_export = array();
  9587. $course_data = api_get_course_info($this->cc);
  9588. if (!empty($course_data)) {
  9589. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9590. $list = self::get_flat_ordered_items_list($lp_id);
  9591. if (!empty($list)) {
  9592. foreach ($list as $item_id) {
  9593. $item = $this->items[$item_id];
  9594. switch ($item->type) {
  9595. case 'document':
  9596. //Getting documents from a LP with chamilo documents
  9597. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9598. // Try loading document from the base course.
  9599. if (empty($file_data) && !empty($sessionId)) {
  9600. $file_data = DocumentManager::get_document_data_by_id(
  9601. $item->path,
  9602. $this->cc,
  9603. false,
  9604. 0
  9605. );
  9606. }
  9607. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9608. if (file_exists($file_path)) {
  9609. $files_to_export[] = array('title'=>$item->get_title(), 'path'=>$file_path);
  9610. }
  9611. break;
  9612. case 'asset': //commes from a scorm package generated by chamilo
  9613. case 'sco':
  9614. $file_path = $scorm_path.'/'.$item->path;
  9615. if (file_exists($file_path)) {
  9616. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9617. }
  9618. break;
  9619. case 'dir':
  9620. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9621. break;
  9622. }
  9623. }
  9624. }
  9625. $pdf = new PDF();
  9626. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9627. return $result;
  9628. }
  9629. return false;
  9630. }
  9631. /**
  9632. * Temp function to be moved in main_api or the best place around for this.
  9633. * Creates a file path if it doesn't exist
  9634. * @param string $path
  9635. */
  9636. public function create_path($path)
  9637. {
  9638. $path_bits = explode('/', dirname($path));
  9639. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9640. $path_built = IS_WINDOWS_OS ? '' : '/';
  9641. foreach ($path_bits as $bit) {
  9642. if (!empty ($bit)) {
  9643. $new_path = $path_built.$bit;
  9644. if (is_dir($new_path)) {
  9645. $path_built = $new_path.'/';
  9646. } else {
  9647. mkdir($new_path, api_get_permissions_for_new_directories());
  9648. $path_built = $new_path.'/';
  9649. }
  9650. }
  9651. }
  9652. }
  9653. /**
  9654. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9655. * @return boolean The results of the unlink function, or false if there was no image to start with
  9656. */
  9657. public function delete_lp_image()
  9658. {
  9659. $img = $this->get_preview_image();
  9660. if ($img != '') {
  9661. $del_file = $this->get_preview_image_path(null, 'sys');
  9662. if (isset($del_file) && file_exists($del_file)) {
  9663. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9664. if (file_exists($del_file_2)) {
  9665. unlink($del_file_2);
  9666. }
  9667. $this->set_preview_image('');
  9668. return @unlink($del_file);
  9669. }
  9670. }
  9671. return false;
  9672. }
  9673. /**
  9674. * Uploads an author image to the upload/learning_path/images directory
  9675. * @param array The image array, coming from the $_FILES superglobal
  9676. * @return boolean True on success, false on error
  9677. */
  9678. public function upload_image($image_array)
  9679. {
  9680. if (!empty ($image_array['name'])) {
  9681. $upload_ok = process_uploaded_file($image_array);
  9682. $has_attachment = true;
  9683. }
  9684. if ($upload_ok) {
  9685. if ($has_attachment) {
  9686. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9687. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9688. $updir = $sys_course_path.$courseDir;
  9689. // Try to add an extension to the file if it hasn't one.
  9690. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9691. if (filter_extension($new_file_name)) {
  9692. $file_extension = explode('.', $image_array['name']);
  9693. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9694. $filename = uniqid('');
  9695. $new_file_name = $filename.'.'.$file_extension;
  9696. $new_path = $updir.'/'.$new_file_name;
  9697. // Resize the image.
  9698. $temp = new Image($image_array['tmp_name']);
  9699. $temp->resize(104);
  9700. $result = $temp->send_image($new_path);
  9701. // Storing the image filename.
  9702. if ($result) {
  9703. $this->set_preview_image($new_file_name);
  9704. //Resize to 64px to use on course homepage
  9705. $temp->resize(64);
  9706. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9707. return true;
  9708. }
  9709. }
  9710. }
  9711. }
  9712. return false;
  9713. }
  9714. /**
  9715. * @param int $lp_id
  9716. * @param string $status
  9717. */
  9718. public function set_autolaunch($lp_id, $status)
  9719. {
  9720. $course_id = api_get_course_int_id();
  9721. $lp_id = intval($lp_id);
  9722. $status = intval($status);
  9723. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9724. // Setting everything to autolaunch = 0
  9725. $attributes['autolaunch'] = 0;
  9726. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9727. Database::update($lp_table, $attributes, $where);
  9728. if ($status == 1) {
  9729. //Setting my lp_id to autolaunch = 1
  9730. $attributes['autolaunch'] = 1;
  9731. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9732. Database::update($lp_table, $attributes, $where);
  9733. }
  9734. }
  9735. /**
  9736. * Gets previous_item_id for the next element of the lp_item table
  9737. * @author Isaac flores paz
  9738. * @return integer Previous item ID
  9739. */
  9740. public function select_previous_item_id()
  9741. {
  9742. $course_id = api_get_course_int_id();
  9743. if ($this->debug > 0) {
  9744. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9745. }
  9746. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9747. // Get the max order of the items
  9748. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9749. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'";
  9750. $rs_max_order = Database::query($sql);
  9751. $row_max_order = Database::fetch_object($rs_max_order);
  9752. $max_order = $row_max_order->display_order;
  9753. // Get the previous item ID
  9754. $sql = "SELECT id as previous FROM $table_lp_item
  9755. WHERE
  9756. c_id = $course_id AND
  9757. lp_id = '".$this->lp_id."' AND
  9758. display_order = '".$max_order."' ";
  9759. $rs_max = Database::query($sql);
  9760. $row_max = Database::fetch_object($rs_max);
  9761. // Return the previous item ID
  9762. return $row_max->previous;
  9763. }
  9764. /**
  9765. * Copies an LP
  9766. */
  9767. public function copy()
  9768. {
  9769. // Course builder
  9770. $cb = new CourseBuilder();
  9771. //Setting tools that will be copied
  9772. $cb->set_tools_to_build(array('learnpaths'));
  9773. //Setting elements that will be copied
  9774. $cb->set_tools_specific_id_list(
  9775. array('learnpaths' => array($this->lp_id))
  9776. );
  9777. $course = $cb->build();
  9778. //Course restorer
  9779. $course_restorer = new CourseRestorer($course);
  9780. $course_restorer->set_add_text_in_items(true);
  9781. $course_restorer->set_tool_copy_settings(
  9782. array('learnpaths' => array('reset_dates' => true))
  9783. );
  9784. $course_restorer->restore(
  9785. api_get_course_id(),
  9786. api_get_session_id(),
  9787. false,
  9788. false
  9789. );
  9790. }
  9791. /**
  9792. * Verify document size
  9793. * @param string $s
  9794. * @return bool
  9795. */
  9796. public static function verify_document_size($s)
  9797. {
  9798. $post_max = ini_get('post_max_size');
  9799. if (substr($post_max, -1, 1) == 'M') {
  9800. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9801. } elseif (substr($post_max, -1, 1) == 'G') {
  9802. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9803. }
  9804. $upl_max = ini_get('upload_max_filesize');
  9805. if (substr($upl_max, -1, 1) == 'M') {
  9806. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9807. } elseif (substr($upl_max, -1, 1) == 'G') {
  9808. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9809. }
  9810. $documents_total_space = DocumentManager::documents_total_space();
  9811. $course_max_space = DocumentManager::get_course_quota();
  9812. $total_size = filesize($s) + $documents_total_space;
  9813. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9814. return true;
  9815. } else {
  9816. return false;
  9817. }
  9818. }
  9819. /**
  9820. * Clear LP prerequisites
  9821. */
  9822. public function clear_prerequisites()
  9823. {
  9824. $course_id = $this->get_course_int_id();
  9825. if ($this->debug > 0) {
  9826. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9827. }
  9828. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9829. $lp_id = $this->get_id();
  9830. //Cleaning prerequisites
  9831. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9832. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9833. Database::query($sql);
  9834. //Cleaning mastery score for exercises
  9835. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9836. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9837. Database::query($sql);
  9838. }
  9839. public function set_previous_step_as_prerequisite_for_all_items()
  9840. {
  9841. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9842. $course_id = $this->get_course_int_id();
  9843. $lp_id = $this->get_id();
  9844. if (!empty($this->items)) {
  9845. $previous_item_id = null;
  9846. $previous_item_max = 0;
  9847. $previous_item_type = null;
  9848. $last_item_not_dir = null;
  9849. $last_item_not_dir_type = null;
  9850. $last_item_not_dir_max = null;
  9851. foreach ($this->items as $item) {
  9852. // if there was a previous item... (otherwise jump to set it)
  9853. if (!empty($previous_item_id)) {
  9854. $current_item_id = $item->get_id(); //save current id
  9855. if ($item->get_type() != 'dir') {
  9856. // Current item is not a folder, so it qualifies to get a prerequisites
  9857. if ($last_item_not_dir_type == 'quiz') {
  9858. // if previous is quiz, mark its max score as default score to be achieved
  9859. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9860. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9861. Database::query($sql);
  9862. }
  9863. // now simply update the prerequisite to set it to the last non-chapter item
  9864. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9865. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9866. Database::query($sql);
  9867. // record item as 'non-chapter' reference
  9868. $last_item_not_dir = $item->get_id();
  9869. $last_item_not_dir_type = $item->get_type();
  9870. $last_item_not_dir_max = $item->get_max();
  9871. }
  9872. } else {
  9873. if ($item->get_type() != 'dir') {
  9874. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9875. $last_item_not_dir = $item->get_id();
  9876. $last_item_not_dir_type = $item->get_type();
  9877. $last_item_not_dir_max = $item->get_max();
  9878. }
  9879. }
  9880. // Saving the item as "previous item" for the next loop
  9881. $previous_item_id = $item->get_id();
  9882. $previous_item_max = $item->get_max();
  9883. $previous_item_type = $item->get_type();
  9884. }
  9885. }
  9886. }
  9887. /**
  9888. * @param array $params
  9889. */
  9890. public static function createCategory($params)
  9891. {
  9892. $em = Database::getManager();
  9893. $item = new CLpCategory();
  9894. $item->setName($params['name']);
  9895. $item->setCId($params['c_id']);
  9896. $em->persist($item);
  9897. $em->flush();
  9898. }
  9899. /**
  9900. * @param array $params
  9901. */
  9902. public static function updateCategory($params)
  9903. {
  9904. $em = Database::getManager();
  9905. /** @var CLpCategory $item */
  9906. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9907. if ($item) {
  9908. $item->setName($params['name']);
  9909. $em->merge($item);
  9910. $em->flush();
  9911. }
  9912. }
  9913. /**
  9914. * @param int $id
  9915. */
  9916. public static function moveUpCategory($id)
  9917. {
  9918. $em = Database::getManager();
  9919. /** @var CLpCategory $item */
  9920. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9921. if ($item) {
  9922. $position = $item->getPosition() - 1;
  9923. $item->setPosition($position);
  9924. $em->persist($item);
  9925. $em->flush();
  9926. }
  9927. }
  9928. /**
  9929. * @param int $id
  9930. */
  9931. public static function moveDownCategory($id)
  9932. {
  9933. $em = Database::getManager();
  9934. /** @var CLpCategory $item */
  9935. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9936. if ($item) {
  9937. $position = $item->getPosition() + 1;
  9938. $item->setPosition($position);
  9939. $em->persist($item);
  9940. $em->flush();
  9941. }
  9942. }
  9943. /**
  9944. * @param int $courseId
  9945. * @return int|mixed
  9946. */
  9947. public static function getCountCategories($courseId)
  9948. {
  9949. if (empty($course_id)) {
  9950. return 0;
  9951. }
  9952. $em = Database::getManager();
  9953. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9954. $query->setParameter('id', $courseId);
  9955. return $query->getSingleScalarResult();
  9956. }
  9957. /**
  9958. * @param int $courseId
  9959. *
  9960. * @return mixed
  9961. */
  9962. public static function getCategories($courseId)
  9963. {
  9964. $em = Database::getManager();
  9965. //Default behaviour
  9966. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9967. array('cId' => $course_id),
  9968. array('name' => 'ASC')
  9969. );*/
  9970. // Using doctrine extensions
  9971. /** @var SortableRepository $repo */
  9972. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9973. $items = $repo
  9974. ->getBySortableGroupsQuery(['cId' => $courseId])
  9975. ->getResult();
  9976. return $items;
  9977. }
  9978. /**
  9979. * @param int $id
  9980. *
  9981. * @return CLpCategory
  9982. */
  9983. public static function getCategory($id)
  9984. {
  9985. $em = Database::getManager();
  9986. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9987. return $item;
  9988. }
  9989. /**
  9990. * @param int $courseId
  9991. * @return array
  9992. */
  9993. public static function getCategoryByCourse($courseId)
  9994. {
  9995. $em = Database::getManager();
  9996. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9997. array('cId' => $courseId)
  9998. );
  9999. return $items;
  10000. }
  10001. /**
  10002. * @param int $id
  10003. *
  10004. * @return mixed
  10005. */
  10006. public static function deleteCategory($id)
  10007. {
  10008. $em = Database::getManager();
  10009. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  10010. if ($item) {
  10011. $courseId = $item->getCId();
  10012. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  10013. $query->setParameter('id', $courseId);
  10014. $query->setParameter('catId', $item->getId());
  10015. $lps = $query->getResult();
  10016. // Setting category = 0.
  10017. if ($lps) {
  10018. foreach ($lps as $lpItem) {
  10019. $lpItem->setCategoryId(0);
  10020. }
  10021. }
  10022. // Removing category.
  10023. $em->remove($item);
  10024. $em->flush();
  10025. }
  10026. }
  10027. /**
  10028. * @param int $courseId
  10029. * @param bool $addSelectOption
  10030. *
  10031. * @return mixed
  10032. */
  10033. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  10034. {
  10035. $items = self::getCategoryByCourse($courseId);
  10036. $cats = array();
  10037. if ($addSelectOption) {
  10038. $cats = array(get_lang('SelectACategory'));
  10039. }
  10040. if (!empty($items)) {
  10041. foreach ($items as $cat) {
  10042. $cats[$cat->getId()] = $cat->getName();
  10043. }
  10044. }
  10045. return $cats;
  10046. }
  10047. /**
  10048. * Return the scorm item type object with spaces replaced with _
  10049. * The return result is use to build a css classname like scorm_type_$return
  10050. * @param $in_type
  10051. * @return mixed
  10052. */
  10053. private static function format_scorm_type_item($in_type)
  10054. {
  10055. return str_replace(' ', '_', $in_type);
  10056. }
  10057. /**
  10058. * @param string $courseCode
  10059. * @param int $lp_id
  10060. * @param int $user_id
  10061. *
  10062. * @return learnpath
  10063. */
  10064. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  10065. {
  10066. $learnPath = null;
  10067. $lpObject = Session::read('lpobject');
  10068. if ($lpObject !== null) {
  10069. $learnPath = unserialize($lpObject);
  10070. }
  10071. if (!is_object($learnPath)) {
  10072. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  10073. }
  10074. return $learnPath;
  10075. }
  10076. /**
  10077. * @param int $itemId
  10078. * @return learnpathItem|false
  10079. */
  10080. public function getItem($itemId)
  10081. {
  10082. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  10083. return $this->items[$itemId];
  10084. }
  10085. return false;
  10086. }
  10087. /**
  10088. * @return int
  10089. */
  10090. public function getCategoryId()
  10091. {
  10092. return $this->categoryId;
  10093. }
  10094. /**
  10095. * @param int $categoryId
  10096. * @return bool
  10097. */
  10098. public function setCategoryId($categoryId)
  10099. {
  10100. $this->categoryId = intval($categoryId);
  10101. $courseId = api_get_course_int_id();
  10102. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10103. $lp_id = $this->get_id();
  10104. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  10105. WHERE c_id = $courseId AND id = $lp_id";
  10106. Database::query($sql);
  10107. return true;
  10108. }
  10109. /**
  10110. * Get whether this is a learning path with the possibility to subscribe
  10111. * users or not
  10112. * @return int
  10113. */
  10114. public function getSubscribeUsers()
  10115. {
  10116. return $this->subscribeUsers;
  10117. }
  10118. /**
  10119. * Set whether this is a learning path with the possibility to subscribe
  10120. * users or not
  10121. * @param int $subscribeUsers (0 = false, 1 = true)
  10122. * @return bool
  10123. */
  10124. public function setSubscribeUsers($value)
  10125. {
  10126. if ($this->debug > 0) {
  10127. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  10128. }
  10129. $this->subscribeUsers = intval($value); ;
  10130. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10131. $lp_id = $this->get_id();
  10132. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  10133. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10134. Database::query($sql);
  10135. return true;
  10136. }
  10137. /**
  10138. * Calculate the count of stars for a user in this LP
  10139. * This calculation is based on the following rules:
  10140. * - the student gets one star when he gets to 50% of the learning path
  10141. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  10142. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  10143. * - the student gets the final star when the score for the *last* test is >= 80%
  10144. * @param int $sessionId Optional. The session ID
  10145. * @return int The count of stars
  10146. */
  10147. public function getCalculateStars($sessionId = 0)
  10148. {
  10149. $stars = 0;
  10150. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  10151. if ($progress >= 50) {
  10152. $stars++;
  10153. }
  10154. // Calculate stars chapters evaluation
  10155. $exercisesItems = $this->getExercisesItems();
  10156. if (!empty($exercisesItems)) {
  10157. $totalResult = 0;
  10158. foreach ($exercisesItems as $exerciseItem) {
  10159. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10160. $this->user_id,
  10161. $exerciseItem->path,
  10162. $this->course_int_id,
  10163. $sessionId,
  10164. $this->lp_id,
  10165. $exerciseItem->db_id
  10166. );
  10167. $exerciseResultInfo = end($exerciseResultInfo);
  10168. if (!$exerciseResultInfo) {
  10169. continue;
  10170. }
  10171. if (!empty($exerciseResultInfo['exe_weighting'])) {
  10172. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  10173. } else {
  10174. $exerciseResult = 0;
  10175. }
  10176. $totalResult += $exerciseResult;
  10177. }
  10178. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  10179. if ($totalExerciseAverage >= 50) {
  10180. $stars++;
  10181. }
  10182. if ($totalExerciseAverage >= 80) {
  10183. $stars++;
  10184. }
  10185. }
  10186. // Calculate star for final evaluation
  10187. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10188. if (!empty($finalEvaluationItem)) {
  10189. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10190. $this->user_id,
  10191. $finalEvaluationItem->path,
  10192. $this->course_int_id,
  10193. $sessionId,
  10194. $this->lp_id,
  10195. $finalEvaluationItem->db_id
  10196. );
  10197. $evaluationResultInfo = end($evaluationResultInfo);
  10198. if ($evaluationResultInfo) {
  10199. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  10200. if ($evaluationResult >= 80) {
  10201. $stars++;
  10202. }
  10203. }
  10204. }
  10205. return $stars;
  10206. }
  10207. /**
  10208. * Get the items of exercise type
  10209. * @return array The items. Otherwise return false
  10210. */
  10211. public function getExercisesItems()
  10212. {
  10213. $exercises = [];
  10214. foreach ($this->items as $item) {
  10215. if ($item->type != 'quiz') {
  10216. continue;
  10217. }
  10218. $exercises[] = $item;
  10219. }
  10220. array_pop($exercises);
  10221. return $exercises;
  10222. }
  10223. /**
  10224. * Get the item of exercise type (evaluation type)
  10225. * @return array The final evaluation. Otherwise return false
  10226. */
  10227. public function getFinalEvaluationItem()
  10228. {
  10229. $exercises = [];
  10230. foreach ($this->items as $item) {
  10231. if ($item->type != 'quiz') {
  10232. continue;
  10233. }
  10234. $exercises[] = $item;
  10235. }
  10236. return array_pop($exercises);
  10237. }
  10238. /**
  10239. * Calculate the total points achieved for the current user in this learning path
  10240. * @param int $sessionId Optional. The session Id
  10241. * @return int
  10242. */
  10243. public function getCalculateScore($sessionId = 0)
  10244. {
  10245. // Calculate stars chapters evaluation
  10246. $exercisesItems = $this->getExercisesItems();
  10247. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10248. $totalExercisesResult = 0;
  10249. $totalEvaluationResult = 0;
  10250. if ($exercisesItems !== false) {
  10251. foreach ($exercisesItems as $exerciseItem) {
  10252. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10253. $this->user_id,
  10254. $exerciseItem->path,
  10255. $this->course_int_id,
  10256. $sessionId,
  10257. $this->lp_id,
  10258. $exerciseItem->db_id
  10259. );
  10260. $exerciseResultInfo = end($exerciseResultInfo);
  10261. if (!$exerciseResultInfo) {
  10262. continue;
  10263. }
  10264. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  10265. }
  10266. }
  10267. if (!empty($finalEvaluationItem)) {
  10268. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10269. $this->user_id,
  10270. $finalEvaluationItem->path,
  10271. $this->course_int_id,
  10272. $sessionId,
  10273. $this->lp_id,
  10274. $finalEvaluationItem->db_id
  10275. );
  10276. $evaluationResultInfo = end($evaluationResultInfo);
  10277. if ($evaluationResultInfo) {
  10278. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  10279. }
  10280. }
  10281. return $totalExercisesResult + $totalEvaluationResult;
  10282. }
  10283. /**
  10284. * Check if URL is not allowed to be show in a iframe
  10285. * @param string $src
  10286. *
  10287. * @return string
  10288. */
  10289. public function fixBlockedLinks($src)
  10290. {
  10291. $urlInfo = parse_url($src);
  10292. $platformProtocol = 'https';
  10293. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  10294. $platformProtocol = 'http';
  10295. }
  10296. $protocolFixApplied = false;
  10297. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  10298. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  10299. if ($platformProtocol != $scheme) {
  10300. Session::write('x_frame_source', $src);
  10301. $src = 'blank.php?error=x_frames_options';
  10302. $protocolFixApplied = true;
  10303. }
  10304. if ($protocolFixApplied == false) {
  10305. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  10306. // Check X-Frame-Options
  10307. $ch = curl_init();
  10308. $options = array(
  10309. CURLOPT_URL => $src,
  10310. CURLOPT_RETURNTRANSFER => true,
  10311. CURLOPT_HEADER => true,
  10312. CURLOPT_FOLLOWLOCATION => true,
  10313. CURLOPT_ENCODING => "",
  10314. CURLOPT_AUTOREFERER => true,
  10315. CURLOPT_CONNECTTIMEOUT => 120,
  10316. CURLOPT_TIMEOUT => 120,
  10317. CURLOPT_MAXREDIRS => 10,
  10318. );
  10319. curl_setopt_array($ch, $options);
  10320. $response = curl_exec($ch);
  10321. $httpCode = curl_getinfo($ch);
  10322. $headers = substr($response, 0, $httpCode['header_size']);
  10323. $error = false;
  10324. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  10325. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  10326. ) {
  10327. $error = true;
  10328. }
  10329. if ($error) {
  10330. Session::write('x_frame_source', $src);
  10331. $src = 'blank.php?error=x_frames_options';
  10332. }
  10333. }
  10334. }
  10335. return $src;
  10336. }
  10337. /**
  10338. * Check if this LP has a created forum in the basis course
  10339. * @return boolean
  10340. */
  10341. public function lpHasForum()
  10342. {
  10343. $forumTable = Database::get_course_table(TABLE_FORUM);
  10344. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10345. $fakeFrom = "
  10346. $forumTable f
  10347. INNER JOIN $itemProperty ip
  10348. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  10349. ";
  10350. $resultData = Database::select(
  10351. 'COUNT(f.iid) AS qty',
  10352. $fakeFrom,
  10353. [
  10354. 'where' => [
  10355. 'ip.visibility != ? AND ' => 2,
  10356. 'ip.tool = ? AND ' => TOOL_FORUM,
  10357. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10358. 'f.lp_id = ?' => intval($this->lp_id)
  10359. ]
  10360. ],
  10361. 'first'
  10362. );
  10363. return $resultData['qty'] > 0;
  10364. }
  10365. /**
  10366. * Get the forum for this learning path
  10367. * @param int $sessionId
  10368. * @return boolean
  10369. */
  10370. public function getForum($sessionId = 0)
  10371. {
  10372. $forumTable = Database::get_course_table(TABLE_FORUM);
  10373. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10374. $fakeFrom = "$forumTable f
  10375. INNER JOIN $itemProperty ip ";
  10376. if ($this->lp_session_id == 0) {
  10377. $fakeFrom .= "
  10378. ON (
  10379. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  10380. f.session_id = ip.session_id OR ip.session_id IS NULL
  10381. )
  10382. )
  10383. ";
  10384. } else {
  10385. $fakeFrom .= "
  10386. ON (
  10387. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  10388. )
  10389. ";
  10390. }
  10391. $resultData = Database::select(
  10392. 'f.*',
  10393. $fakeFrom,
  10394. [
  10395. 'where' => [
  10396. 'ip.visibility != ? AND ' => 2,
  10397. 'ip.tool = ? AND ' => TOOL_FORUM,
  10398. 'f.session_id = ? AND ' => $sessionId,
  10399. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10400. 'f.lp_id = ?' => intval($this->lp_id)
  10401. ]
  10402. ],
  10403. 'first'
  10404. );
  10405. if (empty($resultData)) {
  10406. return false;
  10407. }
  10408. return $resultData;
  10409. }
  10410. /**
  10411. * Create a forum for this learning path
  10412. * @param type $forumCategoryId
  10413. * @return int The forum ID if was created. Otherwise return false
  10414. */
  10415. public function createForum($forumCategoryId)
  10416. {
  10417. require_once api_get_path(SYS_CODE_PATH).'/forum/forumfunction.inc.php';
  10418. $forumId = store_forum(
  10419. [
  10420. 'lp_id' => $this->lp_id,
  10421. 'forum_title' => $this->name,
  10422. 'forum_comment' => null,
  10423. 'forum_category' => intval($forumCategoryId),
  10424. 'students_can_edit_group' => ['students_can_edit' => 0],
  10425. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  10426. 'default_view_type_group' => ['default_view_type' => 'flat'],
  10427. 'group_forum' => 0,
  10428. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  10429. ],
  10430. [],
  10431. true
  10432. );
  10433. return $forumId;
  10434. }
  10435. /**
  10436. * Check and obtain the lp final item if exist
  10437. *
  10438. * @return learnpathItem
  10439. */
  10440. private function getFinalItem()
  10441. {
  10442. if (empty($this->items)) {
  10443. return null;
  10444. }
  10445. foreach ($this->items as $item) {
  10446. if ($item->type !== 'final_item') {
  10447. continue;
  10448. }
  10449. return $item;
  10450. }
  10451. }
  10452. /**
  10453. * Get the LP Final Item Template
  10454. *
  10455. * @return string
  10456. */
  10457. private function getFinalItemTemplate()
  10458. {
  10459. return file_get_contents(api_get_path(SYS_CODE_PATH).'lp/final_item_template/template.html');
  10460. }
  10461. /**
  10462. * Get the LP Final Item Url
  10463. *
  10464. * @return string
  10465. */
  10466. private function getSavedFinalItem()
  10467. {
  10468. $finalItem = $this->getFinalItem();
  10469. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10470. if ($doc && file_exists($doc['absolute_path'])) {
  10471. return file_get_contents($doc['absolute_path']);
  10472. }
  10473. return '';
  10474. }
  10475. /**
  10476. * Get the LP Final Item form
  10477. *
  10478. * @return string
  10479. */
  10480. public function getFinalItemForm()
  10481. {
  10482. $finalItem = $this->getFinalItem();
  10483. $title = '';
  10484. if ($finalItem) {
  10485. $title = $finalItem->get_title();
  10486. $buttonText = get_lang('Save');
  10487. $content = $this->getSavedFinalItem();
  10488. } else {
  10489. $buttonText = get_lang('LPCreateDocument');
  10490. $content = $this->getFinalItemTemplate();
  10491. }
  10492. $courseInfo = api_get_course_info();
  10493. $result = $this->generate_lp_folder($courseInfo);
  10494. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10495. $relative_prefix = '../../';
  10496. $editorConfig = [
  10497. 'ToolbarSet' => 'LearningPathDocuments',
  10498. 'Width' => '100%',
  10499. 'Height' => '500',
  10500. 'FullPage' => true,
  10501. 'CreateDocumentDir' => $relative_prefix,
  10502. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  10503. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  10504. ];
  10505. $url = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  10506. 'type' => 'document',
  10507. 'lp_id' => $this->lp_id
  10508. ]);
  10509. $form = new FormValidator('final_item', 'POST', $url);
  10510. $form->addText('title', get_lang('Title'));
  10511. $form->addButtonSave($buttonText);
  10512. $form->addHtml(
  10513. Display::return_message(
  10514. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10515. 'normal',
  10516. false
  10517. )
  10518. );
  10519. $renderer = $form->defaultRenderer();
  10520. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10521. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10522. $form->addHidden('action', 'add_final_item');
  10523. $form->addHidden('path', Session::read('pathItem'));
  10524. $form->addHidden('previous', $this->get_last());
  10525. $form->setDefaults(
  10526. ['title' => $title, 'content_lp_certificate' => $content]
  10527. );
  10528. if ($form->validate()) {
  10529. $values = $form->exportValues();
  10530. $lastItemId = $this->get_last();
  10531. if (!$finalItem) {
  10532. $documentId = $this->create_document(
  10533. $this->course_info,
  10534. $values['content_lp_certificate'],
  10535. $values['title']
  10536. );
  10537. $this->add_item(
  10538. 0,
  10539. $lastItemId,
  10540. 'final_item',
  10541. $documentId,
  10542. $values['title'],
  10543. ''
  10544. );
  10545. Display::addFlash(
  10546. Display::return_message(get_lang('Added'))
  10547. );
  10548. } else {
  10549. $this->edit_document($this->course_info);
  10550. }
  10551. }
  10552. return $form->returnForm();
  10553. }
  10554. /**
  10555. * Check if the current lp item is first, both, last or none from lp list
  10556. *
  10557. * @param int $currentItemId
  10558. * @return string
  10559. */
  10560. public function isFirstOrLastItem($currentItemId)
  10561. {
  10562. if ($this->debug > 0) {
  10563. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10564. }
  10565. $lpItemId = [];
  10566. $typeListNotToVerify = self::getChapterTypes();
  10567. // Using get_toc() function instead $this->items because returns the correct order of the items
  10568. foreach ($this->get_toc() as $item) {
  10569. if (!in_array($item['type'], $typeListNotToVerify)) {
  10570. $lpItemId[] = $item['id'];
  10571. }
  10572. }
  10573. $lastLpItemIndex = count($lpItemId) - 1;
  10574. $position = array_search($currentItemId, $lpItemId);
  10575. switch ($position) {
  10576. case 0:
  10577. if (!$lastLpItemIndex) {
  10578. $answer = 'both';
  10579. break;
  10580. }
  10581. $answer = 'first';
  10582. break;
  10583. case $lastLpItemIndex:
  10584. $answer = 'last';
  10585. break;
  10586. default:
  10587. $answer = 'none';
  10588. }
  10589. return $answer;
  10590. }
  10591. /**
  10592. * Get whether this is a learning path with the accumulated SCORM time or not
  10593. * @return int
  10594. */
  10595. public function getAccumulateScormTime()
  10596. {
  10597. return $this->accumulateScormTime;
  10598. }
  10599. /**
  10600. * Set whether this is a learning path with the accumulated SCORM time or not
  10601. * @param int $value (0 = false, 1 = true)
  10602. * @return bool Always returns true
  10603. */
  10604. public function setAccumulateScormTime($value)
  10605. {
  10606. if ($this->debug > 0) {
  10607. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10608. }
  10609. $this->accumulateScormTime = intval($value);
  10610. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10611. $lp_id = $this->get_id();
  10612. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10613. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10614. Database::query($sql);
  10615. return true;
  10616. }
  10617. /**
  10618. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10619. * the new learning path tool.
  10620. *
  10621. * The function is a big switch on tool type.
  10622. * In each case, we query the corresponding table for information and build the link
  10623. * with that information.
  10624. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on
  10625. * previous work (display_addedresource_link_in_learnpath())
  10626. * @param int $course_id Course code
  10627. * @param int $learningPathId The learning path ID (in lp table)
  10628. * @param int $id_in_path the unique index in the items table
  10629. * @param int $lpViewId
  10630. * @param string $origin
  10631. * @return string
  10632. */
  10633. public static function rl_get_resource_link_for_learnpath(
  10634. $course_id,
  10635. $learningPathId,
  10636. $id_in_path,
  10637. $lpViewId,
  10638. $origin = 'learnpath'
  10639. ) {
  10640. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10641. $course_info = api_get_course_info_by_id($course_id);
  10642. $course_id = $course_info['real_id'];
  10643. $course_code = $course_info['code'];
  10644. $session_id = api_get_session_id();
  10645. $learningPathId = intval($learningPathId);
  10646. $id_in_path = intval($id_in_path);
  10647. $lpViewId = intval($lpViewId);
  10648. $em = Database::getManager();
  10649. $sql = "SELECT * FROM $tbl_lp_item
  10650. WHERE
  10651. c_id = $course_id AND
  10652. lp_id = $learningPathId AND
  10653. id = $id_in_path
  10654. ";
  10655. $res_item = Database::query($sql);
  10656. if (Database::num_rows($res_item) < 1) {
  10657. return -1;
  10658. }
  10659. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10660. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10661. // Get the lp_item_view with the highest view_count.
  10662. $sql = "SELECT * FROM $item_view_table
  10663. WHERE
  10664. c_id = $course_id AND
  10665. lp_item_id = ".$row_item['id']." AND
  10666. lp_view_id = $lpViewId
  10667. ORDER BY view_count DESC";
  10668. $learnpathItemViewResult = Database::query($sql);
  10669. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10670. $learnpathItemViewId = 0;
  10671. if (isset($learnpathItemViewData)) {
  10672. $learnpathItemViewId = $learnpathItemViewData['id'];
  10673. }
  10674. $type = strtolower($row_item['item_type']);
  10675. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10676. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10677. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10678. $link = '';
  10679. $extraParams = "origin=$origin&cidReq=$course_code&session_id=$session_id&id_session=$session_id";
  10680. switch ($type) {
  10681. case 'dir':
  10682. $link .= $main_dir_path.'lp/blank.php';
  10683. break;
  10684. case TOOL_CALENDAR_EVENT:
  10685. $link .= $main_dir_path.'calendar/agenda.php?agenda_id='.$id.'&'.$extraParams;
  10686. break;
  10687. case TOOL_ANNOUNCEMENT:
  10688. $link .= $main_dir_path.'announcements/announcements.php?ann_id='.$id.'&'.$extraParams;
  10689. break;
  10690. case TOOL_LINK:
  10691. $link .= $main_dir_path.'link/link_goto.php?link_id='.$id.'&'.$extraParams;
  10692. break;
  10693. case TOOL_QUIZ:
  10694. if (!empty($id)) {
  10695. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10696. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10697. $result = Database::query($sql);
  10698. $myrow = Database::fetch_array($result);
  10699. if ($row_item['title'] != '') {
  10700. $myrow['title'] = $row_item['title'];
  10701. }
  10702. $link .= $main_dir_path.'exercise/overview.php?lp_init=1&learnpath_item_view_id='.$learnpathItemViewId.'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id.'&'.$extraParams;
  10703. }
  10704. break;
  10705. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  10706. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10707. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10708. $myrow = Database::fetch_array($result);
  10709. $path = $myrow['path'];
  10710. $link .= $main_dir_path.'exercise/showinframes.php?file='.$path.'&cid='.$course_code.'&uid='.api_get_user_id().'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId.'&'.$extraParams;
  10711. break;
  10712. case TOOL_FORUM:
  10713. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&'.$extraParams;
  10714. break;
  10715. case TOOL_THREAD: //forum post
  10716. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10717. if (!empty($id)) {
  10718. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10719. $result = Database::query($sql);
  10720. $myrow = Database::fetch_array($result);
  10721. $link .= $main_dir_path.'forum/viewthread.php?thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10722. }
  10723. break;
  10724. case TOOL_POST:
  10725. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10726. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10727. $myrow = Database::fetch_array($result);
  10728. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10729. break;
  10730. case TOOL_DOCUMENT:
  10731. $document = $em
  10732. ->getRepository('ChamiloCourseBundle:CDocument')
  10733. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10734. if (!$document) {
  10735. break;
  10736. }
  10737. $documentPathInfo = pathinfo($document->getPath());
  10738. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10739. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10740. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10741. if ($showDirectUrl) {
  10742. $link = $main_course_path.'document'.$document->getPath().'?'.$extraParams;
  10743. } else {
  10744. $link = api_get_path(WEB_CODE_PATH).'document/showinframes.php?id='.$id.'&'.$extraParams;
  10745. }
  10746. $openmethod = 2;
  10747. $officedoc = false;
  10748. Session::write('openmethod', $openmethod);
  10749. Session::write('officedoc', $officedoc);
  10750. break;
  10751. case TOOL_LP_FINAL_ITEM:
  10752. $link .= api_get_path(WEB_CODE_PATH).'lp/lp_final_item.php?&id='.$id.'&lp_id='.$learningPathId.'&'.$extraParams;
  10753. break;
  10754. case 'assignments':
  10755. $link .= $main_dir_path.'work/work.php?'.$extraParams;
  10756. break;
  10757. case TOOL_DROPBOX:
  10758. $link .= $main_dir_path.'dropbox/index.php?'.$extraParams;
  10759. break;
  10760. case 'introduction_text': //DEPRECATED
  10761. $link .= '';
  10762. break;
  10763. case TOOL_COURSE_DESCRIPTION:
  10764. $link .= $main_dir_path.'course_description?'.$extraParams;
  10765. break;
  10766. case TOOL_GROUP:
  10767. $link .= $main_dir_path.'group/group.php?'.$extraParams;
  10768. break;
  10769. case TOOL_USER:
  10770. $link .= $main_dir_path.'user/user.php?'.$extraParams;
  10771. break;
  10772. case TOOL_STUDENTPUBLICATION:
  10773. if (!empty($row_item['path'])) {
  10774. $link .= $main_dir_path.'work/work_list.php?id='.$row_item['path'].'&'.$extraParams;
  10775. break;
  10776. }
  10777. $link .= $main_dir_path.'work/work.php?'.api_get_cidreq().'&id='.$row_item['path'].'&'.$extraParams;
  10778. break;
  10779. } //end switch
  10780. return $link;
  10781. }
  10782. /**
  10783. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10784. *
  10785. * @author Yannick Warnier <ywarnier@beeznest.org>
  10786. * @param string Course code
  10787. * @param string The tool type (using constants declared in main_api.lib.php)
  10788. * @param integer The resource ID
  10789. * @return string
  10790. */
  10791. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10792. {
  10793. $_course = api_get_course_info($course_code);
  10794. $course_id = $_course['real_id'];
  10795. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10796. $learningPathId = intval($learningPathId);
  10797. $id_in_path = intval($id_in_path);
  10798. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10799. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10800. $res_item = Database::query($sql_item);
  10801. if (Database::num_rows($res_item) < 1) {
  10802. return '';
  10803. }
  10804. $row_item = Database::fetch_array($res_item);
  10805. $type = strtolower($row_item['item_type']);
  10806. $id = $row_item['ref'];
  10807. $output = '';
  10808. switch ($type) {
  10809. case TOOL_CALENDAR_EVENT:
  10810. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10811. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10812. $myrow = Database::fetch_array($result);
  10813. $output = $myrow['title'];
  10814. break;
  10815. case TOOL_ANNOUNCEMENT:
  10816. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10817. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10818. $myrow = Database::fetch_array($result);
  10819. $output = $myrow['title'];
  10820. break;
  10821. case TOOL_LINK:
  10822. // Doesn't take $target into account.
  10823. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10824. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10825. $myrow = Database::fetch_array($result);
  10826. $output = $myrow['title'];
  10827. break;
  10828. case TOOL_QUIZ:
  10829. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10830. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10831. $myrow = Database::fetch_array($result);
  10832. $output = $myrow['title'];
  10833. break;
  10834. case TOOL_FORUM:
  10835. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10836. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10837. $myrow = Database::fetch_array($result);
  10838. $output = $myrow['forum_name'];
  10839. break;
  10840. case TOOL_THREAD: //=topics
  10841. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10842. // Grabbing the title of the post.
  10843. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10844. $result_title = Database::query($sql_title);
  10845. $myrow_title = Database::fetch_array($result_title);
  10846. $output = $myrow_title['post_title'];
  10847. break;
  10848. case TOOL_POST:
  10849. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10850. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10851. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10852. $result = Database::query($sql);
  10853. $post = Database::fetch_array($result);
  10854. $output = $post['post_title'];
  10855. break;
  10856. case 'dir':
  10857. $title = $row_item['title'];
  10858. if (!empty($title)) {
  10859. $output = $title;
  10860. } else {
  10861. $output = '-';
  10862. }
  10863. break;
  10864. case TOOL_DOCUMENT:
  10865. $title = $row_item['title'];
  10866. if (!empty($title)) {
  10867. $output = $title;
  10868. } else {
  10869. $output = '-';
  10870. }
  10871. break;
  10872. case 'hotpotatoes':
  10873. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10874. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10875. $myrow = Database::fetch_array($result);
  10876. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10877. $last = count($pathname) - 1; // Making a correct name for the link.
  10878. $filename = $pathname[$last]; // Making a correct name for the link.
  10879. $ext = explode('.', $filename);
  10880. $ext = strtolower($ext[sizeof($ext) - 1]);
  10881. $myrow['path'] = rawurlencode($myrow['path']);
  10882. $output = $filename;
  10883. break;
  10884. }
  10885. return stripslashes($output);
  10886. }
  10887. }
  10888. if (!function_exists('trim_value')) {
  10889. function trim_value(& $value) {
  10890. $value = trim($value);
  10891. }
  10892. }