learnpath.class.php 487 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944
  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. /**
  12. * Class learnpath
  13. * This class defines the parent attributes and methods for Chamilo learnpaths
  14. * and SCORM learnpaths. It is used by the scorm class.
  15. *
  16. * @package chamilo.learnpath
  17. * @author Yannick Warnier <ywarnier@beeznest.org>
  18. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  19. */
  20. class learnpath
  21. {
  22. public $attempt = 0; // The number for the current ID view.
  23. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  24. public $current; // Id of the current item the user is viewing.
  25. public $current_score; // The score of the current item.
  26. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  27. public $current_time_stop; // The time the user closed this resource.
  28. public $default_status = 'not attempted';
  29. public $encoding = 'UTF-8';
  30. public $error = '';
  31. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  32. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  33. public $index; // The index of the active learnpath_item in $ordered_items array.
  34. public $items = array();
  35. public $last; // item_id of last item viewed in the learning path.
  36. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  37. public $license; // Which license this course has been given - not used yet on 20060522.
  38. public $lp_id; // DB ID for this learnpath.
  39. public $lp_view_id; // DB ID for lp_view
  40. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  41. public $message = '';
  42. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  43. public $name; // Learnpath name (they generally have one).
  44. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  45. public $path = ''; // Path inside the scorm directory (if scorm).
  46. public $theme; // The current theme of the learning path.
  47. public $preview_image; // The current image of the learning path.
  48. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  49. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  50. public $prevent_reinit = 1;
  51. // Describes the mode of progress bar display.
  52. public $seriousgame_mode = 0;
  53. public $progress_bar_mode = '%';
  54. // Percentage progress as saved in the db.
  55. public $progress_db = '0';
  56. public $proximity; // Wether the content is distant or local or unknown.
  57. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  58. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  59. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  60. public $type; //type of learnpath. Could be 'chamilo', 'scorm', 'scorm2004', 'aicc', ...
  61. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  62. public $user_id; //ID of the user that is viewing/using the course
  63. public $update_queue = array();
  64. public $scorm_debug = 0;
  65. public $arrMenu = array(); // Array for the menu items.
  66. public $debug = 0; // Logging level.
  67. public $lp_session_id = 0;
  68. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  69. public $prerequisite = 0;
  70. public $use_max_score = 1; // 1 or 0
  71. public $subscribeUsers = 0; // Subscribe users or not
  72. public $created_on = '';
  73. public $modified_on = '';
  74. public $publicated_on = '';
  75. public $expired_on = '';
  76. public $ref = null;
  77. public $course_int_id;
  78. public $course_info = array();
  79. public $categoryId;
  80. /**
  81. * Constructor.
  82. * Needs a database handler, a course code and a learnpath id from the database.
  83. * Also builds the list of items into $this->items.
  84. * @param string $course Course code
  85. * @param integer $lp_id
  86. * @param integer $user_id
  87. */
  88. public function __construct($course, $lp_id, $user_id)
  89. {
  90. $this->encoding = api_get_system_encoding();
  91. if ($this->debug > 0) {
  92. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  93. }
  94. if (empty($course)) {
  95. $course = api_get_course_id();
  96. }
  97. $course_info = api_get_course_info($course);
  98. if (!empty($course_info)) {
  99. $this->cc = $course_info['code'];
  100. $this->course_info = $course_info;
  101. $course_id = $course_info['real_id'];
  102. } else {
  103. $this->error = 'Course code does not exist in database.';
  104. }
  105. $this->set_course_int_id($course_id);
  106. $lp_id = (int) $lp_id;
  107. // Check learnpath ID.
  108. if (empty($lp_id)) {
  109. $this->error = 'Learnpath ID is empty';
  110. } else {
  111. // TODO: Make it flexible to use any course_code (still using env course code here).
  112. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  113. $sql = "SELECT * FROM $lp_table
  114. WHERE id = '$lp_id' AND c_id = $course_id";
  115. if ($this->debug > 2) {
  116. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  117. }
  118. $res = Database::query($sql);
  119. if (Database::num_rows($res) > 0) {
  120. $this->lp_id = $lp_id;
  121. $row = Database::fetch_array($res);
  122. $this->type = $row['lp_type'];
  123. $this->name = stripslashes($row['name']);
  124. $this->proximity = $row['content_local'];
  125. $this->theme = $row['theme'];
  126. $this->maker = $row['content_maker'];
  127. $this->prevent_reinit = $row['prevent_reinit'];
  128. $this->seriousgame_mode = $row['seriousgame_mode'];
  129. $this->license = $row['content_license'];
  130. $this->scorm_debug = $row['debug'];
  131. $this->js_lib = $row['js_lib'];
  132. $this->path = $row['path'];
  133. $this->preview_image = $row['preview_image'];
  134. $this->author = $row['author'];
  135. $this->hide_toc_frame = $row['hide_toc_frame'];
  136. $this->lp_session_id = $row['session_id'];
  137. $this->use_max_score = $row['use_max_score'];
  138. $this->subscribeUsers = $row['subscribe_users'];
  139. $this->created_on = $row['created_on'];
  140. $this->modified_on = $row['modified_on'];
  141. $this->ref = $row['ref'];
  142. $this->categoryId = $row['category_id'];
  143. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  144. if (!empty($row['publicated_on'])) {
  145. $this->publicated_on = $row['publicated_on'];
  146. }
  147. if (!empty($row['expired_on'])) {
  148. $this->expired_on = $row['expired_on'];
  149. }
  150. if ($this->type == 2) {
  151. if ($row['force_commit'] == 1) {
  152. $this->force_commit = true;
  153. }
  154. }
  155. $this->mode = $row['default_view_mod'];
  156. // Check user ID.
  157. if (empty($user_id)) {
  158. $this->error = 'User ID is empty';
  159. } else {
  160. $user_info = api_get_user_info($user_id);
  161. if (!empty($user_info)) {
  162. $this->user_id = $user_info['user_id'];
  163. } else {
  164. $this->error = 'User ID does not exist in database ('.$sql.')';
  165. }
  166. }
  167. // End of variables checking.
  168. $session_id = api_get_session_id();
  169. // Get the session condition for learning paths of the base + session.
  170. $session = api_get_session_condition($session_id);
  171. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  172. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  173. // Selecting by view_count descending allows to get the highest view_count first.
  174. $sql = "SELECT * FROM $lp_table
  175. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  176. ORDER BY view_count DESC";
  177. $res = Database::query($sql);
  178. if ($this->debug > 2) {
  179. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  180. }
  181. if (Database::num_rows($res) > 0) {
  182. if ($this->debug > 2) {
  183. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  184. }
  185. $row = Database::fetch_array($res);
  186. $this->attempt = $row['view_count'];
  187. $this->lp_view_id = $row['id'];
  188. $this->last_item_seen = $row['last_item'];
  189. $this->progress_db = $row['progress'];
  190. $this->lp_view_session_id = $row['session_id'];
  191. } else if (!api_is_invitee()) {
  192. if ($this->debug > 2) {
  193. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  194. }
  195. $this->attempt = 1;
  196. $params = [
  197. 'c_id' => $course_id,
  198. 'lp_id' => $lp_id,
  199. 'user_id' => $user_id,
  200. 'view_count' => 1,
  201. 'session_id' => $session_id,
  202. 'last_item' => 0
  203. ];
  204. $this->lp_view_id = Database::insert($lp_table, $params);
  205. if (!empty($this->lp_view_id)) {
  206. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  207. Database::query($sql);
  208. }
  209. }
  210. // Initialise items.
  211. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  212. $sql = "SELECT * FROM $lp_item_table
  213. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  214. ORDER BY parent_item_id, display_order";
  215. $res = Database::query($sql);
  216. if ($this->debug > 2) {
  217. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  218. error_log('-- Start while--', 0);
  219. }
  220. $lp_item_id_list = array();
  221. while ($row = Database::fetch_array($res)) {
  222. $lp_item_id_list[] = $row['id'];
  223. switch ($this->type) {
  224. case 3: //aicc
  225. $oItem = new aiccItem('db', $row['id'], $course_id);
  226. if (is_object($oItem)) {
  227. $my_item_id = $oItem->get_id();
  228. $oItem->set_lp_view($this->lp_view_id, $course_id);
  229. $oItem->set_prevent_reinit($this->prevent_reinit);
  230. // Don't use reference here as the next loop will make the pointed object change.
  231. $this->items[$my_item_id] = $oItem;
  232. $this->refs_list[$oItem->ref] = $my_item_id;
  233. if ($this->debug > 2) {
  234. error_log(
  235. 'New LP - learnpath::__construct() - ' .
  236. 'aicc object with id ' . $my_item_id .
  237. ' set in items[]',
  238. 0
  239. );
  240. }
  241. }
  242. break;
  243. case 2:
  244. $oItem = new scormItem('db', $row['id'], $course_id);
  245. if (is_object($oItem)) {
  246. $my_item_id = $oItem->get_id();
  247. $oItem->set_lp_view($this->lp_view_id, $course_id);
  248. $oItem->set_prevent_reinit($this->prevent_reinit);
  249. // Don't use reference here as the next loop will make the pointed object change.
  250. $this->items[$my_item_id] = $oItem;
  251. $this->refs_list[$oItem->ref] = $my_item_id;
  252. if ($this->debug > 2) {
  253. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  254. }
  255. }
  256. break;
  257. case 1:
  258. default:
  259. if ($this->debug > 2) {
  260. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  261. }
  262. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  263. if ($this->debug > 2) {
  264. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  265. }
  266. if (is_object($oItem)) {
  267. $my_item_id = $oItem->get_id();
  268. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  269. $oItem->set_prevent_reinit($this->prevent_reinit);
  270. // Don't use reference here as the next loop will make the pointed object change.
  271. $this->items[$my_item_id] = $oItem;
  272. $this->refs_list[$my_item_id] = $my_item_id;
  273. if ($this->debug > 2) {
  274. error_log(
  275. 'New LP - learnpath::__construct() ' . __LINE__ .
  276. ' - object with id ' . $my_item_id . ' set in items[]',
  277. 0
  278. );
  279. }
  280. }
  281. break;
  282. }
  283. // Setting the object level with variable $this->items[$i][parent]
  284. foreach ($this->items as $itemLPObject) {
  285. $level = self::get_level_for_item($this->items, $itemLPObject->db_id);
  286. $itemLPObject->level = $level;
  287. }
  288. // Setting the view in the item object.
  289. if (is_object($this->items[$row['id']])) {
  290. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  291. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  292. $this->items[$row['id']]->current_start_time = 0;
  293. $this->items[$row['id']]->current_stop_time = 0;
  294. }
  295. }
  296. }
  297. if ($this->debug > 2) {
  298. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  299. }
  300. if (!empty($lp_item_id_list)) {
  301. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  302. if (!empty($lp_item_id_list_to_string)) {
  303. // Get last viewing vars.
  304. $itemViewTable = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $itemViewTable
  308. WHERE
  309. c_id = $course_id AND
  310. lp_view_id = ".$this->lp_view_id." AND
  311. lp_item_id IN ('".$lp_item_id_list_to_string."')
  312. ORDER BY view_count DESC ";
  313. if ($this->debug > 2) {
  314. error_log(
  315. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  316. 0
  317. );
  318. }
  319. $status_list = array();
  320. $res = Database::query($sql);
  321. while ($row = Database:: fetch_array($res)) {
  322. $status_list[$row['lp_item_id']] = $row['status'];
  323. }
  324. foreach ($lp_item_id_list as $item_id) {
  325. if (isset($status_list[$item_id])) {
  326. $status = $status_list[$item_id];
  327. if (is_object($this->items[$item_id])) {
  328. $this->items[$item_id]->set_status($status);
  329. if (empty($status)) {
  330. $this->items[$item_id]->set_status(
  331. $this->default_status
  332. );
  333. }
  334. }
  335. } else {
  336. if (!api_is_invitee()) {
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. if (!empty($this->lp_view_id)) {
  343. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  344. $params = [
  345. 'c_id' => $course_id,
  346. 'lp_item_id' => $course_id,
  347. 'lp_view_id' => $this->lp_view_id,
  348. 'view_count' => 1,
  349. 'status' => 'not attempted',
  350. 'start_time' => api_get_utc_datetime(),
  351. 'total_time' => 0,
  352. 'score' => 0
  353. ];
  354. $insertId = Database::insert($itemViewTable, $params);
  355. if ($insertId) {
  356. $sql = "UPDATE $itemViewTable SET id = iid
  357. WHERE iid = $insertId";
  358. Database::query($sql);
  359. }
  360. $this->items[$item_id]->set_lp_view(
  361. $this->lp_view_id,
  362. $course_id
  363. );
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. $this->ordered_items = self::get_flat_ordered_items_list(
  371. $this->get_id(),
  372. 0,
  373. $course_id
  374. );
  375. $this->max_ordered_items = 0;
  376. foreach ($this->ordered_items as $index => $dummy) {
  377. if ($index > $this->max_ordered_items && !empty($dummy)) {
  378. $this->max_ordered_items = $index;
  379. }
  380. }
  381. // TODO: Define the current item better.
  382. $this->first();
  383. if ($this->debug > 2) {
  384. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  385. }
  386. } else {
  387. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  388. }
  389. }
  390. }
  391. /**
  392. * @return string
  393. */
  394. public function getCourseCode()
  395. {
  396. return $this->cc;
  397. }
  398. /**
  399. * @return int
  400. */
  401. public function get_course_int_id()
  402. {
  403. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  404. }
  405. /**
  406. * @param $course_id
  407. * @return int
  408. */
  409. public function set_course_int_id($course_id)
  410. {
  411. return $this->course_int_id = (int) $course_id;
  412. }
  413. /**
  414. * Get the depth level of LP item
  415. * @param array $items
  416. * @param int $currentItemId
  417. * @return int
  418. */
  419. private static function get_level_for_item($items, $currentItemId)
  420. {
  421. $parentItemId = $items[$currentItemId]->parent;
  422. if ($parentItemId == 0) {
  423. return 0;
  424. } else {
  425. return self::get_level_for_item($items, $parentItemId) + 1;
  426. }
  427. }
  428. /**
  429. * Function rewritten based on old_add_item() from Yannick Warnier.
  430. * Due the fact that users can decide where the item should come, I had to overlook this function and
  431. * I found it better to rewrite it. Old function is still available.
  432. * Added also the possibility to add a description.
  433. *
  434. * @param int $parent
  435. * @param int $previous
  436. * @param string $type
  437. * @param int $id resource ID (ref)
  438. * @param string $title
  439. * @param string $description
  440. * @param int $prerequisites
  441. * @param int $max_time_allowed
  442. * @param int $userId
  443. *
  444. * @return int
  445. */
  446. public function add_item(
  447. $parent,
  448. $previous,
  449. $type = 'dir',
  450. $id,
  451. $title,
  452. $description,
  453. $prerequisites = 0,
  454. $max_time_allowed = 0,
  455. $userId = 0
  456. ) {
  457. $course_id = $this->course_info['real_id'];
  458. if ($this->debug > 0) {
  459. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  460. }
  461. if (empty($course_id)) {
  462. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  463. $this->course_info = api_get_course_info($this->cc);
  464. $course_id = $this->course_info['real_id'];
  465. }
  466. $userId = empty($userId) ? api_get_user_id() : $userId;
  467. $sessionId = api_get_session_id();
  468. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  469. $_course = $this->course_info;
  470. $parent = intval($parent);
  471. $previous = intval($previous);
  472. $id = intval($id);
  473. $max_time_allowed = htmlentities($max_time_allowed);
  474. if (empty($max_time_allowed)) {
  475. $max_time_allowed = 0;
  476. }
  477. $sql = "SELECT COUNT(id) AS num
  478. FROM $tbl_lp_item
  479. WHERE
  480. c_id = $course_id AND
  481. lp_id = " . $this->get_id() . " AND
  482. parent_item_id = " . $parent;
  483. $res_count = Database::query($sql);
  484. $row = Database::fetch_array($res_count);
  485. $num = $row['num'];
  486. if ($num > 0) {
  487. if (empty($previous)) {
  488. $sql = "SELECT id, next_item_id, display_order
  489. FROM " . $tbl_lp_item . "
  490. WHERE
  491. c_id = $course_id AND
  492. lp_id = " . $this->get_id() . " AND
  493. parent_item_id = " . $parent . " AND
  494. previous_item_id = 0 OR
  495. previous_item_id=" . $parent;
  496. $result = Database::query($sql);
  497. $row = Database::fetch_array($result);
  498. $tmp_previous = 0;
  499. $next = $row['id'];
  500. $display_order = 0;
  501. } else {
  502. $previous = (int) $previous;
  503. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  504. FROM $tbl_lp_item
  505. WHERE
  506. c_id = $course_id AND
  507. lp_id = " . $this->get_id() . " AND
  508. id = " . $previous;
  509. $result = Database::query($sql);
  510. $row = Database:: fetch_array($result);
  511. $tmp_previous = $row['id'];
  512. $next = $row['next_item_id'];
  513. $display_order = $row['display_order'];
  514. }
  515. } else {
  516. $tmp_previous = 0;
  517. $next = 0;
  518. $display_order = 0;
  519. }
  520. $id = intval($id);
  521. $typeCleaned = Database::escape_string($type);
  522. if ($type == 'quiz') {
  523. $sql = 'SELECT SUM(ponderation)
  524. FROM ' . Database::get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  525. INNER JOIN ' . Database::get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  526. ON
  527. quiz_question.id = quiz_rel_question.question_id AND
  528. quiz_question.c_id = quiz_rel_question.c_id
  529. WHERE
  530. quiz_rel_question.exercice_id = '.$id." AND
  531. quiz_question.c_id = $course_id AND
  532. quiz_rel_question.c_id = $course_id ";
  533. $rsQuiz = Database::query($sql);
  534. $max_score = Database::result($rsQuiz, 0, 0);
  535. // Disabling the exercise if we add it inside a LP
  536. $exercise = new Exercise($course_id);
  537. $exercise->read($id);
  538. $exercise->disable();
  539. $exercise->save();
  540. } else {
  541. $max_score = 100;
  542. }
  543. $params = array(
  544. "c_id" => $course_id,
  545. "lp_id" => $this->get_id(),
  546. "item_type" => $typeCleaned,
  547. "ref" => '',
  548. "title" => $title,
  549. "description" => $description,
  550. "path" => $id,
  551. "max_score" => $max_score,
  552. "parent_item_id" => $parent,
  553. "previous_item_id" => $previous,
  554. "next_item_id" => intval($next),
  555. "display_order" => $display_order + 1,
  556. "prerequisite" => $prerequisites,
  557. "max_time_allowed" => $max_time_allowed,
  558. 'min_score' => 0,
  559. 'launch_data' => ''
  560. );
  561. if ($prerequisites != 0) {
  562. $params['prerequisite'] = $prerequisites;
  563. }
  564. $new_item_id = Database::insert($tbl_lp_item, $params);
  565. if ($this->debug > 2) {
  566. error_log('New LP - Inserting dir/chapter: ' . $new_item_id, 0);
  567. }
  568. if ($new_item_id) {
  569. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. $sql = "UPDATE $tbl_lp_item
  572. SET previous_item_id = $new_item_id
  573. WHERE c_id = $course_id AND id = $next";
  574. Database::query($sql);
  575. // Update the item that should be before the new item.
  576. $sql = "UPDATE $tbl_lp_item
  577. SET next_item_id = $new_item_id
  578. WHERE c_id = $course_id AND id = $tmp_previous";
  579. Database::query($sql);
  580. // Update all the items after the new item.
  581. $sql = "UPDATE " . $tbl_lp_item . "
  582. SET display_order = display_order + 1
  583. WHERE
  584. c_id = $course_id AND
  585. lp_id = " . $this->get_id() . " AND
  586. id <> " . $new_item_id . " AND
  587. parent_item_id = " . $parent . " AND
  588. display_order > " . $display_order;
  589. Database::query($sql);
  590. // Update the item that should come after the new item.
  591. $sql = "UPDATE " . $tbl_lp_item . "
  592. SET ref = " . $new_item_id . "
  593. WHERE c_id = $course_id AND id = " . $new_item_id;
  594. Database::query($sql);
  595. // Upload audio.
  596. if (!empty($_FILES['mp3']['name'])) {
  597. // Create the audio folder if it does not exist yet.
  598. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  599. if (!is_dir($filepath . 'audio')) {
  600. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  601. $audio_id = add_document(
  602. $_course,
  603. '/audio',
  604. 'folder',
  605. 0,
  606. 'audio',
  607. '',
  608. 0,
  609. true,
  610. null,
  611. $sessionId,
  612. $userId
  613. );
  614. api_item_property_update(
  615. $_course,
  616. TOOL_DOCUMENT,
  617. $audio_id,
  618. 'FolderCreated',
  619. $userId,
  620. null,
  621. null,
  622. null,
  623. null,
  624. $sessionId
  625. );
  626. api_item_property_update(
  627. $_course,
  628. TOOL_DOCUMENT,
  629. $audio_id,
  630. 'invisible',
  631. $userId,
  632. null,
  633. null,
  634. null,
  635. null,
  636. $sessionId
  637. );
  638. }
  639. $file_path = handle_uploaded_document(
  640. $_course,
  641. $_FILES['mp3'],
  642. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  643. '/audio',
  644. $userId,
  645. '',
  646. '',
  647. '',
  648. '',
  649. false
  650. );
  651. // Getting the filename only.
  652. $file_components = explode('/', $file_path);
  653. $file = $file_components[count($file_components) - 1];
  654. // Store the mp3 file in the lp_item table.
  655. $sql = "UPDATE $tbl_lp_item SET
  656. audio = '" . Database::escape_string($file) . "'
  657. WHERE id = '" . intval($new_item_id) . "'";
  658. Database::query($sql);
  659. }
  660. }
  661. return $new_item_id;
  662. }
  663. /**
  664. * Static admin function allowing addition of a learnpath to a course.
  665. * @param string Course code
  666. * @param string Learnpath name
  667. * @param string Learnpath description string, if provided
  668. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  669. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  670. * @param string Zip file containing the learnpath or directory containing the learnpath
  671. * @return integer The new learnpath ID on success, 0 on failure
  672. */
  673. public static function add_lp(
  674. $courseCode,
  675. $name,
  676. $description = '',
  677. $learnpath = 'guess',
  678. $origin = 'zip',
  679. $zipname = '',
  680. $publicated_on = '',
  681. $expired_on = '',
  682. $categoryId = 0,
  683. $userId = 0
  684. ) {
  685. global $charset;
  686. if (!empty($courseCode)) {
  687. $courseInfo = api_get_course_info($courseCode);
  688. $course_id = $courseInfo['real_id'];
  689. } else {
  690. $course_id = api_get_course_int_id();
  691. $courseInfo = api_get_course_info();
  692. }
  693. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  694. // Check course code exists.
  695. // Check lp_name doesn't exist, otherwise append something.
  696. $i = 0;
  697. $name = Database::escape_string($name);
  698. $categoryId = intval($categoryId);
  699. // Session id.
  700. $session_id = api_get_session_id();
  701. $userId = empty($userId) ? api_get_user_id() : $userId;
  702. $check_name = "SELECT * FROM $tbl_lp
  703. WHERE c_id = $course_id AND name = '$name'";
  704. $res_name = Database::query($check_name);
  705. if (empty($publicated_on)) {
  706. //by default the publication date is the same that the creation date
  707. //The behaviour above was changed due BT#2800
  708. // global $_custom;
  709. // if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  710. // $publicated_on = null;
  711. // } else {
  712. // $publicated_on = null;
  713. // }
  714. $publicated_on = null;
  715. } else {
  716. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  717. }
  718. if (empty($expired_on)) {
  719. $expired_on = null;
  720. } else {
  721. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  722. }
  723. while (Database::num_rows($res_name)) {
  724. // There is already one such name, update the current one a bit.
  725. $i++;
  726. $name = $name . ' - ' . $i;
  727. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  728. $res_name = Database::query($check_name);
  729. }
  730. // New name does not exist yet; keep it.
  731. // Escape description.
  732. // Kevin: added htmlentities().
  733. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  734. $type = 1;
  735. switch ($learnpath) {
  736. case 'guess':
  737. break;
  738. case 'dokeos':
  739. case 'chamilo':
  740. $type = 1;
  741. break;
  742. case 'aicc':
  743. break;
  744. }
  745. switch ($origin) {
  746. case 'zip':
  747. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  748. break;
  749. case 'manual':
  750. default:
  751. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  752. $res_max = Database::query($get_max);
  753. if (Database::num_rows($res_max) < 1) {
  754. $dsp = 1;
  755. } else {
  756. $row = Database::fetch_array($res_max);
  757. $dsp = $row[0] + 1;
  758. }
  759. $params = [
  760. 'c_id' => $course_id,
  761. 'lp_type' => $type,
  762. 'name' => $name,
  763. 'description' => $description,
  764. 'path' => '',
  765. 'default_view_mod' => 'embedded',
  766. 'default_encoding' => 'UTF-8',
  767. 'display_order' => $dsp,
  768. 'content_maker' => 'Chamilo',
  769. 'content_local' => 'local',
  770. 'js_lib' => '',
  771. 'session_id' => $session_id,
  772. 'created_on' => api_get_utc_datetime(),
  773. 'modified_on' => api_get_utc_datetime(),
  774. 'publicated_on' => $publicated_on,
  775. 'expired_on' => $expired_on,
  776. 'category_id' => $categoryId,
  777. 'force_commit' => 0,
  778. 'content_license' => '',
  779. 'debug' => 0,
  780. 'theme' => '',
  781. 'preview_image' => '',
  782. 'author' => '',
  783. 'prerequisite' => 0,
  784. 'hide_toc_frame' => 0,
  785. 'seriousgame_mode' => 0,
  786. 'autolaunch' => 0,
  787. 'max_attempts' => 0,
  788. 'subscribe_users' => 0,
  789. 'accumulate_scorm_time' => 1
  790. ];
  791. $id = Database::insert($tbl_lp, $params);
  792. if ($id > 0) {
  793. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  794. Database::query($sql);
  795. // Insert into item_property.
  796. api_item_property_update(
  797. $courseInfo,
  798. TOOL_LEARNPATH,
  799. $id,
  800. 'LearnpathAdded',
  801. $userId
  802. );
  803. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  804. return $id;
  805. }
  806. break;
  807. }
  808. }
  809. /**
  810. * Auto completes the parents of an item in case it's been completed or passed
  811. * @param int $item Optional ID of the item from which to look for parents
  812. */
  813. public function autocomplete_parents($item)
  814. {
  815. $debug = $this->debug;
  816. if ($debug) {
  817. error_log('Learnpath::autocomplete_parents()', 0);
  818. }
  819. if (empty($item)) {
  820. $item = $this->current;
  821. }
  822. $currentItem = $this->getItem($item);
  823. if ($currentItem) {
  824. $parent_id = $currentItem->get_parent();
  825. $parent = $this->getItem($parent_id);
  826. if ($parent) {
  827. // if $item points to an object and there is a parent.
  828. if ($debug) {
  829. error_log(
  830. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  831. 0
  832. );
  833. }
  834. // New experiment including failed and browsed in completed status.
  835. //$current_status = $currentItem->get_status();
  836. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  837. // Fixes chapter auto complete
  838. if (true) {
  839. // If the current item is completed or passes or succeeded.
  840. $updateParentStatus = true;
  841. if ($debug) {
  842. error_log('Status of current item is alright', 0);
  843. }
  844. foreach ($parent->get_children() as $childItemId) {
  845. $childItem = $this->getItem($childItemId);
  846. // If children was not set try to get the info
  847. if (empty($childItem->db_item_view_id)) {
  848. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  849. }
  850. // Check all his brothers (parent's children) for completion status.
  851. if ($childItemId != $item) {
  852. if ($debug) {
  853. error_log(
  854. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  855. 0
  856. );
  857. }
  858. // Trying completing parents of failed and browsed items as well.
  859. if ($childItem->status_is(
  860. array(
  861. 'completed',
  862. 'passed',
  863. 'succeeded',
  864. 'browsed',
  865. 'failed'
  866. )
  867. )
  868. ) {
  869. // Keep completion status to true.
  870. continue;
  871. } else {
  872. if ($debug > 2) {
  873. error_log(
  874. '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,
  875. 0
  876. );
  877. }
  878. $updateParentStatus = false;
  879. break;
  880. }
  881. }
  882. }
  883. if ($updateParentStatus) {
  884. // If all the children were completed:
  885. $parent->set_status('completed');
  886. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  887. // Force the status to "completed"
  888. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  889. $this->update_queue[$parent->get_id()] = 'completed';
  890. if ($debug) {
  891. error_log(
  892. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  893. print_r($this->update_queue, 1),
  894. 0
  895. );
  896. }
  897. // Recursive call.
  898. $this->autocomplete_parents($parent->get_id());
  899. }
  900. }
  901. } else {
  902. if ($debug) {
  903. error_log("Parent #$parent_id does not exists");
  904. }
  905. }
  906. } else {
  907. if ($debug) {
  908. error_log("#$item is an item that doesn't have parents");
  909. }
  910. }
  911. }
  912. /**
  913. * Auto saves the current results into the database for the whole learnpath
  914. * @todo: Add save operations for the learnpath itself.
  915. */
  916. public function autosave()
  917. {
  918. if ($this->debug > 0) {
  919. error_log('New LP - In learnpath::autosave()', 0);
  920. }
  921. }
  922. /**
  923. * Closes the current resource
  924. *
  925. * Stops the timer
  926. * Saves into the database if required
  927. * Clears the current resource data from this object
  928. * @return boolean True on success, false on failure
  929. */
  930. public function close()
  931. {
  932. if ($this->debug > 0) {
  933. error_log('New LP - In learnpath::close()', 0);
  934. }
  935. if (empty($this->lp_id)) {
  936. $this->error = 'Trying to close this learnpath but no ID is set';
  937. return false;
  938. }
  939. $this->current_time_stop = time();
  940. $this->ordered_items = array();
  941. $this->index = 0;
  942. unset($this->lp_id);
  943. //unset other stuff
  944. return true;
  945. }
  946. /**
  947. * Static admin function allowing removal of a learnpath
  948. * @param array $courseInfo
  949. * @param integer Learnpath ID
  950. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  951. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  952. */
  953. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  954. {
  955. $course_id = api_get_course_int_id();
  956. if (!empty($courseInfo)) {
  957. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  958. }
  959. // TODO: Implement a way of getting this to work when the current object is not set.
  960. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  961. // If an ID is specifically given and the current LP is not the same, prevent delete.
  962. if (!empty($id) && ($id != $this->lp_id)) {
  963. return false;
  964. }
  965. $lp = Database::get_course_table(TABLE_LP_MAIN);
  966. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  967. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
  968. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  969. // Delete lp item id.
  970. foreach ($this->items as $id => $dummy) {
  971. $sql = "DELETE FROM $lp_item_view
  972. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  973. Database::query($sql);
  974. }
  975. // Proposed by Christophe (nickname: clefevre)
  976. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  977. Database::query($sql);
  978. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  979. Database::query($sql);
  980. self::toggle_publish($this->lp_id, 'i');
  981. if ($this->type == 2 || $this->type == 3) {
  982. // This is a scorm learning path, delete the files as well.
  983. $sql = "SELECT path FROM $lp
  984. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  985. $res = Database::query($sql);
  986. if (Database::num_rows($res) > 0) {
  987. $row = Database::fetch_array($res);
  988. $path = $row['path'];
  989. $sql = "SELECT id FROM $lp
  990. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  991. $res = Database::query($sql);
  992. if (Database::num_rows($res) > 0) {
  993. // Another learning path uses this directory, so don't delete it.
  994. if ($this->debug > 2) {
  995. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  996. }
  997. } else {
  998. // No other LP uses that directory, delete it.
  999. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  1000. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  1001. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  1002. if ($this->debug > 2) {
  1003. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1004. }
  1005. // Proposed by Christophe (clefevre).
  1006. if (strcmp(substr($path, -2), "/.") == 0) {
  1007. $path = substr($path, 0, -1); // Remove "." at the end.
  1008. }
  1009. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1010. rmdirr($course_scorm_dir . $path);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1016. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1017. // Delete tools
  1018. $sql = "DELETE FROM $tbl_tool
  1019. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1020. Database::query($sql);
  1021. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1022. Database::query($sql);
  1023. // Updates the display order of all lps.
  1024. $this->update_display_order();
  1025. api_item_property_update(
  1026. api_get_course_info(),
  1027. TOOL_LEARNPATH,
  1028. $this->lp_id,
  1029. 'delete',
  1030. api_get_user_id()
  1031. );
  1032. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1033. api_get_course_id(),
  1034. 4,
  1035. $id,
  1036. api_get_session_id()
  1037. );
  1038. if ($link_info !== false) {
  1039. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1040. }
  1041. if (api_get_setting('search_enabled') == 'true') {
  1042. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1043. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1044. }
  1045. }
  1046. /**
  1047. * Removes all the children of one item - dangerous!
  1048. * @param integer $id Element ID of which children have to be removed
  1049. * @return integer Total number of children removed
  1050. */
  1051. public function delete_children_items($id)
  1052. {
  1053. $course_id = $this->course_info['real_id'];
  1054. if ($this->debug > 0) {
  1055. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1056. }
  1057. $num = 0;
  1058. if (empty ($id) || $id != strval(intval($id))) {
  1059. return false;
  1060. }
  1061. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1062. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1063. $res = Database::query($sql);
  1064. while ($row = Database::fetch_array($res)) {
  1065. $num += $this->delete_children_items($row['id']);
  1066. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1067. Database::query($sql_del);
  1068. $num++;
  1069. }
  1070. return $num;
  1071. }
  1072. /**
  1073. * Removes an item from the current learnpath
  1074. * @param integer $id Elem ID (0 if first)
  1075. * @param integer $remove Whether to remove the resource/data from the
  1076. * system or leave it (default: 'keep', others 'remove')
  1077. * @return integer Number of elements moved
  1078. * @todo implement resource removal
  1079. */
  1080. public function delete_item($id, $remove = 'keep')
  1081. {
  1082. $course_id = api_get_course_int_id();
  1083. if ($this->debug > 0) {
  1084. error_log('New LP - In learnpath::delete_item()', 0);
  1085. }
  1086. // TODO: Implement the resource removal.
  1087. if (empty ($id) || $id != strval(intval($id))) {
  1088. return false;
  1089. }
  1090. // First select item to get previous, next, and display order.
  1091. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1092. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1093. $res_sel = Database::query($sql_sel);
  1094. if (Database::num_rows($res_sel) < 1) {
  1095. return false;
  1096. }
  1097. $row = Database::fetch_array($res_sel);
  1098. $previous = $row['previous_item_id'];
  1099. $next = $row['next_item_id'];
  1100. $display = $row['display_order'];
  1101. $parent = $row['parent_item_id'];
  1102. $lp = $row['lp_id'];
  1103. // Delete children items.
  1104. $num = $this->delete_children_items($id);
  1105. if ($this->debug > 2) {
  1106. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1107. }
  1108. // Now delete the item.
  1109. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1110. if ($this->debug > 2) {
  1111. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1112. }
  1113. Database::query($sql_del);
  1114. // Now update surrounding items.
  1115. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1116. WHERE c_id = ".$course_id." AND id = $previous";
  1117. Database::query($sql_upd);
  1118. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1119. WHERE c_id = ".$course_id." AND id = $next";
  1120. Database::query($sql_upd);
  1121. // Now update all following items with new display order.
  1122. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1123. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1124. Database::query($sql_all);
  1125. //Removing prerequisites since the item will not longer exist
  1126. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1127. Database::query($sql_all);
  1128. // Remove from search engine if enabled.
  1129. if (api_get_setting('search_enabled') == 'true') {
  1130. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1131. $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';
  1132. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1133. $res = Database::query($sql);
  1134. if (Database::num_rows($res) > 0) {
  1135. $row2 = Database::fetch_array($res);
  1136. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1137. $di = new ChamiloIndexer();
  1138. $di->remove_document((int) $row2['search_did']);
  1139. }
  1140. $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';
  1141. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1142. Database::query($sql);
  1143. }
  1144. }
  1145. /**
  1146. * Updates an item's content in place
  1147. * @param integer $id Element ID
  1148. * @param integer $parent Parent item ID
  1149. * @param integer $previous Previous item ID
  1150. * @param string $title Item title
  1151. * @param string $description Item description
  1152. * @param string $prerequisites Prerequisites (optional)
  1153. * @param array $audio The array resulting of the $_FILES[mp3] element
  1154. * @param int $max_time_allowed
  1155. * @param string $url
  1156. * @return boolean True on success, false on error
  1157. */
  1158. public function edit_item(
  1159. $id,
  1160. $parent,
  1161. $previous,
  1162. $title,
  1163. $description,
  1164. $prerequisites = '0',
  1165. $audio = array(),
  1166. $max_time_allowed = 0,
  1167. $url = ''
  1168. ) {
  1169. $course_id = api_get_course_int_id();
  1170. $_course = api_get_course_info();
  1171. if ($this->debug > 0) {
  1172. error_log('New LP - In learnpath::edit_item()', 0);
  1173. }
  1174. if (empty ($max_time_allowed)) {
  1175. $max_time_allowed = 0;
  1176. }
  1177. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1178. return false;
  1179. }
  1180. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1181. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1182. $res_select = Database::query($sql_select);
  1183. $row_select = Database::fetch_array($res_select);
  1184. $audio_update_sql = '';
  1185. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1186. // Create the audio folder if it does not exist yet.
  1187. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1188. if (!is_dir($filepath . 'audio')) {
  1189. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1190. $audio_id = add_document(
  1191. $_course,
  1192. '/audio',
  1193. 'folder',
  1194. 0,
  1195. 'audio'
  1196. );
  1197. api_item_property_update(
  1198. $_course,
  1199. TOOL_DOCUMENT,
  1200. $audio_id,
  1201. 'FolderCreated',
  1202. api_get_user_id(),
  1203. null,
  1204. null,
  1205. null,
  1206. null,
  1207. api_get_session_id()
  1208. );
  1209. api_item_property_update(
  1210. $_course,
  1211. TOOL_DOCUMENT,
  1212. $audio_id,
  1213. 'invisible',
  1214. api_get_user_id(),
  1215. null,
  1216. null,
  1217. null,
  1218. null,
  1219. api_get_session_id()
  1220. );
  1221. }
  1222. // Upload file in documents.
  1223. $pi = pathinfo($audio['name']);
  1224. if ($pi['extension'] == 'mp3') {
  1225. $c_det = api_get_course_info($this->cc);
  1226. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1227. $path = handle_uploaded_document(
  1228. $c_det,
  1229. $audio,
  1230. $bp,
  1231. '/audio',
  1232. api_get_user_id(),
  1233. 0,
  1234. null,
  1235. 0,
  1236. 'rename',
  1237. false,
  1238. 0
  1239. );
  1240. $path = substr($path, 7);
  1241. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1242. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1243. }
  1244. }
  1245. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1246. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1247. // TODO: htmlspecialchars to be checked for encoding related problems.
  1248. if ($same_parent && $same_previous) {
  1249. // Only update title and description.
  1250. $sql = "UPDATE " . $tbl_lp_item . "
  1251. SET title = '" . Database::escape_string($title) . "',
  1252. prerequisite = '" . $prerequisites . "',
  1253. description = '" . Database::escape_string($description) . "'
  1254. " . $audio_update_sql . ",
  1255. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1256. WHERE c_id = ".$course_id." AND id = " . $id;
  1257. Database::query($sql);
  1258. } else {
  1259. $old_parent = $row_select['parent_item_id'];
  1260. $old_previous = $row_select['previous_item_id'];
  1261. $old_next = $row_select['next_item_id'];
  1262. $old_order = $row_select['display_order'];
  1263. $old_prerequisite = $row_select['prerequisite'];
  1264. $old_max_time_allowed = $row_select['max_time_allowed'];
  1265. /* BEGIN -- virtually remove the current item id */
  1266. /* for the next and previous item it is like the current item doesn't exist anymore */
  1267. if ($old_previous != 0) {
  1268. // Next
  1269. $sql = "UPDATE " . $tbl_lp_item . "
  1270. SET next_item_id = " . $old_next . "
  1271. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1272. Database::query($sql);
  1273. }
  1274. if ($old_next != 0) {
  1275. // Previous
  1276. $sql = "UPDATE " . $tbl_lp_item . "
  1277. SET previous_item_id = " . $old_previous . "
  1278. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1279. Database::query($sql);
  1280. }
  1281. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1282. $sql = "UPDATE " . $tbl_lp_item . "
  1283. SET display_order = display_order - 1
  1284. WHERE
  1285. c_id = ".$course_id." AND
  1286. display_order > " . $old_order . " AND
  1287. lp_id = " . $this->lp_id . " AND
  1288. parent_item_id = " . $old_parent;
  1289. Database::query($sql);
  1290. /* END -- virtually remove the current item id */
  1291. /* BEGIN -- update the current item id to his new location */
  1292. if ($previous == 0) {
  1293. // Select the data of the item that should come after the current item.
  1294. $sql = "SELECT id, display_order
  1295. FROM " . $tbl_lp_item . "
  1296. WHERE
  1297. c_id = ".$course_id." AND
  1298. lp_id = " . $this->lp_id . " AND
  1299. parent_item_id = " . $parent . " AND
  1300. previous_item_id = " . $previous;
  1301. $res_select_old = Database::query($sql);
  1302. $row_select_old = Database::fetch_array($res_select_old);
  1303. // If the new parent didn't have children before.
  1304. if (Database::num_rows($res_select_old) == 0) {
  1305. $new_next = 0;
  1306. $new_order = 1;
  1307. } else {
  1308. $new_next = $row_select_old['id'];
  1309. $new_order = $row_select_old['display_order'];
  1310. }
  1311. } else {
  1312. // Select the data of the item that should come before the current item.
  1313. $sql = "SELECT next_item_id, display_order
  1314. FROM " . $tbl_lp_item . "
  1315. WHERE c_id = ".$course_id." AND id = " . $previous;
  1316. $res_select_old = Database::query($sql);
  1317. $row_select_old = Database::fetch_array($res_select_old);
  1318. $new_next = $row_select_old['next_item_id'];
  1319. $new_order = $row_select_old['display_order'] + 1;
  1320. }
  1321. // TODO: htmlspecialchars to be checked for encoding related problems.
  1322. // Update the current item with the new data.
  1323. $sql = "UPDATE " . $tbl_lp_item . "
  1324. SET
  1325. title = '" . Database::escape_string($title) . "',
  1326. description = '" . Database::escape_string($description) . "',
  1327. parent_item_id = " . $parent . ",
  1328. previous_item_id = " . $previous . ",
  1329. next_item_id = " . $new_next . ",
  1330. display_order = " . $new_order . "
  1331. " . $audio_update_sql . "
  1332. WHERE c_id = ".$course_id." AND id = " . $id;
  1333. Database::query($sql);
  1334. if ($previous != 0) {
  1335. // Update the previous item's next_item_id.
  1336. $sql = "UPDATE " . $tbl_lp_item . "
  1337. SET next_item_id = " . $id . "
  1338. WHERE c_id = ".$course_id." AND id = " . $previous;
  1339. Database::query($sql);
  1340. }
  1341. if ($new_next != 0) {
  1342. // Update the next item's previous_item_id.
  1343. $sql = "UPDATE " . $tbl_lp_item . "
  1344. SET previous_item_id = " . $id . "
  1345. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1346. Database::query($sql);
  1347. }
  1348. if ($old_prerequisite != $prerequisites) {
  1349. $sql = "UPDATE " . $tbl_lp_item . "
  1350. SET prerequisite = '" . $prerequisites . "'
  1351. WHERE c_id = ".$course_id." AND id = " . $id;
  1352. Database::query($sql);
  1353. }
  1354. if ($old_max_time_allowed != $max_time_allowed) {
  1355. // update max time allowed
  1356. $sql = "UPDATE " . $tbl_lp_item . "
  1357. SET max_time_allowed = " . $max_time_allowed . "
  1358. WHERE c_id = ".$course_id." AND id = " . $id;
  1359. Database::query($sql);
  1360. }
  1361. // Update all the items with the same or a bigger display_order than the current item.
  1362. $sql = "UPDATE " . $tbl_lp_item . "
  1363. SET display_order = display_order + 1
  1364. WHERE
  1365. c_id = ".$course_id." AND
  1366. lp_id = " . $this->get_id() . " AND
  1367. id <> " . $id . " AND
  1368. parent_item_id = " . $parent . " AND
  1369. display_order >= " . $new_order;
  1370. Database::query($sql);
  1371. }
  1372. if ($row_select['item_type'] == 'link') {
  1373. $link = new Link();
  1374. $linkId = $row_select['path'];
  1375. $link->updateLink($linkId, $url);
  1376. }
  1377. }
  1378. /**
  1379. * Updates an item's prereq in place
  1380. * @param integer $id Element ID
  1381. * @param string $prerequisite_id Prerequisite Element ID
  1382. * @param int $mastery_score Prerequisite min score
  1383. * @param int $max_score Prerequisite max score
  1384. *
  1385. * @return boolean True on success, false on error
  1386. */
  1387. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1388. {
  1389. $course_id = api_get_course_int_id();
  1390. if ($this->debug > 0) {
  1391. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1392. }
  1393. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1394. return false;
  1395. }
  1396. $prerequisite_id = intval($prerequisite_id);
  1397. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1398. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1399. $mastery_score = 0;
  1400. }
  1401. if (!is_numeric($max_score) || $max_score < 0) {
  1402. $max_score = 100;
  1403. }
  1404. /*if ($mastery_score > $max_score) {
  1405. $max_score = $mastery_score;
  1406. }*/
  1407. if (!is_numeric($prerequisite_id)) {
  1408. $prerequisite_id = 'NULL';
  1409. }
  1410. $mastery_score = floatval($mastery_score);
  1411. $max_score = floatval($max_score);
  1412. $sql = " UPDATE $tbl_lp_item
  1413. SET
  1414. prerequisite = $prerequisite_id ,
  1415. prerequisite_min_score = $mastery_score ,
  1416. prerequisite_max_score = $max_score
  1417. WHERE c_id = $course_id AND id = $id";
  1418. Database::query($sql);
  1419. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1420. // Will this be enough to ensure unicity?
  1421. /*$sql = " UPDATE $tbl_lp_item
  1422. SET mastery_score = $mastery_score
  1423. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1424. Database::query($sql);*/
  1425. }
  1426. // TODO: Update the item object (can be ignored for now because refreshed).
  1427. return true;
  1428. }
  1429. /**
  1430. * Static admin function exporting a learnpath into a zip file
  1431. * @param string Export type (scorm, zip, cd)
  1432. * @param string Course code
  1433. * @param integer Learnpath ID
  1434. * @param string Zip file name
  1435. * @return string Zip file path (or false on error)
  1436. */
  1437. public function export_lp($type, $course, $id, $zipname)
  1438. {
  1439. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1440. return false;
  1441. }
  1442. $url = '';
  1443. switch ($type) {
  1444. case 'scorm':
  1445. break;
  1446. case 'zip':
  1447. break;
  1448. case 'cdrom':
  1449. break;
  1450. }
  1451. return $url;
  1452. }
  1453. /**
  1454. * Gets all the chapters belonging to the same parent as the item/chapter given
  1455. * Can also be called as abstract method
  1456. * @param integer $id Item ID
  1457. * @return array A list of all the "brother items" (or an empty array on failure)
  1458. */
  1459. public function getSiblingDirectories($id)
  1460. {
  1461. $course_id = api_get_course_int_id();
  1462. if ($this->debug > 0) {
  1463. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1464. }
  1465. if (empty($id) || $id != strval(intval($id))) {
  1466. return array ();
  1467. }
  1468. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1469. $sql_parent = "SELECT * FROM $lp_item
  1470. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1471. $res_parent = Database::query($sql_parent);
  1472. if (Database::num_rows($res_parent) > 0) {
  1473. $row_parent = Database::fetch_array($res_parent);
  1474. $parent = $row_parent['parent_item_id'];
  1475. $sql = "SELECT * FROM $lp_item
  1476. WHERE
  1477. c_id = ".$course_id." AND
  1478. parent_item_id = $parent AND
  1479. id = $id AND
  1480. item_type='dir'
  1481. ORDER BY display_order";
  1482. $res_bros = Database::query($sql);
  1483. $list = array();
  1484. while ($row_bro = Database::fetch_array($res_bros)) {
  1485. $list[] = $row_bro;
  1486. }
  1487. return $list;
  1488. }
  1489. return array();
  1490. }
  1491. /**
  1492. * Gets all the items belonging to the same parent as the item given
  1493. * Can also be called as abstract method
  1494. * @param integer $id Item ID
  1495. * @return array A list of all the "brother items" (or an empty array on failure)
  1496. */
  1497. public function get_brother_items($id)
  1498. {
  1499. $course_id = api_get_course_int_id();
  1500. if ($this->debug > 0) {
  1501. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1502. }
  1503. if (empty ($id) || $id != strval(intval($id))) {
  1504. return array();
  1505. }
  1506. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1507. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1508. $res_parent = Database::query($sql_parent);
  1509. if (Database::num_rows($res_parent) > 0) {
  1510. $row_parent = Database::fetch_array($res_parent);
  1511. $parent = $row_parent['parent_item_id'];
  1512. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1513. ORDER BY display_order";
  1514. $res_bros = Database::query($sql_bros);
  1515. $list = [];
  1516. while ($row_bro = Database::fetch_array($res_bros)) {
  1517. $list[] = $row_bro;
  1518. }
  1519. return $list;
  1520. }
  1521. return [];
  1522. }
  1523. /**
  1524. * Get the specific prefix index terms of this learning path
  1525. * @param string $prefix
  1526. * @return array Array of terms
  1527. */
  1528. public function get_common_index_terms_by_prefix($prefix)
  1529. {
  1530. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1531. $terms = get_specific_field_values_list_by_prefix(
  1532. $prefix,
  1533. $this->cc,
  1534. TOOL_LEARNPATH,
  1535. $this->lp_id
  1536. );
  1537. $prefix_terms = array();
  1538. if (!empty($terms)) {
  1539. foreach ($terms as $term) {
  1540. $prefix_terms[] = $term['value'];
  1541. }
  1542. }
  1543. return $prefix_terms;
  1544. }
  1545. /**
  1546. * Gets the number of items currently completed
  1547. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1548. * @return integer The number of items currently completed
  1549. */
  1550. public function get_complete_items_count($failedStatusException = false)
  1551. {
  1552. if ($this->debug > 0) {
  1553. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1554. }
  1555. $i = 0;
  1556. $completedStatusList = array(
  1557. 'completed',
  1558. 'passed',
  1559. 'succeeded',
  1560. 'browsed'
  1561. );
  1562. if (!$failedStatusException) {
  1563. $completedStatusList[] = 'failed';
  1564. }
  1565. foreach ($this->items as $id => $dummy) {
  1566. // Trying failed and browsed considered "progressed" as well.
  1567. if ($this->items[$id]->status_is($completedStatusList) &&
  1568. $this->items[$id]->get_type() != 'dir'
  1569. ) {
  1570. $i++;
  1571. }
  1572. }
  1573. return $i;
  1574. }
  1575. /**
  1576. * Gets the current item ID
  1577. * @return integer The current learnpath item id
  1578. */
  1579. public function get_current_item_id()
  1580. {
  1581. $current = 0;
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1584. }
  1585. if (!empty($this->current)) {
  1586. $current = $this->current;
  1587. }
  1588. if ($this->debug > 2) {
  1589. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1590. }
  1591. return $current;
  1592. }
  1593. /**
  1594. * Force to get the first learnpath item id
  1595. * @return integer The current learnpath item id
  1596. */
  1597. public function get_first_item_id()
  1598. {
  1599. $current = 0;
  1600. if (is_array($this->ordered_items)) {
  1601. $current = $this->ordered_items[0];
  1602. }
  1603. return $current;
  1604. }
  1605. /**
  1606. * Gets the total number of items available for viewing in this SCORM
  1607. * @return integer The total number of items
  1608. */
  1609. public function get_total_items_count()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1613. }
  1614. return count($this->items);
  1615. }
  1616. /**
  1617. * Gets the total number of items available for viewing in this SCORM but without chapters
  1618. * @return integer The total no-chapters number of items
  1619. */
  1620. public function getTotalItemsCountWithoutDirs()
  1621. {
  1622. if ($this->debug > 0) {
  1623. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1624. }
  1625. $total = 0;
  1626. $typeListNotToCount = self::getChapterTypes();
  1627. foreach ($this->items as $temp2) {
  1628. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1629. $total++;
  1630. }
  1631. }
  1632. return $total;
  1633. }
  1634. /**
  1635. * Gets the first element URL.
  1636. * @return string URL to load into the viewer
  1637. */
  1638. public function first()
  1639. {
  1640. if ($this->debug > 0) {
  1641. error_log('New LP - In learnpath::first()', 0);
  1642. error_log('$this->last_item_seen '.$this->last_item_seen);
  1643. }
  1644. // Test if the last_item_seen exists and is not a dir.
  1645. if (count($this->ordered_items) == 0) {
  1646. $this->index = 0;
  1647. }
  1648. if ($this->debug > 0) {
  1649. if (isset($this->items[$this->last_item_seen])) {
  1650. $status = $this->items[$this->last_item_seen]->get_status();
  1651. }
  1652. error_log('status '.$status);
  1653. }
  1654. if (!empty($this->last_item_seen) &&
  1655. !empty($this->items[$this->last_item_seen]) &&
  1656. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1657. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1658. //&& !$this->items[$this->last_item_seen]->is_done()
  1659. ) {
  1660. if ($this->debug > 2) {
  1661. 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);
  1662. }
  1663. $index = -1;
  1664. foreach ($this->ordered_items as $myindex => $item_id) {
  1665. if ($item_id == $this->last_item_seen) {
  1666. $index = $myindex;
  1667. break;
  1668. }
  1669. }
  1670. if ($index == -1) {
  1671. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1672. if ($this->debug > 2) {
  1673. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1674. }
  1675. return false;
  1676. } else {
  1677. $this->last = $this->last_item_seen;
  1678. $this->current = $this->last_item_seen;
  1679. $this->index = $index;
  1680. }
  1681. } else {
  1682. if ($this->debug > 2) {
  1683. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1684. }
  1685. $index = 0;
  1686. // Loop through all ordered items and stop at the first item that is
  1687. // not a directory *and* that has not been completed yet.
  1688. while ( !empty($this->ordered_items[$index]) AND
  1689. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1690. (
  1691. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1692. $this->items[$this->ordered_items[$index]]->is_done() === true
  1693. ) AND $index < $this->max_ordered_items) {
  1694. $index++;
  1695. }
  1696. $this->last = $this->current;
  1697. // current is
  1698. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1699. $this->index = $index;
  1700. if ($this->debug > 2) {
  1701. error_log('$index ' . $index);
  1702. }
  1703. if ($this->debug > 2) {
  1704. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1705. }
  1706. }
  1707. if ($this->debug > 2) {
  1708. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1709. }
  1710. }
  1711. /**
  1712. * Gets the information about an item in a format usable as JavaScript to update
  1713. * the JS API by just printing this content into the <head> section of the message frame
  1714. * @param int $item_id
  1715. * @return string
  1716. */
  1717. public function get_js_info($item_id = 0)
  1718. {
  1719. if ($this->debug > 0) {
  1720. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1721. }
  1722. $info = '';
  1723. $item_id = intval($item_id);
  1724. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1725. //if item is defined, return values from DB
  1726. $oItem = $this->items[$item_id];
  1727. $info .= '<script language="javascript">';
  1728. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1729. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1730. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1731. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1732. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1733. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1734. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1735. $info .= "top.set_flag_synchronized();";
  1736. $info .= '</script>';
  1737. if ($this->debug > 2) {
  1738. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1739. }
  1740. return $info;
  1741. } else {
  1742. // If item_id is empty, just update to default SCORM data.
  1743. $info .= '<script language="javascript">';
  1744. $info .= "top.set_score(" . learnpathItem::get_score() . ");\n";
  1745. $info .= "top.set_max(" . learnpathItem::get_max() . ");\n";
  1746. $info .= "top.set_min(" . learnpathItem::get_min() . ");\n";
  1747. $info .= "top.set_lesson_status('" . learnpathItem::get_status() . "');";
  1748. $info .= "top.set_session_time('" . learnpathItem::getScormTimeFromParameter('js') . "');";
  1749. $info .= "top.set_suspend_data('" . learnpathItem::get_suspend_data() . "');";
  1750. $info .= "top.set_saved_lesson_status('" . learnpathItem::get_status() . "');";
  1751. $info .= "top.set_flag_synchronized();";
  1752. $info .= '</script>';
  1753. if ($this->debug > 2) {
  1754. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1755. }
  1756. return $info;
  1757. }
  1758. }
  1759. /**
  1760. * Gets the js library from the database
  1761. * @return string The name of the javascript library to be used
  1762. */
  1763. public function get_js_lib()
  1764. {
  1765. $lib = '';
  1766. if (!empty ($this->js_lib)) {
  1767. $lib = $this->js_lib;
  1768. }
  1769. return $lib;
  1770. }
  1771. /**
  1772. * Gets the learnpath database ID
  1773. * @return integer Learnpath ID in the lp table
  1774. */
  1775. public function get_id()
  1776. {
  1777. if (!empty ($this->lp_id)) {
  1778. return $this->lp_id;
  1779. } else {
  1780. return 0;
  1781. }
  1782. }
  1783. /**
  1784. * Gets the last element URL.
  1785. * @return string URL to load into the viewer
  1786. */
  1787. public function get_last()
  1788. {
  1789. if ($this->debug > 0) {
  1790. error_log('New LP - In learnpath::get_last()', 0);
  1791. }
  1792. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1793. if (count($this->ordered_items) > 0) {
  1794. $this->index = count($this->ordered_items) - 1;
  1795. return $this->ordered_items[$this->index];
  1796. }
  1797. return false;
  1798. }
  1799. /**
  1800. * Gets the navigation bar for the learnpath display screen
  1801. * @return string The HTML string to use as a navigation bar
  1802. */
  1803. public function get_navigation_bar($idBar = null, $display = null)
  1804. {
  1805. if ($this->debug > 0) {
  1806. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1807. }
  1808. if (empty($idBar)) {
  1809. $idBar = 'control-top';
  1810. }
  1811. $navbar = null;
  1812. $lp_id = $this->lp_id;
  1813. $mycurrentitemid = $this->get_current_item_id();
  1814. $reportingText = get_lang('Reporting');
  1815. $previousText = get_lang('ScormPrevious');
  1816. $nextText = get_lang('ScormNext');
  1817. $fullScreenText = get_lang('ScormExitFullScreen');
  1818. if ($this->mode == 'fullscreen') {
  1819. $navbar = '
  1820. <span id="'.$idBar.'" class="buttons">
  1821. <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">
  1822. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText . '</span>
  1823. </a>
  1824. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1825. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText . '</span>
  1826. </a>
  1827. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1828. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText. '</span>
  1829. </a>
  1830. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1831. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText . '</span>
  1832. </a>
  1833. </span>';
  1834. } else {
  1835. $navbar = '
  1836. <span id="'.$idBar.'" class="buttons text-right">
  1837. <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">
  1838. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText. '</span>
  1839. </a>
  1840. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1841. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText. '</span>
  1842. </a>
  1843. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1844. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText . '</span>
  1845. </a>
  1846. </span>';
  1847. }
  1848. return $navbar;
  1849. }
  1850. /**
  1851. * Gets the next resource in queue (url).
  1852. * @return string URL to load into the viewer
  1853. */
  1854. public function get_next_index()
  1855. {
  1856. if ($this->debug > 0) {
  1857. error_log('New LP - In learnpath::get_next_index()', 0);
  1858. }
  1859. // TODO
  1860. $index = $this->index;
  1861. $index++;
  1862. if ($this->debug > 2) {
  1863. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1864. }
  1865. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1866. $index++;
  1867. if ($index == $this->max_ordered_items){
  1868. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1869. return $this->index;
  1870. } else {
  1871. return $index;
  1872. }
  1873. }
  1874. }
  1875. if (empty ($this->ordered_items[$index])) {
  1876. return $this->index;
  1877. }
  1878. if ($this->debug > 2) {
  1879. error_log('New LP - index is now ' . $index, 0);
  1880. }
  1881. return $index;
  1882. }
  1883. /**
  1884. * Gets item_id for the next element
  1885. * @return integer Next item (DB) ID
  1886. */
  1887. public function get_next_item_id()
  1888. {
  1889. if ($this->debug > 0) {
  1890. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1891. }
  1892. $new_index = $this->get_next_index();
  1893. if (!empty ($new_index)) {
  1894. if (isset ($this->ordered_items[$new_index])) {
  1895. if ($this->debug > 2) {
  1896. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1897. }
  1898. return $this->ordered_items[$new_index];
  1899. }
  1900. }
  1901. if ($this->debug > 2) {
  1902. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1908. *
  1909. * Generally, the package provided is in the form of a zip file, so the function
  1910. * has been written to test a zip file. If not a zip, the function will return the
  1911. * default return value: ''
  1912. * @param string the path to the file
  1913. * @param string the original name of the file
  1914. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1915. */
  1916. public static function get_package_type($file_path, $file_name)
  1917. {
  1918. // Get name of the zip file without the extension.
  1919. $file_info = pathinfo($file_name);
  1920. $filename = $file_info['basename']; // Name including extension.
  1921. $extension = $file_info['extension']; // Extension only.
  1922. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1923. 'dll',
  1924. 'exe'
  1925. ))) {
  1926. return 'oogie';
  1927. }
  1928. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1929. 'dll',
  1930. 'exe'
  1931. ))) {
  1932. return 'woogie';
  1933. }
  1934. $zipFile = new PclZip($file_path);
  1935. // Check the zip content (real size and file extension).
  1936. $zipContentArray = $zipFile->listContent();
  1937. $package_type = '';
  1938. $at_root = false;
  1939. $manifest = '';
  1940. $aicc_match_crs = 0;
  1941. $aicc_match_au = 0;
  1942. $aicc_match_des = 0;
  1943. $aicc_match_cst = 0;
  1944. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1945. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1946. foreach ($zipContentArray as $thisContent) {
  1947. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1948. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1949. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1950. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1951. $package_type = 'scorm';
  1952. break; // Exit the foreach loop.
  1953. } elseif (
  1954. preg_match('/aicc\//i', $thisContent['filename']) ||
  1955. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1956. ) {
  1957. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1958. switch ($ext) {
  1959. case 'crs':
  1960. $aicc_match_crs = 1;
  1961. break;
  1962. case 'au':
  1963. $aicc_match_au = 1;
  1964. break;
  1965. case 'des':
  1966. $aicc_match_des = 1;
  1967. break;
  1968. case 'cst':
  1969. $aicc_match_cst = 1;
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1975. } else {
  1976. $package_type = '';
  1977. }
  1978. }
  1979. }
  1980. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1981. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1982. $package_type = 'aicc';
  1983. }
  1984. return $package_type;
  1985. }
  1986. /**
  1987. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1988. * @return string URL to load into the viewer
  1989. */
  1990. public function get_previous_index()
  1991. {
  1992. if ($this->debug > 0) {
  1993. error_log('New LP - In learnpath::get_previous_index()', 0);
  1994. }
  1995. $index = $this->index;
  1996. if (isset ($this->ordered_items[$index -1])) {
  1997. $index--;
  1998. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1999. $index--;
  2000. if ($index < 0) {
  2001. return $this->index;
  2002. }
  2003. }
  2004. } else {
  2005. if ($this->debug > 2) {
  2006. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2007. }
  2008. // There is no previous item.
  2009. }
  2010. return $index;
  2011. }
  2012. /**
  2013. * Gets item_id for the next element
  2014. * @return integer Previous item (DB) ID
  2015. */
  2016. public function get_previous_item_id()
  2017. {
  2018. if ($this->debug > 0) {
  2019. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2020. }
  2021. $new_index = $this->get_previous_index();
  2022. return $this->ordered_items[$new_index];
  2023. }
  2024. /**
  2025. * Gets the progress value from the progress_db attribute
  2026. * @return integer Current progress value
  2027. */
  2028. public function get_progress()
  2029. {
  2030. if ($this->debug > 0) {
  2031. error_log('New LP - In learnpath::get_progress()', 0);
  2032. }
  2033. if (!empty ($this->progress_db)) {
  2034. return $this->progress_db;
  2035. }
  2036. return 0;
  2037. }
  2038. /**
  2039. * Returns the HTML necessary to print a mediaplayer block inside a page
  2040. * @return string The mediaplayer HTML
  2041. */
  2042. public function get_mediaplayer($lpItemId, $autostart = 'true')
  2043. {
  2044. $course_id = api_get_course_int_id();
  2045. $_course = api_get_course_info();
  2046. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2047. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2048. // Getting all the information about the item.
  2049. $sql = "SELECT * FROM $tbl_lp_item as lp
  2050. INNER JOIN $tbl_lp_item_view as lp_view
  2051. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2052. WHERE
  2053. lp.id = '$lpItemId' AND
  2054. lp.c_id = $course_id AND
  2055. lp_view.c_id = $course_id";
  2056. $result = Database::query($sql);
  2057. $row = Database::fetch_assoc($result);
  2058. $output = '';
  2059. if (!empty ($row['audio'])) {
  2060. $list = $_SESSION['oLP']->get_toc();
  2061. $type_quiz = false;
  2062. foreach($list as $toc) {
  2063. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2064. $type_quiz = true;
  2065. }
  2066. }
  2067. if ($type_quiz) {
  2068. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2069. $autostart_audio = $row['status'] === 'completed' ? 'false' : 'true';
  2070. } else {
  2071. $autostart_audio = $autostart;
  2072. }
  2073. } else {
  2074. $autostart_audio = 'true';
  2075. }
  2076. $courseInfo = api_get_course_info();
  2077. $audio = $row['audio'];
  2078. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2079. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2080. if (!file_exists($file)) {
  2081. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2082. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2083. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2084. }
  2085. $player = Display::getMediaPlayer(
  2086. $file,
  2087. array(
  2088. 'id' => 'lp_audio_media_player',
  2089. 'url' => $url,
  2090. 'autoplay' => $autostart_audio,
  2091. 'width' => '100%'
  2092. )
  2093. );
  2094. // The mp3 player.
  2095. $output = '<div id="container">';
  2096. $output .= $player;
  2097. $output .= '</div>';
  2098. }
  2099. return $output;
  2100. }
  2101. /**
  2102. * @param int $studentId
  2103. * @param int $prerequisite
  2104. * @param array $courseInfo
  2105. * @param int $sessionId
  2106. *
  2107. * @return bool
  2108. *
  2109. */
  2110. public static function isBlockedByPrerequisite(
  2111. $studentId,
  2112. $prerequisite,
  2113. $courseInfo,
  2114. $sessionId
  2115. ) {
  2116. $isBlocked = false;
  2117. if (!empty($prerequisite)) {
  2118. $progress = self::getProgress(
  2119. $prerequisite,
  2120. $studentId,
  2121. $courseInfo['real_id'],
  2122. $sessionId
  2123. );
  2124. if ($progress < 100) {
  2125. $isBlocked = true;
  2126. }
  2127. }
  2128. return $isBlocked;
  2129. }
  2130. /**
  2131. * Checks if the learning path is visible for student after the progress
  2132. * of its prerequisite is completed, considering the time availability and
  2133. * the LP visibility.
  2134. * @param int $lp_id
  2135. * @param int $student_id
  2136. * @param string Course code (optional)
  2137. * @param int $sessionId
  2138. * @return bool
  2139. */
  2140. public static function is_lp_visible_for_student(
  2141. $lp_id,
  2142. $student_id,
  2143. $courseCode = null,
  2144. $sessionId = 0
  2145. ) {
  2146. $courseInfo = api_get_course_info($courseCode);
  2147. $lp_id = (int) $lp_id;
  2148. $sessionId = (int) $sessionId;
  2149. if (empty($sessionId)) {
  2150. $sessionId = api_get_session_id();
  2151. }
  2152. if (empty($courseInfo)) {
  2153. return false;
  2154. }
  2155. $itemInfo = api_get_item_property_info(
  2156. $courseInfo['real_id'],
  2157. TOOL_LEARNPATH,
  2158. $lp_id,
  2159. $sessionId
  2160. );
  2161. // If the item was deleted.
  2162. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2163. return false;
  2164. }
  2165. // @todo remove this query and load the row info as a parameter
  2166. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2167. // Get current prerequisite
  2168. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2169. FROM $tbl_learnpath
  2170. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2171. $rs = Database::query($sql);
  2172. $now = time();
  2173. if (Database::num_rows($rs) > 0) {
  2174. $row = Database::fetch_array($rs, 'ASSOC');
  2175. $prerequisite = $row['prerequisite'];
  2176. $is_visible = true;
  2177. $isBlocked = self::isBlockedByPrerequisite(
  2178. $student_id,
  2179. $prerequisite,
  2180. $courseInfo,
  2181. $sessionId
  2182. );
  2183. if ($isBlocked) {
  2184. $is_visible = false;
  2185. }
  2186. // Also check the time availability of the LP
  2187. if ($is_visible) {
  2188. // Adding visibility restrictions
  2189. if (!empty($row['publicated_on'])) {
  2190. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2191. $is_visible = false;
  2192. }
  2193. }
  2194. // Blocking empty start times see BT#2800
  2195. global $_custom;
  2196. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2197. $_custom['lps_hidden_when_no_start_date']
  2198. ) {
  2199. if (empty($row['publicated_on'])) {
  2200. $is_visible = false;
  2201. }
  2202. }
  2203. if (!empty($row['expired_on'])) {
  2204. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2205. $is_visible = false;
  2206. }
  2207. }
  2208. }
  2209. // Check if the subscription users/group to a LP is ON
  2210. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2211. // Try group
  2212. $is_visible = false;
  2213. // Checking only the user visibility
  2214. $userVisibility = api_get_item_visibility(
  2215. $courseInfo,
  2216. 'learnpath',
  2217. $row['id'],
  2218. $sessionId,
  2219. $student_id,
  2220. 'LearnpathSubscription'
  2221. );
  2222. if ($userVisibility == 1) {
  2223. $is_visible = true;
  2224. } else {
  2225. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2226. if (!empty($userGroups)) {
  2227. foreach ($userGroups as $groupInfo) {
  2228. $groupId = $groupInfo['iid'];
  2229. $userVisibility = api_get_item_visibility(
  2230. $courseInfo,
  2231. 'learnpath',
  2232. $row['id'],
  2233. $sessionId,
  2234. null,
  2235. 'LearnpathSubscription',
  2236. $groupId
  2237. );
  2238. if ($userVisibility == 1) {
  2239. $is_visible = true;
  2240. break;
  2241. }
  2242. }
  2243. }
  2244. }
  2245. }
  2246. return $is_visible;
  2247. }
  2248. return false;
  2249. }
  2250. /**
  2251. * @param int $lpId
  2252. * @param int $userId
  2253. * @param int $courseId
  2254. * @param int $sessionId
  2255. * @return int
  2256. */
  2257. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2258. {
  2259. $lpId = intval($lpId);
  2260. $userId = intval($userId);
  2261. $courseId = intval($courseId);
  2262. $sessionId = intval($sessionId);
  2263. $progress = 0;
  2264. $sessionCondition = api_get_session_condition($sessionId);
  2265. $table = Database::get_course_table(TABLE_LP_VIEW);
  2266. $sql = "SELECT * FROM $table
  2267. WHERE
  2268. c_id = ".$courseId." AND
  2269. lp_id = $lpId AND
  2270. user_id = $userId $sessionCondition";
  2271. $res = Database::query($sql);
  2272. if (Database::num_rows($res) > 0) {
  2273. $row = Database:: fetch_array($res);
  2274. $progress = $row['progress'];
  2275. }
  2276. return (int) $progress;
  2277. }
  2278. /**
  2279. * Displays a progress bar
  2280. * completed so far.
  2281. * @param integer $percentage Progress value to display
  2282. * @param string $text_add Text to display near the progress value
  2283. * @return string HTML string containing the progress bar
  2284. */
  2285. public static function get_progress_bar($percentage = -1, $text_add = '')
  2286. {
  2287. $text = $percentage . $text_add;
  2288. $output = '<div class="progress">
  2289. <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.';">
  2290. '. $text .'
  2291. </div>
  2292. </div>';
  2293. return $output;
  2294. }
  2295. /**
  2296. * @param string $mode can be '%' or 'abs'
  2297. * otherwise this value will be used $this->progress_bar_mode
  2298. * @return string
  2299. */
  2300. public function getProgressBar($mode = null)
  2301. {
  2302. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2303. return self::get_progress_bar($percentage, $text_add);
  2304. }
  2305. /**
  2306. * Gets the progress bar info to display inside the progress bar.
  2307. * Also used by scorm_api.php
  2308. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2309. * we display a number of completed elements per total elements
  2310. * @param integer $add Additional steps to fake as completed
  2311. * @return list array Percentage or number and symbol (% or /xx)
  2312. */
  2313. public function get_progress_bar_text($mode = '', $add = 0)
  2314. {
  2315. if ($this->debug > 0) {
  2316. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2317. }
  2318. if (empty($mode)) {
  2319. $mode = $this->progress_bar_mode;
  2320. }
  2321. $total_items = $this->getTotalItemsCountWithoutDirs();
  2322. if ($this->debug > 2) {
  2323. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2324. }
  2325. $completeItems = $this->get_complete_items_count();
  2326. if ($this->debug > 2) {
  2327. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2328. }
  2329. if ($add != 0) {
  2330. $completeItems += $add;
  2331. if ($this->debug > 2) {
  2332. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2333. }
  2334. }
  2335. $text = '';
  2336. if ($completeItems > $total_items) {
  2337. $completeItems = $total_items;
  2338. }
  2339. $percentage = 0;
  2340. if ($mode == '%') {
  2341. if ($total_items > 0) {
  2342. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2343. } else {
  2344. $percentage = 0;
  2345. }
  2346. $percentage = number_format($percentage, 0);
  2347. $text = '%';
  2348. } elseif ($mode == 'abs') {
  2349. $percentage = $completeItems;
  2350. $text = '/' . $total_items;
  2351. }
  2352. return array(
  2353. $percentage,
  2354. $text
  2355. );
  2356. }
  2357. /**
  2358. * Gets the progress bar mode
  2359. * @return string The progress bar mode attribute
  2360. */
  2361. public function get_progress_bar_mode()
  2362. {
  2363. if ($this->debug > 0) {
  2364. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2365. }
  2366. if (!empty ($this->progress_bar_mode)) {
  2367. return $this->progress_bar_mode;
  2368. } else {
  2369. return '%';
  2370. }
  2371. }
  2372. /**
  2373. * Gets the learnpath proximity (remote or local)
  2374. * @return string Learnpath proximity
  2375. */
  2376. public function get_proximity()
  2377. {
  2378. if ($this->debug > 0) {
  2379. error_log('New LP - In learnpath::get_proximity()', 0);
  2380. }
  2381. if (!empty ($this->proximity)) {
  2382. return $this->proximity;
  2383. } else {
  2384. return '';
  2385. }
  2386. }
  2387. /**
  2388. * Gets the learnpath theme (remote or local)
  2389. * @return string Learnpath theme
  2390. */
  2391. public function get_theme()
  2392. {
  2393. if ($this->debug > 0) {
  2394. error_log('New LP - In learnpath::get_theme()', 0);
  2395. }
  2396. if (!empty ($this->theme)) {
  2397. return $this->theme;
  2398. } else {
  2399. return '';
  2400. }
  2401. }
  2402. /**
  2403. * Gets the learnpath session id
  2404. * @return int
  2405. */
  2406. public function get_lp_session_id()
  2407. {
  2408. if ($this->debug > 0) {
  2409. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2410. }
  2411. if (!empty ($this->lp_session_id)) {
  2412. return (int) $this->lp_session_id;
  2413. } else {
  2414. return 0;
  2415. }
  2416. }
  2417. /**
  2418. * Gets the learnpath image
  2419. * @return string Web URL of the LP image
  2420. */
  2421. public function get_preview_image()
  2422. {
  2423. if ($this->debug > 0) {
  2424. error_log('New LP - In learnpath::get_preview_image()', 0);
  2425. }
  2426. if (!empty($this->preview_image)) {
  2427. return $this->preview_image;
  2428. } else {
  2429. return '';
  2430. }
  2431. }
  2432. /**
  2433. * @param string $size
  2434. * @param string $path_type
  2435. * @return bool|string
  2436. */
  2437. public function get_preview_image_path($size = null, $path_type = 'web')
  2438. {
  2439. $preview_image = $this->get_preview_image();
  2440. if (isset($preview_image) && !empty($preview_image)) {
  2441. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2442. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2443. if (isset($size)) {
  2444. $info = pathinfo($preview_image);
  2445. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2446. if (file_exists($image_sys_path.$image_custom_size)) {
  2447. if ($path_type == 'web') {
  2448. return $image_path.$image_custom_size;
  2449. } else {
  2450. return $image_sys_path.$image_custom_size;
  2451. }
  2452. }
  2453. } else {
  2454. if ($path_type == 'web') {
  2455. return $image_path.$preview_image;
  2456. } else {
  2457. return $image_sys_path.$preview_image;
  2458. }
  2459. }
  2460. }
  2461. return false;
  2462. }
  2463. /**
  2464. * Gets the learnpath author
  2465. * @return string LP's author
  2466. */
  2467. public function get_author()
  2468. {
  2469. if ($this->debug > 0) {
  2470. error_log('New LP - In learnpath::get_author()', 0);
  2471. }
  2472. if (!empty ($this->author)) {
  2473. return $this->author;
  2474. } else {
  2475. return '';
  2476. }
  2477. }
  2478. /**
  2479. * Gets the learnpath author
  2480. * @return string LP's author
  2481. */
  2482. public function get_hide_toc_frame()
  2483. {
  2484. if ($this->debug > 0) {
  2485. error_log('New LP - In learnpath::get_author()', 0);
  2486. }
  2487. if (!empty ($this->hide_toc_frame)) {
  2488. return $this->hide_toc_frame;
  2489. } else {
  2490. return '';
  2491. }
  2492. }
  2493. /**
  2494. * Generate a new prerequisites string for a given item. If this item was a sco and
  2495. * its prerequisites were strings (instead of IDs), then transform those strings into
  2496. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2497. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2498. * same rule as the scorm_export() method
  2499. * @param integer Item ID
  2500. * @return string Prerequisites string ready for the export as SCORM
  2501. */
  2502. public function get_scorm_prereq_string($item_id)
  2503. {
  2504. if ($this->debug > 0) {
  2505. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2506. }
  2507. if (!is_object($this->items[$item_id])) {
  2508. return false;
  2509. }
  2510. /** @var learnpathItem $oItem */
  2511. $oItem = $this->items[$item_id];
  2512. $prereq = $oItem->get_prereq_string();
  2513. if (empty($prereq)) {
  2514. return '';
  2515. }
  2516. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2517. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2518. // then simply return it (with the ITEM_ prefix).
  2519. //return 'ITEM_' . $prereq;
  2520. return $this->items[$prereq]->ref;
  2521. } else {
  2522. if (isset($this->refs_list[$prereq])) {
  2523. // It's a simple string item from which the ID can be found in the refs list,
  2524. // so we can transform it directly to an ID for export.
  2525. return $this->items[$this->refs_list[$prereq]]->ref;
  2526. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2527. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2528. } else {
  2529. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2530. // and replace them, one by one, by the internal IDs (chamilo db)
  2531. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2532. // by a space as well.
  2533. $find = array (
  2534. '&',
  2535. '|',
  2536. '~',
  2537. '=',
  2538. '<>',
  2539. '{',
  2540. '}',
  2541. '*',
  2542. '(',
  2543. ')'
  2544. );
  2545. $replace = array (
  2546. ' ',
  2547. ' ',
  2548. ' ',
  2549. ' ',
  2550. ' ',
  2551. ' ',
  2552. ' ',
  2553. ' ',
  2554. ' ',
  2555. ' '
  2556. );
  2557. $prereq_mod = str_replace($find, $replace, $prereq);
  2558. $ids = explode(' ', $prereq_mod);
  2559. foreach ($ids as $id) {
  2560. $id = trim($id);
  2561. if (isset ($this->refs_list[$id])) {
  2562. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2563. }
  2564. }
  2565. return $prereq;
  2566. }
  2567. }
  2568. }
  2569. /**
  2570. * Returns the XML DOM document's node
  2571. * @param resource Reference to a list of objects to search for the given ITEM_*
  2572. * @param string The identifier to look for
  2573. * @return mixed The reference to the element found with that identifier. False if not found
  2574. */
  2575. public function get_scorm_xml_node(& $children, $id)
  2576. {
  2577. for ($i = 0; $i < $children->length; $i++) {
  2578. $item_temp = $children->item($i);
  2579. if ($item_temp->nodeName == 'item') {
  2580. if ($item_temp->getAttribute('identifier') == $id) {
  2581. return $item_temp;
  2582. }
  2583. }
  2584. $subchildren = $item_temp->childNodes;
  2585. if ($subchildren && $subchildren->length > 0) {
  2586. $val = $this->get_scorm_xml_node($subchildren, $id);
  2587. if (is_object($val)) {
  2588. return $val;
  2589. }
  2590. }
  2591. }
  2592. return false;
  2593. }
  2594. /**
  2595. * Gets the status list for all LP's items
  2596. * @return array Array of [index] => [item ID => current status]
  2597. */
  2598. public function get_items_status_list()
  2599. {
  2600. if ($this->debug > 0) {
  2601. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2602. }
  2603. $list = array ();
  2604. foreach ($this->ordered_items as $item_id) {
  2605. $list[] = array (
  2606. $item_id => $this->items[$item_id]->get_status()
  2607. );
  2608. }
  2609. return $list;
  2610. }
  2611. /**
  2612. * Return the number of interactions for the given learnpath Item View ID.
  2613. * This method can be used as static.
  2614. * @param integer Item View ID
  2615. * @param integer course id
  2616. * @return integer Number of interactions
  2617. */
  2618. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2619. {
  2620. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2621. $lp_iv_id = intval($lp_iv_id);
  2622. $course_id = intval($course_id);
  2623. $sql = "SELECT count(*) FROM $table
  2624. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2625. $res = Database::query($sql);
  2626. $num = 0;
  2627. if (Database::num_rows($res)) {
  2628. $row = Database::fetch_array($res);
  2629. $num = $row[0];
  2630. }
  2631. return $num;
  2632. }
  2633. /**
  2634. * Return the interactions as an array for the given lp_iv_id.
  2635. * This method can be used as static.
  2636. * @param integer Learnpath Item View ID
  2637. * @return array
  2638. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2639. */
  2640. public static function get_iv_interactions_array($lp_iv_id)
  2641. {
  2642. $course_id = api_get_course_int_id();
  2643. $list = array();
  2644. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2645. if (empty($lp_iv_id)) {
  2646. return array();
  2647. }
  2648. $sql = "SELECT * FROM $table
  2649. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2650. ORDER BY order_id ASC";
  2651. $res = Database::query($sql);
  2652. $num = Database::num_rows($res);
  2653. if ($num > 0) {
  2654. $list[] = array (
  2655. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2656. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2657. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2658. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2659. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2660. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2661. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2662. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2663. );
  2664. while ($row = Database::fetch_array($res)) {
  2665. $list[] = array (
  2666. 'order_id' => ($row['order_id'] + 1),
  2667. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2668. 'type' => $row['interaction_type'],
  2669. 'time' => $row['completion_time'],
  2670. //'correct_responses' => $row['correct_responses'],
  2671. 'correct_responses' => '', // Hide correct responses from students.
  2672. 'student_response' => $row['student_response'],
  2673. 'result' => $row['result'],
  2674. 'latency' => $row['latency']
  2675. );
  2676. }
  2677. }
  2678. return $list;
  2679. }
  2680. /**
  2681. * Return the number of objectives for the given learnpath Item View ID.
  2682. * This method can be used as static.
  2683. * @param integer Item View ID
  2684. * @return integer Number of objectives
  2685. */
  2686. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2687. {
  2688. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2689. $course_id = intval($course_id);
  2690. $lp_iv_id = intval($lp_iv_id);
  2691. $sql = "SELECT count(*) FROM $table
  2692. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2693. //@todo seems that this always returns 0
  2694. $res = Database::query($sql);
  2695. $num = 0;
  2696. if (Database::num_rows($res)) {
  2697. $row = Database::fetch_array($res);
  2698. $num = $row[0];
  2699. }
  2700. return $num;
  2701. }
  2702. /**
  2703. * Return the objectives as an array for the given lp_iv_id.
  2704. * This method can be used as static.
  2705. * @param integer Learnpath Item View ID
  2706. * @return array
  2707. * @todo Translate labels
  2708. */
  2709. public static function get_iv_objectives_array($lp_iv_id = 0)
  2710. {
  2711. $course_id = api_get_course_int_id();
  2712. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2713. $sql = "SELECT * FROM $table
  2714. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2715. ORDER BY order_id ASC";
  2716. $res = Database::query($sql);
  2717. $num = Database::num_rows($res);
  2718. $list = array();
  2719. if ($num > 0) {
  2720. $list[] = array(
  2721. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2722. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2723. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2724. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2725. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2726. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2727. );
  2728. while ($row = Database::fetch_array($res)) {
  2729. $list[] = array (
  2730. 'order_id' => ($row['order_id'] + 1),
  2731. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2732. 'score_raw' => $row['score_raw'],
  2733. 'score_max' => $row['score_max'],
  2734. 'score_min' => $row['score_min'],
  2735. 'status' => $row['status']
  2736. );
  2737. }
  2738. }
  2739. return $list;
  2740. }
  2741. /**
  2742. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2743. * used by get_html_toc() to be ready to display
  2744. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2745. */
  2746. public function get_toc()
  2747. {
  2748. if ($this->debug > 0) {
  2749. error_log('learnpath::get_toc()', 0);
  2750. }
  2751. $toc = array();
  2752. foreach ($this->ordered_items as $item_id) {
  2753. if ($this->debug > 2) {
  2754. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2755. }
  2756. // TODO: Change this link generation and use new function instead.
  2757. $toc[] = array(
  2758. 'id' => $item_id,
  2759. 'title' => $this->items[$item_id]->get_title(),
  2760. 'status' => $this->items[$item_id]->get_status(),
  2761. 'level' => $this->items[$item_id]->get_level(),
  2762. 'type' => $this->items[$item_id]->get_type(),
  2763. 'description' => $this->items[$item_id]->get_description(),
  2764. 'path' => $this->items[$item_id]->get_path(),
  2765. 'parent' => $this->items[$item_id]->get_parent(),
  2766. );
  2767. }
  2768. if ($this->debug > 2) {
  2769. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2770. }
  2771. return $toc;
  2772. }
  2773. /**
  2774. * Generate and return the table of contents for this learnpath. The JS
  2775. * table returned is used inside of scorm_api.php
  2776. * @return string A JS array vairiable construction
  2777. */
  2778. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2779. {
  2780. if ($this->debug > 0) {
  2781. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2782. }
  2783. $toc = $varname.' = new Array();';
  2784. foreach ($this->ordered_items as $item_id) {
  2785. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2786. }
  2787. if ($this->debug > 2) {
  2788. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2789. }
  2790. return $toc;
  2791. }
  2792. /**
  2793. * Gets the learning path type
  2794. * @param boolean Return the name? If false, return the ID. Default is false.
  2795. * @return mixed Type ID or name, depending on the parameter
  2796. */
  2797. public function get_type($get_name = false)
  2798. {
  2799. $res = false;
  2800. if ($this->debug > 0) {
  2801. error_log('New LP - In learnpath::get_type()', 0);
  2802. }
  2803. if (!empty ($this->type) && (!$get_name)) {
  2804. $res = $this->type;
  2805. }
  2806. if ($this->debug > 2) {
  2807. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2808. }
  2809. return $res;
  2810. }
  2811. /**
  2812. * Gets the learning path type as static method
  2813. * @param boolean Return the name? If false, return the ID. Default is false.
  2814. * @return mixed Type ID or name, depending on the parameter
  2815. */
  2816. public static function get_type_static($lp_id = 0)
  2817. {
  2818. $course_id = api_get_course_int_id();
  2819. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2820. $lp_id = intval($lp_id);
  2821. $sql = "SELECT lp_type FROM $tbl_lp
  2822. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2823. $res = Database::query($sql);
  2824. if ($res === false) {
  2825. return null;
  2826. }
  2827. if (Database::num_rows($res) <= 0) {
  2828. return null;
  2829. }
  2830. $row = Database::fetch_array($res);
  2831. return $row['lp_type'];
  2832. }
  2833. /**
  2834. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2835. * This method can be used as abstract and is recursive
  2836. * @param integer Learnpath ID
  2837. * @param integer Parent ID of the items to look for
  2838. * @return mixed Ordered list of item IDs or false on error
  2839. */
  2840. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2841. {
  2842. if (empty($course_id)) {
  2843. $course_id = api_get_course_int_id();
  2844. } else {
  2845. $course_id = intval($course_id);
  2846. }
  2847. $list = array();
  2848. if (empty($lp)) {
  2849. return false;
  2850. }
  2851. $lp = intval($lp);
  2852. $parent = intval($parent);
  2853. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2854. $sql = "SELECT id FROM $tbl_lp_item
  2855. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2856. ORDER BY display_order";
  2857. $res = Database::query($sql);
  2858. while ($row = Database::fetch_array($res)) {
  2859. $sublist = self::get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2860. $list[] = $row['id'];
  2861. foreach ($sublist as $item) {
  2862. $list[] = $item;
  2863. }
  2864. }
  2865. return $list;
  2866. }
  2867. /**
  2868. * @return array
  2869. */
  2870. public static function getChapterTypes()
  2871. {
  2872. return array(
  2873. 'dir'
  2874. );
  2875. }
  2876. /**
  2877. * Uses the table generated by get_toc() and returns an HTML-formattedstring ready to display
  2878. * @return string HTML TOC ready to display
  2879. */
  2880. public function getParentToc($tree)
  2881. {
  2882. if ($this->debug > 0) {
  2883. error_log('In learnpath::get_html_toc()', 0);
  2884. }
  2885. if (empty($tree)) {
  2886. $tree = $this->get_toc();
  2887. }
  2888. $dirTypes = self::getChapterTypes();
  2889. $myCurrentId = $this->get_current_item_id();
  2890. $listParent = [];
  2891. $listChildren = [];
  2892. $listNotParent = [];
  2893. $list = [];
  2894. foreach ($tree as $subtree) {
  2895. if (in_array($subtree['type'], $dirTypes)){
  2896. $listChildren = $this->getChildrenToc($tree, $subtree['id']);
  2897. $subtree['children'] = $listChildren;
  2898. if (!empty($subtree['children'])) {
  2899. foreach ($subtree['children'] as $subItem) {
  2900. if ($subItem['id'] == $this->current) {
  2901. $subtree['parent_current'] = 'in';
  2902. $subtree['current'] = 'on';
  2903. }
  2904. }
  2905. }
  2906. $listParent[] = $subtree;
  2907. }
  2908. if (!in_array($subtree['type'], $dirTypes) && $subtree['parent'] == null ) {
  2909. $classStatus = [
  2910. 'not attempted' => 'scorm_not_attempted',
  2911. 'incomplete' => 'scorm_not_attempted',
  2912. 'failed' => 'scorm_failed',
  2913. 'completed' => 'scorm_completed',
  2914. 'passed' => 'scorm_completed',
  2915. 'succeeded' => 'scorm_completed',
  2916. 'browsed' => 'scorm_completed',
  2917. ];
  2918. if (isset($classStatus[$subtree['status']])) {
  2919. $cssStatus = $classStatus[$subtree['status']];
  2920. }
  2921. $title = Security::remove_XSS($subtree['title']);
  2922. unset($subtree['title']);
  2923. if (empty ($title)) {
  2924. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $subtree['id']);
  2925. }
  2926. $classStyle = null;
  2927. if ($subtree['id'] == $this->current) {
  2928. $classStyle = 'scorm_item_normal '. $classStyle . 'scorm_highlight';
  2929. } elseif (!in_array($subtree['type'], $dirTypes)) {
  2930. $classStyle = 'scorm_item_normal '. $classStyle . ' ';
  2931. }
  2932. $subtree['title'] = $title;
  2933. $subtree['class'] = $cssStatus . ' ' .$classStyle;
  2934. $subtree['url'] = $this->get_link('http', $subtree['id'], $tree);
  2935. $subtree['current_id'] = $myCurrentId;
  2936. $listNotParent[] = $subtree;
  2937. }
  2938. }
  2939. $list['are_parents'] = $listParent;
  2940. $list['not_parents'] = $listNotParent;
  2941. return $list;
  2942. }
  2943. /**
  2944. * Uses the table generated by get_toc() and returns an HTML-formattedstring ready to display
  2945. * @return string HTML TOC ready to display
  2946. */
  2947. public function getChildrenToc($tree, $id, $parent = true)
  2948. {
  2949. if ($this->debug > 0) {
  2950. error_log('In learnpath::get_html_toc()', 0);
  2951. }
  2952. if (empty($tree)) {
  2953. $tree = $this->get_toc();
  2954. }
  2955. $dirTypes = self::getChapterTypes();
  2956. $mycurrentitemid = $this->get_current_item_id();
  2957. $list = [];
  2958. $classStatus = [
  2959. 'not attempted' => 'scorm_not_attempted',
  2960. 'incomplete' => 'scorm_not_attempted',
  2961. 'failed' => 'scorm_failed',
  2962. 'completed' => 'scorm_completed',
  2963. 'passed' => 'scorm_completed',
  2964. 'succeeded' => 'scorm_completed',
  2965. 'browsed' => 'scorm_completed',
  2966. ];
  2967. foreach ($tree as $subtree) {
  2968. $subtree['tree'] = null;
  2969. if (!in_array($subtree['type'], $dirTypes) && $subtree['parent'] == $id ) {
  2970. if ($subtree['id'] == $this->current) {
  2971. $subtree['current'] = 'active';
  2972. } else {
  2973. $subtree['current'] = null;
  2974. }
  2975. if (isset($classStatus[$subtree['status']])) {
  2976. $cssStatus = $classStatus[$subtree['status']];
  2977. }
  2978. $title = Security::remove_XSS($subtree['title']);
  2979. unset($subtree['title']);
  2980. if (empty ($title)) {
  2981. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $subtree['id']);
  2982. }
  2983. $classStyle = null;
  2984. if ($subtree['id'] == $this->current) {
  2985. $classStyle = 'scorm_item_normal '. $classStyle . 'scorm_highlight';
  2986. } elseif (!in_array($subtree['type'], $dirTypes)) {
  2987. $classStyle = 'scorm_item_normal '. $classStyle . ' ';
  2988. }
  2989. if (in_array($subtree['type'], $dirTypes)) {
  2990. $subtree['title'] = stripslashes($title);
  2991. } else {
  2992. $subtree['title'] = $title;
  2993. $subtree['class'] = $cssStatus . ' ' .$classStyle;
  2994. $subtree['url'] = $this->get_link('http', $subtree['id'], $tree);
  2995. $subtree['current_id'] = $mycurrentitemid;
  2996. }
  2997. $list[] = $subtree;
  2998. }
  2999. }
  3000. return $list;
  3001. }
  3002. /**
  3003. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  3004. * @return string HTML TOC ready to display
  3005. */
  3006. public function getListArrayToc($toc_list = null)
  3007. {
  3008. if ($this->debug > 0) {
  3009. error_log('In learnpath::get_html_toc()', 0);
  3010. }
  3011. if (empty($toc_list)) {
  3012. $toc_list = $this->get_toc();
  3013. }
  3014. // Temporary variables.
  3015. $mycurrentitemid = $this->get_current_item_id();
  3016. $list = [];
  3017. $arrayList = [];
  3018. $classStatus = [
  3019. 'not attempted' => 'scorm_not_attempted',
  3020. 'incomplete' => 'scorm_not_attempted',
  3021. 'failed' => 'scorm_failed',
  3022. 'completed' => 'scorm_completed',
  3023. 'passed' => 'scorm_completed',
  3024. 'succeeded' => 'scorm_completed',
  3025. 'browsed' => 'scorm_completed',
  3026. ];
  3027. foreach ($toc_list as $item) {
  3028. $list['id'] = $item['id'];
  3029. $list['status'] = $item['status'];
  3030. $cssStatus = null;
  3031. if (isset($classStatus[$item['status']])) {
  3032. $cssStatus = $classStatus[$item['status']];
  3033. }
  3034. $classStyle = ' ';
  3035. $dirTypes = self::getChapterTypes();
  3036. if (in_array($item['type'], $dirTypes)) {
  3037. $classStyle = 'scorm_item_section ';
  3038. }
  3039. if ($item['id'] == $this->current) {
  3040. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  3041. } elseif (!in_array($item['type'], $dirTypes)) {
  3042. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  3043. }
  3044. $title = $item['title'];
  3045. if (empty($title)) {
  3046. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  3047. }
  3048. $title = Security::remove_XSS($item['title']);
  3049. if (empty($item['description'])) {
  3050. $list['description'] = $title;
  3051. } else {
  3052. $list['description'] = $item['description'];
  3053. }
  3054. $list['class'] = $classStyle.' '.$cssStatus;
  3055. $list['level'] = $item['level'];
  3056. $list['type'] = $item['type'];
  3057. if (in_array($item['type'], $dirTypes)) {
  3058. $list['css_level'] = 'level_'.$item['level'];
  3059. } else {
  3060. $list['css_level'] = 'level_'.$item['level']
  3061. .' scorm_type_'
  3062. .learnpath::format_scorm_type_item($item['type']);
  3063. }
  3064. if (in_array($item['type'], $dirTypes)) {
  3065. $list['title'] = stripslashes($title);
  3066. } else {
  3067. $list['title'] = stripslashes($title);
  3068. $list['url'] = $this->get_link('http', $item['id'], $toc_list);
  3069. $list['current_id'] = $mycurrentitemid;
  3070. }
  3071. $arrayList[] = $list;
  3072. }
  3073. return $arrayList;
  3074. }
  3075. /**
  3076. * Returns an HTML-formatted string ready to display with teacher buttons
  3077. * in LP view menu
  3078. * @return string HTML TOC ready to display
  3079. */
  3080. public function get_teacher_toc_buttons()
  3081. {
  3082. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  3083. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  3084. $html = '';
  3085. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  3086. $gradebook = '';
  3087. if (!empty($_GET['gradebook'])) {
  3088. $gradebook = Security::remove_XSS($_GET['gradebook']);
  3089. }
  3090. if ($this->get_lp_session_id() == api_get_session_id()) {
  3091. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3092. $html .= '<div class="btn-group">';
  3093. $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'>" .
  3094. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  3095. $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'>" .
  3096. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  3097. $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">' .
  3098. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  3099. $html .= '</div>';
  3100. $html .= '</div>';
  3101. }
  3102. }
  3103. return $html;
  3104. }
  3105. /**
  3106. * Gets the learnpath maker name - generally the editor's name
  3107. * @return string Learnpath maker name
  3108. */
  3109. public function get_maker()
  3110. {
  3111. if ($this->debug > 0) {
  3112. error_log('New LP - In learnpath::get_maker()', 0);
  3113. }
  3114. if (!empty ($this->maker)) {
  3115. return $this->maker;
  3116. } else {
  3117. return '';
  3118. }
  3119. }
  3120. /**
  3121. * Gets the learnpath name/title
  3122. * @return string Learnpath name/title
  3123. */
  3124. public function get_name()
  3125. {
  3126. if ($this->debug > 0) {
  3127. error_log('New LP - In learnpath::get_name()', 0);
  3128. }
  3129. if (!empty ($this->name)) {
  3130. return $this->name;
  3131. } else {
  3132. return 'N/A';
  3133. }
  3134. }
  3135. /**
  3136. * Gets a link to the resource from the present location, depending on item ID.
  3137. * @param string $type Type of link expected
  3138. * @param integer $item_id Learnpath item ID
  3139. * @return string $provided_toc Link to the lp_item resource
  3140. */
  3141. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3142. {
  3143. $course_id = $this->get_course_int_id();
  3144. if ($this->debug > 0) {
  3145. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3146. }
  3147. if (empty($item_id)) {
  3148. if ($this->debug > 2) {
  3149. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3150. }
  3151. $item_id = $this->get_current_item_id();
  3152. }
  3153. if (empty($item_id)) {
  3154. if ($this->debug > 2) {
  3155. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3156. }
  3157. //still empty, this means there was no item_id given and we are not in an object context or
  3158. //the object property is empty, return empty link
  3159. $item_id = $this->first();
  3160. return '';
  3161. }
  3162. $file = '';
  3163. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3164. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3165. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3166. $item_id = intval($item_id);
  3167. $sql = "SELECT
  3168. l.lp_type as ltype,
  3169. l.path as lpath,
  3170. li.item_type as litype,
  3171. li.path as lipath,
  3172. li.parameters as liparams
  3173. FROM $lp_table l
  3174. INNER JOIN $lp_item_table li
  3175. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3176. WHERE li.id = $item_id ";
  3177. if ($this->debug > 2) {
  3178. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3179. }
  3180. $res = Database::query($sql);
  3181. if (Database::num_rows($res) > 0) {
  3182. $row = Database::fetch_array($res);
  3183. $lp_type = $row['ltype'];
  3184. $lp_path = $row['lpath'];
  3185. $lp_item_type = $row['litype'];
  3186. $lp_item_path = $row['lipath'];
  3187. $lp_item_params = $row['liparams'];
  3188. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3189. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3190. }
  3191. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3192. if ($type == 'http') {
  3193. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3194. } else {
  3195. $course_path = $sys_course_path; //system path
  3196. }
  3197. // 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.
  3198. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3199. $lp_type = 1;
  3200. }
  3201. if ($this->debug > 2) {
  3202. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3203. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3204. }
  3205. // Now go through the specific cases to get the end of the path
  3206. // @todo Use constants instead of int values.
  3207. switch ($lp_type) {
  3208. case 1 :
  3209. $file = self::rl_get_resource_link_for_learnpath(
  3210. $course_id,
  3211. $this->get_id(),
  3212. $item_id,
  3213. $this->get_view_id()
  3214. );
  3215. if ($this->debug > 0) {
  3216. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3217. }
  3218. switch ($lp_item_type) {
  3219. case 'dir':
  3220. $file = 'lp_content.php?type=dir';
  3221. break;
  3222. case 'link':
  3223. if (Link::is_youtube_link($file)) {
  3224. $src = Link::get_youtube_video_id($file);
  3225. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=youtube&source='.$src;
  3226. } elseif (Link::isVimeoLink($file)) {
  3227. $src = Link::getVimeoLinkId($file);
  3228. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=vimeo&source='.$src;
  3229. } else {
  3230. // If the current site is HTTPS and the link is
  3231. // HTTP, browsers will refuse opening the link
  3232. $urlId = api_get_current_access_url_id();
  3233. $url = api_get_access_url($urlId, false);
  3234. $protocol = substr($url['url'], 0, 5);
  3235. if ($protocol === 'https') {
  3236. $linkProtocol = substr($file, 0, 5);
  3237. if ($linkProtocol === 'http:') {
  3238. //this is the special intervention case
  3239. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3240. }
  3241. }
  3242. }
  3243. break;
  3244. case 'quiz':
  3245. // Check how much attempts of a exercise exits in lp
  3246. $lp_item_id = $this->get_current_item_id();
  3247. $lp_view_id = $this->get_view_id();
  3248. $prevent_reinit = null;
  3249. if (isset($this->items[$this->current])) {
  3250. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3251. }
  3252. if (empty($provided_toc)) {
  3253. if ($this->debug > 0) {
  3254. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3255. }
  3256. $list = $this->get_toc();
  3257. } else {
  3258. if ($this->debug > 0) {
  3259. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3260. }
  3261. $list = $provided_toc;
  3262. }
  3263. $type_quiz = false;
  3264. foreach ($list as $toc) {
  3265. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3266. $type_quiz = true;
  3267. }
  3268. }
  3269. if ($type_quiz) {
  3270. $lp_item_id = intval($lp_item_id);
  3271. $lp_view_id = intval($lp_view_id);
  3272. $sql = "SELECT count(*) FROM $lp_item_view_table
  3273. WHERE
  3274. c_id = $course_id AND
  3275. lp_item_id='".$lp_item_id."' AND
  3276. lp_view_id ='".$lp_view_id."' AND
  3277. status='completed'";
  3278. $result = Database::query($sql);
  3279. $row_count = Database:: fetch_row($result);
  3280. $count_item_view = (int)$row_count[0];
  3281. $not_multiple_attempt = 0;
  3282. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3283. $not_multiple_attempt = 1;
  3284. }
  3285. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3286. }
  3287. break;
  3288. }
  3289. $tmp_array = explode('/', $file);
  3290. $document_name = $tmp_array[count($tmp_array) - 1];
  3291. if (strpos($document_name, '_DELETED_')) {
  3292. $file = 'blank.php?error=document_deleted';
  3293. }
  3294. break;
  3295. case 2 :
  3296. if ($this->debug > 2) {
  3297. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3298. }
  3299. if ($lp_item_type != 'dir') {
  3300. // Quite complex here:
  3301. // We want to make sure 'http://' (and similar) links can
  3302. // be loaded as is (withouth the Chamilo path in front) but
  3303. // some contents use this form: resource.htm?resource=http://blablabla
  3304. // which means we have to find a protocol at the path's start, otherwise
  3305. // it should not be considered as an external URL.
  3306. //if ($this->prerequisites_match($item_id)) {
  3307. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3308. if ($this->debug > 2) {
  3309. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3310. }
  3311. // Distant url, return as is.
  3312. $file = $lp_item_path;
  3313. } else {
  3314. if ($this->debug > 2) {
  3315. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3316. }
  3317. // Prevent getting untranslatable urls.
  3318. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3319. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3320. // Prepare the path.
  3321. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3322. // TODO: Fix this for urls with protocol header.
  3323. $file = str_replace('//', '/', $file);
  3324. $file = str_replace(':/', '://', $file);
  3325. if (substr($lp_path, -1) == '/') {
  3326. $lp_path = substr($lp_path, 0, -1);
  3327. }
  3328. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3329. // if file not found.
  3330. $decoded = html_entity_decode($lp_item_path);
  3331. list ($decoded) = explode('?', $decoded);
  3332. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3333. $file = self::rl_get_resource_link_for_learnpath(
  3334. $course_id,
  3335. $this->get_id(),
  3336. $item_id,
  3337. $this->get_view_id()
  3338. );
  3339. if (empty($file)) {
  3340. $file = 'blank.php?error=document_not_found';
  3341. } else {
  3342. $tmp_array = explode('/', $file);
  3343. $document_name = $tmp_array[count($tmp_array) - 1];
  3344. if (strpos($document_name, '_DELETED_')) {
  3345. $file = 'blank.php?error=document_deleted';
  3346. } else {
  3347. $file = 'blank.php?error=document_not_found';
  3348. }
  3349. }
  3350. } else {
  3351. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3352. }
  3353. }
  3354. }
  3355. // We want to use parameters if they were defined in the imsmanifest
  3356. if (strpos($file, 'blank.php') === false) {
  3357. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3358. }
  3359. } else {
  3360. $file = 'lp_content.php?type=dir';
  3361. }
  3362. break;
  3363. case 3 :
  3364. if ($this->debug > 2) {
  3365. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3366. }
  3367. // Formatting AICC HACP append URL.
  3368. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'lp/aicc_hacp.php') . '&';
  3369. if (!empty($lp_item_params)) {
  3370. $aicc_append .= $lp_item_params . '&';
  3371. }
  3372. if ($lp_item_type != 'dir') {
  3373. // Quite complex here:
  3374. // We want to make sure 'http://' (and similar) links can
  3375. // be loaded as is (withouth the Chamilo path in front) but
  3376. // some contents use this form: resource.htm?resource=http://blablabla
  3377. // which means we have to find a protocol at the path's start, otherwise
  3378. // it should not be considered as an external URL.
  3379. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3380. if ($this->debug > 2) {
  3381. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3382. }
  3383. // Distant url, return as is.
  3384. $file = $lp_item_path;
  3385. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3386. /*
  3387. if (stristr($file,'<servername>') !== false) {
  3388. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3389. }
  3390. */
  3391. if (stripos($file, '<servername>') !== false) {
  3392. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3393. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3394. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3395. }
  3396. //
  3397. $file .= $aicc_append;
  3398. } else {
  3399. if ($this->debug > 2) {
  3400. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3401. }
  3402. // Prevent getting untranslatable urls.
  3403. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3404. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3405. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3406. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3407. // TODO: Fix this for urls with protocol header.
  3408. $file = str_replace('//', '/', $file);
  3409. $file = str_replace(':/', '://', $file);
  3410. $file .= $aicc_append;
  3411. }
  3412. } else {
  3413. $file = 'lp_content.php?type=dir';
  3414. }
  3415. break;
  3416. case 4 :
  3417. break;
  3418. default:
  3419. break;
  3420. }
  3421. // Replace &amp; by & because &amp; will break URL with params
  3422. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3423. }
  3424. if ($this->debug > 2) {
  3425. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3426. }
  3427. return $file;
  3428. }
  3429. /**
  3430. * Gets the latest usable view or generate a new one
  3431. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3432. * @return integer DB lp_view id
  3433. */
  3434. public function get_view($attempt_num = 0)
  3435. {
  3436. if ($this->debug > 0) {
  3437. error_log('New LP - In learnpath::get_view()', 0);
  3438. }
  3439. $search = '';
  3440. // Use $attempt_num to enable multi-views management (disabled so far).
  3441. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3442. $search = 'AND view_count = ' . $attempt_num;
  3443. }
  3444. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3445. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3446. $course_id = api_get_course_int_id();
  3447. $sessionId = api_get_session_id();
  3448. $sql = "SELECT id, view_count FROM $lp_view_table
  3449. WHERE
  3450. c_id = " . $course_id . " AND
  3451. lp_id = " . $this->get_id() . " AND
  3452. user_id = " . $this->get_user_id() . " AND
  3453. session_id = $sessionId
  3454. $search
  3455. ORDER BY view_count DESC";
  3456. $res = Database::query($sql);
  3457. if (Database::num_rows($res) > 0) {
  3458. $row = Database::fetch_array($res);
  3459. $this->lp_view_id = $row['id'];
  3460. } else if (!api_is_invitee()) {
  3461. // There is no database record, create one.
  3462. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3463. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3464. Database::query($sql);
  3465. $id = Database::insert_id();
  3466. $this->lp_view_id = $id;
  3467. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3468. Database::query($sql);
  3469. }
  3470. return $this->lp_view_id;
  3471. }
  3472. /**
  3473. * Gets the current view id
  3474. * @return integer View ID (from lp_view)
  3475. */
  3476. public function get_view_id()
  3477. {
  3478. if ($this->debug > 0) {
  3479. error_log('New LP - In learnpath::get_view_id()', 0);
  3480. }
  3481. if (!empty ($this->lp_view_id)) {
  3482. return $this->lp_view_id;
  3483. } else {
  3484. return 0;
  3485. }
  3486. }
  3487. /**
  3488. * Gets the update queue
  3489. * @return array Array containing IDs of items to be updated by JavaScript
  3490. */
  3491. public function get_update_queue()
  3492. {
  3493. if ($this->debug > 0) {
  3494. error_log('New LP - In learnpath::get_update_queue()', 0);
  3495. }
  3496. return $this->update_queue;
  3497. }
  3498. /**
  3499. * Gets the user ID
  3500. * @return integer User ID
  3501. */
  3502. public function get_user_id()
  3503. {
  3504. if ($this->debug > 0) {
  3505. error_log('New LP - In learnpath::get_user_id()', 0);
  3506. }
  3507. if (!empty ($this->user_id)) {
  3508. return $this->user_id;
  3509. } else {
  3510. return false;
  3511. }
  3512. }
  3513. /**
  3514. * Checks if any of the items has an audio element attached
  3515. * @return bool True or false
  3516. */
  3517. public function has_audio()
  3518. {
  3519. if ($this->debug > 1) {
  3520. error_log('New LP - In learnpath::has_audio()', 0);
  3521. }
  3522. $has = false;
  3523. foreach ($this->items as $i => $item) {
  3524. if (!empty ($this->items[$i]->audio)) {
  3525. $has = true;
  3526. break;
  3527. }
  3528. }
  3529. return $has;
  3530. }
  3531. /**
  3532. * Logs a message into a file
  3533. * @param string Message to log
  3534. * @return boolean True on success, false on error or if msg empty
  3535. */
  3536. public function log($msg)
  3537. {
  3538. if ($this->debug > 0) {
  3539. error_log('New LP - In learnpath::log()', 0);
  3540. }
  3541. // TODO
  3542. $this->error .= $msg;
  3543. return true;
  3544. }
  3545. /**
  3546. * Moves an item up and down at its level
  3547. * @param integer Item to move up and down
  3548. * @param string Direction 'up' or 'down'
  3549. * @return integer New display order, or false on error
  3550. */
  3551. public function move_item($id, $direction)
  3552. {
  3553. $course_id = api_get_course_int_id();
  3554. if ($this->debug > 0) {
  3555. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3556. }
  3557. if (empty($id) || empty($direction)) {
  3558. return false;
  3559. }
  3560. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3561. $sql_sel = "SELECT *
  3562. FROM " . $tbl_lp_item . "
  3563. WHERE c_id = ".$course_id." AND id = " . $id;
  3564. $res_sel = Database::query($sql_sel);
  3565. // Check if elem exists.
  3566. if (Database::num_rows($res_sel) < 1) {
  3567. return false;
  3568. }
  3569. // Gather data.
  3570. $row = Database::fetch_array($res_sel);
  3571. $previous = $row['previous_item_id'];
  3572. $next = $row['next_item_id'];
  3573. $display = $row['display_order'];
  3574. $parent = $row['parent_item_id'];
  3575. $lp = $row['lp_id'];
  3576. // Update the item (switch with previous/next one).
  3577. switch ($direction) {
  3578. case 'up':
  3579. if ($this->debug > 2) {
  3580. error_log('Movement up detected', 0);
  3581. }
  3582. if ($display > 1) {
  3583. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3584. WHERE c_id = ".$course_id." AND id = $previous";
  3585. if ($this->debug > 2) {
  3586. error_log('Selecting previous: ' . $sql_sel2, 0);
  3587. }
  3588. $res_sel2 = Database::query($sql_sel2);
  3589. if (Database::num_rows($res_sel2) < 1) {
  3590. $previous_previous = 0;
  3591. }
  3592. // Gather data.
  3593. $row2 = Database::fetch_array($res_sel2);
  3594. $previous_previous = $row2['previous_item_id'];
  3595. // Update previous_previous item (switch "next" with current).
  3596. if ($previous_previous != 0) {
  3597. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3598. next_item_id = $id
  3599. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3600. if ($this->debug > 2) {
  3601. error_log($sql_upd2, 0);
  3602. }
  3603. Database::query($sql_upd2);
  3604. }
  3605. // Update previous item (switch with current).
  3606. if ($previous != 0) {
  3607. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3608. next_item_id = $next,
  3609. previous_item_id = $id,
  3610. display_order = display_order +1
  3611. WHERE c_id = ".$course_id." AND id = $previous";
  3612. if ($this->debug > 2) {
  3613. error_log($sql_upd2, 0);
  3614. }
  3615. Database::query($sql_upd2);
  3616. }
  3617. // Update current item (switch with previous).
  3618. if ($id != 0) {
  3619. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3620. next_item_id = $previous,
  3621. previous_item_id = $previous_previous,
  3622. display_order = display_order-1
  3623. WHERE c_id = ".$course_id." AND id = $id";
  3624. if ($this->debug > 2) {
  3625. error_log($sql_upd2, 0);
  3626. }
  3627. Database::query($sql_upd2);
  3628. }
  3629. // Update next item (new previous item).
  3630. if ($next != 0) {
  3631. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3632. WHERE c_id = ".$course_id." AND id = $next";
  3633. if ($this->debug > 2) {
  3634. error_log($sql_upd2, 0);
  3635. }
  3636. Database::query($sql_upd2);
  3637. }
  3638. $display = $display -1;
  3639. }
  3640. break;
  3641. case 'down':
  3642. if ($this->debug > 2) {
  3643. error_log('Movement down detected', 0);
  3644. }
  3645. if ($next != 0) {
  3646. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3647. if ($this->debug > 2) {
  3648. error_log('Selecting next: ' . $sql_sel2, 0);
  3649. }
  3650. $res_sel2 = Database::query($sql_sel2);
  3651. if (Database::num_rows($res_sel2) < 1) {
  3652. $next_next = 0;
  3653. }
  3654. // Gather data.
  3655. $row2 = Database::fetch_array($res_sel2);
  3656. $next_next = $row2['next_item_id'];
  3657. // Update previous item (switch with current).
  3658. if ($previous != 0) {
  3659. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3660. WHERE c_id = ".$course_id." AND id = $previous";
  3661. Database::query($sql_upd2);
  3662. }
  3663. // Update current item (switch with previous).
  3664. if ($id != 0) {
  3665. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3666. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3667. WHERE c_id = ".$course_id." AND id = $id";
  3668. Database::query($sql_upd2);
  3669. }
  3670. // Update next item (new previous item).
  3671. if ($next != 0) {
  3672. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3673. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3674. WHERE c_id = ".$course_id." AND id = $next";
  3675. Database::query($sql_upd2);
  3676. }
  3677. // Update next_next item (switch "previous" with current).
  3678. if ($next_next != 0) {
  3679. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3680. previous_item_id = $id
  3681. WHERE c_id = ".$course_id." AND id = $next_next";
  3682. Database::query($sql_upd2);
  3683. }
  3684. $display = $display +1;
  3685. }
  3686. break;
  3687. default:
  3688. return false;
  3689. }
  3690. return $display;
  3691. }
  3692. /**
  3693. * Move a LP up (display_order)
  3694. * @param int $lp_id Learnpath ID
  3695. * @param int $categoryId
  3696. * @return bool
  3697. */
  3698. public static function move_up($lp_id, $categoryId = 0)
  3699. {
  3700. $courseId = api_get_course_int_id();
  3701. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3702. $categoryCondition = '';
  3703. if (!empty($categoryId)) {
  3704. $categoryId = (int) $categoryId;
  3705. $categoryCondition = " AND category_id = $categoryId";
  3706. }
  3707. $sql = "SELECT * FROM $lp_table
  3708. WHERE c_id = $courseId
  3709. $categoryCondition
  3710. ORDER BY display_order";
  3711. $res = Database::query($sql);
  3712. if ($res === false) {
  3713. return false;
  3714. }
  3715. $lps = [];
  3716. $lp_order = [];
  3717. $num = Database::num_rows($res);
  3718. // First check the order is correct, globally (might be wrong because
  3719. // of versions < 1.8.4)
  3720. if ($num > 0) {
  3721. $i = 1;
  3722. while ($row = Database::fetch_array($res)) {
  3723. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3724. $sql = "UPDATE $lp_table SET display_order = $i
  3725. WHERE c_id = $courseId AND id = ".$row['id'];
  3726. Database::query($sql);
  3727. }
  3728. $row['display_order'] = $i;
  3729. $lps[$row['id']] = $row;
  3730. $lp_order[$i] = $row['id'];
  3731. $i++;
  3732. }
  3733. }
  3734. if ($num > 1) { // If there's only one element, no need to sort.
  3735. $order = $lps[$lp_id]['display_order'];
  3736. if ($order > 1) { // If it's the first element, no need to move up.
  3737. $sql = "UPDATE $lp_table SET display_order = $order
  3738. WHERE c_id = $courseId AND id = ".$lp_order[$order - 1];
  3739. Database::query($sql);
  3740. $sql = "UPDATE $lp_table SET display_order = ".($order - 1)."
  3741. WHERE c_id = $courseId AND id = ".$lp_id;
  3742. Database::query($sql);
  3743. }
  3744. }
  3745. return true;
  3746. }
  3747. /**
  3748. * Move a learnpath down (display_order)
  3749. * @param int $lp_id Learnpath ID
  3750. * @param int $categoryId
  3751. * @return bool
  3752. */
  3753. public static function move_down($lp_id, $categoryId = 0)
  3754. {
  3755. $courseId = api_get_course_int_id();
  3756. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3757. $categoryCondition = '';
  3758. if (!empty($categoryId)) {
  3759. $categoryId = (int) $categoryId;
  3760. $categoryCondition = " AND category_id = $categoryId";
  3761. }
  3762. $sql = "SELECT * FROM $lp_table
  3763. WHERE c_id = $courseId
  3764. $categoryCondition
  3765. ORDER BY display_order";
  3766. $res = Database::query($sql);
  3767. if ($res === false) {
  3768. return false;
  3769. }
  3770. $lps = [];
  3771. $lp_order = [];
  3772. $num = Database::num_rows($res);
  3773. $max = 0;
  3774. // First check the order is correct, globally (might be wrong because
  3775. // of versions < 1.8.4).
  3776. if ($num > 0) {
  3777. $i = 1;
  3778. while ($row = Database::fetch_array($res)) {
  3779. $max = $i;
  3780. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3781. $need_fix = true;
  3782. $sql_u = "UPDATE $lp_table SET display_order = $i
  3783. WHERE c_id = ".$courseId." AND id = ".$row['id'];
  3784. Database::query($sql_u);
  3785. }
  3786. $row['display_order'] = $i;
  3787. $lps[$row['id']] = $row;
  3788. $lp_order[$i] = $row['id'];
  3789. $i++;
  3790. }
  3791. }
  3792. if ($num > 1) { // If there's only one element, no need to sort.
  3793. $order = $lps[$lp_id]['display_order'];
  3794. if ($order < $max) { // If it's the first element, no need to move up.
  3795. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3796. WHERE c_id = ".$courseId." AND id = ".$lp_order[$order + 1];
  3797. Database::query($sql_u1);
  3798. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3799. WHERE c_id = ".$courseId." AND id = ".$lp_id;
  3800. Database::query($sql_u2);
  3801. }
  3802. }
  3803. return true;
  3804. }
  3805. /**
  3806. * Updates learnpath attributes to point to the next element
  3807. * The last part is similar to set_current_item but processing the other way around
  3808. */
  3809. public function next()
  3810. {
  3811. if ($this->debug > 0) {
  3812. error_log('New LP - In learnpath::next()', 0);
  3813. }
  3814. $this->last = $this->get_current_item_id();
  3815. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3816. $this->autocomplete_parents($this->last);
  3817. $new_index = $this->get_next_index();
  3818. if ($this->debug > 2) {
  3819. error_log('New LP - New index: ' . $new_index, 0);
  3820. }
  3821. $this->index = $new_index;
  3822. if ($this->debug > 2) {
  3823. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3824. }
  3825. $this->current = $this->ordered_items[$new_index];
  3826. if ($this->debug > 2) {
  3827. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3828. }
  3829. }
  3830. /**
  3831. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3832. * class, this might be redefined to allow several behaviours depending on the document type.
  3833. * @param integer Resource ID
  3834. * @return boolean True on success, false otherwise
  3835. */
  3836. public function open($id)
  3837. {
  3838. if ($this->debug > 0) {
  3839. error_log('New LP - In learnpath::open()', 0);
  3840. }
  3841. // TODO:
  3842. // set the current resource attribute to this resource
  3843. // switch on element type (redefine in child class?)
  3844. // set status for this item to "opened"
  3845. // start timer
  3846. // initialise score
  3847. $this->index = 0; //or = the last item seen (see $this->last)
  3848. }
  3849. /**
  3850. * Check that all prerequisites are fulfilled. Returns true and an
  3851. * empty string on success, returns false
  3852. * and the prerequisite string on error.
  3853. * This function is based on the rules for aicc_script language as
  3854. * described in the SCORM 1.2 CAM documentation page 108.
  3855. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3856. * @return boolean True if prerequisites are matched, false otherwise -
  3857. * Empty string if true returned, prerequisites string otherwise.
  3858. */
  3859. public function prerequisites_match($itemId = null)
  3860. {
  3861. $debug = $this->debug;
  3862. if ($debug > 0) {
  3863. error_log('In learnpath::prerequisites_match()', 0);
  3864. }
  3865. if (empty($itemId)) {
  3866. $itemId = $this->current;
  3867. }
  3868. $currentItem = $this->getItem($itemId);
  3869. if ($currentItem) {
  3870. if ($this->type == 2) {
  3871. // Getting prereq from scorm
  3872. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3873. } else {
  3874. $prereq_string = $currentItem->get_prereq_string();
  3875. }
  3876. if (empty($prereq_string)) {
  3877. if ($debug > 0) {
  3878. error_log('Found prereq_string is empty return true');
  3879. }
  3880. return true;
  3881. }
  3882. // Clean spaces.
  3883. $prereq_string = str_replace(' ', '', $prereq_string);
  3884. if ($debug > 0) {
  3885. error_log('Found prereq_string: ' . $prereq_string, 0);
  3886. }
  3887. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3888. $result = $currentItem->parse_prereq(
  3889. $prereq_string,
  3890. $this->items,
  3891. $this->refs_list,
  3892. $this->get_user_id()
  3893. );
  3894. if ($result === false) {
  3895. $this->set_error_msg($currentItem->prereq_alert);
  3896. }
  3897. } else {
  3898. $result = true;
  3899. if ($debug > 1) {
  3900. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3901. }
  3902. }
  3903. if ($debug > 1) {
  3904. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3905. }
  3906. return $result;
  3907. }
  3908. /**
  3909. * Updates learnpath attributes to point to the previous element
  3910. * The last part is similar to set_current_item but processing the other way around
  3911. */
  3912. public function previous()
  3913. {
  3914. if ($this->debug > 0) {
  3915. error_log('New LP - In learnpath::previous()', 0);
  3916. }
  3917. $this->last = $this->get_current_item_id();
  3918. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3919. $this->autocomplete_parents($this->last);
  3920. $new_index = $this->get_previous_index();
  3921. $this->index = $new_index;
  3922. $this->current = $this->ordered_items[$new_index];
  3923. }
  3924. /**
  3925. * Publishes a learnpath. This basically means show or hide the learnpath
  3926. * to normal users.
  3927. * Can be used as abstract
  3928. * @param integer Learnpath ID
  3929. * @param string New visibility
  3930. */
  3931. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3932. {
  3933. $action = 'visible';
  3934. if ($set_visibility != 1) {
  3935. $action = 'invisible';
  3936. self::toggle_publish($lp_id, 'i');
  3937. }
  3938. return api_item_property_update(
  3939. api_get_course_info(),
  3940. TOOL_LEARNPATH,
  3941. $lp_id,
  3942. $action,
  3943. api_get_user_id()
  3944. );
  3945. }
  3946. /**
  3947. * Publishes a learnpath. This basically means show or hide the learnpath
  3948. * on the course homepage
  3949. * Can be used as abstract
  3950. * @param integer $lp_id Learnpath id
  3951. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3952. * @return bool
  3953. */
  3954. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3955. {
  3956. $course_id = api_get_course_int_id();
  3957. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  3958. $lp_id = intval($lp_id);
  3959. $sql = "SELECT * FROM $tbl_lp
  3960. WHERE c_id = $course_id AND id = $lp_id";
  3961. $result = Database::query($sql);
  3962. if (Database::num_rows($result)) {
  3963. $row = Database::fetch_array($result);
  3964. $name = Database::escape_string($row['name']);
  3965. if ($set_visibility == 'i') {
  3966. $v = 0;
  3967. }
  3968. if ($set_visibility == 'v') {
  3969. $v = 1;
  3970. }
  3971. $session_id = api_get_session_id();
  3972. $session_condition = api_get_session_condition($session_id);
  3973. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  3974. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3975. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3976. $sql = "SELECT * FROM $tbl_tool
  3977. WHERE
  3978. c_id = $course_id AND
  3979. (link = '$link' OR link = '$oldLink') AND
  3980. image = 'scormbuilder.gif' AND
  3981. (
  3982. link LIKE '$link%' OR
  3983. link LIKE '$oldLink%'
  3984. )
  3985. $session_condition
  3986. ";
  3987. $result = Database::query($sql);
  3988. $num = Database::num_rows($result);
  3989. if ($set_visibility == 'i' && $num > 0) {
  3990. $sql = "DELETE FROM $tbl_tool
  3991. WHERE
  3992. c_id = $course_id AND
  3993. (link = '$link' OR link = '$oldLink') AND
  3994. image='scormbuilder.gif'
  3995. $session_condition";
  3996. Database::query($sql);
  3997. } elseif ($set_visibility == 'v' && $num == 0) {
  3998. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3999. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  4000. Database::query($sql);
  4001. $insertId = Database::insert_id();
  4002. if ($insertId) {
  4003. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  4004. Database::query($sql);
  4005. }
  4006. } elseif ($set_visibility == 'v' && $num > 0) {
  4007. $sql = "UPDATE $tbl_tool SET
  4008. c_id = $course_id,
  4009. name = '$name',
  4010. link = '$link',
  4011. image = 'scormbuilder.gif',
  4012. visibility = '$v',
  4013. admin = '0',
  4014. address = 'pastillegris.gif',
  4015. added_tool = 0,
  4016. session_id = $session_id
  4017. WHERE
  4018. c_id = ".$course_id." AND
  4019. (link = '$link' OR link = '$oldLink') AND
  4020. image='scormbuilder.gif'
  4021. $session_condition
  4022. ";
  4023. Database::query($sql);
  4024. } else {
  4025. // Parameter and database incompatible, do nothing, exit.
  4026. return false;
  4027. }
  4028. } else {
  4029. return false;
  4030. }
  4031. }
  4032. /**
  4033. * Restart the whole learnpath. Return the URL of the first element.
  4034. * Make sure the results are saved with anoter method. This method should probably be
  4035. * redefined in children classes.
  4036. * To use a similar method statically, use the create_new_attempt() method
  4037. * @return string URL to load in the viewer
  4038. */
  4039. public function restart()
  4040. {
  4041. if ($this->debug > 0) {
  4042. error_log('New LP - In learnpath::restart()', 0);
  4043. }
  4044. // TODO
  4045. // Call autosave method to save the current progress.
  4046. //$this->index = 0;
  4047. if (api_is_invitee()) {
  4048. return false;
  4049. }
  4050. $session_id = api_get_session_id();
  4051. $course_id = api_get_course_int_id();
  4052. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  4053. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  4054. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  4055. if ($this->debug > 2) {
  4056. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  4057. }
  4058. Database::query($sql);
  4059. $view_id = Database::insert_id();
  4060. if ($view_id) {
  4061. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  4062. Database::query($sql);
  4063. $this->lp_view_id = $view_id;
  4064. $this->attempt = $this->attempt + 1;
  4065. } else {
  4066. $this->error = 'Could not insert into item_view table...';
  4067. return false;
  4068. }
  4069. $this->autocomplete_parents($this->current);
  4070. foreach ($this->items as $index => $dummy) {
  4071. $this->items[$index]->restart();
  4072. $this->items[$index]->set_lp_view($this->lp_view_id);
  4073. }
  4074. $this->first();
  4075. return true;
  4076. }
  4077. /**
  4078. * Saves the current item
  4079. * @return boolean
  4080. */
  4081. public function save_current()
  4082. {
  4083. if ($this->debug > 0) {
  4084. error_log('learnpath::save_current()', 0);
  4085. }
  4086. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4087. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4088. if ($this->debug > 2) {
  4089. error_log('New LP - save_current() saving item ' . $this->current, 0);
  4090. }
  4091. if ($this->debug > 2) {
  4092. error_log('' . print_r($this->items, true), 0);
  4093. }
  4094. if (isset($this->items[$this->current]) &&
  4095. is_object($this->items[$this->current])
  4096. ) {
  4097. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  4098. $this->autocomplete_parents($this->current);
  4099. $status = $this->items[$this->current]->get_status();
  4100. $this->update_queue[$this->current] = $status;
  4101. return $res;
  4102. }
  4103. return false;
  4104. }
  4105. /**
  4106. * Saves the given item
  4107. * @param integer $item_id Optional (will take from $_REQUEST if null)
  4108. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4109. * @return boolean
  4110. */
  4111. public function save_item($item_id = null, $from_outside = true)
  4112. {
  4113. $debug = $this->debug;
  4114. if ($debug) {
  4115. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4116. }
  4117. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4118. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4119. if (empty($item_id)) {
  4120. $item_id = intval($_REQUEST['id']);
  4121. }
  4122. if (empty($item_id)) {
  4123. $item_id = $this->get_current_item_id();
  4124. }
  4125. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4126. if ($debug) {
  4127. error_log('Object exists');
  4128. }
  4129. // Saving the item.
  4130. $res = $this->items[$item_id]->save(
  4131. $from_outside,
  4132. $this->prerequisites_match($item_id)
  4133. );
  4134. if ($debug) {
  4135. error_log('update_queue before:');
  4136. error_log(print_r($this->update_queue,1));
  4137. }
  4138. $this->autocomplete_parents($item_id);
  4139. $status = $this->items[$item_id]->get_status();
  4140. $this->update_queue[$item_id] = $status;
  4141. if ($debug) {
  4142. error_log('get_status(): ' . $status);
  4143. error_log('update_queue after:');
  4144. error_log(print_r($this->update_queue,1));
  4145. }
  4146. return $res;
  4147. }
  4148. return false;
  4149. }
  4150. /**
  4151. * Saves the last item seen's ID only in case
  4152. */
  4153. public function save_last()
  4154. {
  4155. $course_id = api_get_course_int_id();
  4156. if ($this->debug > 0) {
  4157. error_log('New LP - In learnpath::save_last()', 0);
  4158. }
  4159. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4160. $table = Database::get_course_table(TABLE_LP_VIEW);
  4161. if (isset($this->current) && !api_is_invitee()) {
  4162. if ($this->debug > 2) {
  4163. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4164. }
  4165. $sql = "UPDATE $table SET
  4166. last_item = " . intval($this->get_current_item_id()). "
  4167. WHERE
  4168. c_id = $course_id AND
  4169. lp_id = " . $this->get_id() . " AND
  4170. user_id = " . $this->get_user_id()." ".$session_condition;
  4171. if ($this->debug > 2) {
  4172. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4173. }
  4174. Database::query($sql);
  4175. }
  4176. if (!api_is_invitee()) {
  4177. // Save progress.
  4178. list($progress,) = $this->get_progress_bar_text('%');
  4179. if ($progress >= 0 && $progress <= 100) {
  4180. $progress = (int) $progress;
  4181. $sql = "UPDATE $table SET
  4182. progress = $progress
  4183. WHERE
  4184. c_id = ".$course_id." AND
  4185. lp_id = " . $this->get_id() . " AND
  4186. user_id = " . $this->get_user_id()." ".$session_condition;
  4187. // Ignore errors as some tables might not have the progress field just yet.
  4188. Database::query($sql);
  4189. $this->progress_db = $progress;
  4190. }
  4191. }
  4192. }
  4193. /**
  4194. * Sets the current item ID (checks if valid and authorized first)
  4195. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4196. */
  4197. public function set_current_item($item_id = null)
  4198. {
  4199. if ($this->debug > 0) {
  4200. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4201. }
  4202. if (empty ($item_id)) {
  4203. if ($this->debug > 2) {
  4204. error_log('New LP - No new current item given, ignore...', 0);
  4205. }
  4206. // Do nothing.
  4207. } else {
  4208. if ($this->debug > 2) {
  4209. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4210. }
  4211. if (is_numeric($item_id)) {
  4212. $item_id = intval($item_id);
  4213. // TODO: Check in database here.
  4214. $this->last = $this->current;
  4215. $this->current = $item_id;
  4216. // TODO: Update $this->index as well.
  4217. foreach ($this->ordered_items as $index => $item) {
  4218. if ($item == $this->current) {
  4219. $this->index = $index;
  4220. break;
  4221. }
  4222. }
  4223. if ($this->debug > 2) {
  4224. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4225. }
  4226. } else {
  4227. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4228. }
  4229. }
  4230. }
  4231. /**
  4232. * Sets the encoding
  4233. * @param string New encoding
  4234. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4235. */
  4236. public function set_encoding($enc = 'UTF-8')
  4237. {
  4238. if ($this->debug > 0) {
  4239. error_log('New LP - In learnpath::set_encoding()', 0);
  4240. }
  4241. $course_id = api_get_course_int_id();
  4242. $enc = api_refine_encoding_id($enc);
  4243. if (empty($enc)) {
  4244. $enc = api_get_system_encoding();
  4245. }
  4246. if (api_is_encoding_supported($enc)) {
  4247. $lp = $this->get_id();
  4248. if ($lp != 0) {
  4249. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4250. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4251. WHERE c_id = ".$course_id." AND id = " . $lp;
  4252. $res = Database::query($sql);
  4253. return $res;
  4254. }
  4255. }
  4256. return false;
  4257. }
  4258. /**
  4259. * Sets the JS lib setting in the database directly.
  4260. * This is the JavaScript library file this lp needs to load on startup
  4261. * @param string Proximity setting
  4262. * @return boolean True on update success. False otherwise.
  4263. */
  4264. public function set_jslib($lib = '')
  4265. {
  4266. if ($this->debug > 0) {
  4267. error_log('New LP - In learnpath::set_jslib()', 0);
  4268. }
  4269. $lp = $this->get_id();
  4270. $course_id = api_get_course_int_id();
  4271. if ($lp != 0) {
  4272. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4273. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4274. WHERE c_id = ".$course_id." AND id = " . $lp;
  4275. $res = Database::query($sql);
  4276. return $res;
  4277. } else {
  4278. return false;
  4279. }
  4280. }
  4281. /**
  4282. * Sets the name of the LP maker (publisher) (and save)
  4283. * @param string Optional string giving the new content_maker of this learnpath
  4284. * @return boolean True
  4285. */
  4286. public function set_maker($name = '')
  4287. {
  4288. if ($this->debug > 0) {
  4289. error_log('New LP - In learnpath::set_maker()', 0);
  4290. }
  4291. if (empty ($name))
  4292. return false;
  4293. $this->maker = $name;
  4294. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4295. $course_id = api_get_course_int_id();
  4296. $lp_id = $this->get_id();
  4297. $sql = "UPDATE $lp_table SET
  4298. content_maker = '" . Database::escape_string($this->maker) . "'
  4299. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4300. if ($this->debug > 2) {
  4301. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4302. }
  4303. Database::query($sql);
  4304. return true;
  4305. }
  4306. /**
  4307. * Sets the name of the current learnpath (and save)
  4308. * @param string $name Optional string giving the new name of this learnpath
  4309. * @return boolean True/False
  4310. */
  4311. public function set_name($name = null)
  4312. {
  4313. if ($this->debug > 0) {
  4314. error_log('New LP - In learnpath::set_name()', 0);
  4315. }
  4316. if (empty($name)) {
  4317. return false;
  4318. }
  4319. $this->name = Database::escape_string($name);
  4320. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4321. $lp_id = $this->get_id();
  4322. $course_id = $this->course_info['real_id'];
  4323. $sql = "UPDATE $lp_table SET
  4324. name = '" . Database::escape_string($this->name). "'
  4325. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4326. if ($this->debug > 2) {
  4327. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4328. }
  4329. $result = Database::query($sql);
  4330. // If the lp is visible on the homepage, change his name there.
  4331. if (Database::affected_rows($result)) {
  4332. $session_id = api_get_session_id();
  4333. $session_condition = api_get_session_condition($session_id);
  4334. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  4335. $link = 'lp/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4336. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4337. WHERE
  4338. c_id = $course_id AND
  4339. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4340. Database::query($sql);
  4341. return true;
  4342. } else {
  4343. return false;
  4344. }
  4345. }
  4346. /**
  4347. * Set index specified prefix terms for all items in this path
  4348. * @param string Comma-separated list of terms
  4349. * @param char Xapian term prefix
  4350. * @return boolean False on error, true otherwise
  4351. */
  4352. public function set_terms_by_prefix($terms_string, $prefix)
  4353. {
  4354. $course_id = api_get_course_int_id();
  4355. if (api_get_setting('search_enabled') !== 'true')
  4356. return false;
  4357. if (!extension_loaded('xapian')) {
  4358. return false;
  4359. }
  4360. $terms_string = trim($terms_string);
  4361. $terms = explode(',', $terms_string);
  4362. array_walk($terms, 'trim_value');
  4363. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4364. // Don't do anything if no change, verify only at DB, not the search engine.
  4365. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4366. return false;
  4367. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4368. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4369. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4370. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4371. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  4372. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4373. $lp_id = intval($_POST['lp_id']);
  4374. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4375. $result = Database::query($sql);
  4376. $di = new ChamiloIndexer();
  4377. while ($lp_item = Database::fetch_array($result)) {
  4378. // Get search_did.
  4379. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4380. $sql = 'SELECT * FROM %s
  4381. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4382. LIMIT 1';
  4383. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4384. //echo $sql; echo '<br>';
  4385. $res = Database::query($sql);
  4386. if (Database::num_rows($res) > 0) {
  4387. $se_ref = Database::fetch_array($res);
  4388. // Compare terms.
  4389. $doc = $di->get_document($se_ref['search_did']);
  4390. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4391. $xterms = array();
  4392. foreach ($xapian_terms as $xapian_term) {
  4393. $xterms[] = substr($xapian_term['name'], 1);
  4394. }
  4395. $dterms = $terms;
  4396. $missing_terms = array_diff($dterms, $xterms);
  4397. $deprecated_terms = array_diff($xterms, $dterms);
  4398. // Save it to search engine.
  4399. foreach ($missing_terms as $term) {
  4400. $doc->add_term($prefix . $term, 1);
  4401. }
  4402. foreach ($deprecated_terms as $term) {
  4403. $doc->remove_term($prefix . $term);
  4404. }
  4405. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4406. $di->getDb()->flush();
  4407. }
  4408. }
  4409. return true;
  4410. }
  4411. /**
  4412. * Sets the theme of the LP (local/remote) (and save)
  4413. * @param string Optional string giving the new theme of this learnpath
  4414. * @return bool Returns true if theme name is not empty
  4415. */
  4416. public function set_theme($name = '')
  4417. {
  4418. $course_id = api_get_course_int_id();
  4419. if ($this->debug > 0) {
  4420. error_log('New LP - In learnpath::set_theme()', 0);
  4421. }
  4422. $this->theme = $name;
  4423. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4424. $lp_id = $this->get_id();
  4425. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4426. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4427. if ($this->debug > 2) {
  4428. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4429. }
  4430. Database::query($sql);
  4431. return true;
  4432. }
  4433. /**
  4434. * Sets the image of an LP (and save)
  4435. * @param string Optional string giving the new image of this learnpath
  4436. * @return bool Returns true if theme name is not empty
  4437. */
  4438. public function set_preview_image($name = '')
  4439. {
  4440. $course_id = api_get_course_int_id();
  4441. if ($this->debug > 0) {
  4442. error_log('New LP - In learnpath::set_preview_image()', 0);
  4443. }
  4444. $this->preview_image = $name;
  4445. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4446. $lp_id = $this->get_id();
  4447. $sql = "UPDATE $lp_table SET
  4448. preview_image = '" . Database::escape_string($this->preview_image). "'
  4449. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4450. if ($this->debug > 2) {
  4451. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4452. }
  4453. Database::query($sql);
  4454. return true;
  4455. }
  4456. /**
  4457. * Sets the author of a LP (and save)
  4458. * @param string Optional string giving the new author of this learnpath
  4459. * @return bool Returns true if author's name is not empty
  4460. */
  4461. public function set_author($name = '')
  4462. {
  4463. $course_id = api_get_course_int_id();
  4464. if ($this->debug > 0) {
  4465. error_log('New LP - In learnpath::set_author()', 0);
  4466. }
  4467. $this->author = $name;
  4468. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4469. $lp_id = $this->get_id();
  4470. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4471. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4472. if ($this->debug > 2) {
  4473. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4474. }
  4475. Database::query($sql);
  4476. return true;
  4477. }
  4478. /**
  4479. * Sets the hide_toc_frame parameter of a LP (and save)
  4480. * @param int 1 if frame is hidden 0 then else
  4481. * @return bool Returns true if author's name is not empty
  4482. */
  4483. public function set_hide_toc_frame($hide)
  4484. {
  4485. $course_id = api_get_course_int_id();
  4486. if ($this->debug > 0) {
  4487. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4488. }
  4489. if (intval($hide) == $hide) {
  4490. $this->hide_toc_frame = $hide;
  4491. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4492. $lp_id = $this->get_id();
  4493. $sql = "UPDATE $lp_table SET
  4494. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4495. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4496. if ($this->debug > 2) {
  4497. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4498. }
  4499. Database::query($sql);
  4500. return true;
  4501. } else {
  4502. return false;
  4503. }
  4504. }
  4505. /**
  4506. * Sets the prerequisite of a LP (and save)
  4507. * @param int integer giving the new prerequisite of this learnpath
  4508. * @return bool returns true if prerequisite is not empty
  4509. */
  4510. public function set_prerequisite($prerequisite)
  4511. {
  4512. $course_id = api_get_course_int_id();
  4513. if ($this->debug > 0) {
  4514. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4515. }
  4516. $this->prerequisite = intval($prerequisite);
  4517. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4518. $lp_id = $this->get_id();
  4519. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4520. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4521. if ($this->debug > 2) {
  4522. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4523. }
  4524. Database::query($sql);
  4525. return true;
  4526. }
  4527. /**
  4528. * Sets the location/proximity of the LP (local/remote) (and save)
  4529. * @param string Optional string giving the new location of this learnpath
  4530. * @return boolean True on success / False on error
  4531. */
  4532. public function set_proximity($name = '')
  4533. {
  4534. $course_id = api_get_course_int_id();
  4535. if ($this->debug > 0) {
  4536. error_log('New LP - In learnpath::set_proximity()', 0);
  4537. }
  4538. if (empty ($name))
  4539. return false;
  4540. $this->proximity = $name;
  4541. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4542. $lp_id = $this->get_id();
  4543. $sql = "UPDATE $lp_table SET
  4544. content_local = '" . Database::escape_string($name) . "'
  4545. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4546. if ($this->debug > 2) {
  4547. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4548. }
  4549. Database::query($sql);
  4550. return true;
  4551. }
  4552. /**
  4553. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4554. * @param integer DB ID of the item
  4555. */
  4556. public function set_previous_item($id)
  4557. {
  4558. if ($this->debug > 0) {
  4559. error_log('New LP - In learnpath::set_previous_item()', 0);
  4560. }
  4561. $this->last = $id;
  4562. }
  4563. /**
  4564. * Sets use_max_score
  4565. * @param string $use_max_score Optional string giving the new location of this learnpath
  4566. * @return boolean True on success / False on error
  4567. */
  4568. public function set_use_max_score($use_max_score = 1)
  4569. {
  4570. $course_id = api_get_course_int_id();
  4571. if ($this->debug > 0) {
  4572. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4573. }
  4574. $use_max_score = intval($use_max_score);
  4575. $this->use_max_score = $use_max_score;
  4576. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4577. $lp_id = $this->get_id();
  4578. $sql = "UPDATE $lp_table SET
  4579. use_max_score = '" . $this->use_max_score . "'
  4580. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4581. if ($this->debug > 2) {
  4582. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4583. }
  4584. Database::query($sql);
  4585. return true;
  4586. }
  4587. /**
  4588. * Sets and saves the expired_on date
  4589. * @param string $expired_on Optional string giving the new author of this learnpath
  4590. * @return bool Returns true if author's name is not empty
  4591. */
  4592. public function set_expired_on($expired_on)
  4593. {
  4594. if ($this->debug > 0) {
  4595. error_log('New LP - In learnpath::set_expired_on()', 0);
  4596. }
  4597. $em = Database::getManager();
  4598. /** @var CLp $lp */
  4599. $lp = $em
  4600. ->getRepository('ChamiloCourseBundle:CLp')
  4601. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4602. if (!$lp) {
  4603. return false;
  4604. }
  4605. $this->expired_on = !empty($expired_on) ? api_get_utc_datetime($expired_on, false, true) : null;
  4606. $lp->setExpiredOn($this->expired_on);
  4607. $em->persist($lp);
  4608. $em->flush();
  4609. if ($this->debug > 2) {
  4610. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4611. }
  4612. return true;
  4613. }
  4614. /**
  4615. * Sets and saves the publicated_on date
  4616. * @param string $publicated_on Optional string giving the new author of this learnpath
  4617. * @return bool Returns true if author's name is not empty
  4618. */
  4619. public function set_publicated_on($publicated_on)
  4620. {
  4621. if ($this->debug > 0) {
  4622. error_log('New LP - In learnpath::set_expired_on()', 0);
  4623. }
  4624. $em = Database::getManager();
  4625. /** @var CLp $lp */
  4626. $lp = $em
  4627. ->getRepository('ChamiloCourseBundle:CLp')
  4628. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4629. if (!$lp) {
  4630. return false;
  4631. }
  4632. $this->publicated_on = !empty($publicated_on) ? api_get_utc_datetime($publicated_on, false, true) : null;
  4633. $lp->setPublicatedOn($this->publicated_on);
  4634. $em->persist($lp);
  4635. $em->flush();
  4636. if ($this->debug > 2) {
  4637. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4638. }
  4639. return true;
  4640. }
  4641. /**
  4642. * Sets and saves the expired_on date
  4643. * @return bool Returns true if author's name is not empty
  4644. */
  4645. public function set_modified_on()
  4646. {
  4647. $course_id = api_get_course_int_id();
  4648. if ($this->debug > 0) {
  4649. error_log('New LP - In learnpath::set_expired_on()', 0);
  4650. }
  4651. $this->modified_on = api_get_utc_datetime();
  4652. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4653. $lp_id = $this->get_id();
  4654. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4655. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4656. if ($this->debug > 2) {
  4657. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4658. }
  4659. Database::query($sql);
  4660. return true;
  4661. }
  4662. /**
  4663. * Sets the object's error message
  4664. * @param string Error message. If empty, reinits the error string
  4665. * @return void
  4666. */
  4667. public function set_error_msg($error = '')
  4668. {
  4669. if ($this->debug > 0) {
  4670. error_log('New LP - In learnpath::set_error_msg()', 0);
  4671. }
  4672. if (empty ($error)) {
  4673. $this->error = '';
  4674. } else {
  4675. $this->error .= $error;
  4676. }
  4677. }
  4678. /**
  4679. * Launches the current item if not 'sco'
  4680. * (starts timer and make sure there is a record ready in the DB)
  4681. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4682. * @return boolean
  4683. */
  4684. public function start_current_item($allow_new_attempt = false)
  4685. {
  4686. if ($this->debug > 0) {
  4687. error_log('New LP - In learnpath::start_current_item()', 0);
  4688. }
  4689. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4690. $type = $this->get_type();
  4691. $item_type = $this->items[$this->current]->get_type();
  4692. if (($type == 2 && $item_type != 'sco') ||
  4693. ($type == 3 && $item_type != 'au') ||
  4694. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4695. ) {
  4696. $this->items[$this->current]->open($allow_new_attempt);
  4697. $this->autocomplete_parents($this->current);
  4698. $prereq_check = $this->prerequisites_match($this->current);
  4699. $this->items[$this->current]->save(false, $prereq_check);
  4700. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4701. }
  4702. // If sco, then it is supposed to have been updated by some other call.
  4703. if ($item_type == 'sco') {
  4704. $this->items[$this->current]->restart();
  4705. }
  4706. }
  4707. if ($this->debug > 0) {
  4708. error_log('New LP - End of learnpath::start_current_item()', 0);
  4709. }
  4710. return true;
  4711. }
  4712. /**
  4713. * Stops the processing and counters for the old item (as held in $this->last)
  4714. * @return boolean True/False
  4715. */
  4716. public function stop_previous_item()
  4717. {
  4718. if ($this->debug > 0) {
  4719. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4720. }
  4721. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4722. if ($this->debug > 2) {
  4723. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4724. }
  4725. switch ($this->get_type()) {
  4726. case '3':
  4727. if ($this->items[$this->last]->get_type() != 'au') {
  4728. if ($this->debug > 2) {
  4729. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4730. }
  4731. $this->items[$this->last]->close();
  4732. //$this->autocomplete_parents($this->last);
  4733. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4734. } else {
  4735. if ($this->debug > 2) {
  4736. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4737. }
  4738. }
  4739. break;
  4740. case '2':
  4741. if ($this->items[$this->last]->get_type() != 'sco') {
  4742. if ($this->debug > 2) {
  4743. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4744. }
  4745. $this->items[$this->last]->close();
  4746. //$this->autocomplete_parents($this->last);
  4747. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4748. } else {
  4749. if ($this->debug > 2) {
  4750. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4751. }
  4752. }
  4753. break;
  4754. case '1':
  4755. default:
  4756. if ($this->debug > 2) {
  4757. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4758. }
  4759. $this->items[$this->last]->close();
  4760. break;
  4761. }
  4762. } else {
  4763. if ($this->debug > 2) {
  4764. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4765. }
  4766. return false;
  4767. }
  4768. return true;
  4769. }
  4770. /**
  4771. * Updates the default view mode from fullscreen to embedded and inversely
  4772. * @return string The current default view mode ('fullscreen' or 'embedded')
  4773. */
  4774. public function update_default_view_mode()
  4775. {
  4776. $course_id = api_get_course_int_id();
  4777. if ($this->debug > 0) {
  4778. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4779. }
  4780. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4781. $sql = "SELECT * FROM $lp_table
  4782. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4783. $res = Database::query($sql);
  4784. if (Database::num_rows($res) > 0) {
  4785. $row = Database::fetch_array($res);
  4786. $default_view_mode = $row['default_view_mod'];
  4787. $view_mode = $default_view_mode;
  4788. switch ($default_view_mode) {
  4789. case 'fullscreen': // default with popup
  4790. $view_mode = 'embedded';
  4791. break;
  4792. case 'embedded': // default view with left menu
  4793. $view_mode = 'embedframe';
  4794. break;
  4795. case 'embedframe': //folded menu
  4796. $view_mode = 'impress';
  4797. break;
  4798. case 'impress':
  4799. $view_mode = 'fullscreen';
  4800. break;
  4801. }
  4802. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4803. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4804. Database::query($sql);
  4805. $this->mode = $view_mode;
  4806. return $view_mode;
  4807. } else {
  4808. if ($this->debug > 2) {
  4809. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4810. }
  4811. }
  4812. return -1;
  4813. }
  4814. /**
  4815. * Updates the default behaviour about auto-commiting SCORM updates
  4816. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4817. */
  4818. public function update_default_scorm_commit()
  4819. {
  4820. $course_id = api_get_course_int_id();
  4821. if ($this->debug > 0) {
  4822. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4823. }
  4824. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4825. $sql = "SELECT * FROM $lp_table
  4826. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4827. $res = Database::query($sql);
  4828. if (Database::num_rows($res) > 0) {
  4829. $row = Database::fetch_array($res);
  4830. $force = $row['force_commit'];
  4831. if ($force == 1) {
  4832. $force = 0;
  4833. $force_return = false;
  4834. } elseif ($force == 0) {
  4835. $force = 1;
  4836. $force_return = true;
  4837. }
  4838. $sql = "UPDATE $lp_table SET force_commit = $force
  4839. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4840. Database::query($sql);
  4841. $this->force_commit = $force_return;
  4842. return $force_return;
  4843. } else {
  4844. if ($this->debug > 2) {
  4845. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4846. }
  4847. }
  4848. return -1;
  4849. }
  4850. /**
  4851. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4852. * @return bool True on success, false on failure
  4853. */
  4854. public function update_display_order()
  4855. {
  4856. $course_id = api_get_course_int_id();
  4857. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4858. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4859. $res = Database::query($sql);
  4860. if ($res === false) {
  4861. return false;
  4862. }
  4863. $num = Database::num_rows($res);
  4864. // First check the order is correct, globally (might be wrong because
  4865. // of versions < 1.8.4).
  4866. if ($num > 0) {
  4867. $i = 1;
  4868. while ($row = Database::fetch_array($res)) {
  4869. if ($row['display_order'] != $i) {
  4870. // If we find a gap in the order, we need to fix it.
  4871. $sql = "UPDATE $lp_table SET display_order = $i
  4872. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4873. Database::query($sql);
  4874. }
  4875. $i++;
  4876. }
  4877. }
  4878. return true;
  4879. }
  4880. /**
  4881. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4882. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4883. */
  4884. public function update_reinit()
  4885. {
  4886. $course_id = api_get_course_int_id();
  4887. if ($this->debug > 0) {
  4888. error_log('New LP - In learnpath::update_reinit()', 0);
  4889. }
  4890. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4891. $sql = "SELECT * FROM $lp_table
  4892. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4893. $res = Database::query($sql);
  4894. if (Database::num_rows($res) > 0) {
  4895. $row = Database::fetch_array($res);
  4896. $force = $row['prevent_reinit'];
  4897. if ($force == 1) {
  4898. $force = 0;
  4899. } elseif ($force == 0) {
  4900. $force = 1;
  4901. }
  4902. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4903. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4904. Database::query($sql);
  4905. $this->prevent_reinit = $force;
  4906. return $force;
  4907. } else {
  4908. if ($this->debug > 2) {
  4909. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4910. }
  4911. }
  4912. return -1;
  4913. }
  4914. /**
  4915. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4916. *
  4917. * @return string 'single', 'multi' or 'seriousgame'
  4918. * @author ndiechburg <noel@cblue.be>
  4919. **/
  4920. public function get_attempt_mode()
  4921. {
  4922. //Set default value for seriousgame_mode
  4923. if (!isset($this->seriousgame_mode)) {
  4924. $this->seriousgame_mode=0;
  4925. }
  4926. // Set default value for prevent_reinit
  4927. if (!isset($this->prevent_reinit)) {
  4928. $this->prevent_reinit =1;
  4929. }
  4930. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4931. return 'seriousgame';
  4932. }
  4933. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4934. return 'single';
  4935. }
  4936. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4937. return 'multiple';
  4938. }
  4939. return 'single';
  4940. }
  4941. /**
  4942. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4943. *
  4944. * @param string 'seriousgame', 'single' or 'multiple'
  4945. * @return boolean
  4946. * @author ndiechburg <noel@cblue.be>
  4947. **/
  4948. public function set_attempt_mode($mode)
  4949. {
  4950. $course_id = api_get_course_int_id();
  4951. switch ($mode) {
  4952. case 'seriousgame':
  4953. $sg_mode = 1;
  4954. $prevent_reinit = 1;
  4955. break;
  4956. case 'single':
  4957. $sg_mode = 0;
  4958. $prevent_reinit = 1;
  4959. break;
  4960. case 'multiple':
  4961. $sg_mode = 0;
  4962. $prevent_reinit = 0;
  4963. break;
  4964. default:
  4965. $sg_mode = 0;
  4966. $prevent_reinit = 0;
  4967. break;
  4968. }
  4969. $this->prevent_reinit = $prevent_reinit;
  4970. $this->seriousgame_mode = $sg_mode;
  4971. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4972. $sql = "UPDATE $lp_table SET
  4973. prevent_reinit = $prevent_reinit ,
  4974. seriousgame_mode = $sg_mode
  4975. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4976. $res = Database::query($sql);
  4977. if ($res) {
  4978. return true;
  4979. } else {
  4980. return false;
  4981. }
  4982. }
  4983. /**
  4984. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4985. *
  4986. * @return boolean
  4987. * @author ndiechburg <noel@cblue.be>
  4988. **/
  4989. public function switch_attempt_mode()
  4990. {
  4991. if ($this->debug > 0) {
  4992. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4993. }
  4994. $mode = $this->get_attempt_mode();
  4995. switch ($mode) {
  4996. case 'single':
  4997. $next_mode = 'multiple';
  4998. break;
  4999. case 'multiple':
  5000. $next_mode = 'seriousgame';
  5001. break;
  5002. case 'seriousgame':
  5003. $next_mode = 'single';
  5004. break;
  5005. default:
  5006. $next_mode = 'single';
  5007. break;
  5008. }
  5009. $this->set_attempt_mode($next_mode);
  5010. }
  5011. /**
  5012. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  5013. * but possibility to do again a completed item.
  5014. *
  5015. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  5016. * @author ndiechburg <noel@cblue.be>
  5017. **/
  5018. public function set_seriousgame_mode()
  5019. {
  5020. $course_id = api_get_course_int_id();
  5021. if ($this->debug > 0) {
  5022. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  5023. }
  5024. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5025. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  5026. $res = Database::query($sql);
  5027. if (Database::num_rows($res) > 0) {
  5028. $row = Database::fetch_array($res);
  5029. $force = $row['seriousgame_mode'];
  5030. if ($force == 1) {
  5031. $force = 0;
  5032. } elseif ($force == 0) {
  5033. $force = 1;
  5034. }
  5035. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  5036. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  5037. Database::query($sql);
  5038. $this->seriousgame_mode = $force;
  5039. return $force;
  5040. } else {
  5041. if ($this->debug > 2) {
  5042. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  5043. }
  5044. }
  5045. return -1;
  5046. }
  5047. /**
  5048. * Updates the "scorm_debug" value that shows or hide the debug window
  5049. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  5050. */
  5051. public function update_scorm_debug()
  5052. {
  5053. $course_id = api_get_course_int_id();
  5054. if ($this->debug > 0) {
  5055. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  5056. }
  5057. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5058. $sql = "SELECT * FROM $lp_table
  5059. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  5060. $res = Database::query($sql);
  5061. if (Database::num_rows($res) > 0) {
  5062. $row = Database::fetch_array($res);
  5063. $force = $row['debug'];
  5064. if ($force == 1) {
  5065. $force = 0;
  5066. } elseif ($force == 0) {
  5067. $force = 1;
  5068. }
  5069. $sql = "UPDATE $lp_table SET debug = $force
  5070. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  5071. Database::query($sql);
  5072. $this->scorm_debug = $force;
  5073. return $force;
  5074. } else {
  5075. if ($this->debug > 2) {
  5076. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  5077. }
  5078. }
  5079. return -1;
  5080. }
  5081. /**
  5082. * Function that makes a call to the function sort_tree_array and create_tree_array
  5083. * @author Kevin Van Den Haute
  5084. * @param array
  5085. */
  5086. public function tree_array($array)
  5087. {
  5088. if ($this->debug > 1) {
  5089. error_log('New LP - In learnpath::tree_array()', 0);
  5090. }
  5091. $array = $this->sort_tree_array($array);
  5092. $this->create_tree_array($array);
  5093. }
  5094. /**
  5095. * Creates an array with the elements of the learning path tree in it
  5096. *
  5097. * @author Kevin Van Den Haute
  5098. * @param array $array
  5099. * @param int $parent
  5100. * @param int $depth
  5101. * @param array $tmp
  5102. */
  5103. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  5104. {
  5105. if ($this->debug > 1) {
  5106. error_log('New LP - In learnpath::create_tree_array())', 0);
  5107. }
  5108. if (is_array($array)) {
  5109. for ($i = 0; $i < count($array); $i++) {
  5110. if ($array[$i]['parent_item_id'] == $parent) {
  5111. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5112. $tmp[] = $array[$i]['parent_item_id'];
  5113. $depth++;
  5114. }
  5115. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5116. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5117. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5118. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5119. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5120. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5121. $this->arrMenu[] = array(
  5122. 'id' => $array[$i]['id'],
  5123. 'ref' => $ref,
  5124. 'item_type' => $array[$i]['item_type'],
  5125. 'title' => $array[$i]['title'],
  5126. 'path' => $path,
  5127. 'description' => $array[$i]['description'],
  5128. 'parent_item_id' => $array[$i]['parent_item_id'],
  5129. 'previous_item_id' => $array[$i]['previous_item_id'],
  5130. 'next_item_id' => $array[$i]['next_item_id'],
  5131. 'min_score' => $array[$i]['min_score'],
  5132. 'max_score' => $array[$i]['max_score'],
  5133. 'mastery_score' => $array[$i]['mastery_score'],
  5134. 'display_order' => $array[$i]['display_order'],
  5135. 'prerequisite' => $preq,
  5136. 'depth' => $depth,
  5137. 'audio' => $audio,
  5138. 'prerequisite_min_score' => $prerequisiteMinScore,
  5139. 'prerequisite_max_score' => $prerequisiteMaxScore
  5140. );
  5141. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5142. }
  5143. }
  5144. }
  5145. }
  5146. /**
  5147. * Sorts a multi dimensional array by parent id and display order
  5148. * @author Kevin Van Den Haute
  5149. *
  5150. * @param array $array (array with al the learning path items in it)
  5151. *
  5152. * @return array
  5153. */
  5154. public function sort_tree_array($array)
  5155. {
  5156. foreach ($array as $key => $row) {
  5157. $parent[$key] = $row['parent_item_id'];
  5158. $position[$key] = $row['display_order'];
  5159. }
  5160. if (count($array) > 0) {
  5161. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5162. }
  5163. return $array;
  5164. }
  5165. /**
  5166. * Function that creates a html list of learning path items so that we can add audio files to them
  5167. * @author Kevin Van Den Haute
  5168. * @return string
  5169. */
  5170. public function overview()
  5171. {
  5172. if ($this->debug > 0) {
  5173. error_log('New LP - In learnpath::overview()', 0);
  5174. }
  5175. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5176. $return = '';
  5177. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5178. // we need to start a form when we want to update all the mp3 files
  5179. if ($update_audio == 'true') {
  5180. $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">';
  5181. }
  5182. $return .= '<div id="message"></div>';
  5183. if (count($this->items) == 0) {
  5184. $return .= Display::return_message(get_lang('YouShouldAddItemsBeforeAttachAudio'), 'normal');
  5185. } else {
  5186. $return_audio = '<table class="data_table">';
  5187. $return_audio .= '<tr>';
  5188. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5189. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5190. $return_audio .= '</tr>';
  5191. if ($update_audio != 'true') {
  5192. $return .= '<div class="col-md-12">';
  5193. $return .= self::return_new_tree($update_audio);
  5194. $return .='</div>';
  5195. $return .= Display::div(
  5196. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5197. array('style' => 'float:left; margin-top:15px;width:100%')
  5198. );
  5199. } else {
  5200. $return_audio .= self::return_new_tree($update_audio);
  5201. $return .= $return_audio.'</table>';
  5202. }
  5203. // We need to close the form when we are updating the mp3 files.
  5204. if ($update_audio == 'true') {
  5205. $return .= '<div class="footer-audio">';
  5206. $return .= Display::button(
  5207. 'save_audio',
  5208. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5209. array('class' => 'btn btn-primary', 'type' => 'submit')
  5210. );
  5211. $return .= '</div>';
  5212. }
  5213. }
  5214. // We need to close the form when we are updating the mp3 files.
  5215. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5216. $return .= '</form>';
  5217. }
  5218. return $return;
  5219. }
  5220. /**
  5221. * @param string string $update_audio
  5222. * @param bool $drop_element_here
  5223. * @return string
  5224. */
  5225. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5226. {
  5227. $return = '';
  5228. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5229. $course_id = api_get_course_int_id();
  5230. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5231. $sql = "SELECT * FROM $tbl_lp_item
  5232. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5233. $result = Database::query($sql);
  5234. $arrLP = array();
  5235. while ($row = Database::fetch_array($result)) {
  5236. $arrLP[] = array(
  5237. 'id' => $row['id'],
  5238. 'item_type' => $row['item_type'],
  5239. 'title' => Security::remove_XSS($row['title']),
  5240. 'path' => $row['path'],
  5241. 'description' => Security::remove_XSS($row['description']),
  5242. 'parent_item_id' => $row['parent_item_id'],
  5243. 'previous_item_id' => $row['previous_item_id'],
  5244. 'next_item_id' => $row['next_item_id'],
  5245. 'max_score' => $row['max_score'],
  5246. 'min_score' => $row['min_score'],
  5247. 'mastery_score' => $row['mastery_score'],
  5248. 'prerequisite' => $row['prerequisite'],
  5249. 'display_order' => $row['display_order'],
  5250. 'audio' => $row['audio'],
  5251. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5252. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5253. );
  5254. }
  5255. $this->tree_array($arrLP);
  5256. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5257. unset($this->arrMenu);
  5258. $default_data = null;
  5259. $default_content = null;
  5260. $elements = array();
  5261. $return_audio = null;
  5262. for ($i = 0; $i < count($arrLP); $i++) {
  5263. $title = $arrLP[$i]['title'];
  5264. $title_cut = cut($arrLP[$i]['title'], 25);
  5265. // Link for the documents
  5266. if ($arrLP[$i]['item_type'] == 'document') {
  5267. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5268. $title_cut = Display::url(
  5269. $title_cut,
  5270. $url,
  5271. array(
  5272. 'class' => 'ajax moved',
  5273. 'data-title' => $title_cut
  5274. )
  5275. );
  5276. }
  5277. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5278. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5279. Session::write('pathItem', $arrLP[$i]['path']);
  5280. }
  5281. if (($i % 2) == 0) {
  5282. $oddClass = 'row_odd';
  5283. } else {
  5284. $oddClass = 'row_even';
  5285. }
  5286. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5287. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5288. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5289. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5290. } else {
  5291. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5292. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5293. } else {
  5294. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5295. $icon = Display::return_icon('certificate.png');
  5296. } else {
  5297. $icon = Display::return_icon('folder_document.gif');
  5298. }
  5299. }
  5300. }
  5301. // The audio column.
  5302. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5303. $audio = '';
  5304. if (!$update_audio || $update_audio <> 'true') {
  5305. if (empty($arrLP[$i]['audio'])) {
  5306. $audio .= '';
  5307. }
  5308. } else {
  5309. $types = self::getChapterTypes();
  5310. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5311. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5312. if (!empty ($arrLP[$i]['audio'])) {
  5313. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5314. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5315. }
  5316. }
  5317. }
  5318. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5319. $return_audio .= '</td>';
  5320. $move_icon = '';
  5321. $move_item_icon = '';
  5322. $edit_icon = '';
  5323. $delete_icon = '';
  5324. $audio_icon = '';
  5325. $prerequisities_icon = '';
  5326. $forumIcon = '';
  5327. $previewIcon = '';
  5328. if ($is_allowed_to_edit) {
  5329. if (!$update_audio || $update_audio <> 'true') {
  5330. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5331. $move_icon .= '<a class="moved" href="#">';
  5332. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5333. $move_icon .= '</a>';
  5334. }
  5335. }
  5336. // No edit for this item types
  5337. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5338. if ($arrLP[$i]['item_type'] != 'dir') {
  5339. $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">';
  5340. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5341. $edit_icon .= '</a>';
  5342. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5343. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5344. $this->course_int_id,
  5345. $this->lp_session_id
  5346. );
  5347. if ($forumThread) {
  5348. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5349. 'action' => 'dissociate_forum',
  5350. 'id' => $arrLP[$i]['id'],
  5351. 'lp_id' => $this->lp_id
  5352. ]);
  5353. $forumIcon = Display::url(
  5354. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5355. $forumIconUrl,
  5356. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5357. );
  5358. } else {
  5359. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5360. 'action' => 'create_forum',
  5361. 'id' => $arrLP[$i]['id'],
  5362. 'lp_id' => $this->lp_id
  5363. ]);
  5364. $forumIcon = Display::url(
  5365. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5366. $forumIconUrl,
  5367. ['class' => "btn btn-default lp-btn-associate-forum"]
  5368. );
  5369. }
  5370. }
  5371. } else {
  5372. $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">';
  5373. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5374. $edit_icon .= '</a>';
  5375. }
  5376. } else {
  5377. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5378. $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">';
  5379. $edit_icon .= Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_TINY);
  5380. $edit_icon .= '</a>';
  5381. }
  5382. }
  5383. $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">';
  5384. $delete_icon .= Display::return_icon(
  5385. 'delete.png',
  5386. get_lang('LearnpathDeleteModule'),
  5387. [],
  5388. ICON_SIZE_TINY
  5389. );
  5390. $delete_icon .= '</a>';
  5391. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5392. $previewImage = Display::return_icon(
  5393. 'preview_view.png',
  5394. get_lang('Preview'),
  5395. [],
  5396. ICON_SIZE_TINY
  5397. );
  5398. switch ($arrLP[$i]['item_type']) {
  5399. case TOOL_DOCUMENT:
  5400. case TOOL_LP_FINAL_ITEM:
  5401. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5402. $previewIcon = Display::url(
  5403. $previewImage,
  5404. $urlPreviewLink,
  5405. array(
  5406. 'target' => '_blank',
  5407. 'class' => 'btn btn-default',
  5408. 'data-title' => $arrLP[$i]['title']
  5409. )
  5410. );
  5411. break;
  5412. case TOOL_THREAD:
  5413. case TOOL_FORUM:
  5414. case TOOL_QUIZ:
  5415. case TOOL_STUDENTPUBLICATION:
  5416. case TOOL_LP_FINAL_ITEM:
  5417. case TOOL_LINK:
  5418. //$target = '';
  5419. //$class = 'btn btn-default ajax';
  5420. //if ($arrLP[$i]['item_type'] == TOOL_LINK) {
  5421. $class = 'btn btn-default';
  5422. $target = '_blank';
  5423. //}
  5424. $link = self::rl_get_resource_link_for_learnpath(
  5425. $this->course_int_id,
  5426. $this->lp_id,
  5427. $arrLP[$i]['id'],
  5428. 0,
  5429. ''
  5430. );
  5431. $previewIcon = Display::url(
  5432. $previewImage,
  5433. $link,
  5434. [
  5435. 'class' => $class,
  5436. 'data-title' => $arrLP[$i]['title'],
  5437. 'target' => $target
  5438. ]
  5439. );
  5440. break;
  5441. default:
  5442. $previewIcon = Display::url(
  5443. $previewImage,
  5444. $url.'&action=view_item',
  5445. ['class' => 'btn btn-default', 'target' => '_blank']
  5446. );
  5447. break;
  5448. }
  5449. if ($arrLP[$i]['item_type'] != 'dir') {
  5450. $prerequisities_icon = Display::url(
  5451. Display::return_icon(
  5452. 'accept.png',
  5453. get_lang('LearnpathPrerequisites'),
  5454. array(),
  5455. ICON_SIZE_TINY
  5456. ),
  5457. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5458. );
  5459. if ($arrLP[$i]['item_type'] != 'final_item') {
  5460. $move_item_icon = Display::url(
  5461. Display::return_icon(
  5462. 'move.png',
  5463. get_lang('Move'),
  5464. array(),
  5465. ICON_SIZE_TINY
  5466. ),
  5467. $url.'&action=move_item',
  5468. ['class' => 'btn btn-default']
  5469. );
  5470. }
  5471. $audio_icon = Display::url(
  5472. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5473. $url.'&action=add_audio',
  5474. ['class' => 'btn btn-default']
  5475. );
  5476. }
  5477. }
  5478. if ($update_audio != 'true') {
  5479. $row = $move_icon . ' ' . $icon .
  5480. Display::span($title_cut) .
  5481. Display::tag(
  5482. 'div',
  5483. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5484. array('class'=>'btn-toolbar button_actions')
  5485. );
  5486. } else {
  5487. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5488. }
  5489. $parent_id = $arrLP[$i]['parent_item_id'];
  5490. $default_data[$arrLP[$i]['id']] = $row;
  5491. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5492. if (empty($parent_id)) {
  5493. $elements[$arrLP[$i]['id']]['data'] = $row;
  5494. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5495. } else {
  5496. $parent_arrays = array();
  5497. if ($arrLP[$i]['depth'] > 1) {
  5498. //Getting list of parents
  5499. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5500. foreach($arrLP as $item) {
  5501. if ($item['id'] == $parent_id) {
  5502. if ($item['parent_item_id'] == 0) {
  5503. $parent_id = $item['id'];
  5504. break;
  5505. } else {
  5506. $parent_id = $item['parent_item_id'];
  5507. if (empty($parent_arrays)) {
  5508. $parent_arrays[] = intval($item['id']);
  5509. }
  5510. $parent_arrays[] = $parent_id;
  5511. break;
  5512. }
  5513. }
  5514. }
  5515. }
  5516. }
  5517. if (!empty($parent_arrays)) {
  5518. $parent_arrays = array_reverse($parent_arrays);
  5519. $val = '$elements';
  5520. $x = 0;
  5521. foreach($parent_arrays as $item) {
  5522. if ($x != count($parent_arrays) -1) {
  5523. $val .= '["'.$item.'"]["children"]';
  5524. } else {
  5525. $val .= '["'.$item.'"]["children"]';
  5526. }
  5527. $x++;
  5528. }
  5529. $val .= "";
  5530. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5531. eval($code_str);
  5532. } else {
  5533. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5534. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5535. }
  5536. }
  5537. }
  5538. $list = '<ul id="lp_item_list">';
  5539. $tree = self::print_recursive($elements, $default_data, $default_content);
  5540. if (!empty($tree)) {
  5541. $list .= $tree;
  5542. } else {
  5543. if ($drop_element_here) {
  5544. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5545. }
  5546. }
  5547. $list .= '</ul>';
  5548. $return .= Display::panelCollapse(
  5549. $this->name,
  5550. $list,
  5551. 'scorm-list',
  5552. null,
  5553. 'scorm-list-accordion',
  5554. 'scorm-list-collapse'
  5555. );
  5556. if ($update_audio == 'true') {
  5557. $return = $return_audio;
  5558. }
  5559. return $return;
  5560. }
  5561. /**
  5562. * @param array $elements
  5563. * @param array $default_data
  5564. * @param array $default_content
  5565. * @return string
  5566. */
  5567. public function print_recursive($elements, $default_data, $default_content)
  5568. {
  5569. $return = '';
  5570. foreach ($elements as $key => $item) {
  5571. if (isset($item['load_data']) || empty($item['data'])) {
  5572. $item['data'] = $default_data[$item['load_data']];
  5573. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5574. }
  5575. $sub_list = '';
  5576. if (isset($item['type']) && $item['type'] == 'dir') {
  5577. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5578. }
  5579. if (empty($item['children'])) {
  5580. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5581. $active = null;
  5582. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5583. $active = 'active';
  5584. }
  5585. $return .= Display::tag(
  5586. 'li',
  5587. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5588. array('id'=>$key, 'class'=>'record li_container')
  5589. );
  5590. } else {
  5591. // Sections
  5592. if (isset($item['children'])) {
  5593. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5594. }
  5595. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5596. $return .= Display::tag(
  5597. 'li',
  5598. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5599. array('id'=>$key, 'class'=>'record li_container')
  5600. );
  5601. }
  5602. }
  5603. return $return;
  5604. }
  5605. /**
  5606. * This function builds the action menu
  5607. * @param bool $returnContent Optional
  5608. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5609. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5610. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5611. * @return string
  5612. */
  5613. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5614. {
  5615. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5616. $actionsLeft = '';
  5617. $actionsRight = '';
  5618. $actionsLeft .= Display::url(
  5619. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5620. 'lp_controller.php?'.api_get_cidreq().'&' . http_build_query([
  5621. 'gradebook' => $gradebook,
  5622. 'action' => 'view',
  5623. 'lp_id' => $_SESSION['oLP']->lp_id,
  5624. 'isStudentView' => 'true'
  5625. ])
  5626. );
  5627. $actionsLeft .= Display::url(
  5628. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5629. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5630. 'action' => 'admin_view',
  5631. 'lp_id' => $_SESSION['oLP']->lp_id,
  5632. 'updateaudio' => 'true'
  5633. ])
  5634. );
  5635. if (!$isConfigPage) {
  5636. $actionsLeft .= Display::url(
  5637. Display::return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM),
  5638. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5639. 'action' => 'edit',
  5640. 'lp_id' => $_SESSION['oLP']->lp_id
  5641. ])
  5642. );
  5643. } else {
  5644. $actionsLeft .= Display::url(
  5645. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5646. 'lp_controller.php?' . http_build_query([
  5647. 'action' => 'build',
  5648. 'lp_id' => $_SESSION['oLP']->lp_id
  5649. ]) . '&' . api_get_cidreq()
  5650. );
  5651. }
  5652. if ($allowExpand) {
  5653. $actionsLeft .= Display::url(
  5654. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM) .
  5655. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5656. '#',
  5657. ['role' => 'button', 'id' => 'hide_bar_template']
  5658. );
  5659. }
  5660. if ($showRequirementButtons) {
  5661. $buttons = array(
  5662. array(
  5663. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5664. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5665. 'action' => 'set_previous_step_as_prerequisite',
  5666. 'lp_id' => $_SESSION['oLP']->lp_id
  5667. ])
  5668. ),
  5669. array(
  5670. 'title' => get_lang('ClearAllPrerequisites'),
  5671. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5672. 'action' => 'clear_prerequisites',
  5673. 'lp_id' => $_SESSION['oLP']->lp_id
  5674. ])
  5675. ),
  5676. );
  5677. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons, true);
  5678. }
  5679. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5680. if ($returnContent) {
  5681. return $toolbar;
  5682. }
  5683. echo $toolbar;
  5684. }
  5685. /**
  5686. * Creates the default learning path folder
  5687. * @param array $course
  5688. * @param int $creatorId
  5689. *
  5690. * @return bool
  5691. */
  5692. public static function generate_learning_path_folder($course, $creatorId = 0)
  5693. {
  5694. // Creating learning_path folder
  5695. $dir = '/learning_path';
  5696. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5697. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5698. $folder = false;
  5699. if (!is_dir($filepath.'/'.$dir)) {
  5700. $folderData = create_unexisting_directory(
  5701. $course,
  5702. $creatorId,
  5703. 0,
  5704. 0,
  5705. 0,
  5706. $filepath,
  5707. $dir,
  5708. get_lang('LearningPaths'),
  5709. 0
  5710. );
  5711. if (!empty($folderData)) {
  5712. $folder = true;
  5713. }
  5714. } else {
  5715. $folder = true;
  5716. }
  5717. return $folder;
  5718. }
  5719. /**
  5720. * @param array $course
  5721. * @param string $lp_name
  5722. * @param int $creatorId
  5723. *
  5724. * @return array
  5725. */
  5726. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5727. {
  5728. $filepath = '';
  5729. $dir = '/learning_path/';
  5730. if (empty($lp_name)) {
  5731. $lp_name = $this->name;
  5732. }
  5733. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5734. $folder = self::generate_learning_path_folder($course, $creatorId);
  5735. // Limits title size
  5736. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5737. $dir = $dir.$title;
  5738. // Creating LP folder
  5739. $documentId = null;
  5740. if ($folder) {
  5741. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5742. if (!is_dir($filepath.'/'.$dir)) {
  5743. $folderData = create_unexisting_directory(
  5744. $course,
  5745. $creatorId,
  5746. 0,
  5747. 0,
  5748. 0,
  5749. $filepath,
  5750. $dir,
  5751. $lp_name
  5752. );
  5753. if (!empty($folderData)) {
  5754. $folder = true;
  5755. }
  5756. $documentId = $folderData['id'];
  5757. } else {
  5758. $folder = true;
  5759. }
  5760. $dir = $dir.'/';
  5761. if ($folder) {
  5762. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5763. }
  5764. }
  5765. if (empty($documentId)) {
  5766. $dir = api_remove_trailing_slash($dir);
  5767. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5768. }
  5769. $array = array(
  5770. 'dir' => $dir,
  5771. 'filepath' => $filepath,
  5772. 'folder' => $folder,
  5773. 'id' => $documentId
  5774. );
  5775. return $array;
  5776. }
  5777. /**
  5778. * Create a new document //still needs some finetuning
  5779. * @param array $courseInfo
  5780. * @param string $content
  5781. * @param string $title
  5782. * @param string $extension
  5783. * @param int $parentId
  5784. * @param int $creatorId creator id
  5785. *
  5786. * @return string
  5787. */
  5788. public function create_document(
  5789. $courseInfo,
  5790. $content = '',
  5791. $title = '',
  5792. $extension = 'html',
  5793. $parentId = 0,
  5794. $creatorId = 0
  5795. ) {
  5796. if (!empty($courseInfo)) {
  5797. $course_id = $courseInfo['real_id'];
  5798. } else {
  5799. $course_id = api_get_course_int_id();
  5800. }
  5801. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5802. $sessionId = api_get_session_id();
  5803. // Generates folder
  5804. $result = $this->generate_lp_folder($courseInfo);
  5805. $dir = $result['dir'];
  5806. if (empty($parentId) || $parentId == '/') {
  5807. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  5808. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5809. if ($parentId === '/') {
  5810. $dir = '/';
  5811. }
  5812. // Please, do not modify this dirname formatting.
  5813. if (strstr($dir, '..')) {
  5814. $dir = '/';
  5815. }
  5816. if (!empty($dir[0]) && $dir[0] == '.') {
  5817. $dir = substr($dir, 1);
  5818. }
  5819. if (!empty($dir[0]) && $dir[0] != '/') {
  5820. $dir = '/'.$dir;
  5821. }
  5822. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5823. $dir .= '/';
  5824. }
  5825. } else {
  5826. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5827. if (!empty($parentInfo)) {
  5828. $dir = $parentInfo['path'].'/';
  5829. }
  5830. }
  5831. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5832. if (!is_dir($filepath)) {
  5833. $dir = '/';
  5834. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5835. }
  5836. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5837. // is already escaped twice when it gets here.
  5838. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5839. if (!empty($title)) {
  5840. $title = api_replace_dangerous_char(stripslashes($title));
  5841. } else {
  5842. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5843. }
  5844. $title = disable_dangerous_file($title);
  5845. $filename = $title;
  5846. $content = !empty($content) ? $content : $_POST['content_lp'];
  5847. $tmp_filename = $filename;
  5848. $i = 0;
  5849. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5850. $tmp_filename = $filename . '_' . ++ $i;
  5851. }
  5852. $filename = $tmp_filename . '.'.$extension;
  5853. if ($extension == 'html') {
  5854. $content = stripslashes($content);
  5855. $content = str_replace(
  5856. api_get_path(WEB_COURSE_PATH),
  5857. api_get_path(REL_PATH).'courses/',
  5858. $content
  5859. );
  5860. // Change the path of mp3 to absolute.
  5861. // The first regexp deals with :// urls.
  5862. $content = preg_replace(
  5863. "|(flashvars=\"file=)([^:/]+)/|",
  5864. "$1".api_get_path(
  5865. REL_COURSE_PATH
  5866. ).$courseInfo['path'].'/document/',
  5867. $content
  5868. );
  5869. // The second regexp deals with audio/ urls.
  5870. $content = preg_replace(
  5871. "|(flashvars=\"file=)([^/]+)/|",
  5872. "$1".api_get_path(
  5873. REL_COURSE_PATH
  5874. ).$courseInfo['path'].'/document/$2/',
  5875. $content
  5876. );
  5877. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5878. $content = str_replace(
  5879. '</body>',
  5880. '<style type="text/css">body{}</style></body>',
  5881. $content
  5882. );
  5883. }
  5884. if (!file_exists($filepath . $filename)) {
  5885. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5886. fputs($fp, $content);
  5887. fclose($fp);
  5888. $file_size = filesize($filepath . $filename);
  5889. $save_file_path = $dir.$filename;
  5890. $document_id = add_document(
  5891. $courseInfo,
  5892. $save_file_path,
  5893. 'file',
  5894. $file_size,
  5895. $tmp_filename,
  5896. '',
  5897. 0, //readonly
  5898. true,
  5899. null,
  5900. $sessionId,
  5901. $creatorId
  5902. );
  5903. if ($document_id) {
  5904. api_item_property_update(
  5905. $courseInfo,
  5906. TOOL_DOCUMENT,
  5907. $document_id,
  5908. 'DocumentAdded',
  5909. $creatorId,
  5910. null,
  5911. null,
  5912. null,
  5913. null,
  5914. $sessionId
  5915. );
  5916. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5917. $new_title = $originalTitle;
  5918. if ($new_comment || $new_title) {
  5919. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5920. $ct = '';
  5921. if ($new_comment)
  5922. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5923. if ($new_title)
  5924. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5925. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5926. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5927. Database::query($sql);
  5928. }
  5929. }
  5930. return $document_id;
  5931. }
  5932. }
  5933. }
  5934. /**
  5935. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5936. * @param array $_course array
  5937. * @return void
  5938. */
  5939. public function edit_document($_course)
  5940. {
  5941. $course_id = api_get_course_int_id();
  5942. $urlAppend = api_get_configuration_value('url_append');
  5943. // Please, do not modify this dirname formatting.
  5944. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5945. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  5946. if (strstr($dir, '..')) {
  5947. $dir = '/';
  5948. }
  5949. if (isset($dir[0]) && $dir[0] == '.') {
  5950. $dir = substr($dir, 1);
  5951. }
  5952. if (isset($dir[0]) && $dir[0] != '/') {
  5953. $dir = '/'.$dir;
  5954. }
  5955. if (isset($dir[strlen($dir) - 1] ) && $dir[strlen($dir) - 1] != '/') {
  5956. $dir .= '/';
  5957. }
  5958. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5959. if (!is_dir($filepath)) {
  5960. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5961. }
  5962. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  5963. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5964. $document_id = intval($_POST['path']);
  5965. $sql = "SELECT path FROM " . $table_doc . "
  5966. WHERE c_id = $course_id AND id = " . $document_id;
  5967. $res = Database::query($sql);
  5968. $row = Database::fetch_array($res);
  5969. $content = stripslashes($_POST['content_lp']);
  5970. $file = $filepath . $row['path'];
  5971. if ($fp = @ fopen($file, 'w')) {
  5972. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  5973. // Change the path of mp3 to absolute.
  5974. // The first regexp deals with :// urls.
  5975. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5976. // The second regexp deals with audio/ urls.
  5977. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5978. fputs($fp, $content);
  5979. fclose($fp);
  5980. $sql = "UPDATE " . $table_doc ." SET
  5981. title='".Database::escape_string($_POST['title'])."'
  5982. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5983. Database::query($sql);
  5984. }
  5985. }
  5986. }
  5987. /**
  5988. * Displays the selected item, with a panel for manipulating the item
  5989. * @param int $item_id
  5990. * @param string $msg
  5991. * @return string
  5992. */
  5993. public function display_item($item_id, $msg = null, $show_actions = true)
  5994. {
  5995. $course_id = api_get_course_int_id();
  5996. $return = '';
  5997. if (is_numeric($item_id)) {
  5998. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5999. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  6000. WHERE
  6001. c_id = $course_id AND
  6002. lp.id = " . intval($item_id);
  6003. $result = Database::query($sql);
  6004. while ($row = Database::fetch_array($result,'ASSOC')) {
  6005. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  6006. // Prevents wrong parent selection for document, see Bug#1251.
  6007. if ($row['item_type'] != 'dir') {
  6008. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  6009. }
  6010. if ($show_actions) {
  6011. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6012. }
  6013. $return .= '<div style="padding:10px;">';
  6014. if ($msg != '') {
  6015. $return .= $msg;
  6016. }
  6017. $return .= '<h3>'.$row['title'].'</h3>';
  6018. switch ($row['item_type']) {
  6019. case TOOL_THREAD:
  6020. $link = $this->rl_get_resource_link_for_learnpath(
  6021. $course_id,
  6022. $row['lp_id'],
  6023. $item_id,
  6024. 0
  6025. );
  6026. $return .= Display::url(
  6027. get_lang('GoToThread'),
  6028. $link,
  6029. ['class' => 'btn btn-primary']
  6030. );
  6031. break;
  6032. case TOOL_FORUM:
  6033. $return .= Display::url(
  6034. get_lang('GoToForum'),
  6035. api_get_path(WEB_CODE_PATH) . 'forum/viewforum.php?' . api_get_cidreq() . '&forum=' . $row['path'],
  6036. ['class' => 'btn btn-primary']
  6037. );
  6038. break;
  6039. case TOOL_QUIZ:
  6040. if (!empty($row['path'])) {
  6041. $exercise = new Exercise();
  6042. $exercise->read($row['path']);
  6043. $return .= $exercise->description.'<br />';
  6044. $return .= Display::url(
  6045. get_lang('GoToExercise'),
  6046. api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . api_get_cidreq() . '&exerciseId=' . $exercise->id,
  6047. ['class' => 'btn btn-primary']
  6048. );
  6049. }
  6050. break;
  6051. case TOOL_LP_FINAL_ITEM:
  6052. $return .= $this->getSavedFinalItem();
  6053. break;
  6054. case TOOL_DOCUMENT:
  6055. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6056. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6057. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  6058. $result = Database::query($sql_doc);
  6059. $path_file = Database::result($result, 0, 0);
  6060. $path_parts = pathinfo($path_file);
  6061. // TODO: Correct the following naive comparisons.
  6062. if (in_array($path_parts['extension'], array(
  6063. 'html',
  6064. 'txt',
  6065. 'png',
  6066. 'jpg',
  6067. 'JPG',
  6068. 'jpeg',
  6069. 'JPEG',
  6070. 'gif',
  6071. 'swf',
  6072. 'pdf',
  6073. 'htm'
  6074. ))) {
  6075. $return .= $this->display_document($row['path'], true, true);
  6076. }
  6077. break;
  6078. case TOOL_HOTPOTATOES:
  6079. $return .= $this->display_document($row['path'], false, true);
  6080. break;
  6081. }
  6082. $return .= '</div>';
  6083. }
  6084. }
  6085. return $return;
  6086. }
  6087. /**
  6088. * Shows the needed forms for editing a specific item
  6089. * @param int $item_id
  6090. * @return string
  6091. */
  6092. public function display_edit_item($item_id)
  6093. {
  6094. $course_id = api_get_course_int_id();
  6095. $return = '';
  6096. if (is_numeric($item_id)) {
  6097. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6098. $sql = "SELECT * FROM $tbl_lp_item
  6099. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  6100. $res = Database::query($sql);
  6101. $row = Database::fetch_array($res);
  6102. switch ($row['item_type']) {
  6103. case 'dir':
  6104. case 'asset':
  6105. case 'sco':
  6106. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  6107. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6108. $return .= $this->display_item_form(
  6109. $row['item_type'],
  6110. get_lang('EditCurrentChapter').' :',
  6111. 'edit',
  6112. $item_id,
  6113. $row
  6114. );
  6115. } else {
  6116. $return .= $this->display_item_small_form(
  6117. $row['item_type'],
  6118. get_lang('EditCurrentChapter').' :',
  6119. $row
  6120. );
  6121. }
  6122. break;
  6123. case TOOL_DOCUMENT:
  6124. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6125. $sql = "SELECT lp.*, doc.path as dir
  6126. FROM $tbl_lp_item as lp
  6127. LEFT JOIN $tbl_doc as doc
  6128. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6129. WHERE
  6130. lp.c_id = $course_id AND
  6131. doc.c_id = $course_id AND
  6132. lp.id = " . intval($item_id);
  6133. $res_step = Database::query($sql);
  6134. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6135. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6136. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6137. break;
  6138. case TOOL_LINK:
  6139. $link_id = (string) $row['path'];
  6140. if (ctype_digit($link_id)) {
  6141. $tbl_link = Database::get_course_table(TABLE_LINK);
  6142. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  6143. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  6144. $res_link = Database::query($sql_select);
  6145. $row_link = Database::fetch_array($res_link);
  6146. if (is_array($row_link)) {
  6147. $row['url'] = $row_link['url'];
  6148. }
  6149. }
  6150. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6151. $return .= $this->display_link_form('edit', $item_id, $row);
  6152. break;
  6153. case TOOL_LP_FINAL_ITEM:
  6154. Session::write('finalItem', true);
  6155. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6156. $sql = "SELECT lp.*, doc.path as dir
  6157. FROM $tbl_lp_item as lp
  6158. LEFT JOIN $tbl_doc as doc
  6159. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6160. WHERE
  6161. lp.c_id = $course_id AND
  6162. doc.c_id = $course_id AND
  6163. lp.id = " . intval($item_id);
  6164. $res_step = Database::query($sql);
  6165. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6166. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6167. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6168. break;
  6169. case TOOL_QUIZ:
  6170. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6171. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6172. break;
  6173. case TOOL_HOTPOTATOES:
  6174. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6175. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6176. break;
  6177. case TOOL_STUDENTPUBLICATION:
  6178. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6179. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6180. break;
  6181. case TOOL_FORUM:
  6182. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6183. $return .= $this->display_forum_form('edit', $item_id, $row);
  6184. break;
  6185. case TOOL_THREAD:
  6186. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6187. $return .= $this->display_thread_form('edit', $item_id, $row);
  6188. break;
  6189. }
  6190. }
  6191. return $return;
  6192. }
  6193. /**
  6194. * Function that displays a list with al the resources that
  6195. * could be added to the learning path
  6196. * @return string
  6197. */
  6198. public function display_resources()
  6199. {
  6200. $course_code = api_get_course_id();
  6201. // Get all the docs.
  6202. $documents = $this->get_documents(true);
  6203. // Get all the exercises.
  6204. $exercises = $this->get_exercises();
  6205. // Get all the links.
  6206. $links = $this->get_links();
  6207. // Get all the student publications.
  6208. $works = $this->get_student_publications();
  6209. // Get all the forums.
  6210. $forums = $this->get_forums(null, $course_code);
  6211. // Get the final item form (see BT#11048) .
  6212. $finish = $this->getFinalItemForm();
  6213. $headers = array(
  6214. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  6215. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  6216. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  6217. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  6218. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  6219. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  6220. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6221. );
  6222. echo Display::return_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'), 'normal');
  6223. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6224. echo Display::tabs(
  6225. $headers,
  6226. array(
  6227. $documents,
  6228. $exercises,
  6229. $links,
  6230. $works,
  6231. $forums,
  6232. $dir,
  6233. $finish,
  6234. ),
  6235. 'resource_tab'
  6236. );
  6237. return true;
  6238. }
  6239. /**
  6240. * Returns the extension of a document
  6241. * @param string $filename
  6242. * @return string Extension (part after the last dot)
  6243. */
  6244. public function get_extension($filename)
  6245. {
  6246. $explode = explode('.', $filename);
  6247. return $explode[count($explode) - 1];
  6248. }
  6249. /**
  6250. * Displays a document by id
  6251. *
  6252. * @param int $id
  6253. * @return string
  6254. */
  6255. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6256. {
  6257. $_course = api_get_course_info();
  6258. $course_id = api_get_course_int_id();
  6259. $return = '';
  6260. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6261. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  6262. WHERE c_id = ".$course_id." AND id = " . $id;
  6263. $res_doc = Database::query($sql_doc);
  6264. $row_doc = Database::fetch_array($res_doc);
  6265. // TODO: Add a path filter.
  6266. if ($iframe) {
  6267. $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>';
  6268. } else {
  6269. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/' . $row_doc['path']);
  6270. }
  6271. return $return;
  6272. }
  6273. /**
  6274. * Return HTML form to add/edit a quiz
  6275. * @param string $action Action (add/edit)
  6276. * @param integer $id Item ID if already exists
  6277. * @param mixed $extra_info Extra information (quiz ID if integer)
  6278. * @return string HTML form
  6279. */
  6280. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6281. {
  6282. $course_id = api_get_course_int_id();
  6283. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6284. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6285. if ($id != 0 && is_array($extra_info)) {
  6286. $item_title = $extra_info['title'];
  6287. $item_description = $extra_info['description'];
  6288. } elseif (is_numeric($extra_info)) {
  6289. $sql = "SELECT title, description
  6290. FROM $tbl_quiz
  6291. WHERE c_id = $course_id AND id = " . $extra_info;
  6292. $result = Database::query($sql);
  6293. $row = Database::fetch_array($result);
  6294. $item_title = $row['title'];
  6295. $item_description = $row['description'];
  6296. } else {
  6297. $item_title = '';
  6298. $item_description = '';
  6299. }
  6300. $item_title = Security::remove_XSS($item_title);
  6301. $item_description = Security::remove_XSS($item_description);
  6302. if ($id != 0 && is_array($extra_info)) {
  6303. $parent = $extra_info['parent_item_id'];
  6304. } else {
  6305. $parent = 0;
  6306. }
  6307. $sql = "SELECT * FROM $tbl_lp_item
  6308. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  6309. $result = Database::query($sql);
  6310. $arrLP = array ();
  6311. while ($row = Database::fetch_array($result)) {
  6312. $arrLP[] = array(
  6313. 'id' => $row['id'],
  6314. 'item_type' => $row['item_type'],
  6315. 'title' => $row['title'],
  6316. 'path' => $row['path'],
  6317. 'description' => $row['description'],
  6318. 'parent_item_id' => $row['parent_item_id'],
  6319. 'previous_item_id' => $row['previous_item_id'],
  6320. 'next_item_id' => $row['next_item_id'],
  6321. 'display_order' => $row['display_order'],
  6322. 'max_score' => $row['max_score'],
  6323. 'min_score' => $row['min_score'],
  6324. 'mastery_score' => $row['mastery_score'],
  6325. 'prerequisite' => $row['prerequisite'],
  6326. 'max_time_allowed' => $row['max_time_allowed']
  6327. );
  6328. }
  6329. $this->tree_array($arrLP);
  6330. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6331. unset($this->arrMenu);
  6332. $form = new FormValidator(
  6333. 'quiz_form',
  6334. 'POST',
  6335. $this->getCurrentBuildingModeURL()
  6336. );
  6337. $defaults = [];
  6338. if ($action == 'add') {
  6339. $legend = get_lang('CreateTheExercise');
  6340. } elseif ($action == 'move') {
  6341. $legend = get_lang('MoveTheCurrentExercise');
  6342. } else {
  6343. $legend = get_lang('EditCurrentExecice');
  6344. }
  6345. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6346. $legend .= Display::return_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6347. }
  6348. $form->addHeader($legend);
  6349. if ($action != 'move') {
  6350. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6351. $defaults['title'] = $item_title;
  6352. }
  6353. // Select for Parent item, root or chapter
  6354. $selectParent = $form->addSelect(
  6355. 'parent',
  6356. get_lang('Parent'),
  6357. [],
  6358. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6359. );
  6360. $selectParent->addOption($this->name, 0);
  6361. $arrHide = array (
  6362. $id
  6363. );
  6364. for ($i = 0; $i < count($arrLP); $i++) {
  6365. if ($action != 'add') {
  6366. if (
  6367. ($arrLP[$i]['item_type'] == 'dir') &&
  6368. !in_array($arrLP[$i]['id'], $arrHide) &&
  6369. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6370. ) {
  6371. $selectParent->addOption(
  6372. $arrLP[$i]['title'],
  6373. $arrLP[$i]['id'],
  6374. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6375. );
  6376. if ($parent == $arrLP[$i]['id']) {
  6377. $selectParent->setSelected($arrLP[$i]['id']);
  6378. }
  6379. } else {
  6380. $arrHide[] = $arrLP[$i]['id'];
  6381. }
  6382. } else {
  6383. if ($arrLP[$i]['item_type'] == 'dir') {
  6384. $selectParent->addOption(
  6385. $arrLP[$i]['title'],
  6386. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6387. );
  6388. if ($parent == $arrLP[$i]['id']) {
  6389. $selectParent->setSelected($arrLP[$i]['id']);
  6390. }
  6391. }
  6392. }
  6393. }
  6394. if (is_array($arrLP)) {
  6395. reset($arrLP);
  6396. }
  6397. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6398. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6399. for ($i = 0; $i < count($arrLP); $i++) {
  6400. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6401. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6402. if (is_array($extra_info)) {
  6403. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6404. $selectPrevious->setSelected($arrLP[$i]['id']);
  6405. }
  6406. } elseif ($action == 'add') {
  6407. $selectPrevious->setSelected($arrLP[$i]['id']);
  6408. }
  6409. }
  6410. }
  6411. if ($action != 'move') {
  6412. $id_prerequisite = 0;
  6413. if (is_array($arrLP)) {
  6414. foreach ($arrLP as $key => $value) {
  6415. if ($value['id'] == $id) {
  6416. $id_prerequisite = $value['prerequisite'];
  6417. break;
  6418. }
  6419. }
  6420. }
  6421. $arrHide = array ();
  6422. for ($i = 0; $i < count($arrLP); $i++) {
  6423. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6424. if (is_array($extra_info)) {
  6425. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6426. $s_selected_position = $arrLP[$i]['id'];
  6427. }
  6428. } elseif ($action == 'add') {
  6429. $s_selected_position = 0;
  6430. }
  6431. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6432. }
  6433. }
  6434. /*// Commented the prerequisites, only visible in edit (exercise).
  6435. $return .= '<tr>';
  6436. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6437. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6438. foreach($arrHide as $key => $value){
  6439. if($key==$s_selected_position && $action == 'add'){
  6440. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6441. }
  6442. elseif($key==$id_prerequisite && $action == 'edit'){
  6443. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6444. }
  6445. else{
  6446. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6447. }
  6448. }
  6449. $return .= "</select></td>";
  6450. */
  6451. /*$return .= '<tr>';
  6452. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6453. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6454. // Remove temporarily the test description.
  6455. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6456. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6457. $return .= '</tr>'; */
  6458. }
  6459. if ($action == 'add') {
  6460. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6461. } else {
  6462. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6463. }
  6464. if ($action == 'move') {
  6465. $form->addHidden('title', $item_title);
  6466. $form->addHidden('description', $item_description);
  6467. }
  6468. if (is_numeric($extra_info)) {
  6469. $form->addHidden('path', $extra_info);
  6470. } elseif (is_array($extra_info)) {
  6471. $form->addHidden('path', $extra_info['path']);
  6472. }
  6473. $form->addHidden('type', TOOL_QUIZ);
  6474. $form->addHidden('post_time', time());
  6475. $form->setDefaults($defaults);
  6476. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6477. }
  6478. /**
  6479. * Addition of Hotpotatoes tests
  6480. * @param string Action
  6481. * @param integer Internal ID of the item
  6482. * @param mixed Extra information - can be an array with title and description indexes
  6483. * @return string HTML structure to display the hotpotatoes addition formular
  6484. */
  6485. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6486. {
  6487. $course_id = api_get_course_int_id();
  6488. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6489. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6490. if ($id != 0 && is_array($extra_info)) {
  6491. $item_title = stripslashes($extra_info['title']);
  6492. $item_description = stripslashes($extra_info['description']);
  6493. } elseif (is_numeric($extra_info)) {
  6494. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6495. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6496. WHERE
  6497. c_id = ".$course_id." AND
  6498. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6499. id = " . (int) $extra_info . "
  6500. ORDER BY id ASC";
  6501. $res_hot = Database::query($sql);
  6502. $row = Database::fetch_array($res_hot);
  6503. $item_title = $row['title'];
  6504. $item_description = $row['description'];
  6505. if (!empty ($row['comment'])) {
  6506. $item_title = $row['comment'];
  6507. }
  6508. } else {
  6509. $item_title = '';
  6510. $item_description = '';
  6511. }
  6512. if ($id != 0 && is_array($extra_info)) {
  6513. $parent = $extra_info['parent_item_id'];
  6514. } else {
  6515. $parent = 0;
  6516. }
  6517. $sql = "SELECT * FROM $tbl_lp_item
  6518. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6519. $result = Database::query($sql);
  6520. $arrLP = array ();
  6521. while ($row = Database::fetch_array($result)) {
  6522. $arrLP[] = array(
  6523. 'id' => $row['id'],
  6524. 'item_type' => $row['item_type'],
  6525. 'title' => $row['title'],
  6526. 'path' => $row['path'],
  6527. 'description' => $row['description'],
  6528. 'parent_item_id' => $row['parent_item_id'],
  6529. 'previous_item_id' => $row['previous_item_id'],
  6530. 'next_item_id' => $row['next_item_id'],
  6531. 'display_order' => $row['display_order'],
  6532. 'max_score' => $row['max_score'],
  6533. 'min_score' => $row['min_score'],
  6534. 'mastery_score' => $row['mastery_score'],
  6535. 'prerequisite' => $row['prerequisite'],
  6536. 'max_time_allowed' => $row['max_time_allowed']
  6537. );
  6538. }
  6539. $legend = '<legend>';
  6540. if ($action == 'add') {
  6541. $legend .= get_lang('CreateTheExercise');
  6542. } elseif ($action == 'move') {
  6543. $legend .= get_lang('MoveTheCurrentExercise');
  6544. } else {
  6545. $legend .= get_lang('EditCurrentExecice');
  6546. }
  6547. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6548. $legend .= Display:: return_message(
  6549. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6550. );
  6551. }
  6552. $legend .= '</legend>';
  6553. $return = '<form method="POST">';
  6554. $return .= $legend;
  6555. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6556. $return .= '<tr>';
  6557. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6558. $return .= '<td class="input">';
  6559. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6560. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6561. $arrHide = array(
  6562. $id
  6563. );
  6564. if (count($arrLP) > 0) {
  6565. for ($i = 0; $i < count($arrLP); $i++) {
  6566. if ($action != 'add') {
  6567. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6568. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6569. } else {
  6570. $arrHide[] = $arrLP[$i]['id'];
  6571. }
  6572. } else {
  6573. if ($arrLP[$i]['item_type'] == 'dir')
  6574. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6575. }
  6576. }
  6577. reset($arrLP);
  6578. }
  6579. $return .= '</select>';
  6580. $return .= '</td>';
  6581. $return .= '</tr>';
  6582. $return .= '<tr>';
  6583. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6584. $return .= '<td class="input">';
  6585. $return .= '<select id="previous" name="previous" size="1">';
  6586. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6587. for ($i = 0; $i < count($arrLP); $i++) {
  6588. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6589. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6590. $selected = 'selected="selected" ';
  6591. elseif ($action == 'add') $selected = 'selected="selected" ';
  6592. else
  6593. $selected = '';
  6594. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6595. }
  6596. }
  6597. $return .= '</select>';
  6598. $return .= '</td>';
  6599. $return .= '</tr>';
  6600. if ($action != 'move') {
  6601. $return .= '<tr>';
  6602. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6603. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6604. $return .= '</tr>';
  6605. $id_prerequisite = 0;
  6606. if (is_array($arrLP) && count($arrLP) > 0) {
  6607. foreach ($arrLP as $key => $value) {
  6608. if ($value['id'] == $id) {
  6609. $id_prerequisite = $value['prerequisite'];
  6610. break;
  6611. }
  6612. }
  6613. $arrHide = array ();
  6614. for ($i = 0; $i < count($arrLP); $i++) {
  6615. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6616. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6617. $s_selected_position = $arrLP[$i]['id'];
  6618. elseif ($action == 'add') $s_selected_position = 0;
  6619. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6620. }
  6621. }
  6622. }
  6623. }
  6624. $return .= '<tr>';
  6625. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6626. $return .= '</tr>';
  6627. $return .= '</table>';
  6628. if ($action == 'move') {
  6629. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6630. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6631. }
  6632. if (is_numeric($extra_info)) {
  6633. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6634. } elseif (is_array($extra_info)) {
  6635. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6636. }
  6637. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6638. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6639. $return .= '</form>';
  6640. return $return;
  6641. }
  6642. /**
  6643. * Return the form to display the forum edit/add option
  6644. * @param string Action (add/edit)
  6645. * @param integer ID of the lp_item if already exists
  6646. * @param mixed Forum ID or title
  6647. * @return string HTML form
  6648. */
  6649. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6650. {
  6651. $course_id = api_get_course_int_id();
  6652. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6653. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6654. if ($id != 0 && is_array($extra_info)) {
  6655. $item_title = stripslashes($extra_info['title']);
  6656. } elseif (is_numeric($extra_info)) {
  6657. $sql = "SELECT forum_title as title, forum_comment as comment
  6658. FROM " . $tbl_forum . "
  6659. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6660. $result = Database::query($sql);
  6661. $row = Database::fetch_array($result);
  6662. $item_title = $row['title'];
  6663. $item_description = $row['comment'];
  6664. } else {
  6665. $item_title = '';
  6666. $item_description = '';
  6667. }
  6668. if ($id != 0 && is_array($extra_info)) {
  6669. $parent = $extra_info['parent_item_id'];
  6670. } else {
  6671. $parent = 0;
  6672. }
  6673. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6674. WHERE
  6675. c_id = ".$course_id." AND
  6676. lp_id = " . $this->lp_id;
  6677. $result = Database::query($sql);
  6678. $arrLP = array();
  6679. while ($row = Database::fetch_array($result)) {
  6680. $arrLP[] = array(
  6681. 'id' => $row['id'],
  6682. 'item_type' => $row['item_type'],
  6683. 'title' => $row['title'],
  6684. 'path' => $row['path'],
  6685. 'description' => $row['description'],
  6686. 'parent_item_id' => $row['parent_item_id'],
  6687. 'previous_item_id' => $row['previous_item_id'],
  6688. 'next_item_id' => $row['next_item_id'],
  6689. 'display_order' => $row['display_order'],
  6690. 'max_score' => $row['max_score'],
  6691. 'min_score' => $row['min_score'],
  6692. 'mastery_score' => $row['mastery_score'],
  6693. 'prerequisite' => $row['prerequisite']
  6694. );
  6695. }
  6696. $this->tree_array($arrLP);
  6697. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6698. unset($this->arrMenu);
  6699. if ($action == 'add') {
  6700. $legend = get_lang('CreateTheForum');
  6701. } elseif ($action == 'move') {
  6702. $legend = get_lang('MoveTheCurrentForum');
  6703. } else {
  6704. $legend = get_lang('EditCurrentForum');
  6705. }
  6706. $form = new FormValidator(
  6707. 'forum_form',
  6708. 'POST',
  6709. $this->getCurrentBuildingModeURL()
  6710. );
  6711. $defaults = [];
  6712. $form->addHeader($legend);
  6713. if ($action != 'move') {
  6714. $form->addText(
  6715. 'title',
  6716. get_lang('Title'),
  6717. true,
  6718. ['id' => 'idTitle', 'class' => 'learnpath_item_form']
  6719. );
  6720. $defaults['title'] = $item_title;
  6721. }
  6722. $selectParent = $form->addSelect(
  6723. 'parent',
  6724. get_lang('Parent'),
  6725. [],
  6726. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6727. );
  6728. $selectParent->addOption($this->name, 0);
  6729. $arrHide = array(
  6730. $id
  6731. );
  6732. //$parent_item_id = $_SESSION['parent_item_id'];
  6733. for ($i = 0; $i < count($arrLP); $i++) {
  6734. if ($action != 'add') {
  6735. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6736. $selectParent->addOption(
  6737. $arrLP[$i]['title'],
  6738. $arrLP[$i]['id'],
  6739. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6740. );
  6741. if ($parent == $arrLP[$i]['id']) {
  6742. $selectParent->setSelected($arrLP[$i]['id']);
  6743. }
  6744. } else {
  6745. $arrHide[] = $arrLP[$i]['id'];
  6746. }
  6747. } else {
  6748. if ($arrLP[$i]['item_type'] == 'dir') {
  6749. $selectParent->addOption(
  6750. $arrLP[$i]['title'],
  6751. $arrLP[$i]['id'],
  6752. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6753. );
  6754. if ($parent == $arrLP[$i]['id']) {
  6755. $selectParent->setSelected($arrLP[$i]['id']);
  6756. }
  6757. }
  6758. }
  6759. }
  6760. if (is_array($arrLP)) {
  6761. reset($arrLP);
  6762. }
  6763. $selectPrevious = $form->addSelect(
  6764. 'previous',
  6765. get_lang('Position'),
  6766. [],
  6767. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6768. );
  6769. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6770. for ($i = 0; $i < count($arrLP); $i++) {
  6771. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6772. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6773. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6774. $selectPrevious->setSelected($arrLP[$i]['id']);
  6775. } elseif ($action == 'add') {
  6776. $selectPrevious->setSelected($arrLP[$i]['id']);
  6777. }
  6778. }
  6779. }
  6780. if ($action != 'move') {
  6781. $id_prerequisite = 0;
  6782. if (is_array($arrLP)) {
  6783. foreach ($arrLP as $key => $value) {
  6784. if ($value['id'] == $id) {
  6785. $id_prerequisite = $value['prerequisite'];
  6786. break;
  6787. }
  6788. }
  6789. }
  6790. $arrHide = array();
  6791. for ($i = 0; $i < count($arrLP); $i++) {
  6792. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6793. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6794. $s_selected_position = $arrLP[$i]['id'];
  6795. elseif ($action == 'add') $s_selected_position = 0;
  6796. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6797. }
  6798. }
  6799. }
  6800. if ($action == 'add') {
  6801. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6802. } else {
  6803. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6804. }
  6805. if ($action == 'move') {
  6806. $form->addHidden('title', $item_title);
  6807. $form->addHidden('description', $item_description);
  6808. }
  6809. if (is_numeric($extra_info)) {
  6810. $form->addHidden('path', $extra_info);
  6811. } elseif (is_array($extra_info)) {
  6812. $form->addHidden('path', $extra_info['path']);
  6813. }
  6814. $form->addHidden('type', TOOL_FORUM);
  6815. $form->addHidden('post_time', time());
  6816. $form->setDefaults($defaults);
  6817. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6818. }
  6819. /**
  6820. * Return HTML form to add/edit forum threads
  6821. * @param string Action (add/edit)
  6822. * @param integer Item ID if already exists in learning path
  6823. * @param mixed Extra information (thread ID if integer)
  6824. * @return string HTML form
  6825. */
  6826. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6827. {
  6828. $course_id = api_get_course_int_id();
  6829. if (empty($course_id)) {
  6830. return null;
  6831. }
  6832. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6833. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  6834. if ($id != 0 && is_array($extra_info)) {
  6835. $item_title = stripslashes($extra_info['title']);
  6836. } elseif (is_numeric($extra_info)) {
  6837. $sql = "SELECT thread_title as title FROM $tbl_forum
  6838. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6839. $result = Database::query($sql);
  6840. $row = Database::fetch_array($result);
  6841. $item_title = $row['title'];
  6842. $item_description = '';
  6843. } else {
  6844. $item_title = '';
  6845. $item_description = '';
  6846. }
  6847. if ($id != 0 && is_array($extra_info)) {
  6848. $parent = $extra_info['parent_item_id'];
  6849. } else {
  6850. $parent = 0;
  6851. }
  6852. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6853. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6854. $result = Database::query($sql);
  6855. $arrLP = array();
  6856. while ($row = Database::fetch_array($result)) {
  6857. $arrLP[] = array (
  6858. 'id' => $row['id'],
  6859. 'item_type' => $row['item_type'],
  6860. 'title' => $row['title'],
  6861. 'path' => $row['path'],
  6862. 'description' => $row['description'],
  6863. 'parent_item_id' => $row['parent_item_id'],
  6864. 'previous_item_id' => $row['previous_item_id'],
  6865. 'next_item_id' => $row['next_item_id'],
  6866. 'display_order' => $row['display_order'],
  6867. 'max_score' => $row['max_score'],
  6868. 'min_score' => $row['min_score'],
  6869. 'mastery_score' => $row['mastery_score'],
  6870. 'prerequisite' => $row['prerequisite']
  6871. );
  6872. }
  6873. $this->tree_array($arrLP);
  6874. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6875. unset($this->arrMenu);
  6876. $form = new FormValidator(
  6877. 'thread_form',
  6878. 'POST',
  6879. $this->getCurrentBuildingModeURL()
  6880. );
  6881. $defaults = [];
  6882. if ($action == 'add') {
  6883. $legend = get_lang('CreateTheForum');
  6884. } elseif ($action == 'move') {
  6885. $legend = get_lang('MoveTheCurrentForum');
  6886. } else {
  6887. $legend = get_lang('EditCurrentForum');
  6888. }
  6889. $form->addHeader($legend);
  6890. $selectParent = $form->addSelect(
  6891. 'parent',
  6892. get_lang('Parent'),
  6893. [],
  6894. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6895. );
  6896. $selectParent->addOption($this->name, 0);
  6897. $arrHide = array (
  6898. $id
  6899. );
  6900. for ($i = 0; $i < count($arrLP); $i++) {
  6901. if ($action != 'add') {
  6902. if (
  6903. ($arrLP[$i]['item_type'] == 'dir') &&
  6904. !in_array($arrLP[$i]['id'], $arrHide) &&
  6905. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6906. ) {
  6907. $selectParent->addOption(
  6908. $arrLP[$i]['title'],
  6909. $arrLP[$i]['id'],
  6910. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6911. );
  6912. if ($parent == $arrLP[$i]['id']) {
  6913. $selectParent->setSelected($arrLP[$i]['id']);
  6914. }
  6915. } else {
  6916. $arrHide[] = $arrLP[$i]['id'];
  6917. }
  6918. } else {
  6919. if ($arrLP[$i]['item_type'] == 'dir') {
  6920. $selectParent->addOption(
  6921. $arrLP[$i]['title'],
  6922. $arrLP[$i]['id'],
  6923. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6924. );
  6925. if ($parent == $arrLP[$i]['id']) {
  6926. $selectParent->setSelected($arrLP[$i]['id']);
  6927. }
  6928. }
  6929. }
  6930. }
  6931. if ($arrLP != null) {
  6932. reset($arrLP);
  6933. }
  6934. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6935. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6936. for ($i = 0; $i < count($arrLP); $i++) {
  6937. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6938. $selectPrevious->addOption(
  6939. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6940. $arrLP[$i]['id']
  6941. );
  6942. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6943. $selectPrevious->setSelected($arrLP[$i]['id']);
  6944. } elseif ($action == 'add') {
  6945. $selectPrevious->setSelected($arrLP[$i]['id']);
  6946. }
  6947. }
  6948. }
  6949. if ($action != 'move') {
  6950. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6951. $defaults['title'] = $item_title;
  6952. $id_prerequisite = 0;
  6953. if ($arrLP != null) {
  6954. foreach ($arrLP as $key => $value) {
  6955. if ($value['id'] == $id) {
  6956. $id_prerequisite = $value['prerequisite'];
  6957. break;
  6958. }
  6959. }
  6960. }
  6961. $arrHide = array();
  6962. $s_selected_position = 0;
  6963. for ($i = 0; $i < count($arrLP); $i++) {
  6964. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6965. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6966. $s_selected_position = $arrLP[$i]['id'];
  6967. elseif ($action == 'add') $s_selected_position = 0;
  6968. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6969. }
  6970. }
  6971. $selectPrerequisites = $form->addSelect(
  6972. 'prerequisites',
  6973. get_lang('LearnpathPrerequisites'),
  6974. [],
  6975. ['id' => 'prerequisites']
  6976. );
  6977. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6978. foreach ($arrHide as $key => $value) {
  6979. $selectPrerequisites->addOption($value['value'], $key);
  6980. if ($key == $s_selected_position && $action == 'add') {
  6981. $selectPrerequisites->setSelected($key);
  6982. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6983. $selectPrerequisites->setSelected($key);
  6984. }
  6985. }
  6986. }
  6987. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6988. if ($action == 'move') {
  6989. $form->addHidden('title', $item_title);
  6990. $form->addHidden('description', $item_description);
  6991. }
  6992. if (is_numeric($extra_info)) {
  6993. $form->addHidden('path', $extra_info);
  6994. }
  6995. elseif (is_array($extra_info)) {
  6996. $form->addHidden('path', $extra_info['path']);
  6997. }
  6998. $form->addHidden('type', TOOL_THREAD);
  6999. $form->addHidden('post_time', time());
  7000. $form->setDefaults($defaults);
  7001. return $form->returnForm();
  7002. }
  7003. /**
  7004. * Return the HTML form to display an item (generally a dir item)
  7005. * @param string Item type (dir)
  7006. * @param string Title (optional, only when creating)
  7007. * @param string Action ('add'/'edit')
  7008. * @param integer lp_item ID
  7009. * @param mixed Extra info
  7010. * @return string HTML form
  7011. */
  7012. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  7013. {
  7014. $course_id = api_get_course_int_id();
  7015. $_course = api_get_course_info();
  7016. global $charset;
  7017. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7018. if ($id != 0 && is_array($extra_info)) {
  7019. $item_title = $extra_info['title'];
  7020. $item_description = $extra_info['description'];
  7021. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  7022. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  7023. } else {
  7024. $item_title = '';
  7025. $item_description = '';
  7026. $item_path_fck = '';
  7027. }
  7028. if ($id != 0 && is_array($extra_info)) {
  7029. $parent = $extra_info['parent_item_id'];
  7030. } else {
  7031. $parent = 0;
  7032. }
  7033. $id = intval($id);
  7034. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7035. WHERE
  7036. c_id = ".$course_id." AND
  7037. lp_id = " . $this->lp_id . " AND
  7038. id != $id";
  7039. if ($item_type == 'dir') {
  7040. $sql .= " AND parent_item_id = 0";
  7041. }
  7042. $result = Database::query($sql);
  7043. $arrLP = [];
  7044. while ($row = Database::fetch_array($result)) {
  7045. $arrLP[] = array(
  7046. 'id' => $row['id'],
  7047. 'item_type' => $row['item_type'],
  7048. 'title' => $row['title'],
  7049. 'path' => $row['path'],
  7050. 'description' => $row['description'],
  7051. 'parent_item_id' => $row['parent_item_id'],
  7052. 'previous_item_id' => $row['previous_item_id'],
  7053. 'next_item_id' => $row['next_item_id'],
  7054. 'max_score' => $row['max_score'],
  7055. 'min_score' => $row['min_score'],
  7056. 'mastery_score' => $row['mastery_score'],
  7057. 'prerequisite' => $row['prerequisite'],
  7058. 'display_order' => $row['display_order']
  7059. );
  7060. }
  7061. $this->tree_array($arrLP);
  7062. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7063. unset($this->arrMenu);
  7064. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  7065. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  7066. $form = new FormValidator('form', 'POST', $url);
  7067. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  7068. $defaults['description'] = $item_description;
  7069. $form->addElement('header', $title);
  7070. //$arrHide = array($id);
  7071. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  7072. $arrHide[0]['padding'] = 20;
  7073. $charset = api_get_system_encoding();
  7074. for ($i = 0; $i < count($arrLP); $i++) {
  7075. if ($action != 'add') {
  7076. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7077. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7078. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7079. if ($parent == $arrLP[$i]['id']) {
  7080. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7081. }
  7082. }
  7083. } else {
  7084. if ($arrLP[$i]['item_type'] == 'dir') {
  7085. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7086. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7087. if ($parent == $arrLP[$i]['id']) {
  7088. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7089. }
  7090. }
  7091. }
  7092. }
  7093. if ($action != 'move') {
  7094. $form->addElement('text', 'title', get_lang('Title'));
  7095. $form->applyFilter('title', 'html_filter');
  7096. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  7097. } else {
  7098. $form->addElement('hidden', 'title');
  7099. }
  7100. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  7101. foreach ($arrHide as $key => $value) {
  7102. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7103. }
  7104. if (!empty($s_selected_parent)) {
  7105. $parent_select->setSelected($s_selected_parent);
  7106. }
  7107. if (is_array($arrLP)) {
  7108. reset($arrLP);
  7109. }
  7110. $arrHide = array();
  7111. // POSITION
  7112. for ($i = 0; $i < count($arrLP); $i++) {
  7113. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7114. //this is the same!
  7115. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7116. $s_selected_position = $arrLP[$i]['id'];
  7117. } elseif ($action == 'add') {
  7118. $s_selected_position = $arrLP[$i]['id'];
  7119. }
  7120. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7121. }
  7122. }
  7123. $position = $form->addElement(
  7124. 'select',
  7125. 'previous',
  7126. get_lang('Position'),
  7127. '',
  7128. array('id' => 'previous')
  7129. );
  7130. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7131. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  7132. foreach ($arrHide as $key => $value) {
  7133. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  7134. }
  7135. if (!empty ($s_selected_position)) {
  7136. $position->setSelected($s_selected_position);
  7137. }
  7138. if (is_array($arrLP)) {
  7139. reset($arrLP);
  7140. }
  7141. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  7142. //fix in order to use the tab
  7143. if ($item_type == 'dir') {
  7144. $form->addElement('hidden', 'type', 'dir');
  7145. }
  7146. $extension = null;
  7147. if (!empty($item_path)) {
  7148. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7149. }
  7150. //assets can't be modified
  7151. //$item_type == 'asset' ||
  7152. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7153. if ($item_type == 'sco') {
  7154. $form->addElement('html', '<script>alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  7155. }
  7156. $renderer = $form->defaultRenderer();
  7157. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7158. $relative_prefix = '';
  7159. $editor_config = array(
  7160. 'ToolbarSet' => 'LearningPathDocuments',
  7161. 'Width' => '100%',
  7162. 'Height' => '500',
  7163. 'FullPage' => true,
  7164. 'CreateDocumentDir' => $relative_prefix,
  7165. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7166. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7167. );
  7168. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7169. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7170. $defaults['content_lp'] = file_get_contents($content_path);
  7171. }
  7172. $form->addElement('hidden', 'type', $item_type);
  7173. $form->addElement('hidden', 'post_time', time());
  7174. $form->setDefaults($defaults);
  7175. return $form->return_form();
  7176. }
  7177. /**
  7178. * @return string
  7179. */
  7180. public function getCurrentBuildingModeURL()
  7181. {
  7182. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7183. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7184. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7185. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7186. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7187. return $currentUrl;
  7188. }
  7189. /**
  7190. * Returns the form to update or create a document
  7191. * @param string $action (add/edit)
  7192. * @param integer $id ID of the lp_item (if already exists)
  7193. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7194. * @return string HTML form
  7195. */
  7196. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7197. {
  7198. $course_id = api_get_course_int_id();
  7199. $_course = api_get_course_info();
  7200. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7201. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7202. $no_display_edit_textarea = false;
  7203. $item_description = '';
  7204. //If action==edit document
  7205. //We don't display the document form if it's not an editable document (html or txt file)
  7206. if ($action == 'edit') {
  7207. if (is_array($extra_info)) {
  7208. $path_parts = pathinfo($extra_info['dir']);
  7209. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7210. $no_display_edit_textarea = true;
  7211. }
  7212. }
  7213. }
  7214. $no_display_add = false;
  7215. // If action==add an existing document
  7216. // We don't display the document form if it's not an editable document (html or txt file).
  7217. if ($action == 'add') {
  7218. if (is_numeric($extra_info)) {
  7219. $sql_doc = "SELECT path FROM $tbl_doc
  7220. WHERE c_id = $course_id AND id = " . intval($extra_info);
  7221. $result = Database::query($sql_doc);
  7222. $path_file = Database::result($result, 0, 0);
  7223. $path_parts = pathinfo($path_file);
  7224. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7225. $no_display_add = true;
  7226. }
  7227. }
  7228. }
  7229. if ($id != 0 && is_array($extra_info)) {
  7230. $item_title = stripslashes($extra_info['title']);
  7231. $item_description = stripslashes($extra_info['description']);
  7232. $item_terms = stripslashes($extra_info['terms']);
  7233. if (empty ($item_title)) {
  7234. $path_parts = pathinfo($extra_info['path']);
  7235. $item_title = stripslashes($path_parts['filename']);
  7236. }
  7237. } elseif (is_numeric($extra_info)) {
  7238. $sql = "SELECT path, title FROM $tbl_doc
  7239. WHERE
  7240. c_id = ".$course_id." AND
  7241. id = " . intval($extra_info);
  7242. $result = Database::query($sql);
  7243. $row = Database::fetch_array($result);
  7244. $item_title = $row['title'];
  7245. $item_title = str_replace('_', ' ', $item_title);
  7246. if (empty ($item_title)) {
  7247. $path_parts = pathinfo($row['path']);
  7248. $item_title = stripslashes($path_parts['filename']);
  7249. }
  7250. } else {
  7251. $item_title = '';
  7252. $item_description = '';
  7253. }
  7254. $return = '<legend>';
  7255. if ($id != 0 && is_array($extra_info)) {
  7256. $parent = $extra_info['parent_item_id'];
  7257. } else {
  7258. $parent = 0;
  7259. }
  7260. $sql = "SELECT * FROM $tbl_lp_item
  7261. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7262. $result = Database::query($sql);
  7263. $arrLP = array();
  7264. while ($row = Database::fetch_array($result)) {
  7265. $arrLP[] = array(
  7266. 'id' => $row['id'],
  7267. 'item_type' => $row['item_type'],
  7268. 'title' => $row['title'],
  7269. 'path' => $row['path'],
  7270. 'description' => $row['description'],
  7271. 'parent_item_id' => $row['parent_item_id'],
  7272. 'previous_item_id' => $row['previous_item_id'],
  7273. 'next_item_id' => $row['next_item_id'],
  7274. 'display_order' => $row['display_order'],
  7275. 'max_score' => $row['max_score'],
  7276. 'min_score' => $row['min_score'],
  7277. 'mastery_score' => $row['mastery_score'],
  7278. 'prerequisite' => $row['prerequisite']
  7279. );
  7280. }
  7281. $this->tree_array($arrLP);
  7282. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7283. unset($this->arrMenu);
  7284. if ($action == 'add') {
  7285. $return .= get_lang('CreateTheDocument');
  7286. } elseif ($action == 'move') {
  7287. $return .= get_lang('MoveTheCurrentDocument');
  7288. } else {
  7289. $return .= get_lang('EditTheCurrentDocument');
  7290. }
  7291. $return .= '</legend>';
  7292. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7293. $return .= Display::return_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7294. }
  7295. $form = new FormValidator(
  7296. 'form',
  7297. 'POST',
  7298. $this->getCurrentBuildingModeURL(),
  7299. '',
  7300. array('enctype' => 'multipart/form-data')
  7301. );
  7302. $defaults['title'] = Security::remove_XSS($item_title);
  7303. if (empty($item_title)) {
  7304. $defaults['title'] = Security::remove_XSS($item_title);
  7305. }
  7306. $defaults['description'] = $item_description;
  7307. $form->addElement('html', $return);
  7308. if ($action != 'move') {
  7309. $data = $this->generate_lp_folder($_course);
  7310. if ($action != 'edit') {
  7311. $folders = DocumentManager::get_all_document_folders(
  7312. $_course,
  7313. 0,
  7314. true
  7315. );
  7316. DocumentManager::build_directory_selector(
  7317. $folders,
  7318. '',
  7319. array(),
  7320. true,
  7321. $form,
  7322. 'directory_parent_id'
  7323. );
  7324. }
  7325. if (isset($data['id'])) {
  7326. $defaults['directory_parent_id'] = $data['id'];
  7327. }
  7328. $form->addElement(
  7329. 'text',
  7330. 'title',
  7331. get_lang('Title'),
  7332. array('id' => 'idTitle', 'class' => 'col-md-4')
  7333. );
  7334. $form->applyFilter('title', 'html_filter');
  7335. }
  7336. $arrHide[0]['value'] = $this->name;
  7337. $arrHide[0]['padding'] = 20;
  7338. for ($i = 0; $i < count($arrLP); $i++) {
  7339. if ($action != 'add') {
  7340. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7341. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7342. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7343. if ($parent == $arrLP[$i]['id']) {
  7344. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7345. }
  7346. }
  7347. } else {
  7348. if ($arrLP[$i]['item_type'] == 'dir') {
  7349. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7350. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7351. if ($parent == $arrLP[$i]['id']) {
  7352. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7353. }
  7354. }
  7355. }
  7356. }
  7357. $parent_select = $form->addSelect(
  7358. 'parent',
  7359. get_lang('Parent'),
  7360. [],
  7361. [
  7362. 'id' => 'idParent',
  7363. 'onchange' => 'javascript: load_cbo(this.value);',
  7364. ]
  7365. );
  7366. $my_count = 0;
  7367. foreach ($arrHide as $key => $value) {
  7368. if ($my_count != 0) {
  7369. // The LP name is also the first section and is not in the same charset like the other sections.
  7370. $value['value'] = Security::remove_XSS($value['value']);
  7371. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7372. } else {
  7373. $value['value'] = Security::remove_XSS($value['value']);
  7374. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7375. }
  7376. $my_count++;
  7377. }
  7378. if (!empty($id)) {
  7379. $parent_select->setSelected($parent);
  7380. } else {
  7381. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7382. $parent_select->setSelected($parent_item_id);
  7383. }
  7384. if (is_array($arrLP)) {
  7385. reset($arrLP);
  7386. }
  7387. $arrHide = array();
  7388. $s_selected_position = null;
  7389. // POSITION
  7390. $lastPosition = null;
  7391. for ($i = 0; $i < count($arrLP); $i++) {
  7392. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7393. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7394. ) {
  7395. if ((isset($extra_info['previous_item_id']) &&
  7396. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  7397. ) {
  7398. $s_selected_position = $arrLP[$i]['id'];
  7399. }
  7400. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7401. }
  7402. $lastPosition = $arrLP[$i]['id'];
  7403. }
  7404. if (empty($s_selected_position)) {
  7405. $s_selected_position = $lastPosition;
  7406. }
  7407. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7408. $position->addOption(get_lang('FirstPosition'), 0);
  7409. foreach ($arrHide as $key => $value) {
  7410. $padding = isset($value['padding']) ? $value['padding']: 20;
  7411. $position->addOption(
  7412. $value['value'],
  7413. $key,
  7414. 'style="padding-left:'.$padding.'px;"'
  7415. );
  7416. }
  7417. $position->setSelected($s_selected_position);
  7418. if (is_array($arrLP)) {
  7419. reset($arrLP);
  7420. }
  7421. if ($action != 'move') {
  7422. $id_prerequisite = 0;
  7423. if (is_array($arrLP)) {
  7424. foreach ($arrLP as $key => $value) {
  7425. if ($value['id'] == $id) {
  7426. $id_prerequisite = $value['prerequisite'];
  7427. break;
  7428. }
  7429. }
  7430. }
  7431. $arrHide = array();
  7432. for ($i = 0; $i < count($arrLP); $i++) {
  7433. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7434. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7435. $s_selected_position = $arrLP[$i]['id'];
  7436. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7437. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7438. }
  7439. }
  7440. if (!$no_display_add) {
  7441. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7442. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7443. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true') {
  7444. if (isset ($_POST['content'])) {
  7445. $content = stripslashes($_POST['content']);
  7446. } elseif (is_array($extra_info)) {
  7447. //If it's an html document or a text file
  7448. if (!$no_display_edit_textarea) {
  7449. $content = $this->display_document($extra_info['path'], false, false);
  7450. }
  7451. } elseif (is_numeric($extra_info)) {
  7452. $content = $this->display_document(
  7453. $extra_info,
  7454. false,
  7455. false
  7456. );
  7457. } else {
  7458. $content = '';
  7459. }
  7460. if (!$no_display_edit_textarea) {
  7461. // We need to calculate here some specific settings for the online editor.
  7462. // The calculated settings work for documents in the Documents tool
  7463. // (on the root or in subfolders).
  7464. // For documents in native scorm packages it is unclear whether the
  7465. // online editor should be activated or not.
  7466. // A new document, it is in the root of the repository.
  7467. $relative_path = '';
  7468. $relative_prefix = '';
  7469. if (is_array($extra_info) && $extra_info != 'new') {
  7470. // The document already exists. Whe have to determine its relative path towards the repository root.
  7471. $relative_path = explode('/', $extra_info['dir']);
  7472. $cnt = count($relative_path) - 2;
  7473. if ($cnt < 0) {
  7474. $cnt = 0;
  7475. }
  7476. $relative_prefix = str_repeat('../', $cnt);
  7477. $relative_path = array_slice($relative_path, 1, $cnt);
  7478. $relative_path = implode('/', $relative_path);
  7479. if (strlen($relative_path) > 0) {
  7480. $relative_path = $relative_path . '/';
  7481. }
  7482. } else {
  7483. $result = $this->generate_lp_folder($_course);
  7484. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7485. $relative_prefix = '../../';
  7486. }
  7487. $editor_config = array(
  7488. 'ToolbarSet' => 'LearningPathDocuments',
  7489. 'Width' => '100%',
  7490. 'Height' => '500',
  7491. 'FullPage' => true,
  7492. 'CreateDocumentDir' => $relative_prefix,
  7493. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7494. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7495. );
  7496. if ($_GET['action'] == 'add_item') {
  7497. $class = 'add';
  7498. $text = get_lang('LPCreateDocument');
  7499. } else {
  7500. if ($_GET['action'] == 'edit_item') {
  7501. $class = 'save';
  7502. $text = get_lang('SaveDocument');
  7503. }
  7504. }
  7505. $form->addButtonSave($text, 'submit_button');
  7506. $renderer = $form->defaultRenderer();
  7507. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7508. $form->addElement('html', '<div class="editor-lp">');
  7509. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7510. $form->addElement('html', '</div>');
  7511. $defaults['content_lp'] = $content;
  7512. }
  7513. } elseif (is_numeric($extra_info)) {
  7514. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7515. $return = $this->display_document($extra_info, true, true, true);
  7516. $form->addElement('html', $return);
  7517. }
  7518. }
  7519. }
  7520. if (isset($extra_info['item_type'] ) &&
  7521. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7522. ) {
  7523. $parent_select->freeze();
  7524. $position->freeze();
  7525. }
  7526. if ($action == 'move') {
  7527. $form->addElement('hidden', 'title', $item_title);
  7528. $form->addElement('hidden', 'description', $item_description);
  7529. }
  7530. if (is_numeric($extra_info)) {
  7531. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7532. $form->addElement('hidden', 'path', $extra_info);
  7533. } elseif (is_array($extra_info)) {
  7534. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7535. $form->addElement('hidden', 'path', $extra_info['path']);
  7536. }
  7537. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7538. $form->addElement('hidden', 'post_time', time());
  7539. $form->setDefaults($defaults);
  7540. return $form->returnForm();
  7541. }
  7542. /**
  7543. * Return HTML form to add/edit a link item
  7544. * @param string $action (add/edit)
  7545. * @param integer $id Item ID if exists
  7546. * @param mixed $extra_info
  7547. * @return string HTML form
  7548. */
  7549. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7550. {
  7551. $course_id = api_get_course_int_id();
  7552. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7553. $tbl_link = Database::get_course_table(TABLE_LINK);
  7554. if ($id != 0 && is_array($extra_info)) {
  7555. $item_title = stripslashes($extra_info['title']);
  7556. $item_description = stripslashes($extra_info['description']);
  7557. $item_url = stripslashes($extra_info['url']);
  7558. } elseif (is_numeric($extra_info)) {
  7559. $extra_info = intval($extra_info);
  7560. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7561. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7562. $result = Database::query($sql);
  7563. $row = Database::fetch_array($result);
  7564. $item_title = $row['title'];
  7565. $item_description = $row['description'];
  7566. $item_url = $row['url'];
  7567. } else {
  7568. $item_title = '';
  7569. $item_description = '';
  7570. $item_url = '';
  7571. }
  7572. $form = new FormValidator(
  7573. 'edit_link',
  7574. 'POST',
  7575. $this->getCurrentBuildingModeURL()
  7576. );
  7577. $defaults = [];
  7578. if ($id != 0 && is_array($extra_info)) {
  7579. $parent = $extra_info['parent_item_id'];
  7580. } else {
  7581. $parent = 0;
  7582. }
  7583. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7584. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7585. $result = Database::query($sql);
  7586. $arrLP = array();
  7587. while ($row = Database::fetch_array($result)) {
  7588. $arrLP[] = array(
  7589. 'id' => $row['id'],
  7590. 'item_type' => $row['item_type'],
  7591. 'title' => $row['title'],
  7592. 'path' => $row['path'],
  7593. 'description' => $row['description'],
  7594. 'parent_item_id' => $row['parent_item_id'],
  7595. 'previous_item_id' => $row['previous_item_id'],
  7596. 'next_item_id' => $row['next_item_id'],
  7597. 'display_order' => $row['display_order'],
  7598. 'max_score' => $row['max_score'],
  7599. 'min_score' => $row['min_score'],
  7600. 'mastery_score' => $row['mastery_score'],
  7601. 'prerequisite' => $row['prerequisite']
  7602. );
  7603. }
  7604. $this->tree_array($arrLP);
  7605. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7606. unset($this->arrMenu);
  7607. if ($action == 'add') {
  7608. $legend = get_lang('CreateTheLink');
  7609. } elseif ($action == 'move') {
  7610. $legend = get_lang('MoveCurrentLink');
  7611. } else {
  7612. $legend = get_lang('EditCurrentLink');
  7613. }
  7614. $form->addHeader($legend);
  7615. if ($action != 'move') {
  7616. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7617. $defaults['title'] = $item_title;
  7618. }
  7619. $selectParent = $form->addSelect(
  7620. 'parent',
  7621. get_lang('Parent'),
  7622. [],
  7623. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7624. );
  7625. $selectParent->addOption($this->name, 0);
  7626. $arrHide = array(
  7627. $id
  7628. );
  7629. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7630. for ($i = 0; $i < count($arrLP); $i++) {
  7631. if ($action != 'add') {
  7632. if (
  7633. ($arrLP[$i]['item_type'] == 'dir') &&
  7634. !in_array($arrLP[$i]['id'], $arrHide) &&
  7635. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7636. ) {
  7637. $selectParent->addOption(
  7638. $arrLP[$i]['title'],
  7639. $arrLP[$i]['id'],
  7640. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7641. );
  7642. if ($parent == $arrLP[$i]['id']) {
  7643. $selectParent->setSelected($arrLP[$i]['id']);
  7644. }
  7645. } else {
  7646. $arrHide[] = $arrLP[$i]['id'];
  7647. }
  7648. } else {
  7649. if ($arrLP[$i]['item_type'] == 'dir') {
  7650. $selectParent->addOption(
  7651. $arrLP[$i]['title'],
  7652. $arrLP[$i]['id'],
  7653. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7654. );
  7655. if ($parent_item_id == $arrLP[$i]['id']) {
  7656. $selectParent->setSelected($arrLP[$i]['id']);
  7657. }
  7658. }
  7659. }
  7660. }
  7661. if (is_array($arrLP)) {
  7662. reset($arrLP);
  7663. }
  7664. $selectPrevious = $form->addSelect(
  7665. 'previous',
  7666. get_lang('Position'),
  7667. [],
  7668. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7669. );
  7670. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7671. for ($i = 0; $i < count($arrLP); $i++) {
  7672. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7673. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7674. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7675. $selectPrevious->setSelected($arrLP[$i]['id']);
  7676. } elseif ($action == 'add') {
  7677. $selectPrevious->setSelected($arrLP[$i]['id']);
  7678. }
  7679. }
  7680. }
  7681. if ($action != 'move') {
  7682. $urlAttributes = ['class' => 'learnpath_item_form'];
  7683. if (is_numeric($extra_info)) {
  7684. $urlAttributes['disabled'] = 'disabled';
  7685. }
  7686. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7687. $defaults['url'] = $item_url;
  7688. $id_prerequisite = 0;
  7689. if (is_array($arrLP)) {
  7690. foreach ($arrLP as $key => $value) {
  7691. if ($value['id'] == $id) {
  7692. $id_prerequisite = $value['prerequisite'];
  7693. break;
  7694. }
  7695. }
  7696. }
  7697. $arrHide = array();
  7698. for ($i = 0; $i < count($arrLP); $i++) {
  7699. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7700. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7701. $s_selected_position = $arrLP[$i]['id'];
  7702. elseif ($action == 'add') $s_selected_position = 0;
  7703. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7704. }
  7705. }
  7706. }
  7707. if ($action == 'add') {
  7708. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7709. } else {
  7710. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7711. }
  7712. if ($action == 'move') {
  7713. $form->addHidden('title', $item_title);
  7714. $form->addHidden('description', $item_description);
  7715. }
  7716. if (is_numeric($extra_info)) {
  7717. $form->addHidden('path', $extra_info);
  7718. } elseif (is_array($extra_info)) {
  7719. $form->addHidden('path', $extra_info['path']);
  7720. }
  7721. $form->addHidden('type', TOOL_LINK);
  7722. $form->addHidden('post_time', time());
  7723. $form->setDefaults($defaults);
  7724. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7725. }
  7726. /**
  7727. * Return HTML form to add/edit a student publication (work)
  7728. * @param string Action (add/edit)
  7729. * @param integer Item ID if already exists
  7730. * @param mixed Extra info (work ID if integer)
  7731. * @return string HTML form
  7732. */
  7733. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7734. {
  7735. $course_id = api_get_course_int_id();
  7736. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7737. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  7738. if ($id != 0 && is_array($extra_info)) {
  7739. $item_title = stripslashes($extra_info['title']);
  7740. $item_description = stripslashes($extra_info['description']);
  7741. } elseif (is_numeric($extra_info)) {
  7742. $extra_info = intval($extra_info);
  7743. $sql = "SELECT title, description
  7744. FROM $tbl_publication
  7745. WHERE c_id = $course_id AND id = " . $extra_info;
  7746. $result = Database::query($sql);
  7747. $row = Database::fetch_array($result);
  7748. $item_title = $row['title'];
  7749. } else {
  7750. $item_title = get_lang('Student_publication');
  7751. }
  7752. if ($id != 0 && is_array($extra_info)) {
  7753. $parent = $extra_info['parent_item_id'];
  7754. } else {
  7755. $parent = 0;
  7756. }
  7757. $sql = "SELECT * FROM $tbl_lp_item
  7758. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7759. $result = Database::query($sql);
  7760. $arrLP = array();
  7761. while ($row = Database::fetch_array($result)) {
  7762. $arrLP[] = array(
  7763. 'id' => $row['id'],
  7764. 'item_type' => $row['item_type'],
  7765. 'title' => $row['title'],
  7766. 'path' => $row['path'],
  7767. 'description' => $row['description'],
  7768. 'parent_item_id' => $row['parent_item_id'],
  7769. 'previous_item_id' => $row['previous_item_id'],
  7770. 'next_item_id' => $row['next_item_id'],
  7771. 'display_order' => $row['display_order'],
  7772. 'max_score' => $row['max_score'],
  7773. 'min_score' => $row['min_score'],
  7774. 'mastery_score' => $row['mastery_score'],
  7775. 'prerequisite' => $row['prerequisite']
  7776. );
  7777. }
  7778. $this->tree_array($arrLP);
  7779. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7780. unset($this->arrMenu);
  7781. $form = new FormValidator('frm_student_publication', 'post', '#');
  7782. if ($action == 'add') {
  7783. $form->addHeader(get_lang('Student_publication'));
  7784. } elseif ($action == 'move') {
  7785. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7786. } else {
  7787. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7788. }
  7789. if ($action != 'move') {
  7790. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7791. }
  7792. $parentSelect = $form->addSelect(
  7793. 'parent',
  7794. get_lang('Parent'),
  7795. ['0' => $this->name],
  7796. [
  7797. 'onchange' => 'javascript: load_cbo(this.value);',
  7798. 'class' => 'learnpath_item_form',
  7799. 'id' => 'idParent'
  7800. ]
  7801. );
  7802. $arrHide = array($id);
  7803. for ($i = 0; $i < count($arrLP); $i++) {
  7804. if ($action != 'add') {
  7805. if (
  7806. ($arrLP[$i]['item_type'] == 'dir') &&
  7807. !in_array($arrLP[$i]['id'], $arrHide) &&
  7808. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7809. ) {
  7810. $parentSelect->addOption(
  7811. $arrLP[$i]['title'],
  7812. $arrLP[$i]['id'],
  7813. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7814. );
  7815. if ($parent == $arrLP[$i]['id']) {
  7816. $parentSelect->setSelected($arrLP[$i]['id']);
  7817. }
  7818. } else {
  7819. $arrHide[] = $arrLP[$i]['id'];
  7820. }
  7821. } else {
  7822. if ($arrLP[$i]['item_type'] == 'dir') {
  7823. $parentSelect->addOption(
  7824. $arrLP[$i]['title'],
  7825. $arrLP[$i]['id'],
  7826. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7827. );
  7828. if ($parent == $arrLP[$i]['id']) {
  7829. $parentSelect->setSelected($arrLP[$i]['id']);
  7830. }
  7831. }
  7832. }
  7833. }
  7834. if (is_array($arrLP)) {
  7835. reset($arrLP);
  7836. }
  7837. $previousSelect = $form->addSelect(
  7838. 'previous',
  7839. get_lang('Position'),
  7840. ['0' => get_lang('FirstPosition')],
  7841. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7842. );
  7843. for ($i = 0; $i < count($arrLP); $i++) {
  7844. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7845. $previousSelect->addOption(
  7846. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7847. $arrLP[$i]['id']
  7848. );
  7849. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7850. $previousSelect->setSelected($arrLP[$i]['id']);
  7851. } elseif ($action == 'add') {
  7852. $previousSelect->setSelected($arrLP[$i]['id']);
  7853. }
  7854. }
  7855. }
  7856. if ($action != 'move') {
  7857. $id_prerequisite = 0;
  7858. if (is_array($arrLP)) {
  7859. foreach ($arrLP as $key => $value) {
  7860. if ($value['id'] == $id) {
  7861. $id_prerequisite = $value['prerequisite'];
  7862. break;
  7863. }
  7864. }
  7865. }
  7866. $arrHide = array ();
  7867. for ($i = 0; $i < count($arrLP); $i++) {
  7868. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7869. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7870. $s_selected_position = $arrLP[$i]['id'];
  7871. elseif ($action == 'add') $s_selected_position = 0;
  7872. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7873. }
  7874. }
  7875. }
  7876. if ($action == 'add') {
  7877. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7878. } else {
  7879. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7880. }
  7881. if ($action == 'move') {
  7882. $form->addHidden('title', $item_title);
  7883. $form->addHidden('description', $item_description);
  7884. }
  7885. if (is_numeric($extra_info)) {
  7886. $form->addHidden('path', $extra_info);
  7887. } elseif (is_array($extra_info)) {
  7888. $form->addHidden('path', $extra_info['path']);
  7889. }
  7890. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7891. $form->addHidden('post_time', time());
  7892. $form->setDefaults(['title' => $item_title]);
  7893. $return = '<div class="sectioncomment">';
  7894. $return .= $form->returnForm();
  7895. $return .= '</div>';
  7896. return $return;
  7897. }
  7898. /**
  7899. * Displays the menu for manipulating a step
  7900. *
  7901. * @param $item_id
  7902. * @param string $item_type
  7903. * @return string
  7904. */
  7905. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7906. {
  7907. $_course = api_get_course_info();
  7908. $course_id = api_get_course_int_id();
  7909. $course_code = api_get_course_id();
  7910. $return = '<div class="actions">';
  7911. switch ($item_type) {
  7912. case 'dir':
  7913. // Commented the message cause should not show it.
  7914. //$lang = get_lang('TitleManipulateChapter');
  7915. break;
  7916. case TOOL_LP_FINAL_ITEM:
  7917. case TOOL_DOCUMENT:
  7918. // Commented the message cause should not show it.
  7919. //$lang = get_lang('TitleManipulateDocument');
  7920. break;
  7921. case TOOL_LINK:
  7922. case 'link' :
  7923. // Commented the message cause should not show it.
  7924. //$lang = get_lang('TitleManipulateLink');
  7925. break;
  7926. case TOOL_QUIZ:
  7927. // Commented the message cause should not show it.
  7928. //$lang = get_lang('TitleManipulateQuiz');
  7929. break;
  7930. case TOOL_STUDENTPUBLICATION:
  7931. // Commented the message cause should not show it.
  7932. //$lang = get_lang('TitleManipulateStudentPublication');
  7933. break;
  7934. }
  7935. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7936. $item_id = intval($item_id);
  7937. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7938. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7939. $result = Database::query($sql);
  7940. $row = Database::fetch_assoc($result);
  7941. $audio_player = null;
  7942. // We display an audio player if needed.
  7943. if (!empty($row['audio'])) {
  7944. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7945. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7946. </div>';
  7947. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7948. $audio_player .= '<script>
  7949. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7950. s1.addParam("allowscriptaccess","always");
  7951. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7952. s1.write("container");
  7953. </script>';
  7954. }
  7955. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7956. if ($item_type != TOOL_LP_FINAL_ITEM) {
  7957. $return .= Display::url(
  7958. Display::return_icon(
  7959. 'edit.png',
  7960. get_lang('Edit'),
  7961. array(),
  7962. ICON_SIZE_SMALL
  7963. ),
  7964. $url.'&action=edit_item&path_item='.$row['path']
  7965. );
  7966. $return .= Display::url(
  7967. Display::return_icon(
  7968. 'move.png',
  7969. get_lang('Move'),
  7970. array(),
  7971. ICON_SIZE_SMALL
  7972. ),
  7973. $url.'&action=move_item'
  7974. );
  7975. }
  7976. // Commented for now as prerequisites cannot be added to chapters.
  7977. if ($item_type != 'dir') {
  7978. $return .= Display::url(
  7979. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7980. $url.'&action=edit_item_prereq'
  7981. );
  7982. }
  7983. $return .= Display::url(
  7984. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7985. $url.'&action=delete_item'
  7986. );
  7987. if ($item_type == TOOL_HOTPOTATOES) {
  7988. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7989. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7990. }
  7991. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7992. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7993. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7994. }
  7995. $return .= '</div>';
  7996. if (!empty($audio_player)) {
  7997. $return .= '<br />'.$audio_player;
  7998. }
  7999. return $return;
  8000. }
  8001. /**
  8002. * Creates the javascript needed for filling up the checkboxes without page reload
  8003. * @return string
  8004. */
  8005. public function get_js_dropdown_array()
  8006. {
  8007. $course_id = api_get_course_int_id();
  8008. $return = 'var child_name = new Array();' . "\n";
  8009. $return .= 'var child_value = new Array();' . "\n\n";
  8010. $return .= 'child_name[0] = new Array();' . "\n";
  8011. $return .= 'child_value[0] = new Array();' . "\n\n";
  8012. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8013. $sql = "SELECT * FROM " . $tbl_lp_item . "
  8014. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  8015. ORDER BY display_order ASC";
  8016. $res_zero = Database::query($sql);
  8017. $i = 0;
  8018. while ($row_zero = Database::fetch_array($res_zero)) {
  8019. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  8020. if ($row_zero['item_type'] == TOOL_QUIZ) {
  8021. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  8022. }
  8023. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  8024. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  8025. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  8026. }
  8027. }
  8028. $return .= "\n";
  8029. $sql = "SELECT * FROM " . $tbl_lp_item . "
  8030. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  8031. $res = Database::query($sql);
  8032. while ($row = Database::fetch_array($res)) {
  8033. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  8034. WHERE
  8035. c_id = ".$course_id." AND
  8036. parent_item_id = " . $row['id'] . "
  8037. ORDER BY display_order ASC";
  8038. $res_parent = Database::query($sql_parent);
  8039. $i = 0;
  8040. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  8041. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  8042. while ($row_parent = Database::fetch_array($res_parent)) {
  8043. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  8044. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  8045. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  8046. }
  8047. $return .= "\n";
  8048. }
  8049. return $return;
  8050. }
  8051. /**
  8052. * Display the form to allow moving an item
  8053. * @param integer Item ID
  8054. * @return string HTML form
  8055. */
  8056. public function display_move_item($item_id)
  8057. {
  8058. $course_id = api_get_course_int_id();
  8059. $return = '';
  8060. if (is_numeric($item_id)) {
  8061. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8062. $sql = "SELECT * FROM " . $tbl_lp_item . "
  8063. WHERE c_id = ".$course_id." AND id = " . $item_id;
  8064. $res = Database::query($sql);
  8065. $row = Database::fetch_array($res);
  8066. switch ($row['item_type']) {
  8067. case 'dir':
  8068. case 'asset':
  8069. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8070. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  8071. break;
  8072. case TOOL_DOCUMENT:
  8073. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8074. $return .= $this->display_document_form('move', $item_id, $row);
  8075. break;
  8076. case TOOL_LINK:
  8077. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8078. $return .= $this->display_link_form('move', $item_id, $row);
  8079. break;
  8080. case TOOL_HOTPOTATOES:
  8081. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8082. $return .= $this->display_link_form('move', $item_id, $row);
  8083. break;
  8084. case TOOL_QUIZ:
  8085. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8086. $return .= $this->display_quiz_form('move', $item_id, $row);
  8087. break;
  8088. case TOOL_STUDENTPUBLICATION:
  8089. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8090. $return .= $this->display_student_publication_form('move', $item_id, $row);
  8091. break;
  8092. case TOOL_FORUM:
  8093. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8094. $return .= $this->display_forum_form('move', $item_id, $row);
  8095. break;
  8096. case TOOL_THREAD:
  8097. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8098. $return .= $this->display_forum_form('move', $item_id, $row);
  8099. break;
  8100. }
  8101. }
  8102. return $return;
  8103. }
  8104. /**
  8105. * Displays a basic form on the overview page for changing the item title and the item description.
  8106. * @param string $item_type
  8107. * @param string $title
  8108. * @param array $data
  8109. * @return string
  8110. */
  8111. public function display_item_small_form($item_type, $title = '', $data = array())
  8112. {
  8113. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  8114. $form = new FormValidator('small_form', 'post', $url);
  8115. $form->addElement('header', $title);
  8116. $form->addElement('text', 'title', get_lang('Title'));
  8117. $form->addButtonSave(get_lang('Save'), 'submit_button');
  8118. $form->addElement('hidden', 'id', $data['id']);
  8119. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  8120. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  8121. $form->setDefaults(array('title' => $data['title']));
  8122. return $form->toHtml();
  8123. }
  8124. /**
  8125. * Return HTML form to allow prerequisites selection
  8126. * @todo use FormValidator
  8127. * @param integer Item ID
  8128. * @return string HTML form
  8129. */
  8130. public function display_item_prerequisites_form($item_id)
  8131. {
  8132. $course_id = api_get_course_int_id();
  8133. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8134. $item_id = intval($item_id);
  8135. /* Current prerequisite */
  8136. $sql = "SELECT * FROM $tbl_lp_item
  8137. WHERE c_id = $course_id AND id = " . $item_id;
  8138. $result = Database::query($sql);
  8139. $row = Database::fetch_array($result);
  8140. $prerequisiteId = $row['prerequisite'];
  8141. $return = '<legend>';
  8142. $return .= get_lang('AddEditPrerequisites');
  8143. $return .= '</legend>';
  8144. $return .= '<form method="POST">';
  8145. $return .= '<div class="table-responsive">';
  8146. $return .= '<table class="table table-hover">';
  8147. $return .= '<tr>';
  8148. $return .= '<th>' . get_lang('LearnpathPrerequisites') . '</th>';
  8149. $return .= '<th width="140" >' . get_lang('Minimum') . '</th>';
  8150. $return .= '<th width="140">' . get_lang('Maximum') . '</th>';
  8151. $return .= '</tr>';
  8152. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8153. $return .= '<tr>';
  8154. $return .= '<td colspan="3">';
  8155. $return .= '<div class="radio learnpath"><label for="idNone">';
  8156. $return .= '<input checked="checked" id="idNone" name="prerequisites" type="radio" />';
  8157. $return .= get_lang('None') . '</label>';
  8158. $return .= '</div>';
  8159. $return .= '</tr>';
  8160. $sql = "SELECT * FROM $tbl_lp_item
  8161. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  8162. $result = Database::query($sql);
  8163. $arrLP = array();
  8164. $selectedMinScore = array();
  8165. $selectedMaxScore = array();
  8166. while ($row = Database::fetch_array($result)) {
  8167. if ($row['id'] == $item_id) {
  8168. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8169. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8170. }
  8171. $arrLP[] = array(
  8172. 'id' => $row['id'],
  8173. 'item_type' => $row['item_type'],
  8174. 'title' => $row['title'],
  8175. 'ref' => $row['ref'],
  8176. 'description' => $row['description'],
  8177. 'parent_item_id' => $row['parent_item_id'],
  8178. 'previous_item_id' => $row['previous_item_id'],
  8179. 'next_item_id' => $row['next_item_id'],
  8180. 'max_score' => $row['max_score'],
  8181. 'min_score' => $row['min_score'],
  8182. 'mastery_score' => $row['mastery_score'],
  8183. 'prerequisite' => $row['prerequisite'],
  8184. 'next_item_id' => $row['next_item_id'],
  8185. 'display_order' => $row['display_order'],
  8186. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  8187. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  8188. );
  8189. }
  8190. $this->tree_array($arrLP);
  8191. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  8192. unset($this->arrMenu);
  8193. for ($i = 0; $i < count($arrLP); $i++) {
  8194. $item = $arrLP[$i];
  8195. if ($item['id'] == $item_id) {
  8196. break;
  8197. }
  8198. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8199. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  8200. $return .= '<tr>';
  8201. $return .= '<td ' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  8202. $return .= '<div style="margin-left:' . $item['depth'] * 20 . 'px;" class="radio learnpath">';
  8203. $return .= '<label for="id' . $item['id'] . '">';
  8204. $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'] . '" />';
  8205. $icon_name = str_replace(' ', '', $item['item_type']);
  8206. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  8207. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  8208. } else {
  8209. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  8210. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  8211. } else {
  8212. $return .= Display::return_icon('folder_document.png');
  8213. }
  8214. }
  8215. $return .= $item['title'] . '</label>';
  8216. $return .= '</div>';
  8217. $return .= '</td>';
  8218. if ($item['item_type'] == TOOL_QUIZ) {
  8219. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8220. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  8221. $tmp_obj_exercice = new Exercise();
  8222. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  8223. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  8224. $tmp_obj_lp_item->update_in_bdd();
  8225. $item['max_score'] = $tmp_obj_lp_item->max_score;
  8226. $return .= '<td>';
  8227. $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. '" />';
  8228. $return .= '</td>';
  8229. $return .= '<td>';
  8230. $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 . '" />';
  8231. $return .= '</td>';
  8232. }
  8233. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8234. $return .= '<td>';
  8235. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  8236. $return .= '</td>';
  8237. $return .= '<td>';
  8238. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  8239. $return .= '</td>';
  8240. }
  8241. $return .= '</tr>';
  8242. }
  8243. $return .= '<tr>';
  8244. $return .= '</tr>';
  8245. $return .= '</table>';
  8246. $return .= '</div>';
  8247. $return .= '<div class="form-group">';
  8248. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  8249. $return .= '</form>';
  8250. return $return;
  8251. }
  8252. /**
  8253. * Return HTML list to allow prerequisites selection for lp
  8254. * @param integer Item ID
  8255. * @return string HTML form
  8256. */
  8257. public function display_lp_prerequisites_list()
  8258. {
  8259. $course_id = api_get_course_int_id();
  8260. $lp_id = $this->lp_id;
  8261. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8262. // get current prerequisite
  8263. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8264. $result = Database::query($sql);
  8265. $row = Database::fetch_array($result);
  8266. $prerequisiteId = $row['prerequisite'];
  8267. $session_id = api_get_session_id();
  8268. $session_condition = api_get_session_condition($session_id, true, true);
  8269. $sql = "SELECT * FROM $tbl_lp
  8270. WHERE c_id = $course_id $session_condition
  8271. ORDER BY display_order ";
  8272. $rs = Database::query($sql);
  8273. $return = '';
  8274. $return .= '<select name="prerequisites" class="form-control">';
  8275. $return .= '<option value="0">'.get_lang('None').'</option>';
  8276. if (Database::num_rows($rs) > 0) {
  8277. while ($row = Database::fetch_array($rs)) {
  8278. if ($row['id'] == $lp_id) {
  8279. continue;
  8280. }
  8281. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  8282. }
  8283. }
  8284. $return .= '</select>';
  8285. return $return;
  8286. }
  8287. /**
  8288. * Creates a list with all the documents in it
  8289. * @param bool $showInvisibleFiles
  8290. * @return string
  8291. */
  8292. public function get_documents($showInvisibleFiles = false)
  8293. {
  8294. $course_info = api_get_course_info();
  8295. $sessionId = api_get_session_id();
  8296. $documentTree = DocumentManager::get_document_preview(
  8297. $course_info,
  8298. $this->lp_id,
  8299. null,
  8300. $sessionId,
  8301. true,
  8302. null,
  8303. null,
  8304. $showInvisibleFiles,
  8305. true
  8306. );
  8307. $headers = array(
  8308. get_lang('Files'),
  8309. get_lang('CreateTheDocument'),
  8310. get_lang('Upload')
  8311. );
  8312. $form = new FormValidator(
  8313. 'form_upload',
  8314. 'POST',
  8315. $this->getCurrentBuildingModeURL(),
  8316. '',
  8317. array('enctype' => 'multipart/form-data'),
  8318. FormValidator::LAYOUT_INLINE
  8319. );
  8320. $folders = DocumentManager::get_all_document_folders(
  8321. api_get_course_info(),
  8322. 0,
  8323. true
  8324. );
  8325. DocumentManager::build_directory_selector(
  8326. $folders,
  8327. '',
  8328. array(),
  8329. true,
  8330. $form,
  8331. 'directory_parent_id'
  8332. );
  8333. $form->addHtml('<hr>');
  8334. $group = array(
  8335. $form->createElement(
  8336. 'radio',
  8337. 'if_exists',
  8338. get_lang("UplWhatIfFileExists"),
  8339. get_lang('Yes'),
  8340. 'nothing'
  8341. ),
  8342. $form->createElement(
  8343. 'radio',
  8344. 'if_exists',
  8345. null,
  8346. get_lang('UplOverwriteLong'),
  8347. 'overwrite'
  8348. ),
  8349. $form->createElement(
  8350. 'radio',
  8351. 'if_exists',
  8352. null,
  8353. get_lang('UplRenameLong'),
  8354. 'rename'
  8355. )
  8356. );
  8357. $form->addGroup($group, null, get_lang('UplWhatIfFileExists'));
  8358. $form->addHtml('<br />');
  8359. /*$form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8360. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8361. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');*/
  8362. $form->setDefaults(['if_exists' => 'rename']);
  8363. // Check box options
  8364. $form->addElement(
  8365. 'checkbox',
  8366. 'unzip',
  8367. get_lang('Options'),
  8368. get_lang('Uncompress')
  8369. );
  8370. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8371. $form->addMultipleUpload($url);
  8372. $new = $this->display_document_form('add', 0);
  8373. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8374. return $tabs;
  8375. }
  8376. /**
  8377. * Creates a list with all the exercises (quiz) in it
  8378. * @return string
  8379. */
  8380. public function get_exercises()
  8381. {
  8382. $course_id = api_get_course_int_id();
  8383. $session_id = api_get_session_id();
  8384. $userInfo = api_get_user_info();
  8385. // New for hotpotatoes.
  8386. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8387. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  8388. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  8389. $condition_session = api_get_session_condition($session_id, true, true);
  8390. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8391. $activeCondition = ' active <> -1 ';
  8392. if ($setting) {
  8393. $activeCondition = ' active = 1 ';
  8394. }
  8395. $sql_quiz = "SELECT * FROM $tbl_quiz
  8396. WHERE c_id = $course_id AND $activeCondition $condition_session
  8397. ORDER BY title ASC";
  8398. $sql_hot = "SELECT * FROM $tbl_doc
  8399. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8400. ORDER BY id ASC";
  8401. $res_quiz = Database::query($sql_quiz);
  8402. $res_hot = Database::query($sql_hot);
  8403. $return = '<ul class="lp_resource">';
  8404. $return .= '<li class="lp_resource_element">';
  8405. $return .= Display::return_icon('new_exercice.png');
  8406. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8407. get_lang('NewExercise') . '</a>';
  8408. $return .= '</li>';
  8409. $previewIcon = Display::return_icon('preview_view.png', get_lang('Preview'));
  8410. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  8411. // Display hotpotatoes
  8412. while ($row_hot = Database::fetch_array($res_hot)) {
  8413. $link = Display::url(
  8414. $previewIcon,
  8415. $exerciseUrl.'&file='.$row_hot['path'],
  8416. ['target' => '_blank']
  8417. );
  8418. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8419. $return .= '<a class="moved" href="#">';
  8420. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8421. $return .= '</a> ';
  8422. $return .= Display::return_icon('hotpotatoes_s.png');
  8423. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8424. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])) .$link. '</a>';
  8425. $return .= '</li>';
  8426. }
  8427. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  8428. while ($row_quiz = Database::fetch_array($res_quiz)) {
  8429. $title = strip_tags(
  8430. api_html_entity_decode($row_quiz['title'])
  8431. );
  8432. $link = Display::url(
  8433. $previewIcon,
  8434. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  8435. ['target' => '_blank']
  8436. );
  8437. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$title.'" >';
  8438. $return .= '<a class="moved" href="#">';
  8439. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8440. $return .= '</a> ';
  8441. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8442. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  8443. $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 . '">' .
  8444. Security::remove_XSS(cut($title, 80)).$link.$sessionStar.
  8445. '</a>';
  8446. $return .= '</li>';
  8447. }
  8448. $return .= '</ul>';
  8449. return $return;
  8450. }
  8451. /**
  8452. * Creates a list with all the links in it
  8453. * @return string
  8454. */
  8455. public function get_links()
  8456. {
  8457. $selfUrl = api_get_self();
  8458. $courseIdReq = api_get_cidreq();
  8459. $course = api_get_course_info();
  8460. $userInfo = api_get_user_info();
  8461. $course_id = $course['real_id'];
  8462. $tbl_link = Database::get_course_table(TABLE_LINK);
  8463. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8464. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8465. $session_id = api_get_session_id();
  8466. $condition_session = api_get_session_condition(
  8467. $session_id,
  8468. true,
  8469. true,
  8470. "link.session_id"
  8471. );
  8472. $sql = "SELECT
  8473. link.id as link_id,
  8474. link.title as link_title,
  8475. link.session_id as link_session_id,
  8476. link.category_id as category_id,
  8477. link_category.category_title as category_title
  8478. FROM $tbl_link as link
  8479. LEFT JOIN $linkCategoryTable as link_category
  8480. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8481. WHERE link.c_id = ".$course_id." $condition_session
  8482. ORDER BY link_category.category_title ASC, link.title ASC";
  8483. $result = Database::query($sql);
  8484. $categorizedLinks = array();
  8485. $categories = array();
  8486. while ($link = Database::fetch_array($result)) {
  8487. if (!$link['category_id']) {
  8488. $link['category_title'] = get_lang('Uncategorized');
  8489. }
  8490. $categories[$link['category_id']] = $link['category_title'];
  8491. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  8492. }
  8493. $linksHtmlCode =
  8494. '<script>
  8495. function toggle_tool(tool, id){
  8496. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8497. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8498. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8499. } else {
  8500. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8501. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8502. }
  8503. }
  8504. </script>
  8505. <ul class="lp_resource">
  8506. <li class="lp_resource_element">
  8507. '.Display::return_icon('linksnew.gif').'
  8508. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8509. get_lang('LinkAdd').'
  8510. </a>
  8511. </li>';
  8512. foreach ($categorizedLinks as $categoryId => $links) {
  8513. $linkNodes = null;
  8514. foreach ($links as $key => $linkInfo) {
  8515. $title = $linkInfo['link_title'];
  8516. $linkSessionId = $linkInfo['link_session_id'];
  8517. $link = Display::url(
  8518. Display::return_icon('preview_view.png', get_lang('Preview')),
  8519. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8520. ['target' => '_blank']
  8521. );
  8522. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8523. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8524. $linkNodes .=
  8525. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8526. <a class="moved" href="#">'.
  8527. $moveEverywhereIcon.
  8528. '</a>
  8529. '.Display::return_icon('lp_link.png').'
  8530. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8531. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8532. Security::remove_XSS($title).$sessionStar.$link.
  8533. '</a>
  8534. </li>';
  8535. }
  8536. }
  8537. $linksHtmlCode .=
  8538. '<li>
  8539. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8540. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8541. align="absbottom" />
  8542. </a>
  8543. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8544. </li>
  8545. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8546. }
  8547. $linksHtmlCode .= '</ul>';
  8548. return $linksHtmlCode;
  8549. }
  8550. /**
  8551. * Creates a list with all the student publications in it
  8552. * @return string
  8553. */
  8554. public function get_student_publications()
  8555. {
  8556. $return = '<ul class="lp_resource">';
  8557. $return .= '<li class="lp_resource_element">';
  8558. $return .= Display::return_icon('works_new.gif');
  8559. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8560. get_lang('AddAssignmentPage') . '</a>';
  8561. $return .= '</li>';
  8562. $sessionId = api_get_session_id();
  8563. if (empty($sessionId)) {
  8564. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8565. $works = getWorkListTeacher(0, 100, null, null, null);
  8566. if (!empty($works)) {
  8567. foreach ($works as $work) {
  8568. $link = Display::url(
  8569. Display::return_icon('preview_view.png', get_lang('Preview')),
  8570. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8571. ['target' => '_blank']
  8572. );
  8573. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8574. $return .= '<a class="moved" href="#">';
  8575. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8576. $return .= '</a> ';
  8577. $return .= Display::return_icon('works.gif');
  8578. $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 . '">' .
  8579. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8580. </a>';
  8581. $return .= '</li>';
  8582. }
  8583. }
  8584. }
  8585. $return .= '</ul>';
  8586. return $return;
  8587. }
  8588. /**
  8589. * Creates a list with all the forums in it
  8590. * @return string
  8591. */
  8592. public function get_forums()
  8593. {
  8594. require_once '../forum/forumfunction.inc.php';
  8595. require_once '../forum/forumconfig.inc.php';
  8596. $forumCategories = get_forum_categories();
  8597. $forumsInNoCategory = get_forums_in_category(0);
  8598. if (!empty($forumsInNoCategory)) {
  8599. $forumCategories = array_merge(
  8600. $forumCategories,
  8601. array(
  8602. array(
  8603. 'cat_id' => 0,
  8604. 'session_id' => 0,
  8605. 'visibility' => 1,
  8606. 'cat_comment' => null,
  8607. ),
  8608. )
  8609. );
  8610. }
  8611. $forumList = get_forums();
  8612. $a_forums = [];
  8613. foreach ($forumCategories as $forumCategory) {
  8614. // The forums in this category.
  8615. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8616. if (!empty($forumsInCategory)) {
  8617. foreach ($forumList as $forum) {
  8618. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8619. $a_forums[] = $forum;
  8620. }
  8621. }
  8622. }
  8623. }
  8624. $return = '<ul class="lp_resource">';
  8625. // First add link
  8626. $return .= '<li class="lp_resource_element">';
  8627. $return .= Display::return_icon('new_forum.png');
  8628. $return .= Display::url(
  8629. get_lang('CreateANewForum'),
  8630. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8631. 'action' => 'add',
  8632. 'content' => 'forum',
  8633. 'lp_id' => $this->lp_id
  8634. ]),
  8635. ['title' => get_lang('CreateANewForum')]
  8636. );
  8637. $return .= '</li>';
  8638. $return .= '<script>
  8639. function toggle_forum(forum_id) {
  8640. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8641. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8642. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8643. } else {
  8644. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8645. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8646. }
  8647. }
  8648. </script>';
  8649. foreach ($a_forums as $forum) {
  8650. if (!empty($forum['forum_id'])) {
  8651. $link = Display::url(
  8652. Display::return_icon('preview_view.png', get_lang('Preview')),
  8653. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8654. ['target' => '_blank']
  8655. );
  8656. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8657. $return .= '<a class="moved" href="#">';
  8658. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8659. $return .= ' </a>';
  8660. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8661. $return .= '<a onclick="javascript:toggle_forum(' . $forum['forum_id'] . ');" style="cursor:hand; vertical-align:middle">
  8662. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8663. </a>
  8664. <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">' .
  8665. Security::remove_XSS($forum['forum_title']) . ' '.$link.'</a>';
  8666. $return .= '</li>';
  8667. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8668. $a_threads = get_threads($forum['forum_id']);
  8669. if (is_array($a_threads)) {
  8670. foreach ($a_threads as $thread) {
  8671. $link = Display::url(
  8672. Display::return_icon('preview_view.png', get_lang('Preview')),
  8673. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8674. ['target' => '_blank']
  8675. );
  8676. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8677. $return .= '&nbsp;<a class="moved" href="#">';
  8678. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8679. $return .= ' </a>';
  8680. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8681. $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 . '">' .
  8682. Security::remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8683. $return .= '</li>';
  8684. }
  8685. }
  8686. $return .= '</div>';
  8687. }
  8688. }
  8689. $return .= '</ul>';
  8690. return $return;
  8691. }
  8692. /**
  8693. * // TODO: The output encoding should be equal to the system encoding.
  8694. *
  8695. * Exports the learning path as a SCORM package. This is the main function that
  8696. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8697. * whole thing and returns the zip.
  8698. *
  8699. * This method needs to be called in PHP5, as it will fail with non-adequate
  8700. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8701. * you need to call it on a learnpath object.
  8702. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8703. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8704. * path has been modified, it should use the generic method here below.
  8705. * @TODO link this function with the export_lp() function in the same class
  8706. * @param string Optional name of zip file. If none, title of learnpath is
  8707. * domesticated and trailed with ".zip"
  8708. * @return string Returns the zip package string, or null if error
  8709. */
  8710. public function scorm_export()
  8711. {
  8712. $_course = api_get_course_info();
  8713. $course_id = $_course['real_id'];
  8714. // Remove memory and time limits as much as possible as this might be a long process...
  8715. if (function_exists('ini_set')) {
  8716. api_set_memory_limit('256M');
  8717. ini_set('max_execution_time', 600);
  8718. }
  8719. // Create the zip handler (this will remain available throughout the method).
  8720. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8721. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8722. $temp_dir_short = uniqid();
  8723. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8724. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8725. $zip_folder = new PclZip($temp_zip_file);
  8726. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8727. $root_path = $main_path = api_get_path(SYS_PATH);
  8728. $files_cleanup = array();
  8729. // Place to temporarily stash the zip file.
  8730. // create the temp dir if it doesn't exist
  8731. // or do a cleanup before creating the zip file.
  8732. if (!is_dir($temp_zip_dir)) {
  8733. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8734. } else {
  8735. // Cleanup: Check the temp dir for old files and delete them.
  8736. $handle = opendir($temp_zip_dir);
  8737. while (false !== ($file = readdir($handle))) {
  8738. if ($file != '.' && $file != '..') {
  8739. unlink("$temp_zip_dir/$file");
  8740. }
  8741. }
  8742. closedir($handle);
  8743. }
  8744. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8745. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8746. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8747. ) {
  8748. // Remove the possible . at the end of the path.
  8749. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8750. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8751. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8752. copyr(
  8753. $current_course_path.'/scorm/'.$this->path,
  8754. $dest_path_to_scorm_folder,
  8755. array('imsmanifest'),
  8756. $zip_files
  8757. );
  8758. }
  8759. // Build a dummy imsmanifest structure.
  8760. // Do not add to the zip yet (we still need it).
  8761. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8762. // Aggregation Model official document, section "2.3 Content Packaging".
  8763. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8764. $xmldoc = new DOMDocument('1.0');
  8765. $root = $xmldoc->createElement('manifest');
  8766. $root->setAttribute('identifier', 'SingleCourseManifest');
  8767. $root->setAttribute('version', '1.1');
  8768. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8769. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8770. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8771. $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');
  8772. // Build mandatory sub-root container elements.
  8773. $metadata = $xmldoc->createElement('metadata');
  8774. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8775. $metadata->appendChild($md_schema);
  8776. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8777. $metadata->appendChild($md_schemaversion);
  8778. $root->appendChild($metadata);
  8779. $organizations = $xmldoc->createElement('organizations');
  8780. $resources = $xmldoc->createElement('resources');
  8781. // Build the only organization we will use in building our learnpaths.
  8782. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8783. $organization = $xmldoc->createElement('organization');
  8784. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8785. // To set the title of the SCORM entity (=organization), we take the name given
  8786. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8787. // learning path charset) as it is the encoding that defines how it is stored
  8788. // in the database. Then we convert it to HTML entities again as the "&" character
  8789. // alone is not authorized in XML (must be &amp;).
  8790. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8791. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8792. $organization->appendChild($org_title);
  8793. $folder_name = 'document';
  8794. // Removes the learning_path/scorm_folder path when exporting see #4841
  8795. $path_to_remove = null;
  8796. $result = $this->generate_lp_folder($_course);
  8797. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8798. $path_to_remove = 'document'.$result['dir'];
  8799. $path_to_replace = $folder_name.'/';
  8800. }
  8801. // Fixes chamilo scorm exports
  8802. if ($this->ref === 'chamilo_scorm_export') {
  8803. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8804. }
  8805. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8806. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8807. $link_updates = array();
  8808. $links_to_create = array();
  8809. //foreach ($this->items as $index => $item) {
  8810. foreach ($this->ordered_items as $index => $itemId) {
  8811. $item = $this->items[$itemId];
  8812. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8813. // Get included documents from this item.
  8814. if ($item->type === 'sco') {
  8815. $inc_docs = $item->get_resources_from_source(
  8816. null,
  8817. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8818. );
  8819. } else {
  8820. $inc_docs = $item->get_resources_from_source();
  8821. }
  8822. // Give a child element <item> to the <organization> element.
  8823. $my_item_id = $item->get_id();
  8824. $my_item = $xmldoc->createElement('item');
  8825. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8826. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8827. $my_item->setAttribute('isvisible', 'true');
  8828. // Give a child element <title> to the <item> element.
  8829. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8830. $my_item->appendChild($my_title);
  8831. // Give a child element <adlcp:prerequisites> to the <item> element.
  8832. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8833. $my_prereqs->setAttribute('type', 'aicc_script');
  8834. $my_item->appendChild($my_prereqs);
  8835. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8836. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8837. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8838. //$xmldoc->createElement('adlcp:timelimitaction','');
  8839. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8840. //$xmldoc->createElement('adlcp:datafromlms','');
  8841. // Give a child element <adlcp:masteryscore> to the <item> element.
  8842. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8843. $my_item->appendChild($my_masteryscore);
  8844. // Attach this item to the organization element or hits parent if there is one.
  8845. if (!empty($item->parent) && $item->parent != 0) {
  8846. $children = $organization->childNodes;
  8847. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8848. if (is_object($possible_parent)) {
  8849. $possible_parent->appendChild($my_item);
  8850. } else {
  8851. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8852. }
  8853. } else {
  8854. if ($this->debug > 0) { error_log('No parent'); }
  8855. $organization->appendChild($my_item);
  8856. }
  8857. // Get the path of the file(s) from the course directory root.
  8858. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8859. if (!empty($path_to_remove)) {
  8860. //From docs
  8861. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8862. //From quiz
  8863. if ($this->ref === 'chamilo_scorm_export') {
  8864. $path_to_remove = 'scorm/'.$this->path.'/';
  8865. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8866. }
  8867. } else {
  8868. $my_xml_file_path = $my_file_path;
  8869. }
  8870. $my_sub_dir = dirname($my_file_path);
  8871. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8872. $my_xml_sub_dir = $my_sub_dir;
  8873. // Give a <resource> child to the <resources> element
  8874. $my_resource = $xmldoc->createElement('resource');
  8875. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8876. $my_resource->setAttribute('type', 'webcontent');
  8877. $my_resource->setAttribute('href', $my_xml_file_path);
  8878. // adlcp:scormtype can be either 'sco' or 'asset'.
  8879. if ($item->type === 'sco') {
  8880. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8881. } else {
  8882. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8883. }
  8884. // xml:base is the base directory to find the files declared in this resource.
  8885. $my_resource->setAttribute('xml:base', '');
  8886. // Give a <file> child to the <resource> element.
  8887. $my_file = $xmldoc->createElement('file');
  8888. $my_file->setAttribute('href', $my_xml_file_path);
  8889. $my_resource->appendChild($my_file);
  8890. // Dependency to other files - not yet supported.
  8891. $i = 1;
  8892. if ($inc_docs)
  8893. foreach ($inc_docs as $doc_info) {
  8894. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8895. continue;
  8896. }
  8897. $my_dep = $xmldoc->createElement('resource');
  8898. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8899. $my_dep->setAttribute('identifier', $res_id);
  8900. $my_dep->setAttribute('type', 'webcontent');
  8901. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8902. $my_dep_file = $xmldoc->createElement('file');
  8903. // Check type of URL.
  8904. if ($doc_info[1] == 'remote') {
  8905. // Remote file. Save url as is.
  8906. $my_dep_file->setAttribute('href', $doc_info[0]);
  8907. $my_dep->setAttribute('xml:base', '');
  8908. } elseif ($doc_info[1] === 'local') {
  8909. switch ($doc_info[2]) {
  8910. case 'url': // Local URL - save path as url for now, don't zip file.
  8911. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8912. $current_dir = dirname($abs_path);
  8913. $current_dir = str_replace('\\', '/', $current_dir);
  8914. $file_path = realpath($abs_path);
  8915. $file_path = str_replace('\\', '/', $file_path);
  8916. $my_dep_file->setAttribute('href', $file_path);
  8917. $my_dep->setAttribute('xml:base', '');
  8918. if (strstr($file_path, $main_path) !== false) {
  8919. // The calculated real path is really inside Chamilo's root path.
  8920. // Reduce file path to what's under the DocumentRoot.
  8921. $file_path = substr($file_path, strlen($root_path) - 1);
  8922. //echo $file_path;echo '<br /><br />';
  8923. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8924. $zip_files_abs[] = $file_path;
  8925. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8926. $my_dep_file->setAttribute('href', $file_path);
  8927. $my_dep->setAttribute('xml:base', '');
  8928. } elseif (empty($file_path)) {
  8929. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8930. if (strpos($document_root, -1) == '/') {
  8931. $document_root = substr(0, -1, $document_root);
  8932. }*/
  8933. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8934. $file_path = str_replace('//', '/', $file_path);
  8935. if (file_exists($file_path)) {
  8936. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8937. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8938. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8939. $my_dep_file->setAttribute('href', $file_path);
  8940. $my_dep->setAttribute('xml:base', '');
  8941. }
  8942. }
  8943. break;
  8944. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8945. $my_dep_file->setAttribute('href', $doc_info[0]);
  8946. $my_dep->setAttribute('xml:base', '');
  8947. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8948. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8949. $abs_img_path_without_subdir = $doc_info[0];
  8950. $relp = api_get_path(REL_PATH); // The url-append config param.
  8951. $pos = strpos($abs_img_path_without_subdir, $relp);
  8952. if ($pos === 0) {
  8953. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8954. }
  8955. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8956. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8957. $file_path = str_replace('\\', '/', $file_path);
  8958. $file_path = str_replace('//', '/', $file_path);
  8959. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8960. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8961. // Check if the current document is in that path.
  8962. if (strstr($file_path, $cur_path) !== false) {
  8963. // The document is in that path, now get the relative path
  8964. // to the containing document.
  8965. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8966. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8967. $relative_path = '';
  8968. if (strstr($file_path, $cur_path) !== false) {
  8969. //$relative_path = substr($file_path, strlen($orig_file_path));
  8970. $relative_path = str_replace($cur_path, '', $file_path);
  8971. $file_path = substr($file_path, strlen($cur_path));
  8972. } else {
  8973. // This case is still a problem as it's difficult to calculate a relative path easily
  8974. // might still generate wrong links.
  8975. //$file_path = substr($file_path,strlen($cur_path));
  8976. // Calculate the directory path to the current file (without trailing slash).
  8977. $my_relative_path = dirname($file_path);
  8978. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8979. $my_relative_file = basename($file_path);
  8980. // Calculate the directory path to the containing file (without trailing slash).
  8981. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8982. $dotdot = '';
  8983. $subdir = '';
  8984. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8985. $my_relative_path2 = dirname($my_relative_path);
  8986. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8987. $my_orig_file_path = dirname($my_orig_file_path);
  8988. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8989. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8990. $dotdot += '../';
  8991. $my_relative_path = $my_relative_path2;
  8992. }
  8993. $relative_path = $dotdot.$subdir.$my_relative_file;
  8994. }
  8995. // Put the current document in the zip (this array is the array
  8996. // that will manage documents already in the course folder - relative).
  8997. $zip_files[] = $file_path;
  8998. // Update the links to the current document in the containing document (make them relative).
  8999. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  9000. $my_dep_file->setAttribute('href', $file_path);
  9001. $my_dep->setAttribute('xml:base', '');
  9002. } elseif (strstr($file_path, $main_path) !== false) {
  9003. // The calculated real path is really inside Chamilo's root path.
  9004. // Reduce file path to what's under the DocumentRoot.
  9005. $file_path = substr($file_path, strlen($root_path));
  9006. //echo $file_path;echo '<br /><br />';
  9007. //error_log('Reduced path: '.$file_path, 0);
  9008. $zip_files_abs[] = $file_path;
  9009. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9010. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9011. $my_dep->setAttribute('xml:base', '');
  9012. } elseif (empty($file_path)) {
  9013. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9014. if(strpos($document_root,-1) == '/') {
  9015. $document_root = substr(0, -1, $document_root);
  9016. }*/
  9017. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9018. $file_path = str_replace('//', '/', $file_path);
  9019. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9020. $current_dir = dirname($abs_path);
  9021. $current_dir = str_replace('\\', '/', $current_dir);
  9022. if (file_exists($file_path)) {
  9023. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9024. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9025. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9026. $my_dep_file->setAttribute('href','document/'.$file_path);
  9027. $my_dep->setAttribute('xml:base', '');
  9028. }
  9029. }
  9030. break;
  9031. case 'rel':
  9032. // Path relative to the current document.
  9033. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  9034. if (substr($doc_info[0], 0, 2) == '..') {
  9035. // Relative path going up.
  9036. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9037. $current_dir = str_replace('\\', '/', $current_dir);
  9038. $file_path = realpath($current_dir.$doc_info[0]);
  9039. $file_path = str_replace('\\', '/', $file_path);
  9040. //error_log($file_path.' <-> '.$main_path,0);
  9041. if (strstr($file_path, $main_path) !== false) {
  9042. // The calculated real path is really inside Chamilo's root path.
  9043. // Reduce file path to what's under the DocumentRoot.
  9044. $file_path = substr($file_path, strlen($root_path));
  9045. //error_log('Reduced path: '.$file_path, 0);
  9046. $zip_files_abs[] = $file_path;
  9047. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9048. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9049. $my_dep->setAttribute('xml:base', '');
  9050. }
  9051. } else {
  9052. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9053. $my_dep_file->setAttribute('href', $doc_info[0]);
  9054. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9055. }
  9056. break;
  9057. default:
  9058. $my_dep_file->setAttribute('href', $doc_info[0]);
  9059. $my_dep->setAttribute('xml:base', '');
  9060. break;
  9061. }
  9062. }
  9063. $my_dep->appendChild($my_dep_file);
  9064. $resources->appendChild($my_dep);
  9065. $dependency = $xmldoc->createElement('dependency');
  9066. $dependency->setAttribute('identifierref', $res_id);
  9067. $my_resource->appendChild($dependency);
  9068. $i++;
  9069. }
  9070. $resources->appendChild($my_resource);
  9071. $zip_files[] = $my_file_path;
  9072. } else {
  9073. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  9074. switch ($item->type) {
  9075. case TOOL_LINK:
  9076. $my_item = $xmldoc->createElement('item');
  9077. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9078. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9079. $my_item->setAttribute('isvisible', 'true');
  9080. // Give a child element <title> to the <item> element.
  9081. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  9082. $my_item->appendChild($my_title);
  9083. // Give a child element <adlcp:prerequisites> to the <item> element.
  9084. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9085. $my_prereqs->setAttribute('type', 'aicc_script');
  9086. $my_item->appendChild($my_prereqs);
  9087. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  9088. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  9089. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  9090. //$xmldoc->createElement('adlcp:timelimitaction', '');
  9091. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  9092. //$xmldoc->createElement('adlcp:datafromlms', '');
  9093. // Give a child element <adlcp:masteryscore> to the <item> element.
  9094. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9095. $my_item->appendChild($my_masteryscore);
  9096. // Attach this item to the organization element or its parent if there is one.
  9097. if (!empty($item->parent) && $item->parent != 0) {
  9098. $children = $organization->childNodes;
  9099. for ($i = 0; $i < $children->length; $i++) {
  9100. $item_temp = $children->item($i);
  9101. if ($item_temp -> nodeName == 'item') {
  9102. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9103. $item_temp -> appendChild($my_item);
  9104. }
  9105. }
  9106. }
  9107. } else {
  9108. $organization->appendChild($my_item);
  9109. }
  9110. $my_file_path = 'link_'.$item->get_id().'.html';
  9111. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  9112. WHERE c_id = '.$course_id.' AND id='.$item->path;
  9113. $rs = Database::query($sql);
  9114. if ($link = Database::fetch_array($rs)) {
  9115. $url = $link['url'];
  9116. $title = stripslashes($link['title']);
  9117. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  9118. $my_xml_file_path = $my_file_path;
  9119. $my_sub_dir = dirname($my_file_path);
  9120. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9121. $my_xml_sub_dir = $my_sub_dir;
  9122. // Give a <resource> child to the <resources> element.
  9123. $my_resource = $xmldoc->createElement('resource');
  9124. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9125. $my_resource->setAttribute('type', 'webcontent');
  9126. $my_resource->setAttribute('href', $my_xml_file_path);
  9127. // adlcp:scormtype can be either 'sco' or 'asset'.
  9128. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9129. // xml:base is the base directory to find the files declared in this resource.
  9130. $my_resource->setAttribute('xml:base', '');
  9131. // give a <file> child to the <resource> element.
  9132. $my_file = $xmldoc->createElement('file');
  9133. $my_file->setAttribute('href', $my_xml_file_path);
  9134. $my_resource->appendChild($my_file);
  9135. $resources->appendChild($my_resource);
  9136. }
  9137. break;
  9138. case TOOL_QUIZ:
  9139. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  9140. $exe = new Exercise();
  9141. $exe->read($exe_id);
  9142. $my_item = $xmldoc->createElement('item');
  9143. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9144. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9145. $my_item->setAttribute('isvisible', 'true');
  9146. // Give a child element <title> to the <item> element.
  9147. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  9148. $my_item->appendChild($my_title);
  9149. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  9150. //$my_item->appendChild($my_max_score);
  9151. // Give a child element <adlcp:prerequisites> to the <item> element.
  9152. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9153. $my_prereqs->setAttribute('type','aicc_script');
  9154. $my_item->appendChild($my_prereqs);
  9155. // Give a child element <adlcp:masteryscore> to the <item> element.
  9156. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9157. $my_item->appendChild($my_masteryscore);
  9158. // Attach this item to the organization element or hits parent if there is one.
  9159. if (!empty($item->parent) && $item->parent != 0) {
  9160. $children = $organization->childNodes;
  9161. /*for ($i = 0; $i < $children->length; $i++) {
  9162. $item_temp = $children->item($i);
  9163. if ($exe_id == 81) {
  9164. error_log($item_temp->nodeName );
  9165. error_log($item_temp->getAttribute('identifier'));
  9166. }
  9167. if ($item_temp->nodeName == 'item') {
  9168. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9169. $item_temp->appendChild($my_item);
  9170. }
  9171. }
  9172. }*/
  9173. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9174. if ($possible_parent) {
  9175. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9176. $possible_parent->appendChild($my_item);
  9177. }
  9178. }
  9179. } else {
  9180. $organization->appendChild($my_item);
  9181. }
  9182. // Get the path of the file(s) from the course directory root
  9183. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9184. $my_file_path = 'quiz_'.$item->get_id().'.html';
  9185. // Write the contents of the exported exercise into a (big) html file
  9186. // to later pack it into the exported SCORM. The file will be removed afterwards.
  9187. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  9188. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  9189. $res = file_put_contents($tmp_file_path, $contents);
  9190. if ($res === false) {
  9191. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  9192. }
  9193. $files_cleanup[] = $tmp_file_path;
  9194. $my_xml_file_path = $my_file_path;
  9195. $my_sub_dir = dirname($my_file_path);
  9196. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9197. $my_xml_sub_dir = $my_sub_dir;
  9198. // Give a <resource> child to the <resources> element.
  9199. $my_resource = $xmldoc->createElement('resource');
  9200. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9201. $my_resource->setAttribute('type', 'webcontent');
  9202. $my_resource->setAttribute('href', $my_xml_file_path);
  9203. // adlcp:scormtype can be either 'sco' or 'asset'.
  9204. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9205. // xml:base is the base directory to find the files declared in this resource.
  9206. $my_resource->setAttribute('xml:base', '');
  9207. // Give a <file> child to the <resource> element.
  9208. $my_file = $xmldoc->createElement('file');
  9209. $my_file->setAttribute('href', $my_xml_file_path);
  9210. $my_resource->appendChild($my_file);
  9211. // Get included docs.
  9212. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  9213. // Dependency to other files - not yet supported.
  9214. $i = 1;
  9215. foreach ($inc_docs as $doc_info) {
  9216. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  9217. $my_dep = $xmldoc->createElement('resource');
  9218. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9219. $my_dep->setAttribute('identifier', $res_id);
  9220. $my_dep->setAttribute('type', 'webcontent');
  9221. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9222. $my_dep_file = $xmldoc->createElement('file');
  9223. // Check type of URL.
  9224. if ($doc_info[1] == 'remote') {
  9225. // Remote file. Save url as is.
  9226. $my_dep_file->setAttribute('href', $doc_info[0]);
  9227. $my_dep->setAttribute('xml:base', '');
  9228. } elseif ($doc_info[1] == 'local') {
  9229. switch ($doc_info[2]) {
  9230. case 'url': // Local URL - save path as url for now, don't zip file.
  9231. // Save file but as local file (retrieve from URL).
  9232. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9233. $current_dir = dirname($abs_path);
  9234. $current_dir = str_replace('\\', '/', $current_dir);
  9235. $file_path = realpath($abs_path);
  9236. $file_path = str_replace('\\', '/', $file_path);
  9237. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9238. $my_dep->setAttribute('xml:base', '');
  9239. if (strstr($file_path, $main_path) !== false) {
  9240. // The calculated real path is really inside the chamilo root path.
  9241. // Reduce file path to what's under the DocumentRoot.
  9242. $file_path = substr($file_path, strlen($root_path));
  9243. $zip_files_abs[] = $file_path;
  9244. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9245. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9246. $my_dep->setAttribute('xml:base', '');
  9247. } elseif (empty($file_path)) {
  9248. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  9249. if (strpos($document_root,-1) == '/') {
  9250. $document_root = substr(0, -1, $document_root);
  9251. }*/
  9252. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9253. $file_path = str_replace('//', '/', $file_path);
  9254. if (file_exists($file_path)) {
  9255. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9256. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9257. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9258. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9259. $my_dep->setAttribute('xml:base', '');
  9260. }
  9261. }
  9262. break;
  9263. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9264. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9265. $current_dir = str_replace('\\', '/', $current_dir);
  9266. $file_path = realpath($doc_info[0]);
  9267. $file_path = str_replace('\\', '/', $file_path);
  9268. $my_dep_file->setAttribute('href', $file_path);
  9269. $my_dep->setAttribute('xml:base', '');
  9270. if (strstr($file_path,$main_path) !== false) {
  9271. // The calculated real path is really inside the chamilo root path.
  9272. // Reduce file path to what's under the DocumentRoot.
  9273. $file_path = substr($file_path, strlen($root_path));
  9274. //echo $file_path;echo '<br /><br />';
  9275. //error_log('Reduced path: '.$file_path, 0);
  9276. $zip_files_abs[] = $file_path;
  9277. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $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'].$doc_info[0];
  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' => $file_path);
  9291. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9292. $my_dep->setAttribute('xml:base', '');
  9293. }
  9294. }
  9295. break;
  9296. 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
  9297. if (substr($doc_info[0], 0, 2) == '..') {
  9298. // Relative path going up.
  9299. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9300. $current_dir = str_replace('\\', '/', $current_dir);
  9301. $file_path = realpath($current_dir.$doc_info[0]);
  9302. $file_path = str_replace('\\', '/', $file_path);
  9303. //error_log($file_path.' <-> '.$main_path, 0);
  9304. if (strstr($file_path, $main_path) !== false) {
  9305. // The calculated real path is really inside Chamilo's root path.
  9306. // Reduce file path to what's under the DocumentRoot.
  9307. $file_path = substr($file_path, strlen($root_path));
  9308. $file_path_dest = $file_path;
  9309. // File path is courses/CHAMILO/document/....
  9310. $info_file_path = explode('/', $file_path);
  9311. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9312. $file_path_dest = 'document/'.$file_path;
  9313. }
  9314. //error_log('Reduced path: '.$file_path, 0);
  9315. $zip_files_abs[] = $file_path;
  9316. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  9317. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9318. $my_dep->setAttribute('xml:base', '');
  9319. }
  9320. } else {
  9321. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9322. $my_dep_file->setAttribute('href', $doc_info[0]);
  9323. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9324. }
  9325. break;
  9326. default:
  9327. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9328. $my_dep->setAttribute('xml:base', '');
  9329. break;
  9330. }
  9331. }
  9332. $my_dep->appendChild($my_dep_file);
  9333. $resources->appendChild($my_dep);
  9334. $dependency = $xmldoc->createElement('dependency');
  9335. $dependency->setAttribute('identifierref', $res_id);
  9336. $my_resource->appendChild($dependency);
  9337. $i++;
  9338. }
  9339. $resources->appendChild($my_resource);
  9340. $zip_files[] = $my_file_path;
  9341. break;
  9342. default:
  9343. // Get the path of the file(s) from the course directory root
  9344. $my_file_path = 'non_exportable.html';
  9345. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9346. $my_xml_file_path = $my_file_path;
  9347. $my_sub_dir = dirname($my_file_path);
  9348. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9349. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9350. $my_xml_sub_dir = $my_sub_dir;
  9351. // Give a <resource> child to the <resources> element.
  9352. $my_resource = $xmldoc->createElement('resource');
  9353. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9354. $my_resource->setAttribute('type', 'webcontent');
  9355. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9356. // adlcp:scormtype can be either 'sco' or 'asset'.
  9357. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9358. // xml:base is the base directory to find the files declared in this resource.
  9359. $my_resource->setAttribute('xml:base', '');
  9360. // Give a <file> child to the <resource> element.
  9361. $my_file = $xmldoc->createElement('file');
  9362. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9363. $my_resource->appendChild($my_file);
  9364. $resources->appendChild($my_resource);
  9365. break;
  9366. }
  9367. }
  9368. }
  9369. $organizations->appendChild($organization);
  9370. $root->appendChild($organizations);
  9371. $root->appendChild($resources);
  9372. $xmldoc->appendChild($root);
  9373. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  9374. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9375. // then add the file to the zip, then destroy the file (this is done automatically).
  9376. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9377. foreach ($zip_files as $file_path) {
  9378. if (empty($file_path)) {
  9379. continue;
  9380. }
  9381. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  9382. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9383. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9384. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9385. }
  9386. $this->create_path($dest_file);
  9387. @copy($filePath, $dest_file);
  9388. // Check if the file needs a link update.
  9389. if (in_array($file_path, array_keys($link_updates))) {
  9390. $string = file_get_contents($dest_file);
  9391. unlink($dest_file);
  9392. foreach ($link_updates[$file_path] as $old_new) {
  9393. // This is an ugly hack that allows .flv files to be found by the flv player that
  9394. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9395. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9396. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9397. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9398. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9399. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9400. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9401. }
  9402. //Fix to avoid problems with default_course_document
  9403. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9404. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9405. $newDestination = $old_new['dest'];
  9406. } else {
  9407. $newDestination = str_replace('document/', '', $old_new['dest']);
  9408. }
  9409. $string = str_replace($old_new['orig'], $newDestination, $string);
  9410. // Add files inside the HTMLs
  9411. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9412. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9413. if (file_exists($sys_course_path.$new_path)) {
  9414. copy($sys_course_path.$new_path, $destinationFile);
  9415. }
  9416. }
  9417. file_put_contents($dest_file, $string);
  9418. }
  9419. if (file_exists($filePath) && $copyAll) {
  9420. $extension = $this->get_extension($filePath);
  9421. if (in_array($extension, ['html', 'html'])) {
  9422. $containerOrigin = dirname($filePath);
  9423. $containerDestination = dirname($dest_file);
  9424. $finder = new Finder();
  9425. $finder->files()->in($containerOrigin)
  9426. ->notName('*_DELETED_*')
  9427. ->exclude('share_folder')
  9428. ->exclude('chat_files')
  9429. ->exclude('certificates')
  9430. ;
  9431. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9432. $fs = new Filesystem();
  9433. $fs->mirror(
  9434. $containerOrigin,
  9435. $containerDestination,
  9436. $finder
  9437. );
  9438. }
  9439. }
  9440. }
  9441. }
  9442. foreach ($zip_files_abs as $file_path) {
  9443. if (empty($file_path)) {
  9444. continue;
  9445. }
  9446. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9447. continue;
  9448. }
  9449. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9450. $this->create_path($dest_file);
  9451. copy($main_path.$file_path, $dest_file);
  9452. // Check if the file needs a link update.
  9453. if (in_array($file_path, array_keys($link_updates))) {
  9454. $string = file_get_contents($dest_file);
  9455. unlink($dest_file);
  9456. foreach ($link_updates[$file_path] as $old_new) {
  9457. // This is an ugly hack that allows .flv files to be found by the flv player that
  9458. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9459. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9460. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9461. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9462. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9463. }
  9464. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9465. }
  9466. file_put_contents($dest_file, $string);
  9467. }
  9468. }
  9469. if (is_array($links_to_create)) {
  9470. foreach ($links_to_create as $file => $link) {
  9471. $file_content = '<!DOCTYPE html><head>
  9472. <meta charset="'.api_get_language_isocode().'" />
  9473. <title>'.$link['title'].'</title>
  9474. </head>
  9475. <body dir="'.api_get_text_direction().'">
  9476. <div style="text-align:center">
  9477. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9478. </body>
  9479. </html>';
  9480. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9481. }
  9482. }
  9483. // Add non exportable message explanation.
  9484. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9485. $file_content = '<!DOCTYPE html><head>
  9486. <meta charset="'.api_get_language_isocode().'" />
  9487. <title>'.$lang_not_exportable.'</title>
  9488. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9489. </head>
  9490. <body dir="'.api_get_text_direction().'">';
  9491. $file_content .=
  9492. <<<EOD
  9493. <style>
  9494. .error-message {
  9495. font-family: arial, verdana, helvetica, sans-serif;
  9496. border-width: 1px;
  9497. border-style: solid;
  9498. left: 50%;
  9499. margin: 10px auto;
  9500. min-height: 30px;
  9501. padding: 5px;
  9502. right: 50%;
  9503. width: 500px;
  9504. background-color: #FFD1D1;
  9505. border-color: #FF0000;
  9506. color: #000;
  9507. }
  9508. </style>
  9509. <body>
  9510. <div class="error-message">
  9511. $lang_not_exportable
  9512. </div>
  9513. </body>
  9514. </html>
  9515. EOD;
  9516. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9517. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9518. }
  9519. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9520. // Add the extra files that go along with a SCORM package.
  9521. $main_code_path = api_get_path(SYS_CODE_PATH) . 'lp/packaging/';
  9522. $fs = new Filesystem();
  9523. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9524. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9525. $manifest = @$xmldoc->saveXML();
  9526. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9527. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9528. $zip_folder->add(
  9529. $archive_path.'/'.$temp_dir_short,
  9530. PCLZIP_OPT_REMOVE_PATH,
  9531. $archive_path.'/'.$temp_dir_short.'/'
  9532. );
  9533. // Clean possible temporary files.
  9534. foreach ($files_cleanup as $file) {
  9535. $res = unlink($file);
  9536. if ($res === false) {
  9537. error_log(
  9538. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9539. 0
  9540. );
  9541. }
  9542. }
  9543. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9544. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9545. }
  9546. /**
  9547. * @param int $lp_id
  9548. * @return bool
  9549. */
  9550. public function scorm_export_to_pdf($lp_id)
  9551. {
  9552. $lp_id = intval($lp_id);
  9553. $files_to_export = array();
  9554. $course_data = api_get_course_info($this->cc);
  9555. if (!empty($course_data)) {
  9556. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9557. $list = self::get_flat_ordered_items_list($lp_id);
  9558. if (!empty($list)) {
  9559. foreach ($list as $item_id) {
  9560. $item = $this->items[$item_id];
  9561. switch ($item->type) {
  9562. case 'document':
  9563. //Getting documents from a LP with chamilo documents
  9564. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9565. // Try loading document from the base course.
  9566. if (empty($file_data) && !empty($sessionId)) {
  9567. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9568. }
  9569. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9570. if (file_exists($file_path)) {
  9571. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9572. }
  9573. break;
  9574. case 'asset': //commes from a scorm package generated by chamilo
  9575. case 'sco':
  9576. $file_path = $scorm_path.'/'.$item->path;
  9577. if (file_exists($file_path)) {
  9578. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9579. }
  9580. break;
  9581. case 'dir':
  9582. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9583. break;
  9584. }
  9585. }
  9586. }
  9587. $pdf = new PDF();
  9588. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9589. return $result;
  9590. }
  9591. return false;
  9592. }
  9593. /**
  9594. * Temp function to be moved in main_api or the best place around for this.
  9595. * Creates a file path if it doesn't exist
  9596. * @param string $path
  9597. */
  9598. public function create_path($path)
  9599. {
  9600. $path_bits = explode('/', dirname($path));
  9601. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9602. $path_built = IS_WINDOWS_OS ? '' : '/';
  9603. foreach ($path_bits as $bit) {
  9604. if (!empty ($bit)) {
  9605. $new_path = $path_built . $bit;
  9606. if (is_dir($new_path)) {
  9607. $path_built = $new_path . '/';
  9608. } else {
  9609. mkdir($new_path, api_get_permissions_for_new_directories());
  9610. $path_built = $new_path . '/';
  9611. }
  9612. }
  9613. }
  9614. }
  9615. /**
  9616. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9617. * @return boolean The results of the unlink function, or false if there was no image to start with
  9618. */
  9619. public function delete_lp_image()
  9620. {
  9621. $img = $this->get_preview_image();
  9622. if ($img != '') {
  9623. $del_file = $this->get_preview_image_path(null, 'sys');
  9624. if (isset($del_file) && file_exists($del_file)) {
  9625. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9626. if (file_exists($del_file_2)) {
  9627. unlink($del_file_2);
  9628. }
  9629. $this->set_preview_image('');
  9630. return @unlink($del_file);
  9631. }
  9632. }
  9633. return false;
  9634. }
  9635. /**
  9636. * Uploads an author image to the upload/learning_path/images directory
  9637. * @param array The image array, coming from the $_FILES superglobal
  9638. * @return boolean True on success, false on error
  9639. */
  9640. public function upload_image($image_array)
  9641. {
  9642. if (!empty ($image_array['name'])) {
  9643. $upload_ok = process_uploaded_file($image_array);
  9644. $has_attachment = true;
  9645. }
  9646. if ($upload_ok) {
  9647. if ($has_attachment) {
  9648. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9649. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9650. $updir = $sys_course_path . $courseDir;
  9651. // Try to add an extension to the file if it hasn't one.
  9652. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9653. if (filter_extension($new_file_name)) {
  9654. $file_extension = explode('.', $image_array['name']);
  9655. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9656. $filename = uniqid('');
  9657. $new_file_name = $filename.'.'.$file_extension;
  9658. $new_path = $updir.'/'.$new_file_name;
  9659. // Resize the image.
  9660. $temp = new Image($image_array['tmp_name']);
  9661. $temp->resize(104);
  9662. $result = $temp->send_image($new_path);
  9663. // Storing the image filename.
  9664. if ($result) {
  9665. $this->set_preview_image($new_file_name);
  9666. //Resize to 64px to use on course homepage
  9667. $temp->resize(64);
  9668. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9669. return true;
  9670. }
  9671. }
  9672. }
  9673. }
  9674. return false;
  9675. }
  9676. /**
  9677. * @param int $lp_id
  9678. * @param string $status
  9679. */
  9680. public function set_autolaunch($lp_id, $status)
  9681. {
  9682. $course_id = api_get_course_int_id();
  9683. $lp_id = intval($lp_id);
  9684. $status = intval($status);
  9685. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9686. // Setting everything to autolaunch = 0
  9687. $attributes['autolaunch'] = 0;
  9688. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9689. Database::update($lp_table, $attributes, $where);
  9690. if ($status == 1) {
  9691. //Setting my lp_id to autolaunch = 1
  9692. $attributes['autolaunch'] = 1;
  9693. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9694. Database::update($lp_table, $attributes, $where );
  9695. }
  9696. }
  9697. /**
  9698. * Gets previous_item_id for the next element of the lp_item table
  9699. * @author Isaac flores paz
  9700. * @return integer Previous item ID
  9701. */
  9702. public function select_previous_item_id()
  9703. {
  9704. $course_id = api_get_course_int_id();
  9705. if ($this->debug > 0) {
  9706. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9707. }
  9708. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9709. // Get the max order of the items
  9710. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9711. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9712. $rs_max_order = Database::query($sql);
  9713. $row_max_order = Database::fetch_object($rs_max_order);
  9714. $max_order = $row_max_order->display_order;
  9715. // Get the previous item ID
  9716. $sql = "SELECT id as previous FROM $table_lp_item
  9717. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9718. $rs_max = Database::query($sql);
  9719. $row_max = Database::fetch_object($rs_max);
  9720. // Return the previous item ID
  9721. return $row_max->previous;
  9722. }
  9723. /**
  9724. * Copies an LP
  9725. */
  9726. public function copy()
  9727. {
  9728. // Course builder
  9729. $cb = new CourseBuilder();
  9730. //Setting tools that will be copied
  9731. $cb->set_tools_to_build(array('learnpaths'));
  9732. //Setting elements that will be copied
  9733. $cb->set_tools_specific_id_list(
  9734. array('learnpaths' => array($this->lp_id))
  9735. );
  9736. $course = $cb->build();
  9737. //Course restorer
  9738. $course_restorer = new CourseRestorer($course);
  9739. $course_restorer->set_add_text_in_items(true);
  9740. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9741. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9742. }
  9743. public function verify_document_size($s)
  9744. {
  9745. $post_max = ini_get('post_max_size');
  9746. if (substr($post_max, -1, 1) == 'M') {
  9747. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9748. } elseif (substr($post_max, -1, 1) == 'G') {
  9749. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9750. }
  9751. $upl_max = ini_get('upload_max_filesize');
  9752. if (substr($upl_max, -1, 1) == 'M') {
  9753. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9754. } elseif (substr($upl_max, -1, 1) == 'G') {
  9755. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9756. }
  9757. $documents_total_space = DocumentManager::documents_total_space();
  9758. $course_max_space = DocumentManager::get_course_quota();
  9759. $total_size = filesize($s) + $documents_total_space;
  9760. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9761. return true;
  9762. } else{
  9763. return false;
  9764. }
  9765. }
  9766. /**
  9767. * Clear LP prerequisites
  9768. */
  9769. public function clear_prerequisites()
  9770. {
  9771. $course_id = $this->get_course_int_id();
  9772. if ($this->debug > 0) {
  9773. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9774. }
  9775. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9776. $lp_id = $this->get_id();
  9777. //Cleaning prerequisites
  9778. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9779. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9780. Database::query($sql);
  9781. //Cleaning mastery score for exercises
  9782. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9783. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9784. Database::query($sql);
  9785. }
  9786. public function set_previous_step_as_prerequisite_for_all_items()
  9787. {
  9788. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9789. $course_id = $this->get_course_int_id();
  9790. $lp_id = $this->get_id();
  9791. if (!empty($this->items)) {
  9792. $previous_item_id = null;
  9793. $previous_item_max = 0;
  9794. $previous_item_type = null;
  9795. $last_item_not_dir = null;
  9796. $last_item_not_dir_type = null;
  9797. $last_item_not_dir_max = null;
  9798. foreach ($this->items as $item) {
  9799. // if there was a previous item... (otherwise jump to set it)
  9800. if (!empty($previous_item_id)) {
  9801. $current_item_id = $item->get_id(); //save current id
  9802. if ($item->get_type() != 'dir') {
  9803. // Current item is not a folder, so it qualifies to get a prerequisites
  9804. if ($last_item_not_dir_type == 'quiz') {
  9805. // if previous is quiz, mark its max score as default score to be achieved
  9806. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9807. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9808. Database::query($sql);
  9809. }
  9810. // now simply update the prerequisite to set it to the last non-chapter item
  9811. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9812. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9813. Database::query($sql);
  9814. // record item as 'non-chapter' reference
  9815. $last_item_not_dir = $item->get_id();
  9816. $last_item_not_dir_type = $item->get_type();
  9817. $last_item_not_dir_max = $item->get_max();
  9818. }
  9819. } else {
  9820. if ($item->get_type() != 'dir') {
  9821. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9822. $last_item_not_dir = $item->get_id();
  9823. $last_item_not_dir_type = $item->get_type();
  9824. $last_item_not_dir_max = $item->get_max();
  9825. }
  9826. }
  9827. // Saving the item as "previous item" for the next loop
  9828. $previous_item_id = $item->get_id();
  9829. $previous_item_max = $item->get_max();
  9830. $previous_item_type = $item->get_type();
  9831. }
  9832. }
  9833. }
  9834. /**
  9835. * @param array $params
  9836. */
  9837. public static function createCategory($params)
  9838. {
  9839. $em = Database::getManager();
  9840. $item = new CLpCategory();
  9841. $item->setName($params['name']);
  9842. $item->setCId($params['c_id']);
  9843. $em->persist($item);
  9844. $em->flush();
  9845. }
  9846. /**
  9847. * @param array $params
  9848. */
  9849. public static function updateCategory($params)
  9850. {
  9851. $em = Database::getManager();
  9852. /** @var CLpCategory $item */
  9853. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9854. if ($item) {
  9855. $item->setName($params['name']);
  9856. $em->merge($item);
  9857. $em->flush();
  9858. }
  9859. }
  9860. /**
  9861. * @param int $id
  9862. */
  9863. public static function moveUpCategory($id)
  9864. {
  9865. $em = Database::getManager();
  9866. /** @var CLpCategory $item */
  9867. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9868. if ($item) {
  9869. $position = $item->getPosition() - 1;
  9870. $item->setPosition($position);
  9871. $em->persist($item);
  9872. $em->flush();
  9873. }
  9874. }
  9875. /**
  9876. * @param int $id
  9877. */
  9878. public static function moveDownCategory($id)
  9879. {
  9880. $em = Database::getManager();
  9881. /** @var CLpCategory $item */
  9882. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9883. if ($item) {
  9884. $position = $item->getPosition() + 1;
  9885. $item->setPosition($position);
  9886. $em->persist($item);
  9887. $em->flush();
  9888. }
  9889. }
  9890. /**
  9891. * @param int $courseId
  9892. * @return int|mixed
  9893. */
  9894. public static function getCountCategories($courseId)
  9895. {
  9896. if (empty($course_id)) {
  9897. return 0;
  9898. }
  9899. $em = Database::getManager();
  9900. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9901. $query->setParameter('id', $courseId);
  9902. return $query->getSingleScalarResult();
  9903. }
  9904. /**
  9905. * @param int $courseId
  9906. *
  9907. * @return mixed
  9908. */
  9909. public static function getCategories($courseId)
  9910. {
  9911. $em = Database::getManager();
  9912. //Default behaviour
  9913. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9914. array('cId' => $course_id),
  9915. array('name' => 'ASC')
  9916. );*/
  9917. // Using doctrine extensions
  9918. /** @var SortableRepository $repo */
  9919. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9920. $items = $repo
  9921. ->getBySortableGroupsQuery(['cId' => $courseId])
  9922. ->getResult();
  9923. return $items;
  9924. }
  9925. /**
  9926. * @param int $id
  9927. *
  9928. * @return CLpCategory
  9929. */
  9930. public static function getCategory($id)
  9931. {
  9932. $em = Database::getManager();
  9933. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9934. return $item;
  9935. }
  9936. /**
  9937. * @param int $courseId
  9938. * @return array
  9939. */
  9940. public static function getCategoryByCourse($courseId)
  9941. {
  9942. $em = Database::getManager();
  9943. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9944. array('cId' => $courseId)
  9945. );
  9946. return $items;
  9947. }
  9948. /**
  9949. * @param int $id
  9950. *
  9951. * @return mixed
  9952. */
  9953. public static function deleteCategory($id)
  9954. {
  9955. $em = Database::getManager();
  9956. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9957. if ($item) {
  9958. $courseId = $item->getCId();
  9959. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9960. $query->setParameter('id', $courseId);
  9961. $query->setParameter('catId', $item->getId());
  9962. $lps = $query->getResult();
  9963. // Setting category = 0.
  9964. if ($lps) {
  9965. foreach ($lps as $lpItem) {
  9966. $lpItem->setCategoryId(0);
  9967. }
  9968. }
  9969. // Removing category.
  9970. $em->remove($item);
  9971. $em->flush();
  9972. }
  9973. }
  9974. /**
  9975. * @param int $courseId
  9976. * @param bool $addSelectOption
  9977. *
  9978. * @return mixed
  9979. */
  9980. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9981. {
  9982. $items = self::getCategoryByCourse($courseId);
  9983. $cats = array();
  9984. if ($addSelectOption) {
  9985. $cats = array(get_lang('SelectACategory'));
  9986. }
  9987. if (!empty($items)) {
  9988. foreach ($items as $cat) {
  9989. $cats[$cat->getId()] = $cat->getName();
  9990. }
  9991. }
  9992. return $cats;
  9993. }
  9994. /**
  9995. * Return the scorm item type object with spaces replaced with _
  9996. * The return result is use to build a css classname like scorm_type_$return
  9997. * @param $in_type
  9998. * @return mixed
  9999. */
  10000. private static function format_scorm_type_item($in_type)
  10001. {
  10002. return str_replace(' ', '_', $in_type);
  10003. }
  10004. /**
  10005. * @param string $courseCode
  10006. * @param int $lp_id
  10007. * @param int $user_id
  10008. *
  10009. * @return learnpath
  10010. */
  10011. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  10012. {
  10013. $learnPath = null;
  10014. $lpObject = Session::read('lpobject');
  10015. if ($lpObject !== null) {
  10016. $learnPath = unserialize($lpObject);
  10017. }
  10018. if (!is_object($learnPath)) {
  10019. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  10020. }
  10021. return $learnPath;
  10022. }
  10023. /**
  10024. * @param int $itemId
  10025. * @return learnpathItem|false
  10026. */
  10027. public function getItem($itemId)
  10028. {
  10029. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  10030. return $this->items[$itemId];
  10031. }
  10032. return false;
  10033. }
  10034. /**
  10035. * @return int
  10036. */
  10037. public function getCategoryId()
  10038. {
  10039. return $this->categoryId;
  10040. }
  10041. /**
  10042. * @param int $categoryId
  10043. * @return bool
  10044. */
  10045. public function setCategoryId($categoryId)
  10046. {
  10047. $this->categoryId = intval($categoryId);
  10048. $courseId = api_get_course_int_id();
  10049. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10050. $lp_id = $this->get_id();
  10051. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  10052. WHERE c_id = $courseId AND id = $lp_id";
  10053. Database::query($sql);
  10054. return true;
  10055. }
  10056. /**
  10057. * Get whether this is a learning path with the possibility to subscribe
  10058. * users or not
  10059. * @return int
  10060. */
  10061. public function getSubscribeUsers()
  10062. {
  10063. return $this->subscribeUsers;
  10064. }
  10065. /**
  10066. * Set whether this is a learning path with the possibility to subscribe
  10067. * users or not
  10068. * @param int $subscribeUsers (0 = false, 1 = true)
  10069. */
  10070. public function setSubscribeUsers($value)
  10071. {
  10072. if ($this->debug > 0) {
  10073. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  10074. }
  10075. $this->subscribeUsers = intval($value);;
  10076. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10077. $lp_id = $this->get_id();
  10078. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  10079. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10080. Database::query($sql);
  10081. return true;
  10082. }
  10083. /**
  10084. * Calculate the count of stars for a user in this LP
  10085. * This calculation is based on the following rules:
  10086. * - the student gets one star when he gets to 50% of the learning path
  10087. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  10088. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  10089. * - the student gets the final star when the score for the *last* test is >= 80%
  10090. * @param int $sessionId Optional. The session ID
  10091. * @return int The count of stars
  10092. */
  10093. public function getCalculateStars($sessionId = 0)
  10094. {
  10095. $stars = 0;
  10096. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  10097. if ($progress >= 50) {
  10098. $stars++;
  10099. }
  10100. // Calculate stars chapters evaluation
  10101. $exercisesItems = $this->getExercisesItems();
  10102. if (!empty($exercisesItems)) {
  10103. $totalResult = 0;
  10104. foreach ($exercisesItems as $exerciseItem) {
  10105. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10106. $this->user_id,
  10107. $exerciseItem->path,
  10108. $this->course_int_id,
  10109. $sessionId,
  10110. $this->lp_id,
  10111. $exerciseItem->db_id
  10112. );
  10113. $exerciseResultInfo = end($exerciseResultInfo);
  10114. if (!$exerciseResultInfo) {
  10115. continue;
  10116. }
  10117. if (!empty($exerciseResultInfo['exe_weighting'])) {
  10118. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  10119. } else {
  10120. $exerciseResult = 0;
  10121. }
  10122. $totalResult += $exerciseResult;
  10123. }
  10124. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  10125. if ($totalExerciseAverage >= 50) {
  10126. $stars++;
  10127. }
  10128. if ($totalExerciseAverage >= 80) {
  10129. $stars++;
  10130. }
  10131. }
  10132. // Calculate star for final evaluation
  10133. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10134. if (!empty($finalEvaluationItem)) {
  10135. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10136. $this->user_id,
  10137. $finalEvaluationItem->path,
  10138. $this->course_int_id,
  10139. $sessionId,
  10140. $this->lp_id,
  10141. $finalEvaluationItem->db_id
  10142. );
  10143. $evaluationResultInfo = end($evaluationResultInfo);
  10144. if ($evaluationResultInfo) {
  10145. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  10146. if ($evaluationResult >= 80) {
  10147. $stars++;
  10148. }
  10149. }
  10150. }
  10151. return $stars;
  10152. }
  10153. /**
  10154. * Get the items of exercise type
  10155. * @return array The items. Otherwise return false
  10156. */
  10157. public function getExercisesItems()
  10158. {
  10159. $exercises = [];
  10160. foreach ($this->items as $item) {
  10161. if ($item->type != 'quiz') {
  10162. continue;
  10163. }
  10164. $exercises[] = $item;
  10165. }
  10166. array_pop($exercises);
  10167. return $exercises;
  10168. }
  10169. /**
  10170. * Get the item of exercise type (evaluation type)
  10171. * @return array The final evaluation. Otherwise return false
  10172. */
  10173. public function getFinalEvaluationItem()
  10174. {
  10175. $exercises = [];
  10176. foreach ($this->items as $item) {
  10177. if ($item->type != 'quiz') {
  10178. continue;
  10179. }
  10180. $exercises[] = $item;
  10181. }
  10182. return array_pop($exercises);
  10183. }
  10184. /**
  10185. * Calculate the total points achieved for the current user in this learning path
  10186. * @param int $sessionId Optional. The session Id
  10187. * @return int
  10188. */
  10189. public function getCalculateScore($sessionId = 0)
  10190. {
  10191. // Calculate stars chapters evaluation
  10192. $exercisesItems = $this->getExercisesItems();
  10193. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10194. $totalExercisesResult = 0;
  10195. $totalEvaluationResult = 0;
  10196. if ($exercisesItems !== false) {
  10197. foreach ($exercisesItems as $exerciseItem) {
  10198. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10199. $this->user_id,
  10200. $exerciseItem->path,
  10201. $this->course_int_id,
  10202. $sessionId,
  10203. $this->lp_id,
  10204. $exerciseItem->db_id
  10205. );
  10206. $exerciseResultInfo = end($exerciseResultInfo);
  10207. if (!$exerciseResultInfo) {
  10208. continue;
  10209. }
  10210. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  10211. }
  10212. }
  10213. if (!empty($finalEvaluationItem)) {
  10214. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10215. $this->user_id,
  10216. $finalEvaluationItem->path,
  10217. $this->course_int_id,
  10218. $sessionId,
  10219. $this->lp_id,
  10220. $finalEvaluationItem->db_id
  10221. );
  10222. $evaluationResultInfo = end($evaluationResultInfo);
  10223. if ($evaluationResultInfo) {
  10224. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  10225. }
  10226. }
  10227. return $totalExercisesResult + $totalEvaluationResult;
  10228. }
  10229. /**
  10230. * Check if URL is not allowed to be show in a iframe
  10231. * @param string $src
  10232. *
  10233. * @return string
  10234. */
  10235. public function fixBlockedLinks($src)
  10236. {
  10237. $urlInfo = parse_url($src);
  10238. $platformProtocol = 'https';
  10239. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  10240. $platformProtocol = 'http';
  10241. }
  10242. $protocolFixApplied = false;
  10243. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  10244. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  10245. if ($platformProtocol != $scheme) {
  10246. Session::write('x_frame_source', $src);
  10247. $src = 'blank.php?error=x_frames_options';
  10248. $protocolFixApplied = true;
  10249. }
  10250. if ($protocolFixApplied == false) {
  10251. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  10252. // Check X-Frame-Options
  10253. $ch = curl_init();
  10254. $options = array(
  10255. CURLOPT_URL => $src,
  10256. CURLOPT_RETURNTRANSFER => true,
  10257. CURLOPT_HEADER => true,
  10258. CURLOPT_FOLLOWLOCATION => true,
  10259. CURLOPT_ENCODING => "",
  10260. CURLOPT_AUTOREFERER => true,
  10261. CURLOPT_CONNECTTIMEOUT => 120,
  10262. CURLOPT_TIMEOUT => 120,
  10263. CURLOPT_MAXREDIRS => 10,
  10264. );
  10265. curl_setopt_array($ch, $options);
  10266. $response = curl_exec($ch);
  10267. $httpCode = curl_getinfo($ch);
  10268. $headers = substr($response, 0, $httpCode['header_size']);
  10269. $error = false;
  10270. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  10271. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  10272. ) {
  10273. $error = true;
  10274. }
  10275. if ($error) {
  10276. Session::write('x_frame_source', $src);
  10277. $src = 'blank.php?error=x_frames_options';
  10278. }
  10279. }
  10280. }
  10281. return $src;
  10282. }
  10283. /**
  10284. * Check if this LP has a created forum in the basis course
  10285. * @return boolean
  10286. */
  10287. public function lpHasForum()
  10288. {
  10289. $forumTable = Database::get_course_table(TABLE_FORUM);
  10290. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10291. $fakeFrom = "
  10292. $forumTable f
  10293. INNER JOIN $itemProperty ip
  10294. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  10295. ";
  10296. $resultData = Database::select(
  10297. 'COUNT(f.iid) AS qty',
  10298. $fakeFrom,
  10299. [
  10300. 'where' => [
  10301. 'ip.visibility != ? AND ' => 2,
  10302. 'ip.tool = ? AND ' => TOOL_FORUM,
  10303. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10304. 'f.lp_id = ?' => intval($this->lp_id)
  10305. ]
  10306. ],
  10307. 'first'
  10308. );
  10309. return $resultData['qty'] > 0;
  10310. }
  10311. /**
  10312. * Get the forum for this learning path
  10313. * @return boolean
  10314. */
  10315. public function getForum($sessionId = 0)
  10316. {
  10317. $forumTable = Database::get_course_table(TABLE_FORUM);
  10318. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10319. $fakeFrom = "$forumTable f
  10320. INNER JOIN $itemProperty ip ";
  10321. if ($this->lp_session_id == 0) {
  10322. $fakeFrom .= "
  10323. ON (
  10324. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  10325. f.session_id = ip.session_id OR ip.session_id IS NULL
  10326. )
  10327. )
  10328. ";
  10329. } else {
  10330. $fakeFrom .= "
  10331. ON (
  10332. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  10333. )
  10334. ";
  10335. }
  10336. $resultData = Database::select(
  10337. 'f.*',
  10338. $fakeFrom,
  10339. [
  10340. 'where' => [
  10341. 'ip.visibility != ? AND ' => 2,
  10342. 'ip.tool = ? AND ' => TOOL_FORUM,
  10343. 'f.session_id = ? AND ' => $sessionId,
  10344. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10345. 'f.lp_id = ?' => intval($this->lp_id)
  10346. ]
  10347. ],
  10348. 'first'
  10349. );
  10350. if (empty($resultData)) {
  10351. return false;
  10352. }
  10353. return $resultData;
  10354. }
  10355. /**
  10356. * Create a forum for this learning path
  10357. * @param type $forumCategoryId
  10358. * @return int The forum ID if was created. Otherwise return false
  10359. */
  10360. public function createForum($forumCategoryId)
  10361. {
  10362. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  10363. $forumId = store_forum(
  10364. [
  10365. 'lp_id' => $this->lp_id,
  10366. 'forum_title' => $this->name,
  10367. 'forum_comment' => null,
  10368. 'forum_category' => intval($forumCategoryId),
  10369. 'students_can_edit_group' => ['students_can_edit' => 0],
  10370. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  10371. 'default_view_type_group' => ['default_view_type' => 'flat'],
  10372. 'group_forum' => 0,
  10373. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  10374. ],
  10375. [],
  10376. true
  10377. );
  10378. return $forumId;
  10379. }
  10380. /**
  10381. * Check and obtain the lp final item if exist
  10382. *
  10383. * @return learnpathItem
  10384. */
  10385. private function getFinalItem()
  10386. {
  10387. if (empty($this->items)) {
  10388. return null;
  10389. }
  10390. foreach ($this->items as $item) {
  10391. if ($item->type !== 'final_item') {
  10392. continue;
  10393. }
  10394. return $item;
  10395. }
  10396. }
  10397. /**
  10398. * Get the LP Final Item Template
  10399. *
  10400. * @return string
  10401. */
  10402. private function getFinalItemTemplate()
  10403. {
  10404. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'lp/final_item_template/template.html');
  10405. }
  10406. /**
  10407. * Get the LP Final Item Url
  10408. *
  10409. * @return string
  10410. */
  10411. private function getSavedFinalItem()
  10412. {
  10413. $finalItem = $this->getFinalItem();
  10414. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10415. if ($doc && file_exists($doc['absolute_path'])) {
  10416. return file_get_contents($doc['absolute_path']);
  10417. }
  10418. return '';
  10419. }
  10420. /**
  10421. * Get the LP Final Item form
  10422. *
  10423. * @return string
  10424. */
  10425. public function getFinalItemForm()
  10426. {
  10427. $finalItem = $this->getFinalItem();
  10428. $title = '';
  10429. if ($finalItem) {
  10430. $title = $finalItem->get_title();
  10431. $buttonText = get_lang('Save');
  10432. $content = $this->getSavedFinalItem();
  10433. } else {
  10434. $buttonText = get_lang('LPCreateDocument');
  10435. $content = $this->getFinalItemTemplate();
  10436. }
  10437. $courseInfo = api_get_course_info();
  10438. $result = $this->generate_lp_folder($courseInfo);
  10439. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10440. $relative_prefix = '../../';
  10441. $editorConfig = [
  10442. 'ToolbarSet' => 'LearningPathDocuments',
  10443. 'Width' => '100%',
  10444. 'Height' => '500',
  10445. 'FullPage' => true,
  10446. 'CreateDocumentDir' => $relative_prefix,
  10447. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  10448. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  10449. ];
  10450. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  10451. 'type' => 'document',
  10452. 'lp_id' => $this->lp_id
  10453. ]);
  10454. $form = new FormValidator('final_item', 'POST', $url);
  10455. $form->addText('title', get_lang('Title'));
  10456. $form->addButtonSave($buttonText);
  10457. $form->addHtml(
  10458. Display::return_message(
  10459. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10460. 'normal',
  10461. false
  10462. )
  10463. );
  10464. $renderer = $form->defaultRenderer();
  10465. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10466. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10467. $form->addHidden('action', 'add_final_item');
  10468. $form->addHidden('path', Session::read('pathItem'));
  10469. $form->addHidden('previous', $this->get_last());
  10470. $form->setDefaults(
  10471. ['title' => $title, 'content_lp_certificate' => $content]
  10472. );
  10473. if ($form->validate()) {
  10474. $values = $form->exportValues();
  10475. $lastItemId = $this->get_last();
  10476. if (!$finalItem) {
  10477. $documentId = $this->create_document(
  10478. $this->course_info,
  10479. $values['content_lp_certificate'],
  10480. $values['title']
  10481. );
  10482. $this->add_item(
  10483. 0,
  10484. $lastItemId,
  10485. 'final_item',
  10486. $documentId,
  10487. $values['title'],
  10488. ''
  10489. );
  10490. Display::addFlash(
  10491. Display::return_message(get_lang('Added'))
  10492. );
  10493. } else {
  10494. $this->edit_document($this->course_info);
  10495. }
  10496. }
  10497. return $form->returnForm();
  10498. }
  10499. /**
  10500. * Check if the current lp item is first, both, last or none from lp list
  10501. *
  10502. * @param int $currentItemId
  10503. * @return string
  10504. */
  10505. public function isFirstOrLastItem($currentItemId)
  10506. {
  10507. if ($this->debug > 0) {
  10508. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10509. }
  10510. $lpItemId = [];
  10511. $typeListNotToVerify = self::getChapterTypes();
  10512. // Using get_toc() function instead $this->items because returns the correct order of the items
  10513. foreach ($this->get_toc() as $item) {
  10514. if (!in_array($item['type'], $typeListNotToVerify)) {
  10515. $lpItemId[] = $item['id'];
  10516. }
  10517. }
  10518. $lastLpItemIndex = count($lpItemId) - 1;
  10519. $position = array_search($currentItemId, $lpItemId);
  10520. switch ($position) {
  10521. case 0:
  10522. if (!$lastLpItemIndex) {
  10523. $answer = 'both';
  10524. break;
  10525. }
  10526. $answer = 'first';
  10527. break;
  10528. case $lastLpItemIndex:
  10529. $answer = 'last';
  10530. break;
  10531. default:
  10532. $answer = 'none';
  10533. }
  10534. return $answer;
  10535. }
  10536. /**
  10537. * Get whether this is a learning path with the accumulated SCORM time or not
  10538. * @return int
  10539. */
  10540. public function getAccumulateScormTime()
  10541. {
  10542. return $this->accumulateScormTime;
  10543. }
  10544. /**
  10545. * Set whether this is a learning path with the accumulated SCORM time or not
  10546. * @param int $value (0 = false, 1 = true)
  10547. * @return bool Always returns true
  10548. */
  10549. public function setAccumulateScormTime($value)
  10550. {
  10551. if ($this->debug > 0) {
  10552. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10553. }
  10554. $this->accumulateScormTime = intval($value);
  10555. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10556. $lp_id = $this->get_id();
  10557. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10558. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10559. Database::query($sql);
  10560. return true;
  10561. }
  10562. /**
  10563. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10564. * the new learning path tool.
  10565. *
  10566. * The function is a big switch on tool type.
  10567. * In each case, we query the corresponding table for information and build the link
  10568. * with that information.
  10569. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on
  10570. * previous work (display_addedresource_link_in_learnpath())
  10571. * @param int $course_id Course code
  10572. * @param int $learningPathId The learning path ID (in lp table)
  10573. * @param int $id_in_path the unique index in the items table
  10574. * @param int $lpViewId
  10575. * @param string $origin
  10576. * @return string
  10577. */
  10578. public static function rl_get_resource_link_for_learnpath(
  10579. $course_id,
  10580. $learningPathId,
  10581. $id_in_path,
  10582. $lpViewId,
  10583. $origin = 'learnpath'
  10584. ) {
  10585. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10586. $course_info = api_get_course_info_by_id($course_id);
  10587. $course_id = $course_info['real_id'];
  10588. $course_code = $course_info['code'];
  10589. $session_id = api_get_session_id();
  10590. $learningPathId = intval($learningPathId);
  10591. $id_in_path = intval($id_in_path);
  10592. $lpViewId = intval($lpViewId);
  10593. $em = Database::getManager();
  10594. $sql = "SELECT * FROM $tbl_lp_item
  10595. WHERE
  10596. c_id = $course_id AND
  10597. lp_id = $learningPathId AND
  10598. id = $id_in_path
  10599. ";
  10600. $res_item = Database::query($sql);
  10601. if (Database::num_rows($res_item) < 1) {
  10602. return -1;
  10603. }
  10604. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10605. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10606. // Get the lp_item_view with the highest view_count.
  10607. $sql = "SELECT * FROM $item_view_table
  10608. WHERE
  10609. c_id = $course_id AND
  10610. lp_item_id = ".$row_item['id']." AND
  10611. lp_view_id = $lpViewId
  10612. ORDER BY view_count DESC";
  10613. $learnpathItemViewResult = Database::query($sql);
  10614. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10615. $learnpathItemViewId = 0;
  10616. if (isset($learnpathItemViewData)) {
  10617. $learnpathItemViewId = $learnpathItemViewData['id'];
  10618. }
  10619. $type = strtolower($row_item['item_type']);
  10620. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10621. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10622. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10623. $link = '';
  10624. $extraParams = "origin=$origin&cidReq=$course_code&session_id=$session_id&id_session=$session_id";
  10625. switch ($type) {
  10626. case 'dir':
  10627. $link .= $main_dir_path.'lp/blank.php';
  10628. break;
  10629. case TOOL_CALENDAR_EVENT:
  10630. $link .= $main_dir_path.'calendar/agenda.php?agenda_id='.$id.'&'.$extraParams;
  10631. break;
  10632. case TOOL_ANNOUNCEMENT:
  10633. $link .= $main_dir_path.'announcements/announcements.php?ann_id='.$id.'&'.$extraParams;
  10634. break;
  10635. case TOOL_LINK:
  10636. $link .= $main_dir_path.'link/link_goto.php?link_id='.$id.'&'.$extraParams;
  10637. break;
  10638. case TOOL_QUIZ:
  10639. if (!empty($id)) {
  10640. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10641. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10642. $result= Database::query($sql);
  10643. $myrow = Database::fetch_array($result);
  10644. if ($row_item['title'] != '') {
  10645. $myrow['title'] = $row_item['title'];
  10646. }
  10647. $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;
  10648. }
  10649. break;
  10650. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  10651. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10652. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10653. $myrow = Database::fetch_array($result);
  10654. $path = $myrow['path'];
  10655. $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;
  10656. break;
  10657. case TOOL_FORUM:
  10658. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&'.$extraParams;
  10659. break;
  10660. case TOOL_THREAD: //forum post
  10661. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10662. if (!empty($id)) {
  10663. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10664. $result = Database::query($sql);
  10665. $myrow = Database::fetch_array($result);
  10666. $link .= $main_dir_path.'forum/viewthread.php?thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10667. }
  10668. break;
  10669. case TOOL_POST:
  10670. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10671. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10672. $myrow = Database::fetch_array($result);
  10673. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10674. break;
  10675. case TOOL_DOCUMENT:
  10676. $document = $em
  10677. ->getRepository('ChamiloCourseBundle:CDocument')
  10678. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10679. if (!$document) {
  10680. break;
  10681. }
  10682. $documentPathInfo = pathinfo($document->getPath());
  10683. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10684. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10685. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10686. if ($showDirectUrl) {
  10687. $link = $main_course_path.'document'.$document->getPath().'?'.$extraParams;
  10688. } else {
  10689. $link = api_get_path(WEB_CODE_PATH).'document/showinframes.php?id='.$id.'&'.$extraParams;
  10690. }
  10691. $openmethod = 2;
  10692. $officedoc = false;
  10693. Session::write('openmethod',$openmethod);
  10694. Session::write('officedoc',$officedoc);
  10695. break;
  10696. case TOOL_LP_FINAL_ITEM:
  10697. $link .= api_get_path(WEB_CODE_PATH).'lp/lp_final_item.php?&id='.$id.'&lp_id='.$learningPathId.'&'.$extraParams;
  10698. break;
  10699. case 'assignments':
  10700. $link .= $main_dir_path.'work/work.php?'.$extraParams;
  10701. break;
  10702. case TOOL_DROPBOX:
  10703. $link .= $main_dir_path.'dropbox/index.php?'.$extraParams;
  10704. break;
  10705. case 'introduction_text': //DEPRECATED
  10706. $link .= '';
  10707. break;
  10708. case TOOL_COURSE_DESCRIPTION:
  10709. $link .= $main_dir_path.'course_description?'.$extraParams;
  10710. break;
  10711. case TOOL_GROUP:
  10712. $link .= $main_dir_path.'group/group.php?'.$extraParams;
  10713. break;
  10714. case TOOL_USER:
  10715. $link .= $main_dir_path.'user/user.php?'.$extraParams;
  10716. break;
  10717. case TOOL_STUDENTPUBLICATION:
  10718. if (!empty($row_item['path'])) {
  10719. $link .= $main_dir_path.'work/work_list.php?id='.$row_item['path'].'&'.$extraParams;
  10720. break;
  10721. }
  10722. $link .= $main_dir_path.'work/work.php?'.api_get_cidreq().'&id='.$row_item['path'].'&'.$extraParams;
  10723. break;
  10724. } //end switch
  10725. return $link;
  10726. }
  10727. /**
  10728. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10729. *
  10730. * @author Yannick Warnier <ywarnier@beeznest.org>
  10731. * @param string Course code
  10732. * @param string The tool type (using constants declared in main_api.lib.php)
  10733. * @param integer The resource ID
  10734. */
  10735. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10736. {
  10737. $_course = api_get_course_info($course_code);
  10738. $course_id = $_course['real_id'];
  10739. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10740. $learningPathId = intval($learningPathId);
  10741. $id_in_path = intval($id_in_path);
  10742. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10743. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10744. $res_item = Database::query($sql_item);
  10745. if (Database::num_rows($res_item) < 1) {
  10746. return '';
  10747. }
  10748. $row_item = Database::fetch_array($res_item);
  10749. $type = strtolower($row_item['item_type']);
  10750. $id = $row_item['ref'];
  10751. $output = '';
  10752. switch ($type) {
  10753. case TOOL_CALENDAR_EVENT:
  10754. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10755. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10756. $myrow = Database::fetch_array($result);
  10757. $output = $myrow['title'];
  10758. break;
  10759. case TOOL_ANNOUNCEMENT:
  10760. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10761. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10762. $myrow = Database::fetch_array($result);
  10763. $output = $myrow['title'];
  10764. break;
  10765. case TOOL_LINK:
  10766. // Doesn't take $target into account.
  10767. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10768. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10769. $myrow = Database::fetch_array($result);
  10770. $output = $myrow['title'];
  10771. break;
  10772. case TOOL_QUIZ:
  10773. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10774. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10775. $myrow = Database::fetch_array($result);
  10776. $output = $myrow['title'];
  10777. break;
  10778. case TOOL_FORUM:
  10779. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10780. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10781. $myrow = Database::fetch_array($result);
  10782. $output = $myrow['forum_name'];
  10783. break;
  10784. case TOOL_THREAD: //=topics
  10785. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10786. // Grabbing the title of the post.
  10787. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10788. $result_title = Database::query($sql_title);
  10789. $myrow_title = Database::fetch_array($result_title);
  10790. $output = $myrow_title['post_title'];
  10791. break;
  10792. case TOOL_POST:
  10793. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10794. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10795. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10796. $result = Database::query($sql);
  10797. $post = Database::fetch_array($result);
  10798. $output = $post['post_title'];
  10799. break;
  10800. case 'dir':
  10801. $title = $row_item['title'];
  10802. if (!empty($title)) {
  10803. $output = $title;
  10804. } else {
  10805. $output = '-';
  10806. }
  10807. break;
  10808. case TOOL_DOCUMENT:
  10809. $title = $row_item['title'];
  10810. if (!empty($title)) {
  10811. $output = $title;
  10812. } else {
  10813. $output = '-';
  10814. }
  10815. break;
  10816. case 'hotpotatoes':
  10817. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10818. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10819. $myrow = Database::fetch_array($result);
  10820. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10821. $last = count($pathname) - 1; // Making a correct name for the link.
  10822. $filename = $pathname[$last]; // Making a correct name for the link.
  10823. $ext = explode('.', $filename);
  10824. $ext = strtolower($ext[sizeof($ext) - 1]);
  10825. $myrow['path'] = rawurlencode($myrow['path']);
  10826. $output = $filename;
  10827. break;
  10828. }
  10829. return stripslashes($output);
  10830. }
  10831. }
  10832. if (!function_exists('trim_value')) {
  10833. function trim_value(& $value) {
  10834. $value = trim($value);
  10835. }
  10836. }