learnpath.class.php 510 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Repository\CourseRepository;
  4. use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  7. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  8. use Chamilo\CourseBundle\Entity\CDocument;
  9. use Chamilo\CourseBundle\Entity\CItemProperty;
  10. use Chamilo\CourseBundle\Entity\CLp;
  11. use Chamilo\CourseBundle\Entity\CLpCategory;
  12. use Chamilo\CourseBundle\Entity\CLpItem;
  13. use Chamilo\CourseBundle\Entity\CLpItemView;
  14. use Chamilo\CourseBundle\Entity\CTool;
  15. use Chamilo\UserBundle\Entity\User;
  16. use ChamiloSession as Session;
  17. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\Finder\Finder;
  20. /**
  21. * Class learnpath
  22. * This class defines the parent attributes and methods for Chamilo learnpaths
  23. * and SCORM learnpaths. It is used by the scorm class.
  24. *
  25. * @todo decouple class
  26. *
  27. * @package chamilo.learnpath
  28. *
  29. * @author Yannick Warnier <ywarnier@beeznest.org>
  30. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  31. */
  32. class learnpath
  33. {
  34. const MAX_LP_ITEM_TITLE_LENGTH = 32;
  35. public $attempt = 0; // The number for the current ID view.
  36. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  37. public $current; // Id of the current item the user is viewing.
  38. public $current_score; // The score of the current item.
  39. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  40. public $current_time_stop; // The time the user closed this resource.
  41. public $default_status = 'not attempted';
  42. public $encoding = 'UTF-8';
  43. public $error = '';
  44. public $force_commit = false; // For SCORM only- if true will send a scorm LMSCommit() request on each LMSSetValue()
  45. public $index; // The index of the active learnpath_item in $ordered_items array.
  46. public $items = [];
  47. public $last; // item_id of last item viewed in the learning path.
  48. public $last_item_seen = 0; // In case we have already come in this lp, reuse the last item seen if authorized.
  49. public $license; // Which license this course has been given - not used yet on 20060522.
  50. public $lp_id; // DB iid for this learnpath.
  51. public $lp_view_id; // DB ID for lp_view
  52. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  53. public $message = '';
  54. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  55. public $name; // Learnpath name (they generally have one).
  56. public $ordered_items = []; // List of the learnpath items in the order they are to be read.
  57. public $path = ''; // Path inside the scorm directory (if scorm).
  58. public $theme; // The current theme of the learning path.
  59. public $preview_image; // The current image of the learning path.
  60. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  61. public $accumulateWorkTime; // The min time of learnpath
  62. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  63. public $prevent_reinit = 1;
  64. // Describes the mode of progress bar display.
  65. public $seriousgame_mode = 0;
  66. public $progress_bar_mode = '%';
  67. // Percentage progress as saved in the db.
  68. public $progress_db = 0;
  69. public $proximity; // Wether the content is distant or local or unknown.
  70. public $refs_list = []; //list of items by ref => db_id. Used only for prerequisites match.
  71. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  72. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  73. public $type; //type of learnpath. Could be 'chamilo', 'scorm', 'scorm2004', 'aicc', ...
  74. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  75. public $user_id; //ID of the user that is viewing/using the course
  76. public $update_queue = [];
  77. public $scorm_debug = 0;
  78. public $arrMenu = []; // Array for the menu items.
  79. public $debug = 0; // Logging level.
  80. public $lp_session_id = 0;
  81. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  82. public $prerequisite = 0;
  83. public $use_max_score = 1; // 1 or 0
  84. public $subscribeUsers = 0; // Subscribe users or not
  85. public $created_on = '';
  86. public $modified_on = '';
  87. public $publicated_on = '';
  88. public $expired_on = '';
  89. public $ref = null;
  90. public $course_int_id;
  91. public $course_info = [];
  92. public $categoryId;
  93. /**
  94. * Constructor.
  95. * Needs a database handler, a course code and a learnpath id from the database.
  96. * Also builds the list of items into $this->items.
  97. *
  98. * @param string $course Course code
  99. * @param int $lp_id c_lp.iid
  100. * @param int $user_id
  101. */
  102. public function __construct($course, $lp_id, $user_id)
  103. {
  104. $debug = $this->debug;
  105. $this->encoding = api_get_system_encoding();
  106. if ($debug) {
  107. error_log('In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')');
  108. }
  109. if (empty($course)) {
  110. $course = api_get_course_id();
  111. }
  112. $course_info = api_get_course_info($course);
  113. if (!empty($course_info)) {
  114. $this->cc = $course_info['code'];
  115. $this->course_info = $course_info;
  116. $course_id = $course_info['real_id'];
  117. } else {
  118. $this->error = 'Course code does not exist in database.';
  119. }
  120. $lp_id = (int) $lp_id;
  121. $course_id = (int) $course_id;
  122. $this->set_course_int_id($course_id);
  123. // Check learnpath ID.
  124. if (empty($lp_id) || empty($course_id)) {
  125. $this->error = "Parameter is empty: LpId:'$lp_id', courseId: '$lp_id'";
  126. } else {
  127. // TODO: Make it flexible to use any course_code (still using env course code here).
  128. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  129. $sql = "SELECT * FROM $lp_table
  130. WHERE iid = $lp_id";
  131. if ($debug) {
  132. error_log('learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  133. }
  134. $res = Database::query($sql);
  135. if (Database::num_rows($res) > 0) {
  136. $this->lp_id = $lp_id;
  137. $row = Database::fetch_array($res);
  138. $this->type = $row['lp_type'];
  139. $this->name = stripslashes($row['name']);
  140. $this->proximity = $row['content_local'];
  141. $this->theme = $row['theme'];
  142. $this->maker = $row['content_maker'];
  143. $this->prevent_reinit = $row['prevent_reinit'];
  144. $this->seriousgame_mode = $row['seriousgame_mode'];
  145. $this->license = $row['content_license'];
  146. $this->scorm_debug = $row['debug'];
  147. $this->js_lib = $row['js_lib'];
  148. $this->path = $row['path'];
  149. $this->preview_image = $row['preview_image'];
  150. $this->author = $row['author'];
  151. $this->hide_toc_frame = $row['hide_toc_frame'];
  152. $this->lp_session_id = $row['session_id'];
  153. $this->use_max_score = $row['use_max_score'];
  154. $this->subscribeUsers = $row['subscribe_users'];
  155. $this->created_on = $row['created_on'];
  156. $this->modified_on = $row['modified_on'];
  157. $this->ref = $row['ref'];
  158. $this->categoryId = $row['category_id'];
  159. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  160. $this->accumulateWorkTime = isset($row['accumulate_work_time']) ? $row['accumulate_work_time'] : 0;
  161. if (!empty($row['publicated_on'])) {
  162. $this->publicated_on = $row['publicated_on'];
  163. }
  164. if (!empty($row['expired_on'])) {
  165. $this->expired_on = $row['expired_on'];
  166. }
  167. if ($this->type == 2) {
  168. if ($row['force_commit'] == 1) {
  169. $this->force_commit = true;
  170. }
  171. }
  172. $this->mode = $row['default_view_mod'];
  173. // Check user ID.
  174. if (empty($user_id)) {
  175. $this->error = 'User ID is empty';
  176. } else {
  177. $userInfo = api_get_user_info($user_id);
  178. if (!empty($userInfo)) {
  179. $this->user_id = $userInfo['user_id'];
  180. } else {
  181. $this->error = 'User ID does not exist in database #'.$user_id;
  182. }
  183. }
  184. // End of variables checking.
  185. $session_id = api_get_session_id();
  186. // Get the session condition for learning paths of the base + session.
  187. $session = api_get_session_condition($session_id);
  188. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  189. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  190. // Selecting by view_count descending allows to get the highest view_count first.
  191. $sql = "SELECT * FROM $lp_table
  192. WHERE
  193. c_id = $course_id AND
  194. lp_id = $lp_id AND
  195. user_id = $user_id
  196. $session
  197. ORDER BY view_count DESC";
  198. $res = Database::query($sql);
  199. if ($debug) {
  200. error_log('learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  201. }
  202. if (Database::num_rows($res) > 0) {
  203. if ($debug) {
  204. error_log('learnpath::__construct() '.__LINE__.' - Found previous view');
  205. }
  206. $row = Database::fetch_array($res);
  207. $this->attempt = $row['view_count'];
  208. $this->lp_view_id = $row['id'];
  209. $this->last_item_seen = $row['last_item'];
  210. $this->progress_db = $row['progress'];
  211. $this->lp_view_session_id = $row['session_id'];
  212. } elseif (!api_is_invitee()) {
  213. if ($debug) {
  214. error_log('learnpath::__construct() '.__LINE__.' - NOT Found previous view');
  215. }
  216. $this->attempt = 1;
  217. $params = [
  218. 'c_id' => $course_id,
  219. 'lp_id' => $lp_id,
  220. 'user_id' => $user_id,
  221. 'view_count' => 1,
  222. 'session_id' => $session_id,
  223. 'last_item' => 0,
  224. ];
  225. $this->last_item_seen = 0;
  226. $this->lp_view_session_id = $session_id;
  227. $this->lp_view_id = Database::insert($lp_table, $params);
  228. if (!empty($this->lp_view_id)) {
  229. $sql = "UPDATE $lp_table SET id = iid
  230. WHERE iid = ".$this->lp_view_id;
  231. Database::query($sql);
  232. }
  233. }
  234. // Initialise items.
  235. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  236. $sql = "SELECT * FROM $lp_item_table
  237. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  238. ORDER BY parent_item_id, display_order";
  239. $res = Database::query($sql);
  240. $lp_item_id_list = [];
  241. while ($row = Database::fetch_array($res)) {
  242. $lp_item_id_list[] = $row['iid'];
  243. switch ($this->type) {
  244. case 3: //aicc
  245. $oItem = new aiccItem('db', $row['iid'], $course_id);
  246. if (is_object($oItem)) {
  247. $my_item_id = $oItem->get_id();
  248. $oItem->set_lp_view($this->lp_view_id, $course_id);
  249. $oItem->set_prevent_reinit($this->prevent_reinit);
  250. // Don't use reference here as the next loop will make the pointed object change.
  251. $this->items[$my_item_id] = $oItem;
  252. $this->refs_list[$oItem->ref] = $my_item_id;
  253. if ($debug) {
  254. error_log(
  255. 'learnpath::__construct() - '.
  256. 'aicc object with id '.$my_item_id.
  257. ' set in items[]',
  258. 0
  259. );
  260. }
  261. }
  262. break;
  263. case 2:
  264. $oItem = new scormItem('db', $row['iid'], $course_id);
  265. if (is_object($oItem)) {
  266. $my_item_id = $oItem->get_id();
  267. $oItem->set_lp_view($this->lp_view_id, $course_id);
  268. $oItem->set_prevent_reinit($this->prevent_reinit);
  269. // Don't use reference here as the next loop will make the pointed object change.
  270. $this->items[$my_item_id] = $oItem;
  271. $this->refs_list[$oItem->ref] = $my_item_id;
  272. if ($debug) {
  273. error_log('object with id '.$my_item_id.' set in items[]');
  274. }
  275. }
  276. break;
  277. case 1:
  278. default:
  279. if ($debug) {
  280. error_log('learnpath::__construct() '.__LINE__.' - calling learnpathItem');
  281. }
  282. $oItem = new learnpathItem($row['iid'], $user_id, $course_id, $row);
  283. if ($debug) {
  284. error_log('learnpath::__construct() '.__LINE__.' - end calling learnpathItem');
  285. }
  286. if (is_object($oItem)) {
  287. $my_item_id = $oItem->get_id();
  288. // Moved down to when we are sure the item_view exists.
  289. //$oItem->set_lp_view($this->lp_view_id);
  290. $oItem->set_prevent_reinit($this->prevent_reinit);
  291. // Don't use reference here as the next loop will make the pointed object change.
  292. $this->items[$my_item_id] = $oItem;
  293. $this->refs_list[$my_item_id] = $my_item_id;
  294. if ($debug) {
  295. error_log(
  296. 'learnpath::__construct() '.__LINE__.
  297. ' - object with id '.$my_item_id.' set in items[]'
  298. );
  299. }
  300. }
  301. break;
  302. }
  303. // Setting the object level with variable $this->items[$i][parent]
  304. foreach ($this->items as $itemLPObject) {
  305. $level = self::get_level_for_item(
  306. $this->items,
  307. $itemLPObject->db_id
  308. );
  309. $itemLPObject->level = $level;
  310. }
  311. // Setting the view in the item object.
  312. if (is_object($this->items[$row['iid']])) {
  313. $this->items[$row['iid']]->set_lp_view($this->lp_view_id, $course_id);
  314. if ($this->items[$row['iid']]->get_type() == TOOL_HOTPOTATOES) {
  315. $this->items[$row['iid']]->current_start_time = 0;
  316. $this->items[$row['iid']]->current_stop_time = 0;
  317. }
  318. }
  319. }
  320. if (!empty($lp_item_id_list)) {
  321. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  322. if (!empty($lp_item_id_list_to_string)) {
  323. // Get last viewing vars.
  324. $itemViewTable = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  325. // This query should only return one or zero result.
  326. $sql = "SELECT lp_item_id, status
  327. FROM $itemViewTable
  328. WHERE
  329. c_id = $course_id AND
  330. lp_view_id = ".$this->get_view_id()." AND
  331. lp_item_id IN ('".$lp_item_id_list_to_string."')
  332. ORDER BY view_count DESC ";
  333. $status_list = [];
  334. $res = Database::query($sql);
  335. while ($row = Database:: fetch_array($res)) {
  336. $status_list[$row['lp_item_id']] = $row['status'];
  337. }
  338. foreach ($lp_item_id_list as $item_id) {
  339. if (isset($status_list[$item_id])) {
  340. $status = $status_list[$item_id];
  341. if (is_object($this->items[$item_id])) {
  342. $this->items[$item_id]->set_status($status);
  343. if (empty($status)) {
  344. $this->items[$item_id]->set_status(
  345. $this->default_status
  346. );
  347. }
  348. }
  349. } else {
  350. if (!api_is_invitee()) {
  351. if (is_object($this->items[$item_id])) {
  352. $this->items[$item_id]->set_status(
  353. $this->default_status
  354. );
  355. }
  356. if (!empty($this->lp_view_id)) {
  357. // Add that row to the lp_item_view table so that
  358. // we have something to show in the stats page.
  359. $params = [
  360. 'c_id' => $course_id,
  361. 'lp_item_id' => $item_id,
  362. 'lp_view_id' => $this->lp_view_id,
  363. 'view_count' => 1,
  364. 'status' => 'not attempted',
  365. 'start_time' => time(),
  366. 'total_time' => 0,
  367. 'score' => 0,
  368. ];
  369. $insertId = Database::insert($itemViewTable, $params);
  370. if ($insertId) {
  371. $sql = "UPDATE $itemViewTable SET id = iid
  372. WHERE iid = $insertId";
  373. Database::query($sql);
  374. }
  375. $this->items[$item_id]->set_lp_view(
  376. $this->lp_view_id,
  377. $course_id
  378. );
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. $this->ordered_items = self::get_flat_ordered_items_list(
  386. $this->get_id(),
  387. 0,
  388. $course_id
  389. );
  390. $this->max_ordered_items = 0;
  391. foreach ($this->ordered_items as $index => $dummy) {
  392. if ($index > $this->max_ordered_items && !empty($dummy)) {
  393. $this->max_ordered_items = $index;
  394. }
  395. }
  396. // TODO: Define the current item better.
  397. $this->first();
  398. if ($debug) {
  399. error_log('lp_view_session_id '.$this->lp_view_session_id);
  400. error_log('End of learnpath constructor for learnpath '.$this->get_id());
  401. }
  402. } else {
  403. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  404. }
  405. }
  406. }
  407. /**
  408. * @return string
  409. */
  410. public function getCourseCode()
  411. {
  412. return $this->cc;
  413. }
  414. /**
  415. * @return int
  416. */
  417. public function get_course_int_id()
  418. {
  419. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  420. }
  421. /**
  422. * @param $course_id
  423. *
  424. * @return int
  425. */
  426. public function set_course_int_id($course_id)
  427. {
  428. return $this->course_int_id = (int) $course_id;
  429. }
  430. /**
  431. * Function rewritten based on old_add_item() from Yannick Warnier.
  432. * Due the fact that users can decide where the item should come, I had to overlook this function and
  433. * I found it better to rewrite it. Old function is still available.
  434. * Added also the possibility to add a description.
  435. *
  436. * @param int $parent
  437. * @param int $previous
  438. * @param string $type
  439. * @param int $id resource ID (ref)
  440. * @param string $title
  441. * @param string $description
  442. * @param int $prerequisites
  443. * @param int $max_time_allowed
  444. * @param int $userId
  445. *
  446. * @return int
  447. */
  448. public function add_item(
  449. $parent,
  450. $previous,
  451. $type = 'dir',
  452. $id,
  453. $title,
  454. $description,
  455. $prerequisites = 0,
  456. $max_time_allowed = 0,
  457. $userId = 0
  458. ) {
  459. $course_id = $this->course_info['real_id'];
  460. if (empty($course_id)) {
  461. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  462. $this->course_info = api_get_course_info($this->cc);
  463. $course_id = $this->course_info['real_id'];
  464. }
  465. $userId = empty($userId) ? api_get_user_id() : $userId;
  466. $sessionId = api_get_session_id();
  467. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  468. $_course = $this->course_info;
  469. $parent = (int) $parent;
  470. $previous = (int) $previous;
  471. $id = (int) $id;
  472. $max_time_allowed = htmlentities($max_time_allowed);
  473. if (empty($max_time_allowed)) {
  474. $max_time_allowed = 0;
  475. }
  476. $sql = "SELECT COUNT(iid) AS num
  477. FROM $tbl_lp_item
  478. WHERE
  479. c_id = $course_id AND
  480. lp_id = ".$this->get_id()." AND
  481. parent_item_id = $parent ";
  482. $res_count = Database::query($sql);
  483. $row = Database::fetch_array($res_count);
  484. $num = $row['num'];
  485. $tmp_previous = 0;
  486. $display_order = 0;
  487. $next = 0;
  488. if ($num > 0) {
  489. if (empty($previous)) {
  490. $sql = "SELECT iid, next_item_id, display_order
  491. FROM $tbl_lp_item
  492. WHERE
  493. c_id = $course_id AND
  494. lp_id = ".$this->get_id()." AND
  495. parent_item_id = $parent AND
  496. previous_item_id = 0 OR
  497. previous_item_id = $parent";
  498. $result = Database::query($sql);
  499. $row = Database::fetch_array($result);
  500. if ($row) {
  501. $next = $row['iid'];
  502. }
  503. } else {
  504. $previous = (int) $previous;
  505. $sql = "SELECT iid, previous_item_id, next_item_id, display_order
  506. FROM $tbl_lp_item
  507. WHERE
  508. c_id = $course_id AND
  509. lp_id = ".$this->get_id()." AND
  510. id = $previous";
  511. $result = Database::query($sql);
  512. $row = Database::fetch_array($result);
  513. if ($row) {
  514. $tmp_previous = $row['iid'];
  515. $next = $row['next_item_id'];
  516. $display_order = $row['display_order'];
  517. }
  518. }
  519. }
  520. $id = (int) $id;
  521. $typeCleaned = Database::escape_string($type);
  522. $max_score = 100;
  523. if ($type === 'quiz') {
  524. $sql = 'SELECT SUM(ponderation)
  525. FROM '.Database::get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  526. INNER JOIN '.Database::get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  527. ON
  528. quiz_question.id = quiz_rel_question.question_id AND
  529. quiz_question.c_id = quiz_rel_question.c_id
  530. WHERE
  531. quiz_rel_question.exercice_id = '.$id." AND
  532. quiz_question.c_id = $course_id AND
  533. quiz_rel_question.c_id = $course_id ";
  534. $rsQuiz = Database::query($sql);
  535. $max_score = Database::result($rsQuiz, 0, 0);
  536. // Disabling the exercise if we add it inside a LP
  537. $exercise = new Exercise($course_id);
  538. $exercise->read($id);
  539. $exercise->disable();
  540. $exercise->save();
  541. }
  542. $params = [
  543. 'c_id' => $course_id,
  544. 'lp_id' => $this->get_id(),
  545. 'item_type' => $typeCleaned,
  546. 'ref' => '',
  547. 'title' => $title,
  548. 'description' => $description,
  549. 'path' => $id,
  550. 'max_score' => $max_score,
  551. 'parent_item_id' => $parent,
  552. 'previous_item_id' => $previous,
  553. 'next_item_id' => (int) $next,
  554. 'display_order' => $display_order + 1,
  555. 'prerequisite' => $prerequisites,
  556. 'max_time_allowed' => $max_time_allowed,
  557. 'min_score' => 0,
  558. 'launch_data' => '',
  559. ];
  560. if ($prerequisites != 0) {
  561. $params['prerequisite'] = $prerequisites;
  562. }
  563. $new_item_id = Database::insert($tbl_lp_item, $params);
  564. if ($new_item_id) {
  565. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  566. Database::query($sql);
  567. if (!empty($next)) {
  568. $sql = "UPDATE $tbl_lp_item
  569. SET previous_item_id = $new_item_id
  570. WHERE c_id = $course_id AND id = $next AND item_type != '".TOOL_LP_FINAL_ITEM."'";
  571. Database::query($sql);
  572. }
  573. // Update the item that should be before the new item.
  574. if (!empty($tmp_previous)) {
  575. $sql = "UPDATE $tbl_lp_item
  576. SET next_item_id = $new_item_id
  577. WHERE c_id = $course_id AND id = $tmp_previous";
  578. Database::query($sql);
  579. }
  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. iid <> $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 iid = $new_item_id";
  594. Database::query($sql);
  595. $sql = "UPDATE $tbl_lp_item
  596. SET previous_item_id = ".$this->getLastInFirstLevel()."
  597. WHERE c_id = $course_id AND lp_id = {$this->lp_id} AND item_type = '".TOOL_LP_FINAL_ITEM."'";
  598. Database::query($sql);
  599. // Upload audio.
  600. if (!empty($_FILES['mp3']['name'])) {
  601. // Create the audio folder if it does not exist yet.
  602. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  603. if (!is_dir($filepath.'audio')) {
  604. mkdir(
  605. $filepath.'audio',
  606. api_get_permissions_for_new_directories()
  607. );
  608. $audio_id = add_document(
  609. $_course,
  610. '/audio',
  611. 'folder',
  612. 0,
  613. 'audio',
  614. '',
  615. 0,
  616. true,
  617. null,
  618. $sessionId,
  619. $userId
  620. );
  621. api_item_property_update(
  622. $_course,
  623. TOOL_DOCUMENT,
  624. $audio_id,
  625. 'FolderCreated',
  626. $userId,
  627. null,
  628. null,
  629. null,
  630. null,
  631. $sessionId
  632. );
  633. api_item_property_update(
  634. $_course,
  635. TOOL_DOCUMENT,
  636. $audio_id,
  637. 'invisible',
  638. $userId,
  639. null,
  640. null,
  641. null,
  642. null,
  643. $sessionId
  644. );
  645. }
  646. $file_path = handle_uploaded_document(
  647. $_course,
  648. $_FILES['mp3'],
  649. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  650. '/audio',
  651. $userId,
  652. '',
  653. '',
  654. '',
  655. '',
  656. false
  657. );
  658. // Getting the filename only.
  659. $file_components = explode('/', $file_path);
  660. $file = $file_components[count($file_components) - 1];
  661. // Store the mp3 file in the lp_item table.
  662. $sql = "UPDATE $tbl_lp_item SET
  663. audio = '".Database::escape_string($file)."'
  664. WHERE iid = '".intval($new_item_id)."'";
  665. Database::query($sql);
  666. }
  667. }
  668. return $new_item_id;
  669. }
  670. /**
  671. * Static admin function allowing addition of a learnpath to a course.
  672. *
  673. * @param string $courseCode
  674. * @param string $name
  675. * @param string $description
  676. * @param string $learnpath
  677. * @param string $origin
  678. * @param string $zipname Zip file containing the learnpath or directory containing the learnpath
  679. * @param string $publicated_on
  680. * @param string $expired_on
  681. * @param int $categoryId
  682. * @param int $userId
  683. *
  684. * @return int The new learnpath ID on success, 0 on failure
  685. */
  686. public static function add_lp(
  687. $courseCode,
  688. $name,
  689. $description = '',
  690. $learnpath = 'guess',
  691. $origin = 'zip',
  692. $zipname = '',
  693. $publicated_on = '',
  694. $expired_on = '',
  695. $categoryId = 0,
  696. $userId = 0
  697. ) {
  698. global $charset;
  699. if (!empty($courseCode)) {
  700. $courseInfo = api_get_course_info($courseCode);
  701. $course_id = $courseInfo['real_id'];
  702. } else {
  703. $course_id = api_get_course_int_id();
  704. $courseInfo = api_get_course_info();
  705. }
  706. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  707. // Check course code exists.
  708. // Check lp_name doesn't exist, otherwise append something.
  709. $i = 0;
  710. $name = Database::escape_string($name);
  711. $categoryId = (int) $categoryId;
  712. // Session id.
  713. $session_id = api_get_session_id();
  714. $userId = empty($userId) ? api_get_user_id() : $userId;
  715. $check_name = "SELECT * FROM $tbl_lp
  716. WHERE c_id = $course_id AND name = '$name'";
  717. $res_name = Database::query($check_name);
  718. if (empty($publicated_on)) {
  719. $publicated_on = null;
  720. } else {
  721. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  722. }
  723. if (empty($expired_on)) {
  724. $expired_on = null;
  725. } else {
  726. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  727. }
  728. while (Database::num_rows($res_name)) {
  729. // There is already one such name, update the current one a bit.
  730. $i++;
  731. $name = $name.' - '.$i;
  732. $check_name = "SELECT * FROM $tbl_lp
  733. WHERE c_id = $course_id AND name = '$name'";
  734. $res_name = Database::query($check_name);
  735. }
  736. // New name does not exist yet; keep it.
  737. // Escape description.
  738. // Kevin: added htmlentities().
  739. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  740. $type = 1;
  741. switch ($learnpath) {
  742. case 'guess':
  743. break;
  744. case 'dokeos':
  745. case 'chamilo':
  746. $type = 1;
  747. break;
  748. case 'aicc':
  749. break;
  750. }
  751. switch ($origin) {
  752. case 'zip':
  753. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  754. break;
  755. case 'manual':
  756. default:
  757. $get_max = "SELECT MAX(display_order)
  758. FROM $tbl_lp WHERE c_id = $course_id";
  759. $res_max = Database::query($get_max);
  760. if (Database::num_rows($res_max) < 1) {
  761. $dsp = 1;
  762. } else {
  763. $row = Database::fetch_array($res_max);
  764. $dsp = $row[0] + 1;
  765. }
  766. $params = [
  767. 'c_id' => $course_id,
  768. 'lp_type' => $type,
  769. 'name' => $name,
  770. 'description' => $description,
  771. 'path' => '',
  772. 'default_view_mod' => 'embedded',
  773. 'default_encoding' => 'UTF-8',
  774. 'display_order' => $dsp,
  775. 'content_maker' => 'Chamilo',
  776. 'content_local' => 'local',
  777. 'js_lib' => '',
  778. 'session_id' => $session_id,
  779. 'created_on' => api_get_utc_datetime(),
  780. 'modified_on' => api_get_utc_datetime(),
  781. 'publicated_on' => $publicated_on,
  782. 'expired_on' => $expired_on,
  783. 'category_id' => $categoryId,
  784. 'force_commit' => 0,
  785. 'content_license' => '',
  786. 'debug' => 0,
  787. 'theme' => '',
  788. 'preview_image' => '',
  789. 'author' => '',
  790. 'prerequisite' => 0,
  791. 'hide_toc_frame' => 0,
  792. 'seriousgame_mode' => 0,
  793. 'autolaunch' => 0,
  794. 'max_attempts' => 0,
  795. 'subscribe_users' => 0,
  796. 'accumulate_scorm_time' => 1,
  797. ];
  798. $id = Database::insert($tbl_lp, $params);
  799. if ($id > 0) {
  800. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  801. Database::query($sql);
  802. // Insert into item_property.
  803. api_item_property_update(
  804. $courseInfo,
  805. TOOL_LEARNPATH,
  806. $id,
  807. 'LearnpathAdded',
  808. $userId
  809. );
  810. api_set_default_visibility(
  811. $id,
  812. TOOL_LEARNPATH,
  813. 0,
  814. $courseInfo,
  815. $session_id,
  816. $userId
  817. );
  818. return $id;
  819. }
  820. break;
  821. }
  822. }
  823. /**
  824. * Auto completes the parents of an item in case it's been completed or passed.
  825. *
  826. * @param int $item Optional ID of the item from which to look for parents
  827. */
  828. public function autocomplete_parents($item)
  829. {
  830. $debug = $this->debug;
  831. if (empty($item)) {
  832. $item = $this->current;
  833. }
  834. $currentItem = $this->getItem($item);
  835. if ($currentItem) {
  836. $parent_id = $currentItem->get_parent();
  837. $parent = $this->getItem($parent_id);
  838. if ($parent) {
  839. // if $item points to an object and there is a parent.
  840. if ($debug) {
  841. error_log(
  842. 'Autocompleting parent of item '.$item.' '.
  843. $currentItem->get_title().'" (item '.$parent_id.' "'.$parent->get_title().'") ',
  844. 0
  845. );
  846. }
  847. // New experiment including failed and browsed in completed status.
  848. //$current_status = $currentItem->get_status();
  849. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  850. // Fixes chapter auto complete
  851. if (true) {
  852. // If the current item is completed or passes or succeeded.
  853. $updateParentStatus = true;
  854. if ($debug) {
  855. error_log('Status of current item is alright');
  856. }
  857. foreach ($parent->get_children() as $childItemId) {
  858. $childItem = $this->getItem($childItemId);
  859. // If children was not set try to get the info
  860. if (empty($childItem->db_item_view_id)) {
  861. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  862. }
  863. // Check all his brothers (parent's children) for completion status.
  864. if ($childItemId != $item) {
  865. if ($debug) {
  866. error_log(
  867. 'Looking at brother #'.$childItemId.' "'.$childItem->get_title().'", status is '.$childItem->get_status(),
  868. 0
  869. );
  870. }
  871. // Trying completing parents of failed and browsed items as well.
  872. if ($childItem->status_is(
  873. [
  874. 'completed',
  875. 'passed',
  876. 'succeeded',
  877. 'browsed',
  878. 'failed',
  879. ]
  880. )
  881. ) {
  882. // Keep completion status to true.
  883. continue;
  884. } else {
  885. if ($debug > 2) {
  886. error_log(
  887. '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,
  888. 0
  889. );
  890. }
  891. $updateParentStatus = false;
  892. break;
  893. }
  894. }
  895. }
  896. if ($updateParentStatus) {
  897. // If all the children were completed:
  898. $parent->set_status('completed');
  899. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  900. // Force the status to "completed"
  901. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  902. $this->update_queue[$parent->get_id()] = 'completed';
  903. if ($debug) {
  904. error_log(
  905. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  906. print_r($this->update_queue, 1),
  907. 0
  908. );
  909. }
  910. // Recursive call.
  911. $this->autocomplete_parents($parent->get_id());
  912. }
  913. }
  914. } else {
  915. if ($debug) {
  916. error_log("Parent #$parent_id does not exists");
  917. }
  918. }
  919. } else {
  920. if ($debug) {
  921. error_log("#$item is an item that doesn't have parents");
  922. }
  923. }
  924. }
  925. /**
  926. * Closes the current resource.
  927. *
  928. * Stops the timer
  929. * Saves into the database if required
  930. * Clears the current resource data from this object
  931. *
  932. * @return bool True on success, false on failure
  933. */
  934. public function close()
  935. {
  936. if (empty($this->lp_id)) {
  937. $this->error = 'Trying to close this learnpath but no ID is set';
  938. return false;
  939. }
  940. $this->current_time_stop = time();
  941. $this->ordered_items = [];
  942. $this->index = 0;
  943. unset($this->lp_id);
  944. //unset other stuff
  945. return true;
  946. }
  947. /**
  948. * Static admin function allowing removal of a learnpath.
  949. *
  950. * @param array $courseInfo
  951. * @param int $id Learnpath ID
  952. * @param string $delete Whether to delete data or keep it (default: 'keep', others: 'remove')
  953. *
  954. * @return bool True on success, false on failure (might change that to return number of elements deleted)
  955. */
  956. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  957. {
  958. $course_id = api_get_course_int_id();
  959. if (!empty($courseInfo)) {
  960. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  961. }
  962. // TODO: Implement a way of getting this to work when the current object is not set.
  963. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  964. // If an ID is specifically given and the current LP is not the same, prevent delete.
  965. if (!empty($id) && ($id != $this->lp_id)) {
  966. return false;
  967. }
  968. $lp = Database::get_course_table(TABLE_LP_MAIN);
  969. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  970. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
  971. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  972. // Delete lp item id.
  973. foreach ($this->items as $lpItemId => $dummy) {
  974. $sql = "DELETE FROM $lp_item_view
  975. WHERE c_id = $course_id AND lp_item_id = '".$lpItemId."'";
  976. Database::query($sql);
  977. }
  978. // Proposed by Christophe (nickname: clefevre)
  979. $sql = "DELETE FROM $lp_item
  980. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  981. Database::query($sql);
  982. $sql = "DELETE FROM $lp_view
  983. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  984. Database::query($sql);
  985. self::toggle_publish($this->lp_id, 'i');
  986. if ($this->type == 2 || $this->type == 3) {
  987. // This is a scorm learning path, delete the files as well.
  988. $sql = "SELECT path FROM $lp
  989. WHERE iid = ".$this->lp_id;
  990. $res = Database::query($sql);
  991. if (Database::num_rows($res) > 0) {
  992. $row = Database::fetch_array($res);
  993. $path = $row['path'];
  994. $sql = "SELECT id FROM $lp
  995. WHERE
  996. c_id = $course_id AND
  997. path = '$path' AND
  998. iid != ".$this->lp_id;
  999. $res = Database::query($sql);
  1000. if (Database::num_rows($res) > 0) {
  1001. // Another learning path uses this directory, so don't delete it.
  1002. if ($this->debug > 2) {
  1003. error_log('In learnpath::delete(), found other LP using path '.$path.', keeping directory', 0);
  1004. }
  1005. } else {
  1006. // No other LP uses that directory, delete it.
  1007. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  1008. // The absolute system path for this course.
  1009. $course_scorm_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir;
  1010. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) && !empty($course_scorm_dir)) {
  1011. if ($this->debug > 2) {
  1012. error_log('In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path, 0);
  1013. }
  1014. // Proposed by Christophe (clefevre).
  1015. if (strcmp(substr($path, -2), "/.") == 0) {
  1016. $path = substr($path, 0, -1); // Remove "." at the end.
  1017. }
  1018. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1019. rmdirr($course_scorm_dir.$path);
  1020. }
  1021. }
  1022. }
  1023. }
  1024. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1025. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1026. // Delete tools
  1027. $sql = "DELETE FROM $tbl_tool
  1028. WHERE c_id = $course_id AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1029. Database::query($sql);
  1030. $sql = "DELETE FROM $lp
  1031. WHERE iid = ".$this->lp_id;
  1032. Database::query($sql);
  1033. // Updates the display order of all lps.
  1034. $this->update_display_order();
  1035. api_item_property_update(
  1036. api_get_course_info(),
  1037. TOOL_LEARNPATH,
  1038. $this->lp_id,
  1039. 'delete',
  1040. api_get_user_id()
  1041. );
  1042. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1043. api_get_course_id(),
  1044. 4,
  1045. $id,
  1046. api_get_session_id()
  1047. );
  1048. if ($link_info !== false) {
  1049. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1050. }
  1051. if (api_get_setting('search_enabled') == 'true') {
  1052. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1053. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1054. }
  1055. }
  1056. /**
  1057. * Removes all the children of one item - dangerous!
  1058. *
  1059. * @param int $id Element ID of which children have to be removed
  1060. *
  1061. * @return int Total number of children removed
  1062. */
  1063. public function delete_children_items($id)
  1064. {
  1065. $course_id = $this->course_info['real_id'];
  1066. $num = 0;
  1067. $id = (int) $id;
  1068. if (empty($id) || empty($course_id)) {
  1069. return false;
  1070. }
  1071. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1072. $sql = "SELECT * FROM $lp_item
  1073. WHERE c_id = $course_id AND parent_item_id = $id";
  1074. $res = Database::query($sql);
  1075. while ($row = Database::fetch_array($res)) {
  1076. $num += $this->delete_children_items($row['iid']);
  1077. $sql = "DELETE FROM $lp_item
  1078. WHERE c_id = $course_id AND iid = ".$row['iid'];
  1079. Database::query($sql);
  1080. $num++;
  1081. }
  1082. return $num;
  1083. }
  1084. /**
  1085. * Removes an item from the current learnpath.
  1086. *
  1087. * @param int $id Elem ID (0 if first)
  1088. *
  1089. * @return int Number of elements moved
  1090. *
  1091. * @todo implement resource removal
  1092. */
  1093. public function delete_item($id)
  1094. {
  1095. $course_id = api_get_course_int_id();
  1096. $id = (int) $id;
  1097. // TODO: Implement the resource removal.
  1098. if (empty($id) || empty($course_id)) {
  1099. return false;
  1100. }
  1101. // First select item to get previous, next, and display order.
  1102. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1103. $sql_sel = "SELECT * FROM $lp_item WHERE iid = $id";
  1104. $res_sel = Database::query($sql_sel);
  1105. if (Database::num_rows($res_sel) < 1) {
  1106. return false;
  1107. }
  1108. $row = Database::fetch_array($res_sel);
  1109. $previous = $row['previous_item_id'];
  1110. $next = $row['next_item_id'];
  1111. $display = $row['display_order'];
  1112. $parent = $row['parent_item_id'];
  1113. $lp = $row['lp_id'];
  1114. // Delete children items.
  1115. $this->delete_children_items($id);
  1116. // Now delete the item.
  1117. $sql_del = "DELETE FROM $lp_item WHERE iid = $id";
  1118. Database::query($sql_del);
  1119. // Now update surrounding items.
  1120. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1121. WHERE iid = $previous";
  1122. Database::query($sql_upd);
  1123. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1124. WHERE iid = $next AND item_type != '".TOOL_LP_FINAL_ITEM."'";
  1125. Database::query($sql_upd);
  1126. // Now update all following items with new display order.
  1127. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1128. WHERE
  1129. c_id = $course_id AND
  1130. lp_id = $lp AND
  1131. parent_item_id = $parent AND
  1132. display_order > $display";
  1133. Database::query($sql_all);
  1134. //Removing prerequisites since the item will not longer exist
  1135. $sql_all = "UPDATE $lp_item SET prerequisite = ''
  1136. WHERE c_id = $course_id AND prerequisite = $id";
  1137. Database::query($sql_all);
  1138. $sql = "UPDATE $lp_item
  1139. SET previous_item_id = ".$this->getLastInFirstLevel()."
  1140. WHERE c_id = $course_id AND lp_id = {$this->lp_id} AND item_type = '".TOOL_LP_FINAL_ITEM."'";
  1141. Database::query($sql);
  1142. // Remove from search engine if enabled.
  1143. if (api_get_setting('search_enabled') === 'true') {
  1144. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1145. $sql = 'SELECT * FROM %s
  1146. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  1147. LIMIT 1';
  1148. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1149. $res = Database::query($sql);
  1150. if (Database::num_rows($res) > 0) {
  1151. $row2 = Database::fetch_array($res);
  1152. $di = new ChamiloIndexer();
  1153. $di->remove_document($row2['search_did']);
  1154. }
  1155. $sql = 'DELETE FROM %s
  1156. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  1157. LIMIT 1';
  1158. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1159. Database::query($sql);
  1160. }
  1161. }
  1162. /**
  1163. * Updates an item's content in place.
  1164. *
  1165. * @param int $id Element ID
  1166. * @param int $parent Parent item ID
  1167. * @param int $previous Previous item ID
  1168. * @param string $title Item title
  1169. * @param string $description Item description
  1170. * @param string $prerequisites Prerequisites (optional)
  1171. * @param array $audio The array resulting of the $_FILES[mp3] element
  1172. * @param int $max_time_allowed
  1173. * @param string $url
  1174. *
  1175. * @return bool True on success, false on error
  1176. */
  1177. public function edit_item(
  1178. $id,
  1179. $parent,
  1180. $previous,
  1181. $title,
  1182. $description,
  1183. $prerequisites = '0',
  1184. $audio = [],
  1185. $max_time_allowed = 0,
  1186. $url = ''
  1187. ) {
  1188. $course_id = api_get_course_int_id();
  1189. $_course = api_get_course_info();
  1190. $id = (int) $id;
  1191. if (empty($max_time_allowed)) {
  1192. $max_time_allowed = 0;
  1193. }
  1194. if (empty($id) || empty($_course)) {
  1195. return false;
  1196. }
  1197. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1198. $sql = "SELECT * FROM $tbl_lp_item
  1199. WHERE iid = $id";
  1200. $res_select = Database::query($sql);
  1201. $row_select = Database::fetch_array($res_select);
  1202. $audio_update_sql = '';
  1203. if (is_array($audio) && !empty($audio['tmp_name']) && $audio['error'] === 0) {
  1204. // Create the audio folder if it does not exist yet.
  1205. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1206. if (!is_dir($filepath.'audio')) {
  1207. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1208. $audio_id = add_document(
  1209. $_course,
  1210. '/audio',
  1211. 'folder',
  1212. 0,
  1213. 'audio'
  1214. );
  1215. api_item_property_update(
  1216. $_course,
  1217. TOOL_DOCUMENT,
  1218. $audio_id,
  1219. 'FolderCreated',
  1220. api_get_user_id(),
  1221. null,
  1222. null,
  1223. null,
  1224. null,
  1225. api_get_session_id()
  1226. );
  1227. api_item_property_update(
  1228. $_course,
  1229. TOOL_DOCUMENT,
  1230. $audio_id,
  1231. 'invisible',
  1232. api_get_user_id(),
  1233. null,
  1234. null,
  1235. null,
  1236. null,
  1237. api_get_session_id()
  1238. );
  1239. }
  1240. // Upload file in documents.
  1241. $pi = pathinfo($audio['name']);
  1242. if ($pi['extension'] === 'mp3') {
  1243. $c_det = api_get_course_info($this->cc);
  1244. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1245. $path = handle_uploaded_document(
  1246. $c_det,
  1247. $audio,
  1248. $bp,
  1249. '/audio',
  1250. api_get_user_id(),
  1251. 0,
  1252. null,
  1253. 0,
  1254. 'rename',
  1255. false,
  1256. 0
  1257. );
  1258. $path = substr($path, 7);
  1259. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1260. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1261. }
  1262. }
  1263. $same_parent = $row_select['parent_item_id'] == $parent ? true : false;
  1264. $same_previous = $row_select['previous_item_id'] == $previous ? true : false;
  1265. // TODO: htmlspecialchars to be checked for encoding related problems.
  1266. if ($same_parent && $same_previous) {
  1267. // Only update title and description.
  1268. $sql = "UPDATE $tbl_lp_item
  1269. SET title = '".Database::escape_string($title)."',
  1270. prerequisite = '".$prerequisites."',
  1271. description = '".Database::escape_string($description)."'
  1272. ".$audio_update_sql.",
  1273. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1274. WHERE iid = $id";
  1275. Database::query($sql);
  1276. } else {
  1277. $old_parent = $row_select['parent_item_id'];
  1278. $old_previous = $row_select['previous_item_id'];
  1279. $old_next = $row_select['next_item_id'];
  1280. $old_order = $row_select['display_order'];
  1281. $old_prerequisite = $row_select['prerequisite'];
  1282. $old_max_time_allowed = $row_select['max_time_allowed'];
  1283. /* BEGIN -- virtually remove the current item id */
  1284. /* for the next and previous item it is like the current item doesn't exist anymore */
  1285. if ($old_previous != 0) {
  1286. // Next
  1287. $sql = "UPDATE $tbl_lp_item
  1288. SET next_item_id = $old_next
  1289. WHERE iid = $old_previous";
  1290. Database::query($sql);
  1291. }
  1292. if (!empty($old_next)) {
  1293. // Previous
  1294. $sql = "UPDATE $tbl_lp_item
  1295. SET previous_item_id = $old_previous
  1296. WHERE iid = $old_next";
  1297. Database::query($sql);
  1298. }
  1299. // display_order - 1 for every item with a display_order
  1300. // bigger then the display_order of the current item.
  1301. $sql = "UPDATE $tbl_lp_item
  1302. SET display_order = display_order - 1
  1303. WHERE
  1304. c_id = $course_id AND
  1305. display_order > $old_order AND
  1306. lp_id = ".$this->lp_id." AND
  1307. parent_item_id = $old_parent";
  1308. Database::query($sql);
  1309. /* END -- virtually remove the current item id */
  1310. /* BEGIN -- update the current item id to his new location */
  1311. if ($previous == 0) {
  1312. // Select the data of the item that should come after the current item.
  1313. $sql = "SELECT id, display_order
  1314. FROM $tbl_lp_item
  1315. WHERE
  1316. c_id = $course_id AND
  1317. lp_id = ".$this->lp_id." AND
  1318. parent_item_id = $parent AND
  1319. previous_item_id = $previous";
  1320. $res_select_old = Database::query($sql);
  1321. $row_select_old = Database::fetch_array($res_select_old);
  1322. // If the new parent didn't have children before.
  1323. if (Database::num_rows($res_select_old) == 0) {
  1324. $new_next = 0;
  1325. $new_order = 1;
  1326. } else {
  1327. $new_next = $row_select_old['id'];
  1328. $new_order = $row_select_old['display_order'];
  1329. }
  1330. } else {
  1331. // Select the data of the item that should come before the current item.
  1332. $sql = "SELECT next_item_id, display_order
  1333. FROM $tbl_lp_item
  1334. WHERE iid = $previous";
  1335. $res_select_old = Database::query($sql);
  1336. $row_select_old = Database::fetch_array($res_select_old);
  1337. $new_next = $row_select_old['next_item_id'];
  1338. $new_order = $row_select_old['display_order'] + 1;
  1339. }
  1340. // TODO: htmlspecialchars to be checked for encoding related problems.
  1341. // Update the current item with the new data.
  1342. $sql = "UPDATE $tbl_lp_item
  1343. SET
  1344. title = '".Database::escape_string($title)."',
  1345. description = '".Database::escape_string($description)."',
  1346. parent_item_id = $parent,
  1347. previous_item_id = $previous,
  1348. next_item_id = $new_next,
  1349. display_order = $new_order
  1350. $audio_update_sql
  1351. WHERE iid = $id";
  1352. Database::query($sql);
  1353. if ($previous != 0) {
  1354. // Update the previous item's next_item_id.
  1355. $sql = "UPDATE $tbl_lp_item
  1356. SET next_item_id = $id
  1357. WHERE iid = $previous";
  1358. Database::query($sql);
  1359. }
  1360. if (!empty($new_next)) {
  1361. // Update the next item's previous_item_id.
  1362. $sql = "UPDATE $tbl_lp_item
  1363. SET previous_item_id = $id
  1364. WHERE iid = $new_next";
  1365. Database::query($sql);
  1366. }
  1367. if ($old_prerequisite != $prerequisites) {
  1368. $sql = "UPDATE $tbl_lp_item
  1369. SET prerequisite = '$prerequisites'
  1370. WHERE iid = $id";
  1371. Database::query($sql);
  1372. }
  1373. if ($old_max_time_allowed != $max_time_allowed) {
  1374. // update max time allowed
  1375. $sql = "UPDATE $tbl_lp_item
  1376. SET max_time_allowed = $max_time_allowed
  1377. WHERE iid = $id";
  1378. Database::query($sql);
  1379. }
  1380. // Update all the items with the same or a bigger display_order than the current item.
  1381. $sql = "UPDATE $tbl_lp_item
  1382. SET display_order = display_order + 1
  1383. WHERE
  1384. c_id = $course_id AND
  1385. lp_id = ".$this->get_id()." AND
  1386. iid <> $id AND
  1387. parent_item_id = $parent AND
  1388. display_order >= $new_order";
  1389. Database::query($sql);
  1390. }
  1391. if ($row_select['item_type'] == 'link') {
  1392. $link = new Link();
  1393. $linkId = $row_select['path'];
  1394. $link->updateLink($linkId, $url);
  1395. }
  1396. }
  1397. /**
  1398. * Updates an item's prereq in place.
  1399. *
  1400. * @param int $id Element ID
  1401. * @param string $prerequisite_id Prerequisite Element ID
  1402. * @param int $minScore Prerequisite min score
  1403. * @param int $maxScore Prerequisite max score
  1404. *
  1405. * @return bool True on success, false on error
  1406. */
  1407. public function edit_item_prereq(
  1408. $id,
  1409. $prerequisite_id,
  1410. $minScore = 0,
  1411. $maxScore = 100
  1412. ) {
  1413. $id = (int) $id;
  1414. $prerequisite_id = (int) $prerequisite_id;
  1415. if (empty($id)) {
  1416. return false;
  1417. }
  1418. if (empty($minScore) || $minScore < 0) {
  1419. $minScore = 0;
  1420. }
  1421. if (empty($maxScore) || $maxScore < 0) {
  1422. $maxScore = 100;
  1423. }
  1424. $minScore = floatval($minScore);
  1425. $maxScore = floatval($maxScore);
  1426. if (empty($prerequisite_id)) {
  1427. $prerequisite_id = 'NULL';
  1428. $minScore = 0;
  1429. $maxScore = 100;
  1430. }
  1431. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1432. $sql = " UPDATE $tbl_lp_item
  1433. SET
  1434. prerequisite = $prerequisite_id ,
  1435. prerequisite_min_score = $minScore ,
  1436. prerequisite_max_score = $maxScore
  1437. WHERE iid = $id";
  1438. Database::query($sql);
  1439. return true;
  1440. }
  1441. /**
  1442. * Get the specific prefix index terms of this learning path.
  1443. *
  1444. * @param string $prefix
  1445. *
  1446. * @return array Array of terms
  1447. */
  1448. public function get_common_index_terms_by_prefix($prefix)
  1449. {
  1450. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1451. $terms = get_specific_field_values_list_by_prefix(
  1452. $prefix,
  1453. $this->cc,
  1454. TOOL_LEARNPATH,
  1455. $this->lp_id
  1456. );
  1457. $prefix_terms = [];
  1458. if (!empty($terms)) {
  1459. foreach ($terms as $term) {
  1460. $prefix_terms[] = $term['value'];
  1461. }
  1462. }
  1463. return $prefix_terms;
  1464. }
  1465. /**
  1466. * Gets the number of items currently completed.
  1467. *
  1468. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1469. *
  1470. * @return int The number of items currently completed
  1471. */
  1472. public function get_complete_items_count($failedStatusException = false)
  1473. {
  1474. $i = 0;
  1475. $completedStatusList = [
  1476. 'completed',
  1477. 'passed',
  1478. 'succeeded',
  1479. 'browsed',
  1480. ];
  1481. if (!$failedStatusException) {
  1482. $completedStatusList[] = 'failed';
  1483. }
  1484. foreach ($this->items as $id => $dummy) {
  1485. // Trying failed and browsed considered "progressed" as well.
  1486. if ($this->items[$id]->status_is($completedStatusList) &&
  1487. $this->items[$id]->get_type() != 'dir'
  1488. ) {
  1489. $i++;
  1490. }
  1491. }
  1492. return $i;
  1493. }
  1494. /**
  1495. * Gets the current item ID.
  1496. *
  1497. * @return int The current learnpath item id
  1498. */
  1499. public function get_current_item_id()
  1500. {
  1501. $current = 0;
  1502. if (!empty($this->current)) {
  1503. $current = (int) $this->current;
  1504. }
  1505. return $current;
  1506. }
  1507. /**
  1508. * Force to get the first learnpath item id.
  1509. *
  1510. * @return int The current learnpath item id
  1511. */
  1512. public function get_first_item_id()
  1513. {
  1514. $current = 0;
  1515. if (is_array($this->ordered_items)) {
  1516. $current = $this->ordered_items[0];
  1517. }
  1518. return $current;
  1519. }
  1520. /**
  1521. * Gets the total number of items available for viewing in this SCORM.
  1522. *
  1523. * @return int The total number of items
  1524. */
  1525. public function get_total_items_count()
  1526. {
  1527. return count($this->items);
  1528. }
  1529. /**
  1530. * Gets the total number of items available for viewing in this SCORM but without chapters.
  1531. *
  1532. * @return int The total no-chapters number of items
  1533. */
  1534. public function getTotalItemsCountWithoutDirs()
  1535. {
  1536. $total = 0;
  1537. $typeListNotToCount = self::getChapterTypes();
  1538. foreach ($this->items as $temp2) {
  1539. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1540. $total++;
  1541. }
  1542. }
  1543. return $total;
  1544. }
  1545. /**
  1546. * Sets the first element URL.
  1547. */
  1548. public function first()
  1549. {
  1550. if ($this->debug > 0) {
  1551. error_log('In learnpath::first()', 0);
  1552. error_log('$this->last_item_seen '.$this->last_item_seen);
  1553. }
  1554. // Test if the last_item_seen exists and is not a dir.
  1555. if (count($this->ordered_items) == 0) {
  1556. $this->index = 0;
  1557. }
  1558. if (!empty($this->last_item_seen) &&
  1559. !empty($this->items[$this->last_item_seen]) &&
  1560. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1561. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1562. //&& !$this->items[$this->last_item_seen]->is_done()
  1563. ) {
  1564. if ($this->debug > 2) {
  1565. error_log(
  1566. 'In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.
  1567. $this->items[$this->last_item_seen]->get_type()
  1568. );
  1569. }
  1570. $index = -1;
  1571. foreach ($this->ordered_items as $myindex => $item_id) {
  1572. if ($item_id == $this->last_item_seen) {
  1573. $index = $myindex;
  1574. break;
  1575. }
  1576. }
  1577. if ($index == -1) {
  1578. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1579. if ($this->debug > 2) {
  1580. error_log('Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!', 0);
  1581. }
  1582. return false;
  1583. } else {
  1584. $this->last = $this->last_item_seen;
  1585. $this->current = $this->last_item_seen;
  1586. $this->index = $index;
  1587. }
  1588. } else {
  1589. if ($this->debug > 2) {
  1590. error_log('In learnpath::first() - No last item seen', 0);
  1591. }
  1592. $index = 0;
  1593. // Loop through all ordered items and stop at the first item that is
  1594. // not a directory *and* that has not been completed yet.
  1595. while (!empty($this->ordered_items[$index]) &&
  1596. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') &&
  1597. (
  1598. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' ||
  1599. $this->items[$this->ordered_items[$index]]->is_done() === true
  1600. ) && $index < $this->max_ordered_items) {
  1601. $index++;
  1602. }
  1603. $this->last = $this->current;
  1604. // current is
  1605. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1606. $this->index = $index;
  1607. if ($this->debug > 2) {
  1608. error_log('$index '.$index);
  1609. error_log('In learnpath::first() - No last item seen');
  1610. error_log('New last = '.$this->last.'('.$this->ordered_items[$index].')');
  1611. }
  1612. }
  1613. if ($this->debug > 2) {
  1614. error_log('In learnpath::first() - First item is '.$this->get_current_item_id());
  1615. }
  1616. }
  1617. /**
  1618. * Gets the js library from the database.
  1619. *
  1620. * @return string The name of the javascript library to be used
  1621. */
  1622. public function get_js_lib()
  1623. {
  1624. $lib = '';
  1625. if (!empty($this->js_lib)) {
  1626. $lib = $this->js_lib;
  1627. }
  1628. return $lib;
  1629. }
  1630. /**
  1631. * Gets the learnpath database ID.
  1632. *
  1633. * @return int Learnpath ID in the lp table
  1634. */
  1635. public function get_id()
  1636. {
  1637. if (!empty($this->lp_id)) {
  1638. return (int) $this->lp_id;
  1639. }
  1640. return 0;
  1641. }
  1642. /**
  1643. * Gets the last element URL.
  1644. *
  1645. * @return string URL to load into the viewer
  1646. */
  1647. public function get_last()
  1648. {
  1649. // This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1650. if (count($this->ordered_items) > 0) {
  1651. $this->index = count($this->ordered_items) - 1;
  1652. return $this->ordered_items[$this->index];
  1653. }
  1654. return false;
  1655. }
  1656. /**
  1657. * Get the last element in the first level.
  1658. * Unlike learnpath::get_last this function doesn't consider the subsection' elements.
  1659. *
  1660. * @return mixed
  1661. */
  1662. public function getLastInFirstLevel()
  1663. {
  1664. try {
  1665. $lastId = Database::getManager()
  1666. ->createQuery('SELECT i.iid FROM ChamiloCourseBundle:CLpItem i
  1667. WHERE i.lpId = :lp AND i.parentItemId = 0 AND i.itemType != :type ORDER BY i.displayOrder DESC')
  1668. ->setMaxResults(1)
  1669. ->setParameters(['lp' => $this->lp_id, 'type' => TOOL_LP_FINAL_ITEM])
  1670. ->getSingleScalarResult();
  1671. return $lastId;
  1672. } catch (Exception $exception) {
  1673. return 0;
  1674. }
  1675. }
  1676. /**
  1677. * Gets the navigation bar for the learnpath display screen.
  1678. *
  1679. * @param string $barId
  1680. *
  1681. * @return string The HTML string to use as a navigation bar
  1682. */
  1683. public function get_navigation_bar($barId = '')
  1684. {
  1685. if (empty($barId)) {
  1686. $barId = 'control-top';
  1687. }
  1688. $lpId = $this->lp_id;
  1689. $mycurrentitemid = $this->get_current_item_id();
  1690. $reportingText = get_lang('Reporting');
  1691. $previousText = get_lang('ScormPrevious');
  1692. $nextText = get_lang('ScormNext');
  1693. $fullScreenText = get_lang('ScormExitFullScreen');
  1694. $settings = api_get_configuration_value('lp_view_settings');
  1695. $display = isset($settings['display']) ? $settings['display'] : false;
  1696. $reportingIcon = '
  1697. <a class="icon-toolbar"
  1698. id="stats_link"
  1699. href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lpId.'"
  1700. onclick="window.parent.API.save_asset(); return true;"
  1701. target="content_name" title="'.$reportingText.'">
  1702. <span class="fa fa-info"></span><span class="sr-only">'.$reportingText.'</span>
  1703. </a>';
  1704. if (!empty($display)) {
  1705. $showReporting = isset($display['show_reporting_icon']) ? $display['show_reporting_icon'] : true;
  1706. if ($showReporting === false) {
  1707. $reportingIcon = '';
  1708. }
  1709. }
  1710. $hideArrows = false;
  1711. if (isset($settings['display']) && isset($settings['display']['hide_lp_arrow_navigation'])) {
  1712. $hideArrows = $settings['display']['hide_lp_arrow_navigation'];
  1713. }
  1714. $previousIcon = '';
  1715. $nextIcon = '';
  1716. if ($hideArrows === false) {
  1717. $previousIcon = '
  1718. <a class="icon-toolbar" id="scorm-previous" href="#"
  1719. onclick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="'.$previousText.'">
  1720. <span class="fa fa-chevron-left"></span><span class="sr-only">'.$previousText.'</span>
  1721. </a>';
  1722. $nextIcon = '
  1723. <a class="icon-toolbar" id="scorm-next" href="#"
  1724. onclick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="'.$nextText.'">
  1725. <span class="fa fa-chevron-right"></span><span class="sr-only">'.$nextText.'</span>
  1726. </a>';
  1727. }
  1728. if ($this->mode === 'fullscreen') {
  1729. $navbar = '
  1730. <span id="'.$barId.'" class="buttons">
  1731. '.$reportingIcon.'
  1732. '.$previousIcon.'
  1733. '.$nextIcon.'
  1734. <a class="icon-toolbar" id="view-embedded"
  1735. href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1736. <span class="fa fa-columns"></span><span class="sr-only">'.$fullScreenText.'</span>
  1737. </a>
  1738. </span>';
  1739. } else {
  1740. $navbar = '
  1741. <span id="'.$barId.'" class="buttons text-right">
  1742. '.$reportingIcon.'
  1743. '.$previousIcon.'
  1744. '.$nextIcon.'
  1745. </span>';
  1746. }
  1747. return $navbar;
  1748. }
  1749. /**
  1750. * Gets the next resource in queue (url).
  1751. *
  1752. * @return string URL to load into the viewer
  1753. */
  1754. public function get_next_index()
  1755. {
  1756. // TODO
  1757. $index = $this->index;
  1758. $index++;
  1759. while (
  1760. !empty($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') &&
  1761. $index < $this->max_ordered_items
  1762. ) {
  1763. $index++;
  1764. if ($index == $this->max_ordered_items) {
  1765. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1766. return $this->index;
  1767. }
  1768. return $index;
  1769. }
  1770. }
  1771. if (empty($this->ordered_items[$index])) {
  1772. return $this->index;
  1773. }
  1774. return $index;
  1775. }
  1776. /**
  1777. * Gets item_id for the next element.
  1778. *
  1779. * @return int Next item (DB) ID
  1780. */
  1781. public function get_next_item_id()
  1782. {
  1783. $new_index = $this->get_next_index();
  1784. if (!empty($new_index)) {
  1785. if (isset($this->ordered_items[$new_index])) {
  1786. return $this->ordered_items[$new_index];
  1787. }
  1788. }
  1789. return 0;
  1790. }
  1791. /**
  1792. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...).
  1793. *
  1794. * Generally, the package provided is in the form of a zip file, so the function
  1795. * has been written to test a zip file. If not a zip, the function will return the
  1796. * default return value: ''
  1797. *
  1798. * @param string $file_path the path to the file
  1799. * @param string $file_name the original name of the file
  1800. *
  1801. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1802. */
  1803. public static function get_package_type($file_path, $file_name)
  1804. {
  1805. // Get name of the zip file without the extension.
  1806. $file_info = pathinfo($file_name);
  1807. $extension = $file_info['extension']; // Extension only.
  1808. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), [
  1809. 'dll',
  1810. 'exe',
  1811. ])) {
  1812. return 'oogie';
  1813. }
  1814. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), [
  1815. 'dll',
  1816. 'exe',
  1817. ])) {
  1818. return 'woogie';
  1819. }
  1820. $zipFile = new PclZip($file_path);
  1821. // Check the zip content (real size and file extension).
  1822. $zipContentArray = $zipFile->listContent();
  1823. $package_type = '';
  1824. $manifest = '';
  1825. $aicc_match_crs = 0;
  1826. $aicc_match_au = 0;
  1827. $aicc_match_des = 0;
  1828. $aicc_match_cst = 0;
  1829. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1830. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1831. foreach ($zipContentArray as $thisContent) {
  1832. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1833. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1834. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1835. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1836. $package_type = 'scorm';
  1837. break; // Exit the foreach loop.
  1838. } elseif (
  1839. preg_match('/aicc\//i', $thisContent['filename']) ||
  1840. in_array(
  1841. strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)),
  1842. ['crs', 'au', 'des', 'cst']
  1843. )
  1844. ) {
  1845. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1846. switch ($ext) {
  1847. case 'crs':
  1848. $aicc_match_crs = 1;
  1849. break;
  1850. case 'au':
  1851. $aicc_match_au = 1;
  1852. break;
  1853. case 'des':
  1854. $aicc_match_des = 1;
  1855. break;
  1856. case 'cst':
  1857. $aicc_match_cst = 1;
  1858. break;
  1859. default:
  1860. break;
  1861. }
  1862. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1863. } else {
  1864. $package_type = '';
  1865. }
  1866. }
  1867. }
  1868. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1869. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1870. $package_type = 'aicc';
  1871. }
  1872. // Try with chamilo course builder
  1873. if (empty($package_type)) {
  1874. $package_type = 'chamilo';
  1875. }
  1876. return $package_type;
  1877. }
  1878. /**
  1879. * Gets the previous resource in queue (url). Also initialises time values for this viewing.
  1880. *
  1881. * @return string URL to load into the viewer
  1882. */
  1883. public function get_previous_index()
  1884. {
  1885. $index = $this->index;
  1886. if (isset($this->ordered_items[$index - 1])) {
  1887. $index--;
  1888. while (isset($this->ordered_items[$index]) &&
  1889. ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')
  1890. ) {
  1891. $index--;
  1892. if ($index < 0) {
  1893. return $this->index;
  1894. }
  1895. }
  1896. }
  1897. return $index;
  1898. }
  1899. /**
  1900. * Gets item_id for the next element.
  1901. *
  1902. * @return int Previous item (DB) ID
  1903. */
  1904. public function get_previous_item_id()
  1905. {
  1906. $index = $this->get_previous_index();
  1907. return $this->ordered_items[$index];
  1908. }
  1909. /**
  1910. * Returns the HTML necessary to print a mediaplayer block inside a page.
  1911. *
  1912. * @param int $lpItemId
  1913. * @param string $autostart
  1914. *
  1915. * @return string The mediaplayer HTML
  1916. */
  1917. public function get_mediaplayer($lpItemId, $autostart = 'true')
  1918. {
  1919. $course_id = api_get_course_int_id();
  1920. $_course = api_get_course_info();
  1921. if (empty($_course)) {
  1922. return '';
  1923. }
  1924. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1925. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  1926. $lpItemId = (int) $lpItemId;
  1927. /** @var learnpathItem $item */
  1928. $item = isset($this->items[$lpItemId]) ? $this->items[$lpItemId] : null;
  1929. $itemViewId = 0;
  1930. if ($item) {
  1931. $itemViewId = (int) $item->db_item_view_id;
  1932. }
  1933. // Getting all the information about the item.
  1934. $sql = "SELECT lpi.audio, lpi.item_type, lp_view.status
  1935. FROM $tbl_lp_item as lpi
  1936. INNER JOIN $tbl_lp_item_view as lp_view
  1937. ON (lpi.iid = lp_view.lp_item_id)
  1938. WHERE
  1939. lp_view.iid = $itemViewId AND
  1940. lpi.iid = $lpItemId AND
  1941. lp_view.c_id = $course_id";
  1942. $result = Database::query($sql);
  1943. $row = Database::fetch_assoc($result);
  1944. $output = '';
  1945. if (!empty($row['audio'])) {
  1946. $list = $_SESSION['oLP']->get_toc();
  1947. switch ($row['item_type']) {
  1948. case 'quiz':
  1949. $type_quiz = false;
  1950. foreach ($list as $toc) {
  1951. if ($toc['id'] == $_SESSION['oLP']->current) {
  1952. $type_quiz = true;
  1953. }
  1954. }
  1955. if ($type_quiz) {
  1956. if ($_SESSION['oLP']->prevent_reinit == 1) {
  1957. $autostart_audio = $row['status'] === 'completed' ? 'false' : 'true';
  1958. } else {
  1959. $autostart_audio = $autostart;
  1960. }
  1961. }
  1962. break;
  1963. case TOOL_READOUT_TEXT:;
  1964. $autostart_audio = 'false';
  1965. break;
  1966. default:
  1967. $autostart_audio = 'true';
  1968. }
  1969. $courseInfo = api_get_course_info();
  1970. $audio = $row['audio'];
  1971. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  1972. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  1973. if (!file_exists($file)) {
  1974. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  1975. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  1976. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  1977. }
  1978. $player = Display::getMediaPlayer(
  1979. $file,
  1980. [
  1981. 'id' => 'lp_audio_media_player',
  1982. 'url' => $url,
  1983. 'autoplay' => $autostart_audio,
  1984. 'width' => '100%',
  1985. ]
  1986. );
  1987. // The mp3 player.
  1988. $output = '<div id="container">';
  1989. $output .= $player;
  1990. $output .= '</div>';
  1991. }
  1992. return $output;
  1993. }
  1994. /**
  1995. * @param int $studentId
  1996. * @param int $prerequisite
  1997. * @param array $courseInfo
  1998. * @param int $sessionId
  1999. *
  2000. * @return bool
  2001. */
  2002. public static function isBlockedByPrerequisite(
  2003. $studentId,
  2004. $prerequisite,
  2005. $courseInfo,
  2006. $sessionId
  2007. ) {
  2008. if (empty($courseInfo)) {
  2009. return false;
  2010. }
  2011. $courseId = $courseInfo['real_id'];
  2012. $allow = api_get_configuration_value('allow_teachers_to_access_blocked_lp_by_prerequisite');
  2013. if ($allow) {
  2014. if (api_is_allowed_to_edit() ||
  2015. api_is_platform_admin(true) ||
  2016. api_is_drh() ||
  2017. api_is_coach($sessionId, $courseId, false)
  2018. ) {
  2019. return false;
  2020. }
  2021. }
  2022. $isBlocked = false;
  2023. if (!empty($prerequisite)) {
  2024. $progress = self::getProgress(
  2025. $prerequisite,
  2026. $studentId,
  2027. $courseId,
  2028. $sessionId
  2029. );
  2030. if ($progress < 100) {
  2031. $isBlocked = true;
  2032. }
  2033. if (Tracking::minimumTimeAvailable($sessionId, $courseId)) {
  2034. // Block if it does not exceed minimum time
  2035. // Minimum time (in minutes) to pass the learning path
  2036. $accumulateWorkTime = self::getAccumulateWorkTimePrerequisite($prerequisite, $courseId);
  2037. if ($accumulateWorkTime > 0) {
  2038. // Total time in course (sum of times in learning paths from course)
  2039. $accumulateWorkTimeTotal = self::getAccumulateWorkTimeTotal($courseId);
  2040. // Connect with the plugin_licences_course_session table
  2041. // which indicates what percentage of the time applies
  2042. // Minimum connection percentage
  2043. $perc = 100;
  2044. // Time from the course
  2045. $tc = $accumulateWorkTimeTotal;
  2046. // Percentage of the learning paths
  2047. $pl = $accumulateWorkTime / $accumulateWorkTimeTotal;
  2048. // Minimum time for each learning path
  2049. $accumulateWorkTime = ($pl * $tc * $perc / 100);
  2050. // Spent time (in seconds) so far in the learning path
  2051. $lpTimeList = Tracking::getCalculateTime($studentId, $courseId, $sessionId);
  2052. $lpTime = isset($lpTimeList[TOOL_LEARNPATH][$prerequisite]) ? $lpTimeList[TOOL_LEARNPATH][$prerequisite] : 0;
  2053. if ($lpTime < ($accumulateWorkTime * 60)) {
  2054. $isBlocked = true;
  2055. }
  2056. }
  2057. }
  2058. }
  2059. return $isBlocked;
  2060. }
  2061. /**
  2062. * Checks if the learning path is visible for student after the progress
  2063. * of its prerequisite is completed, considering the time availability and
  2064. * the LP visibility.
  2065. *
  2066. * @param int $lp_id
  2067. * @param int $student_id
  2068. * @param array $courseInfo
  2069. * @param int $sessionId
  2070. *
  2071. * @return bool
  2072. */
  2073. public static function is_lp_visible_for_student(
  2074. $lp_id,
  2075. $student_id,
  2076. $courseInfo = [],
  2077. $sessionId = 0
  2078. ) {
  2079. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  2080. $lp_id = (int) $lp_id;
  2081. $sessionId = (int) $sessionId;
  2082. if (empty($courseInfo)) {
  2083. return false;
  2084. }
  2085. if (empty($sessionId)) {
  2086. $sessionId = api_get_session_id();
  2087. }
  2088. $courseId = $courseInfo['real_id'];
  2089. $itemInfo = api_get_item_property_info(
  2090. $courseId,
  2091. TOOL_LEARNPATH,
  2092. $lp_id,
  2093. $sessionId
  2094. );
  2095. // If the item was deleted.
  2096. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2097. return false;
  2098. }
  2099. // @todo remove this query and load the row info as a parameter
  2100. $table = Database::get_course_table(TABLE_LP_MAIN);
  2101. // Get current prerequisite
  2102. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2103. FROM $table
  2104. WHERE iid = $lp_id";
  2105. $rs = Database::query($sql);
  2106. $now = time();
  2107. if (Database::num_rows($rs) > 0) {
  2108. $row = Database::fetch_array($rs, 'ASSOC');
  2109. $prerequisite = $row['prerequisite'];
  2110. $is_visible = true;
  2111. $isBlocked = self::isBlockedByPrerequisite(
  2112. $student_id,
  2113. $prerequisite,
  2114. $courseInfo,
  2115. $sessionId
  2116. );
  2117. if ($isBlocked) {
  2118. $is_visible = false;
  2119. }
  2120. // Also check the time availability of the LP
  2121. if ($is_visible) {
  2122. // Adding visibility restrictions
  2123. if (!empty($row['publicated_on'])) {
  2124. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2125. $is_visible = false;
  2126. }
  2127. }
  2128. // Blocking empty start times see BT#2800
  2129. global $_custom;
  2130. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2131. $_custom['lps_hidden_when_no_start_date']
  2132. ) {
  2133. if (empty($row['publicated_on'])) {
  2134. $is_visible = false;
  2135. }
  2136. }
  2137. if (!empty($row['expired_on'])) {
  2138. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2139. $is_visible = false;
  2140. }
  2141. }
  2142. }
  2143. if ($is_visible) {
  2144. $subscriptionSettings = self::getSubscriptionSettings();
  2145. // Check if the subscription users/group to a LP is ON
  2146. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 &&
  2147. $subscriptionSettings['allow_add_users_to_lp'] === true
  2148. ) {
  2149. // Try group
  2150. $is_visible = false;
  2151. // Checking only the user visibility
  2152. $userVisibility = api_get_item_visibility(
  2153. $courseInfo,
  2154. 'learnpath',
  2155. $row['id'],
  2156. $sessionId,
  2157. $student_id,
  2158. 'LearnpathSubscription'
  2159. );
  2160. if ($userVisibility == 1) {
  2161. $is_visible = true;
  2162. } else {
  2163. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id, $courseId);
  2164. if (!empty($userGroups)) {
  2165. foreach ($userGroups as $groupInfo) {
  2166. $groupId = $groupInfo['iid'];
  2167. $userVisibility = api_get_item_visibility(
  2168. $courseInfo,
  2169. 'learnpath',
  2170. $row['id'],
  2171. $sessionId,
  2172. null,
  2173. 'LearnpathSubscription',
  2174. $groupId
  2175. );
  2176. if ($userVisibility == 1) {
  2177. $is_visible = true;
  2178. break;
  2179. }
  2180. }
  2181. }
  2182. }
  2183. }
  2184. }
  2185. return $is_visible;
  2186. }
  2187. return false;
  2188. }
  2189. /**
  2190. * @param int $lpId
  2191. * @param int $userId
  2192. * @param int $courseId
  2193. * @param int $sessionId
  2194. *
  2195. * @return int
  2196. */
  2197. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2198. {
  2199. $lpId = (int) $lpId;
  2200. $userId = (int) $userId;
  2201. $courseId = (int) $courseId;
  2202. $sessionId = (int) $sessionId;
  2203. $sessionCondition = api_get_session_condition($sessionId);
  2204. $table = Database::get_course_table(TABLE_LP_VIEW);
  2205. $sql = "SELECT progress FROM $table
  2206. WHERE
  2207. c_id = $courseId AND
  2208. lp_id = $lpId AND
  2209. user_id = $userId $sessionCondition ";
  2210. $res = Database::query($sql);
  2211. $progress = 0;
  2212. if (Database::num_rows($res) > 0) {
  2213. $row = Database::fetch_array($res);
  2214. $progress = (int) $row['progress'];
  2215. }
  2216. return $progress;
  2217. }
  2218. /**
  2219. * @param array $lpList
  2220. * @param int $userId
  2221. * @param int $courseId
  2222. * @param int $sessionId
  2223. *
  2224. * @return array
  2225. */
  2226. public static function getProgressFromLpList($lpList, $userId, $courseId, $sessionId = 0)
  2227. {
  2228. $lpList = array_map('intval', $lpList);
  2229. if (empty($lpList)) {
  2230. return [];
  2231. }
  2232. $lpList = implode("','", $lpList);
  2233. $userId = (int) $userId;
  2234. $courseId = (int) $courseId;
  2235. $sessionId = (int) $sessionId;
  2236. $sessionCondition = api_get_session_condition($sessionId);
  2237. $table = Database::get_course_table(TABLE_LP_VIEW);
  2238. $sql = "SELECT lp_id, progress FROM $table
  2239. WHERE
  2240. c_id = $courseId AND
  2241. lp_id IN ('".$lpList."') AND
  2242. user_id = $userId $sessionCondition ";
  2243. $res = Database::query($sql);
  2244. if (Database::num_rows($res) > 0) {
  2245. $list = [];
  2246. while ($row = Database::fetch_array($res)) {
  2247. $list[$row['lp_id']] = $row['progress'];
  2248. }
  2249. return $list;
  2250. }
  2251. return [];
  2252. }
  2253. /**
  2254. * Displays a progress bar
  2255. * completed so far.
  2256. *
  2257. * @param int $percentage Progress value to display
  2258. * @param string $text_add Text to display near the progress value
  2259. *
  2260. * @return string HTML string containing the progress bar
  2261. */
  2262. public static function get_progress_bar($percentage = -1, $text_add = '')
  2263. {
  2264. $text = $percentage.$text_add;
  2265. $output = '<div class="progress">
  2266. <div id="progress_bar_value"
  2267. class="progress-bar progress-bar-success" role="progressbar"
  2268. aria-valuenow="'.$percentage.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2269. '.$text.'
  2270. </div>
  2271. </div>';
  2272. return $output;
  2273. }
  2274. /**
  2275. * @param string $mode can be '%' or 'abs'
  2276. * otherwise this value will be used $this->progress_bar_mode
  2277. *
  2278. * @return string
  2279. */
  2280. public function getProgressBar($mode = null)
  2281. {
  2282. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2283. return self::get_progress_bar($percentage, $text_add);
  2284. }
  2285. /**
  2286. * Gets the progress bar info to display inside the progress bar.
  2287. * Also used by scorm_api.php.
  2288. *
  2289. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2290. * we display a number of completed elements per total elements
  2291. * @param int $add Additional steps to fake as completed
  2292. *
  2293. * @return array Percentage or number and symbol (% or /xx)
  2294. */
  2295. public function get_progress_bar_text($mode = '', $add = 0)
  2296. {
  2297. if (empty($mode)) {
  2298. $mode = $this->progress_bar_mode;
  2299. }
  2300. $total_items = $this->getTotalItemsCountWithoutDirs();
  2301. $completeItems = $this->get_complete_items_count();
  2302. if ($add != 0) {
  2303. $completeItems += $add;
  2304. }
  2305. $text = '';
  2306. if ($completeItems > $total_items) {
  2307. $completeItems = $total_items;
  2308. }
  2309. $percentage = 0;
  2310. if ($mode == '%') {
  2311. if ($total_items > 0) {
  2312. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2313. }
  2314. $percentage = number_format($percentage, 0);
  2315. $text = '%';
  2316. } elseif ($mode === 'abs') {
  2317. $percentage = $completeItems;
  2318. $text = '/'.$total_items;
  2319. }
  2320. return [
  2321. $percentage,
  2322. $text,
  2323. ];
  2324. }
  2325. /**
  2326. * Gets the progress bar mode.
  2327. *
  2328. * @return string The progress bar mode attribute
  2329. */
  2330. public function get_progress_bar_mode()
  2331. {
  2332. if (!empty($this->progress_bar_mode)) {
  2333. return $this->progress_bar_mode;
  2334. }
  2335. return '%';
  2336. }
  2337. /**
  2338. * Gets the learnpath theme (remote or local).
  2339. *
  2340. * @return string Learnpath theme
  2341. */
  2342. public function get_theme()
  2343. {
  2344. if (!empty($this->theme)) {
  2345. return $this->theme;
  2346. }
  2347. return '';
  2348. }
  2349. /**
  2350. * Gets the learnpath session id.
  2351. *
  2352. * @return int
  2353. */
  2354. public function get_lp_session_id()
  2355. {
  2356. if (!empty($this->lp_session_id)) {
  2357. return (int) $this->lp_session_id;
  2358. }
  2359. return 0;
  2360. }
  2361. /**
  2362. * Gets the learnpath image.
  2363. *
  2364. * @return string Web URL of the LP image
  2365. */
  2366. public function get_preview_image()
  2367. {
  2368. if (!empty($this->preview_image)) {
  2369. return $this->preview_image;
  2370. }
  2371. return '';
  2372. }
  2373. /**
  2374. * @param string $size
  2375. * @param string $path_type
  2376. *
  2377. * @return bool|string
  2378. */
  2379. public function get_preview_image_path($size = null, $path_type = 'web')
  2380. {
  2381. $preview_image = $this->get_preview_image();
  2382. if (isset($preview_image) && !empty($preview_image)) {
  2383. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2384. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2385. if (isset($size)) {
  2386. $info = pathinfo($preview_image);
  2387. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2388. if (file_exists($image_sys_path.$image_custom_size)) {
  2389. if ($path_type == 'web') {
  2390. return $image_path.$image_custom_size;
  2391. } else {
  2392. return $image_sys_path.$image_custom_size;
  2393. }
  2394. }
  2395. } else {
  2396. if ($path_type == 'web') {
  2397. return $image_path.$preview_image;
  2398. } else {
  2399. return $image_sys_path.$preview_image;
  2400. }
  2401. }
  2402. }
  2403. return false;
  2404. }
  2405. /**
  2406. * Gets the learnpath author.
  2407. *
  2408. * @return string LP's author
  2409. */
  2410. public function get_author()
  2411. {
  2412. if (!empty($this->author)) {
  2413. return $this->author;
  2414. }
  2415. return '';
  2416. }
  2417. /**
  2418. * Gets hide table of contents.
  2419. *
  2420. * @return int
  2421. */
  2422. public function getHideTableOfContents()
  2423. {
  2424. return (int) $this->hide_toc_frame;
  2425. }
  2426. /**
  2427. * Generate a new prerequisites string for a given item. If this item was a sco and
  2428. * its prerequisites were strings (instead of IDs), then transform those strings into
  2429. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2430. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2431. * same rule as the scormExport() method.
  2432. *
  2433. * @param int $item_id Item ID
  2434. *
  2435. * @return string Prerequisites string ready for the export as SCORM
  2436. */
  2437. public function get_scorm_prereq_string($item_id)
  2438. {
  2439. if ($this->debug > 0) {
  2440. error_log('In learnpath::get_scorm_prereq_string()');
  2441. }
  2442. if (!is_object($this->items[$item_id])) {
  2443. return false;
  2444. }
  2445. /** @var learnpathItem $oItem */
  2446. $oItem = $this->items[$item_id];
  2447. $prereq = $oItem->get_prereq_string();
  2448. if (empty($prereq)) {
  2449. return '';
  2450. }
  2451. if (preg_match('/^\d+$/', $prereq) &&
  2452. isset($this->items[$prereq]) &&
  2453. is_object($this->items[$prereq])
  2454. ) {
  2455. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2456. // then simply return it (with the ITEM_ prefix).
  2457. //return 'ITEM_' . $prereq;
  2458. return $this->items[$prereq]->ref;
  2459. } else {
  2460. if (isset($this->refs_list[$prereq])) {
  2461. // It's a simple string item from which the ID can be found in the refs list,
  2462. // so we can transform it directly to an ID for export.
  2463. return $this->items[$this->refs_list[$prereq]]->ref;
  2464. } elseif (isset($this->refs_list['ITEM_'.$prereq])) {
  2465. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2466. } else {
  2467. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2468. // and replace them, one by one, by the internal IDs (chamilo db)
  2469. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2470. // by a space as well.
  2471. $find = [
  2472. '&',
  2473. '|',
  2474. '~',
  2475. '=',
  2476. '<>',
  2477. '{',
  2478. '}',
  2479. '*',
  2480. '(',
  2481. ')',
  2482. ];
  2483. $replace = [
  2484. ' ',
  2485. ' ',
  2486. ' ',
  2487. ' ',
  2488. ' ',
  2489. ' ',
  2490. ' ',
  2491. ' ',
  2492. ' ',
  2493. ' ',
  2494. ];
  2495. $prereq_mod = str_replace($find, $replace, $prereq);
  2496. $ids = explode(' ', $prereq_mod);
  2497. foreach ($ids as $id) {
  2498. $id = trim($id);
  2499. if (isset($this->refs_list[$id])) {
  2500. $prereq = preg_replace(
  2501. '/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/',
  2502. 'ITEM_'.$this->refs_list[$id],
  2503. $prereq
  2504. );
  2505. }
  2506. }
  2507. return $prereq;
  2508. }
  2509. }
  2510. }
  2511. /**
  2512. * Returns the XML DOM document's node.
  2513. *
  2514. * @param resource $children Reference to a list of objects to search for the given ITEM_*
  2515. * @param string $id The identifier to look for
  2516. *
  2517. * @return mixed The reference to the element found with that identifier. False if not found
  2518. */
  2519. public function get_scorm_xml_node(&$children, $id)
  2520. {
  2521. for ($i = 0; $i < $children->length; $i++) {
  2522. $item_temp = $children->item($i);
  2523. if ($item_temp->nodeName == 'item') {
  2524. if ($item_temp->getAttribute('identifier') == $id) {
  2525. return $item_temp;
  2526. }
  2527. }
  2528. $subchildren = $item_temp->childNodes;
  2529. if ($subchildren && $subchildren->length > 0) {
  2530. $val = $this->get_scorm_xml_node($subchildren, $id);
  2531. if (is_object($val)) {
  2532. return $val;
  2533. }
  2534. }
  2535. }
  2536. return false;
  2537. }
  2538. /**
  2539. * Gets the status list for all LP's items.
  2540. *
  2541. * @return array Array of [index] => [item ID => current status]
  2542. */
  2543. public function get_items_status_list()
  2544. {
  2545. $list = [];
  2546. foreach ($this->ordered_items as $item_id) {
  2547. $list[] = [
  2548. $item_id => $this->items[$item_id]->get_status(),
  2549. ];
  2550. }
  2551. return $list;
  2552. }
  2553. /**
  2554. * Return the number of interactions for the given learnpath Item View ID.
  2555. * This method can be used as static.
  2556. *
  2557. * @param int $lp_iv_id Item View ID
  2558. * @param int $course_id course id
  2559. *
  2560. * @return int
  2561. */
  2562. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2563. {
  2564. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2565. $lp_iv_id = (int) $lp_iv_id;
  2566. $course_id = (int) $course_id;
  2567. $sql = "SELECT count(*) FROM $table
  2568. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2569. $res = Database::query($sql);
  2570. $num = 0;
  2571. if (Database::num_rows($res)) {
  2572. $row = Database::fetch_array($res);
  2573. $num = $row[0];
  2574. }
  2575. return $num;
  2576. }
  2577. /**
  2578. * Return the interactions as an array for the given lp_iv_id.
  2579. * This method can be used as static.
  2580. *
  2581. * @param int $lp_iv_id Learnpath Item View ID
  2582. *
  2583. * @return array
  2584. *
  2585. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2586. */
  2587. public static function get_iv_interactions_array($lp_iv_id, $course_id = 0)
  2588. {
  2589. $course_id = empty($course_id) ? api_get_course_int_id() : (int) $course_id;
  2590. $list = [];
  2591. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2592. $lp_iv_id = (int) $lp_iv_id;
  2593. if (empty($lp_iv_id) || empty($course_id)) {
  2594. return [];
  2595. }
  2596. $sql = "SELECT * FROM $table
  2597. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2598. ORDER BY order_id ASC";
  2599. $res = Database::query($sql);
  2600. $num = Database::num_rows($res);
  2601. if ($num > 0) {
  2602. $list[] = [
  2603. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2604. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2605. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2606. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2607. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2608. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2609. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2610. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES),
  2611. 'student_response_formatted' => '',
  2612. ];
  2613. while ($row = Database::fetch_array($res)) {
  2614. $studentResponseFormatted = urldecode($row['student_response']);
  2615. $content_student_response = explode('__|', $studentResponseFormatted);
  2616. if (count($content_student_response) > 0) {
  2617. if (count($content_student_response) >= 3) {
  2618. // Pop the element off the end of array.
  2619. array_pop($content_student_response);
  2620. }
  2621. $studentResponseFormatted = implode(',', $content_student_response);
  2622. }
  2623. $list[] = [
  2624. 'order_id' => $row['order_id'] + 1,
  2625. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2626. 'type' => $row['interaction_type'],
  2627. 'time' => $row['completion_time'],
  2628. 'correct_responses' => '', // Hide correct responses from students.
  2629. 'student_response' => $row['student_response'],
  2630. 'result' => $row['result'],
  2631. 'latency' => $row['latency'],
  2632. 'student_response_formatted' => $studentResponseFormatted,
  2633. ];
  2634. }
  2635. }
  2636. return $list;
  2637. }
  2638. /**
  2639. * Return the number of objectives for the given learnpath Item View ID.
  2640. * This method can be used as static.
  2641. *
  2642. * @param int $lp_iv_id Item View ID
  2643. * @param int $course_id Course ID
  2644. *
  2645. * @return int Number of objectives
  2646. */
  2647. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2648. {
  2649. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2650. $course_id = (int) $course_id;
  2651. $lp_iv_id = (int) $lp_iv_id;
  2652. $sql = "SELECT count(*) FROM $table
  2653. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2654. //@todo seems that this always returns 0
  2655. $res = Database::query($sql);
  2656. $num = 0;
  2657. if (Database::num_rows($res)) {
  2658. $row = Database::fetch_array($res);
  2659. $num = $row[0];
  2660. }
  2661. return $num;
  2662. }
  2663. /**
  2664. * Return the objectives as an array for the given lp_iv_id.
  2665. * This method can be used as static.
  2666. *
  2667. * @param int $lpItemViewId Learnpath Item View ID
  2668. * @param int $course_id
  2669. *
  2670. * @return array
  2671. *
  2672. * @todo Translate labels
  2673. */
  2674. public static function get_iv_objectives_array($lpItemViewId = 0, $course_id = 0)
  2675. {
  2676. $course_id = empty($course_id) ? api_get_course_int_id() : (int) $course_id;
  2677. $lpItemViewId = (int) $lpItemViewId;
  2678. if (empty($course_id) || empty($lpItemViewId)) {
  2679. return [];
  2680. }
  2681. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2682. $sql = "SELECT * FROM $table
  2683. WHERE c_id = $course_id AND lp_iv_id = $lpItemViewId
  2684. ORDER BY order_id ASC";
  2685. $res = Database::query($sql);
  2686. $num = Database::num_rows($res);
  2687. $list = [];
  2688. if ($num > 0) {
  2689. $list[] = [
  2690. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2691. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2692. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2693. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2694. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2695. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES),
  2696. ];
  2697. while ($row = Database::fetch_array($res)) {
  2698. $list[] = [
  2699. 'order_id' => $row['order_id'] + 1,
  2700. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F
  2701. 'score_raw' => $row['score_raw'],
  2702. 'score_max' => $row['score_max'],
  2703. 'score_min' => $row['score_min'],
  2704. 'status' => $row['status'],
  2705. ];
  2706. }
  2707. }
  2708. return $list;
  2709. }
  2710. /**
  2711. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2712. * used by get_html_toc() to be ready to display.
  2713. *
  2714. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2715. */
  2716. public function get_toc()
  2717. {
  2718. $toc = [];
  2719. foreach ($this->ordered_items as $item_id) {
  2720. // TODO: Change this link generation and use new function instead.
  2721. $toc[] = [
  2722. 'id' => $item_id,
  2723. 'title' => $this->items[$item_id]->get_title(),
  2724. 'status' => $this->items[$item_id]->get_status(),
  2725. 'level' => $this->items[$item_id]->get_level(),
  2726. 'type' => $this->items[$item_id]->get_type(),
  2727. 'description' => $this->items[$item_id]->get_description(),
  2728. 'path' => $this->items[$item_id]->get_path(),
  2729. 'parent' => $this->items[$item_id]->get_parent(),
  2730. ];
  2731. }
  2732. return $toc;
  2733. }
  2734. /**
  2735. * Generate and return the table of contents for this learnpath. The JS
  2736. * table returned is used inside of scorm_api.php.
  2737. *
  2738. * @param string $varname
  2739. *
  2740. * @return string A JS array variable construction
  2741. */
  2742. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2743. {
  2744. $toc = $varname.' = new Array();';
  2745. foreach ($this->ordered_items as $item_id) {
  2746. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2747. }
  2748. return $toc;
  2749. }
  2750. /**
  2751. * Gets the learning path type.
  2752. *
  2753. * @param bool $get_name Return the name? If false, return the ID. Default is false.
  2754. *
  2755. * @return mixed Type ID or name, depending on the parameter
  2756. */
  2757. public function get_type($get_name = false)
  2758. {
  2759. $res = false;
  2760. if (!empty($this->type) && (!$get_name)) {
  2761. $res = $this->type;
  2762. }
  2763. return $res;
  2764. }
  2765. /**
  2766. * Gets the learning path type as static method.
  2767. *
  2768. * @param int $lp_id
  2769. *
  2770. * @return mixed Type ID or name, depending on the parameter
  2771. */
  2772. public static function get_type_static($lp_id = 0)
  2773. {
  2774. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2775. $lp_id = (int) $lp_id;
  2776. $sql = "SELECT lp_type FROM $tbl_lp
  2777. WHERE iid = $lp_id";
  2778. $res = Database::query($sql);
  2779. if ($res === false) {
  2780. return null;
  2781. }
  2782. if (Database::num_rows($res) <= 0) {
  2783. return null;
  2784. }
  2785. $row = Database::fetch_array($res);
  2786. return $row['lp_type'];
  2787. }
  2788. /**
  2789. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2790. * This method can be used as abstract and is recursive.
  2791. *
  2792. * @param int $lp Learnpath ID
  2793. * @param int $parent Parent ID of the items to look for
  2794. * @param int $course_id
  2795. *
  2796. * @return array Ordered list of item IDs (empty array on error)
  2797. */
  2798. public static function get_flat_ordered_items_list($lp = 1, $parent = 0, $course_id = 0)
  2799. {
  2800. if (empty($course_id)) {
  2801. $course_id = api_get_course_int_id();
  2802. } else {
  2803. $course_id = (int) $course_id;
  2804. }
  2805. $list = [];
  2806. if (empty($lp)) {
  2807. return $list;
  2808. }
  2809. $lp = (int) $lp;
  2810. $parent = (int) $parent;
  2811. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2812. $sql = "SELECT iid FROM $tbl_lp_item
  2813. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2814. ORDER BY display_order";
  2815. $res = Database::query($sql);
  2816. while ($row = Database::fetch_array($res)) {
  2817. $sublist = self::get_flat_ordered_items_list(
  2818. $lp,
  2819. $row['iid'],
  2820. $course_id
  2821. );
  2822. $list[] = $row['iid'];
  2823. foreach ($sublist as $item) {
  2824. $list[] = $item;
  2825. }
  2826. }
  2827. return $list;
  2828. }
  2829. /**
  2830. * @return array
  2831. */
  2832. public static function getChapterTypes()
  2833. {
  2834. return [
  2835. 'dir',
  2836. ];
  2837. }
  2838. /**
  2839. * Uses the table generated by get_toc() and returns an HTML-formattedstring ready to display.
  2840. *
  2841. * @param $tree
  2842. *
  2843. * @return array HTML TOC ready to display
  2844. */
  2845. public function getParentToc($tree)
  2846. {
  2847. if (empty($tree)) {
  2848. $tree = $this->get_toc();
  2849. }
  2850. $dirTypes = self::getChapterTypes();
  2851. $myCurrentId = $this->get_current_item_id();
  2852. $listParent = [];
  2853. $listChildren = [];
  2854. $listNotParent = [];
  2855. $list = [];
  2856. foreach ($tree as $subtree) {
  2857. if (in_array($subtree['type'], $dirTypes)) {
  2858. $listChildren = $this->getChildrenToc($tree, $subtree['id']);
  2859. $subtree['children'] = $listChildren;
  2860. if (!empty($subtree['children'])) {
  2861. foreach ($subtree['children'] as $subItem) {
  2862. if ($subItem['id'] == $this->current) {
  2863. $subtree['parent_current'] = 'in';
  2864. $subtree['current'] = 'on';
  2865. }
  2866. }
  2867. }
  2868. $listParent[] = $subtree;
  2869. }
  2870. if (!in_array($subtree['type'], $dirTypes) && $subtree['parent'] == null) {
  2871. $classStatus = [
  2872. 'not attempted' => 'scorm_not_attempted',
  2873. 'incomplete' => 'scorm_not_attempted',
  2874. 'failed' => 'scorm_failed',
  2875. 'completed' => 'scorm_completed',
  2876. 'passed' => 'scorm_completed',
  2877. 'succeeded' => 'scorm_completed',
  2878. 'browsed' => 'scorm_completed',
  2879. ];
  2880. if (isset($classStatus[$subtree['status']])) {
  2881. $cssStatus = $classStatus[$subtree['status']];
  2882. }
  2883. $title = Security::remove_XSS($subtree['title']);
  2884. unset($subtree['title']);
  2885. if (empty($title)) {
  2886. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $subtree['id']);
  2887. }
  2888. $classStyle = null;
  2889. if ($subtree['id'] == $this->current) {
  2890. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  2891. } elseif (!in_array($subtree['type'], $dirTypes)) {
  2892. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  2893. }
  2894. $subtree['title'] = $title;
  2895. $subtree['class'] = $classStyle.' '.$cssStatus;
  2896. $subtree['url'] = $this->get_link('http', $subtree['id'], $tree);
  2897. $subtree['current_id'] = $myCurrentId;
  2898. $listNotParent[] = $subtree;
  2899. }
  2900. }
  2901. $list['are_parents'] = $listParent;
  2902. $list['not_parents'] = $listNotParent;
  2903. return $list;
  2904. }
  2905. /**
  2906. * Uses the table generated by get_toc() and returns an HTML-formattedstring ready to display.
  2907. *
  2908. * @param array $tree
  2909. * @param int $id
  2910. * @param bool $parent
  2911. *
  2912. * @return array HTML TOC ready to display
  2913. */
  2914. public function getChildrenToc($tree, $id, $parent = true)
  2915. {
  2916. if (empty($tree)) {
  2917. $tree = $this->get_toc();
  2918. }
  2919. $dirTypes = self::getChapterTypes();
  2920. $currentItemId = $this->get_current_item_id();
  2921. $list = [];
  2922. $classStatus = [
  2923. 'not attempted' => 'scorm_not_attempted',
  2924. 'incomplete' => 'scorm_not_attempted',
  2925. 'failed' => 'scorm_failed',
  2926. 'completed' => 'scorm_completed',
  2927. 'passed' => 'scorm_completed',
  2928. 'succeeded' => 'scorm_completed',
  2929. 'browsed' => 'scorm_completed',
  2930. ];
  2931. foreach ($tree as $subtree) {
  2932. $subtree['tree'] = null;
  2933. if (!in_array($subtree['type'], $dirTypes) && $subtree['parent'] == $id) {
  2934. if ($subtree['id'] == $this->current) {
  2935. $subtree['current'] = 'active';
  2936. } else {
  2937. $subtree['current'] = null;
  2938. }
  2939. if (isset($classStatus[$subtree['status']])) {
  2940. $cssStatus = $classStatus[$subtree['status']];
  2941. }
  2942. $title = Security::remove_XSS($subtree['title']);
  2943. unset($subtree['title']);
  2944. if (empty($title)) {
  2945. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $subtree['id']);
  2946. }
  2947. $classStyle = null;
  2948. if ($subtree['id'] == $this->current) {
  2949. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  2950. } elseif (!in_array($subtree['type'], $dirTypes)) {
  2951. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  2952. }
  2953. if (in_array($subtree['type'], $dirTypes)) {
  2954. $subtree['title'] = stripslashes($title);
  2955. } else {
  2956. $subtree['title'] = $title;
  2957. $subtree['class'] = $classStyle.' '.$cssStatus;
  2958. $subtree['url'] = $this->get_link('http', $subtree['id'], $tree);
  2959. $subtree['current_id'] = $currentItemId;
  2960. }
  2961. $list[] = $subtree;
  2962. }
  2963. }
  2964. return $list;
  2965. }
  2966. /**
  2967. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display.
  2968. *
  2969. * @param array $toc_list
  2970. *
  2971. * @return array HTML TOC ready to display
  2972. */
  2973. public function getListArrayToc($toc_list = [])
  2974. {
  2975. if (empty($toc_list)) {
  2976. $toc_list = $this->get_toc();
  2977. }
  2978. // Temporary variables.
  2979. $currentItemId = $this->get_current_item_id();
  2980. $list = [];
  2981. $arrayList = [];
  2982. $classStatus = [
  2983. 'not attempted' => 'scorm_not_attempted',
  2984. 'incomplete' => 'scorm_not_attempted',
  2985. 'failed' => 'scorm_failed',
  2986. 'completed' => 'scorm_completed',
  2987. 'passed' => 'scorm_completed',
  2988. 'succeeded' => 'scorm_completed',
  2989. 'browsed' => 'scorm_completed',
  2990. ];
  2991. foreach ($toc_list as $item) {
  2992. $list['id'] = $item['id'];
  2993. $list['status'] = $item['status'];
  2994. $cssStatus = null;
  2995. if (isset($classStatus[$item['status']])) {
  2996. $cssStatus = $classStatus[$item['status']];
  2997. }
  2998. $classStyle = ' ';
  2999. $dirTypes = self::getChapterTypes();
  3000. if (in_array($item['type'], $dirTypes)) {
  3001. $classStyle = 'scorm_item_section ';
  3002. }
  3003. if ($item['id'] == $this->current) {
  3004. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  3005. } elseif (!in_array($item['type'], $dirTypes)) {
  3006. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  3007. }
  3008. $title = $item['title'];
  3009. if (empty($title)) {
  3010. $title = self::rl_get_resource_name(
  3011. api_get_course_id(),
  3012. $this->get_id(),
  3013. $item['id']
  3014. );
  3015. }
  3016. $title = Security::remove_XSS($item['title']);
  3017. if (empty($item['description'])) {
  3018. $list['description'] = $title;
  3019. } else {
  3020. $list['description'] = $item['description'];
  3021. }
  3022. $list['class'] = $classStyle.' '.$cssStatus;
  3023. $list['level'] = $item['level'];
  3024. $list['type'] = $item['type'];
  3025. if (in_array($item['type'], $dirTypes)) {
  3026. $list['css_level'] = 'level_'.$item['level'];
  3027. } else {
  3028. $list['css_level'] = 'level_'.$item['level'].' scorm_type_'.self::format_scorm_type_item($item['type']);
  3029. }
  3030. if (in_array($item['type'], $dirTypes)) {
  3031. $list['title'] = stripslashes($title);
  3032. } else {
  3033. $list['title'] = stripslashes($title);
  3034. $list['url'] = $this->get_link('http', $item['id'], $toc_list);
  3035. $list['current_id'] = $currentItemId;
  3036. }
  3037. $arrayList[] = $list;
  3038. }
  3039. return $arrayList;
  3040. }
  3041. /**
  3042. * Returns an HTML-formatted string ready to display with teacher buttons
  3043. * in LP view menu.
  3044. *
  3045. * @return string HTML TOC ready to display
  3046. */
  3047. public function get_teacher_toc_buttons()
  3048. {
  3049. $isAllow = api_is_allowed_to_edit(null, true, false, false);
  3050. $hideIcons = api_get_configuration_value('hide_teacher_icons_lp');
  3051. $html = '';
  3052. if ($isAllow && $hideIcons == false) {
  3053. if ($this->get_lp_session_id() == api_get_session_id()) {
  3054. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3055. $html .= '<div class="btn-group">';
  3056. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?".api_get_cidreq()."&action=build&lp_id=".$this->lp_id."&isStudentView=false' target='_parent'>".
  3057. Display::returnFontAwesomeIcon('street-view').get_lang('Overview')."</a>";
  3058. $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'>".
  3059. Display::returnFontAwesomeIcon('pencil').get_lang('Edit')."</a>";
  3060. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&action=edit&lp_id=".$this->lp_id.'&isStudentView=false">'.
  3061. Display::returnFontAwesomeIcon('cog').get_lang('Settings').'</a>';
  3062. $html .= '</div>';
  3063. $html .= '</div>';
  3064. }
  3065. }
  3066. return $html;
  3067. }
  3068. /**
  3069. * Gets the learnpath maker name - generally the editor's name.
  3070. *
  3071. * @return string Learnpath maker name
  3072. */
  3073. public function get_maker()
  3074. {
  3075. if (!empty($this->maker)) {
  3076. return $this->maker;
  3077. }
  3078. return '';
  3079. }
  3080. /**
  3081. * Gets the learnpath name/title.
  3082. *
  3083. * @return string Learnpath name/title
  3084. */
  3085. public function get_name()
  3086. {
  3087. if (!empty($this->name)) {
  3088. return $this->name;
  3089. }
  3090. return 'N/A';
  3091. }
  3092. /**
  3093. * @return string
  3094. */
  3095. public function getNameNoTags()
  3096. {
  3097. return strip_tags($this->get_name());
  3098. }
  3099. /**
  3100. * Gets a link to the resource from the present location, depending on item ID.
  3101. *
  3102. * @param string $type Type of link expected
  3103. * @param int $item_id Learnpath item ID
  3104. * @param bool $provided_toc
  3105. *
  3106. * @return string $provided_toc Link to the lp_item resource
  3107. */
  3108. public function get_link($type = 'http', $item_id = 0, $provided_toc = false)
  3109. {
  3110. $course_id = $this->get_course_int_id();
  3111. $item_id = (int) $item_id;
  3112. if (empty($item_id)) {
  3113. $item_id = $this->get_current_item_id();
  3114. if (empty($item_id)) {
  3115. //still empty, this means there was no item_id given and we are not in an object context or
  3116. //the object property is empty, return empty link
  3117. $this->first();
  3118. return '';
  3119. }
  3120. }
  3121. $file = '';
  3122. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3123. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3124. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3125. $sql = "SELECT
  3126. l.lp_type as ltype,
  3127. l.path as lpath,
  3128. li.item_type as litype,
  3129. li.path as lipath,
  3130. li.parameters as liparams
  3131. FROM $lp_table l
  3132. INNER JOIN $lp_item_table li
  3133. ON (li.lp_id = l.iid)
  3134. WHERE
  3135. li.iid = $item_id
  3136. ";
  3137. $res = Database::query($sql);
  3138. if (Database::num_rows($res) > 0) {
  3139. $row = Database::fetch_array($res);
  3140. $lp_type = $row['ltype'];
  3141. $lp_path = $row['lpath'];
  3142. $lp_item_type = $row['litype'];
  3143. $lp_item_path = $row['lipath'];
  3144. $lp_item_params = $row['liparams'];
  3145. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3146. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3147. }
  3148. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3149. if ($type === 'http') {
  3150. //web path
  3151. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path();
  3152. } else {
  3153. $course_path = $sys_course_path; //system path
  3154. }
  3155. // Fixed issue BT#1272 - If the item type is a Chamilo Item (quiz, link, etc),
  3156. // then change the lp type to thread it as a normal Chamilo LP not a SCO.
  3157. if (in_array(
  3158. $lp_item_type,
  3159. ['quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication']
  3160. )
  3161. ) {
  3162. $lp_type = 1;
  3163. }
  3164. // Now go through the specific cases to get the end of the path
  3165. // @todo Use constants instead of int values.
  3166. switch ($lp_type) {
  3167. case 1:
  3168. $file = self::rl_get_resource_link_for_learnpath(
  3169. $course_id,
  3170. $this->get_id(),
  3171. $item_id,
  3172. $this->get_view_id()
  3173. );
  3174. switch ($lp_item_type) {
  3175. case 'document':
  3176. // Shows a button to download the file instead of just downloading the file directly.
  3177. $documentPathInfo = pathinfo($file);
  3178. if (isset($documentPathInfo['extension'])) {
  3179. $parsed = parse_url($documentPathInfo['extension']);
  3180. if (isset($parsed['path'])) {
  3181. $extension = $parsed['path'];
  3182. $extensionsToDownload = [
  3183. 'zip',
  3184. 'ppt',
  3185. 'pptx',
  3186. 'ods',
  3187. 'xlsx',
  3188. 'xls',
  3189. 'csv',
  3190. 'doc',
  3191. 'docx',
  3192. 'dot',
  3193. ];
  3194. if (in_array($extension, $extensionsToDownload)) {
  3195. $file = api_get_path(WEB_CODE_PATH).
  3196. 'lp/embed.php?type=download&source=file&lp_item_id='.$item_id.'&'.api_get_cidreq();
  3197. }
  3198. }
  3199. }
  3200. break;
  3201. case 'dir':
  3202. $file = 'lp_content.php?type=dir';
  3203. break;
  3204. case 'link':
  3205. if (Link::is_youtube_link($file)) {
  3206. $src = Link::get_youtube_video_id($file);
  3207. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=youtube&source='.$src;
  3208. } elseif (Link::isVimeoLink($file)) {
  3209. $src = Link::getVimeoLinkId($file);
  3210. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=vimeo&source='.$src;
  3211. } else {
  3212. // If the current site is HTTPS and the link is
  3213. // HTTP, browsers will refuse opening the link
  3214. $urlId = api_get_current_access_url_id();
  3215. $url = api_get_access_url($urlId, false);
  3216. $protocol = substr($url['url'], 0, 5);
  3217. if ($protocol === 'https') {
  3218. $linkProtocol = substr($file, 0, 5);
  3219. if ($linkProtocol === 'http:') {
  3220. //this is the special intervention case
  3221. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3222. }
  3223. }
  3224. }
  3225. break;
  3226. case 'quiz':
  3227. // Check how much attempts of a exercise exits in lp
  3228. $lp_item_id = $this->get_current_item_id();
  3229. $lp_view_id = $this->get_view_id();
  3230. $prevent_reinit = null;
  3231. if (isset($this->items[$this->current])) {
  3232. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3233. }
  3234. if (empty($provided_toc)) {
  3235. if ($this->debug > 0) {
  3236. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3237. }
  3238. $list = $this->get_toc();
  3239. } else {
  3240. if ($this->debug > 0) {
  3241. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3242. }
  3243. $list = $provided_toc;
  3244. }
  3245. $type_quiz = false;
  3246. foreach ($list as $toc) {
  3247. if ($toc['id'] == $lp_item_id && $toc['type'] == 'quiz') {
  3248. $type_quiz = true;
  3249. }
  3250. }
  3251. if ($type_quiz) {
  3252. $lp_item_id = (int) $lp_item_id;
  3253. $lp_view_id = (int) $lp_view_id;
  3254. $sql = "SELECT count(*) FROM $lp_item_view_table
  3255. WHERE
  3256. c_id = $course_id AND
  3257. lp_item_id='".$lp_item_id."' AND
  3258. lp_view_id ='".$lp_view_id."' AND
  3259. status='completed'";
  3260. $result = Database::query($sql);
  3261. $row_count = Database:: fetch_row($result);
  3262. $count_item_view = (int) $row_count[0];
  3263. $not_multiple_attempt = 0;
  3264. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3265. $not_multiple_attempt = 1;
  3266. }
  3267. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3268. }
  3269. break;
  3270. }
  3271. $tmp_array = explode('/', $file);
  3272. $document_name = $tmp_array[count($tmp_array) - 1];
  3273. if (strpos($document_name, '_DELETED_')) {
  3274. $file = 'blank.php?error=document_deleted';
  3275. }
  3276. break;
  3277. case 2:
  3278. if ($this->debug > 2) {
  3279. error_log('In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3280. }
  3281. if ($lp_item_type != 'dir') {
  3282. // Quite complex here:
  3283. // We want to make sure 'http://' (and similar) links can
  3284. // be loaded as is (withouth the Chamilo path in front) but
  3285. // some contents use this form: resource.htm?resource=http://blablabla
  3286. // which means we have to find a protocol at the path's start, otherwise
  3287. // it should not be considered as an external URL.
  3288. // if ($this->prerequisites_match($item_id)) {
  3289. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3290. if ($this->debug > 2) {
  3291. error_log('In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3292. }
  3293. // Distant url, return as is.
  3294. $file = $lp_item_path;
  3295. } else {
  3296. if ($this->debug > 2) {
  3297. error_log('In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3298. }
  3299. // Prevent getting untranslatable urls.
  3300. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3301. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3302. // Prepare the path.
  3303. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3304. // TODO: Fix this for urls with protocol header.
  3305. $file = str_replace('//', '/', $file);
  3306. $file = str_replace(':/', '://', $file);
  3307. if (substr($lp_path, -1) == '/') {
  3308. $lp_path = substr($lp_path, 0, -1);
  3309. }
  3310. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3311. // if file not found.
  3312. $decoded = html_entity_decode($lp_item_path);
  3313. list($decoded) = explode('?', $decoded);
  3314. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3315. $file = self::rl_get_resource_link_for_learnpath(
  3316. $course_id,
  3317. $this->get_id(),
  3318. $item_id,
  3319. $this->get_view_id()
  3320. );
  3321. if (empty($file)) {
  3322. $file = 'blank.php?error=document_not_found';
  3323. } else {
  3324. $tmp_array = explode('/', $file);
  3325. $document_name = $tmp_array[count($tmp_array) - 1];
  3326. if (strpos($document_name, '_DELETED_')) {
  3327. $file = 'blank.php?error=document_deleted';
  3328. } else {
  3329. $file = 'blank.php?error=document_not_found';
  3330. }
  3331. }
  3332. } else {
  3333. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3334. }
  3335. }
  3336. }
  3337. // We want to use parameters if they were defined in the imsmanifest
  3338. if (strpos($file, 'blank.php') === false) {
  3339. $lp_item_params = ltrim($lp_item_params, '?');
  3340. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3341. }
  3342. } else {
  3343. $file = 'lp_content.php?type=dir';
  3344. }
  3345. break;
  3346. case 3:
  3347. if ($this->debug > 2) {
  3348. error_log('In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3349. }
  3350. // Formatting AICC HACP append URL.
  3351. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(api_get_path(WEB_CODE_PATH).'lp/aicc_hacp.php').'&';
  3352. if (!empty($lp_item_params)) {
  3353. $aicc_append .= $lp_item_params.'&';
  3354. }
  3355. if ($lp_item_type != 'dir') {
  3356. // Quite complex here:
  3357. // We want to make sure 'http://' (and similar) links can
  3358. // be loaded as is (withouth the Chamilo path in front) but
  3359. // some contents use this form: resource.htm?resource=http://blablabla
  3360. // which means we have to find a protocol at the path's start, otherwise
  3361. // it should not be considered as an external URL.
  3362. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3363. if ($this->debug > 2) {
  3364. error_log('In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3365. }
  3366. // Distant url, return as is.
  3367. $file = $lp_item_path;
  3368. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3369. /*
  3370. if (stristr($file,'<servername>') !== false) {
  3371. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3372. }
  3373. */
  3374. if (stripos($file, '<servername>') !== false) {
  3375. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3376. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3377. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3378. }
  3379. $file .= $aicc_append;
  3380. } else {
  3381. if ($this->debug > 2) {
  3382. error_log('In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3383. }
  3384. // Prevent getting untranslatable urls.
  3385. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3386. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3387. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3388. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3389. // TODO: Fix this for urls with protocol header.
  3390. $file = str_replace('//', '/', $file);
  3391. $file = str_replace(':/', '://', $file);
  3392. $file .= $aicc_append;
  3393. }
  3394. } else {
  3395. $file = 'lp_content.php?type=dir';
  3396. }
  3397. break;
  3398. case 4:
  3399. break;
  3400. default:
  3401. break;
  3402. }
  3403. // Replace &amp; by & because &amp; will break URL with params
  3404. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3405. }
  3406. if ($this->debug > 2) {
  3407. error_log('In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3408. }
  3409. return $file;
  3410. }
  3411. /**
  3412. * Gets the latest usable view or generate a new one.
  3413. *
  3414. * @param int $attempt_num Optional attempt number. If none given, takes the highest from the lp_view table
  3415. *
  3416. * @return int DB lp_view id
  3417. */
  3418. public function get_view($attempt_num = 0)
  3419. {
  3420. $search = '';
  3421. // Use $attempt_num to enable multi-views management (disabled so far).
  3422. if ($attempt_num != 0 && intval(strval($attempt_num)) == $attempt_num) {
  3423. $search = 'AND view_count = '.$attempt_num;
  3424. }
  3425. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3426. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3427. $course_id = api_get_course_int_id();
  3428. $sessionId = api_get_session_id();
  3429. $sql = "SELECT iid, view_count FROM $lp_view_table
  3430. WHERE
  3431. c_id = $course_id AND
  3432. lp_id = ".$this->get_id()." AND
  3433. user_id = ".$this->get_user_id()." AND
  3434. session_id = $sessionId
  3435. $search
  3436. ORDER BY view_count DESC";
  3437. $res = Database::query($sql);
  3438. if (Database::num_rows($res) > 0) {
  3439. $row = Database::fetch_array($res);
  3440. $this->lp_view_id = $row['iid'];
  3441. } elseif (!api_is_invitee()) {
  3442. // There is no database record, create one.
  3443. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) VALUES
  3444. ($course_id, ".$this->get_id().",".$this->get_user_id().", 1, $sessionId)";
  3445. Database::query($sql);
  3446. $id = Database::insert_id();
  3447. $this->lp_view_id = $id;
  3448. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3449. Database::query($sql);
  3450. }
  3451. return $this->lp_view_id;
  3452. }
  3453. /**
  3454. * Gets the current view id.
  3455. *
  3456. * @return int View ID (from lp_view)
  3457. */
  3458. public function get_view_id()
  3459. {
  3460. if (!empty($this->lp_view_id)) {
  3461. return (int) $this->lp_view_id;
  3462. }
  3463. return 0;
  3464. }
  3465. /**
  3466. * Gets the update queue.
  3467. *
  3468. * @return array Array containing IDs of items to be updated by JavaScript
  3469. */
  3470. public function get_update_queue()
  3471. {
  3472. return $this->update_queue;
  3473. }
  3474. /**
  3475. * Gets the user ID.
  3476. *
  3477. * @return int User ID
  3478. */
  3479. public function get_user_id()
  3480. {
  3481. if (!empty($this->user_id)) {
  3482. return (int) $this->user_id;
  3483. }
  3484. return false;
  3485. }
  3486. /**
  3487. * Checks if any of the items has an audio element attached.
  3488. *
  3489. * @return bool True or false
  3490. */
  3491. public function has_audio()
  3492. {
  3493. $has = false;
  3494. foreach ($this->items as $i => $item) {
  3495. if (!empty($this->items[$i]->audio)) {
  3496. $has = true;
  3497. break;
  3498. }
  3499. }
  3500. return $has;
  3501. }
  3502. /**
  3503. * Moves an item up and down at its level.
  3504. *
  3505. * @param int $id Item to move up and down
  3506. * @param string $direction Direction 'up' or 'down'
  3507. *
  3508. * @return bool|int
  3509. */
  3510. public function move_item($id, $direction)
  3511. {
  3512. $course_id = api_get_course_int_id();
  3513. if (empty($id) || empty($direction)) {
  3514. return false;
  3515. }
  3516. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3517. $sql_sel = "SELECT *
  3518. FROM $tbl_lp_item
  3519. WHERE
  3520. iid = $id
  3521. ";
  3522. $res_sel = Database::query($sql_sel);
  3523. // Check if elem exists.
  3524. if (Database::num_rows($res_sel) < 1) {
  3525. return false;
  3526. }
  3527. // Gather data.
  3528. $row = Database::fetch_array($res_sel);
  3529. $previous = $row['previous_item_id'];
  3530. $next = $row['next_item_id'];
  3531. $display = $row['display_order'];
  3532. $parent = $row['parent_item_id'];
  3533. $lp = $row['lp_id'];
  3534. // Update the item (switch with previous/next one).
  3535. switch ($direction) {
  3536. case 'up':
  3537. if ($display > 1) {
  3538. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3539. WHERE iid = $previous";
  3540. $res_sel2 = Database::query($sql_sel2);
  3541. if (Database::num_rows($res_sel2) < 1) {
  3542. $previous_previous = 0;
  3543. }
  3544. // Gather data.
  3545. $row2 = Database::fetch_array($res_sel2);
  3546. $previous_previous = $row2['previous_item_id'];
  3547. // Update previous_previous item (switch "next" with current).
  3548. if ($previous_previous != 0) {
  3549. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3550. next_item_id = $id
  3551. WHERE iid = $previous_previous";
  3552. Database::query($sql_upd2);
  3553. }
  3554. // Update previous item (switch with current).
  3555. if ($previous != 0) {
  3556. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3557. next_item_id = $next,
  3558. previous_item_id = $id,
  3559. display_order = display_order +1
  3560. WHERE iid = $previous";
  3561. Database::query($sql_upd2);
  3562. }
  3563. // Update current item (switch with previous).
  3564. if ($id != 0) {
  3565. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3566. next_item_id = $previous,
  3567. previous_item_id = $previous_previous,
  3568. display_order = display_order-1
  3569. WHERE c_id = ".$course_id." AND id = $id";
  3570. Database::query($sql_upd2);
  3571. }
  3572. // Update next item (new previous item).
  3573. if (!empty($next)) {
  3574. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3575. WHERE iid = $next";
  3576. Database::query($sql_upd2);
  3577. }
  3578. $display = $display - 1;
  3579. }
  3580. break;
  3581. case 'down':
  3582. if ($next != 0) {
  3583. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3584. WHERE iid = $next";
  3585. $res_sel2 = Database::query($sql_sel2);
  3586. if (Database::num_rows($res_sel2) < 1) {
  3587. $next_next = 0;
  3588. }
  3589. // Gather data.
  3590. $row2 = Database::fetch_array($res_sel2);
  3591. $next_next = $row2['next_item_id'];
  3592. // Update previous item (switch with current).
  3593. if ($previous != 0) {
  3594. $sql_upd2 = "UPDATE $tbl_lp_item
  3595. SET next_item_id = $next
  3596. WHERE iid = $previous";
  3597. Database::query($sql_upd2);
  3598. }
  3599. // Update current item (switch with previous).
  3600. if ($id != 0) {
  3601. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3602. previous_item_id = $next,
  3603. next_item_id = $next_next,
  3604. display_order = display_order + 1
  3605. WHERE iid = $id";
  3606. Database::query($sql_upd2);
  3607. }
  3608. // Update next item (new previous item).
  3609. if ($next != 0) {
  3610. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3611. previous_item_id = $previous,
  3612. next_item_id = $id,
  3613. display_order = display_order-1
  3614. WHERE iid = $next";
  3615. Database::query($sql_upd2);
  3616. }
  3617. // Update next_next item (switch "previous" with current).
  3618. if ($next_next != 0) {
  3619. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3620. previous_item_id = $id
  3621. WHERE iid = $next_next";
  3622. Database::query($sql_upd2);
  3623. }
  3624. $display = $display + 1;
  3625. }
  3626. break;
  3627. default:
  3628. return false;
  3629. }
  3630. return $display;
  3631. }
  3632. /**
  3633. * Move a LP up (display_order).
  3634. *
  3635. * @param int $lp_id Learnpath ID
  3636. * @param int $categoryId Category ID
  3637. *
  3638. * @return bool
  3639. */
  3640. public static function move_up($lp_id, $categoryId = 0)
  3641. {
  3642. $courseId = api_get_course_int_id();
  3643. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3644. $categoryCondition = '';
  3645. if (!empty($categoryId)) {
  3646. $categoryId = (int) $categoryId;
  3647. $categoryCondition = " AND category_id = $categoryId";
  3648. }
  3649. $sql = "SELECT * FROM $lp_table
  3650. WHERE c_id = $courseId
  3651. $categoryCondition
  3652. ORDER BY display_order";
  3653. $res = Database::query($sql);
  3654. if ($res === false) {
  3655. return false;
  3656. }
  3657. $lps = [];
  3658. $lp_order = [];
  3659. $num = Database::num_rows($res);
  3660. // First check the order is correct, globally (might be wrong because
  3661. // of versions < 1.8.4)
  3662. if ($num > 0) {
  3663. $i = 1;
  3664. while ($row = Database::fetch_array($res)) {
  3665. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3666. $sql = "UPDATE $lp_table SET display_order = $i
  3667. WHERE iid = ".$row['iid'];
  3668. Database::query($sql);
  3669. }
  3670. $row['display_order'] = $i;
  3671. $lps[$row['iid']] = $row;
  3672. $lp_order[$i] = $row['iid'];
  3673. $i++;
  3674. }
  3675. }
  3676. if ($num > 1) { // If there's only one element, no need to sort.
  3677. $order = $lps[$lp_id]['display_order'];
  3678. if ($order > 1) { // If it's the first element, no need to move up.
  3679. $sql = "UPDATE $lp_table SET display_order = $order
  3680. WHERE iid = ".$lp_order[$order - 1];
  3681. Database::query($sql);
  3682. $sql = "UPDATE $lp_table SET display_order = ".($order - 1)."
  3683. WHERE iid = $lp_id";
  3684. Database::query($sql);
  3685. }
  3686. }
  3687. return true;
  3688. }
  3689. /**
  3690. * Move a learnpath down (display_order).
  3691. *
  3692. * @param int $lp_id Learnpath ID
  3693. * @param int $categoryId Category ID
  3694. *
  3695. * @return bool
  3696. */
  3697. public static function move_down($lp_id, $categoryId = 0)
  3698. {
  3699. $courseId = api_get_course_int_id();
  3700. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3701. $categoryCondition = '';
  3702. if (!empty($categoryId)) {
  3703. $categoryId = (int) $categoryId;
  3704. $categoryCondition = " AND category_id = $categoryId";
  3705. }
  3706. $sql = "SELECT * FROM $lp_table
  3707. WHERE c_id = $courseId
  3708. $categoryCondition
  3709. ORDER BY display_order";
  3710. $res = Database::query($sql);
  3711. if ($res === false) {
  3712. return false;
  3713. }
  3714. $lps = [];
  3715. $lp_order = [];
  3716. $num = Database::num_rows($res);
  3717. $max = 0;
  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. $max = $i;
  3724. if ($row['display_order'] != $i) {
  3725. // If we find a gap in the order, we need to fix it.
  3726. $sql = "UPDATE $lp_table SET display_order = $i
  3727. WHERE iid = ".$row['iid'];
  3728. Database::query($sql);
  3729. }
  3730. $row['display_order'] = $i;
  3731. $lps[$row['iid']] = $row;
  3732. $lp_order[$i] = $row['iid'];
  3733. $i++;
  3734. }
  3735. }
  3736. if ($num > 1) { // If there's only one element, no need to sort.
  3737. $order = $lps[$lp_id]['display_order'];
  3738. if ($order < $max) { // If it's the first element, no need to move up.
  3739. $sql = "UPDATE $lp_table SET display_order = $order
  3740. WHERE iid = ".$lp_order[$order + 1];
  3741. Database::query($sql);
  3742. $sql = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3743. WHERE iid = $lp_id";
  3744. Database::query($sql);
  3745. }
  3746. }
  3747. return true;
  3748. }
  3749. /**
  3750. * Updates learnpath attributes to point to the next element
  3751. * The last part is similar to set_current_item but processing the other way around.
  3752. */
  3753. public function next()
  3754. {
  3755. if ($this->debug > 0) {
  3756. error_log('In learnpath::next()', 0);
  3757. }
  3758. $this->last = $this->get_current_item_id();
  3759. $this->items[$this->last]->save(
  3760. false,
  3761. $this->prerequisites_match($this->last)
  3762. );
  3763. $this->autocomplete_parents($this->last);
  3764. $new_index = $this->get_next_index();
  3765. if ($this->debug > 2) {
  3766. error_log('New index: '.$new_index, 0);
  3767. }
  3768. $this->index = $new_index;
  3769. if ($this->debug > 2) {
  3770. error_log('Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3771. }
  3772. $this->current = $this->ordered_items[$new_index];
  3773. if ($this->debug > 2) {
  3774. error_log('new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3775. }
  3776. }
  3777. /**
  3778. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3779. * class, this might be redefined to allow several behaviours depending on the document type.
  3780. *
  3781. * @param int $id Resource ID
  3782. */
  3783. public function open($id)
  3784. {
  3785. // TODO:
  3786. // set the current resource attribute to this resource
  3787. // switch on element type (redefine in child class?)
  3788. // set status for this item to "opened"
  3789. // start timer
  3790. // initialise score
  3791. $this->index = 0; //or = the last item seen (see $this->last)
  3792. }
  3793. /**
  3794. * Check that all prerequisites are fulfilled. Returns true and an
  3795. * empty string on success, returns false
  3796. * and the prerequisite string on error.
  3797. * This function is based on the rules for aicc_script language as
  3798. * described in the SCORM 1.2 CAM documentation page 108.
  3799. *
  3800. * @param int $itemId Optional item ID. If none given, uses the current open item.
  3801. *
  3802. * @return bool true if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites
  3803. * string otherwise
  3804. */
  3805. public function prerequisites_match($itemId = null)
  3806. {
  3807. $allow = api_get_configuration_value('allow_teachers_to_access_blocked_lp_by_prerequisite');
  3808. if ($allow) {
  3809. if (api_is_allowed_to_edit() ||
  3810. api_is_platform_admin(true) ||
  3811. api_is_drh() ||
  3812. api_is_coach(api_get_session_id(), api_get_course_int_id())
  3813. ) {
  3814. return true;
  3815. }
  3816. }
  3817. $debug = $this->debug;
  3818. if ($debug > 0) {
  3819. error_log('In learnpath::prerequisites_match()');
  3820. }
  3821. if (empty($itemId)) {
  3822. $itemId = $this->current;
  3823. }
  3824. $currentItem = $this->getItem($itemId);
  3825. if ($currentItem) {
  3826. if ($this->type == 2) {
  3827. // Getting prereq from scorm
  3828. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3829. } else {
  3830. $prereq_string = $currentItem->get_prereq_string();
  3831. }
  3832. if (empty($prereq_string)) {
  3833. if ($debug > 0) {
  3834. error_log('Found prereq_string is empty return true');
  3835. }
  3836. return true;
  3837. }
  3838. // Clean spaces.
  3839. $prereq_string = str_replace(' ', '', $prereq_string);
  3840. if ($debug > 0) {
  3841. error_log('Found prereq_string: '.$prereq_string, 0);
  3842. }
  3843. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3844. $result = $currentItem->parse_prereq(
  3845. $prereq_string,
  3846. $this->items,
  3847. $this->refs_list,
  3848. $this->get_user_id()
  3849. );
  3850. if ($result === false) {
  3851. $this->set_error_msg($currentItem->prereq_alert);
  3852. }
  3853. } else {
  3854. $result = true;
  3855. if ($debug > 1) {
  3856. error_log('$this->items['.$itemId.'] was not an object', 0);
  3857. }
  3858. }
  3859. if ($debug > 1) {
  3860. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3861. }
  3862. return $result;
  3863. }
  3864. /**
  3865. * Updates learnpath attributes to point to the previous element
  3866. * The last part is similar to set_current_item but processing the other way around.
  3867. */
  3868. public function previous()
  3869. {
  3870. $this->last = $this->get_current_item_id();
  3871. $this->items[$this->last]->save(
  3872. false,
  3873. $this->prerequisites_match($this->last)
  3874. );
  3875. $this->autocomplete_parents($this->last);
  3876. $new_index = $this->get_previous_index();
  3877. $this->index = $new_index;
  3878. $this->current = $this->ordered_items[$new_index];
  3879. }
  3880. /**
  3881. * Publishes a learnpath. This basically means show or hide the learnpath
  3882. * to normal users.
  3883. * Can be used as abstract.
  3884. *
  3885. * @param int $lp_id Learnpath ID
  3886. * @param int $set_visibility New visibility
  3887. *
  3888. * @return bool
  3889. */
  3890. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3891. {
  3892. $action = 'visible';
  3893. if ($set_visibility != 1) {
  3894. $action = 'invisible';
  3895. self::toggle_publish($lp_id, 'i');
  3896. }
  3897. return api_item_property_update(
  3898. api_get_course_info(),
  3899. TOOL_LEARNPATH,
  3900. $lp_id,
  3901. $action,
  3902. api_get_user_id()
  3903. );
  3904. }
  3905. /**
  3906. * Publishes a learnpath category.
  3907. * This basically means show or hide the learnpath category to normal users.
  3908. *
  3909. * @param int $id
  3910. * @param int $visibility
  3911. *
  3912. * @throws \Doctrine\ORM\NonUniqueResultException
  3913. * @throws \Doctrine\ORM\ORMException
  3914. * @throws \Doctrine\ORM\OptimisticLockException
  3915. * @throws \Doctrine\ORM\TransactionRequiredException
  3916. *
  3917. * @return bool
  3918. */
  3919. public static function toggleCategoryVisibility($id, $visibility = 1)
  3920. {
  3921. $action = 'visible';
  3922. if ($visibility != 1) {
  3923. $action = 'invisible';
  3924. $list = new LearnpathList(
  3925. api_get_user_id(),
  3926. null,
  3927. null,
  3928. null,
  3929. false,
  3930. $id
  3931. );
  3932. $lpList = $list->get_flat_list();
  3933. foreach ($lpList as $lp) {
  3934. self::toggle_visibility($lp['iid'], 0);
  3935. }
  3936. self::toggleCategoryPublish($id, 0);
  3937. }
  3938. return api_item_property_update(
  3939. api_get_course_info(),
  3940. TOOL_LEARNPATH_CATEGORY,
  3941. $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. *
  3951. * @param int $lp_id Learnpath id
  3952. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3953. *
  3954. * @return bool
  3955. */
  3956. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3957. {
  3958. $course_id = api_get_course_int_id();
  3959. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  3960. $lp_id = (int) $lp_id;
  3961. $sql = "SELECT * FROM $tbl_lp
  3962. WHERE iid = $lp_id";
  3963. $result = Database::query($sql);
  3964. if (Database::num_rows($result)) {
  3965. $row = Database::fetch_array($result);
  3966. $name = Database::escape_string($row['name']);
  3967. if ($set_visibility == 'i') {
  3968. $v = 0;
  3969. }
  3970. if ($set_visibility == 'v') {
  3971. $v = 1;
  3972. }
  3973. $session_id = api_get_session_id();
  3974. $session_condition = api_get_session_condition($session_id);
  3975. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  3976. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3977. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3978. $sql = "SELECT * FROM $tbl_tool
  3979. WHERE
  3980. c_id = $course_id AND
  3981. (link = '$link' OR link = '$oldLink') AND
  3982. image = 'scormbuilder.gif' AND
  3983. (
  3984. link LIKE '$link%' OR
  3985. link LIKE '$oldLink%'
  3986. )
  3987. $session_condition
  3988. ";
  3989. $result = Database::query($sql);
  3990. $num = Database::num_rows($result);
  3991. if ($set_visibility == 'i' && $num > 0) {
  3992. $sql = "DELETE FROM $tbl_tool
  3993. WHERE
  3994. c_id = $course_id AND
  3995. (link = '$link' OR link = '$oldLink') AND
  3996. image='scormbuilder.gif'
  3997. $session_condition";
  3998. Database::query($sql);
  3999. } elseif ($set_visibility == 'v' && $num == 0) {
  4000. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  4001. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  4002. Database::query($sql);
  4003. $insertId = Database::insert_id();
  4004. if ($insertId) {
  4005. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  4006. Database::query($sql);
  4007. }
  4008. } elseif ($set_visibility == 'v' && $num > 0) {
  4009. $sql = "UPDATE $tbl_tool SET
  4010. c_id = $course_id,
  4011. name = '$name',
  4012. link = '$link',
  4013. image = 'scormbuilder.gif',
  4014. visibility = '$v',
  4015. admin = '0',
  4016. address = 'pastillegris.gif',
  4017. added_tool = 0,
  4018. session_id = $session_id
  4019. WHERE
  4020. c_id = ".$course_id." AND
  4021. (link = '$link' OR link = '$oldLink') AND
  4022. image='scormbuilder.gif'
  4023. $session_condition
  4024. ";
  4025. Database::query($sql);
  4026. } else {
  4027. // Parameter and database incompatible, do nothing, exit.
  4028. return false;
  4029. }
  4030. } else {
  4031. return false;
  4032. }
  4033. }
  4034. /**
  4035. * Publishes a learnpath.
  4036. * Show or hide the learnpath category on the course homepage.
  4037. *
  4038. * @param int $id
  4039. * @param int $setVisibility
  4040. *
  4041. * @throws \Doctrine\ORM\NonUniqueResultException
  4042. * @throws \Doctrine\ORM\ORMException
  4043. * @throws \Doctrine\ORM\OptimisticLockException
  4044. * @throws \Doctrine\ORM\TransactionRequiredException
  4045. *
  4046. * @return bool
  4047. */
  4048. public static function toggleCategoryPublish($id, $setVisibility = 1)
  4049. {
  4050. $courseId = api_get_course_int_id();
  4051. $sessionId = api_get_session_id();
  4052. $sessionCondition = api_get_session_condition(
  4053. $sessionId,
  4054. true,
  4055. false,
  4056. 't.sessionId'
  4057. );
  4058. $em = Database::getManager();
  4059. /** @var CLpCategory $category */
  4060. $category = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  4061. if (!$category) {
  4062. return false;
  4063. }
  4064. if (empty($courseId)) {
  4065. return false;
  4066. }
  4067. $link = self::getCategoryLinkForTool($id);
  4068. /** @var CTool $tool */
  4069. $tool = $em->createQuery("
  4070. SELECT t FROM ChamiloCourseBundle:CTool t
  4071. WHERE
  4072. t.cId = :course AND
  4073. t.link = :link1 AND
  4074. t.image = 'lp_category.gif' AND
  4075. t.link LIKE :link2
  4076. $sessionCondition
  4077. ")
  4078. ->setParameters([
  4079. 'course' => (int) $courseId,
  4080. 'link1' => $link,
  4081. 'link2' => "$link%",
  4082. ])
  4083. ->getOneOrNullResult();
  4084. if ($setVisibility == 0 && $tool) {
  4085. $em->remove($tool);
  4086. $em->flush();
  4087. return true;
  4088. }
  4089. if ($setVisibility == 1 && !$tool) {
  4090. $tool = new CTool();
  4091. $tool
  4092. ->setCategory('authoring')
  4093. ->setCId($courseId)
  4094. ->setName(strip_tags($category->getName()))
  4095. ->setLink($link)
  4096. ->setImage('lp_category.gif')
  4097. ->setVisibility(1)
  4098. ->setAdmin(0)
  4099. ->setAddress('pastillegris.gif')
  4100. ->setAddedTool(0)
  4101. ->setSessionId($sessionId)
  4102. ->setTarget('_self');
  4103. $em->persist($tool);
  4104. $em->flush();
  4105. $tool->setId($tool->getIid());
  4106. $em->persist($tool);
  4107. $em->flush();
  4108. return true;
  4109. }
  4110. if ($setVisibility == 1 && $tool) {
  4111. $tool
  4112. ->setName(strip_tags($category->getName()))
  4113. ->setVisibility(1);
  4114. $em->persist($tool);
  4115. $em->flush();
  4116. return true;
  4117. }
  4118. return false;
  4119. }
  4120. /**
  4121. * Check if the learnpath category is visible for a user.
  4122. *
  4123. * @param CLpCategory $category
  4124. * @param User $user
  4125. * @param int
  4126. * @param int
  4127. *
  4128. * @return bool
  4129. */
  4130. public static function categoryIsVisibleForStudent(
  4131. CLpCategory $category,
  4132. User $user,
  4133. $courseId = 0,
  4134. $sessionId = 0
  4135. ) {
  4136. $subscriptionSettings = self::getSubscriptionSettings();
  4137. if ($subscriptionSettings['allow_add_users_to_lp_category'] == false) {
  4138. return true;
  4139. }
  4140. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  4141. if ($isAllowedToEdit) {
  4142. return true;
  4143. }
  4144. if (empty($category)) {
  4145. return false;
  4146. }
  4147. $users = $category->getUsers();
  4148. if (empty($users) || !$users->count()) {
  4149. return true;
  4150. }
  4151. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  4152. $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
  4153. if ($category->hasUserAdded($user)) {
  4154. return true;
  4155. }
  4156. $groups = GroupManager::getAllGroupPerUserSubscription($user->getId());
  4157. if (!empty($groups)) {
  4158. $em = Database::getManager();
  4159. /** @var ItemPropertyRepository $itemRepo */
  4160. $itemRepo = $em->getRepository('ChamiloCourseBundle:CItemProperty');
  4161. /** @var CourseRepository $courseRepo */
  4162. $courseRepo = $em->getRepository('ChamiloCoreBundle:Course');
  4163. $session = null;
  4164. if (!empty($sessionId)) {
  4165. $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
  4166. }
  4167. $course = $courseRepo->find($courseId);
  4168. // Subscribed groups to a LP
  4169. $subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem(
  4170. TOOL_LEARNPATH_CATEGORY,
  4171. $category->getId(),
  4172. $course,
  4173. $session
  4174. );
  4175. if (!empty($subscribedGroupsInLp)) {
  4176. $groups = array_column($groups, 'iid');
  4177. /** @var CItemProperty $item */
  4178. foreach ($subscribedGroupsInLp as $item) {
  4179. if ($item->getGroup() &&
  4180. in_array($item->getGroup()->getId(), $groups)
  4181. ) {
  4182. return true;
  4183. }
  4184. }
  4185. }
  4186. }
  4187. return false;
  4188. }
  4189. /**
  4190. * Check if a learnpath category is published as course tool.
  4191. *
  4192. * @param CLpCategory $category
  4193. * @param int $courseId
  4194. *
  4195. * @return bool
  4196. */
  4197. public static function categoryIsPublished(
  4198. CLpCategory $category,
  4199. $courseId
  4200. ) {
  4201. $link = self::getCategoryLinkForTool($category->getId());
  4202. $em = Database::getManager();
  4203. $tools = $em
  4204. ->createQuery("
  4205. SELECT t FROM ChamiloCourseBundle:CTool t
  4206. WHERE t.cId = :course AND
  4207. t.name = :name AND
  4208. t.image = 'lp_category.gif' AND
  4209. t.link LIKE :link
  4210. ")
  4211. ->setParameters([
  4212. 'course' => $courseId,
  4213. 'name' => strip_tags($category->getName()),
  4214. 'link' => "$link%",
  4215. ])
  4216. ->getResult();
  4217. /** @var CTool $tool */
  4218. $tool = current($tools);
  4219. return $tool ? $tool->getVisibility() : false;
  4220. }
  4221. /**
  4222. * Restart the whole learnpath. Return the URL of the first element.
  4223. * Make sure the results are saved with anoter method. This method should probably be redefined in children classes.
  4224. * To use a similar method statically, use the create_new_attempt() method.
  4225. *
  4226. * @return bool
  4227. */
  4228. public function restart()
  4229. {
  4230. if ($this->debug > 0) {
  4231. error_log('In learnpath::restart()', 0);
  4232. }
  4233. // TODO
  4234. // Call autosave method to save the current progress.
  4235. //$this->index = 0;
  4236. if (api_is_invitee()) {
  4237. return false;
  4238. }
  4239. $session_id = api_get_session_id();
  4240. $course_id = api_get_course_int_id();
  4241. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  4242. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  4243. VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  4244. if ($this->debug > 2) {
  4245. error_log('Inserting new lp_view for restart: '.$sql, 0);
  4246. }
  4247. Database::query($sql);
  4248. $view_id = Database::insert_id();
  4249. if ($view_id) {
  4250. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  4251. Database::query($sql);
  4252. $this->lp_view_id = $view_id;
  4253. $this->attempt = $this->attempt + 1;
  4254. } else {
  4255. $this->error = 'Could not insert into item_view table...';
  4256. return false;
  4257. }
  4258. $this->autocomplete_parents($this->current);
  4259. foreach ($this->items as $index => $dummy) {
  4260. $this->items[$index]->restart();
  4261. $this->items[$index]->set_lp_view($this->lp_view_id);
  4262. }
  4263. $this->first();
  4264. return true;
  4265. }
  4266. /**
  4267. * Saves the current item.
  4268. *
  4269. * @return bool
  4270. */
  4271. public function save_current()
  4272. {
  4273. $debug = $this->debug;
  4274. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4275. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4276. if ($debug) {
  4277. error_log('save_current() saving item '.$this->current, 0);
  4278. error_log(''.print_r($this->items, true), 0);
  4279. }
  4280. if (isset($this->items[$this->current]) &&
  4281. is_object($this->items[$this->current])
  4282. ) {
  4283. if ($debug) {
  4284. error_log('Before save last_scorm_session_time: '.$this->items[$this->current]->getLastScormSessionTime());
  4285. }
  4286. $res = $this->items[$this->current]->save(
  4287. false,
  4288. $this->prerequisites_match($this->current)
  4289. );
  4290. $this->autocomplete_parents($this->current);
  4291. $status = $this->items[$this->current]->get_status();
  4292. $this->update_queue[$this->current] = $status;
  4293. if ($debug) {
  4294. error_log('After save last_scorm_session_time: '.$this->items[$this->current]->getLastScormSessionTime());
  4295. }
  4296. return $res;
  4297. }
  4298. return false;
  4299. }
  4300. /**
  4301. * Saves the given item.
  4302. *
  4303. * @param int $item_id Optional (will take from $_REQUEST if null)
  4304. * @param bool $from_outside Save from url params (true) or from current attributes (false). Default true
  4305. *
  4306. * @return bool
  4307. */
  4308. public function save_item($item_id = null, $from_outside = true)
  4309. {
  4310. $debug = $this->debug;
  4311. if ($debug) {
  4312. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  4313. }
  4314. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4315. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4316. if (empty($item_id)) {
  4317. $item_id = (int) $_REQUEST['id'];
  4318. }
  4319. if (empty($item_id)) {
  4320. $item_id = $this->get_current_item_id();
  4321. }
  4322. if (isset($this->items[$item_id]) &&
  4323. is_object($this->items[$item_id])
  4324. ) {
  4325. if ($debug) {
  4326. error_log('Object exists');
  4327. }
  4328. // Saving the item.
  4329. $res = $this->items[$item_id]->save(
  4330. $from_outside,
  4331. $this->prerequisites_match($item_id)
  4332. );
  4333. if ($debug) {
  4334. error_log('update_queue before:');
  4335. error_log(print_r($this->update_queue, 1));
  4336. }
  4337. $this->autocomplete_parents($item_id);
  4338. $status = $this->items[$item_id]->get_status();
  4339. $this->update_queue[$item_id] = $status;
  4340. if ($debug) {
  4341. error_log('get_status(): '.$status);
  4342. error_log('update_queue after:');
  4343. error_log(print_r($this->update_queue, 1));
  4344. }
  4345. return $res;
  4346. }
  4347. return false;
  4348. }
  4349. /**
  4350. * Saves the last item seen's ID only in case.
  4351. */
  4352. public function save_last()
  4353. {
  4354. $course_id = api_get_course_int_id();
  4355. $debug = $this->debug;
  4356. if ($debug) {
  4357. error_log('In learnpath::save_last()', 0);
  4358. }
  4359. $session_condition = api_get_session_condition(
  4360. api_get_session_id(),
  4361. true,
  4362. false
  4363. );
  4364. $table = Database::get_course_table(TABLE_LP_VIEW);
  4365. if (isset($this->current) && !api_is_invitee()) {
  4366. if ($debug) {
  4367. error_log('Saving current item ('.$this->current.') for later review', 0);
  4368. }
  4369. $sql = "UPDATE $table SET
  4370. last_item = ".$this->get_current_item_id()."
  4371. WHERE
  4372. c_id = $course_id AND
  4373. lp_id = ".$this->get_id()." AND
  4374. user_id = ".$this->get_user_id()." ".$session_condition;
  4375. if ($debug) {
  4376. error_log('Saving last item seen : '.$sql, 0);
  4377. }
  4378. Database::query($sql);
  4379. }
  4380. if (!api_is_invitee()) {
  4381. // Save progress.
  4382. list($progress) = $this->get_progress_bar_text('%');
  4383. if ($progress >= 0 && $progress <= 100) {
  4384. $progress = (int) $progress;
  4385. $sql = "UPDATE $table SET
  4386. progress = $progress
  4387. WHERE
  4388. c_id = $course_id AND
  4389. lp_id = ".$this->get_id()." AND
  4390. user_id = ".$this->get_user_id()." ".$session_condition;
  4391. // Ignore errors as some tables might not have the progress field just yet.
  4392. Database::query($sql);
  4393. $this->progress_db = $progress;
  4394. }
  4395. }
  4396. }
  4397. /**
  4398. * Sets the current item ID (checks if valid and authorized first).
  4399. *
  4400. * @param int $item_id New item ID. If not given or not authorized, defaults to current
  4401. */
  4402. public function set_current_item($item_id = null)
  4403. {
  4404. $debug = $this->debug;
  4405. if ($debug) {
  4406. error_log('In learnpath::set_current_item('.$item_id.')', 0);
  4407. }
  4408. if (empty($item_id)) {
  4409. if ($debug) {
  4410. error_log('No new current item given, ignore...', 0);
  4411. }
  4412. // Do nothing.
  4413. } else {
  4414. if ($debug) {
  4415. error_log('New current item given is '.$item_id.'...', 0);
  4416. }
  4417. if (is_numeric($item_id)) {
  4418. $item_id = (int) $item_id;
  4419. // TODO: Check in database here.
  4420. $this->last = $this->current;
  4421. $this->current = $item_id;
  4422. // TODO: Update $this->index as well.
  4423. foreach ($this->ordered_items as $index => $item) {
  4424. if ($item == $this->current) {
  4425. $this->index = $index;
  4426. break;
  4427. }
  4428. }
  4429. if ($debug) {
  4430. error_log('set_current_item('.$item_id.') done. Index is now : '.$this->index);
  4431. }
  4432. } else {
  4433. if ($debug) {
  4434. error_log('set_current_item('.$item_id.') failed. Not a numeric value: ');
  4435. }
  4436. }
  4437. }
  4438. }
  4439. /**
  4440. * Sets the encoding.
  4441. *
  4442. * @param string $enc New encoding
  4443. *
  4444. * @return bool
  4445. *
  4446. * @todo (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4447. */
  4448. public function set_encoding($enc = 'UTF-8')
  4449. {
  4450. $enc = api_refine_encoding_id($enc);
  4451. if (empty($enc)) {
  4452. $enc = api_get_system_encoding();
  4453. }
  4454. if (api_is_encoding_supported($enc)) {
  4455. $lp = $this->get_id();
  4456. if ($lp != 0) {
  4457. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4458. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4459. WHERE iid = ".$lp;
  4460. $res = Database::query($sql);
  4461. return $res;
  4462. }
  4463. }
  4464. return false;
  4465. }
  4466. /**
  4467. * Sets the JS lib setting in the database directly.
  4468. * This is the JavaScript library file this lp needs to load on startup.
  4469. *
  4470. * @param string $lib Proximity setting
  4471. *
  4472. * @return bool True on update success. False otherwise.
  4473. */
  4474. public function set_jslib($lib = '')
  4475. {
  4476. $lp = $this->get_id();
  4477. if ($lp != 0) {
  4478. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4479. $lib = Database::escape_string($lib);
  4480. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4481. WHERE iid = $lp";
  4482. $res = Database::query($sql);
  4483. return $res;
  4484. }
  4485. return false;
  4486. }
  4487. /**
  4488. * Sets the name of the LP maker (publisher) (and save).
  4489. *
  4490. * @param string $name Optional string giving the new content_maker of this learnpath
  4491. *
  4492. * @return bool True
  4493. */
  4494. public function set_maker($name = '')
  4495. {
  4496. if (empty($name)) {
  4497. return false;
  4498. }
  4499. $this->maker = $name;
  4500. $table = Database::get_course_table(TABLE_LP_MAIN);
  4501. $lp_id = $this->get_id();
  4502. $sql = "UPDATE $table SET
  4503. content_maker = '".Database::escape_string($this->maker)."'
  4504. WHERE iid = $lp_id";
  4505. Database::query($sql);
  4506. return true;
  4507. }
  4508. /**
  4509. * Sets the name of the current learnpath (and save).
  4510. *
  4511. * @param string $name Optional string giving the new name of this learnpath
  4512. *
  4513. * @return bool True/False
  4514. */
  4515. public function set_name($name = null)
  4516. {
  4517. if (empty($name)) {
  4518. return false;
  4519. }
  4520. $this->name = Database::escape_string($name);
  4521. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4522. $lp_id = $this->get_id();
  4523. $course_id = $this->course_info['real_id'];
  4524. $sql = "UPDATE $lp_table SET
  4525. name = '".Database::escape_string($this->name)."'
  4526. WHERE iid = $lp_id";
  4527. $result = Database::query($sql);
  4528. // If the lp is visible on the homepage, change his name there.
  4529. if (Database::affected_rows($result)) {
  4530. $session_id = api_get_session_id();
  4531. $session_condition = api_get_session_condition($session_id);
  4532. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  4533. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4534. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4535. WHERE
  4536. c_id = $course_id AND
  4537. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4538. Database::query($sql);
  4539. return true;
  4540. }
  4541. return false;
  4542. }
  4543. /**
  4544. * Set index specified prefix terms for all items in this path.
  4545. *
  4546. * @param string $terms_string Comma-separated list of terms
  4547. * @param string $prefix Xapian term prefix
  4548. *
  4549. * @return bool False on error, true otherwise
  4550. */
  4551. public function set_terms_by_prefix($terms_string, $prefix)
  4552. {
  4553. $course_id = api_get_course_int_id();
  4554. if (api_get_setting('search_enabled') !== 'true') {
  4555. return false;
  4556. }
  4557. if (!extension_loaded('xapian')) {
  4558. return false;
  4559. }
  4560. $terms_string = trim($terms_string);
  4561. $terms = explode(',', $terms_string);
  4562. array_walk($terms, 'trim_value');
  4563. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4564. // Don't do anything if no change, verify only at DB, not the search engine.
  4565. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4566. return false;
  4567. }
  4568. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4569. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4570. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  4571. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4572. $lp_id = (int) $_POST['lp_id'];
  4573. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4574. $result = Database::query($sql);
  4575. $di = new ChamiloIndexer();
  4576. while ($lp_item = Database::fetch_array($result)) {
  4577. // Get search_did.
  4578. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4579. $sql = 'SELECT * FROM %s
  4580. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4581. LIMIT 1';
  4582. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4583. //echo $sql; echo '<br>';
  4584. $res = Database::query($sql);
  4585. if (Database::num_rows($res) > 0) {
  4586. $se_ref = Database::fetch_array($res);
  4587. // Compare terms.
  4588. $doc = $di->get_document($se_ref['search_did']);
  4589. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4590. $xterms = [];
  4591. foreach ($xapian_terms as $xapian_term) {
  4592. $xterms[] = substr($xapian_term['name'], 1);
  4593. }
  4594. $dterms = $terms;
  4595. $missing_terms = array_diff($dterms, $xterms);
  4596. $deprecated_terms = array_diff($xterms, $dterms);
  4597. // Save it to search engine.
  4598. foreach ($missing_terms as $term) {
  4599. $doc->add_term($prefix.$term, 1);
  4600. }
  4601. foreach ($deprecated_terms as $term) {
  4602. $doc->remove_term($prefix.$term);
  4603. }
  4604. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4605. $di->getDb()->flush();
  4606. }
  4607. }
  4608. return true;
  4609. }
  4610. /**
  4611. * Sets the theme of the LP (local/remote) (and save).
  4612. *
  4613. * @param string $name Optional string giving the new theme of this learnpath
  4614. *
  4615. * @return bool Returns true if theme name is not empty
  4616. */
  4617. public function set_theme($name = '')
  4618. {
  4619. $this->theme = $name;
  4620. $table = Database::get_course_table(TABLE_LP_MAIN);
  4621. $lp_id = $this->get_id();
  4622. $sql = "UPDATE $table
  4623. SET theme = '".Database::escape_string($this->theme)."'
  4624. WHERE iid = $lp_id";
  4625. Database::query($sql);
  4626. return true;
  4627. }
  4628. /**
  4629. * Sets the image of an LP (and save).
  4630. *
  4631. * @param string $name Optional string giving the new image of this learnpath
  4632. *
  4633. * @return bool Returns true if theme name is not empty
  4634. */
  4635. public function set_preview_image($name = '')
  4636. {
  4637. $this->preview_image = $name;
  4638. $table = Database::get_course_table(TABLE_LP_MAIN);
  4639. $lp_id = $this->get_id();
  4640. $sql = "UPDATE $table SET
  4641. preview_image = '".Database::escape_string($this->preview_image)."'
  4642. WHERE iid = $lp_id";
  4643. Database::query($sql);
  4644. return true;
  4645. }
  4646. /**
  4647. * Sets the author of a LP (and save).
  4648. *
  4649. * @param string $name Optional string giving the new author of this learnpath
  4650. *
  4651. * @return bool Returns true if author's name is not empty
  4652. */
  4653. public function set_author($name = '')
  4654. {
  4655. $this->author = $name;
  4656. $table = Database::get_course_table(TABLE_LP_MAIN);
  4657. $lp_id = $this->get_id();
  4658. $sql = "UPDATE $table SET author = '".Database::escape_string($name)."'
  4659. WHERE iid = $lp_id";
  4660. Database::query($sql);
  4661. return true;
  4662. }
  4663. /**
  4664. * Sets the hide_toc_frame parameter of a LP (and save).
  4665. *
  4666. * @param int $hide 1 if frame is hidden 0 then else
  4667. *
  4668. * @return bool Returns true if author's name is not empty
  4669. */
  4670. public function set_hide_toc_frame($hide)
  4671. {
  4672. if (intval($hide) == $hide) {
  4673. $this->hide_toc_frame = $hide;
  4674. $table = Database::get_course_table(TABLE_LP_MAIN);
  4675. $lp_id = $this->get_id();
  4676. $sql = "UPDATE $table SET
  4677. hide_toc_frame = '".(int) $this->hide_toc_frame."'
  4678. WHERE iid = $lp_id";
  4679. Database::query($sql);
  4680. return true;
  4681. }
  4682. return false;
  4683. }
  4684. /**
  4685. * Sets the prerequisite of a LP (and save).
  4686. *
  4687. * @param int $prerequisite integer giving the new prerequisite of this learnpath
  4688. *
  4689. * @return bool returns true if prerequisite is not empty
  4690. */
  4691. public function set_prerequisite($prerequisite)
  4692. {
  4693. $this->prerequisite = (int) $prerequisite;
  4694. $table = Database::get_course_table(TABLE_LP_MAIN);
  4695. $lp_id = $this->get_id();
  4696. $sql = "UPDATE $table SET prerequisite = '".$this->prerequisite."'
  4697. WHERE iid = $lp_id";
  4698. Database::query($sql);
  4699. return true;
  4700. }
  4701. /**
  4702. * Sets the location/proximity of the LP (local/remote) (and save).
  4703. *
  4704. * @param string $name Optional string giving the new location of this learnpath
  4705. *
  4706. * @return bool True on success / False on error
  4707. */
  4708. public function set_proximity($name = '')
  4709. {
  4710. if (empty($name)) {
  4711. return false;
  4712. }
  4713. $this->proximity = $name;
  4714. $table = Database::get_course_table(TABLE_LP_MAIN);
  4715. $lp_id = $this->get_id();
  4716. $sql = "UPDATE $table SET
  4717. content_local = '".Database::escape_string($name)."'
  4718. WHERE iid = $lp_id";
  4719. Database::query($sql);
  4720. return true;
  4721. }
  4722. /**
  4723. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item.
  4724. *
  4725. * @param int $id DB ID of the item
  4726. */
  4727. public function set_previous_item($id)
  4728. {
  4729. if ($this->debug > 0) {
  4730. error_log('In learnpath::set_previous_item()', 0);
  4731. }
  4732. $this->last = $id;
  4733. }
  4734. /**
  4735. * Sets use_max_score.
  4736. *
  4737. * @param int $use_max_score Optional string giving the new location of this learnpath
  4738. *
  4739. * @return bool True on success / False on error
  4740. */
  4741. public function set_use_max_score($use_max_score = 1)
  4742. {
  4743. $use_max_score = (int) $use_max_score;
  4744. $this->use_max_score = $use_max_score;
  4745. $table = Database::get_course_table(TABLE_LP_MAIN);
  4746. $lp_id = $this->get_id();
  4747. $sql = "UPDATE $table SET
  4748. use_max_score = '".$this->use_max_score."'
  4749. WHERE iid = $lp_id";
  4750. Database::query($sql);
  4751. return true;
  4752. }
  4753. /**
  4754. * Sets and saves the expired_on date.
  4755. *
  4756. * @param string $expired_on Optional string giving the new author of this learnpath
  4757. *
  4758. * @throws \Doctrine\ORM\OptimisticLockException
  4759. *
  4760. * @return bool Returns true if author's name is not empty
  4761. */
  4762. public function set_expired_on($expired_on)
  4763. {
  4764. $em = Database::getManager();
  4765. /** @var CLp $lp */
  4766. $lp = $em
  4767. ->getRepository('ChamiloCourseBundle:CLp')
  4768. ->findOneBy(
  4769. [
  4770. 'iid' => $this->get_id(),
  4771. ]
  4772. );
  4773. if (!$lp) {
  4774. return false;
  4775. }
  4776. $this->expired_on = !empty($expired_on) ? api_get_utc_datetime($expired_on, false, true) : null;
  4777. $lp->setExpiredOn($this->expired_on);
  4778. $em->persist($lp);
  4779. $em->flush();
  4780. return true;
  4781. }
  4782. /**
  4783. * Sets and saves the publicated_on date.
  4784. *
  4785. * @param string $publicated_on Optional string giving the new author of this learnpath
  4786. *
  4787. * @throws \Doctrine\ORM\OptimisticLockException
  4788. *
  4789. * @return bool Returns true if author's name is not empty
  4790. */
  4791. public function set_publicated_on($publicated_on)
  4792. {
  4793. $em = Database::getManager();
  4794. /** @var CLp $lp */
  4795. $lp = $em
  4796. ->getRepository('ChamiloCourseBundle:CLp')
  4797. ->findOneBy(
  4798. [
  4799. 'iid' => $this->get_id(),
  4800. ]
  4801. );
  4802. if (!$lp) {
  4803. return false;
  4804. }
  4805. $this->publicated_on = !empty($publicated_on) ? api_get_utc_datetime($publicated_on, false, true) : null;
  4806. $lp->setPublicatedOn($this->publicated_on);
  4807. $em->persist($lp);
  4808. $em->flush();
  4809. return true;
  4810. }
  4811. /**
  4812. * Sets and saves the expired_on date.
  4813. *
  4814. * @return bool Returns true if author's name is not empty
  4815. */
  4816. public function set_modified_on()
  4817. {
  4818. $this->modified_on = api_get_utc_datetime();
  4819. $table = Database::get_course_table(TABLE_LP_MAIN);
  4820. $lp_id = $this->get_id();
  4821. $sql = "UPDATE $table SET modified_on = '".$this->modified_on."'
  4822. WHERE iid = $lp_id";
  4823. Database::query($sql);
  4824. return true;
  4825. }
  4826. /**
  4827. * Sets the object's error message.
  4828. *
  4829. * @param string $error Error message. If empty, reinits the error string
  4830. */
  4831. public function set_error_msg($error = '')
  4832. {
  4833. if ($this->debug > 0) {
  4834. error_log('In learnpath::set_error_msg()', 0);
  4835. }
  4836. if (empty($error)) {
  4837. $this->error = '';
  4838. } else {
  4839. $this->error .= $error;
  4840. }
  4841. }
  4842. /**
  4843. * Launches the current item if not 'sco'
  4844. * (starts timer and make sure there is a record ready in the DB).
  4845. *
  4846. * @param bool $allow_new_attempt Whether to allow a new attempt or not
  4847. *
  4848. * @return bool
  4849. */
  4850. public function start_current_item($allow_new_attempt = false)
  4851. {
  4852. $debug = $this->debug;
  4853. if ($debug) {
  4854. error_log('In learnpath::start_current_item()');
  4855. error_log('current: '.$this->current);
  4856. }
  4857. if ($this->current != 0 && isset($this->items[$this->current]) && is_object($this->items[$this->current])) {
  4858. $type = $this->get_type();
  4859. $item_type = $this->items[$this->current]->get_type();
  4860. if (($type == 2 && $item_type != 'sco') ||
  4861. ($type == 3 && $item_type != 'au') ||
  4862. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4863. ) {
  4864. if ($debug) {
  4865. error_log('item type: '.$item_type);
  4866. error_log('lp type: '.$type);
  4867. }
  4868. $this->items[$this->current]->open($allow_new_attempt);
  4869. $this->autocomplete_parents($this->current);
  4870. $prereq_check = $this->prerequisites_match($this->current);
  4871. if ($debug) {
  4872. error_log('start_current_item will save item with prereq: '.$prereq_check);
  4873. }
  4874. $this->items[$this->current]->save(false, $prereq_check);
  4875. }
  4876. // If sco, then it is supposed to have been updated by some other call.
  4877. if ($item_type == 'sco') {
  4878. $this->items[$this->current]->restart();
  4879. }
  4880. }
  4881. if ($debug) {
  4882. error_log('lp_view_session_id');
  4883. error_log($this->lp_view_session_id);
  4884. error_log('api session id');
  4885. error_log(api_get_session_id());
  4886. error_log('End of learnpath::start_current_item()');
  4887. }
  4888. return true;
  4889. }
  4890. /**
  4891. * Stops the processing and counters for the old item (as held in $this->last).
  4892. *
  4893. * @return bool True/False
  4894. */
  4895. public function stop_previous_item()
  4896. {
  4897. $debug = $this->debug;
  4898. if ($debug) {
  4899. error_log('In learnpath::stop_previous_item()', 0);
  4900. }
  4901. if ($this->last != 0 && $this->last != $this->current &&
  4902. isset($this->items[$this->last]) && is_object($this->items[$this->last])
  4903. ) {
  4904. if ($debug) {
  4905. error_log('In learnpath::stop_previous_item() - '.$this->last.' is object');
  4906. }
  4907. switch ($this->get_type()) {
  4908. case '3':
  4909. if ($this->items[$this->last]->get_type() != 'au') {
  4910. if ($debug) {
  4911. error_log('In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au');
  4912. }
  4913. $this->items[$this->last]->close();
  4914. } else {
  4915. if ($debug) {
  4916. error_log('In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals');
  4917. }
  4918. }
  4919. break;
  4920. case '2':
  4921. if ($this->items[$this->last]->get_type() != 'sco') {
  4922. if ($debug) {
  4923. error_log('In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco');
  4924. }
  4925. $this->items[$this->last]->close();
  4926. } else {
  4927. if ($debug) {
  4928. error_log('In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals');
  4929. }
  4930. }
  4931. break;
  4932. case '1':
  4933. default:
  4934. if ($debug) {
  4935. error_log('In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset');
  4936. }
  4937. $this->items[$this->last]->close();
  4938. break;
  4939. }
  4940. } else {
  4941. if ($debug) {
  4942. error_log('In learnpath::stop_previous_item() - No previous element found, ignoring...');
  4943. }
  4944. return false;
  4945. }
  4946. return true;
  4947. }
  4948. /**
  4949. * Updates the default view mode from fullscreen to embedded and inversely.
  4950. *
  4951. * @return string The current default view mode ('fullscreen' or 'embedded')
  4952. */
  4953. public function update_default_view_mode()
  4954. {
  4955. $table = Database::get_course_table(TABLE_LP_MAIN);
  4956. $sql = "SELECT * FROM $table
  4957. WHERE iid = ".$this->get_id();
  4958. $res = Database::query($sql);
  4959. if (Database::num_rows($res) > 0) {
  4960. $row = Database::fetch_array($res);
  4961. $default_view_mode = $row['default_view_mod'];
  4962. $view_mode = $default_view_mode;
  4963. switch ($default_view_mode) {
  4964. case 'fullscreen': // default with popup
  4965. $view_mode = 'embedded';
  4966. break;
  4967. case 'embedded': // default view with left menu
  4968. $view_mode = 'embedframe';
  4969. break;
  4970. case 'embedframe': //folded menu
  4971. $view_mode = 'impress';
  4972. break;
  4973. case 'impress':
  4974. $view_mode = 'fullscreen';
  4975. break;
  4976. }
  4977. $sql = "UPDATE $table SET default_view_mod = '$view_mode'
  4978. WHERE iid = ".$this->get_id();
  4979. Database::query($sql);
  4980. $this->mode = $view_mode;
  4981. return $view_mode;
  4982. }
  4983. return -1;
  4984. }
  4985. /**
  4986. * Updates the default behaviour about auto-commiting SCORM updates.
  4987. *
  4988. * @return bool True if auto-commit has been set to 'on', false otherwise
  4989. */
  4990. public function update_default_scorm_commit()
  4991. {
  4992. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4993. $sql = "SELECT * FROM $lp_table
  4994. WHERE iid = ".$this->get_id();
  4995. $res = Database::query($sql);
  4996. if (Database::num_rows($res) > 0) {
  4997. $row = Database::fetch_array($res);
  4998. $force = $row['force_commit'];
  4999. if ($force == 1) {
  5000. $force = 0;
  5001. $force_return = false;
  5002. } elseif ($force == 0) {
  5003. $force = 1;
  5004. $force_return = true;
  5005. }
  5006. $sql = "UPDATE $lp_table SET force_commit = $force
  5007. WHERE iid = ".$this->get_id();
  5008. Database::query($sql);
  5009. $this->force_commit = $force_return;
  5010. return $force_return;
  5011. }
  5012. return -1;
  5013. }
  5014. /**
  5015. * Updates the order of learning paths (goes through all of them by order and fills the gaps).
  5016. *
  5017. * @return bool True on success, false on failure
  5018. */
  5019. public function update_display_order()
  5020. {
  5021. $course_id = api_get_course_int_id();
  5022. $table = Database::get_course_table(TABLE_LP_MAIN);
  5023. $sql = "SELECT * FROM $table
  5024. WHERE c_id = $course_id
  5025. ORDER BY display_order";
  5026. $res = Database::query($sql);
  5027. if ($res === false) {
  5028. return false;
  5029. }
  5030. $num = Database::num_rows($res);
  5031. // First check the order is correct, globally (might be wrong because
  5032. // of versions < 1.8.4).
  5033. if ($num > 0) {
  5034. $i = 1;
  5035. while ($row = Database::fetch_array($res)) {
  5036. if ($row['display_order'] != $i) {
  5037. // If we find a gap in the order, we need to fix it.
  5038. $sql = "UPDATE $table SET display_order = $i
  5039. WHERE iid = ".$row['iid'];
  5040. Database::query($sql);
  5041. }
  5042. $i++;
  5043. }
  5044. }
  5045. return true;
  5046. }
  5047. /**
  5048. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view.
  5049. *
  5050. * @return bool True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  5051. */
  5052. public function update_reinit()
  5053. {
  5054. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5055. $sql = "SELECT * FROM $lp_table
  5056. WHERE iid = ".$this->get_id();
  5057. $res = Database::query($sql);
  5058. if (Database::num_rows($res) > 0) {
  5059. $row = Database::fetch_array($res);
  5060. $force = $row['prevent_reinit'];
  5061. if ($force == 1) {
  5062. $force = 0;
  5063. } elseif ($force == 0) {
  5064. $force = 1;
  5065. }
  5066. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  5067. WHERE iid = ".$this->get_id();
  5068. Database::query($sql);
  5069. $this->prevent_reinit = $force;
  5070. return $force;
  5071. }
  5072. return -1;
  5073. }
  5074. /**
  5075. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag.
  5076. *
  5077. * @return string 'single', 'multi' or 'seriousgame'
  5078. *
  5079. * @author ndiechburg <noel@cblue.be>
  5080. */
  5081. public function get_attempt_mode()
  5082. {
  5083. //Set default value for seriousgame_mode
  5084. if (!isset($this->seriousgame_mode)) {
  5085. $this->seriousgame_mode = 0;
  5086. }
  5087. // Set default value for prevent_reinit
  5088. if (!isset($this->prevent_reinit)) {
  5089. $this->prevent_reinit = 1;
  5090. }
  5091. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  5092. return 'seriousgame';
  5093. }
  5094. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  5095. return 'single';
  5096. }
  5097. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  5098. return 'multiple';
  5099. }
  5100. return 'single';
  5101. }
  5102. /**
  5103. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags.
  5104. *
  5105. * @param string 'seriousgame', 'single' or 'multiple'
  5106. *
  5107. * @return bool
  5108. *
  5109. * @author ndiechburg <noel@cblue.be>
  5110. */
  5111. public function set_attempt_mode($mode)
  5112. {
  5113. switch ($mode) {
  5114. case 'seriousgame':
  5115. $sg_mode = 1;
  5116. $prevent_reinit = 1;
  5117. break;
  5118. case 'single':
  5119. $sg_mode = 0;
  5120. $prevent_reinit = 1;
  5121. break;
  5122. case 'multiple':
  5123. $sg_mode = 0;
  5124. $prevent_reinit = 0;
  5125. break;
  5126. default:
  5127. $sg_mode = 0;
  5128. $prevent_reinit = 0;
  5129. break;
  5130. }
  5131. $this->prevent_reinit = $prevent_reinit;
  5132. $this->seriousgame_mode = $sg_mode;
  5133. $table = Database::get_course_table(TABLE_LP_MAIN);
  5134. $sql = "UPDATE $table SET
  5135. prevent_reinit = $prevent_reinit ,
  5136. seriousgame_mode = $sg_mode
  5137. WHERE iid = ".$this->get_id();
  5138. $res = Database::query($sql);
  5139. if ($res) {
  5140. return true;
  5141. } else {
  5142. return false;
  5143. }
  5144. }
  5145. /**
  5146. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm).
  5147. *
  5148. * @author ndiechburg <noel@cblue.be>
  5149. */
  5150. public function switch_attempt_mode()
  5151. {
  5152. $mode = $this->get_attempt_mode();
  5153. switch ($mode) {
  5154. case 'single':
  5155. $next_mode = 'multiple';
  5156. break;
  5157. case 'multiple':
  5158. $next_mode = 'seriousgame';
  5159. break;
  5160. case 'seriousgame':
  5161. default:
  5162. $next_mode = 'single';
  5163. break;
  5164. }
  5165. $this->set_attempt_mode($next_mode);
  5166. }
  5167. /**
  5168. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  5169. * but possibility to do again a completed item.
  5170. *
  5171. * @return bool true if seriousgame_mode has been set to 1, false otherwise
  5172. *
  5173. * @author ndiechburg <noel@cblue.be>
  5174. */
  5175. public function set_seriousgame_mode()
  5176. {
  5177. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5178. $sql = "SELECT * FROM $lp_table
  5179. WHERE iid = ".$this->get_id();
  5180. $res = Database::query($sql);
  5181. if (Database::num_rows($res) > 0) {
  5182. $row = Database::fetch_array($res);
  5183. $force = $row['seriousgame_mode'];
  5184. if ($force == 1) {
  5185. $force = 0;
  5186. } elseif ($force == 0) {
  5187. $force = 1;
  5188. }
  5189. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  5190. WHERE iid = ".$this->get_id();
  5191. Database::query($sql);
  5192. $this->seriousgame_mode = $force;
  5193. return $force;
  5194. }
  5195. return -1;
  5196. }
  5197. /**
  5198. * Updates the "scorm_debug" value that shows or hide the debug window.
  5199. *
  5200. * @return bool True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  5201. */
  5202. public function update_scorm_debug()
  5203. {
  5204. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  5205. $sql = "SELECT * FROM $lp_table
  5206. WHERE iid = ".$this->get_id();
  5207. $res = Database::query($sql);
  5208. if (Database::num_rows($res) > 0) {
  5209. $row = Database::fetch_array($res);
  5210. $force = $row['debug'];
  5211. if ($force == 1) {
  5212. $force = 0;
  5213. } elseif ($force == 0) {
  5214. $force = 1;
  5215. }
  5216. $sql = "UPDATE $lp_table SET debug = $force
  5217. WHERE iid = ".$this->get_id();
  5218. Database::query($sql);
  5219. $this->scorm_debug = $force;
  5220. return $force;
  5221. }
  5222. return -1;
  5223. }
  5224. /**
  5225. * Function that makes a call to the function sort_tree_array and create_tree_array.
  5226. *
  5227. * @author Kevin Van Den Haute
  5228. *
  5229. * @param array
  5230. */
  5231. public function tree_array($array)
  5232. {
  5233. $array = $this->sort_tree_array($array);
  5234. $this->create_tree_array($array);
  5235. }
  5236. /**
  5237. * Creates an array with the elements of the learning path tree in it.
  5238. *
  5239. * @author Kevin Van Den Haute
  5240. *
  5241. * @param array $array
  5242. * @param int $parent
  5243. * @param int $depth
  5244. * @param array $tmp
  5245. */
  5246. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = [])
  5247. {
  5248. if (is_array($array)) {
  5249. for ($i = 0; $i < count($array); $i++) {
  5250. if ($array[$i]['parent_item_id'] == $parent) {
  5251. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5252. $tmp[] = $array[$i]['parent_item_id'];
  5253. $depth++;
  5254. }
  5255. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5256. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5257. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5258. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5259. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5260. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5261. $this->arrMenu[] = [
  5262. 'id' => $array[$i]['id'],
  5263. 'ref' => $ref,
  5264. 'item_type' => $array[$i]['item_type'],
  5265. 'title' => $array[$i]['title'],
  5266. 'title_raw' => $array[$i]['title_raw'],
  5267. 'path' => $path,
  5268. 'description' => $array[$i]['description'],
  5269. 'parent_item_id' => $array[$i]['parent_item_id'],
  5270. 'previous_item_id' => $array[$i]['previous_item_id'],
  5271. 'next_item_id' => $array[$i]['next_item_id'],
  5272. 'min_score' => $array[$i]['min_score'],
  5273. 'max_score' => $array[$i]['max_score'],
  5274. 'mastery_score' => $array[$i]['mastery_score'],
  5275. 'display_order' => $array[$i]['display_order'],
  5276. 'prerequisite' => $preq,
  5277. 'depth' => $depth,
  5278. 'audio' => $audio,
  5279. 'prerequisite_min_score' => $prerequisiteMinScore,
  5280. 'prerequisite_max_score' => $prerequisiteMaxScore,
  5281. ];
  5282. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5283. }
  5284. }
  5285. }
  5286. }
  5287. /**
  5288. * Sorts a multi dimensional array by parent id and display order.
  5289. *
  5290. * @author Kevin Van Den Haute
  5291. *
  5292. * @param array $array (array with al the learning path items in it)
  5293. *
  5294. * @return array
  5295. */
  5296. public function sort_tree_array($array)
  5297. {
  5298. foreach ($array as $key => $row) {
  5299. $parent[$key] = $row['parent_item_id'];
  5300. $position[$key] = $row['display_order'];
  5301. }
  5302. if (count($array) > 0) {
  5303. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5304. }
  5305. return $array;
  5306. }
  5307. /**
  5308. * Function that creates a html list of learning path items so that we can add audio files to them.
  5309. *
  5310. * @author Kevin Van Den Haute
  5311. *
  5312. * @return string
  5313. */
  5314. public function overview()
  5315. {
  5316. $return = '';
  5317. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5318. // we need to start a form when we want to update all the mp3 files
  5319. if ($update_audio == 'true') {
  5320. $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">';
  5321. }
  5322. $return .= '<div id="message"></div>';
  5323. if (count($this->items) == 0) {
  5324. $return .= Display::return_message(get_lang('YouShouldAddItemsBeforeAttachAudio'), 'normal');
  5325. } else {
  5326. $return_audio = '<table class="data_table">';
  5327. $return_audio .= '<tr>';
  5328. $return_audio .= '<th width="40%">'.get_lang('Title').'</th>';
  5329. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5330. $return_audio .= '</tr>';
  5331. if ($update_audio != 'true') {
  5332. $return .= '<div class="col-md-12">';
  5333. $return .= self::return_new_tree($update_audio);
  5334. $return .= '</div>';
  5335. $return .= Display::div(
  5336. Display::url(get_lang('Save'), '#', ['id' => 'listSubmit', 'class' => 'btn btn-primary']),
  5337. ['style' => 'float:left; margin-top:15px;width:100%']
  5338. );
  5339. } else {
  5340. $return_audio .= self::return_new_tree($update_audio);
  5341. $return .= $return_audio.'</table>';
  5342. }
  5343. // We need to close the form when we are updating the mp3 files.
  5344. if ($update_audio == 'true') {
  5345. $return .= '<div class="footer-audio">';
  5346. $return .= Display::button(
  5347. 'save_audio',
  5348. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5349. ['class' => 'btn btn-primary', 'type' => 'submit']
  5350. );
  5351. $return .= '</div>';
  5352. }
  5353. }
  5354. // We need to close the form when we are updating the mp3 files.
  5355. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5356. $return .= '</form>';
  5357. }
  5358. return $return;
  5359. }
  5360. /**
  5361. * @param string $update_audio
  5362. *
  5363. * @return array
  5364. */
  5365. public function processBuildMenuElements($update_audio = 'false')
  5366. {
  5367. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5368. $arrLP = $this->getItemsForForm();
  5369. $this->tree_array($arrLP);
  5370. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  5371. unset($this->arrMenu);
  5372. $default_data = null;
  5373. $default_content = null;
  5374. $elements = [];
  5375. $return_audio = null;
  5376. $iconPath = api_get_path(SYS_CODE_PATH).'img/';
  5377. $mainUrl = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq();
  5378. $countItems = count($arrLP);
  5379. $upIcon = Display::return_icon(
  5380. 'up.png',
  5381. get_lang('Up'),
  5382. [],
  5383. ICON_SIZE_TINY
  5384. );
  5385. $disableUpIcon = Display::return_icon(
  5386. 'up_na.png',
  5387. get_lang('Up'),
  5388. [],
  5389. ICON_SIZE_TINY
  5390. );
  5391. $downIcon = Display::return_icon(
  5392. 'down.png',
  5393. get_lang('Down'),
  5394. [],
  5395. ICON_SIZE_TINY
  5396. );
  5397. $disableDownIcon = Display::return_icon(
  5398. 'down_na.png',
  5399. get_lang('Down'),
  5400. [],
  5401. ICON_SIZE_TINY
  5402. );
  5403. $show = api_get_configuration_value('show_full_lp_item_title_in_edition');
  5404. $pluginCalendar = api_get_plugin_setting('learning_calendar', 'enabled') === 'true';
  5405. $plugin = null;
  5406. if ($pluginCalendar) {
  5407. $plugin = LearningCalendarPlugin::create();
  5408. }
  5409. for ($i = 0; $i < $countItems; $i++) {
  5410. $parent_id = $arrLP[$i]['parent_item_id'];
  5411. $title = $arrLP[$i]['title'];
  5412. $title_cut = $arrLP[$i]['title_raw'];
  5413. if ($show === false) {
  5414. $title_cut = cut($arrLP[$i]['title'], self::MAX_LP_ITEM_TITLE_LENGTH);
  5415. }
  5416. // Link for the documents
  5417. if ($arrLP[$i]['item_type'] === 'document' || $arrLP[$i]['item_type'] == TOOL_READOUT_TEXT) {
  5418. $url = $mainUrl.'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5419. $title_cut = Display::url(
  5420. $title_cut,
  5421. $url,
  5422. [
  5423. 'class' => 'ajax moved',
  5424. 'data-title' => $title,
  5425. 'title' => $title,
  5426. ]
  5427. );
  5428. }
  5429. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5430. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5431. Session::write('pathItem', $arrLP[$i]['path']);
  5432. }
  5433. $oddClass = 'row_even';
  5434. if (($i % 2) == 0) {
  5435. $oddClass = 'row_odd';
  5436. }
  5437. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddClass.'">';
  5438. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5439. if (file_exists($iconPath.'lp_'.$icon_name.'.png')) {
  5440. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5441. } else {
  5442. if (file_exists($iconPath.'lp_'.$icon_name.'.gif')) {
  5443. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5444. } else {
  5445. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5446. $icon = Display::return_icon('certificate.png');
  5447. } else {
  5448. $icon = Display::return_icon('folder_document.gif');
  5449. }
  5450. }
  5451. }
  5452. // The audio column.
  5453. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5454. $audio = '';
  5455. if (!$update_audio || $update_audio != 'true') {
  5456. if (empty($arrLP[$i]['audio'])) {
  5457. $audio .= '';
  5458. }
  5459. } else {
  5460. $types = self::getChapterTypes();
  5461. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5462. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5463. if (!empty($arrLP[$i]['audio'])) {
  5464. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5465. <input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang('RemoveAudio');
  5466. }
  5467. }
  5468. }
  5469. $return_audio .= Display::span($icon.' '.$title).
  5470. Display::tag(
  5471. 'td',
  5472. $audio,
  5473. ['style' => '']
  5474. );
  5475. $return_audio .= '</td>';
  5476. $move_icon = '';
  5477. $move_item_icon = '';
  5478. $edit_icon = '';
  5479. $delete_icon = '';
  5480. $audio_icon = '';
  5481. $prerequisities_icon = '';
  5482. $forumIcon = '';
  5483. $previewIcon = '';
  5484. $pluginCalendarIcon = '';
  5485. $orderIcons = '';
  5486. $pluginUrl = api_get_path(WEB_PLUGIN_PATH).'learning_calendar/start.php?';
  5487. if ($is_allowed_to_edit) {
  5488. if (!$update_audio || $update_audio != 'true') {
  5489. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5490. $move_icon .= '<a class="moved" href="#">';
  5491. $move_icon .= Display::return_icon(
  5492. 'move_everywhere.png',
  5493. get_lang('Move'),
  5494. [],
  5495. ICON_SIZE_TINY
  5496. );
  5497. $move_icon .= '</a>';
  5498. }
  5499. }
  5500. // No edit for this item types
  5501. if (!in_array($arrLP[$i]['item_type'], ['sco', 'asset', 'final_item'])) {
  5502. if ($arrLP[$i]['item_type'] != 'dir') {
  5503. $edit_icon .= '<a href="'.$mainUrl.'&action=edit_item&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id.'&path_item='.$arrLP[$i]['path'].'" class="btn btn-default">';
  5504. $edit_icon .= Display::return_icon(
  5505. 'edit.png',
  5506. get_lang('LearnpathEditModule'),
  5507. [],
  5508. ICON_SIZE_TINY
  5509. );
  5510. $edit_icon .= '</a>';
  5511. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5512. $forumThread = null;
  5513. if (isset($this->items[$arrLP[$i]['id']])) {
  5514. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5515. $this->course_int_id,
  5516. $this->lp_session_id
  5517. );
  5518. }
  5519. if ($forumThread) {
  5520. $forumIconUrl = $mainUrl.'&'.http_build_query([
  5521. 'action' => 'dissociate_forum',
  5522. 'id' => $arrLP[$i]['id'],
  5523. 'lp_id' => $this->lp_id,
  5524. ]);
  5525. $forumIcon = Display::url(
  5526. Display::return_icon(
  5527. 'forum.png',
  5528. get_lang('DissociateForumToLPItem'),
  5529. [],
  5530. ICON_SIZE_TINY
  5531. ),
  5532. $forumIconUrl,
  5533. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5534. );
  5535. } else {
  5536. $forumIconUrl = $mainUrl.'&'.http_build_query([
  5537. 'action' => 'create_forum',
  5538. 'id' => $arrLP[$i]['id'],
  5539. 'lp_id' => $this->lp_id,
  5540. ]);
  5541. $forumIcon = Display::url(
  5542. Display::return_icon(
  5543. 'forum.png',
  5544. get_lang('AssociateForumToLPItem'),
  5545. [],
  5546. ICON_SIZE_TINY
  5547. ),
  5548. $forumIconUrl,
  5549. ['class' => 'btn btn-default lp-btn-associate-forum']
  5550. );
  5551. }
  5552. }
  5553. } else {
  5554. $edit_icon .= '<a href="'.$mainUrl.'&action=edit_item&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id.'&path_item='.$arrLP[$i]['path'].'" class="btn btn-default">';
  5555. $edit_icon .= Display::return_icon(
  5556. 'edit.png',
  5557. get_lang('LearnpathEditModule'),
  5558. [],
  5559. ICON_SIZE_TINY
  5560. );
  5561. $edit_icon .= '</a>';
  5562. }
  5563. } else {
  5564. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5565. $edit_icon .= '<a href="'.$mainUrl.'&action=edit_item&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id.'" class="btn btn-default">';
  5566. $edit_icon .= Display::return_icon(
  5567. 'edit.png',
  5568. get_lang('Edit'),
  5569. [],
  5570. ICON_SIZE_TINY
  5571. );
  5572. $edit_icon .= '</a>';
  5573. }
  5574. }
  5575. if ($pluginCalendar) {
  5576. $pluginLink = $pluginUrl.
  5577. '&action=toggle_visibility&lp_item_id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5578. $iconCalendar = Display::return_icon('agenda_na.png', get_lang('OneDay'), [], ICON_SIZE_TINY);
  5579. $itemInfo = $plugin->getItemVisibility($arrLP[$i]['id']);
  5580. if ($itemInfo && $itemInfo['value'] == 1) {
  5581. $iconCalendar = Display::return_icon('agenda.png', get_lang('OneDay'), [], ICON_SIZE_TINY);
  5582. }
  5583. $pluginCalendarIcon = Display::url(
  5584. $iconCalendar,
  5585. $pluginLink,
  5586. ['class' => 'btn btn-default']
  5587. );
  5588. }
  5589. if ($arrLP[$i]['item_type'] != 'final_item') {
  5590. $orderIcons = Display::url(
  5591. $upIcon,
  5592. 'javascript:void(0)',
  5593. ['class' => 'btn btn-default order_items', 'data-dir' => 'up', 'data-id' => $arrLP[$i]['id']]
  5594. );
  5595. $orderIcons .= Display::url(
  5596. $downIcon,
  5597. 'javascript:void(0)',
  5598. ['class' => 'btn btn-default order_items', 'data-dir' => 'down', 'data-id' => $arrLP[$i]['id']]
  5599. );
  5600. }
  5601. $delete_icon .= ' <a
  5602. href="'.$mainUrl.'&action=delete_item&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id.'"
  5603. onclick="return confirmation(\''.addslashes($title).'\');"
  5604. class="btn btn-default">';
  5605. $delete_icon .= Display::return_icon(
  5606. 'delete.png',
  5607. get_lang('LearnpathDeleteModule'),
  5608. [],
  5609. ICON_SIZE_TINY
  5610. );
  5611. $delete_icon .= '</a>';
  5612. $url = $mainUrl.'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5613. $previewImage = Display::return_icon(
  5614. 'preview_view.png',
  5615. get_lang('Preview'),
  5616. [],
  5617. ICON_SIZE_TINY
  5618. );
  5619. switch ($arrLP[$i]['item_type']) {
  5620. case TOOL_DOCUMENT:
  5621. case TOOL_LP_FINAL_ITEM:
  5622. case TOOL_READOUT_TEXT:
  5623. $urlPreviewLink = $mainUrl.'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5624. $previewIcon = Display::url(
  5625. $previewImage,
  5626. $urlPreviewLink,
  5627. [
  5628. 'target' => '_blank',
  5629. 'class' => 'btn btn-default',
  5630. 'data-title' => $arrLP[$i]['title'],
  5631. 'title' => $arrLP[$i]['title'],
  5632. ]
  5633. );
  5634. break;
  5635. case TOOL_THREAD:
  5636. case TOOL_FORUM:
  5637. case TOOL_QUIZ:
  5638. case TOOL_STUDENTPUBLICATION:
  5639. case TOOL_LP_FINAL_ITEM:
  5640. case TOOL_LINK:
  5641. $class = 'btn btn-default';
  5642. $target = '_blank';
  5643. $link = self::rl_get_resource_link_for_learnpath(
  5644. $this->course_int_id,
  5645. $this->lp_id,
  5646. $arrLP[$i]['id'],
  5647. 0
  5648. );
  5649. $previewIcon = Display::url(
  5650. $previewImage,
  5651. $link,
  5652. [
  5653. 'class' => $class,
  5654. 'data-title' => $arrLP[$i]['title'],
  5655. 'title' => $arrLP[$i]['title'],
  5656. 'target' => $target,
  5657. ]
  5658. );
  5659. break;
  5660. default:
  5661. $previewIcon = Display::url(
  5662. $previewImage,
  5663. $url.'&action=view_item',
  5664. ['class' => 'btn btn-default', 'target' => '_blank']
  5665. );
  5666. break;
  5667. }
  5668. if ($arrLP[$i]['item_type'] != 'dir') {
  5669. $prerequisities_icon = Display::url(
  5670. Display::return_icon(
  5671. 'accept.png',
  5672. get_lang('LearnpathPrerequisites'),
  5673. [],
  5674. ICON_SIZE_TINY
  5675. ),
  5676. $url.'&action=edit_item_prereq',
  5677. ['class' => 'btn btn-default']
  5678. );
  5679. if ($arrLP[$i]['item_type'] != 'final_item') {
  5680. $move_item_icon = Display::url(
  5681. Display::return_icon(
  5682. 'move.png',
  5683. get_lang('Move'),
  5684. [],
  5685. ICON_SIZE_TINY
  5686. ),
  5687. $url.'&action=move_item',
  5688. ['class' => 'btn btn-default']
  5689. );
  5690. }
  5691. $audio_icon = Display::url(
  5692. Display::return_icon(
  5693. 'audio.png',
  5694. get_lang('UplUpload'),
  5695. [],
  5696. ICON_SIZE_TINY
  5697. ),
  5698. $url.'&action=add_audio',
  5699. ['class' => 'btn btn-default']
  5700. );
  5701. }
  5702. }
  5703. if ($update_audio != 'true') {
  5704. $row = $move_icon.' '.$icon.
  5705. Display::span($title_cut).
  5706. Display::tag(
  5707. 'div',
  5708. "<div class=\"btn-group btn-group-xs\">
  5709. $previewIcon
  5710. $audio
  5711. $edit_icon
  5712. $pluginCalendarIcon
  5713. $forumIcon
  5714. $prerequisities_icon
  5715. $move_item_icon
  5716. $audio_icon
  5717. $orderIcons
  5718. $delete_icon
  5719. </div>",
  5720. ['class' => 'btn-toolbar button_actions']
  5721. );
  5722. } else {
  5723. $row =
  5724. Display::span($title.$icon).
  5725. Display::span($audio, ['class' => 'button_actions']);
  5726. }
  5727. $default_data[$arrLP[$i]['id']] = $row;
  5728. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5729. if (empty($parent_id)) {
  5730. $elements[$arrLP[$i]['id']]['data'] = $row;
  5731. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5732. } else {
  5733. $parent_arrays = [];
  5734. if ($arrLP[$i]['depth'] > 1) {
  5735. // Getting list of parents
  5736. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5737. foreach ($arrLP as $item) {
  5738. if ($item['id'] == $parent_id) {
  5739. if ($item['parent_item_id'] == 0) {
  5740. $parent_id = $item['id'];
  5741. break;
  5742. } else {
  5743. $parent_id = $item['parent_item_id'];
  5744. if (empty($parent_arrays)) {
  5745. $parent_arrays[] = intval($item['id']);
  5746. }
  5747. $parent_arrays[] = $parent_id;
  5748. break;
  5749. }
  5750. }
  5751. }
  5752. }
  5753. }
  5754. if (!empty($parent_arrays)) {
  5755. $parent_arrays = array_reverse($parent_arrays);
  5756. $val = '$elements';
  5757. $x = 0;
  5758. foreach ($parent_arrays as $item) {
  5759. if ($x != count($parent_arrays) - 1) {
  5760. $val .= '["'.$item.'"]["children"]';
  5761. } else {
  5762. $val .= '["'.$item.'"]["children"]';
  5763. }
  5764. $x++;
  5765. }
  5766. $val .= "";
  5767. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5768. eval($code_str);
  5769. } else {
  5770. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5771. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5772. }
  5773. }
  5774. }
  5775. return [
  5776. 'elements' => $elements,
  5777. 'default_data' => $default_data,
  5778. 'default_content' => $default_content,
  5779. 'return_audio' => $return_audio,
  5780. ];
  5781. }
  5782. /**
  5783. * @param string $updateAudio true/false strings
  5784. *
  5785. * @return string
  5786. */
  5787. public function returnLpItemList($updateAudio)
  5788. {
  5789. $result = $this->processBuildMenuElements($updateAudio);
  5790. $html = self::print_recursive(
  5791. $result['elements'],
  5792. $result['default_data'],
  5793. $result['default_content']
  5794. );
  5795. if (!empty($html)) {
  5796. $html .= Display::return_message(get_lang('DragAndDropAnElementHere'));
  5797. }
  5798. return $html;
  5799. }
  5800. /**
  5801. * @param string $update_audio
  5802. * @param bool $drop_element_here
  5803. *
  5804. * @return string
  5805. */
  5806. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5807. {
  5808. $result = $this->processBuildMenuElements($update_audio);
  5809. $list = '<ul id="lp_item_list">';
  5810. $tree = $this->print_recursive(
  5811. $result['elements'],
  5812. $result['default_data'],
  5813. $result['default_content']
  5814. );
  5815. if (!empty($tree)) {
  5816. $list .= $tree;
  5817. } else {
  5818. if ($drop_element_here) {
  5819. $list .= Display::return_message(get_lang('DragAndDropAnElementHere'));
  5820. }
  5821. }
  5822. $list .= '</ul>';
  5823. $return = Display::panelCollapse(
  5824. $this->name,
  5825. $list,
  5826. 'scorm-list',
  5827. null,
  5828. 'scorm-list-accordion',
  5829. 'scorm-list-collapse'
  5830. );
  5831. if ($update_audio === 'true') {
  5832. $return = $result['return_audio'];
  5833. }
  5834. return $return;
  5835. }
  5836. /**
  5837. * @param array $elements
  5838. * @param array $default_data
  5839. * @param array $default_content
  5840. *
  5841. * @return string
  5842. */
  5843. public function print_recursive($elements, $default_data, $default_content)
  5844. {
  5845. $return = '';
  5846. foreach ($elements as $key => $item) {
  5847. if (isset($item['load_data']) || empty($item['data'])) {
  5848. $item['data'] = $default_data[$item['load_data']];
  5849. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5850. }
  5851. $sub_list = '';
  5852. if (isset($item['type']) && $item['type'] === 'dir') {
  5853. // empty value
  5854. $sub_list = Display::tag('li', '', ['class' => 'sub_item empty']);
  5855. }
  5856. if (empty($item['children'])) {
  5857. $sub_list = Display::tag('ul', $sub_list, ['id' => 'UL_'.$key, 'class' => 'record li_container']);
  5858. $active = null;
  5859. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5860. $active = 'active';
  5861. }
  5862. $return .= Display::tag(
  5863. 'li',
  5864. Display::div($item['data'], ['class' => "item_data $active"]).$sub_list,
  5865. ['id' => $key, 'class' => 'record li_container']
  5866. );
  5867. } else {
  5868. // Sections
  5869. $data = '';
  5870. if (isset($item['children'])) {
  5871. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5872. }
  5873. $sub_list = Display::tag('ul', $sub_list.$data, ['id' => 'UL_'.$key, 'class' => 'record li_container']);
  5874. $return .= Display::tag(
  5875. 'li',
  5876. Display::div($item['data'], ['class' => 'item_data']).$sub_list,
  5877. ['id' => $key, 'class' => 'record li_container']
  5878. );
  5879. }
  5880. }
  5881. return $return;
  5882. }
  5883. /**
  5884. * This function builds the action menu.
  5885. *
  5886. * @param bool $returnContent Optional
  5887. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5888. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5889. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5890. *
  5891. * @return string
  5892. */
  5893. public function build_action_menu(
  5894. $returnContent = false,
  5895. $showRequirementButtons = true,
  5896. $isConfigPage = false,
  5897. $allowExpand = true
  5898. ) {
  5899. $actionsLeft = '';
  5900. $actionsRight = '';
  5901. $actionsLeft .= Display::url(
  5902. Display::return_icon(
  5903. 'back.png',
  5904. get_lang('ReturnToLearningPaths'),
  5905. '',
  5906. ICON_SIZE_MEDIUM
  5907. ),
  5908. 'lp_controller.php?'.api_get_cidreq()
  5909. );
  5910. $actionsLeft .= Display::url(
  5911. Display::return_icon(
  5912. 'preview_view.png',
  5913. get_lang('Preview'),
  5914. '',
  5915. ICON_SIZE_MEDIUM
  5916. ),
  5917. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5918. 'action' => 'view',
  5919. 'lp_id' => $this->lp_id,
  5920. 'isStudentView' => 'true',
  5921. ])
  5922. );
  5923. $actionsLeft .= Display::url(
  5924. Display::return_icon(
  5925. 'upload_audio.png',
  5926. get_lang('UpdateAllAudioFragments'),
  5927. '',
  5928. ICON_SIZE_MEDIUM
  5929. ),
  5930. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5931. 'action' => 'admin_view',
  5932. 'lp_id' => $this->lp_id,
  5933. 'updateaudio' => 'true',
  5934. ])
  5935. );
  5936. if (!$isConfigPage) {
  5937. $actionsLeft .= Display::url(
  5938. Display::return_icon(
  5939. 'settings.png',
  5940. get_lang('CourseSettings'),
  5941. '',
  5942. ICON_SIZE_MEDIUM
  5943. ),
  5944. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5945. 'action' => 'edit',
  5946. 'lp_id' => $this->lp_id,
  5947. ])
  5948. );
  5949. } else {
  5950. $actionsLeft .= Display::url(
  5951. Display::return_icon(
  5952. 'edit.png',
  5953. get_lang('Edit'),
  5954. '',
  5955. ICON_SIZE_MEDIUM
  5956. ),
  5957. 'lp_controller.php?'.http_build_query([
  5958. 'action' => 'build',
  5959. 'lp_id' => $this->lp_id,
  5960. ]).'&'.api_get_cidreq()
  5961. );
  5962. }
  5963. if ($allowExpand) {
  5964. $actionsLeft .= Display::url(
  5965. Display::return_icon(
  5966. 'expand.png',
  5967. get_lang('Expand'),
  5968. ['id' => 'expand'],
  5969. ICON_SIZE_MEDIUM
  5970. ).
  5971. Display::return_icon(
  5972. 'contract.png',
  5973. get_lang('Collapse'),
  5974. ['id' => 'contract', 'class' => 'hide'],
  5975. ICON_SIZE_MEDIUM
  5976. ),
  5977. '#',
  5978. ['role' => 'button', 'id' => 'hide_bar_template']
  5979. );
  5980. }
  5981. if ($showRequirementButtons) {
  5982. $buttons = [
  5983. [
  5984. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5985. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5986. 'action' => 'set_previous_step_as_prerequisite',
  5987. 'lp_id' => $this->lp_id,
  5988. ]),
  5989. ],
  5990. [
  5991. 'title' => get_lang('ClearAllPrerequisites'),
  5992. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5993. 'action' => 'clear_prerequisites',
  5994. 'lp_id' => $this->lp_id,
  5995. ]),
  5996. ],
  5997. ];
  5998. $actionsRight = Display::groupButtonWithDropDown(
  5999. get_lang('PrerequisitesOptions'),
  6000. $buttons,
  6001. true
  6002. );
  6003. }
  6004. $toolbar = Display::toolbarAction(
  6005. 'actions-lp-controller',
  6006. [$actionsLeft, $actionsRight]
  6007. );
  6008. if ($returnContent) {
  6009. return $toolbar;
  6010. }
  6011. echo $toolbar;
  6012. }
  6013. /**
  6014. * Creates the default learning path folder.
  6015. *
  6016. * @param array $course
  6017. * @param int $creatorId
  6018. *
  6019. * @return bool
  6020. */
  6021. public static function generate_learning_path_folder($course, $creatorId = 0)
  6022. {
  6023. // Creating learning_path folder
  6024. $dir = '/learning_path';
  6025. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  6026. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  6027. $folder = false;
  6028. if (!is_dir($filepath.'/'.$dir)) {
  6029. $folderData = create_unexisting_directory(
  6030. $course,
  6031. $creatorId,
  6032. 0,
  6033. null,
  6034. 0,
  6035. $filepath,
  6036. $dir,
  6037. get_lang('LearningPaths'),
  6038. 0
  6039. );
  6040. if (!empty($folderData)) {
  6041. $folder = true;
  6042. }
  6043. } else {
  6044. $folder = true;
  6045. }
  6046. return $folder;
  6047. }
  6048. /**
  6049. * @param array $course
  6050. * @param string $lp_name
  6051. * @param int $creatorId
  6052. *
  6053. * @return array
  6054. */
  6055. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  6056. {
  6057. $filepath = '';
  6058. $dir = '/learning_path/';
  6059. if (empty($lp_name)) {
  6060. $lp_name = $this->name;
  6061. }
  6062. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  6063. $folder = self::generate_learning_path_folder($course, $creatorId);
  6064. // Limits title size
  6065. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  6066. $dir = $dir.$title;
  6067. // Creating LP folder
  6068. $documentId = null;
  6069. if ($folder) {
  6070. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  6071. if (!is_dir($filepath.'/'.$dir)) {
  6072. $folderData = create_unexisting_directory(
  6073. $course,
  6074. $creatorId,
  6075. 0,
  6076. 0,
  6077. 0,
  6078. $filepath,
  6079. $dir,
  6080. $lp_name
  6081. );
  6082. if (!empty($folderData)) {
  6083. $folder = true;
  6084. }
  6085. $documentId = $folderData['id'];
  6086. } else {
  6087. $folder = true;
  6088. }
  6089. $dir = $dir.'/';
  6090. if ($folder) {
  6091. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  6092. }
  6093. }
  6094. if (empty($documentId)) {
  6095. $dir = api_remove_trailing_slash($dir);
  6096. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  6097. }
  6098. $array = [
  6099. 'dir' => $dir,
  6100. 'filepath' => $filepath,
  6101. 'folder' => $folder,
  6102. 'id' => $documentId,
  6103. ];
  6104. return $array;
  6105. }
  6106. /**
  6107. * Create a new document //still needs some finetuning.
  6108. *
  6109. * @param array $courseInfo
  6110. * @param string $content
  6111. * @param string $title
  6112. * @param string $extension
  6113. * @param int $parentId
  6114. * @param int $creatorId creator id
  6115. *
  6116. * @return int
  6117. */
  6118. public function create_document(
  6119. $courseInfo,
  6120. $content = '',
  6121. $title = '',
  6122. $extension = 'html',
  6123. $parentId = 0,
  6124. $creatorId = 0
  6125. ) {
  6126. if (!empty($courseInfo)) {
  6127. $course_id = $courseInfo['real_id'];
  6128. } else {
  6129. $course_id = api_get_course_int_id();
  6130. }
  6131. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  6132. $sessionId = api_get_session_id();
  6133. // Generates folder
  6134. $result = $this->generate_lp_folder($courseInfo);
  6135. $dir = $result['dir'];
  6136. if (empty($parentId) || $parentId == '/') {
  6137. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  6138. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  6139. if ($parentId === '/') {
  6140. $dir = '/';
  6141. }
  6142. // Please, do not modify this dirname formatting.
  6143. if (strstr($dir, '..')) {
  6144. $dir = '/';
  6145. }
  6146. if (!empty($dir[0]) && $dir[0] == '.') {
  6147. $dir = substr($dir, 1);
  6148. }
  6149. if (!empty($dir[0]) && $dir[0] != '/') {
  6150. $dir = '/'.$dir;
  6151. }
  6152. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  6153. $dir .= '/';
  6154. }
  6155. } else {
  6156. $parentInfo = DocumentManager::get_document_data_by_id(
  6157. $parentId,
  6158. $courseInfo['code']
  6159. );
  6160. if (!empty($parentInfo)) {
  6161. $dir = $parentInfo['path'].'/';
  6162. }
  6163. }
  6164. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  6165. if (!is_dir($filepath)) {
  6166. $dir = '/';
  6167. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  6168. }
  6169. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  6170. // is already escaped twice when it gets here.
  6171. $originalTitle = !empty($title) ? $title : $_POST['title'];
  6172. if (!empty($title)) {
  6173. $title = api_replace_dangerous_char(stripslashes($title));
  6174. } else {
  6175. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  6176. }
  6177. $title = disable_dangerous_file($title);
  6178. $filename = $title;
  6179. $content = !empty($content) ? $content : $_POST['content_lp'];
  6180. $tmp_filename = $filename;
  6181. $i = 0;
  6182. while (file_exists($filepath.$tmp_filename.'.'.$extension)) {
  6183. $tmp_filename = $filename.'_'.++$i;
  6184. }
  6185. $filename = $tmp_filename.'.'.$extension;
  6186. if ($extension == 'html') {
  6187. $content = stripslashes($content);
  6188. $content = str_replace(
  6189. api_get_path(WEB_COURSE_PATH),
  6190. api_get_path(REL_PATH).'courses/',
  6191. $content
  6192. );
  6193. // Change the path of mp3 to absolute.
  6194. // The first regexp deals with :// urls.
  6195. $content = preg_replace(
  6196. "|(flashvars=\"file=)([^:/]+)/|",
  6197. "$1".api_get_path(
  6198. REL_COURSE_PATH
  6199. ).$courseInfo['path'].'/document/',
  6200. $content
  6201. );
  6202. // The second regexp deals with audio/ urls.
  6203. $content = preg_replace(
  6204. "|(flashvars=\"file=)([^/]+)/|",
  6205. "$1".api_get_path(
  6206. REL_COURSE_PATH
  6207. ).$courseInfo['path'].'/document/$2/',
  6208. $content
  6209. );
  6210. // For flv player: To prevent edition problem with firefox,
  6211. // we have to use a strange tip (don't blame me please).
  6212. $content = str_replace(
  6213. '</body>',
  6214. '<style type="text/css">body{}</style></body>',
  6215. $content
  6216. );
  6217. }
  6218. if (!file_exists($filepath.$filename)) {
  6219. if ($fp = @fopen($filepath.$filename, 'w')) {
  6220. fputs($fp, $content);
  6221. fclose($fp);
  6222. $file_size = filesize($filepath.$filename);
  6223. $save_file_path = $dir.$filename;
  6224. $document_id = add_document(
  6225. $courseInfo,
  6226. $save_file_path,
  6227. 'file',
  6228. $file_size,
  6229. $tmp_filename,
  6230. '',
  6231. 0, //readonly
  6232. true,
  6233. null,
  6234. $sessionId,
  6235. $creatorId
  6236. );
  6237. if ($document_id) {
  6238. api_item_property_update(
  6239. $courseInfo,
  6240. TOOL_DOCUMENT,
  6241. $document_id,
  6242. 'DocumentAdded',
  6243. $creatorId,
  6244. null,
  6245. null,
  6246. null,
  6247. null,
  6248. $sessionId
  6249. );
  6250. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  6251. $new_title = $originalTitle;
  6252. if ($new_comment || $new_title) {
  6253. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6254. $ct = '';
  6255. if ($new_comment) {
  6256. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  6257. }
  6258. if ($new_title) {
  6259. $ct .= ", title='".Database::escape_string($new_title)."' ";
  6260. }
  6261. $sql = "UPDATE ".$tbl_doc." SET ".substr($ct, 1)."
  6262. WHERE c_id = ".$course_id." AND id = ".$document_id;
  6263. Database::query($sql);
  6264. }
  6265. }
  6266. return $document_id;
  6267. }
  6268. }
  6269. }
  6270. /**
  6271. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'.
  6272. *
  6273. * @param array $_course array
  6274. */
  6275. public function edit_document($_course)
  6276. {
  6277. $course_id = api_get_course_int_id();
  6278. $urlAppend = api_get_configuration_value('url_append');
  6279. // Please, do not modify this dirname formatting.
  6280. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  6281. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  6282. if (strstr($dir, '..')) {
  6283. $dir = '/';
  6284. }
  6285. if (isset($dir[0]) && $dir[0] == '.') {
  6286. $dir = substr($dir, 1);
  6287. }
  6288. if (isset($dir[0]) && $dir[0] != '/') {
  6289. $dir = '/'.$dir;
  6290. }
  6291. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  6292. $dir .= '/';
  6293. }
  6294. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  6295. if (!is_dir($filepath)) {
  6296. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  6297. }
  6298. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  6299. if (isset($_POST['path']) && !empty($_POST['path'])) {
  6300. $document_id = (int) $_POST['path'];
  6301. $documentInfo = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), false, null, true);
  6302. if (empty($documentInfo)) {
  6303. // Try with iid
  6304. $table = Database::get_course_table(TABLE_DOCUMENT);
  6305. $sql = "SELECT id, path FROM $table
  6306. WHERE c_id = $course_id AND iid = $document_id AND path NOT LIKE '%_DELETED_%' ";
  6307. $res_doc = Database::query($sql);
  6308. $row = Database::fetch_array($res_doc);
  6309. if ($row) {
  6310. $document_id = $row['id'];
  6311. $documentPath = $row['path'];
  6312. }
  6313. } else {
  6314. $documentPath = $documentInfo['path'];
  6315. }
  6316. $content = stripslashes($_POST['content_lp']);
  6317. $file = $filepath.$documentPath;
  6318. if (!file_exists($file)) {
  6319. return false;
  6320. }
  6321. if ($fp = @fopen($file, 'w')) {
  6322. $content = str_replace(
  6323. api_get_path(WEB_COURSE_PATH),
  6324. $urlAppend.api_get_path(REL_COURSE_PATH),
  6325. $content
  6326. );
  6327. // Change the path of mp3 to absolute.
  6328. // The first regexp deals with :// urls.
  6329. $content = preg_replace(
  6330. "|(flashvars=\"file=)([^:/]+)/|",
  6331. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  6332. $content
  6333. );
  6334. // The second regexp deals with audio/ urls.
  6335. $content = preg_replace(
  6336. "|(flashvars=\"file=)([^:/]+)/|",
  6337. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  6338. $content
  6339. );
  6340. fputs($fp, $content);
  6341. fclose($fp);
  6342. $sql = "UPDATE $table_doc SET
  6343. title='".Database::escape_string($_POST['title'])."'
  6344. WHERE c_id = $course_id AND id = ".$document_id;
  6345. Database::query($sql);
  6346. }
  6347. }
  6348. }
  6349. /**
  6350. * Displays the selected item, with a panel for manipulating the item.
  6351. *
  6352. * @param int $item_id
  6353. * @param string $msg
  6354. * @param bool $show_actions
  6355. *
  6356. * @return string
  6357. */
  6358. public function display_item($item_id, $msg = null, $show_actions = true)
  6359. {
  6360. $course_id = api_get_course_int_id();
  6361. $return = '';
  6362. if (is_numeric($item_id)) {
  6363. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6364. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  6365. WHERE lp.iid = ".intval($item_id);
  6366. $result = Database::query($sql);
  6367. while ($row = Database::fetch_array($result, 'ASSOC')) {
  6368. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  6369. // Prevents wrong parent selection for document, see Bug#1251.
  6370. if ($row['item_type'] != 'dir') {
  6371. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  6372. }
  6373. if ($show_actions) {
  6374. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6375. }
  6376. $return .= '<div style="padding:10px;">';
  6377. if ($msg != '') {
  6378. $return .= $msg;
  6379. }
  6380. $return .= '<h3>'.$row['title'].'</h3>';
  6381. switch ($row['item_type']) {
  6382. case TOOL_THREAD:
  6383. $link = $this->rl_get_resource_link_for_learnpath(
  6384. $course_id,
  6385. $row['lp_id'],
  6386. $item_id,
  6387. 0
  6388. );
  6389. $return .= Display::url(
  6390. get_lang('GoToThread'),
  6391. $link,
  6392. ['class' => 'btn btn-primary']
  6393. );
  6394. break;
  6395. case TOOL_FORUM:
  6396. $return .= Display::url(
  6397. get_lang('GoToForum'),
  6398. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$row['path'],
  6399. ['class' => 'btn btn-primary']
  6400. );
  6401. break;
  6402. case TOOL_QUIZ:
  6403. if (!empty($row['path'])) {
  6404. $exercise = new Exercise();
  6405. $exercise->read($row['path']);
  6406. $return .= $exercise->description.'<br />';
  6407. $return .= Display::url(
  6408. get_lang('GoToExercise'),
  6409. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$exercise->id,
  6410. ['class' => 'btn btn-primary']
  6411. );
  6412. }
  6413. break;
  6414. case TOOL_LP_FINAL_ITEM:
  6415. $return .= $this->getSavedFinalItem();
  6416. break;
  6417. case TOOL_DOCUMENT:
  6418. case TOOL_READOUT_TEXT:
  6419. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6420. $sql_doc = "SELECT path FROM $tbl_doc
  6421. WHERE c_id = $course_id AND iid = ".intval($row['path']);
  6422. $result = Database::query($sql_doc);
  6423. $path_file = Database::result($result, 0, 0);
  6424. $path_parts = pathinfo($path_file);
  6425. // TODO: Correct the following naive comparisons.
  6426. if (in_array($path_parts['extension'], [
  6427. 'html',
  6428. 'txt',
  6429. 'png',
  6430. 'jpg',
  6431. 'JPG',
  6432. 'jpeg',
  6433. 'JPEG',
  6434. 'gif',
  6435. 'swf',
  6436. 'pdf',
  6437. 'htm',
  6438. ])) {
  6439. $return .= $this->display_document($row['path'], true, true);
  6440. }
  6441. break;
  6442. case TOOL_HOTPOTATOES:
  6443. $return .= $this->display_document($row['path'], false, true);
  6444. break;
  6445. }
  6446. $return .= '</div>';
  6447. }
  6448. }
  6449. return $return;
  6450. }
  6451. /**
  6452. * Shows the needed forms for editing a specific item.
  6453. *
  6454. * @param int $item_id
  6455. *
  6456. * @throws Exception
  6457. * @throws HTML_QuickForm_Error
  6458. *
  6459. * @return string
  6460. */
  6461. public function display_edit_item($item_id)
  6462. {
  6463. $course_id = api_get_course_int_id();
  6464. $return = '';
  6465. $item_id = (int) $item_id;
  6466. if (empty($item_id)) {
  6467. return '';
  6468. }
  6469. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6470. $sql = "SELECT * FROM $tbl_lp_item
  6471. WHERE iid = ".$item_id;
  6472. $res = Database::query($sql);
  6473. $row = Database::fetch_array($res);
  6474. switch ($row['item_type']) {
  6475. case 'dir':
  6476. case 'asset':
  6477. case 'sco':
  6478. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  6479. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6480. $return .= $this->display_item_form(
  6481. $row['item_type'],
  6482. get_lang('EditCurrentChapter').' :',
  6483. 'edit',
  6484. $item_id,
  6485. $row
  6486. );
  6487. } else {
  6488. $return .= $this->display_item_form(
  6489. $row['item_type'],
  6490. get_lang('EditCurrentChapter').' :',
  6491. 'edit_item',
  6492. $item_id,
  6493. $row
  6494. );
  6495. }
  6496. break;
  6497. case TOOL_DOCUMENT:
  6498. case TOOL_READOUT_TEXT:
  6499. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6500. $sql = "SELECT lp.*, doc.path as dir
  6501. FROM $tbl_lp_item as lp
  6502. LEFT JOIN $tbl_doc as doc
  6503. ON (doc.iid = lp.path AND lp.c_id = doc.c_id)
  6504. WHERE
  6505. doc.c_id = $course_id AND
  6506. lp.iid = ".$item_id;
  6507. $res_step = Database::query($sql);
  6508. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6509. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6510. if ($row['item_type'] === TOOL_DOCUMENT) {
  6511. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6512. }
  6513. if ($row['item_type'] === TOOL_READOUT_TEXT) {
  6514. $return .= $this->displayFrmReadOutText('edit', $item_id, $row_step);
  6515. }
  6516. break;
  6517. case TOOL_LINK:
  6518. $linkId = (int) $row['path'];
  6519. if (!empty($linkId)) {
  6520. $table = Database::get_course_table(TABLE_LINK);
  6521. $sql = 'SELECT url FROM '.$table.'
  6522. WHERE c_id = '.$course_id.' AND iid = '.$linkId;
  6523. $res_link = Database::query($sql);
  6524. $row_link = Database::fetch_array($res_link);
  6525. if (empty($row_link)) {
  6526. // Try with id
  6527. $sql = 'SELECT url FROM '.$table.'
  6528. WHERE c_id = '.$course_id.' AND id = '.$linkId;
  6529. $res_link = Database::query($sql);
  6530. $row_link = Database::fetch_array($res_link);
  6531. }
  6532. if (is_array($row_link)) {
  6533. $row['url'] = $row_link['url'];
  6534. }
  6535. }
  6536. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6537. $return .= $this->display_link_form('edit', $item_id, $row);
  6538. break;
  6539. case TOOL_LP_FINAL_ITEM:
  6540. Session::write('finalItem', true);
  6541. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6542. $sql = "SELECT lp.*, doc.path as dir
  6543. FROM $tbl_lp_item as lp
  6544. LEFT JOIN $tbl_doc as doc
  6545. ON (doc.iid = lp.path AND lp.c_id = doc.c_id)
  6546. WHERE
  6547. doc.c_id = $course_id AND
  6548. lp.iid = ".$item_id;
  6549. $res_step = Database::query($sql);
  6550. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6551. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6552. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6553. break;
  6554. case TOOL_QUIZ:
  6555. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6556. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6557. break;
  6558. case TOOL_HOTPOTATOES:
  6559. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6560. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6561. break;
  6562. case TOOL_STUDENTPUBLICATION:
  6563. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6564. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6565. break;
  6566. case TOOL_FORUM:
  6567. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6568. $return .= $this->display_forum_form('edit', $item_id, $row);
  6569. break;
  6570. case TOOL_THREAD:
  6571. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6572. $return .= $this->display_thread_form('edit', $item_id, $row);
  6573. break;
  6574. }
  6575. return $return;
  6576. }
  6577. /**
  6578. * Function that displays a list with al the resources that
  6579. * could be added to the learning path.
  6580. *
  6581. * @throws Exception
  6582. * @throws HTML_QuickForm_Error
  6583. *
  6584. * @return bool
  6585. */
  6586. public function display_resources()
  6587. {
  6588. $course_code = api_get_course_id();
  6589. // Get all the docs.
  6590. $documents = $this->get_documents(true);
  6591. // Get all the exercises.
  6592. $exercises = $this->get_exercises();
  6593. // Get all the links.
  6594. $links = $this->get_links();
  6595. // Get all the student publications.
  6596. $works = $this->get_student_publications();
  6597. // Get all the forums.
  6598. $forums = $this->get_forums(null, $course_code);
  6599. // Get the final item form (see BT#11048) .
  6600. $finish = $this->getFinalItemForm();
  6601. $headers = [
  6602. Display::return_icon('folder_document.png', get_lang('Documents'), [], ICON_SIZE_BIG),
  6603. Display::return_icon('quiz.png', get_lang('Quiz'), [], ICON_SIZE_BIG),
  6604. Display::return_icon('links.png', get_lang('Links'), [], ICON_SIZE_BIG),
  6605. Display::return_icon('works.png', get_lang('Works'), [], ICON_SIZE_BIG),
  6606. Display::return_icon('forum.png', get_lang('Forums'), [], ICON_SIZE_BIG),
  6607. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), [], ICON_SIZE_BIG),
  6608. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6609. ];
  6610. echo Display::return_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'), 'normal');
  6611. $dir = $this->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6612. $selected = isset($_REQUEST['lp_build_selected']) ? (int) $_REQUEST['lp_build_selected'] : 0;
  6613. echo Display::tabs(
  6614. $headers,
  6615. [
  6616. $documents,
  6617. $exercises,
  6618. $links,
  6619. $works,
  6620. $forums,
  6621. $dir,
  6622. $finish,
  6623. ],
  6624. 'resource_tab',
  6625. [],
  6626. [],
  6627. $selected
  6628. );
  6629. return true;
  6630. }
  6631. /**
  6632. * Returns the extension of a document.
  6633. *
  6634. * @param string $filename
  6635. *
  6636. * @return string Extension (part after the last dot)
  6637. */
  6638. public function get_extension($filename)
  6639. {
  6640. $explode = explode('.', $filename);
  6641. return $explode[count($explode) - 1];
  6642. }
  6643. /**
  6644. * Displays a document by id.
  6645. *
  6646. * @param int $id
  6647. * @param bool $show_title
  6648. * @param bool $iframe
  6649. * @param bool $edit_link
  6650. *
  6651. * @return string
  6652. */
  6653. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6654. {
  6655. $_course = api_get_course_info();
  6656. $course_id = api_get_course_int_id();
  6657. $id = (int) $id;
  6658. $return = '';
  6659. $table = Database::get_course_table(TABLE_DOCUMENT);
  6660. $sql_doc = "SELECT * FROM $table
  6661. WHERE c_id = $course_id AND iid = $id";
  6662. $res_doc = Database::query($sql_doc);
  6663. $row_doc = Database::fetch_array($res_doc);
  6664. // TODO: Add a path filter.
  6665. if ($iframe) {
  6666. $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>';
  6667. } else {
  6668. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/'.$row_doc['path']);
  6669. }
  6670. return $return;
  6671. }
  6672. /**
  6673. * Return HTML form to add/edit a quiz.
  6674. *
  6675. * @param string $action Action (add/edit)
  6676. * @param int $id Item ID if already exists
  6677. * @param mixed $extra_info Extra information (quiz ID if integer)
  6678. *
  6679. * @throws Exception
  6680. *
  6681. * @return string HTML form
  6682. */
  6683. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6684. {
  6685. $course_id = api_get_course_int_id();
  6686. $id = (int) $id;
  6687. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6688. if ($id != 0 && is_array($extra_info)) {
  6689. $item_title = $extra_info['title'];
  6690. $item_description = $extra_info['description'];
  6691. } elseif (is_numeric($extra_info)) {
  6692. $sql = "SELECT title, description
  6693. FROM $tbl_quiz
  6694. WHERE c_id = $course_id AND iid = ".$extra_info;
  6695. $result = Database::query($sql);
  6696. $row = Database::fetch_array($result);
  6697. $item_title = $row['title'];
  6698. $item_description = $row['description'];
  6699. } else {
  6700. $item_title = '';
  6701. $item_description = '';
  6702. }
  6703. $item_title = Security::remove_XSS($item_title);
  6704. $item_description = Security::remove_XSS($item_description);
  6705. $parent = 0;
  6706. if ($id != 0 && is_array($extra_info)) {
  6707. $parent = $extra_info['parent_item_id'];
  6708. }
  6709. $arrLP = $this->getItemsForForm();
  6710. $this->tree_array($arrLP);
  6711. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  6712. unset($this->arrMenu);
  6713. $form = new FormValidator(
  6714. 'quiz_form',
  6715. 'POST',
  6716. $this->getCurrentBuildingModeURL()
  6717. );
  6718. $defaults = [];
  6719. if ($action === 'add') {
  6720. $legend = get_lang('CreateTheExercise');
  6721. } elseif ($action === 'move') {
  6722. $legend = get_lang('MoveTheCurrentExercise');
  6723. } else {
  6724. $legend = get_lang('EditCurrentExecice');
  6725. }
  6726. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  6727. $legend .= Display::return_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6728. }
  6729. $form->addHeader($legend);
  6730. if ($action != 'move') {
  6731. $this->setItemTitle($form);
  6732. $defaults['title'] = $item_title;
  6733. }
  6734. // Select for Parent item, root or chapter
  6735. $selectParent = $form->addSelect(
  6736. 'parent',
  6737. get_lang('Parent'),
  6738. [],
  6739. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6740. );
  6741. $selectParent->addOption($this->name, 0);
  6742. $arrHide = [
  6743. $id,
  6744. ];
  6745. for ($i = 0; $i < count($arrLP); $i++) {
  6746. if ($action != 'add') {
  6747. if (
  6748. ($arrLP[$i]['item_type'] == 'dir') &&
  6749. !in_array($arrLP[$i]['id'], $arrHide) &&
  6750. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6751. ) {
  6752. $selectParent->addOption(
  6753. $arrLP[$i]['title'],
  6754. $arrLP[$i]['id'],
  6755. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6756. );
  6757. if ($parent == $arrLP[$i]['id']) {
  6758. $selectParent->setSelected($arrLP[$i]['id']);
  6759. }
  6760. } else {
  6761. $arrHide[] = $arrLP[$i]['id'];
  6762. }
  6763. } else {
  6764. if ($arrLP[$i]['item_type'] == 'dir') {
  6765. $selectParent->addOption(
  6766. $arrLP[$i]['title'],
  6767. $arrLP[$i]['id'],
  6768. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6769. );
  6770. if ($parent == $arrLP[$i]['id']) {
  6771. $selectParent->setSelected($arrLP[$i]['id']);
  6772. }
  6773. }
  6774. }
  6775. }
  6776. if (is_array($arrLP)) {
  6777. reset($arrLP);
  6778. }
  6779. $selectPrevious = $form->addSelect(
  6780. 'previous',
  6781. get_lang('Position'),
  6782. [],
  6783. ['id' => 'previous']
  6784. );
  6785. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6786. for ($i = 0; $i < count($arrLP); $i++) {
  6787. if ($arrLP[$i]['parent_item_id'] == $parent &&
  6788. $arrLP[$i]['id'] != $id
  6789. ) {
  6790. $selectPrevious->addOption(
  6791. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  6792. $arrLP[$i]['id']
  6793. );
  6794. if (is_array($extra_info)) {
  6795. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6796. $selectPrevious->setSelected($arrLP[$i]['id']);
  6797. }
  6798. } elseif ($action == 'add') {
  6799. $selectPrevious->setSelected($arrLP[$i]['id']);
  6800. }
  6801. }
  6802. }
  6803. if ($action != 'move') {
  6804. $arrHide = [];
  6805. for ($i = 0; $i < count($arrLP); $i++) {
  6806. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6807. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6808. }
  6809. }
  6810. }
  6811. if ($action === 'add') {
  6812. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6813. } else {
  6814. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6815. }
  6816. if ($action === 'move') {
  6817. $form->addHidden('title', $item_title);
  6818. $form->addHidden('description', $item_description);
  6819. }
  6820. if (is_numeric($extra_info)) {
  6821. $form->addHidden('path', $extra_info);
  6822. } elseif (is_array($extra_info)) {
  6823. $form->addHidden('path', $extra_info['path']);
  6824. }
  6825. $form->addHidden('type', TOOL_QUIZ);
  6826. $form->addHidden('post_time', time());
  6827. $form->setDefaults($defaults);
  6828. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  6829. }
  6830. /**
  6831. * Addition of Hotpotatoes tests.
  6832. *
  6833. * @param string $action
  6834. * @param int $id Internal ID of the item
  6835. * @param string $extra_info
  6836. *
  6837. * @return string HTML structure to display the hotpotatoes addition formular
  6838. */
  6839. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6840. {
  6841. $course_id = api_get_course_int_id();
  6842. $uploadPath = DIR_HOTPOTATOES;
  6843. if ($id != 0 && is_array($extra_info)) {
  6844. $item_title = stripslashes($extra_info['title']);
  6845. $item_description = stripslashes($extra_info['description']);
  6846. } elseif (is_numeric($extra_info)) {
  6847. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6848. $sql = "SELECT * FROM $TBL_DOCUMENT
  6849. WHERE
  6850. c_id = $course_id AND
  6851. path LIKE '".$uploadPath."/%/%htm%' AND
  6852. iid = ".(int) $extra_info."
  6853. ORDER BY iid ASC";
  6854. $res_hot = Database::query($sql);
  6855. $row = Database::fetch_array($res_hot);
  6856. $item_title = $row['title'];
  6857. $item_description = $row['description'];
  6858. if (!empty($row['comment'])) {
  6859. $item_title = $row['comment'];
  6860. }
  6861. } else {
  6862. $item_title = '';
  6863. $item_description = '';
  6864. }
  6865. $parent = 0;
  6866. if ($id != 0 && is_array($extra_info)) {
  6867. $parent = $extra_info['parent_item_id'];
  6868. }
  6869. $arrLP = $this->getItemsForForm();
  6870. $legend = '<legend>';
  6871. if ($action == 'add') {
  6872. $legend .= get_lang('CreateTheExercise');
  6873. } elseif ($action == 'move') {
  6874. $legend .= get_lang('MoveTheCurrentExercise');
  6875. } else {
  6876. $legend .= get_lang('EditCurrentExecice');
  6877. }
  6878. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  6879. $legend .= Display:: return_message(
  6880. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6881. );
  6882. }
  6883. $legend .= '</legend>';
  6884. $return = '<form method="POST">';
  6885. $return .= $legend;
  6886. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6887. $return .= '<tr>';
  6888. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6889. $return .= '<td class="input">';
  6890. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6891. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6892. $arrHide = [$id];
  6893. if (count($arrLP) > 0) {
  6894. for ($i = 0; $i < count($arrLP); $i++) {
  6895. if ($action != 'add') {
  6896. if ($arrLP[$i]['item_type'] == 'dir' &&
  6897. !in_array($arrLP[$i]['id'], $arrHide) &&
  6898. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6899. ) {
  6900. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6901. } else {
  6902. $arrHide[] = $arrLP[$i]['id'];
  6903. }
  6904. } else {
  6905. if ($arrLP[$i]['item_type'] == 'dir') {
  6906. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6907. }
  6908. }
  6909. }
  6910. reset($arrLP);
  6911. }
  6912. $return .= '</select>';
  6913. $return .= '</td>';
  6914. $return .= '</tr>';
  6915. $return .= '<tr>';
  6916. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6917. $return .= '<td class="input">';
  6918. $return .= '<select id="previous" name="previous" size="1">';
  6919. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6920. for ($i = 0; $i < count($arrLP); $i++) {
  6921. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6922. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6923. $selected = 'selected="selected" ';
  6924. } elseif ($action == 'add') {
  6925. $selected = 'selected="selected" ';
  6926. } else {
  6927. $selected = '';
  6928. }
  6929. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.
  6930. get_lang('After').' "'.$arrLP[$i]['title'].'"</option>';
  6931. }
  6932. }
  6933. $return .= '</select>';
  6934. $return .= '</td>';
  6935. $return .= '</tr>';
  6936. if ($action != 'move') {
  6937. $return .= '<tr>';
  6938. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6939. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6940. $return .= '</tr>';
  6941. $id_prerequisite = 0;
  6942. if (is_array($arrLP) && count($arrLP) > 0) {
  6943. foreach ($arrLP as $key => $value) {
  6944. if ($value['id'] == $id) {
  6945. $id_prerequisite = $value['prerequisite'];
  6946. break;
  6947. }
  6948. }
  6949. $arrHide = [];
  6950. for ($i = 0; $i < count($arrLP); $i++) {
  6951. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6952. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6953. }
  6954. }
  6955. }
  6956. }
  6957. $return .= '<tr>';
  6958. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.
  6959. get_lang('SaveHotpotatoes').'</button></td>';
  6960. $return .= '</tr>';
  6961. $return .= '</table>';
  6962. if ($action == 'move') {
  6963. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6964. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6965. }
  6966. if (is_numeric($extra_info)) {
  6967. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6968. } elseif (is_array($extra_info)) {
  6969. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6970. }
  6971. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6972. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6973. $return .= '</form>';
  6974. return $return;
  6975. }
  6976. /**
  6977. * Return the form to display the forum edit/add option.
  6978. *
  6979. * @param string $action
  6980. * @param int $id ID of the lp_item if already exists
  6981. * @param string $extra_info
  6982. *
  6983. * @throws Exception
  6984. *
  6985. * @return string HTML form
  6986. */
  6987. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6988. {
  6989. $course_id = api_get_course_int_id();
  6990. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6991. $item_title = '';
  6992. $item_description = '';
  6993. if ($id != 0 && is_array($extra_info)) {
  6994. $item_title = stripslashes($extra_info['title']);
  6995. } elseif (is_numeric($extra_info)) {
  6996. $sql = "SELECT forum_title as title, forum_comment as comment
  6997. FROM $tbl_forum
  6998. WHERE c_id = $course_id AND forum_id = ".$extra_info;
  6999. $result = Database::query($sql);
  7000. $row = Database::fetch_array($result);
  7001. $item_title = $row['title'];
  7002. $item_description = $row['comment'];
  7003. }
  7004. $parent = 0;
  7005. if ($id != 0 && is_array($extra_info)) {
  7006. $parent = $extra_info['parent_item_id'];
  7007. }
  7008. $arrLP = $this->getItemsForForm();
  7009. $this->tree_array($arrLP);
  7010. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  7011. unset($this->arrMenu);
  7012. if ($action == 'add') {
  7013. $legend = get_lang('CreateTheForum');
  7014. } elseif ($action == 'move') {
  7015. $legend = get_lang('MoveTheCurrentForum');
  7016. } else {
  7017. $legend = get_lang('EditCurrentForum');
  7018. }
  7019. $form = new FormValidator(
  7020. 'forum_form',
  7021. 'POST',
  7022. $this->getCurrentBuildingModeURL()
  7023. );
  7024. $defaults = [];
  7025. $form->addHeader($legend);
  7026. if ($action != 'move') {
  7027. $this->setItemTitle($form);
  7028. $defaults['title'] = $item_title;
  7029. }
  7030. $selectParent = $form->addSelect(
  7031. 'parent',
  7032. get_lang('Parent'),
  7033. [],
  7034. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7035. );
  7036. $selectParent->addOption($this->name, 0);
  7037. $arrHide = [
  7038. $id,
  7039. ];
  7040. for ($i = 0; $i < count($arrLP); $i++) {
  7041. if ($action != 'add') {
  7042. if ($arrLP[$i]['item_type'] == 'dir' &&
  7043. !in_array($arrLP[$i]['id'], $arrHide) &&
  7044. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7045. ) {
  7046. $selectParent->addOption(
  7047. $arrLP[$i]['title'],
  7048. $arrLP[$i]['id'],
  7049. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7050. );
  7051. if ($parent == $arrLP[$i]['id']) {
  7052. $selectParent->setSelected($arrLP[$i]['id']);
  7053. }
  7054. } else {
  7055. $arrHide[] = $arrLP[$i]['id'];
  7056. }
  7057. } else {
  7058. if ($arrLP[$i]['item_type'] == 'dir') {
  7059. $selectParent->addOption(
  7060. $arrLP[$i]['title'],
  7061. $arrLP[$i]['id'],
  7062. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7063. );
  7064. if ($parent == $arrLP[$i]['id']) {
  7065. $selectParent->setSelected($arrLP[$i]['id']);
  7066. }
  7067. }
  7068. }
  7069. }
  7070. if (is_array($arrLP)) {
  7071. reset($arrLP);
  7072. }
  7073. $selectPrevious = $form->addSelect(
  7074. 'previous',
  7075. get_lang('Position'),
  7076. [],
  7077. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7078. );
  7079. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7080. for ($i = 0; $i < count($arrLP); $i++) {
  7081. if ($arrLP[$i]['parent_item_id'] == $parent &&
  7082. $arrLP[$i]['id'] != $id
  7083. ) {
  7084. $selectPrevious->addOption(
  7085. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  7086. $arrLP[$i]['id']
  7087. );
  7088. if (isset($extra_info['previous_item_id']) &&
  7089. $extra_info['previous_item_id'] == $arrLP[$i]['id']
  7090. ) {
  7091. $selectPrevious->setSelected($arrLP[$i]['id']);
  7092. } elseif ($action == 'add') {
  7093. $selectPrevious->setSelected($arrLP[$i]['id']);
  7094. }
  7095. }
  7096. }
  7097. if ($action != 'move') {
  7098. $id_prerequisite = 0;
  7099. if (is_array($arrLP)) {
  7100. foreach ($arrLP as $key => $value) {
  7101. if ($value['id'] == $id) {
  7102. $id_prerequisite = $value['prerequisite'];
  7103. break;
  7104. }
  7105. }
  7106. }
  7107. $arrHide = [];
  7108. for ($i = 0; $i < count($arrLP); $i++) {
  7109. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7110. if (isset($extra_info['previous_item_id']) &&
  7111. $extra_info['previous_item_id'] == $arrLP[$i]['id']
  7112. ) {
  7113. $s_selected_position = $arrLP[$i]['id'];
  7114. } elseif ($action == 'add') {
  7115. $s_selected_position = 0;
  7116. }
  7117. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7118. }
  7119. }
  7120. }
  7121. if ($action == 'add') {
  7122. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  7123. } else {
  7124. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  7125. }
  7126. if ($action == 'move') {
  7127. $form->addHidden('title', $item_title);
  7128. $form->addHidden('description', $item_description);
  7129. }
  7130. if (is_numeric($extra_info)) {
  7131. $form->addHidden('path', $extra_info);
  7132. } elseif (is_array($extra_info)) {
  7133. $form->addHidden('path', $extra_info['path']);
  7134. }
  7135. $form->addHidden('type', TOOL_FORUM);
  7136. $form->addHidden('post_time', time());
  7137. $form->setDefaults($defaults);
  7138. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  7139. }
  7140. /**
  7141. * Return HTML form to add/edit forum threads.
  7142. *
  7143. * @param string $action
  7144. * @param int $id Item ID if already exists in learning path
  7145. * @param string $extra_info
  7146. *
  7147. * @throws Exception
  7148. *
  7149. * @return string HTML form
  7150. */
  7151. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  7152. {
  7153. $course_id = api_get_course_int_id();
  7154. if (empty($course_id)) {
  7155. return null;
  7156. }
  7157. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  7158. $item_title = '';
  7159. $item_description = '';
  7160. if ($id != 0 && is_array($extra_info)) {
  7161. $item_title = stripslashes($extra_info['title']);
  7162. } elseif (is_numeric($extra_info)) {
  7163. $sql = "SELECT thread_title as title FROM $tbl_forum
  7164. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  7165. $result = Database::query($sql);
  7166. $row = Database::fetch_array($result);
  7167. $item_title = $row['title'];
  7168. $item_description = '';
  7169. }
  7170. $parent = 0;
  7171. if ($id != 0 && is_array($extra_info)) {
  7172. $parent = $extra_info['parent_item_id'];
  7173. }
  7174. $arrLP = $this->getItemsForForm();
  7175. $this->tree_array($arrLP);
  7176. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  7177. unset($this->arrMenu);
  7178. $form = new FormValidator(
  7179. 'thread_form',
  7180. 'POST',
  7181. $this->getCurrentBuildingModeURL()
  7182. );
  7183. $defaults = [];
  7184. if ($action == 'add') {
  7185. $legend = get_lang('CreateTheForum');
  7186. } elseif ($action == 'move') {
  7187. $legend = get_lang('MoveTheCurrentForum');
  7188. } else {
  7189. $legend = get_lang('EditCurrentForum');
  7190. }
  7191. $form->addHeader($legend);
  7192. $selectParent = $form->addSelect(
  7193. 'parent',
  7194. get_lang('Parent'),
  7195. [],
  7196. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  7197. );
  7198. $selectParent->addOption($this->name, 0);
  7199. $arrHide = [
  7200. $id,
  7201. ];
  7202. for ($i = 0; $i < count($arrLP); $i++) {
  7203. if ($action != 'add') {
  7204. if (
  7205. ($arrLP[$i]['item_type'] == 'dir') &&
  7206. !in_array($arrLP[$i]['id'], $arrHide) &&
  7207. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7208. ) {
  7209. $selectParent->addOption(
  7210. $arrLP[$i]['title'],
  7211. $arrLP[$i]['id'],
  7212. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7213. );
  7214. if ($parent == $arrLP[$i]['id']) {
  7215. $selectParent->setSelected($arrLP[$i]['id']);
  7216. }
  7217. } else {
  7218. $arrHide[] = $arrLP[$i]['id'];
  7219. }
  7220. } else {
  7221. if ($arrLP[$i]['item_type'] == 'dir') {
  7222. $selectParent->addOption(
  7223. $arrLP[$i]['title'],
  7224. $arrLP[$i]['id'],
  7225. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7226. );
  7227. if ($parent == $arrLP[$i]['id']) {
  7228. $selectParent->setSelected($arrLP[$i]['id']);
  7229. }
  7230. }
  7231. }
  7232. }
  7233. if ($arrLP != null) {
  7234. reset($arrLP);
  7235. }
  7236. $selectPrevious = $form->addSelect(
  7237. 'previous',
  7238. get_lang('Position'),
  7239. [],
  7240. ['id' => 'previous']
  7241. );
  7242. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7243. for ($i = 0; $i < count($arrLP); $i++) {
  7244. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7245. $selectPrevious->addOption(
  7246. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  7247. $arrLP[$i]['id']
  7248. );
  7249. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7250. $selectPrevious->setSelected($arrLP[$i]['id']);
  7251. } elseif ($action == 'add') {
  7252. $selectPrevious->setSelected($arrLP[$i]['id']);
  7253. }
  7254. }
  7255. }
  7256. if ($action != 'move') {
  7257. $this->setItemTitle($form);
  7258. $defaults['title'] = $item_title;
  7259. $id_prerequisite = 0;
  7260. if ($arrLP != null) {
  7261. foreach ($arrLP as $key => $value) {
  7262. if ($value['id'] == $id) {
  7263. $id_prerequisite = $value['prerequisite'];
  7264. break;
  7265. }
  7266. }
  7267. }
  7268. $arrHide = [];
  7269. $s_selected_position = 0;
  7270. for ($i = 0; $i < count($arrLP); $i++) {
  7271. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7272. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7273. $s_selected_position = $arrLP[$i]['id'];
  7274. } elseif ($action == 'add') {
  7275. $s_selected_position = 0;
  7276. }
  7277. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7278. }
  7279. }
  7280. $selectPrerequisites = $form->addSelect(
  7281. 'prerequisites',
  7282. get_lang('LearnpathPrerequisites'),
  7283. [],
  7284. ['id' => 'prerequisites']
  7285. );
  7286. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  7287. foreach ($arrHide as $key => $value) {
  7288. $selectPrerequisites->addOption($value['value'], $key);
  7289. if ($key == $s_selected_position && $action == 'add') {
  7290. $selectPrerequisites->setSelected($key);
  7291. } elseif ($key == $id_prerequisite && $action == 'edit') {
  7292. $selectPrerequisites->setSelected($key);
  7293. }
  7294. }
  7295. }
  7296. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  7297. if ($action == 'move') {
  7298. $form->addHidden('title', $item_title);
  7299. $form->addHidden('description', $item_description);
  7300. }
  7301. if (is_numeric($extra_info)) {
  7302. $form->addHidden('path', $extra_info);
  7303. } elseif (is_array($extra_info)) {
  7304. $form->addHidden('path', $extra_info['path']);
  7305. }
  7306. $form->addHidden('type', TOOL_THREAD);
  7307. $form->addHidden('post_time', time());
  7308. $form->setDefaults($defaults);
  7309. return $form->returnForm();
  7310. }
  7311. /**
  7312. * Return the HTML form to display an item (generally a dir item).
  7313. *
  7314. * @param string $item_type
  7315. * @param string $title
  7316. * @param string $action
  7317. * @param int $id
  7318. * @param string $extra_info
  7319. *
  7320. * @throws Exception
  7321. * @throws HTML_QuickForm_Error
  7322. *
  7323. * @return string HTML form
  7324. */
  7325. public function display_item_form(
  7326. $item_type,
  7327. $title = '',
  7328. $action = 'add_item',
  7329. $id = 0,
  7330. $extra_info = 'new'
  7331. ) {
  7332. $_course = api_get_course_info();
  7333. global $charset;
  7334. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7335. $item_title = '';
  7336. $item_description = '';
  7337. $item_path_fck = '';
  7338. if ($id != 0 && is_array($extra_info)) {
  7339. $item_title = $extra_info['title'];
  7340. $item_description = $extra_info['description'];
  7341. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  7342. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  7343. }
  7344. $parent = 0;
  7345. if ($id != 0 && is_array($extra_info)) {
  7346. $parent = $extra_info['parent_item_id'];
  7347. }
  7348. $id = (int) $id;
  7349. $sql = "SELECT * FROM $tbl_lp_item
  7350. WHERE
  7351. lp_id = ".$this->lp_id." AND
  7352. iid != $id";
  7353. if ($item_type == 'dir') {
  7354. $sql .= " AND parent_item_id = 0";
  7355. }
  7356. $result = Database::query($sql);
  7357. $arrLP = [];
  7358. while ($row = Database::fetch_array($result)) {
  7359. $arrLP[] = [
  7360. 'id' => $row['iid'],
  7361. 'item_type' => $row['item_type'],
  7362. 'title' => $this->cleanItemTitle($row['title']),
  7363. 'title_raw' => $row['title'],
  7364. 'path' => $row['path'],
  7365. 'description' => $row['description'],
  7366. 'parent_item_id' => $row['parent_item_id'],
  7367. 'previous_item_id' => $row['previous_item_id'],
  7368. 'next_item_id' => $row['next_item_id'],
  7369. 'max_score' => $row['max_score'],
  7370. 'min_score' => $row['min_score'],
  7371. 'mastery_score' => $row['mastery_score'],
  7372. 'prerequisite' => $row['prerequisite'],
  7373. 'display_order' => $row['display_order'],
  7374. ];
  7375. }
  7376. $this->tree_array($arrLP);
  7377. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  7378. unset($this->arrMenu);
  7379. $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  7380. $form = new FormValidator('form_'.$item_type, 'POST', $url);
  7381. $defaults['title'] = api_html_entity_decode(
  7382. $item_title,
  7383. ENT_QUOTES,
  7384. $charset
  7385. );
  7386. $defaults['description'] = $item_description;
  7387. $form->addHeader($title);
  7388. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  7389. $arrHide[0]['padding'] = 20;
  7390. $charset = api_get_system_encoding();
  7391. for ($i = 0; $i < count($arrLP); $i++) {
  7392. if ($action != 'add') {
  7393. if ($arrLP[$i]['item_type'] === 'dir' && !in_array($arrLP[$i]['id'], $arrHide) &&
  7394. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7395. ) {
  7396. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7397. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7398. if ($parent == $arrLP[$i]['id']) {
  7399. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7400. }
  7401. }
  7402. } else {
  7403. if ($arrLP[$i]['item_type'] === 'dir') {
  7404. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7405. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7406. if ($parent == $arrLP[$i]['id']) {
  7407. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7408. }
  7409. }
  7410. }
  7411. }
  7412. if ($action != 'move') {
  7413. $this->setItemTitle($form);
  7414. } else {
  7415. $form->addElement('hidden', 'title');
  7416. }
  7417. $parentSelect = $form->addElement(
  7418. 'select',
  7419. 'parent',
  7420. get_lang('Parent'),
  7421. '',
  7422. [
  7423. 'id' => 'idParent',
  7424. 'onchange' => 'javascript: load_cbo(this.value);',
  7425. ]
  7426. );
  7427. foreach ($arrHide as $key => $value) {
  7428. $parentSelect->addOption(
  7429. $value['value'],
  7430. $key,
  7431. 'style="padding-left:'.$value['padding'].'px;"'
  7432. );
  7433. $lastPosition = $key;
  7434. }
  7435. if (!empty($s_selected_parent)) {
  7436. $parentSelect->setSelected($s_selected_parent);
  7437. }
  7438. if (is_array($arrLP)) {
  7439. reset($arrLP);
  7440. }
  7441. $arrHide = [];
  7442. // POSITION
  7443. for ($i = 0; $i < count($arrLP); $i++) {
  7444. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id &&
  7445. $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7446. //this is the same!
  7447. if (isset($extra_info['previous_item_id']) &&
  7448. $extra_info['previous_item_id'] == $arrLP[$i]['id']
  7449. ) {
  7450. $s_selected_position = $arrLP[$i]['id'];
  7451. } elseif ($action == 'add') {
  7452. $s_selected_position = $arrLP[$i]['id'];
  7453. }
  7454. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7455. }
  7456. }
  7457. $position = $form->addElement(
  7458. 'select',
  7459. 'previous',
  7460. get_lang('Position'),
  7461. '',
  7462. ['id' => 'previous']
  7463. );
  7464. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7465. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  7466. $lastPosition = null;
  7467. foreach ($arrHide as $key => $value) {
  7468. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7469. $lastPosition = $key;
  7470. }
  7471. if (!empty($s_selected_position)) {
  7472. $position->setSelected($s_selected_position);
  7473. }
  7474. // When new chapter add at the end
  7475. if ($action === 'add_item') {
  7476. $position->setSelected($lastPosition);
  7477. }
  7478. if (is_array($arrLP)) {
  7479. reset($arrLP);
  7480. }
  7481. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  7482. //fix in order to use the tab
  7483. if ($item_type === 'dir') {
  7484. $form->addElement('hidden', 'type', 'dir');
  7485. }
  7486. $extension = null;
  7487. if (!empty($item_path)) {
  7488. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7489. }
  7490. //assets can't be modified
  7491. //$item_type == 'asset' ||
  7492. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7493. if ($item_type == 'sco') {
  7494. $form->addElement(
  7495. 'html',
  7496. '<script>alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  7497. );
  7498. }
  7499. $renderer = $form->defaultRenderer();
  7500. $renderer->setElementTemplate(
  7501. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7502. 'content_lp'
  7503. );
  7504. $relative_prefix = '';
  7505. $editor_config = [
  7506. 'ToolbarSet' => 'LearningPathDocuments',
  7507. 'Width' => '100%',
  7508. 'Height' => '500',
  7509. 'FullPage' => true,
  7510. 'CreateDocumentDir' => $relative_prefix,
  7511. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7512. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck,
  7513. ];
  7514. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7515. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7516. $defaults['content_lp'] = file_get_contents($content_path);
  7517. }
  7518. if (!empty($id)) {
  7519. $form->addHidden('id', $id);
  7520. }
  7521. $form->addElement('hidden', 'type', $item_type);
  7522. $form->addElement('hidden', 'post_time', time());
  7523. $form->setDefaults($defaults);
  7524. return $form->returnForm();
  7525. }
  7526. /**
  7527. * @return string
  7528. */
  7529. public function getCurrentBuildingModeURL()
  7530. {
  7531. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7532. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7533. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7534. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7535. $currentUrl = api_get_self().'?'.api_get_cidreq().
  7536. '&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7537. return $currentUrl;
  7538. }
  7539. /**
  7540. * Returns the form to update or create a document.
  7541. *
  7542. * @param string $action (add/edit)
  7543. * @param int $id ID of the lp_item (if already exists)
  7544. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7545. *
  7546. * @throws Exception
  7547. * @throws HTML_QuickForm_Error
  7548. *
  7549. * @return string HTML form
  7550. */
  7551. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7552. {
  7553. $course_id = api_get_course_int_id();
  7554. $_course = api_get_course_info();
  7555. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7556. $no_display_edit_textarea = false;
  7557. $item_description = '';
  7558. //If action==edit document
  7559. //We don't display the document form if it's not an editable document (html or txt file)
  7560. if ($action === 'edit') {
  7561. if (is_array($extra_info)) {
  7562. $path_parts = pathinfo($extra_info['dir']);
  7563. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7564. $no_display_edit_textarea = true;
  7565. }
  7566. }
  7567. }
  7568. $no_display_add = false;
  7569. // If action==add an existing document
  7570. // We don't display the document form if it's not an editable document (html or txt file).
  7571. if ($action === 'add') {
  7572. if (is_numeric($extra_info)) {
  7573. $extra_info = (int) $extra_info;
  7574. $sql_doc = "SELECT path FROM $tbl_doc
  7575. WHERE c_id = $course_id AND iid = ".$extra_info;
  7576. $result = Database::query($sql_doc);
  7577. $path_file = Database::result($result, 0, 0);
  7578. $path_parts = pathinfo($path_file);
  7579. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7580. $no_display_add = true;
  7581. }
  7582. }
  7583. }
  7584. $item_title = '';
  7585. $item_description = '';
  7586. if ($id != 0 && is_array($extra_info)) {
  7587. $item_title = stripslashes($extra_info['title']);
  7588. $item_description = stripslashes($extra_info['description']);
  7589. if (empty($item_title)) {
  7590. $path_parts = pathinfo($extra_info['path']);
  7591. $item_title = stripslashes($path_parts['filename']);
  7592. }
  7593. } elseif (is_numeric($extra_info)) {
  7594. $sql = "SELECT path, title FROM $tbl_doc
  7595. WHERE
  7596. c_id = ".$course_id." AND
  7597. iid = ".intval($extra_info);
  7598. $result = Database::query($sql);
  7599. $row = Database::fetch_array($result);
  7600. $item_title = $row['title'];
  7601. $item_title = str_replace('_', ' ', $item_title);
  7602. if (empty($item_title)) {
  7603. $path_parts = pathinfo($row['path']);
  7604. $item_title = stripslashes($path_parts['filename']);
  7605. }
  7606. }
  7607. $return = '<legend>';
  7608. $parent = 0;
  7609. if ($id != 0 && is_array($extra_info)) {
  7610. $parent = $extra_info['parent_item_id'];
  7611. }
  7612. $arrLP = $this->getItemsForForm();
  7613. $this->tree_array($arrLP);
  7614. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  7615. unset($this->arrMenu);
  7616. if ($action == 'add') {
  7617. $return .= get_lang('CreateTheDocument');
  7618. } elseif ($action == 'move') {
  7619. $return .= get_lang('MoveTheCurrentDocument');
  7620. } else {
  7621. $return .= get_lang('EditTheCurrentDocument');
  7622. }
  7623. $return .= '</legend>';
  7624. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7625. $return .= Display::return_message(
  7626. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7627. false
  7628. );
  7629. }
  7630. $form = new FormValidator(
  7631. 'form',
  7632. 'POST',
  7633. $this->getCurrentBuildingModeURL(),
  7634. '',
  7635. ['enctype' => 'multipart/form-data']
  7636. );
  7637. $defaults['title'] = Security::remove_XSS($item_title);
  7638. if (empty($item_title)) {
  7639. $defaults['title'] = Security::remove_XSS($item_title);
  7640. }
  7641. $defaults['description'] = $item_description;
  7642. $form->addElement('html', $return);
  7643. if ($action != 'move') {
  7644. $data = $this->generate_lp_folder($_course);
  7645. if ($action != 'edit') {
  7646. $folders = DocumentManager::get_all_document_folders(
  7647. $_course,
  7648. 0,
  7649. true
  7650. );
  7651. DocumentManager::build_directory_selector(
  7652. $folders,
  7653. '',
  7654. [],
  7655. true,
  7656. $form,
  7657. 'directory_parent_id'
  7658. );
  7659. }
  7660. if (isset($data['id'])) {
  7661. $defaults['directory_parent_id'] = $data['id'];
  7662. }
  7663. $this->setItemTitle($form);
  7664. }
  7665. $arrHide[0]['value'] = $this->name;
  7666. $arrHide[0]['padding'] = 20;
  7667. for ($i = 0; $i < count($arrLP); $i++) {
  7668. if ($action != 'add') {
  7669. if ($arrLP[$i]['item_type'] == 'dir' &&
  7670. !in_array($arrLP[$i]['id'], $arrHide) &&
  7671. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7672. ) {
  7673. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7674. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7675. }
  7676. } else {
  7677. if ($arrLP[$i]['item_type'] == 'dir') {
  7678. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7679. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7680. }
  7681. }
  7682. }
  7683. $parentSelect = $form->addSelect(
  7684. 'parent',
  7685. get_lang('Parent'),
  7686. [],
  7687. [
  7688. 'id' => 'idParent',
  7689. 'onchange' => 'javascript: load_cbo(this.value);',
  7690. ]
  7691. );
  7692. $my_count = 0;
  7693. foreach ($arrHide as $key => $value) {
  7694. if ($my_count != 0) {
  7695. // The LP name is also the first section and is not in the same charset like the other sections.
  7696. $value['value'] = Security::remove_XSS($value['value']);
  7697. $parentSelect->addOption(
  7698. $value['value'],
  7699. $key,
  7700. 'style="padding-left:'.$value['padding'].'px;"'
  7701. );
  7702. } else {
  7703. $value['value'] = Security::remove_XSS($value['value']);
  7704. $parentSelect->addOption(
  7705. $value['value'],
  7706. $key,
  7707. 'style="padding-left:'.$value['padding'].'px;"'
  7708. );
  7709. }
  7710. $my_count++;
  7711. }
  7712. if (!empty($id)) {
  7713. $parentSelect->setSelected($parent);
  7714. } else {
  7715. $parent_item_id = Session::read('parent_item_id', 0);
  7716. $parentSelect->setSelected($parent_item_id);
  7717. }
  7718. if (is_array($arrLP)) {
  7719. reset($arrLP);
  7720. }
  7721. $arrHide = [];
  7722. // POSITION
  7723. for ($i = 0; $i < count($arrLP); $i++) {
  7724. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7725. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7726. ) {
  7727. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7728. }
  7729. }
  7730. $selectedPosition = isset($extra_info['previous_item_id']) ? $extra_info['previous_item_id'] : 0;
  7731. $position = $form->addSelect(
  7732. 'previous',
  7733. get_lang('Position'),
  7734. [],
  7735. ['id' => 'previous']
  7736. );
  7737. $position->addOption(get_lang('FirstPosition'), 0);
  7738. foreach ($arrHide as $key => $value) {
  7739. $padding = isset($value['padding']) ? $value['padding'] : 20;
  7740. $position->addOption(
  7741. $value['value'],
  7742. $key,
  7743. 'style="padding-left:'.$padding.'px;"'
  7744. );
  7745. }
  7746. $position->setSelected($selectedPosition);
  7747. if (is_array($arrLP)) {
  7748. reset($arrLP);
  7749. }
  7750. if ($action != 'move') {
  7751. $arrHide = [];
  7752. for ($i = 0; $i < count($arrLP); $i++) {
  7753. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' &&
  7754. $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM
  7755. ) {
  7756. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7757. }
  7758. }
  7759. if (!$no_display_add) {
  7760. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7761. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7762. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT ||
  7763. $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true'
  7764. ) {
  7765. if (isset($_POST['content'])) {
  7766. $content = stripslashes($_POST['content']);
  7767. } elseif (is_array($extra_info)) {
  7768. //If it's an html document or a text file
  7769. if (!$no_display_edit_textarea) {
  7770. $content = $this->display_document(
  7771. $extra_info['path'],
  7772. false,
  7773. false
  7774. );
  7775. }
  7776. } elseif (is_numeric($extra_info)) {
  7777. $content = $this->display_document(
  7778. $extra_info,
  7779. false,
  7780. false
  7781. );
  7782. } else {
  7783. $content = '';
  7784. }
  7785. if (!$no_display_edit_textarea) {
  7786. // We need to calculate here some specific settings for the online editor.
  7787. // The calculated settings work for documents in the Documents tool
  7788. // (on the root or in subfolders).
  7789. // For documents in native scorm packages it is unclear whether the
  7790. // online editor should be activated or not.
  7791. // A new document, it is in the root of the repository.
  7792. $relative_path = '';
  7793. $relative_prefix = '';
  7794. if (is_array($extra_info) && $extra_info != 'new') {
  7795. // The document already exists. Whe have to determine its relative path towards the repository root.
  7796. $relative_path = explode('/', $extra_info['dir']);
  7797. $cnt = count($relative_path) - 2;
  7798. if ($cnt < 0) {
  7799. $cnt = 0;
  7800. }
  7801. $relative_prefix = str_repeat('../', $cnt);
  7802. $relative_path = array_slice($relative_path, 1, $cnt);
  7803. $relative_path = implode('/', $relative_path);
  7804. if (strlen($relative_path) > 0) {
  7805. $relative_path = $relative_path.'/';
  7806. }
  7807. } else {
  7808. $result = $this->generate_lp_folder($_course);
  7809. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7810. $relative_prefix = '../../';
  7811. }
  7812. $editor_config = [
  7813. 'ToolbarSet' => 'LearningPathDocuments',
  7814. 'Width' => '100%',
  7815. 'Height' => '500',
  7816. 'FullPage' => true,
  7817. 'CreateDocumentDir' => $relative_prefix,
  7818. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7819. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path,
  7820. ];
  7821. if ($_GET['action'] == 'add_item') {
  7822. $class = 'add';
  7823. $text = get_lang('LPCreateDocument');
  7824. } else {
  7825. if ($_GET['action'] == 'edit_item') {
  7826. $class = 'save';
  7827. $text = get_lang('SaveDocument');
  7828. }
  7829. }
  7830. $form->addButtonSave($text, 'submit_button');
  7831. $renderer = $form->defaultRenderer();
  7832. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7833. $form->addElement('html', '<div class="editor-lp">');
  7834. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7835. $form->addElement('html', '</div>');
  7836. $defaults['content_lp'] = $content;
  7837. }
  7838. } elseif (is_numeric($extra_info)) {
  7839. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7840. $return = $this->display_document($extra_info, true, true, true);
  7841. $form->addElement('html', $return);
  7842. }
  7843. }
  7844. }
  7845. if (isset($extra_info['item_type']) &&
  7846. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7847. ) {
  7848. $parentSelect->freeze();
  7849. $position->freeze();
  7850. }
  7851. if ($action == 'move') {
  7852. $form->addElement('hidden', 'title', $item_title);
  7853. $form->addElement('hidden', 'description', $item_description);
  7854. }
  7855. if (is_numeric($extra_info)) {
  7856. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7857. $form->addElement('hidden', 'path', $extra_info);
  7858. } elseif (is_array($extra_info)) {
  7859. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7860. $form->addElement('hidden', 'path', $extra_info['path']);
  7861. }
  7862. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7863. $form->addElement('hidden', 'post_time', time());
  7864. $form->setDefaults($defaults);
  7865. return $form->returnForm();
  7866. }
  7867. /**
  7868. * Returns the form to update or create a read-out text.
  7869. *
  7870. * @param string $action "add" or "edit"
  7871. * @param int $id ID of the lp_item (if already exists)
  7872. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7873. *
  7874. * @throws Exception
  7875. * @throws HTML_QuickForm_Error
  7876. *
  7877. * @return string HTML form
  7878. */
  7879. public function displayFrmReadOutText($action = 'add', $id = 0, $extra_info = 'new')
  7880. {
  7881. $course_id = api_get_course_int_id();
  7882. $_course = api_get_course_info();
  7883. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7884. $no_display_edit_textarea = false;
  7885. $item_description = '';
  7886. //If action==edit document
  7887. //We don't display the document form if it's not an editable document (html or txt file)
  7888. if ($action == 'edit') {
  7889. if (is_array($extra_info)) {
  7890. $path_parts = pathinfo($extra_info['dir']);
  7891. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7892. $no_display_edit_textarea = true;
  7893. }
  7894. }
  7895. }
  7896. $no_display_add = false;
  7897. $item_title = '';
  7898. $item_description = '';
  7899. if ($id != 0 && is_array($extra_info)) {
  7900. $item_title = stripslashes($extra_info['title']);
  7901. $item_description = stripslashes($extra_info['description']);
  7902. $item_terms = stripslashes($extra_info['terms']);
  7903. if (empty($item_title)) {
  7904. $path_parts = pathinfo($extra_info['path']);
  7905. $item_title = stripslashes($path_parts['filename']);
  7906. }
  7907. } elseif (is_numeric($extra_info)) {
  7908. $sql = "SELECT path, title FROM $tbl_doc WHERE c_id = ".$course_id." AND iid = ".intval($extra_info);
  7909. $result = Database::query($sql);
  7910. $row = Database::fetch_array($result);
  7911. $item_title = $row['title'];
  7912. $item_title = str_replace('_', ' ', $item_title);
  7913. if (empty($item_title)) {
  7914. $path_parts = pathinfo($row['path']);
  7915. $item_title = stripslashes($path_parts['filename']);
  7916. }
  7917. }
  7918. $parent = 0;
  7919. if ($id != 0 && is_array($extra_info)) {
  7920. $parent = $extra_info['parent_item_id'];
  7921. }
  7922. $arrLP = $this->getItemsForForm();
  7923. $this->tree_array($arrLP);
  7924. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  7925. unset($this->arrMenu);
  7926. if ($action === 'add') {
  7927. $formHeader = get_lang('CreateTheDocument');
  7928. } else {
  7929. $formHeader = get_lang('EditTheCurrentDocument');
  7930. }
  7931. if ('edit' === $action) {
  7932. $urlAudioIcon = Display::url(
  7933. Display::return_icon('audio.png', get_lang('CreateReadOutText'), [], ICON_SIZE_TINY),
  7934. api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&lp_id='.$this->lp_id.'&'
  7935. .http_build_query(['view' => 'build', 'id' => $id, 'action' => 'add_audio'])
  7936. );
  7937. } else {
  7938. $urlAudioIcon = Display::return_icon('audio.png', get_lang('CreateReadOutText'), [], ICON_SIZE_TINY);
  7939. }
  7940. $form = new FormValidator(
  7941. 'frm_add_reading',
  7942. 'POST',
  7943. $this->getCurrentBuildingModeURL(),
  7944. '',
  7945. ['enctype' => 'multipart/form-data']
  7946. );
  7947. $form->addHeader($formHeader);
  7948. $form->addHtml(
  7949. Display::return_message(
  7950. sprintf(get_lang('FrmReadOutTextIntro'), $urlAudioIcon),
  7951. 'normal',
  7952. false
  7953. )
  7954. );
  7955. $defaults['title'] = !empty($item_title) ? Security::remove_XSS($item_title) : '';
  7956. $defaults['description'] = $item_description;
  7957. $data = $this->generate_lp_folder($_course);
  7958. if ($action != 'edit') {
  7959. $folders = DocumentManager::get_all_document_folders($_course, 0, true);
  7960. DocumentManager::build_directory_selector(
  7961. $folders,
  7962. '',
  7963. [],
  7964. true,
  7965. $form,
  7966. 'directory_parent_id'
  7967. );
  7968. }
  7969. if (isset($data['id'])) {
  7970. $defaults['directory_parent_id'] = $data['id'];
  7971. }
  7972. $this->setItemTitle($form);
  7973. $arrHide[0]['value'] = $this->name;
  7974. $arrHide[0]['padding'] = 20;
  7975. for ($i = 0; $i < count($arrLP); $i++) {
  7976. if ($action != 'add') {
  7977. if ($arrLP[$i]['item_type'] == 'dir' &&
  7978. !in_array($arrLP[$i]['id'], $arrHide) &&
  7979. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7980. ) {
  7981. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7982. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7983. }
  7984. } else {
  7985. if ($arrLP[$i]['item_type'] == 'dir') {
  7986. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7987. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7988. }
  7989. }
  7990. }
  7991. $parent_select = $form->addSelect(
  7992. 'parent',
  7993. get_lang('Parent'),
  7994. [],
  7995. ['onchange' => "javascript: load_cbo(this.value, 'frm_add_reading_previous');"]
  7996. );
  7997. $my_count = 0;
  7998. foreach ($arrHide as $key => $value) {
  7999. if ($my_count != 0) {
  8000. // The LP name is also the first section and is not in the same charset like the other sections.
  8001. $value['value'] = Security::remove_XSS($value['value']);
  8002. $parent_select->addOption(
  8003. $value['value'],
  8004. $key,
  8005. 'style="padding-left:'.$value['padding'].'px;"'
  8006. );
  8007. } else {
  8008. $value['value'] = Security::remove_XSS($value['value']);
  8009. $parent_select->addOption(
  8010. $value['value'],
  8011. $key,
  8012. 'style="padding-left:'.$value['padding'].'px;"'
  8013. );
  8014. }
  8015. $my_count++;
  8016. }
  8017. if (!empty($id)) {
  8018. $parent_select->setSelected($parent);
  8019. } else {
  8020. $parent_item_id = Session::read('parent_item_id', 0);
  8021. $parent_select->setSelected($parent_item_id);
  8022. }
  8023. if (is_array($arrLP)) {
  8024. reset($arrLP);
  8025. }
  8026. $arrHide = [];
  8027. $s_selected_position = null;
  8028. // POSITION
  8029. $lastPosition = null;
  8030. for ($i = 0; $i < count($arrLP); $i++) {
  8031. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) &&
  8032. $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM
  8033. ) {
  8034. if ((isset($extra_info['previous_item_id']) &&
  8035. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  8036. ) {
  8037. $s_selected_position = $arrLP[$i]['id'];
  8038. }
  8039. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  8040. }
  8041. $lastPosition = $arrLP[$i]['id'];
  8042. }
  8043. if (empty($s_selected_position)) {
  8044. $s_selected_position = $lastPosition;
  8045. }
  8046. $position = $form->addSelect(
  8047. 'previous',
  8048. get_lang('Position'),
  8049. []
  8050. );
  8051. $position->addOption(get_lang('FirstPosition'), 0);
  8052. foreach ($arrHide as $key => $value) {
  8053. $padding = isset($value['padding']) ? $value['padding'] : 20;
  8054. $position->addOption(
  8055. $value['value'],
  8056. $key,
  8057. 'style="padding-left:'.$padding.'px;"'
  8058. );
  8059. }
  8060. $position->setSelected($s_selected_position);
  8061. if (is_array($arrLP)) {
  8062. reset($arrLP);
  8063. }
  8064. $arrHide = [];
  8065. for ($i = 0; $i < count($arrLP); $i++) {
  8066. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' &&
  8067. $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM
  8068. ) {
  8069. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  8070. }
  8071. }
  8072. if (!$no_display_add) {
  8073. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  8074. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  8075. if ($extra_info == 'new' || $item_type == TOOL_READOUT_TEXT || $edit == 'true') {
  8076. if (!$no_display_edit_textarea) {
  8077. $content = '';
  8078. if (isset($_POST['content'])) {
  8079. $content = stripslashes($_POST['content']);
  8080. } elseif (is_array($extra_info)) {
  8081. $content = $this->display_document($extra_info['path'], false, false);
  8082. } elseif (is_numeric($extra_info)) {
  8083. $content = $this->display_document($extra_info, false, false);
  8084. }
  8085. // A new document, it is in the root of the repository.
  8086. if (is_array($extra_info) && $extra_info != 'new') {
  8087. } else {
  8088. $this->generate_lp_folder($_course);
  8089. }
  8090. if ($_GET['action'] == 'add_item') {
  8091. $text = get_lang('LPCreateDocument');
  8092. } else {
  8093. $text = get_lang('SaveDocument');
  8094. }
  8095. $form->addTextarea('content_lp', get_lang('Content'), ['rows' => 20]);
  8096. $form
  8097. ->defaultRenderer()
  8098. ->setElementTemplate($form->getDefaultElementTemplate(), 'content_lp');
  8099. $form->addButtonSave($text, 'submit_button');
  8100. $defaults['content_lp'] = $content;
  8101. }
  8102. } elseif (is_numeric($extra_info)) {
  8103. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  8104. $return = $this->display_document($extra_info, true, true, true);
  8105. $form->addElement('html', $return);
  8106. }
  8107. }
  8108. if (is_numeric($extra_info)) {
  8109. $form->addElement('hidden', 'path', $extra_info);
  8110. } elseif (is_array($extra_info)) {
  8111. $form->addElement('hidden', 'path', $extra_info['path']);
  8112. }
  8113. $form->addElement('hidden', 'type', TOOL_READOUT_TEXT);
  8114. $form->addElement('hidden', 'post_time', time());
  8115. $form->setDefaults($defaults);
  8116. return $form->returnForm();
  8117. }
  8118. /**
  8119. * @param array $courseInfo
  8120. * @param string $content
  8121. * @param string $title
  8122. * @param int $parentId
  8123. *
  8124. * @throws \Doctrine\ORM\ORMException
  8125. * @throws \Doctrine\ORM\OptimisticLockException
  8126. * @throws \Doctrine\ORM\TransactionRequiredException
  8127. *
  8128. * @return int
  8129. */
  8130. public function createReadOutText($courseInfo, $content = '', $title = '', $parentId = 0)
  8131. {
  8132. $creatorId = api_get_user_id();
  8133. $sessionId = api_get_session_id();
  8134. // Generates folder
  8135. $result = $this->generate_lp_folder($courseInfo);
  8136. $dir = $result['dir'];
  8137. if (empty($parentId) || $parentId == '/') {
  8138. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  8139. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  8140. if ($parentId === '/') {
  8141. $dir = '/';
  8142. }
  8143. // Please, do not modify this dirname formatting.
  8144. if (strstr($dir, '..')) {
  8145. $dir = '/';
  8146. }
  8147. if (!empty($dir[0]) && $dir[0] == '.') {
  8148. $dir = substr($dir, 1);
  8149. }
  8150. if (!empty($dir[0]) && $dir[0] != '/') {
  8151. $dir = '/'.$dir;
  8152. }
  8153. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  8154. $dir .= '/';
  8155. }
  8156. } else {
  8157. $parentInfo = DocumentManager::get_document_data_by_id(
  8158. $parentId,
  8159. $courseInfo['code']
  8160. );
  8161. if (!empty($parentInfo)) {
  8162. $dir = $parentInfo['path'].'/';
  8163. }
  8164. }
  8165. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  8166. if (!is_dir($filepath)) {
  8167. $dir = '/';
  8168. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  8169. }
  8170. $originalTitle = !empty($title) ? $title : $_POST['title'];
  8171. if (!empty($title)) {
  8172. $title = api_replace_dangerous_char(stripslashes($title));
  8173. } else {
  8174. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  8175. }
  8176. $title = disable_dangerous_file($title);
  8177. $filename = $title;
  8178. $content = !empty($content) ? $content : $_POST['content_lp'];
  8179. $tmpFileName = $filename;
  8180. $i = 0;
  8181. while (file_exists($filepath.$tmpFileName.'.html')) {
  8182. $tmpFileName = $filename.'_'.++$i;
  8183. }
  8184. $filename = $tmpFileName.'.html';
  8185. $content = stripslashes($content);
  8186. if (file_exists($filepath.$filename)) {
  8187. return 0;
  8188. }
  8189. $putContent = file_put_contents($filepath.$filename, $content);
  8190. if ($putContent === false) {
  8191. return 0;
  8192. }
  8193. $fileSize = filesize($filepath.$filename);
  8194. $saveFilePath = $dir.$filename;
  8195. $documentId = add_document(
  8196. $courseInfo,
  8197. $saveFilePath,
  8198. 'file',
  8199. $fileSize,
  8200. $tmpFileName,
  8201. '',
  8202. 0, //readonly
  8203. true,
  8204. null,
  8205. $sessionId,
  8206. $creatorId
  8207. );
  8208. if (!$documentId) {
  8209. return 0;
  8210. }
  8211. api_item_property_update(
  8212. $courseInfo,
  8213. TOOL_DOCUMENT,
  8214. $documentId,
  8215. 'DocumentAdded',
  8216. $creatorId,
  8217. null,
  8218. null,
  8219. null,
  8220. null,
  8221. $sessionId
  8222. );
  8223. $newComment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  8224. $newTitle = $originalTitle;
  8225. if ($newComment || $newTitle) {
  8226. $em = Database::getManager();
  8227. /** @var CDocument $doc */
  8228. $doc = $em->find('ChamiloCourseBundle:CDocument', $documentId);
  8229. if ($newComment) {
  8230. $doc->setComment($newComment);
  8231. }
  8232. if ($newTitle) {
  8233. $doc->setTitle($newTitle);
  8234. }
  8235. $em->persist($doc);
  8236. $em->flush();
  8237. }
  8238. return $documentId;
  8239. }
  8240. /**
  8241. * Return HTML form to add/edit a link item.
  8242. *
  8243. * @param string $action (add/edit)
  8244. * @param int $id Item ID if exists
  8245. * @param mixed $extra_info
  8246. *
  8247. * @throws Exception
  8248. * @throws HTML_QuickForm_Error
  8249. *
  8250. * @return string HTML form
  8251. */
  8252. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  8253. {
  8254. $course_id = api_get_course_int_id();
  8255. $tbl_link = Database::get_course_table(TABLE_LINK);
  8256. $item_title = '';
  8257. $item_description = '';
  8258. $item_url = '';
  8259. if ($id != 0 && is_array($extra_info)) {
  8260. $item_title = stripslashes($extra_info['title']);
  8261. $item_description = stripslashes($extra_info['description']);
  8262. $item_url = stripslashes($extra_info['url']);
  8263. } elseif (is_numeric($extra_info)) {
  8264. $extra_info = (int) $extra_info;
  8265. $sql = "SELECT title, description, url
  8266. FROM $tbl_link
  8267. WHERE c_id = $course_id AND iid = $extra_info";
  8268. $result = Database::query($sql);
  8269. $row = Database::fetch_array($result);
  8270. $item_title = $row['title'];
  8271. $item_description = $row['description'];
  8272. $item_url = $row['url'];
  8273. }
  8274. $form = new FormValidator(
  8275. 'edit_link',
  8276. 'POST',
  8277. $this->getCurrentBuildingModeURL()
  8278. );
  8279. $defaults = [];
  8280. $parent = 0;
  8281. if ($id != 0 && is_array($extra_info)) {
  8282. $parent = $extra_info['parent_item_id'];
  8283. }
  8284. $arrLP = $this->getItemsForForm();
  8285. $this->tree_array($arrLP);
  8286. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  8287. unset($this->arrMenu);
  8288. if ($action == 'add') {
  8289. $legend = get_lang('CreateTheLink');
  8290. } elseif ($action == 'move') {
  8291. $legend = get_lang('MoveCurrentLink');
  8292. } else {
  8293. $legend = get_lang('EditCurrentLink');
  8294. }
  8295. $form->addHeader($legend);
  8296. if ($action != 'move') {
  8297. $this->setItemTitle($form);
  8298. $defaults['title'] = $item_title;
  8299. }
  8300. $selectParent = $form->addSelect(
  8301. 'parent',
  8302. get_lang('Parent'),
  8303. [],
  8304. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  8305. );
  8306. $selectParent->addOption($this->name, 0);
  8307. $arrHide = [
  8308. $id,
  8309. ];
  8310. $parent_item_id = Session::read('parent_item_id', 0);
  8311. for ($i = 0; $i < count($arrLP); $i++) {
  8312. if ($action != 'add') {
  8313. if (
  8314. ($arrLP[$i]['item_type'] == 'dir') &&
  8315. !in_array($arrLP[$i]['id'], $arrHide) &&
  8316. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  8317. ) {
  8318. $selectParent->addOption(
  8319. $arrLP[$i]['title'],
  8320. $arrLP[$i]['id'],
  8321. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px;']
  8322. );
  8323. if ($parent == $arrLP[$i]['id']) {
  8324. $selectParent->setSelected($arrLP[$i]['id']);
  8325. }
  8326. } else {
  8327. $arrHide[] = $arrLP[$i]['id'];
  8328. }
  8329. } else {
  8330. if ($arrLP[$i]['item_type'] == 'dir') {
  8331. $selectParent->addOption(
  8332. $arrLP[$i]['title'],
  8333. $arrLP[$i]['id'],
  8334. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  8335. );
  8336. if ($parent_item_id == $arrLP[$i]['id']) {
  8337. $selectParent->setSelected($arrLP[$i]['id']);
  8338. }
  8339. }
  8340. }
  8341. }
  8342. if (is_array($arrLP)) {
  8343. reset($arrLP);
  8344. }
  8345. $selectPrevious = $form->addSelect(
  8346. 'previous',
  8347. get_lang('Position'),
  8348. [],
  8349. ['id' => 'previous', 'class' => 'learnpath_item_form']
  8350. );
  8351. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  8352. for ($i = 0; $i < count($arrLP); $i++) {
  8353. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  8354. $selectPrevious->addOption(
  8355. $arrLP[$i]['title'],
  8356. $arrLP[$i]['id']
  8357. );
  8358. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  8359. $selectPrevious->setSelected($arrLP[$i]['id']);
  8360. } elseif ($action == 'add') {
  8361. $selectPrevious->setSelected($arrLP[$i]['id']);
  8362. }
  8363. }
  8364. }
  8365. if ($action != 'move') {
  8366. $urlAttributes = ['class' => 'learnpath_item_form'];
  8367. if (is_numeric($extra_info)) {
  8368. $urlAttributes['disabled'] = 'disabled';
  8369. }
  8370. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  8371. $defaults['url'] = $item_url;
  8372. $arrHide = [];
  8373. for ($i = 0; $i < count($arrLP); $i++) {
  8374. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  8375. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  8376. }
  8377. }
  8378. }
  8379. if ($action == 'add') {
  8380. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  8381. } else {
  8382. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  8383. }
  8384. if ($action == 'move') {
  8385. $form->addHidden('title', $item_title);
  8386. $form->addHidden('description', $item_description);
  8387. }
  8388. if (is_numeric($extra_info)) {
  8389. $form->addHidden('path', $extra_info);
  8390. } elseif (is_array($extra_info)) {
  8391. $form->addHidden('path', $extra_info['path']);
  8392. }
  8393. $form->addHidden('type', TOOL_LINK);
  8394. $form->addHidden('post_time', time());
  8395. $form->setDefaults($defaults);
  8396. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  8397. }
  8398. /**
  8399. * Return HTML form to add/edit a student publication (work).
  8400. *
  8401. * @param string $action
  8402. * @param int $id Item ID if already exists
  8403. * @param string $extra_info
  8404. *
  8405. * @throws Exception
  8406. *
  8407. * @return string HTML form
  8408. */
  8409. public function display_student_publication_form(
  8410. $action = 'add',
  8411. $id = 0,
  8412. $extra_info = ''
  8413. ) {
  8414. $course_id = api_get_course_int_id();
  8415. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  8416. $item_title = get_lang('Student_publication');
  8417. if ($id != 0 && is_array($extra_info)) {
  8418. $item_title = stripslashes($extra_info['title']);
  8419. $item_description = stripslashes($extra_info['description']);
  8420. } elseif (is_numeric($extra_info)) {
  8421. $extra_info = (int) $extra_info;
  8422. $sql = "SELECT title, description
  8423. FROM $tbl_publication
  8424. WHERE c_id = $course_id AND id = ".$extra_info;
  8425. $result = Database::query($sql);
  8426. $row = Database::fetch_array($result);
  8427. if ($row) {
  8428. $item_title = $row['title'];
  8429. }
  8430. }
  8431. $parent = 0;
  8432. if ($id != 0 && is_array($extra_info)) {
  8433. $parent = $extra_info['parent_item_id'];
  8434. }
  8435. $arrLP = $this->getItemsForForm();
  8436. $this->tree_array($arrLP);
  8437. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  8438. unset($this->arrMenu);
  8439. $form = new FormValidator('frm_student_publication', 'post', '#');
  8440. if ($action == 'add') {
  8441. $form->addHeader(get_lang('Student_publication'));
  8442. } elseif ($action == 'move') {
  8443. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  8444. } else {
  8445. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  8446. }
  8447. if ($action != 'move') {
  8448. $this->setItemTitle($form);
  8449. }
  8450. $parentSelect = $form->addSelect(
  8451. 'parent',
  8452. get_lang('Parent'),
  8453. ['0' => $this->name],
  8454. [
  8455. 'onchange' => 'javascript: load_cbo(this.value);',
  8456. 'class' => 'learnpath_item_form',
  8457. 'id' => 'idParent',
  8458. ]
  8459. );
  8460. $arrHide = [$id];
  8461. for ($i = 0; $i < count($arrLP); $i++) {
  8462. if ($action != 'add') {
  8463. if (
  8464. ($arrLP[$i]['item_type'] == 'dir') &&
  8465. !in_array($arrLP[$i]['id'], $arrHide) &&
  8466. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  8467. ) {
  8468. $parentSelect->addOption(
  8469. $arrLP[$i]['title'],
  8470. $arrLP[$i]['id'],
  8471. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  8472. );
  8473. if ($parent == $arrLP[$i]['id']) {
  8474. $parentSelect->setSelected($arrLP[$i]['id']);
  8475. }
  8476. } else {
  8477. $arrHide[] = $arrLP[$i]['id'];
  8478. }
  8479. } else {
  8480. if ($arrLP[$i]['item_type'] == 'dir') {
  8481. $parentSelect->addOption(
  8482. $arrLP[$i]['title'],
  8483. $arrLP[$i]['id'],
  8484. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  8485. );
  8486. if ($parent == $arrLP[$i]['id']) {
  8487. $parentSelect->setSelected($arrLP[$i]['id']);
  8488. }
  8489. }
  8490. }
  8491. }
  8492. if (is_array($arrLP)) {
  8493. reset($arrLP);
  8494. }
  8495. $previousSelect = $form->addSelect(
  8496. 'previous',
  8497. get_lang('Position'),
  8498. ['0' => get_lang('FirstPosition')],
  8499. ['id' => 'previous', 'class' => 'learnpath_item_form']
  8500. );
  8501. for ($i = 0; $i < count($arrLP); $i++) {
  8502. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  8503. $previousSelect->addOption(
  8504. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  8505. $arrLP[$i]['id']
  8506. );
  8507. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  8508. $previousSelect->setSelected($arrLP[$i]['id']);
  8509. } elseif ($action == 'add') {
  8510. $previousSelect->setSelected($arrLP[$i]['id']);
  8511. }
  8512. }
  8513. }
  8514. if ($action == 'add') {
  8515. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  8516. } else {
  8517. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  8518. }
  8519. if ($action == 'move') {
  8520. $form->addHidden('title', $item_title);
  8521. $form->addHidden('description', $item_description);
  8522. }
  8523. if (is_numeric($extra_info)) {
  8524. $form->addHidden('path', $extra_info);
  8525. } elseif (is_array($extra_info)) {
  8526. $form->addHidden('path', $extra_info['path']);
  8527. }
  8528. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  8529. $form->addHidden('post_time', time());
  8530. $form->setDefaults(['title' => $item_title]);
  8531. $return = '<div class="sectioncomment">';
  8532. $return .= $form->returnForm();
  8533. $return .= '</div>';
  8534. return $return;
  8535. }
  8536. /**
  8537. * Displays the menu for manipulating a step.
  8538. *
  8539. * @param id $item_id
  8540. * @param string $item_type
  8541. *
  8542. * @return string
  8543. */
  8544. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  8545. {
  8546. $_course = api_get_course_info();
  8547. $course_code = api_get_course_id();
  8548. $item_id = (int) $item_id;
  8549. $return = '<div class="actions">';
  8550. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8551. $sql = "SELECT * FROM $tbl_lp_item
  8552. WHERE iid = ".$item_id;
  8553. $result = Database::query($sql);
  8554. $row = Database::fetch_assoc($result);
  8555. $audio_player = null;
  8556. // We display an audio player if needed.
  8557. if (!empty($row['audio'])) {
  8558. $webAudioPath = '../..'.api_get_path(REL_COURSE_PATH).$_course['path'].'/document/audio/'.$row['audio'];
  8559. $audio_player .= '<div class="lp_mediaplayer" id="container">'
  8560. .'<audio src="'.$webAudioPath.'" controls>'
  8561. .'</div><br>';
  8562. }
  8563. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  8564. if ($item_type != TOOL_LP_FINAL_ITEM) {
  8565. $return .= Display::url(
  8566. Display::return_icon(
  8567. 'edit.png',
  8568. get_lang('Edit'),
  8569. [],
  8570. ICON_SIZE_SMALL
  8571. ),
  8572. $url.'&action=edit_item&path_item='.$row['path']
  8573. );
  8574. $return .= Display::url(
  8575. Display::return_icon(
  8576. 'move.png',
  8577. get_lang('Move'),
  8578. [],
  8579. ICON_SIZE_SMALL
  8580. ),
  8581. $url.'&action=move_item'
  8582. );
  8583. }
  8584. // Commented for now as prerequisites cannot be added to chapters.
  8585. if ($item_type != 'dir') {
  8586. $return .= Display::url(
  8587. Display::return_icon(
  8588. 'accept.png',
  8589. get_lang('LearnpathPrerequisites'),
  8590. [],
  8591. ICON_SIZE_SMALL
  8592. ),
  8593. $url.'&action=edit_item_prereq'
  8594. );
  8595. }
  8596. $return .= Display::url(
  8597. Display::return_icon(
  8598. 'delete.png',
  8599. get_lang('Delete'),
  8600. [],
  8601. ICON_SIZE_SMALL
  8602. ),
  8603. $url.'&action=delete_item'
  8604. );
  8605. if (in_array($item_type, [TOOL_DOCUMENT, TOOL_LP_FINAL_ITEM, TOOL_HOTPOTATOES])) {
  8606. $documentData = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  8607. if (empty($documentData)) {
  8608. // Try with iid
  8609. $table = Database::get_course_table(TABLE_DOCUMENT);
  8610. $sql = "SELECT path FROM $table
  8611. WHERE
  8612. c_id = ".api_get_course_int_id()." AND
  8613. iid = ".$row['path']." AND
  8614. path NOT LIKE '%_DELETED_%'";
  8615. $result = Database::query($sql);
  8616. $documentData = Database::fetch_array($result);
  8617. if ($documentData) {
  8618. $documentData['absolute_path_from_document'] = '/document'.$documentData['path'];
  8619. }
  8620. }
  8621. if (isset($documentData['absolute_path_from_document'])) {
  8622. $return .= get_lang('File').': '.$documentData['absolute_path_from_document'];
  8623. }
  8624. }
  8625. $return .= '</div>';
  8626. if (!empty($audio_player)) {
  8627. $return .= $audio_player;
  8628. }
  8629. return $return;
  8630. }
  8631. /**
  8632. * Creates the javascript needed for filling up the checkboxes without page reload.
  8633. *
  8634. * @return string
  8635. */
  8636. public function get_js_dropdown_array()
  8637. {
  8638. $course_id = api_get_course_int_id();
  8639. $return = 'var child_name = new Array();'."\n";
  8640. $return .= 'var child_value = new Array();'."\n\n";
  8641. $return .= 'child_name[0] = new Array();'."\n";
  8642. $return .= 'child_value[0] = new Array();'."\n\n";
  8643. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8644. $sql = "SELECT * FROM ".$tbl_lp_item."
  8645. WHERE
  8646. c_id = $course_id AND
  8647. lp_id = ".$this->lp_id." AND
  8648. parent_item_id = 0
  8649. ORDER BY display_order ASC";
  8650. $res_zero = Database::query($sql);
  8651. $i = 0;
  8652. $list = $this->getItemsForForm(true);
  8653. foreach ($list as $row_zero) {
  8654. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  8655. if ($row_zero['item_type'] == TOOL_QUIZ) {
  8656. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  8657. }
  8658. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  8659. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  8660. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['iid'].'";'."\n";
  8661. }
  8662. }
  8663. $return .= "\n";
  8664. $sql = "SELECT * FROM $tbl_lp_item
  8665. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8666. $res = Database::query($sql);
  8667. while ($row = Database::fetch_array($res)) {
  8668. $sql_parent = "SELECT * FROM ".$tbl_lp_item."
  8669. WHERE
  8670. c_id = ".$course_id." AND
  8671. parent_item_id = ".$row['iid']."
  8672. ORDER BY display_order ASC";
  8673. $res_parent = Database::query($sql_parent);
  8674. $i = 0;
  8675. $return .= 'child_name['.$row['iid'].'] = new Array();'."\n";
  8676. $return .= 'child_value['.$row['iid'].'] = new Array();'."\n\n";
  8677. while ($row_parent = Database::fetch_array($res_parent)) {
  8678. $js_var = json_encode(get_lang('After').' '.$this->cleanItemTitle($row_parent['title']));
  8679. $return .= 'child_name['.$row['iid'].']['.$i.'] = '.$js_var.' ;'."\n";
  8680. $return .= 'child_value['.$row['iid'].']['.$i++.'] = "'.$row_parent['iid'].'";'."\n";
  8681. }
  8682. $return .= "\n";
  8683. }
  8684. $return .= "
  8685. function load_cbo(id) {
  8686. if (!id) {
  8687. return false;
  8688. }
  8689. var cbo = document.getElementById('previous');
  8690. for(var i = cbo.length - 1; i > 0; i--) {
  8691. cbo.options[i] = null;
  8692. }
  8693. var k=0;
  8694. for(var i = 1; i <= child_name[id].length; i++){
  8695. var option = new Option(child_name[id][i - 1], child_value[id][i - 1]);
  8696. option.style.paddingLeft = '40px';
  8697. cbo.options[i] = option;
  8698. k = i;
  8699. }
  8700. cbo.options[k].selected = true;
  8701. $('#previous').selectpicker('refresh');
  8702. }";
  8703. return $return;
  8704. }
  8705. /**
  8706. * Display the form to allow moving an item.
  8707. *
  8708. * @param int $item_id Item ID
  8709. *
  8710. * @throws Exception
  8711. * @throws HTML_QuickForm_Error
  8712. *
  8713. * @return string HTML form
  8714. */
  8715. public function display_move_item($item_id)
  8716. {
  8717. $return = '';
  8718. if (is_numeric($item_id)) {
  8719. $item_id = (int) $item_id;
  8720. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8721. $sql = "SELECT * FROM $tbl_lp_item
  8722. WHERE iid = $item_id";
  8723. $res = Database::query($sql);
  8724. $row = Database::fetch_array($res);
  8725. switch ($row['item_type']) {
  8726. case 'dir':
  8727. case 'asset':
  8728. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8729. $return .= $this->display_item_form(
  8730. $row['item_type'],
  8731. get_lang('MoveCurrentChapter'),
  8732. 'move',
  8733. $item_id,
  8734. $row
  8735. );
  8736. break;
  8737. case TOOL_DOCUMENT:
  8738. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8739. $return .= $this->display_document_form('move', $item_id, $row);
  8740. break;
  8741. case TOOL_LINK:
  8742. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8743. $return .= $this->display_link_form('move', $item_id, $row);
  8744. break;
  8745. case TOOL_HOTPOTATOES:
  8746. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8747. $return .= $this->display_link_form('move', $item_id, $row);
  8748. break;
  8749. case TOOL_QUIZ:
  8750. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8751. $return .= $this->display_quiz_form('move', $item_id, $row);
  8752. break;
  8753. case TOOL_STUDENTPUBLICATION:
  8754. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8755. $return .= $this->display_student_publication_form('move', $item_id, $row);
  8756. break;
  8757. case TOOL_FORUM:
  8758. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8759. $return .= $this->display_forum_form('move', $item_id, $row);
  8760. break;
  8761. case TOOL_THREAD:
  8762. $return .= $this->display_manipulate($item_id, $row['item_type']);
  8763. $return .= $this->display_forum_form('move', $item_id, $row);
  8764. break;
  8765. }
  8766. }
  8767. return $return;
  8768. }
  8769. /**
  8770. * Return HTML form to allow prerequisites selection.
  8771. *
  8772. * @todo use FormValidator
  8773. *
  8774. * @param int Item ID
  8775. *
  8776. * @return string HTML form
  8777. */
  8778. public function display_item_prerequisites_form($item_id = 0)
  8779. {
  8780. $course_id = api_get_course_int_id();
  8781. $item_id = (int) $item_id;
  8782. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8783. /* Current prerequisite */
  8784. $sql = "SELECT * FROM $tbl_lp_item
  8785. WHERE iid = $item_id";
  8786. $result = Database::query($sql);
  8787. $row = Database::fetch_array($result);
  8788. $prerequisiteId = $row['prerequisite'];
  8789. $return = '<legend>';
  8790. $return .= get_lang('AddEditPrerequisites');
  8791. $return .= '</legend>';
  8792. $return .= '<form method="POST">';
  8793. $return .= '<div class="table-responsive">';
  8794. $return .= '<table class="table table-hover">';
  8795. $return .= '<thead>';
  8796. $return .= '<tr>';
  8797. $return .= '<th>'.get_lang('LearnpathPrerequisites').'</th>';
  8798. $return .= '<th width="140">'.get_lang('Minimum').'</th>';
  8799. $return .= '<th width="140">'.get_lang('Maximum').'</th>';
  8800. $return .= '</tr>';
  8801. $return .= '</thead>';
  8802. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8803. $return .= '<tbody>';
  8804. $return .= '<tr>';
  8805. $return .= '<td colspan="3">';
  8806. $return .= '<div class="radio learnpath"><label for="idNone">';
  8807. $return .= '<input checked="checked" id="idNone" name="prerequisites" type="radio" />';
  8808. $return .= get_lang('None').'</label>';
  8809. $return .= '</div>';
  8810. $return .= '</tr>';
  8811. $sql = "SELECT * FROM $tbl_lp_item
  8812. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8813. $result = Database::query($sql);
  8814. $selectedMinScore = [];
  8815. $selectedMaxScore = [];
  8816. $masteryScore = [];
  8817. while ($row = Database::fetch_array($result)) {
  8818. if ($row['iid'] == $item_id) {
  8819. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8820. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8821. }
  8822. $masteryScore[$row['iid']] = $row['mastery_score'];
  8823. }
  8824. $arrLP = $this->getItemsForForm();
  8825. $this->tree_array($arrLP);
  8826. $arrLP = isset($this->arrMenu) ? $this->arrMenu : [];
  8827. unset($this->arrMenu);
  8828. for ($i = 0; $i < count($arrLP); $i++) {
  8829. $item = $arrLP[$i];
  8830. if ($item['id'] == $item_id) {
  8831. break;
  8832. }
  8833. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8834. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']] : 0;
  8835. $masteryScoreAsMinValue = isset($masteryScore[$item['id']]) ? $masteryScore[$item['id']] : 0;
  8836. $return .= '<tr>';
  8837. $return .= '<td '.(($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8838. $return .= '<div style="margin-left:'.($item['depth'] * 20).'px;" class="radio learnpath">';
  8839. $return .= '<label for="id'.$item['id'].'">';
  8840. $return .= '<input'.(in_array($prerequisiteId, [$item['id'], $item['ref']]) ? ' checked="checked" ' : '').($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ').'id="id'.$item['id'].'" name="prerequisites" type="radio" value="'.$item['id'].'" />';
  8841. $icon_name = str_replace(' ', '', $item['item_type']);
  8842. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8843. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8844. } else {
  8845. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8846. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8847. } else {
  8848. $return .= Display::return_icon('folder_document.png');
  8849. }
  8850. }
  8851. $return .= $item['title'].'</label>';
  8852. $return .= '</div>';
  8853. $return .= '</td>';
  8854. if ($item['item_type'] == TOOL_QUIZ) {
  8855. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8856. $lpItemObj = new LpItem($course_id, $item['id']);
  8857. $exercise = new Exercise($course_id);
  8858. $exercise->read($lpItemObj->path);
  8859. $lpItemObj->max_score = $exercise->get_max_score();
  8860. $lpItemObj->update();
  8861. $item['max_score'] = $lpItemObj->max_score;
  8862. if (empty($selectedMinScoreValue) && !empty($masteryScoreAsMinValue)) {
  8863. // Backwards compatibility with 1.9.x use mastery_score as min value
  8864. $selectedMinScoreValue = $masteryScoreAsMinValue;
  8865. }
  8866. $return .= '<td>';
  8867. $return .= '<input
  8868. class="form-control"
  8869. size="4" maxlength="3"
  8870. name="min_'.$item['id'].'"
  8871. type="number"
  8872. min="0"
  8873. step="1"
  8874. max="'.$item['max_score'].'"
  8875. value="'.$selectedMinScoreValue.'"
  8876. />';
  8877. $return .= '</td>';
  8878. $return .= '<td>';
  8879. $return .= '<input
  8880. class="form-control"
  8881. size="4"
  8882. maxlength="3"
  8883. name="max_'.$item['id'].'"
  8884. type="number"
  8885. min="0"
  8886. step="1"
  8887. max="'.$item['max_score'].'"
  8888. value="'.$selectedMaxScoreValue.'"
  8889. />';
  8890. $return .= '</td>';
  8891. }
  8892. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8893. $return .= '<td>';
  8894. $return .= '<input
  8895. size="4"
  8896. maxlength="3"
  8897. name="min_'.$item['id'].'"
  8898. type="number"
  8899. min="0"
  8900. step="1"
  8901. max="'.$item['max_score'].'"
  8902. value="'.$selectedMinScoreValue.'"
  8903. />';
  8904. $return .= '</td>';
  8905. $return .= '<td>';
  8906. $return .= '<input
  8907. size="4"
  8908. maxlength="3"
  8909. name="max_'.$item['id'].'"
  8910. type="number"
  8911. min="0"
  8912. step="1"
  8913. max="'.$item['max_score'].'"
  8914. value="'.$selectedMaxScoreValue.'"
  8915. />';
  8916. $return .= '</td>';
  8917. }
  8918. $return .= '</tr>';
  8919. }
  8920. $return .= '<tr>';
  8921. $return .= '</tr>';
  8922. $return .= '</tbody>';
  8923. $return .= '</table>';
  8924. $return .= '</div>';
  8925. $return .= '<div class="form-group">';
  8926. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">'.
  8927. get_lang('ModifyPrerequisites').'</button>';
  8928. $return .= '</form>';
  8929. return $return;
  8930. }
  8931. /**
  8932. * Return HTML list to allow prerequisites selection for lp.
  8933. *
  8934. * @return string HTML form
  8935. */
  8936. public function display_lp_prerequisites_list()
  8937. {
  8938. $course_id = api_get_course_int_id();
  8939. $lp_id = $this->lp_id;
  8940. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8941. // get current prerequisite
  8942. $sql = "SELECT * FROM $tbl_lp WHERE iid = $lp_id ";
  8943. $result = Database::query($sql);
  8944. $row = Database::fetch_array($result);
  8945. $prerequisiteId = $row['prerequisite'];
  8946. $session_id = api_get_session_id();
  8947. $session_condition = api_get_session_condition($session_id, true, true);
  8948. $sql = "SELECT * FROM $tbl_lp
  8949. WHERE c_id = $course_id $session_condition
  8950. ORDER BY display_order ";
  8951. $rs = Database::query($sql);
  8952. $return = '';
  8953. $return .= '<select name="prerequisites" class="form-control">';
  8954. $return .= '<option value="0">'.get_lang('None').'</option>';
  8955. if (Database::num_rows($rs) > 0) {
  8956. while ($row = Database::fetch_array($rs)) {
  8957. if ($row['id'] == $lp_id) {
  8958. continue;
  8959. }
  8960. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $prerequisiteId) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8961. }
  8962. }
  8963. $return .= '</select>';
  8964. return $return;
  8965. }
  8966. /**
  8967. * Creates a list with all the documents in it.
  8968. *
  8969. * @param bool $showInvisibleFiles
  8970. *
  8971. * @throws Exception
  8972. * @throws HTML_QuickForm_Error
  8973. *
  8974. * @return string
  8975. */
  8976. public function get_documents($showInvisibleFiles = false)
  8977. {
  8978. $course_info = api_get_course_info();
  8979. $sessionId = api_get_session_id();
  8980. $documentTree = DocumentManager::get_document_preview(
  8981. $course_info,
  8982. $this->lp_id,
  8983. null,
  8984. $sessionId,
  8985. true,
  8986. null,
  8987. null,
  8988. $showInvisibleFiles,
  8989. true
  8990. );
  8991. $headers = [
  8992. get_lang('Files'),
  8993. get_lang('CreateTheDocument'),
  8994. get_lang('CreateReadOutText'),
  8995. get_lang('Upload'),
  8996. ];
  8997. $form = new FormValidator(
  8998. 'form_upload',
  8999. 'POST',
  9000. $this->getCurrentBuildingModeURL(),
  9001. '',
  9002. ['enctype' => 'multipart/form-data']
  9003. );
  9004. $folders = DocumentManager::get_all_document_folders(
  9005. api_get_course_info(),
  9006. 0,
  9007. true
  9008. );
  9009. $lpPathInfo = $this->generate_lp_folder(api_get_course_info());
  9010. DocumentManager::build_directory_selector(
  9011. $folders,
  9012. $lpPathInfo['id'],
  9013. [],
  9014. true,
  9015. $form,
  9016. 'directory_parent_id'
  9017. );
  9018. $group = [
  9019. $form->createElement(
  9020. 'radio',
  9021. 'if_exists',
  9022. get_lang('UplWhatIfFileExists'),
  9023. get_lang('UplDoNothing'),
  9024. 'nothing'
  9025. ),
  9026. $form->createElement(
  9027. 'radio',
  9028. 'if_exists',
  9029. null,
  9030. get_lang('UplOverwriteLong'),
  9031. 'overwrite'
  9032. ),
  9033. $form->createElement(
  9034. 'radio',
  9035. 'if_exists',
  9036. null,
  9037. get_lang('UplRenameLong'),
  9038. 'rename'
  9039. ),
  9040. ];
  9041. $form->addGroup($group, null, get_lang('UplWhatIfFileExists'));
  9042. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  9043. $defaultFileExistsOption = 'rename';
  9044. if (!empty($fileExistsOption)) {
  9045. $defaultFileExistsOption = $fileExistsOption;
  9046. }
  9047. $form->setDefaults(['if_exists' => $defaultFileExistsOption]);
  9048. // Check box options
  9049. $form->addElement(
  9050. 'checkbox',
  9051. 'unzip',
  9052. get_lang('Options'),
  9053. get_lang('Uncompress')
  9054. );
  9055. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  9056. $form->addMultipleUpload($url);
  9057. $new = $this->display_document_form('add', 0);
  9058. $frmReadOutText = $this->displayFrmReadOutText('add');
  9059. $tabs = Display::tabs(
  9060. $headers,
  9061. [$documentTree, $new, $frmReadOutText, $form->returnForm()],
  9062. 'subtab'
  9063. );
  9064. return $tabs;
  9065. }
  9066. /**
  9067. * Creates a list with all the exercises (quiz) in it.
  9068. *
  9069. * @return string
  9070. */
  9071. public function get_exercises()
  9072. {
  9073. $course_id = api_get_course_int_id();
  9074. $session_id = api_get_session_id();
  9075. $userInfo = api_get_user_info();
  9076. // New for hotpotatoes.
  9077. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  9078. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  9079. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  9080. $condition_session = api_get_session_condition($session_id, true, true);
  9081. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  9082. $activeCondition = ' active <> -1 ';
  9083. if ($setting) {
  9084. $activeCondition = ' active = 1 ';
  9085. }
  9086. $categoryCondition = '';
  9087. $categoryId = isset($_REQUEST['category_id']) ? (int) $_REQUEST['category_id'] : 0;
  9088. if (api_get_configuration_value('allow_exercise_categories') && !empty($categoryId)) {
  9089. $categoryCondition = " AND exercise_category_id = $categoryId ";
  9090. }
  9091. $keywordCondition = '';
  9092. $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
  9093. if (!empty($keyword)) {
  9094. $keyword = Database::escape_string($keyword);
  9095. $keywordCondition = " AND title LIKE '%$keyword%' ";
  9096. }
  9097. $sql_quiz = "SELECT * FROM $tbl_quiz
  9098. WHERE
  9099. c_id = $course_id AND
  9100. $activeCondition
  9101. $condition_session
  9102. $categoryCondition
  9103. $keywordCondition
  9104. ORDER BY title ASC";
  9105. $sql_hot = "SELECT * FROM $tbl_doc
  9106. WHERE
  9107. c_id = $course_id AND
  9108. path LIKE '".$uploadPath."/%/%htm%'
  9109. $condition_session
  9110. ORDER BY id ASC";
  9111. $res_quiz = Database::query($sql_quiz);
  9112. $res_hot = Database::query($sql_hot);
  9113. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action=add_item&type=step&lp_id='.$this->lp_id.'#resource_tab-2';
  9114. // Create a search-box
  9115. $form = new FormValidator('search_simple', 'get', $currentUrl);
  9116. $form->addHidden('action', 'add_item');
  9117. $form->addHidden('type', 'step');
  9118. $form->addHidden('lp_id', $this->lp_id);
  9119. $form->addHidden('lp_build_selected', '2');
  9120. $form->addCourseHiddenParams();
  9121. $form->addText(
  9122. 'keyword',
  9123. get_lang('Search'),
  9124. false,
  9125. [
  9126. 'aria-label' => get_lang('Search'),
  9127. ]
  9128. );
  9129. if (api_get_configuration_value('allow_exercise_categories')) {
  9130. $manager = new ExerciseCategoryManager();
  9131. $options = $manager->getCategoriesForSelect(api_get_course_int_id());
  9132. if (!empty($options)) {
  9133. $form->addSelect(
  9134. 'category_id',
  9135. get_lang('Category'),
  9136. $options,
  9137. ['placeholder' => get_lang('SelectAnOption')]
  9138. );
  9139. }
  9140. }
  9141. $form->addButtonSearch(get_lang('Search'));
  9142. $return = $form->returnForm();
  9143. $return .= '<ul class="lp_resource">';
  9144. $return .= '<li class="lp_resource_element">';
  9145. $return .= Display::return_icon('new_exercice.png');
  9146. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id='.$this->lp_id.'">'.
  9147. get_lang('NewExercise').'</a>';
  9148. $return .= '</li>';
  9149. $previewIcon = Display::return_icon(
  9150. 'preview_view.png',
  9151. get_lang('Preview')
  9152. );
  9153. $quizIcon = Display::return_icon('quiz.png', '', [], ICON_SIZE_TINY);
  9154. $moveIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), [], ICON_SIZE_TINY);
  9155. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  9156. // Display hotpotatoes
  9157. while ($row_hot = Database::fetch_array($res_hot)) {
  9158. $link = Display::url(
  9159. $previewIcon,
  9160. $exerciseUrl.'&file='.$row_hot['path'],
  9161. ['target' => '_blank']
  9162. );
  9163. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  9164. $return .= '<a class="moved" href="#">';
  9165. $return .= Display::return_icon(
  9166. 'move_everywhere.png',
  9167. get_lang('Move'),
  9168. [],
  9169. ICON_SIZE_TINY
  9170. );
  9171. $return .= '</a> ';
  9172. $return .= Display::return_icon('hotpotatoes_s.png');
  9173. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_HOTPOTATOES.'&file='.$row_hot['id'].'&lp_id='.$this->lp_id.'">'.
  9174. ((!empty($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])).$link.'</a>';
  9175. $return .= '</li>';
  9176. }
  9177. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  9178. while ($row_quiz = Database::fetch_array($res_quiz)) {
  9179. $title = strip_tags(
  9180. api_html_entity_decode($row_quiz['title'])
  9181. );
  9182. $visibility = api_get_item_visibility(
  9183. ['real_id' => $course_id],
  9184. TOOL_QUIZ,
  9185. $row_quiz['iid'],
  9186. $session_id
  9187. );
  9188. $link = Display::url(
  9189. $previewIcon,
  9190. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  9191. ['target' => '_blank']
  9192. );
  9193. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$title.'" >';
  9194. $return .= Display::url($moveIcon, '#', ['class' => 'moved']);
  9195. $return .= $quizIcon;
  9196. $sessionStar = api_get_session_image(
  9197. $row_quiz['session_id'],
  9198. $userInfo['status']
  9199. );
  9200. $return .= Display::url(
  9201. Security::remove_XSS(cut($title, 80)).$link.$sessionStar,
  9202. api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_QUIZ.'&file='.$row_quiz['id'].'&lp_id='.$this->lp_id,
  9203. [
  9204. 'class' => $visibility == 0 ? 'moved text-muted' : 'moved',
  9205. ]
  9206. );
  9207. $return .= '</li>';
  9208. }
  9209. $return .= '</ul>';
  9210. return $return;
  9211. }
  9212. /**
  9213. * Creates a list with all the links in it.
  9214. *
  9215. * @return string
  9216. */
  9217. public function get_links()
  9218. {
  9219. $selfUrl = api_get_self();
  9220. $courseIdReq = api_get_cidreq();
  9221. $course = api_get_course_info();
  9222. $userInfo = api_get_user_info();
  9223. $course_id = $course['real_id'];
  9224. $tbl_link = Database::get_course_table(TABLE_LINK);
  9225. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  9226. $moveEverywhereIcon = Display::return_icon(
  9227. 'move_everywhere.png',
  9228. get_lang('Move'),
  9229. [],
  9230. ICON_SIZE_TINY
  9231. );
  9232. $session_id = api_get_session_id();
  9233. $condition_session = api_get_session_condition(
  9234. $session_id,
  9235. true,
  9236. true,
  9237. 'link.session_id'
  9238. );
  9239. $sql = "SELECT
  9240. link.id as link_id,
  9241. link.title as link_title,
  9242. link.session_id as link_session_id,
  9243. link.category_id as category_id,
  9244. link_category.category_title as category_title
  9245. FROM $tbl_link as link
  9246. LEFT JOIN $linkCategoryTable as link_category
  9247. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  9248. WHERE link.c_id = $course_id $condition_session
  9249. ORDER BY link_category.category_title ASC, link.title ASC";
  9250. $result = Database::query($sql);
  9251. $categorizedLinks = [];
  9252. $categories = [];
  9253. while ($link = Database::fetch_array($result)) {
  9254. if (!$link['category_id']) {
  9255. $link['category_title'] = get_lang('Uncategorized');
  9256. }
  9257. $categories[$link['category_id']] = $link['category_title'];
  9258. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  9259. }
  9260. $linksHtmlCode =
  9261. '<script>
  9262. function toggle_tool(tool, id) {
  9263. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  9264. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  9265. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('remove.gif').'";
  9266. } else {
  9267. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  9268. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  9269. }
  9270. }
  9271. </script>
  9272. <ul class="lp_resource">
  9273. <li class="lp_resource_element">
  9274. '.Display::return_icon('linksnew.gif').'
  9275. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  9276. get_lang('LinkAdd').'
  9277. </a>
  9278. </li>';
  9279. foreach ($categorizedLinks as $categoryId => $links) {
  9280. $linkNodes = null;
  9281. foreach ($links as $key => $linkInfo) {
  9282. $title = $linkInfo['link_title'];
  9283. $linkSessionId = $linkInfo['link_session_id'];
  9284. $link = Display::url(
  9285. Display::return_icon('preview_view.png', get_lang('Preview')),
  9286. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  9287. ['target' => '_blank']
  9288. );
  9289. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  9290. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  9291. $linkNodes .=
  9292. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  9293. <a class="moved" href="#">'.
  9294. $moveEverywhereIcon.
  9295. '</a>
  9296. '.Display::return_icon('links.png', '', [], ICON_SIZE_TINY).'
  9297. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  9298. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  9299. Security::remove_XSS($title).$sessionStar.$link.
  9300. '</a>
  9301. </li>';
  9302. }
  9303. }
  9304. $linksHtmlCode .=
  9305. '<li>
  9306. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  9307. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  9308. align="absbottom" />
  9309. </a>
  9310. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  9311. </li>
  9312. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  9313. }
  9314. $linksHtmlCode .= '</ul>';
  9315. return $linksHtmlCode;
  9316. }
  9317. /**
  9318. * Creates a list with all the student publications in it.
  9319. *
  9320. * @return string
  9321. */
  9322. public function get_student_publications()
  9323. {
  9324. $return = '<ul class="lp_resource">';
  9325. $return .= '<li class="lp_resource_element">';
  9326. $return .= Display::return_icon('works_new.gif');
  9327. $return .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_STUDENTPUBLICATION.'&lp_id='.$this->lp_id.'">'.
  9328. get_lang('AddAssignmentPage').'</a>';
  9329. $return .= '</li>';
  9330. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  9331. $works = getWorkListTeacher(0, 100, null, null, null);
  9332. if (!empty($works)) {
  9333. foreach ($works as $work) {
  9334. $link = Display::url(
  9335. Display::return_icon('preview_view.png', get_lang('Preview')),
  9336. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  9337. ['target' => '_blank']
  9338. );
  9339. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  9340. $return .= '<a class="moved" href="#">';
  9341. $return .= Display::return_icon(
  9342. 'move_everywhere.png',
  9343. get_lang('Move'),
  9344. [],
  9345. ICON_SIZE_TINY
  9346. );
  9347. $return .= '</a> ';
  9348. $return .= Display::return_icon('works.png', '', [], ICON_SIZE_TINY);
  9349. $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.'">'.
  9350. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  9351. </a>';
  9352. $return .= '</li>';
  9353. }
  9354. }
  9355. $return .= '</ul>';
  9356. return $return;
  9357. }
  9358. /**
  9359. * Creates a list with all the forums in it.
  9360. *
  9361. * @return string
  9362. */
  9363. public function get_forums()
  9364. {
  9365. require_once '../forum/forumfunction.inc.php';
  9366. $forumCategories = get_forum_categories();
  9367. $forumsInNoCategory = get_forums_in_category(0);
  9368. if (!empty($forumsInNoCategory)) {
  9369. $forumCategories = array_merge(
  9370. $forumCategories,
  9371. [
  9372. [
  9373. 'cat_id' => 0,
  9374. 'session_id' => 0,
  9375. 'visibility' => 1,
  9376. 'cat_comment' => null,
  9377. ],
  9378. ]
  9379. );
  9380. }
  9381. $forumList = get_forums();
  9382. $a_forums = [];
  9383. foreach ($forumCategories as $forumCategory) {
  9384. // The forums in this category.
  9385. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  9386. if (!empty($forumsInCategory)) {
  9387. foreach ($forumList as $forum) {
  9388. if (isset($forum['forum_category']) &&
  9389. $forum['forum_category'] == $forumCategory['cat_id']
  9390. ) {
  9391. $a_forums[] = $forum;
  9392. }
  9393. }
  9394. }
  9395. }
  9396. $return = '<ul class="lp_resource">';
  9397. // First add link
  9398. $return .= '<li class="lp_resource_element">';
  9399. $return .= Display::return_icon('new_forum.png');
  9400. $return .= Display::url(
  9401. get_lang('CreateANewForum'),
  9402. api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&'.http_build_query([
  9403. 'action' => 'add',
  9404. 'content' => 'forum',
  9405. 'lp_id' => $this->lp_id,
  9406. ]),
  9407. ['title' => get_lang('CreateANewForum')]
  9408. );
  9409. $return .= '</li>';
  9410. $return .= '<script>
  9411. function toggle_forum(forum_id) {
  9412. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  9413. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  9414. document.getElementById("forum_"+forum_id+"_opener").src = "'.Display::returnIconPath('remove.gif').'";
  9415. } else {
  9416. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  9417. document.getElementById("forum_"+forum_id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  9418. }
  9419. }
  9420. </script>';
  9421. foreach ($a_forums as $forum) {
  9422. if (!empty($forum['forum_id'])) {
  9423. $link = Display::url(
  9424. Display::return_icon('preview_view.png', get_lang('Preview')),
  9425. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  9426. ['target' => '_blank']
  9427. );
  9428. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  9429. $return .= '<a class="moved" href="#">';
  9430. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), [], ICON_SIZE_TINY);
  9431. $return .= ' </a>';
  9432. $return .= Display::return_icon('forum.png', '', [], ICON_SIZE_TINY);
  9433. $return .= '<a onclick="javascript:toggle_forum('.$forum['forum_id'].');" style="cursor:hand; vertical-align:middle">
  9434. <img src="'.Display::returnIconPath('add.gif').'" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  9435. </a>
  9436. <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">'.
  9437. Security::remove_XSS($forum['forum_title']).' '.$link.'</a>';
  9438. $return .= '</li>';
  9439. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  9440. $a_threads = get_threads($forum['forum_id']);
  9441. if (is_array($a_threads)) {
  9442. foreach ($a_threads as $thread) {
  9443. $link = Display::url(
  9444. Display::return_icon('preview_view.png', get_lang('Preview')),
  9445. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  9446. ['target' => '_blank']
  9447. );
  9448. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  9449. $return .= '&nbsp;<a class="moved" href="#">';
  9450. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), [], ICON_SIZE_TINY);
  9451. $return .= ' </a>';
  9452. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), [], ICON_SIZE_TINY);
  9453. $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.'">'.
  9454. Security::remove_XSS($thread['thread_title']).' '.$link.'</a>';
  9455. $return .= '</li>';
  9456. }
  9457. }
  9458. $return .= '</div>';
  9459. }
  9460. }
  9461. $return .= '</ul>';
  9462. return $return;
  9463. }
  9464. /**
  9465. * // TODO: The output encoding should be equal to the system encoding.
  9466. *
  9467. * Exports the learning path as a SCORM package. This is the main function that
  9468. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  9469. * whole thing and returns the zip.
  9470. *
  9471. * This method needs to be called in PHP5, as it will fail with non-adequate
  9472. * XML package (like the ones for PHP4), and it is *not* a static method, so
  9473. * you need to call it on a learnpath object.
  9474. *
  9475. * @TODO The method might be redefined later on in the scorm class itself to avoid
  9476. * creating a SCORM structure if there is one already. However, if the initial SCORM
  9477. * path has been modified, it should use the generic method here below.
  9478. *
  9479. * @return string Returns the zip package string, or null if error
  9480. */
  9481. public function scormExport()
  9482. {
  9483. api_set_more_memory_and_time_limits();
  9484. $_course = api_get_course_info();
  9485. $course_id = $_course['real_id'];
  9486. // Create the zip handler (this will remain available throughout the method).
  9487. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  9488. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9489. $temp_dir_short = uniqid('scorm_export', true);
  9490. $temp_zip_dir = $archivePath.'/'.$temp_dir_short;
  9491. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  9492. $zip_folder = new PclZip($temp_zip_file);
  9493. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  9494. $root_path = $main_path = api_get_path(SYS_PATH);
  9495. $files_cleanup = [];
  9496. // Place to temporarily stash the zip file.
  9497. // create the temp dir if it doesn't exist
  9498. // or do a cleanup before creating the zip file.
  9499. if (!is_dir($temp_zip_dir)) {
  9500. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  9501. } else {
  9502. // Cleanup: Check the temp dir for old files and delete them.
  9503. $handle = opendir($temp_zip_dir);
  9504. while (false !== ($file = readdir($handle))) {
  9505. if ($file != '.' && $file != '..') {
  9506. unlink("$temp_zip_dir/$file");
  9507. }
  9508. }
  9509. closedir($handle);
  9510. }
  9511. $zip_files = $zip_files_abs = $zip_files_dist = [];
  9512. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  9513. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  9514. ) {
  9515. // Remove the possible . at the end of the path.
  9516. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  9517. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  9518. mkdir(
  9519. $dest_path_to_scorm_folder,
  9520. api_get_permissions_for_new_directories(),
  9521. true
  9522. );
  9523. copyr(
  9524. $current_course_path.'/scorm/'.$this->path,
  9525. $dest_path_to_scorm_folder,
  9526. ['imsmanifest'],
  9527. $zip_files
  9528. );
  9529. }
  9530. // Build a dummy imsmanifest structure.
  9531. // Do not add to the zip yet (we still need it).
  9532. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  9533. // Aggregation Model official document, section "2.3 Content Packaging".
  9534. // We are going to build a UTF-8 encoded manifest.
  9535. // Later we will recode it to the desired (and supported) encoding.
  9536. $xmldoc = new DOMDocument('1.0');
  9537. $root = $xmldoc->createElement('manifest');
  9538. $root->setAttribute('identifier', 'SingleCourseManifest');
  9539. $root->setAttribute('version', '1.1');
  9540. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  9541. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  9542. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  9543. $root->setAttribute(
  9544. 'xsi:schemaLocation',
  9545. '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'
  9546. );
  9547. // Build mandatory sub-root container elements.
  9548. $metadata = $xmldoc->createElement('metadata');
  9549. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  9550. $metadata->appendChild($md_schema);
  9551. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  9552. $metadata->appendChild($md_schemaversion);
  9553. $root->appendChild($metadata);
  9554. $organizations = $xmldoc->createElement('organizations');
  9555. $resources = $xmldoc->createElement('resources');
  9556. // Build the only organization we will use in building our learnpaths.
  9557. $organizations->setAttribute('default', 'chamilo_scorm_export');
  9558. $organization = $xmldoc->createElement('organization');
  9559. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  9560. // To set the title of the SCORM entity (=organization), we take the name given
  9561. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  9562. // learning path charset) as it is the encoding that defines how it is stored
  9563. // in the database. Then we convert it to HTML entities again as the "&" character
  9564. // alone is not authorized in XML (must be &amp;).
  9565. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  9566. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  9567. $organization->appendChild($org_title);
  9568. $folder_name = 'document';
  9569. // Removes the learning_path/scorm_folder path when exporting see #4841
  9570. $path_to_remove = '';
  9571. $path_to_replace = '';
  9572. $result = $this->generate_lp_folder($_course);
  9573. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  9574. $path_to_remove = 'document'.$result['dir'];
  9575. $path_to_replace = $folder_name.'/';
  9576. }
  9577. // Fixes chamilo scorm exports
  9578. if ($this->ref === 'chamilo_scorm_export') {
  9579. $path_to_remove = 'scorm/'.$this->path.'/document/';
  9580. }
  9581. // For each element, add it to the imsmanifest structure, then add it to the zip.
  9582. $link_updates = [];
  9583. $links_to_create = [];
  9584. foreach ($this->ordered_items as $index => $itemId) {
  9585. /** @var learnpathItem $item */
  9586. $item = $this->items[$itemId];
  9587. if (!in_array($item->type, [TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION])) {
  9588. // Get included documents from this item.
  9589. if ($item->type === 'sco') {
  9590. $inc_docs = $item->get_resources_from_source(
  9591. null,
  9592. $current_course_path.'/scorm/'.$this->path.'/'.$item->get_path()
  9593. );
  9594. } else {
  9595. $inc_docs = $item->get_resources_from_source();
  9596. }
  9597. // Give a child element <item> to the <organization> element.
  9598. $my_item_id = $item->get_id();
  9599. $my_item = $xmldoc->createElement('item');
  9600. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  9601. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  9602. $my_item->setAttribute('isvisible', 'true');
  9603. // Give a child element <title> to the <item> element.
  9604. $my_title = $xmldoc->createElement(
  9605. 'title',
  9606. htmlspecialchars(
  9607. api_utf8_encode($item->get_title()),
  9608. ENT_QUOTES,
  9609. 'UTF-8'
  9610. )
  9611. );
  9612. $my_item->appendChild($my_title);
  9613. // Give a child element <adlcp:prerequisites> to the <item> element.
  9614. $my_prereqs = $xmldoc->createElement(
  9615. 'adlcp:prerequisites',
  9616. $this->get_scorm_prereq_string($my_item_id)
  9617. );
  9618. $my_prereqs->setAttribute('type', 'aicc_script');
  9619. $my_item->appendChild($my_prereqs);
  9620. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  9621. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  9622. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  9623. //$xmldoc->createElement('adlcp:timelimitaction','');
  9624. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  9625. //$xmldoc->createElement('adlcp:datafromlms','');
  9626. // Give a child element <adlcp:masteryscore> to the <item> element.
  9627. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9628. $my_item->appendChild($my_masteryscore);
  9629. // Attach this item to the organization element or hits parent if there is one.
  9630. if (!empty($item->parent) && $item->parent != 0) {
  9631. $children = $organization->childNodes;
  9632. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9633. if (is_object($possible_parent)) {
  9634. $possible_parent->appendChild($my_item);
  9635. } else {
  9636. if ($this->debug > 0) {
  9637. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  9638. }
  9639. }
  9640. } else {
  9641. if ($this->debug > 0) {
  9642. error_log('No parent');
  9643. }
  9644. $organization->appendChild($my_item);
  9645. }
  9646. // Get the path of the file(s) from the course directory root.
  9647. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9648. $my_xml_file_path = $my_file_path;
  9649. if (!empty($path_to_remove)) {
  9650. // From docs
  9651. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  9652. // From quiz
  9653. if ($this->ref === 'chamilo_scorm_export') {
  9654. $path_to_remove = 'scorm/'.$this->path.'/';
  9655. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  9656. }
  9657. }
  9658. $my_sub_dir = dirname($my_file_path);
  9659. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9660. $my_xml_sub_dir = $my_sub_dir;
  9661. // Give a <resource> child to the <resources> element
  9662. $my_resource = $xmldoc->createElement('resource');
  9663. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9664. $my_resource->setAttribute('type', 'webcontent');
  9665. $my_resource->setAttribute('href', $my_xml_file_path);
  9666. // adlcp:scormtype can be either 'sco' or 'asset'.
  9667. if ($item->type === 'sco') {
  9668. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9669. } else {
  9670. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9671. }
  9672. // xml:base is the base directory to find the files declared in this resource.
  9673. $my_resource->setAttribute('xml:base', '');
  9674. // Give a <file> child to the <resource> element.
  9675. $my_file = $xmldoc->createElement('file');
  9676. $my_file->setAttribute('href', $my_xml_file_path);
  9677. $my_resource->appendChild($my_file);
  9678. // Dependency to other files - not yet supported.
  9679. $i = 1;
  9680. if ($inc_docs) {
  9681. foreach ($inc_docs as $doc_info) {
  9682. if (count($doc_info) < 1 || empty($doc_info[0])) {
  9683. continue;
  9684. }
  9685. $my_dep = $xmldoc->createElement('resource');
  9686. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9687. $my_dep->setAttribute('identifier', $res_id);
  9688. $my_dep->setAttribute('type', 'webcontent');
  9689. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9690. $my_dep_file = $xmldoc->createElement('file');
  9691. // Check type of URL.
  9692. if ($doc_info[1] == 'remote') {
  9693. // Remote file. Save url as is.
  9694. $my_dep_file->setAttribute('href', $doc_info[0]);
  9695. $my_dep->setAttribute('xml:base', '');
  9696. } elseif ($doc_info[1] === 'local') {
  9697. switch ($doc_info[2]) {
  9698. case 'url':
  9699. // Local URL - save path as url for now, don't zip file.
  9700. $abs_path = api_get_path(SYS_PATH).
  9701. str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9702. $current_dir = dirname($abs_path);
  9703. $current_dir = str_replace('\\', '/', $current_dir);
  9704. $file_path = realpath($abs_path);
  9705. $file_path = str_replace('\\', '/', $file_path);
  9706. $my_dep_file->setAttribute('href', $file_path);
  9707. $my_dep->setAttribute('xml:base', '');
  9708. if (strstr($file_path, $main_path) !== false) {
  9709. // The calculated real path is really inside Chamilo's root path.
  9710. // Reduce file path to what's under the DocumentRoot.
  9711. $replace = $file_path;
  9712. $file_path = substr($file_path, strlen($root_path) - 1);
  9713. $destinationFile = $file_path;
  9714. if (strstr($file_path, 'upload/users') !== false) {
  9715. $pos = strpos($file_path, 'my_files/');
  9716. if ($pos !== false) {
  9717. $onlyDirectory = str_replace(
  9718. 'upload/users/',
  9719. '',
  9720. substr($file_path, $pos, strlen($file_path))
  9721. );
  9722. }
  9723. $replace = $onlyDirectory;
  9724. $destinationFile = $replace;
  9725. }
  9726. $zip_files_abs[] = $file_path;
  9727. $link_updates[$my_file_path][] = [
  9728. 'orig' => $doc_info[0],
  9729. 'dest' => $destinationFile,
  9730. 'replace' => $replace,
  9731. ];
  9732. $my_dep_file->setAttribute('href', $file_path);
  9733. $my_dep->setAttribute('xml:base', '');
  9734. } elseif (empty($file_path)) {
  9735. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9736. $file_path = str_replace('//', '/', $file_path);
  9737. if (file_exists($file_path)) {
  9738. // We get the relative path.
  9739. $file_path = substr($file_path, strlen($current_dir));
  9740. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9741. $link_updates[$my_file_path][] = [
  9742. 'orig' => $doc_info[0],
  9743. 'dest' => $file_path,
  9744. ];
  9745. $my_dep_file->setAttribute('href', $file_path);
  9746. $my_dep->setAttribute('xml:base', '');
  9747. }
  9748. }
  9749. break;
  9750. case 'abs':
  9751. // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9752. $my_dep_file->setAttribute('href', $doc_info[0]);
  9753. $my_dep->setAttribute('xml:base', '');
  9754. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  9755. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  9756. $abs_img_path_without_subdir = $doc_info[0];
  9757. $relp = api_get_path(REL_PATH); // The url-append config param.
  9758. $pos = strpos($abs_img_path_without_subdir, $relp);
  9759. if ($pos === 0) {
  9760. $abs_img_path_without_subdir = trim('/'.substr($abs_img_path_without_subdir, strlen($relp)));
  9761. }
  9762. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  9763. $file_path = str_replace(['\\', '//'], '/', $file_path);
  9764. // Prepare the current directory path (until just under 'document') with a trailing slash.
  9765. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  9766. // Check if the current document is in that path.
  9767. if (strstr($file_path, $cur_path) !== false) {
  9768. $destinationFile = substr($file_path, strlen($cur_path));
  9769. $filePathNoCoursePart = substr($file_path, strlen($cur_path));
  9770. $fileToTest = $cur_path.$my_file_path;
  9771. if (!empty($path_to_remove)) {
  9772. $fileToTest = str_replace(
  9773. $path_to_remove.'/',
  9774. $path_to_replace,
  9775. $cur_path.$my_file_path
  9776. );
  9777. }
  9778. $relative_path = api_get_relative_path($fileToTest, $file_path);
  9779. // Put the current document in the zip (this array is the array
  9780. // that will manage documents already in the course folder - relative).
  9781. $zip_files[] = $filePathNoCoursePart;
  9782. // Update the links to the current document in the
  9783. // containing document (make them relative).
  9784. $link_updates[$my_file_path][] = [
  9785. 'orig' => $doc_info[0],
  9786. 'dest' => $destinationFile,
  9787. 'replace' => $relative_path,
  9788. ];
  9789. $my_dep_file->setAttribute('href', $file_path);
  9790. $my_dep->setAttribute('xml:base', '');
  9791. } elseif (strstr($file_path, $main_path) !== false) {
  9792. // The calculated real path is really inside Chamilo's root path.
  9793. // Reduce file path to what's under the DocumentRoot.
  9794. $file_path = substr($file_path, strlen($root_path));
  9795. $zip_files_abs[] = $file_path;
  9796. $link_updates[$my_file_path][] = ['orig' => $doc_info[0], 'dest' => $file_path];
  9797. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9798. $my_dep->setAttribute('xml:base', '');
  9799. } elseif (empty($file_path)) {
  9800. // Probably this is an image inside "/main" directory
  9801. $file_path = api_get_path(SYS_PATH).$abs_img_path_without_subdir;
  9802. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9803. if (file_exists($file_path)) {
  9804. if (strstr($file_path, 'main/default_course_document') !== false) {
  9805. // We get the relative path.
  9806. $pos = strpos($file_path, 'main/default_course_document/');
  9807. if ($pos !== false) {
  9808. $onlyDirectory = str_replace(
  9809. 'main/default_course_document/',
  9810. '',
  9811. substr($file_path, $pos, strlen($file_path))
  9812. );
  9813. }
  9814. $destinationFile = 'default_course_document/'.$onlyDirectory;
  9815. $fileAbs = substr($file_path, strlen(api_get_path(SYS_PATH)));
  9816. $zip_files_abs[] = $fileAbs;
  9817. $link_updates[$my_file_path][] = [
  9818. 'orig' => $doc_info[0],
  9819. 'dest' => $destinationFile,
  9820. ];
  9821. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9822. $my_dep->setAttribute('xml:base', '');
  9823. }
  9824. }
  9825. }
  9826. break;
  9827. case 'rel':
  9828. // Path relative to the current document.
  9829. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  9830. if (substr($doc_info[0], 0, 2) === '..') {
  9831. // Relative path going up.
  9832. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9833. $current_dir = str_replace('\\', '/', $current_dir);
  9834. $file_path = realpath($current_dir.$doc_info[0]);
  9835. $file_path = str_replace('\\', '/', $file_path);
  9836. if (strstr($file_path, $main_path) !== false) {
  9837. // The calculated real path is really inside Chamilo's root path.
  9838. // Reduce file path to what's under the DocumentRoot.
  9839. $file_path = substr($file_path, strlen($root_path));
  9840. $zip_files_abs[] = $file_path;
  9841. $link_updates[$my_file_path][] = ['orig' => $doc_info[0], 'dest' => $file_path];
  9842. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9843. $my_dep->setAttribute('xml:base', '');
  9844. }
  9845. } else {
  9846. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9847. $my_dep_file->setAttribute('href', $doc_info[0]);
  9848. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9849. }
  9850. break;
  9851. default:
  9852. $my_dep_file->setAttribute('href', $doc_info[0]);
  9853. $my_dep->setAttribute('xml:base', '');
  9854. break;
  9855. }
  9856. }
  9857. $my_dep->appendChild($my_dep_file);
  9858. $resources->appendChild($my_dep);
  9859. $dependency = $xmldoc->createElement('dependency');
  9860. $dependency->setAttribute('identifierref', $res_id);
  9861. $my_resource->appendChild($dependency);
  9862. $i++;
  9863. }
  9864. }
  9865. $resources->appendChild($my_resource);
  9866. $zip_files[] = $my_file_path;
  9867. } else {
  9868. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  9869. switch ($item->type) {
  9870. case TOOL_LINK:
  9871. $my_item = $xmldoc->createElement('item');
  9872. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9873. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9874. $my_item->setAttribute('isvisible', 'true');
  9875. // Give a child element <title> to the <item> element.
  9876. $my_title = $xmldoc->createElement(
  9877. 'title',
  9878. htmlspecialchars(
  9879. api_utf8_encode($item->get_title()),
  9880. ENT_QUOTES,
  9881. 'UTF-8'
  9882. )
  9883. );
  9884. $my_item->appendChild($my_title);
  9885. // Give a child element <adlcp:prerequisites> to the <item> element.
  9886. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9887. $my_prereqs->setAttribute('type', 'aicc_script');
  9888. $my_item->appendChild($my_prereqs);
  9889. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  9890. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  9891. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  9892. //$xmldoc->createElement('adlcp:timelimitaction', '');
  9893. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  9894. //$xmldoc->createElement('adlcp:datafromlms', '');
  9895. // Give a child element <adlcp:masteryscore> to the <item> element.
  9896. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9897. $my_item->appendChild($my_masteryscore);
  9898. // Attach this item to the organization element or its parent if there is one.
  9899. if (!empty($item->parent) && $item->parent != 0) {
  9900. $children = $organization->childNodes;
  9901. for ($i = 0; $i < $children->length; $i++) {
  9902. $item_temp = $children->item($i);
  9903. if ($item_temp->nodeName == 'item') {
  9904. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9905. $item_temp->appendChild($my_item);
  9906. }
  9907. }
  9908. }
  9909. } else {
  9910. $organization->appendChild($my_item);
  9911. }
  9912. $my_file_path = 'link_'.$item->get_id().'.html';
  9913. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  9914. WHERE c_id = '.$course_id.' AND id = '.$item->path;
  9915. $rs = Database::query($sql);
  9916. if ($link = Database::fetch_array($rs)) {
  9917. $url = $link['url'];
  9918. $title = stripslashes($link['title']);
  9919. $links_to_create[$my_file_path] = ['title' => $title, 'url' => $url];
  9920. $my_xml_file_path = $my_file_path;
  9921. $my_sub_dir = dirname($my_file_path);
  9922. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9923. $my_xml_sub_dir = $my_sub_dir;
  9924. // Give a <resource> child to the <resources> element.
  9925. $my_resource = $xmldoc->createElement('resource');
  9926. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9927. $my_resource->setAttribute('type', 'webcontent');
  9928. $my_resource->setAttribute('href', $my_xml_file_path);
  9929. // adlcp:scormtype can be either 'sco' or 'asset'.
  9930. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9931. // xml:base is the base directory to find the files declared in this resource.
  9932. $my_resource->setAttribute('xml:base', '');
  9933. // give a <file> child to the <resource> element.
  9934. $my_file = $xmldoc->createElement('file');
  9935. $my_file->setAttribute('href', $my_xml_file_path);
  9936. $my_resource->appendChild($my_file);
  9937. $resources->appendChild($my_resource);
  9938. }
  9939. break;
  9940. case TOOL_QUIZ:
  9941. $exe_id = $item->path;
  9942. // Should be using ref when everything will be cleaned up in this regard.
  9943. $exe = new Exercise();
  9944. $exe->read($exe_id);
  9945. $my_item = $xmldoc->createElement('item');
  9946. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9947. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9948. $my_item->setAttribute('isvisible', 'true');
  9949. // Give a child element <title> to the <item> element.
  9950. $my_title = $xmldoc->createElement(
  9951. 'title',
  9952. htmlspecialchars(
  9953. api_utf8_encode($item->get_title()),
  9954. ENT_QUOTES,
  9955. 'UTF-8'
  9956. )
  9957. );
  9958. $my_item->appendChild($my_title);
  9959. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  9960. $my_item->appendChild($my_max_score);
  9961. // Give a child element <adlcp:prerequisites> to the <item> element.
  9962. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9963. $my_prereqs->setAttribute('type', 'aicc_script');
  9964. $my_item->appendChild($my_prereqs);
  9965. // Give a child element <adlcp:masteryscore> to the <item> element.
  9966. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9967. $my_item->appendChild($my_masteryscore);
  9968. // Attach this item to the organization element or hits parent if there is one.
  9969. if (!empty($item->parent) && $item->parent != 0) {
  9970. $children = $organization->childNodes;
  9971. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9972. if ($possible_parent) {
  9973. if ($possible_parent->getAttribute('identifier') === 'ITEM_'.$item->parent) {
  9974. $possible_parent->appendChild($my_item);
  9975. }
  9976. }
  9977. } else {
  9978. $organization->appendChild($my_item);
  9979. }
  9980. // Get the path of the file(s) from the course directory root
  9981. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9982. $my_file_path = 'quiz_'.$item->get_id().'.html';
  9983. // Write the contents of the exported exercise into a (big) html file
  9984. // to later pack it into the exported SCORM. The file will be removed afterwards.
  9985. $scormExercise = new ScormExercise($exe, true);
  9986. $contents = $scormExercise->export();
  9987. $tmp_file_path = $archivePath.$temp_dir_short.'/'.$my_file_path;
  9988. $res = file_put_contents($tmp_file_path, $contents);
  9989. if ($res === false) {
  9990. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  9991. }
  9992. $files_cleanup[] = $tmp_file_path;
  9993. $my_xml_file_path = $my_file_path;
  9994. $my_sub_dir = dirname($my_file_path);
  9995. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9996. $my_xml_sub_dir = $my_sub_dir;
  9997. // Give a <resource> child to the <resources> element.
  9998. $my_resource = $xmldoc->createElement('resource');
  9999. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  10000. $my_resource->setAttribute('type', 'webcontent');
  10001. $my_resource->setAttribute('href', $my_xml_file_path);
  10002. // adlcp:scormtype can be either 'sco' or 'asset'.
  10003. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  10004. // xml:base is the base directory to find the files declared in this resource.
  10005. $my_resource->setAttribute('xml:base', '');
  10006. // Give a <file> child to the <resource> element.
  10007. $my_file = $xmldoc->createElement('file');
  10008. $my_file->setAttribute('href', $my_xml_file_path);
  10009. $my_resource->appendChild($my_file);
  10010. // Get included docs.
  10011. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  10012. // Dependency to other files - not yet supported.
  10013. $i = 1;
  10014. foreach ($inc_docs as $doc_info) {
  10015. if (count($doc_info) < 1 || empty($doc_info[0])) {
  10016. continue;
  10017. }
  10018. $my_dep = $xmldoc->createElement('resource');
  10019. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  10020. $my_dep->setAttribute('identifier', $res_id);
  10021. $my_dep->setAttribute('type', 'webcontent');
  10022. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  10023. $my_dep_file = $xmldoc->createElement('file');
  10024. // Check type of URL.
  10025. if ($doc_info[1] == 'remote') {
  10026. // Remote file. Save url as is.
  10027. $my_dep_file->setAttribute('href', $doc_info[0]);
  10028. $my_dep->setAttribute('xml:base', '');
  10029. } elseif ($doc_info[1] == 'local') {
  10030. switch ($doc_info[2]) {
  10031. case 'url': // Local URL - save path as url for now, don't zip file.
  10032. // Save file but as local file (retrieve from URL).
  10033. $abs_path = api_get_path(SYS_PATH).
  10034. str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  10035. $current_dir = dirname($abs_path);
  10036. $current_dir = str_replace('\\', '/', $current_dir);
  10037. $file_path = realpath($abs_path);
  10038. $file_path = str_replace('\\', '/', $file_path);
  10039. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10040. $my_dep->setAttribute('xml:base', '');
  10041. if (strstr($file_path, $main_path) !== false) {
  10042. // The calculated real path is really inside the chamilo root path.
  10043. // Reduce file path to what's under the DocumentRoot.
  10044. $file_path = substr($file_path, strlen($root_path));
  10045. $zip_files_abs[] = $file_path;
  10046. $link_updates[$my_file_path][] = [
  10047. 'orig' => $doc_info[0],
  10048. 'dest' => 'document/'.$file_path,
  10049. ];
  10050. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10051. $my_dep->setAttribute('xml:base', '');
  10052. } elseif (empty($file_path)) {
  10053. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  10054. $file_path = str_replace('//', '/', $file_path);
  10055. if (file_exists($file_path)) {
  10056. $file_path = substr($file_path, strlen($current_dir));
  10057. // We get the relative path.
  10058. $zip_files[] = $my_sub_dir.'/'.$file_path;
  10059. $link_updates[$my_file_path][] = [
  10060. 'orig' => $doc_info[0],
  10061. 'dest' => 'document/'.$file_path,
  10062. ];
  10063. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10064. $my_dep->setAttribute('xml:base', '');
  10065. }
  10066. }
  10067. break;
  10068. case 'abs':
  10069. // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  10070. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  10071. $current_dir = str_replace('\\', '/', $current_dir);
  10072. $file_path = realpath($doc_info[0]);
  10073. $file_path = str_replace('\\', '/', $file_path);
  10074. $my_dep_file->setAttribute('href', $file_path);
  10075. $my_dep->setAttribute('xml:base', '');
  10076. if (strstr($file_path, $main_path) !== false) {
  10077. // The calculated real path is really inside the chamilo root path.
  10078. // Reduce file path to what's under the DocumentRoot.
  10079. $file_path = substr($file_path, strlen($root_path));
  10080. $zip_files_abs[] = $file_path;
  10081. $link_updates[$my_file_path][] = [
  10082. 'orig' => $doc_info[0],
  10083. 'dest' => $file_path,
  10084. ];
  10085. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10086. $my_dep->setAttribute('xml:base', '');
  10087. } elseif (empty($file_path)) {
  10088. $docSysPartPath = str_replace(
  10089. api_get_path(REL_COURSE_PATH),
  10090. '',
  10091. $doc_info[0]
  10092. );
  10093. $docSysPartPathNoCourseCode = str_replace(
  10094. $_course['directory'].'/',
  10095. '',
  10096. $docSysPartPath
  10097. );
  10098. $docSysPath = api_get_path(SYS_COURSE_PATH).$docSysPartPath;
  10099. if (file_exists($docSysPath)) {
  10100. $file_path = $docSysPartPathNoCourseCode;
  10101. $zip_files[] = $my_sub_dir.'/'.$file_path;
  10102. $link_updates[$my_file_path][] = [
  10103. 'orig' => $doc_info[0],
  10104. 'dest' => $file_path,
  10105. ];
  10106. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10107. $my_dep->setAttribute('xml:base', '');
  10108. }
  10109. }
  10110. break;
  10111. case 'rel':
  10112. // Path relative to the current document. Save xml:base as current document's
  10113. // directory and save file in zip as subdir.file_path
  10114. if (substr($doc_info[0], 0, 2) === '..') {
  10115. // Relative path going up.
  10116. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  10117. $current_dir = str_replace('\\', '/', $current_dir);
  10118. $file_path = realpath($current_dir.$doc_info[0]);
  10119. $file_path = str_replace('\\', '/', $file_path);
  10120. if (strstr($file_path, $main_path) !== false) {
  10121. // The calculated real path is really inside Chamilo's root path.
  10122. // Reduce file path to what's under the DocumentRoot.
  10123. $file_path = substr($file_path, strlen($root_path));
  10124. $file_path_dest = $file_path;
  10125. // File path is courses/CHAMILO/document/....
  10126. $info_file_path = explode('/', $file_path);
  10127. if ($info_file_path[0] == 'courses') {
  10128. // Add character "/" in file path.
  10129. $file_path_dest = 'document/'.$file_path;
  10130. }
  10131. $zip_files_abs[] = $file_path;
  10132. $link_updates[$my_file_path][] = [
  10133. 'orig' => $doc_info[0],
  10134. 'dest' => $file_path_dest,
  10135. ];
  10136. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  10137. $my_dep->setAttribute('xml:base', '');
  10138. }
  10139. } else {
  10140. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  10141. $my_dep_file->setAttribute('href', $doc_info[0]);
  10142. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  10143. }
  10144. break;
  10145. default:
  10146. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  10147. $my_dep->setAttribute('xml:base', '');
  10148. break;
  10149. }
  10150. }
  10151. $my_dep->appendChild($my_dep_file);
  10152. $resources->appendChild($my_dep);
  10153. $dependency = $xmldoc->createElement('dependency');
  10154. $dependency->setAttribute('identifierref', $res_id);
  10155. $my_resource->appendChild($dependency);
  10156. $i++;
  10157. }
  10158. $resources->appendChild($my_resource);
  10159. $zip_files[] = $my_file_path;
  10160. break;
  10161. default:
  10162. // Get the path of the file(s) from the course directory root
  10163. $my_file_path = 'non_exportable.html';
  10164. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  10165. $my_xml_file_path = $my_file_path;
  10166. $my_sub_dir = dirname($my_file_path);
  10167. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  10168. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  10169. $my_xml_sub_dir = $my_sub_dir;
  10170. // Give a <resource> child to the <resources> element.
  10171. $my_resource = $xmldoc->createElement('resource');
  10172. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  10173. $my_resource->setAttribute('type', 'webcontent');
  10174. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  10175. // adlcp:scormtype can be either 'sco' or 'asset'.
  10176. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  10177. // xml:base is the base directory to find the files declared in this resource.
  10178. $my_resource->setAttribute('xml:base', '');
  10179. // Give a <file> child to the <resource> element.
  10180. $my_file = $xmldoc->createElement('file');
  10181. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  10182. $my_resource->appendChild($my_file);
  10183. $resources->appendChild($my_resource);
  10184. break;
  10185. }
  10186. }
  10187. }
  10188. $organizations->appendChild($organization);
  10189. $root->appendChild($organizations);
  10190. $root->appendChild($resources);
  10191. $xmldoc->appendChild($root);
  10192. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  10193. // then add the file to the zip, then destroy the file (this is done automatically).
  10194. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  10195. foreach ($zip_files as $file_path) {
  10196. if (empty($file_path)) {
  10197. continue;
  10198. }
  10199. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  10200. $dest_file = $archivePath.$temp_dir_short.'/'.$file_path;
  10201. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  10202. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  10203. }
  10204. $this->create_path($dest_file);
  10205. @copy($filePath, $dest_file);
  10206. // Check if the file needs a link update.
  10207. if (in_array($file_path, array_keys($link_updates))) {
  10208. $string = file_get_contents($dest_file);
  10209. unlink($dest_file);
  10210. foreach ($link_updates[$file_path] as $old_new) {
  10211. // This is an ugly hack that allows .flv files to be found by the flv player that
  10212. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  10213. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  10214. // ../../.. to return from inc/lib/flv_player to the document/main path.
  10215. if (substr($old_new['dest'], -3) === 'flv' &&
  10216. substr($old_new['dest'], 0, 5) === 'main/'
  10217. ) {
  10218. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  10219. } elseif (substr($old_new['dest'], -3) === 'flv' &&
  10220. substr($old_new['dest'], 0, 6) === 'video/'
  10221. ) {
  10222. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  10223. }
  10224. // Fix to avoid problems with default_course_document
  10225. if (strpos('main/default_course_document', $old_new['dest']) === false) {
  10226. $newDestination = $old_new['dest'];
  10227. if (isset($old_new['replace']) && !empty($old_new['replace'])) {
  10228. $newDestination = $old_new['replace'];
  10229. }
  10230. } else {
  10231. $newDestination = str_replace('document/', '', $old_new['dest']);
  10232. }
  10233. $string = str_replace($old_new['orig'], $newDestination, $string);
  10234. // Add files inside the HTMLs
  10235. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  10236. $destinationFile = $archivePath.$temp_dir_short.'/'.$old_new['dest'];
  10237. if (file_exists($sys_course_path.$new_path) && is_file($sys_course_path.$new_path)) {
  10238. copy(
  10239. $sys_course_path.$new_path,
  10240. $destinationFile
  10241. );
  10242. }
  10243. }
  10244. file_put_contents($dest_file, $string);
  10245. }
  10246. if (file_exists($filePath) && $copyAll) {
  10247. $extension = $this->get_extension($filePath);
  10248. if (in_array($extension, ['html', 'html'])) {
  10249. $containerOrigin = dirname($filePath);
  10250. $containerDestination = dirname($dest_file);
  10251. $finder = new Finder();
  10252. $finder->files()->in($containerOrigin)
  10253. ->notName('*_DELETED_*')
  10254. ->exclude('share_folder')
  10255. ->exclude('chat_files')
  10256. ->exclude('certificates')
  10257. ;
  10258. if (is_dir($containerOrigin) &&
  10259. is_dir($containerDestination)
  10260. ) {
  10261. $fs = new Filesystem();
  10262. $fs->mirror(
  10263. $containerOrigin,
  10264. $containerDestination,
  10265. $finder
  10266. );
  10267. }
  10268. }
  10269. }
  10270. }
  10271. foreach ($zip_files_abs as $file_path) {
  10272. if (empty($file_path)) {
  10273. continue;
  10274. }
  10275. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  10276. continue;
  10277. }
  10278. $dest_file = $archivePath.$temp_dir_short.'/document/'.$file_path;
  10279. if (strstr($file_path, 'upload/users') !== false) {
  10280. $pos = strpos($file_path, 'my_files/');
  10281. if ($pos !== false) {
  10282. $onlyDirectory = str_replace(
  10283. 'upload/users/',
  10284. '',
  10285. substr($file_path, $pos, strlen($file_path))
  10286. );
  10287. $dest_file = $archivePath.$temp_dir_short.'/document/'.$onlyDirectory;
  10288. }
  10289. }
  10290. if (strstr($file_path, 'default_course_document/') !== false) {
  10291. $replace = str_replace('/main', '', $file_path);
  10292. $dest_file = $archivePath.$temp_dir_short.'/document/'.$replace;
  10293. }
  10294. if (empty($dest_file)) {
  10295. continue;
  10296. }
  10297. $this->create_path($dest_file);
  10298. copy($main_path.$file_path, $dest_file);
  10299. // Check if the file needs a link update.
  10300. if (in_array($file_path, array_keys($link_updates))) {
  10301. $string = file_get_contents($dest_file);
  10302. unlink($dest_file);
  10303. foreach ($link_updates[$file_path] as $old_new) {
  10304. // This is an ugly hack that allows .flv files to be found by the flv player that
  10305. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  10306. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  10307. // ../../.. to return from inc/lib/flv_player to the document/main path.
  10308. if (substr($old_new['dest'], -3) == 'flv' &&
  10309. substr($old_new['dest'], 0, 5) == 'main/'
  10310. ) {
  10311. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  10312. }
  10313. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  10314. }
  10315. file_put_contents($dest_file, $string);
  10316. }
  10317. }
  10318. if (is_array($links_to_create)) {
  10319. foreach ($links_to_create as $file => $link) {
  10320. $content = '<!DOCTYPE html><head>
  10321. <meta charset="'.api_get_language_isocode().'" />
  10322. <title>'.$link['title'].'</title>
  10323. </head>
  10324. <body dir="'.api_get_text_direction().'">
  10325. <div style="text-align:center">
  10326. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  10327. </body>
  10328. </html>';
  10329. file_put_contents($archivePath.$temp_dir_short.'/'.$file, $content);
  10330. }
  10331. }
  10332. // Add non exportable message explanation.
  10333. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  10334. $file_content = '<!DOCTYPE html><head>
  10335. <meta charset="'.api_get_language_isocode().'" />
  10336. <title>'.$lang_not_exportable.'</title>
  10337. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  10338. </head>
  10339. <body dir="'.api_get_text_direction().'">';
  10340. $file_content .=
  10341. <<<EOD
  10342. <style>
  10343. .error-message {
  10344. font-family: arial, verdana, helvetica, sans-serif;
  10345. border-width: 1px;
  10346. border-style: solid;
  10347. left: 50%;
  10348. margin: 10px auto;
  10349. min-height: 30px;
  10350. padding: 5px;
  10351. right: 50%;
  10352. width: 500px;
  10353. background-color: #FFD1D1;
  10354. border-color: #FF0000;
  10355. color: #000;
  10356. }
  10357. </style>
  10358. <body>
  10359. <div class="error-message">
  10360. $lang_not_exportable
  10361. </div>
  10362. </body>
  10363. </html>
  10364. EOD;
  10365. if (!is_dir($archivePath.$temp_dir_short.'/document')) {
  10366. @mkdir($archivePath.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  10367. }
  10368. file_put_contents($archivePath.$temp_dir_short.'/document/non_exportable.html', $file_content);
  10369. // Add the extra files that go along with a SCORM package.
  10370. $main_code_path = api_get_path(SYS_CODE_PATH).'lp/packaging/';
  10371. $fs = new Filesystem();
  10372. $fs->mirror($main_code_path, $archivePath.$temp_dir_short);
  10373. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  10374. $manifest = @$xmldoc->saveXML();
  10375. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  10376. file_put_contents($archivePath.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  10377. $zip_folder->add(
  10378. $archivePath.'/'.$temp_dir_short,
  10379. PCLZIP_OPT_REMOVE_PATH,
  10380. $archivePath.'/'.$temp_dir_short.'/'
  10381. );
  10382. // Clean possible temporary files.
  10383. foreach ($files_cleanup as $file) {
  10384. $res = unlink($file);
  10385. if ($res === false) {
  10386. error_log(
  10387. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  10388. 0
  10389. );
  10390. }
  10391. }
  10392. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  10393. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  10394. }
  10395. /**
  10396. * @param int $lp_id
  10397. *
  10398. * @return bool
  10399. */
  10400. public function scorm_export_to_pdf($lp_id)
  10401. {
  10402. $lp_id = (int) $lp_id;
  10403. $files_to_export = [];
  10404. $sessionId = api_get_session_id();
  10405. $course_data = api_get_course_info($this->cc);
  10406. if (!empty($course_data)) {
  10407. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  10408. $list = self::get_flat_ordered_items_list($lp_id);
  10409. if (!empty($list)) {
  10410. foreach ($list as $item_id) {
  10411. $item = $this->items[$item_id];
  10412. switch ($item->type) {
  10413. case 'document':
  10414. // Getting documents from a LP with chamilo documents
  10415. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  10416. // Try loading document from the base course.
  10417. if (empty($file_data) && !empty($sessionId)) {
  10418. $file_data = DocumentManager::get_document_data_by_id(
  10419. $item->path,
  10420. $this->cc,
  10421. false,
  10422. 0
  10423. );
  10424. }
  10425. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  10426. if (file_exists($file_path)) {
  10427. $files_to_export[] = [
  10428. 'title' => $item->get_title(),
  10429. 'path' => $file_path,
  10430. ];
  10431. }
  10432. break;
  10433. case 'asset': //commes from a scorm package generated by chamilo
  10434. case 'sco':
  10435. $file_path = $scorm_path.'/'.$item->path;
  10436. if (file_exists($file_path)) {
  10437. $files_to_export[] = [
  10438. 'title' => $item->get_title(),
  10439. 'path' => $file_path,
  10440. ];
  10441. }
  10442. break;
  10443. case 'dir':
  10444. $files_to_export[] = [
  10445. 'title' => $item->get_title(),
  10446. 'path' => null,
  10447. ];
  10448. break;
  10449. }
  10450. }
  10451. }
  10452. $pdf = new PDF();
  10453. $result = $pdf->html_to_pdf(
  10454. $files_to_export,
  10455. $this->name,
  10456. $this->cc,
  10457. true,
  10458. true,
  10459. true,
  10460. $this->get_name()
  10461. );
  10462. return $result;
  10463. }
  10464. return false;
  10465. }
  10466. /**
  10467. * Temp function to be moved in main_api or the best place around for this.
  10468. * Creates a file path if it doesn't exist.
  10469. *
  10470. * @param string $path
  10471. */
  10472. public function create_path($path)
  10473. {
  10474. $path_bits = explode('/', dirname($path));
  10475. // IS_WINDOWS_OS has been defined in main_api.lib.php
  10476. $path_built = IS_WINDOWS_OS ? '' : '/';
  10477. foreach ($path_bits as $bit) {
  10478. if (!empty($bit)) {
  10479. $new_path = $path_built.$bit;
  10480. if (is_dir($new_path)) {
  10481. $path_built = $new_path.'/';
  10482. } else {
  10483. mkdir($new_path, api_get_permissions_for_new_directories());
  10484. $path_built = $new_path.'/';
  10485. }
  10486. }
  10487. }
  10488. }
  10489. /**
  10490. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  10491. *
  10492. * @return bool The results of the unlink function, or false if there was no image to start with
  10493. */
  10494. public function delete_lp_image()
  10495. {
  10496. $img = $this->get_preview_image();
  10497. if ($img != '') {
  10498. $del_file = $this->get_preview_image_path(null, 'sys');
  10499. if (isset($del_file) && file_exists($del_file)) {
  10500. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  10501. if (file_exists($del_file_2)) {
  10502. unlink($del_file_2);
  10503. }
  10504. $this->set_preview_image('');
  10505. return @unlink($del_file);
  10506. }
  10507. }
  10508. return false;
  10509. }
  10510. /**
  10511. * Uploads an author image to the upload/learning_path/images directory.
  10512. *
  10513. * @param array The image array, coming from the $_FILES superglobal
  10514. *
  10515. * @return bool True on success, false on error
  10516. */
  10517. public function upload_image($image_array)
  10518. {
  10519. if (!empty($image_array['name'])) {
  10520. $upload_ok = process_uploaded_file($image_array);
  10521. $has_attachment = true;
  10522. }
  10523. if ($upload_ok && $has_attachment) {
  10524. $courseDir = api_get_course_path().'/upload/learning_path/images';
  10525. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  10526. $updir = $sys_course_path.$courseDir;
  10527. // Try to add an extension to the file if it hasn't one.
  10528. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  10529. if (filter_extension($new_file_name)) {
  10530. $file_extension = explode('.', $image_array['name']);
  10531. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  10532. $filename = uniqid('');
  10533. $new_file_name = $filename.'.'.$file_extension;
  10534. $new_path = $updir.'/'.$new_file_name;
  10535. // Resize the image.
  10536. $temp = new Image($image_array['tmp_name']);
  10537. $temp->resize(104);
  10538. $result = $temp->send_image($new_path);
  10539. // Storing the image filename.
  10540. if ($result) {
  10541. $this->set_preview_image($new_file_name);
  10542. //Resize to 64px to use on course homepage
  10543. $temp->resize(64);
  10544. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  10545. return true;
  10546. }
  10547. }
  10548. }
  10549. return false;
  10550. }
  10551. /**
  10552. * @param int $lp_id
  10553. * @param string $status
  10554. */
  10555. public function set_autolaunch($lp_id, $status)
  10556. {
  10557. $course_id = api_get_course_int_id();
  10558. $lp_id = (int) $lp_id;
  10559. $status = (int) $status;
  10560. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10561. // Setting everything to autolaunch = 0
  10562. $attributes['autolaunch'] = 0;
  10563. $where = [
  10564. 'session_id = ? AND c_id = ? ' => [
  10565. api_get_session_id(),
  10566. $course_id,
  10567. ],
  10568. ];
  10569. Database::update($lp_table, $attributes, $where);
  10570. if ($status == 1) {
  10571. //Setting my lp_id to autolaunch = 1
  10572. $attributes['autolaunch'] = 1;
  10573. $where = [
  10574. 'iid = ? AND session_id = ? AND c_id = ?' => [
  10575. $lp_id,
  10576. api_get_session_id(),
  10577. $course_id,
  10578. ],
  10579. ];
  10580. Database::update($lp_table, $attributes, $where);
  10581. }
  10582. }
  10583. /**
  10584. * Gets previous_item_id for the next element of the lp_item table.
  10585. *
  10586. * @author Isaac flores paz
  10587. *
  10588. * @return int Previous item ID
  10589. */
  10590. public function select_previous_item_id()
  10591. {
  10592. $course_id = api_get_course_int_id();
  10593. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10594. // Get the max order of the items
  10595. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  10596. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  10597. $rs_max_order = Database::query($sql);
  10598. $row_max_order = Database::fetch_object($rs_max_order);
  10599. $max_order = $row_max_order->display_order;
  10600. // Get the previous item ID
  10601. $sql = "SELECT iid as previous FROM $table_lp_item
  10602. WHERE
  10603. c_id = $course_id AND
  10604. lp_id = ".$this->lp_id." AND
  10605. display_order = '$max_order' ";
  10606. $rs_max = Database::query($sql);
  10607. $row_max = Database::fetch_object($rs_max);
  10608. // Return the previous item ID
  10609. return $row_max->previous;
  10610. }
  10611. /**
  10612. * Copies an LP.
  10613. */
  10614. public function copy()
  10615. {
  10616. // Course builder
  10617. $cb = new CourseBuilder();
  10618. //Setting tools that will be copied
  10619. $cb->set_tools_to_build(['learnpaths']);
  10620. //Setting elements that will be copied
  10621. $cb->set_tools_specific_id_list(
  10622. ['learnpaths' => [$this->lp_id]]
  10623. );
  10624. $course = $cb->build();
  10625. //Course restorer
  10626. $course_restorer = new CourseRestorer($course);
  10627. $course_restorer->set_add_text_in_items(true);
  10628. $course_restorer->set_tool_copy_settings(
  10629. ['learnpaths' => ['reset_dates' => true]]
  10630. );
  10631. $course_restorer->restore(
  10632. api_get_course_id(),
  10633. api_get_session_id(),
  10634. false,
  10635. false
  10636. );
  10637. }
  10638. /**
  10639. * Verify document size.
  10640. *
  10641. * @param string $s
  10642. *
  10643. * @return bool
  10644. */
  10645. public static function verify_document_size($s)
  10646. {
  10647. $post_max = ini_get('post_max_size');
  10648. if (substr($post_max, -1, 1) == 'M') {
  10649. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  10650. } elseif (substr($post_max, -1, 1) == 'G') {
  10651. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  10652. }
  10653. $upl_max = ini_get('upload_max_filesize');
  10654. if (substr($upl_max, -1, 1) == 'M') {
  10655. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  10656. } elseif (substr($upl_max, -1, 1) == 'G') {
  10657. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  10658. }
  10659. $documents_total_space = DocumentManager::documents_total_space();
  10660. $course_max_space = DocumentManager::get_course_quota();
  10661. $total_size = filesize($s) + $documents_total_space;
  10662. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  10663. return true;
  10664. }
  10665. return false;
  10666. }
  10667. /**
  10668. * Clear LP prerequisites.
  10669. */
  10670. public function clear_prerequisites()
  10671. {
  10672. $course_id = $this->get_course_int_id();
  10673. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10674. $lp_id = $this->get_id();
  10675. // Cleaning prerequisites
  10676. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  10677. WHERE c_id = $course_id AND lp_id = $lp_id";
  10678. Database::query($sql);
  10679. // Cleaning mastery score for exercises
  10680. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  10681. WHERE c_id = $course_id AND lp_id = $lp_id AND item_type = 'quiz'";
  10682. Database::query($sql);
  10683. }
  10684. public function set_previous_step_as_prerequisite_for_all_items()
  10685. {
  10686. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10687. $course_id = $this->get_course_int_id();
  10688. $lp_id = $this->get_id();
  10689. if (!empty($this->items)) {
  10690. $previous_item_id = null;
  10691. $previous_item_max = 0;
  10692. $previous_item_type = null;
  10693. $last_item_not_dir = null;
  10694. $last_item_not_dir_type = null;
  10695. $last_item_not_dir_max = null;
  10696. foreach ($this->ordered_items as $itemId) {
  10697. $item = $this->getItem($itemId);
  10698. // if there was a previous item... (otherwise jump to set it)
  10699. if (!empty($previous_item_id)) {
  10700. $current_item_id = $item->get_id(); //save current id
  10701. if ($item->get_type() != 'dir') {
  10702. // Current item is not a folder, so it qualifies to get a prerequisites
  10703. if ($last_item_not_dir_type == 'quiz') {
  10704. // if previous is quiz, mark its max score as default score to be achieved
  10705. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  10706. WHERE c_id = $course_id AND lp_id = $lp_id AND iid = $last_item_not_dir";
  10707. Database::query($sql);
  10708. }
  10709. // now simply update the prerequisite to set it to the last non-chapter item
  10710. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  10711. WHERE c_id = $course_id AND lp_id = $lp_id AND iid = $current_item_id";
  10712. Database::query($sql);
  10713. // record item as 'non-chapter' reference
  10714. $last_item_not_dir = $item->get_id();
  10715. $last_item_not_dir_type = $item->get_type();
  10716. $last_item_not_dir_max = $item->get_max();
  10717. }
  10718. } else {
  10719. if ($item->get_type() != 'dir') {
  10720. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  10721. $last_item_not_dir = $item->get_id();
  10722. $last_item_not_dir_type = $item->get_type();
  10723. $last_item_not_dir_max = $item->get_max();
  10724. }
  10725. }
  10726. // Saving the item as "previous item" for the next loop
  10727. $previous_item_id = $item->get_id();
  10728. $previous_item_max = $item->get_max();
  10729. $previous_item_type = $item->get_type();
  10730. }
  10731. }
  10732. }
  10733. /**
  10734. * @param array $params
  10735. *
  10736. * @throws \Doctrine\ORM\OptimisticLockException
  10737. *
  10738. * @return int
  10739. */
  10740. public static function createCategory($params)
  10741. {
  10742. $em = Database::getManager();
  10743. $item = new CLpCategory();
  10744. $item->setName($params['name']);
  10745. $item->setCId($params['c_id']);
  10746. $em->persist($item);
  10747. $em->flush();
  10748. api_item_property_update(
  10749. api_get_course_info(),
  10750. TOOL_LEARNPATH_CATEGORY,
  10751. $item->getId(),
  10752. 'visible',
  10753. api_get_user_id()
  10754. );
  10755. return $item->getId();
  10756. }
  10757. /**
  10758. * @param array $params
  10759. *
  10760. * @throws \Doctrine\ORM\ORMException
  10761. * @throws \Doctrine\ORM\OptimisticLockException
  10762. * @throws \Doctrine\ORM\TransactionRequiredException
  10763. */
  10764. public static function updateCategory($params)
  10765. {
  10766. $em = Database::getManager();
  10767. /** @var CLpCategory $item */
  10768. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  10769. if ($item) {
  10770. $item->setName($params['name']);
  10771. $em->merge($item);
  10772. $em->flush();
  10773. }
  10774. }
  10775. /**
  10776. * @param int $id
  10777. *
  10778. * @throws \Doctrine\ORM\ORMException
  10779. * @throws \Doctrine\ORM\OptimisticLockException
  10780. * @throws \Doctrine\ORM\TransactionRequiredException
  10781. */
  10782. public static function moveUpCategory($id)
  10783. {
  10784. $id = (int) $id;
  10785. $em = Database::getManager();
  10786. /** @var CLpCategory $item */
  10787. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  10788. if ($item) {
  10789. $position = $item->getPosition() - 1;
  10790. $item->setPosition($position);
  10791. $em->persist($item);
  10792. $em->flush();
  10793. }
  10794. }
  10795. /**
  10796. * @param int $id
  10797. *
  10798. * @throws \Doctrine\ORM\ORMException
  10799. * @throws \Doctrine\ORM\OptimisticLockException
  10800. * @throws \Doctrine\ORM\TransactionRequiredException
  10801. */
  10802. public static function moveDownCategory($id)
  10803. {
  10804. $id = (int) $id;
  10805. $em = Database::getManager();
  10806. /** @var CLpCategory $item */
  10807. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  10808. if ($item) {
  10809. $position = $item->getPosition() + 1;
  10810. $item->setPosition($position);
  10811. $em->persist($item);
  10812. $em->flush();
  10813. }
  10814. }
  10815. /**
  10816. * @param int $courseId
  10817. *
  10818. * @throws \Doctrine\ORM\Query\QueryException
  10819. *
  10820. * @return int|mixed
  10821. */
  10822. public static function getCountCategories($courseId)
  10823. {
  10824. if (empty($courseId)) {
  10825. return 0;
  10826. }
  10827. $em = Database::getManager();
  10828. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  10829. $query->setParameter('id', $courseId);
  10830. return $query->getSingleScalarResult();
  10831. }
  10832. /**
  10833. * @param int $courseId
  10834. *
  10835. * @return mixed
  10836. */
  10837. public static function getCategories($courseId)
  10838. {
  10839. $em = Database::getManager();
  10840. // Using doctrine extensions
  10841. /** @var SortableRepository $repo */
  10842. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  10843. $items = $repo
  10844. ->getBySortableGroupsQuery(['cId' => $courseId])
  10845. ->getResult();
  10846. return $items;
  10847. }
  10848. /**
  10849. * @param int $id
  10850. *
  10851. * @throws \Doctrine\ORM\ORMException
  10852. * @throws \Doctrine\ORM\OptimisticLockException
  10853. * @throws \Doctrine\ORM\TransactionRequiredException
  10854. *
  10855. * @return CLpCategory
  10856. */
  10857. public static function getCategory($id)
  10858. {
  10859. $id = (int) $id;
  10860. $em = Database::getManager();
  10861. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  10862. return $item;
  10863. }
  10864. /**
  10865. * @param int $courseId
  10866. *
  10867. * @return array
  10868. */
  10869. public static function getCategoryByCourse($courseId)
  10870. {
  10871. $em = Database::getManager();
  10872. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  10873. ['cId' => $courseId]
  10874. );
  10875. return $items;
  10876. }
  10877. /**
  10878. * @param int $id
  10879. *
  10880. * @throws \Doctrine\ORM\ORMException
  10881. * @throws \Doctrine\ORM\OptimisticLockException
  10882. * @throws \Doctrine\ORM\TransactionRequiredException
  10883. *
  10884. * @return mixed
  10885. */
  10886. public static function deleteCategory($id)
  10887. {
  10888. $em = Database::getManager();
  10889. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  10890. if ($item) {
  10891. $courseId = $item->getCId();
  10892. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  10893. $query->setParameter('id', $courseId);
  10894. $query->setParameter('catId', $item->getId());
  10895. $lps = $query->getResult();
  10896. // Setting category = 0.
  10897. if ($lps) {
  10898. foreach ($lps as $lpItem) {
  10899. $lpItem->setCategoryId(0);
  10900. }
  10901. }
  10902. // Removing category.
  10903. $em->remove($item);
  10904. $em->flush();
  10905. $courseInfo = api_get_course_info_by_id($courseId);
  10906. $sessionId = api_get_session_id();
  10907. // Delete link tool
  10908. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  10909. $link = 'lp/lp_controller.php?cidReq='.$courseInfo['code'].'&id_session='.$sessionId.'&gidReq=0&gradebook=0&origin=&action=view_category&id='.$id;
  10910. // Delete tools
  10911. $sql = "DELETE FROM $tbl_tool
  10912. WHERE c_id = ".$courseId." AND (link LIKE '$link%' AND image='lp_category.gif')";
  10913. Database::query($sql);
  10914. return true;
  10915. }
  10916. return false;
  10917. }
  10918. /**
  10919. * @param int $courseId
  10920. * @param bool $addSelectOption
  10921. *
  10922. * @return mixed
  10923. */
  10924. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  10925. {
  10926. $items = self::getCategoryByCourse($courseId);
  10927. $cats = [];
  10928. if ($addSelectOption) {
  10929. $cats = [get_lang('SelectACategory')];
  10930. }
  10931. if (!empty($items)) {
  10932. foreach ($items as $cat) {
  10933. $cats[$cat->getId()] = $cat->getName();
  10934. }
  10935. }
  10936. return $cats;
  10937. }
  10938. /**
  10939. * @param string $courseCode
  10940. * @param int $lpId
  10941. * @param int $user_id
  10942. *
  10943. * @return learnpath
  10944. */
  10945. public static function getLpFromSession($courseCode, $lpId, $user_id)
  10946. {
  10947. $debug = 0;
  10948. $learnPath = null;
  10949. $lpObject = Session::read('lpobject');
  10950. if ($lpObject !== null) {
  10951. $learnPath = UnserializeApi::unserialize('lp', $lpObject);
  10952. if ($debug) {
  10953. error_log('getLpFromSession: unserialize');
  10954. error_log('------getLpFromSession------');
  10955. error_log('------unserialize------');
  10956. error_log("lp_view_session_id: ".$learnPath->lp_view_session_id);
  10957. error_log("api_get_sessionid: ".api_get_session_id());
  10958. }
  10959. }
  10960. if (!is_object($learnPath)) {
  10961. $learnPath = new learnpath($courseCode, $lpId, $user_id);
  10962. if ($debug) {
  10963. error_log('------getLpFromSession------');
  10964. error_log('getLpFromSession: create new learnpath');
  10965. error_log("create new LP with $courseCode - $lpId - $user_id");
  10966. error_log("lp_view_session_id: ".$learnPath->lp_view_session_id);
  10967. error_log("api_get_sessionid: ".api_get_session_id());
  10968. }
  10969. }
  10970. return $learnPath;
  10971. }
  10972. /**
  10973. * @param int $itemId
  10974. *
  10975. * @return learnpathItem|false
  10976. */
  10977. public function getItem($itemId)
  10978. {
  10979. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  10980. return $this->items[$itemId];
  10981. }
  10982. return false;
  10983. }
  10984. /**
  10985. * @return int
  10986. */
  10987. public function getCurrentAttempt()
  10988. {
  10989. $attempt = $this->getItem($this->get_current_item_id());
  10990. if ($attempt) {
  10991. $attemptId = $attempt->get_attempt_id();
  10992. return $attemptId;
  10993. }
  10994. return 0;
  10995. }
  10996. /**
  10997. * @return int
  10998. */
  10999. public function getCategoryId()
  11000. {
  11001. return (int) $this->categoryId;
  11002. }
  11003. /**
  11004. * @param int $categoryId
  11005. *
  11006. * @return bool
  11007. */
  11008. public function setCategoryId($categoryId)
  11009. {
  11010. $this->categoryId = (int) $categoryId;
  11011. $table = Database::get_course_table(TABLE_LP_MAIN);
  11012. $lp_id = $this->get_id();
  11013. $sql = "UPDATE $table SET category_id = ".$this->categoryId."
  11014. WHERE iid = $lp_id";
  11015. Database::query($sql);
  11016. return true;
  11017. }
  11018. /**
  11019. * Get whether this is a learning path with the possibility to subscribe
  11020. * users or not.
  11021. *
  11022. * @return int
  11023. */
  11024. public function getSubscribeUsers()
  11025. {
  11026. return $this->subscribeUsers;
  11027. }
  11028. /**
  11029. * Set whether this is a learning path with the possibility to subscribe
  11030. * users or not.
  11031. *
  11032. * @param int $value (0 = false, 1 = true)
  11033. *
  11034. * @return bool
  11035. */
  11036. public function setSubscribeUsers($value)
  11037. {
  11038. $this->subscribeUsers = (int) $value;
  11039. $table = Database::get_course_table(TABLE_LP_MAIN);
  11040. $lp_id = $this->get_id();
  11041. $sql = "UPDATE $table SET subscribe_users = ".$this->subscribeUsers."
  11042. WHERE iid = $lp_id";
  11043. Database::query($sql);
  11044. return true;
  11045. }
  11046. /**
  11047. * Calculate the count of stars for a user in this LP
  11048. * This calculation is based on the following rules:
  11049. * - the student gets one star when he gets to 50% of the learning path
  11050. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  11051. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  11052. * - the student gets the final star when the score for the *last* test is >= 80%.
  11053. *
  11054. * @param int $sessionId Optional. The session ID
  11055. *
  11056. * @return int The count of stars
  11057. */
  11058. public function getCalculateStars($sessionId = 0)
  11059. {
  11060. $stars = 0;
  11061. $progress = self::getProgress(
  11062. $this->lp_id,
  11063. $this->user_id,
  11064. $this->course_int_id,
  11065. $sessionId
  11066. );
  11067. if ($progress >= 50) {
  11068. $stars++;
  11069. }
  11070. // Calculate stars chapters evaluation
  11071. $exercisesItems = $this->getExercisesItems();
  11072. if (!empty($exercisesItems)) {
  11073. $totalResult = 0;
  11074. foreach ($exercisesItems as $exerciseItem) {
  11075. $exerciseResultInfo = Event::getExerciseResultsByUser(
  11076. $this->user_id,
  11077. $exerciseItem->path,
  11078. $this->course_int_id,
  11079. $sessionId,
  11080. $this->lp_id,
  11081. $exerciseItem->db_id
  11082. );
  11083. $exerciseResultInfo = end($exerciseResultInfo);
  11084. if (!$exerciseResultInfo) {
  11085. continue;
  11086. }
  11087. if (!empty($exerciseResultInfo['exe_weighting'])) {
  11088. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  11089. } else {
  11090. $exerciseResult = 0;
  11091. }
  11092. $totalResult += $exerciseResult;
  11093. }
  11094. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  11095. if ($totalExerciseAverage >= 50) {
  11096. $stars++;
  11097. }
  11098. if ($totalExerciseAverage >= 80) {
  11099. $stars++;
  11100. }
  11101. }
  11102. // Calculate star for final evaluation
  11103. $finalEvaluationItem = $this->getFinalEvaluationItem();
  11104. if (!empty($finalEvaluationItem)) {
  11105. $evaluationResultInfo = Event::getExerciseResultsByUser(
  11106. $this->user_id,
  11107. $finalEvaluationItem->path,
  11108. $this->course_int_id,
  11109. $sessionId,
  11110. $this->lp_id,
  11111. $finalEvaluationItem->db_id
  11112. );
  11113. $evaluationResultInfo = end($evaluationResultInfo);
  11114. if ($evaluationResultInfo) {
  11115. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  11116. if ($evaluationResult >= 80) {
  11117. $stars++;
  11118. }
  11119. }
  11120. }
  11121. return $stars;
  11122. }
  11123. /**
  11124. * Get the items of exercise type.
  11125. *
  11126. * @return array The items. Otherwise return false
  11127. */
  11128. public function getExercisesItems()
  11129. {
  11130. $exercises = [];
  11131. foreach ($this->items as $item) {
  11132. if ($item->type != 'quiz') {
  11133. continue;
  11134. }
  11135. $exercises[] = $item;
  11136. }
  11137. array_pop($exercises);
  11138. return $exercises;
  11139. }
  11140. /**
  11141. * Get the item of exercise type (evaluation type).
  11142. *
  11143. * @return array The final evaluation. Otherwise return false
  11144. */
  11145. public function getFinalEvaluationItem()
  11146. {
  11147. $exercises = [];
  11148. foreach ($this->items as $item) {
  11149. if ($item->type != 'quiz') {
  11150. continue;
  11151. }
  11152. $exercises[] = $item;
  11153. }
  11154. return array_pop($exercises);
  11155. }
  11156. /**
  11157. * Calculate the total points achieved for the current user in this learning path.
  11158. *
  11159. * @param int $sessionId Optional. The session Id
  11160. *
  11161. * @return int
  11162. */
  11163. public function getCalculateScore($sessionId = 0)
  11164. {
  11165. // Calculate stars chapters evaluation
  11166. $exercisesItems = $this->getExercisesItems();
  11167. $finalEvaluationItem = $this->getFinalEvaluationItem();
  11168. $totalExercisesResult = 0;
  11169. $totalEvaluationResult = 0;
  11170. if ($exercisesItems !== false) {
  11171. foreach ($exercisesItems as $exerciseItem) {
  11172. $exerciseResultInfo = Event::getExerciseResultsByUser(
  11173. $this->user_id,
  11174. $exerciseItem->path,
  11175. $this->course_int_id,
  11176. $sessionId,
  11177. $this->lp_id,
  11178. $exerciseItem->db_id
  11179. );
  11180. $exerciseResultInfo = end($exerciseResultInfo);
  11181. if (!$exerciseResultInfo) {
  11182. continue;
  11183. }
  11184. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  11185. }
  11186. }
  11187. if (!empty($finalEvaluationItem)) {
  11188. $evaluationResultInfo = Event::getExerciseResultsByUser(
  11189. $this->user_id,
  11190. $finalEvaluationItem->path,
  11191. $this->course_int_id,
  11192. $sessionId,
  11193. $this->lp_id,
  11194. $finalEvaluationItem->db_id
  11195. );
  11196. $evaluationResultInfo = end($evaluationResultInfo);
  11197. if ($evaluationResultInfo) {
  11198. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  11199. }
  11200. }
  11201. return $totalExercisesResult + $totalEvaluationResult;
  11202. }
  11203. /**
  11204. * Check if URL is not allowed to be show in a iframe.
  11205. *
  11206. * @param string $src
  11207. *
  11208. * @return string
  11209. */
  11210. public function fixBlockedLinks($src)
  11211. {
  11212. $urlInfo = parse_url($src);
  11213. $platformProtocol = 'https';
  11214. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  11215. $platformProtocol = 'http';
  11216. }
  11217. $protocolFixApplied = false;
  11218. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  11219. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  11220. $host = isset($urlInfo['host']) ? $urlInfo['host'] : null;
  11221. if ($platformProtocol != $scheme) {
  11222. Session::write('x_frame_source', $src);
  11223. $src = 'blank.php?error=x_frames_options';
  11224. $protocolFixApplied = true;
  11225. }
  11226. if ($protocolFixApplied == false) {
  11227. if (strpos(api_get_path(WEB_PATH), $host) === false) {
  11228. // Check X-Frame-Options
  11229. $ch = curl_init();
  11230. $options = [
  11231. CURLOPT_URL => $src,
  11232. CURLOPT_RETURNTRANSFER => true,
  11233. CURLOPT_HEADER => true,
  11234. CURLOPT_FOLLOWLOCATION => true,
  11235. CURLOPT_ENCODING => "",
  11236. CURLOPT_AUTOREFERER => true,
  11237. CURLOPT_CONNECTTIMEOUT => 120,
  11238. CURLOPT_TIMEOUT => 120,
  11239. CURLOPT_MAXREDIRS => 10,
  11240. ];
  11241. $proxySettings = api_get_configuration_value('proxy_settings');
  11242. if (!empty($proxySettings) &&
  11243. isset($proxySettings['curl_setopt_array'])
  11244. ) {
  11245. $options[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY'];
  11246. $options[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT'];
  11247. }
  11248. curl_setopt_array($ch, $options);
  11249. $response = curl_exec($ch);
  11250. $httpCode = curl_getinfo($ch);
  11251. $headers = substr($response, 0, $httpCode['header_size']);
  11252. $error = false;
  11253. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  11254. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  11255. ) {
  11256. $error = true;
  11257. }
  11258. if ($error) {
  11259. Session::write('x_frame_source', $src);
  11260. $src = 'blank.php?error=x_frames_options';
  11261. }
  11262. }
  11263. }
  11264. return $src;
  11265. }
  11266. /**
  11267. * Check if this LP has a created forum in the basis course.
  11268. *
  11269. * @return bool
  11270. */
  11271. public function lpHasForum()
  11272. {
  11273. $forumTable = Database::get_course_table(TABLE_FORUM);
  11274. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  11275. $fakeFrom = "
  11276. $forumTable f
  11277. INNER JOIN $itemProperty ip
  11278. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  11279. ";
  11280. $resultData = Database::select(
  11281. 'COUNT(f.iid) AS qty',
  11282. $fakeFrom,
  11283. [
  11284. 'where' => [
  11285. 'ip.visibility != ? AND ' => 2,
  11286. 'ip.tool = ? AND ' => TOOL_FORUM,
  11287. 'f.c_id = ? AND ' => intval($this->course_int_id),
  11288. 'f.lp_id = ?' => intval($this->lp_id),
  11289. ],
  11290. ],
  11291. 'first'
  11292. );
  11293. return $resultData['qty'] > 0;
  11294. }
  11295. /**
  11296. * Get the forum for this learning path.
  11297. *
  11298. * @param int $sessionId
  11299. *
  11300. * @return bool
  11301. */
  11302. public function getForum($sessionId = 0)
  11303. {
  11304. $forumTable = Database::get_course_table(TABLE_FORUM);
  11305. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  11306. $fakeFrom = "$forumTable f
  11307. INNER JOIN $itemProperty ip ";
  11308. if ($this->lp_session_id == 0) {
  11309. $fakeFrom .= "
  11310. ON (
  11311. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  11312. f.session_id = ip.session_id OR ip.session_id IS NULL
  11313. )
  11314. )
  11315. ";
  11316. } else {
  11317. $fakeFrom .= "
  11318. ON (
  11319. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  11320. )
  11321. ";
  11322. }
  11323. $resultData = Database::select(
  11324. 'f.*',
  11325. $fakeFrom,
  11326. [
  11327. 'where' => [
  11328. 'ip.visibility != ? AND ' => 2,
  11329. 'ip.tool = ? AND ' => TOOL_FORUM,
  11330. 'f.session_id = ? AND ' => $sessionId,
  11331. 'f.c_id = ? AND ' => intval($this->course_int_id),
  11332. 'f.lp_id = ?' => intval($this->lp_id),
  11333. ],
  11334. ],
  11335. 'first'
  11336. );
  11337. if (empty($resultData)) {
  11338. return false;
  11339. }
  11340. return $resultData;
  11341. }
  11342. /**
  11343. * Create a forum for this learning path.
  11344. *
  11345. * @param int $forumCategoryId
  11346. *
  11347. * @return int The forum ID if was created. Otherwise return false
  11348. */
  11349. public function createForum($forumCategoryId)
  11350. {
  11351. require_once api_get_path(SYS_CODE_PATH).'/forum/forumfunction.inc.php';
  11352. $forumId = store_forum(
  11353. [
  11354. 'lp_id' => $this->lp_id,
  11355. 'forum_title' => $this->name,
  11356. 'forum_comment' => null,
  11357. 'forum_category' => (int) $forumCategoryId,
  11358. 'students_can_edit_group' => ['students_can_edit' => 0],
  11359. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  11360. 'default_view_type_group' => ['default_view_type' => 'flat'],
  11361. 'group_forum' => 0,
  11362. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public'],
  11363. ],
  11364. [],
  11365. true
  11366. );
  11367. return $forumId;
  11368. }
  11369. /**
  11370. * Get the LP Final Item form.
  11371. *
  11372. * @throws Exception
  11373. * @throws HTML_QuickForm_Error
  11374. *
  11375. * @return string
  11376. */
  11377. public function getFinalItemForm()
  11378. {
  11379. $finalItem = $this->getFinalItem();
  11380. $title = '';
  11381. if ($finalItem) {
  11382. $title = $finalItem->get_title();
  11383. $buttonText = get_lang('Save');
  11384. $content = $this->getSavedFinalItem();
  11385. } else {
  11386. $buttonText = get_lang('LPCreateDocument');
  11387. $content = $this->getFinalItemTemplate();
  11388. }
  11389. $courseInfo = api_get_course_info();
  11390. $result = $this->generate_lp_folder($courseInfo);
  11391. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  11392. $relative_prefix = '../../';
  11393. $editorConfig = [
  11394. 'ToolbarSet' => 'LearningPathDocuments',
  11395. 'Width' => '100%',
  11396. 'Height' => '500',
  11397. 'FullPage' => true,
  11398. 'CreateDocumentDir' => $relative_prefix,
  11399. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  11400. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path,
  11401. ];
  11402. $url = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  11403. 'type' => 'document',
  11404. 'lp_id' => $this->lp_id,
  11405. ]);
  11406. $form = new FormValidator('final_item', 'POST', $url);
  11407. $form->addText('title', get_lang('Title'));
  11408. $form->addButtonSave($buttonText);
  11409. $form->addHtml(
  11410. Display::return_message(
  11411. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  11412. 'normal',
  11413. false
  11414. )
  11415. );
  11416. $renderer = $form->defaultRenderer();
  11417. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  11418. $form->addHtmlEditor(
  11419. 'content_lp_certificate',
  11420. null,
  11421. true,
  11422. false,
  11423. $editorConfig,
  11424. true
  11425. );
  11426. $form->addHidden('action', 'add_final_item');
  11427. $form->addHidden('path', Session::read('pathItem'));
  11428. $form->addHidden('previous', $this->get_last());
  11429. $form->setDefaults(
  11430. ['title' => $title, 'content_lp_certificate' => $content]
  11431. );
  11432. if ($form->validate()) {
  11433. $values = $form->exportValues();
  11434. $lastItemId = $this->getLastInFirstLevel();
  11435. if (!$finalItem) {
  11436. $documentId = $this->create_document(
  11437. $this->course_info,
  11438. $values['content_lp_certificate'],
  11439. $values['title']
  11440. );
  11441. $this->add_item(
  11442. 0,
  11443. $lastItemId,
  11444. 'final_item',
  11445. $documentId,
  11446. $values['title'],
  11447. ''
  11448. );
  11449. Display::addFlash(
  11450. Display::return_message(get_lang('Added'))
  11451. );
  11452. } else {
  11453. $this->edit_document($this->course_info);
  11454. }
  11455. }
  11456. return $form->returnForm();
  11457. }
  11458. /**
  11459. * Check if the current lp item is first, both, last or none from lp list.
  11460. *
  11461. * @param int $currentItemId
  11462. *
  11463. * @return string
  11464. */
  11465. public function isFirstOrLastItem($currentItemId)
  11466. {
  11467. $lpItemId = [];
  11468. $typeListNotToVerify = self::getChapterTypes();
  11469. // Using get_toc() function instead $this->items because returns the correct order of the items
  11470. foreach ($this->get_toc() as $item) {
  11471. if (!in_array($item['type'], $typeListNotToVerify)) {
  11472. $lpItemId[] = $item['id'];
  11473. }
  11474. }
  11475. $lastLpItemIndex = count($lpItemId) - 1;
  11476. $position = array_search($currentItemId, $lpItemId);
  11477. switch ($position) {
  11478. case 0:
  11479. if (!$lastLpItemIndex) {
  11480. $answer = 'both';
  11481. break;
  11482. }
  11483. $answer = 'first';
  11484. break;
  11485. case $lastLpItemIndex:
  11486. $answer = 'last';
  11487. break;
  11488. default:
  11489. $answer = 'none';
  11490. }
  11491. return $answer;
  11492. }
  11493. /**
  11494. * Get whether this is a learning path with the accumulated SCORM time or not.
  11495. *
  11496. * @return int
  11497. */
  11498. public function getAccumulateScormTime()
  11499. {
  11500. return $this->accumulateScormTime;
  11501. }
  11502. /**
  11503. * Set whether this is a learning path with the accumulated SCORM time or not.
  11504. *
  11505. * @param int $value (0 = false, 1 = true)
  11506. *
  11507. * @return bool Always returns true
  11508. */
  11509. public function setAccumulateScormTime($value)
  11510. {
  11511. $this->accumulateScormTime = (int) $value;
  11512. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  11513. $lp_id = $this->get_id();
  11514. $sql = "UPDATE $lp_table
  11515. SET accumulate_scorm_time = ".$this->accumulateScormTime."
  11516. WHERE iid = $lp_id";
  11517. Database::query($sql);
  11518. return true;
  11519. }
  11520. /**
  11521. * Returns an HTML-formatted link to a resource, to incorporate directly into
  11522. * the new learning path tool.
  11523. *
  11524. * The function is a big switch on tool type.
  11525. * In each case, we query the corresponding table for information and build the link
  11526. * with that information.
  11527. *
  11528. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on
  11529. * previous work (display_addedresource_link_in_learnpath())
  11530. *
  11531. * @param int $course_id Course code
  11532. * @param int $learningPathId The learning path ID (in lp table)
  11533. * @param int $id_in_path the unique index in the items table
  11534. * @param int $lpViewId
  11535. *
  11536. * @return string
  11537. */
  11538. public static function rl_get_resource_link_for_learnpath(
  11539. $course_id,
  11540. $learningPathId,
  11541. $id_in_path,
  11542. $lpViewId
  11543. ) {
  11544. $session_id = api_get_session_id();
  11545. $course_info = api_get_course_info_by_id($course_id);
  11546. $learningPathId = (int) $learningPathId;
  11547. $id_in_path = (int) $id_in_path;
  11548. $lpViewId = (int) $lpViewId;
  11549. $em = Database::getManager();
  11550. $lpItemRepo = $em->getRepository('ChamiloCourseBundle:CLpItem');
  11551. /** @var CLpItem $rowItem */
  11552. $rowItem = $lpItemRepo->findOneBy([
  11553. 'cId' => $course_id,
  11554. 'lpId' => $learningPathId,
  11555. 'iid' => $id_in_path,
  11556. ]);
  11557. if (!$rowItem) {
  11558. // Try one more time with "id"
  11559. /** @var CLpItem $rowItem */
  11560. $rowItem = $lpItemRepo->findOneBy([
  11561. 'cId' => $course_id,
  11562. 'lpId' => $learningPathId,
  11563. 'id' => $id_in_path,
  11564. ]);
  11565. if (!$rowItem) {
  11566. return -1;
  11567. }
  11568. }
  11569. $course_code = $course_info['code'];
  11570. $type = $rowItem->getItemType();
  11571. $id = empty($rowItem->getPath()) ? '0' : $rowItem->getPath();
  11572. $main_dir_path = api_get_path(WEB_CODE_PATH);
  11573. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  11574. $link = '';
  11575. $extraParams = api_get_cidreq(true, true, 'learnpath').'&session_id='.$session_id;
  11576. switch ($type) {
  11577. case 'dir':
  11578. return $main_dir_path.'lp/blank.php';
  11579. case TOOL_CALENDAR_EVENT:
  11580. return $main_dir_path.'calendar/agenda.php?agenda_id='.$id.'&'.$extraParams;
  11581. case TOOL_ANNOUNCEMENT:
  11582. return $main_dir_path.'announcements/announcements.php?ann_id='.$id.'&'.$extraParams;
  11583. case TOOL_LINK:
  11584. $linkInfo = Link::getLinkInfo($id);
  11585. if (isset($linkInfo['url'])) {
  11586. return $linkInfo['url'];
  11587. }
  11588. return '';
  11589. case TOOL_QUIZ:
  11590. if (empty($id)) {
  11591. return '';
  11592. }
  11593. // Get the lp_item_view with the highest view_count.
  11594. $learnpathItemViewResult = $em
  11595. ->getRepository('ChamiloCourseBundle:CLpItemView')
  11596. ->findBy(
  11597. ['cId' => $course_id, 'lpItemId' => $rowItem->getId(), 'lpViewId' => $lpViewId],
  11598. ['viewCount' => 'DESC'],
  11599. 1
  11600. );
  11601. /** @var CLpItemView $learnpathItemViewData */
  11602. $learnpathItemViewData = current($learnpathItemViewResult);
  11603. $learnpathItemViewId = $learnpathItemViewData ? $learnpathItemViewData->getId() : 0;
  11604. return $main_dir_path.'exercise/overview.php?'.$extraParams.'&'
  11605. .http_build_query([
  11606. 'lp_init' => 1,
  11607. 'learnpath_item_view_id' => $learnpathItemViewId,
  11608. 'learnpath_id' => $learningPathId,
  11609. 'learnpath_item_id' => $id_in_path,
  11610. 'exerciseId' => $id,
  11611. ]);
  11612. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  11613. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  11614. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND iid=$id");
  11615. $myrow = Database::fetch_array($result);
  11616. $path = $myrow['path'];
  11617. return $main_dir_path.'exercise/showinframes.php?file='.$path.'&cid='.$course_code.'&uid='
  11618. .api_get_user_id().'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path
  11619. .'&lp_view_id='.$lpViewId.'&'.$extraParams;
  11620. case TOOL_FORUM:
  11621. return $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&'.$extraParams;
  11622. case TOOL_THREAD:
  11623. // forum post
  11624. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  11625. if (empty($id)) {
  11626. return '';
  11627. }
  11628. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  11629. $result = Database::query($sql);
  11630. $myrow = Database::fetch_array($result);
  11631. return $main_dir_path.'forum/viewthread.php?thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true&'
  11632. .$extraParams;
  11633. case TOOL_POST:
  11634. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  11635. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  11636. $myrow = Database::fetch_array($result);
  11637. return $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='
  11638. .$myrow['forum_id'].'&lp=true&'.$extraParams;
  11639. case TOOL_READOUT_TEXT:
  11640. return api_get_path(WEB_CODE_PATH).
  11641. 'lp/readout_text.php?&id='.$id.'&lp_id='.$learningPathId.'&'.$extraParams;
  11642. case TOOL_DOCUMENT:
  11643. $repo = $em->getRepository('ChamiloCourseBundle:CDocument');
  11644. $document = $repo->findOneBy(['cId' => $course_id, 'iid' => $id]);
  11645. if (empty($document)) {
  11646. // Try with normal id
  11647. $document = $repo->findOneBy(['cId' => $course_id, 'id' => $id]);
  11648. if (empty($document)) {
  11649. return '';
  11650. }
  11651. }
  11652. $documentPathInfo = pathinfo($document->getPath());
  11653. $mediaSupportedFiles = ['mp3', 'mp4', 'ogv', 'flv', 'm4v'];
  11654. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  11655. $showDirectUrl = !in_array($extension, $mediaSupportedFiles);
  11656. $openmethod = 2;
  11657. $officedoc = false;
  11658. Session::write('openmethod', $openmethod);
  11659. Session::write('officedoc', $officedoc);
  11660. if ($showDirectUrl) {
  11661. $file = $main_course_path.'document'.$document->getPath().'?'.$extraParams;
  11662. if (api_get_configuration_value('allow_pdf_viewerjs_in_lp')) {
  11663. if (Link::isPdfLink($file)) {
  11664. $pdfUrl = api_get_path(WEB_LIBRARY_PATH).'javascript/ViewerJS/index.html#'.$file;
  11665. return $pdfUrl;
  11666. }
  11667. }
  11668. return $file;
  11669. }
  11670. return api_get_path(WEB_CODE_PATH).'document/showinframes.php?id='.$id.'&'.$extraParams;
  11671. case TOOL_LP_FINAL_ITEM:
  11672. return api_get_path(WEB_CODE_PATH).'lp/lp_final_item.php?&id='.$id.'&lp_id='.$learningPathId.'&'
  11673. .$extraParams;
  11674. case 'assignments':
  11675. return $main_dir_path.'work/work.php?'.$extraParams;
  11676. case TOOL_DROPBOX:
  11677. return $main_dir_path.'dropbox/index.php?'.$extraParams;
  11678. case 'introduction_text': //DEPRECATED
  11679. return '';
  11680. case TOOL_COURSE_DESCRIPTION:
  11681. return $main_dir_path.'course_description?'.$extraParams;
  11682. case TOOL_GROUP:
  11683. return $main_dir_path.'group/group.php?'.$extraParams;
  11684. case TOOL_USER:
  11685. return $main_dir_path.'user/user.php?'.$extraParams;
  11686. case TOOL_STUDENTPUBLICATION:
  11687. if (!empty($rowItem->getPath())) {
  11688. return $main_dir_path.'work/work_list.php?id='.$rowItem->getPath().'&'.$extraParams;
  11689. }
  11690. return $main_dir_path.'work/work.php?'.api_get_cidreq().'&id='.$rowItem->getPath().'&'.$extraParams;
  11691. }
  11692. return $link;
  11693. }
  11694. /**
  11695. * Gets the name of a resource (generally used in learnpath when no name is provided).
  11696. *
  11697. * @author Yannick Warnier <ywarnier@beeznest.org>
  11698. *
  11699. * @param string $course_code Course code
  11700. * @param int $learningPathId
  11701. * @param int $id_in_path The resource ID
  11702. *
  11703. * @return string
  11704. */
  11705. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  11706. {
  11707. $_course = api_get_course_info($course_code);
  11708. if (empty($_course)) {
  11709. return '';
  11710. }
  11711. $course_id = $_course['real_id'];
  11712. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  11713. $learningPathId = (int) $learningPathId;
  11714. $id_in_path = (int) $id_in_path;
  11715. $sql = "SELECT item_type, title, ref
  11716. FROM $tbl_lp_item
  11717. WHERE c_id = $course_id AND lp_id = $learningPathId AND iid = $id_in_path";
  11718. $res_item = Database::query($sql);
  11719. if (Database::num_rows($res_item) < 1) {
  11720. return '';
  11721. }
  11722. $row_item = Database::fetch_array($res_item);
  11723. $type = strtolower($row_item['item_type']);
  11724. $id = $row_item['ref'];
  11725. $output = '';
  11726. switch ($type) {
  11727. case TOOL_CALENDAR_EVENT:
  11728. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  11729. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  11730. $myrow = Database::fetch_array($result);
  11731. $output = $myrow['title'];
  11732. break;
  11733. case TOOL_ANNOUNCEMENT:
  11734. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  11735. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  11736. $myrow = Database::fetch_array($result);
  11737. $output = $myrow['title'];
  11738. break;
  11739. case TOOL_LINK:
  11740. // Doesn't take $target into account.
  11741. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  11742. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  11743. $myrow = Database::fetch_array($result);
  11744. $output = $myrow['title'];
  11745. break;
  11746. case TOOL_QUIZ:
  11747. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  11748. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id = $id");
  11749. $myrow = Database::fetch_array($result);
  11750. $output = $myrow['title'];
  11751. break;
  11752. case TOOL_FORUM:
  11753. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  11754. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id = $id");
  11755. $myrow = Database::fetch_array($result);
  11756. $output = $myrow['forum_name'];
  11757. break;
  11758. case TOOL_THREAD:
  11759. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  11760. // Grabbing the title of the post.
  11761. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  11762. $result_title = Database::query($sql_title);
  11763. $myrow_title = Database::fetch_array($result_title);
  11764. $output = $myrow_title['post_title'];
  11765. break;
  11766. case TOOL_POST:
  11767. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  11768. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  11769. $result = Database::query($sql);
  11770. $post = Database::fetch_array($result);
  11771. $output = $post['post_title'];
  11772. break;
  11773. case 'dir':
  11774. case TOOL_DOCUMENT:
  11775. $title = $row_item['title'];
  11776. $output = '-';
  11777. if (!empty($title)) {
  11778. $output = $title;
  11779. }
  11780. break;
  11781. case 'hotpotatoes':
  11782. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  11783. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND iid = $id");
  11784. $myrow = Database::fetch_array($result);
  11785. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  11786. $last = count($pathname) - 1; // Making a correct name for the link.
  11787. $filename = $pathname[$last]; // Making a correct name for the link.
  11788. $myrow['path'] = rawurlencode($myrow['path']);
  11789. $output = $filename;
  11790. break;
  11791. }
  11792. return stripslashes($output);
  11793. }
  11794. /**
  11795. * Get the parent names for the current item.
  11796. *
  11797. * @param int $newItemId Optional. The item ID
  11798. *
  11799. * @return array
  11800. */
  11801. public function getCurrentItemParentNames($newItemId = 0)
  11802. {
  11803. $newItemId = $newItemId ?: $this->get_current_item_id();
  11804. $return = [];
  11805. $item = $this->getItem($newItemId);
  11806. $parent = $this->getItem($item->get_parent());
  11807. while ($parent) {
  11808. $return[] = $parent->get_title();
  11809. $parent = $this->getItem($parent->get_parent());
  11810. }
  11811. return array_reverse($return);
  11812. }
  11813. /**
  11814. * Reads and process "lp_subscription_settings" setting.
  11815. *
  11816. * @return array
  11817. */
  11818. public static function getSubscriptionSettings()
  11819. {
  11820. $subscriptionSettings = api_get_configuration_value('lp_subscription_settings');
  11821. if (empty($subscriptionSettings)) {
  11822. // By default allow both settings
  11823. $subscriptionSettings = [
  11824. 'allow_add_users_to_lp' => true,
  11825. 'allow_add_users_to_lp_category' => true,
  11826. ];
  11827. } else {
  11828. $subscriptionSettings = $subscriptionSettings['options'];
  11829. }
  11830. return $subscriptionSettings;
  11831. }
  11832. /**
  11833. * Exports a LP to a courseBuilder zip file. It adds the documents related to the LP.
  11834. */
  11835. public function exportToCourseBuildFormat()
  11836. {
  11837. if (!api_is_allowed_to_edit()) {
  11838. return false;
  11839. }
  11840. $courseBuilder = new CourseBuilder();
  11841. $itemList = [];
  11842. /** @var learnpathItem $item */
  11843. foreach ($this->items as $item) {
  11844. $itemList[$item->get_type()][] = $item->get_path();
  11845. }
  11846. if (empty($itemList)) {
  11847. return false;
  11848. }
  11849. if (isset($itemList['document'])) {
  11850. // Get parents
  11851. foreach ($itemList['document'] as $documentId) {
  11852. $documentInfo = DocumentManager::get_document_data_by_id($documentId, api_get_course_id(), true);
  11853. if (!empty($documentInfo['parents'])) {
  11854. foreach ($documentInfo['parents'] as $parentInfo) {
  11855. if (in_array($parentInfo['iid'], $itemList['document'])) {
  11856. continue;
  11857. }
  11858. $itemList['document'][] = $parentInfo['iid'];
  11859. }
  11860. }
  11861. }
  11862. $courseInfo = api_get_course_info();
  11863. foreach ($itemList['document'] as $documentId) {
  11864. $documentInfo = DocumentManager::get_document_data_by_id($documentId, api_get_course_id());
  11865. $items = DocumentManager::get_resources_from_source_html(
  11866. $documentInfo['absolute_path'],
  11867. true,
  11868. TOOL_DOCUMENT
  11869. );
  11870. if (!empty($items)) {
  11871. foreach ($items as $item) {
  11872. // Get information about source url
  11873. $url = $item[0]; // url
  11874. $scope = $item[1]; // scope (local, remote)
  11875. $type = $item[2]; // type (rel, abs, url)
  11876. $origParseUrl = parse_url($url);
  11877. $realOrigPath = isset($origParseUrl['path']) ? $origParseUrl['path'] : null;
  11878. if ($scope == 'local') {
  11879. if ($type == 'abs' || $type == 'rel') {
  11880. $documentFile = strstr($realOrigPath, 'document');
  11881. if (strpos($realOrigPath, $documentFile) !== false) {
  11882. $documentFile = str_replace('document', '', $documentFile);
  11883. $itemDocumentId = DocumentManager::get_document_id($courseInfo, $documentFile);
  11884. // Document found! Add it to the list
  11885. if ($itemDocumentId) {
  11886. $itemList['document'][] = $itemDocumentId;
  11887. }
  11888. }
  11889. }
  11890. }
  11891. }
  11892. }
  11893. }
  11894. $courseBuilder->build_documents(
  11895. api_get_session_id(),
  11896. $this->get_course_int_id(),
  11897. true,
  11898. $itemList['document']
  11899. );
  11900. }
  11901. if (isset($itemList['quiz'])) {
  11902. $courseBuilder->build_quizzes(
  11903. api_get_session_id(),
  11904. $this->get_course_int_id(),
  11905. true,
  11906. $itemList['quiz']
  11907. );
  11908. }
  11909. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  11910. /*if (!empty($itemList['thread'])) {
  11911. $postList = [];
  11912. foreach ($itemList['thread'] as $postId) {
  11913. $post = get_post_information($postId);
  11914. if ($post) {
  11915. if (!isset($itemList['forum'])) {
  11916. $itemList['forum'] = [];
  11917. }
  11918. $itemList['forum'][] = $post['forum_id'];
  11919. $postList[] = $postId;
  11920. }
  11921. }
  11922. if (!empty($postList)) {
  11923. $courseBuilder->build_forum_posts(
  11924. $this->get_course_int_id(),
  11925. null,
  11926. null,
  11927. $postList
  11928. );
  11929. }
  11930. }*/
  11931. if (!empty($itemList['thread'])) {
  11932. $threadList = [];
  11933. $em = Database::getManager();
  11934. $repo = $em->getRepository('ChamiloCourseBundle:CForumThread');
  11935. foreach ($itemList['thread'] as $threadId) {
  11936. /** @var \Chamilo\CourseBundle\Entity\CForumThread $thread */
  11937. $thread = $repo->find($threadId);
  11938. if ($thread) {
  11939. $itemList['forum'][] = $thread->getForumId();
  11940. $threadList[] = $thread->getIid();
  11941. }
  11942. }
  11943. if (!empty($threadList)) {
  11944. $courseBuilder->build_forum_topics(
  11945. api_get_session_id(),
  11946. $this->get_course_int_id(),
  11947. null,
  11948. $threadList
  11949. );
  11950. }
  11951. }
  11952. $forumCategoryList = [];
  11953. if (isset($itemList['forum'])) {
  11954. foreach ($itemList['forum'] as $forumId) {
  11955. $forumInfo = get_forums($forumId);
  11956. $forumCategoryList[] = $forumInfo['forum_category'];
  11957. }
  11958. }
  11959. if (!empty($forumCategoryList)) {
  11960. $courseBuilder->build_forum_category(
  11961. api_get_session_id(),
  11962. $this->get_course_int_id(),
  11963. true,
  11964. $forumCategoryList
  11965. );
  11966. }
  11967. if (!empty($itemList['forum'])) {
  11968. $courseBuilder->build_forums(
  11969. api_get_session_id(),
  11970. $this->get_course_int_id(),
  11971. true,
  11972. $itemList['forum']
  11973. );
  11974. }
  11975. if (isset($itemList['link'])) {
  11976. $courseBuilder->build_links(
  11977. api_get_session_id(),
  11978. $this->get_course_int_id(),
  11979. true,
  11980. $itemList['link']
  11981. );
  11982. }
  11983. if (!empty($itemList['student_publication'])) {
  11984. $courseBuilder->build_works(
  11985. api_get_session_id(),
  11986. $this->get_course_int_id(),
  11987. true,
  11988. $itemList['student_publication']
  11989. );
  11990. }
  11991. $courseBuilder->build_learnpaths(
  11992. api_get_session_id(),
  11993. $this->get_course_int_id(),
  11994. true,
  11995. [$this->get_id()],
  11996. false
  11997. );
  11998. $courseBuilder->restoreDocumentsFromList();
  11999. $zipFile = CourseArchiver::createBackup($courseBuilder->course);
  12000. $zipPath = CourseArchiver::getBackupDir().$zipFile;
  12001. $result = DocumentManager::file_send_for_download(
  12002. $zipPath,
  12003. true,
  12004. $this->get_name().'.zip'
  12005. );
  12006. if ($result) {
  12007. api_not_allowed();
  12008. }
  12009. return true;
  12010. }
  12011. /**
  12012. * Get whether this is a learning path with the accumulated work time or not.
  12013. *
  12014. * @return int
  12015. */
  12016. public function getAccumulateWorkTime()
  12017. {
  12018. return (int) $this->accumulateWorkTime;
  12019. }
  12020. /**
  12021. * Get whether this is a learning path with the accumulated work time or not.
  12022. *
  12023. * @return int
  12024. */
  12025. public function getAccumulateWorkTimeTotalCourse()
  12026. {
  12027. $table = Database::get_course_table(TABLE_LP_MAIN);
  12028. $sql = "SELECT SUM(accumulate_work_time) AS total
  12029. FROM $table
  12030. WHERE c_id = ".$this->course_int_id;
  12031. $result = Database::query($sql);
  12032. $row = Database::fetch_array($result);
  12033. return (int) $row['total'];
  12034. }
  12035. /**
  12036. * Set whether this is a learning path with the accumulated work time or not.
  12037. *
  12038. * @param int $value (0 = false, 1 = true)
  12039. *
  12040. * @return bool
  12041. */
  12042. public function setAccumulateWorkTime($value)
  12043. {
  12044. if (!api_get_configuration_value('lp_minimum_time')) {
  12045. return false;
  12046. }
  12047. $this->accumulateWorkTime = (int) $value;
  12048. $table = Database::get_course_table(TABLE_LP_MAIN);
  12049. $lp_id = $this->get_id();
  12050. $sql = "UPDATE $table SET accumulate_work_time = ".$this->accumulateWorkTime."
  12051. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  12052. Database::query($sql);
  12053. return true;
  12054. }
  12055. /**
  12056. * @param int $lpId
  12057. * @param int $courseId
  12058. *
  12059. * @return mixed
  12060. */
  12061. public static function getAccumulateWorkTimePrerequisite($lpId, $courseId)
  12062. {
  12063. $lpId = (int) $lpId;
  12064. $courseId = (int) $courseId;
  12065. $table = Database::get_course_table(TABLE_LP_MAIN);
  12066. $sql = "SELECT accumulate_work_time
  12067. FROM $table
  12068. WHERE c_id = $courseId AND id = $lpId";
  12069. $result = Database::query($sql);
  12070. $row = Database::fetch_array($result);
  12071. return $row['accumulate_work_time'];
  12072. }
  12073. /**
  12074. * @param int $courseId
  12075. *
  12076. * @return int
  12077. */
  12078. public static function getAccumulateWorkTimeTotal($courseId)
  12079. {
  12080. $table = Database::get_course_table(TABLE_LP_MAIN);
  12081. $courseId = (int) $courseId;
  12082. $sql = "SELECT SUM(accumulate_work_time) AS total
  12083. FROM $table
  12084. WHERE c_id = $courseId";
  12085. $result = Database::query($sql);
  12086. $row = Database::fetch_array($result);
  12087. return (int) $row['total'];
  12088. }
  12089. /**
  12090. * In order to use the lp icon option you need to create the "lp_icon" LP extra field
  12091. * and put the images in.
  12092. *
  12093. * @return array
  12094. */
  12095. public static function getIconSelect()
  12096. {
  12097. $theme = api_get_visual_theme();
  12098. $path = api_get_path(SYS_PUBLIC_PATH).'css/themes/'.$theme.'/lp_icons/';
  12099. $icons = ['' => get_lang('SelectAnOption')];
  12100. if (is_dir($path)) {
  12101. $finder = new Finder();
  12102. $finder->files()->in($path);
  12103. $allowedExtensions = ['jpeg', 'jpg', 'png'];
  12104. /** @var SplFileInfo $file */
  12105. foreach ($finder as $file) {
  12106. if (in_array(strtolower($file->getExtension()), $allowedExtensions)) {
  12107. $icons[$file->getFilename()] = $file->getFilename();
  12108. }
  12109. }
  12110. }
  12111. return $icons;
  12112. }
  12113. /**
  12114. * @param int $lpId
  12115. *
  12116. * @return string
  12117. */
  12118. public static function getSelectedIcon($lpId)
  12119. {
  12120. $extraFieldValue = new ExtraFieldValue('lp');
  12121. $lpIcon = $extraFieldValue->get_values_by_handler_and_field_variable($lpId, 'lp_icon');
  12122. $icon = '';
  12123. if (!empty($lpIcon) && isset($lpIcon['value'])) {
  12124. $icon = $lpIcon['value'];
  12125. }
  12126. return $icon;
  12127. }
  12128. /**
  12129. * @param int $lpId
  12130. *
  12131. * @return string
  12132. */
  12133. public static function getSelectedIconHtml($lpId)
  12134. {
  12135. $icon = self::getSelectedIcon($lpId);
  12136. if (empty($icon)) {
  12137. return '';
  12138. }
  12139. $theme = api_get_visual_theme();
  12140. $path = api_get_path(WEB_PUBLIC_PATH).'css/themes/'.$theme.'/lp_icons/'.$icon;
  12141. return Display::img($path);
  12142. }
  12143. /**
  12144. * @param string $value
  12145. *
  12146. * @return string
  12147. */
  12148. public function cleanItemTitle($value)
  12149. {
  12150. $value = Security::remove_XSS(strip_tags($value));
  12151. return $value;
  12152. }
  12153. /**
  12154. * @param FormValidator $form
  12155. */
  12156. public function setItemTitle(FormValidator $form)
  12157. {
  12158. if (api_get_configuration_value('save_titles_as_html')) {
  12159. $form->addHtmlEditor(
  12160. 'title',
  12161. get_lang('Title'),
  12162. true,
  12163. false,
  12164. ['ToolbarSet' => 'TitleAsHtml']
  12165. );
  12166. } else {
  12167. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  12168. $form->applyFilter('title', 'trim');
  12169. $form->applyFilter('title', 'html_filter');
  12170. }
  12171. }
  12172. /**
  12173. * @return array
  12174. */
  12175. public function getItemsForForm($addParentCondition = false)
  12176. {
  12177. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  12178. $course_id = api_get_course_int_id();
  12179. $sql = "SELECT * FROM $tbl_lp_item
  12180. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  12181. if ($addParentCondition) {
  12182. $sql .= ' AND parent_item_id = 0 ';
  12183. }
  12184. $sql .= ' ORDER BY display_order ASC';
  12185. $result = Database::query($sql);
  12186. $arrLP = [];
  12187. while ($row = Database::fetch_array($result)) {
  12188. $arrLP[] = [
  12189. 'iid' => $row['iid'],
  12190. 'id' => $row['iid'],
  12191. 'item_type' => $row['item_type'],
  12192. 'title' => $this->cleanItemTitle($row['title']),
  12193. 'title_raw' => $row['title'],
  12194. 'path' => $row['path'],
  12195. 'description' => Security::remove_XSS($row['description']),
  12196. 'parent_item_id' => $row['parent_item_id'],
  12197. 'previous_item_id' => $row['previous_item_id'],
  12198. 'next_item_id' => $row['next_item_id'],
  12199. 'display_order' => $row['display_order'],
  12200. 'max_score' => $row['max_score'],
  12201. 'min_score' => $row['min_score'],
  12202. 'mastery_score' => $row['mastery_score'],
  12203. 'prerequisite' => $row['prerequisite'],
  12204. 'max_time_allowed' => $row['max_time_allowed'],
  12205. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  12206. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  12207. ];
  12208. }
  12209. return $arrLP;
  12210. }
  12211. /**
  12212. * Get the depth level of LP item.
  12213. *
  12214. * @param array $items
  12215. * @param int $currentItemId
  12216. *
  12217. * @return int
  12218. */
  12219. private static function get_level_for_item($items, $currentItemId)
  12220. {
  12221. $parentItemId = 0;
  12222. if (isset($items[$currentItemId])) {
  12223. $parentItemId = $items[$currentItemId]->parent;
  12224. }
  12225. if ($parentItemId == 0) {
  12226. return 0;
  12227. } else {
  12228. return self::get_level_for_item($items, $parentItemId) + 1;
  12229. }
  12230. }
  12231. /**
  12232. * Generate the link for a learnpath category as course tool.
  12233. *
  12234. * @param int $categoryId
  12235. *
  12236. * @return string
  12237. */
  12238. private static function getCategoryLinkForTool($categoryId)
  12239. {
  12240. $categoryId = (int) $categoryId;
  12241. $link = 'lp/lp_controller.php?'.api_get_cidreq().'&'
  12242. .http_build_query(
  12243. [
  12244. 'action' => 'view_category',
  12245. 'id' => $categoryId,
  12246. ]
  12247. );
  12248. return $link;
  12249. }
  12250. /**
  12251. * Return the scorm item type object with spaces replaced with _
  12252. * The return result is use to build a css classname like scorm_type_$return.
  12253. *
  12254. * @param $in_type
  12255. *
  12256. * @return mixed
  12257. */
  12258. private static function format_scorm_type_item($in_type)
  12259. {
  12260. return str_replace(' ', '_', $in_type);
  12261. }
  12262. /**
  12263. * Check and obtain the lp final item if exist.
  12264. *
  12265. * @return learnpathItem
  12266. */
  12267. private function getFinalItem()
  12268. {
  12269. if (empty($this->items)) {
  12270. return null;
  12271. }
  12272. foreach ($this->items as $item) {
  12273. if ($item->type !== 'final_item') {
  12274. continue;
  12275. }
  12276. return $item;
  12277. }
  12278. }
  12279. /**
  12280. * Get the LP Final Item Template.
  12281. *
  12282. * @return string
  12283. */
  12284. private function getFinalItemTemplate()
  12285. {
  12286. return file_get_contents(api_get_path(SYS_CODE_PATH).'lp/final_item_template/template.html');
  12287. }
  12288. /**
  12289. * Get the LP Final Item Url.
  12290. *
  12291. * @return string
  12292. */
  12293. private function getSavedFinalItem()
  12294. {
  12295. $finalItem = $this->getFinalItem();
  12296. $doc = DocumentManager::get_document_data_by_id(
  12297. $finalItem->path,
  12298. $this->cc
  12299. );
  12300. if ($doc && file_exists($doc['absolute_path'])) {
  12301. return file_get_contents($doc['absolute_path']);
  12302. }
  12303. return '';
  12304. }
  12305. }