learnpath.class.php 471 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  8. /**
  9. * Class learnpath
  10. * This class defines the parent attributes and methods for Chamilo learnpaths
  11. * and SCORM learnpaths. It is used by the scorm class.
  12. *
  13. * @package chamilo.learnpath
  14. * @author Yannick Warnier <ywarnier@beeznest.org>
  15. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  16. */
  17. class learnpath
  18. {
  19. public $attempt = 0; // The number for the current ID view.
  20. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  21. public $current; // Id of the current item the user is viewing.
  22. public $current_score; // The score of the current item.
  23. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  24. public $current_time_stop; // The time the user closed this resource.
  25. public $default_status = 'not attempted';
  26. public $encoding = 'UTF-8';
  27. public $error = '';
  28. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  29. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  30. public $index; // The index of the active learnpath_item in $ordered_items array.
  31. public $items = array();
  32. public $last; // item_id of last item viewed in the learning path.
  33. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  34. public $license; // Which license this course has been given - not used yet on 20060522.
  35. public $lp_id; // DB ID for this learnpath.
  36. public $lp_view_id; // DB ID for lp_view
  37. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  38. public $message = '';
  39. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  40. public $name; // Learnpath name (they generally have one).
  41. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  42. public $path = ''; // Path inside the scorm directory (if scorm).
  43. public $theme; // The current theme of the learning path.
  44. public $preview_image; // The current image of the learning path.
  45. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  46. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  47. public $prevent_reinit = 1;
  48. // Describes the mode of progress bar display.
  49. public $seriousgame_mode = 0;
  50. public $progress_bar_mode = '%';
  51. // Percentage progress as saved in the db.
  52. public $progress_db = '0';
  53. public $proximity; // Wether the content is distant or local or unknown.
  54. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  55. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  56. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  57. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  58. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  59. public $user_id; //ID of the user that is viewing/using the course
  60. public $update_queue = array();
  61. public $scorm_debug = 0;
  62. public $arrMenu = array(); // Array for the menu items.
  63. public $debug = 0; // Logging level.
  64. public $lp_session_id = 0;
  65. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  66. public $prerequisite = 0;
  67. public $use_max_score = 1; // 1 or 0
  68. public $subscribeUsers = 0; // Subscribe users or not
  69. public $created_on = '';
  70. public $modified_on = '';
  71. public $publicated_on = '';
  72. public $expired_on = '';
  73. public $ref = null;
  74. public $course_int_id;
  75. public $course_info = array();
  76. public $categoryId;
  77. /**
  78. * Constructor.
  79. * Needs a database handler, a course code and a learnpath id from the database.
  80. * Also builds the list of items into $this->items.
  81. * @param string $course Course code
  82. * @param integer $lp_id
  83. * @param integer $user_id
  84. * @return mixed True on success, false on error
  85. */
  86. public function __construct($course, $lp_id, $user_id)
  87. {
  88. $this->encoding = api_get_system_encoding();
  89. if ($this->debug > 0) {
  90. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  91. }
  92. if (empty($course)) {
  93. $this->error = 'Course code is empty';
  94. return false;
  95. } else {
  96. $course_info = api_get_course_info($course);
  97. if (!empty($course_info)) {
  98. $this->cc = $course_info['code'];
  99. $this->course_info = $course_info;
  100. $course_id = $course_info['real_id'];
  101. } else {
  102. $this->error = 'Course code does not exist in database.';
  103. return false;
  104. }
  105. }
  106. $this->set_course_int_id($course_id);
  107. // Check learnpath ID.
  108. if (empty($lp_id)) {
  109. $this->error = 'Learnpath ID is empty';
  110. return false;
  111. } else {
  112. // TODO: Make it flexible to use any course_code (still using env course code here).
  113. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  114. $lp_id = intval($lp_id);
  115. $sql = "SELECT * FROM $lp_table
  116. WHERE id = '$lp_id' AND c_id = $course_id";
  117. if ($this->debug > 2) {
  118. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  119. }
  120. $res = Database::query($sql);
  121. if (Database::num_rows($res) > 0) {
  122. $this->lp_id = $lp_id;
  123. $row = Database::fetch_array($res);
  124. $this->type = $row['lp_type'];
  125. $this->name = stripslashes($row['name']);
  126. $this->proximity = $row['content_local'];
  127. $this->theme = $row['theme'];
  128. $this->maker = $row['content_maker'];
  129. $this->prevent_reinit = $row['prevent_reinit'];
  130. $this->seriousgame_mode = $row['seriousgame_mode'];
  131. $this->license = $row['content_license'];
  132. $this->scorm_debug = $row['debug'];
  133. $this->js_lib = $row['js_lib'];
  134. $this->path = $row['path'];
  135. $this->preview_image = $row['preview_image'];
  136. $this->author = $row['author'];
  137. $this->hide_toc_frame = $row['hide_toc_frame'];
  138. $this->lp_session_id = $row['session_id'];
  139. $this->use_max_score = $row['use_max_score'];
  140. $this->subscribeUsers = $row['subscribe_users'];
  141. $this->created_on = $row['created_on'];
  142. $this->modified_on = $row['modified_on'];
  143. $this->ref = $row['ref'];
  144. $this->categoryId = $row['category_id'];
  145. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  146. if (!empty($row['publicated_on'])) {
  147. $this->publicated_on = $row['publicated_on'];
  148. }
  149. if (!empty($row['expired_on'])) {
  150. $this->expired_on = $row['expired_on'];
  151. }
  152. if ($this->type == 2) {
  153. if ($row['force_commit'] == 1) {
  154. $this->force_commit = true;
  155. }
  156. }
  157. $this->mode = $row['default_view_mod'];
  158. } else {
  159. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  160. return false;
  161. }
  162. }
  163. // Check user ID.
  164. if (empty($user_id)) {
  165. $this->error = 'User ID is empty';
  166. return false;
  167. } else {
  168. $user_info = api_get_user_info($user_id);
  169. if (!empty($user_info)) {
  170. $this->user_id = $user_info['user_id'];
  171. } else {
  172. $this->error = 'User ID does not exist in database ('.$sql.')';
  173. return false;
  174. }
  175. }
  176. // End of variables checking.
  177. $session_id = api_get_session_id();
  178. // Get the session condition for learning paths of the base + session.
  179. $session = api_get_session_condition($session_id);
  180. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  181. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  182. // Selecting by view_count descending allows to get the highest view_count first.
  183. $sql = "SELECT * FROM $lp_table
  184. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  185. ORDER BY view_count DESC";
  186. $res = Database::query($sql);
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  189. }
  190. if (Database :: num_rows($res) > 0) {
  191. if ($this->debug > 2) {
  192. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  193. }
  194. $row = Database :: fetch_array($res);
  195. $this->attempt = $row['view_count'];
  196. $this->lp_view_id = $row['id'];
  197. $this->last_item_seen = $row['last_item'];
  198. $this->progress_db = $row['progress'];
  199. $this->lp_view_session_id = $row['session_id'];
  200. } else if (!api_is_invitee()) {
  201. if ($this->debug > 2) {
  202. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  203. }
  204. $this->attempt = 1;
  205. $params = [
  206. 'c_id' => $course_id,
  207. 'lp_id' => $lp_id,
  208. 'user_id' => $user_id,
  209. 'view_count' => 1,
  210. 'session_id' => $session_id,
  211. 'last_item' => 0
  212. ];
  213. Database::insert($lp_table, $params);
  214. $this->lp_view_id = Database::insert_id();
  215. if ($this->debug > 2) {
  216. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  217. }
  218. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  219. Database::query($sql);
  220. }
  221. // Initialise items.
  222. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  223. $sql = "SELECT * FROM $lp_item_table
  224. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  225. ORDER BY parent_item_id, display_order";
  226. $res = Database::query($sql);
  227. if ($this->debug > 2) {
  228. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  229. error_log('-- Start while--', 0);
  230. }
  231. $lp_item_id_list = array();
  232. while ($row = Database::fetch_array($res)) {
  233. $lp_item_id_list[] = $row['id'];
  234. switch ($this->type) {
  235. case 3: //aicc
  236. $oItem = new aiccItem('db', $row['id'], $course_id);
  237. if (is_object($oItem)) {
  238. $my_item_id = $oItem->get_id();
  239. $oItem->set_lp_view($this->lp_view_id, $course_id);
  240. $oItem->set_prevent_reinit($this->prevent_reinit);
  241. // Don't use reference here as the next loop will make the pointed object change.
  242. $this->items[$my_item_id] = $oItem;
  243. $this->refs_list[$oItem->ref] = $my_item_id;
  244. if ($this->debug > 2) {
  245. error_log(
  246. 'New LP - learnpath::__construct() - ' .
  247. 'aicc object with id ' . $my_item_id .
  248. ' set in items[]',
  249. 0
  250. );
  251. }
  252. }
  253. break;
  254. case 2:
  255. $oItem = new scormItem('db', $row['id'], $course_id);
  256. if (is_object($oItem)) {
  257. $my_item_id = $oItem->get_id();
  258. $oItem->set_lp_view($this->lp_view_id, $course_id);
  259. $oItem->set_prevent_reinit($this->prevent_reinit);
  260. // Don't use reference here as the next loop will make the pointed object change.
  261. $this->items[$my_item_id] = $oItem;
  262. $this->refs_list[$oItem->ref] = $my_item_id;
  263. if ($this->debug > 2) {
  264. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  265. }
  266. }
  267. break;
  268. case 1:
  269. default:
  270. if ($this->debug > 2) {
  271. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  272. }
  273. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  274. if ($this->debug > 2) {
  275. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  276. }
  277. if (is_object($oItem)) {
  278. $my_item_id = $oItem->get_id();
  279. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  280. $oItem->set_prevent_reinit($this->prevent_reinit);
  281. // Don't use reference here as the next loop will make the pointed object change.
  282. $this->items[$my_item_id] = $oItem;
  283. $this->refs_list[$my_item_id] = $my_item_id;
  284. if ($this->debug > 2) {
  285. error_log(
  286. 'New LP - learnpath::__construct() ' . __LINE__ .
  287. ' - object with id ' . $my_item_id . ' set in items[]',
  288. 0
  289. );
  290. }
  291. }
  292. break;
  293. }
  294. // Setting the object level with variable $this->items[$i][parent]
  295. foreach ($this->items as $itemLPObject) {
  296. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  297. $itemLPObject->level = $level;
  298. }
  299. // Setting the view in the item object.
  300. if (is_object($this->items[$row['id']])) {
  301. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  302. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  303. $this->items[$row['id']]->current_start_time = 0;
  304. $this->items[$row['id']]->current_stop_time = 0;
  305. }
  306. }
  307. }
  308. if ($this->debug > 2) {
  309. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  310. }
  311. if (!empty($lp_item_id_list)) {
  312. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  313. if (!empty($lp_item_id_list_to_string)) {
  314. // Get last viewing vars.
  315. $lp_item_view_table = Database:: get_course_table(
  316. TABLE_LP_ITEM_VIEW
  317. );
  318. // This query should only return one or zero result.
  319. $sql = "SELECT lp_item_id, status
  320. FROM $lp_item_view_table
  321. WHERE
  322. c_id = $course_id AND
  323. lp_view_id = ".$this->lp_view_id." AND
  324. lp_item_id IN ('".$lp_item_id_list_to_string."')
  325. ORDER BY view_count DESC ";
  326. if ($this->debug > 2) {
  327. error_log(
  328. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  329. 0
  330. );
  331. }
  332. $status_list = array();
  333. $res = Database::query($sql);
  334. while ($row = Database:: fetch_array($res)) {
  335. $status_list[$row['lp_item_id']] = $row['status'];
  336. }
  337. foreach ($lp_item_id_list as $item_id) {
  338. if (isset($status_list[$item_id])) {
  339. $status = $status_list[$item_id];
  340. if (is_object($this->items[$item_id])) {
  341. $this->items[$item_id]->set_status($status);
  342. if (empty($status)) {
  343. $this->items[$item_id]->set_status(
  344. $this->default_status
  345. );
  346. }
  347. }
  348. } else {
  349. if (!api_is_invitee()) {
  350. if (is_object($this->items[$item_id])) {
  351. $this->items[$item_id]->set_status(
  352. $this->default_status
  353. );
  354. }
  355. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  356. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  357. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  358. if ($this->debug > 2) {
  359. error_log(
  360. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  361. 0
  362. );
  363. }
  364. $this->items[$item_id]->set_lp_view(
  365. $this->lp_view_id,
  366. $course_id
  367. );
  368. Database::query($sql);
  369. $insertId = Database::insert_id();
  370. $sql = "UPDATE $lp_item_view_table SET id = iid
  371. WHERE iid = $insertId";
  372. Database::query($sql);
  373. }
  374. }
  375. }
  376. }
  377. }
  378. $this->ordered_items = self::get_flat_ordered_items_list(
  379. $this->get_id(),
  380. 0,
  381. $course_id
  382. );
  383. $this->max_ordered_items = 0;
  384. foreach ($this->ordered_items as $index => $dummy) {
  385. if ($index > $this->max_ordered_items && !empty($dummy)) {
  386. $this->max_ordered_items = $index;
  387. }
  388. }
  389. // TODO: Define the current item better.
  390. $this->first();
  391. if ($this->debug > 2) {
  392. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  393. }
  394. return true;
  395. }
  396. /**
  397. * @return string
  398. */
  399. public function getCourseCode()
  400. {
  401. return $this->cc;
  402. }
  403. /**
  404. * @return int
  405. */
  406. public function get_course_int_id()
  407. {
  408. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  409. }
  410. /**
  411. * @param $course_id
  412. * @return int
  413. */
  414. public function set_course_int_id($course_id)
  415. {
  416. return $this->course_int_id = intval($course_id);
  417. }
  418. /**
  419. * Get the depth level of LP item
  420. * @param $in_tab_items
  421. * @param $in_current_item_id
  422. * @return int
  423. */
  424. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  425. {
  426. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  427. if ($parent_item_id == 0) {
  428. return 0;
  429. } else {
  430. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  431. }
  432. }
  433. /**
  434. * Function rewritten based on old_add_item() from Yannick Warnier.
  435. * Due the fact that users can decide where the item should come, I had to overlook this function and
  436. * I found it better to rewrite it. Old function is still available.
  437. * Added also the possibility to add a description.
  438. *
  439. * @param int $parent
  440. * @param int $previous
  441. * @param string $type
  442. * @param int resource ID (ref)
  443. * @param string $title
  444. * @param string $description
  445. * @param int $prerequisites
  446. * @param int $max_time_allowed
  447. * @param int $userId
  448. *
  449. * @return int
  450. */
  451. public function add_item(
  452. $parent,
  453. $previous,
  454. $type = 'dir',
  455. $id,
  456. $title,
  457. $description,
  458. $prerequisites = 0,
  459. $max_time_allowed = 0,
  460. $userId = 0
  461. ) {
  462. $course_id = $this->course_info['real_id'];
  463. if ($this->debug > 0) {
  464. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  465. }
  466. if (empty($course_id)) {
  467. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  468. $this->course_info = api_get_course_info($this->cc);
  469. $course_id = $this->course_info['real_id'];
  470. }
  471. $userId = empty($userId) ? api_get_user_id() : $userId;
  472. $sessionId = api_get_session_id();
  473. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  474. $_course = $this->course_info;
  475. $parent = intval($parent);
  476. $previous = intval($previous);
  477. $id = intval($id);
  478. $max_time_allowed = htmlentities($max_time_allowed);
  479. if (empty($max_time_allowed)) {
  480. $max_time_allowed = 0;
  481. }
  482. $sql = "SELECT COUNT(id) AS num
  483. FROM $tbl_lp_item
  484. WHERE
  485. c_id = $course_id AND
  486. lp_id = " . $this->get_id() . " AND
  487. parent_item_id = " . $parent;
  488. $res_count = Database::query($sql);
  489. $row = Database :: fetch_array($res_count);
  490. $num = $row['num'];
  491. if ($num > 0) {
  492. if ($previous == 0) {
  493. $sql = "SELECT id, next_item_id, display_order
  494. FROM " . $tbl_lp_item . "
  495. WHERE
  496. c_id = $course_id AND
  497. lp_id = " . $this->get_id() . " AND
  498. parent_item_id = " . $parent . " AND
  499. previous_item_id = 0 OR
  500. previous_item_id=" . $parent;
  501. $result = Database::query($sql);
  502. $row = Database :: fetch_array($result);
  503. $tmp_previous = 0;
  504. $next = $row['id'];
  505. $display_order = 0;
  506. } else {
  507. $previous = (int) $previous;
  508. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  509. FROM $tbl_lp_item
  510. WHERE
  511. c_id = $course_id AND
  512. lp_id = " . $this->get_id() . " AND
  513. id = " . $previous;
  514. $result = Database::query($sql);
  515. $row = Database:: fetch_array($result);
  516. $tmp_previous = $row['id'];
  517. $next = $row['next_item_id'];
  518. $display_order = $row['display_order'];
  519. }
  520. } else {
  521. $tmp_previous = 0;
  522. $next = 0;
  523. $display_order = 0;
  524. }
  525. $id = intval($id);
  526. $typeCleaned = Database::escape_string($type);
  527. if ($type == 'quiz') {
  528. $sql = 'SELECT SUM(ponderation)
  529. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  530. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  531. ON
  532. quiz_question.id = quiz_rel_question.question_id AND
  533. quiz_question.c_id = quiz_rel_question.c_id
  534. WHERE
  535. quiz_rel_question.exercice_id = '.$id." AND
  536. quiz_question.c_id = $course_id AND
  537. quiz_rel_question.c_id = $course_id ";
  538. $rsQuiz = Database::query($sql);
  539. $max_score = Database :: result($rsQuiz, 0, 0);
  540. // Disabling the exercise if we add it inside a LP
  541. $exercise = new Exercise($course_id);
  542. $exercise->read($id);
  543. $exercise->disable();
  544. $exercise->save();
  545. } else {
  546. $max_score = 100;
  547. }
  548. $params = array(
  549. "c_id" => $course_id,
  550. "lp_id" => $this->get_id(),
  551. "item_type" => $typeCleaned,
  552. "ref" => '',
  553. "title" => $title,
  554. "description" => $description,
  555. "path" => $id,
  556. "max_score" => $max_score,
  557. "parent_item_id" => $parent,
  558. "previous_item_id" => $previous,
  559. "next_item_id" => intval($next),
  560. "display_order" => $display_order + 1,
  561. "prerequisite" => $prerequisites,
  562. "max_time_allowed" => $max_time_allowed,
  563. 'min_score' => 0,
  564. 'launch_data' => ''
  565. );
  566. if ($prerequisites != 0) {
  567. $params['prerequisite'] = $prerequisites;
  568. }
  569. $new_item_id = Database::insert($tbl_lp_item, $params);
  570. if ($this->debug > 2) {
  571. error_log('New LP - Inserting dir/chapter: ' . $new_item_id, 0);
  572. }
  573. if ($new_item_id) {
  574. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  575. Database::query($sql);
  576. // Update the item that should come after the new item.
  577. $sql = " UPDATE $tbl_lp_item SET
  578. previous_item_id = $new_item_id,
  579. next_item_id = $new_item_id,
  580. id = $new_item_id
  581. WHERE iid = $new_item_id";
  582. Database::query($sql);
  583. // Update all the items after the new item.
  584. $sql = "UPDATE " . $tbl_lp_item . "
  585. SET display_order = display_order + 1
  586. WHERE
  587. c_id = $course_id AND
  588. lp_id = " . $this->get_id() . " AND
  589. id <> " . $new_item_id . " AND
  590. parent_item_id = " . $parent . " AND
  591. display_order > " . $display_order;
  592. Database::query($sql);
  593. // Update the item that should come after the new item.
  594. $sql = "UPDATE " . $tbl_lp_item . "
  595. SET ref = " . $new_item_id . "
  596. WHERE c_id = $course_id AND id = " . $new_item_id;
  597. Database::query($sql);
  598. // Upload audio.
  599. if (!empty($_FILES['mp3']['name'])) {
  600. // Create the audio folder if it does not exist yet.
  601. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  602. if (!is_dir($filepath . 'audio')) {
  603. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  604. $audio_id = add_document(
  605. $_course,
  606. '/audio',
  607. 'folder',
  608. 0,
  609. 'audio',
  610. '',
  611. 0,
  612. true,
  613. null,
  614. $sessionId,
  615. $userId
  616. );
  617. api_item_property_update(
  618. $_course,
  619. TOOL_DOCUMENT,
  620. $audio_id,
  621. 'FolderCreated',
  622. $userId,
  623. null,
  624. null,
  625. null,
  626. null,
  627. $sessionId
  628. );
  629. api_item_property_update(
  630. $_course,
  631. TOOL_DOCUMENT,
  632. $audio_id,
  633. 'invisible',
  634. $userId,
  635. null,
  636. null,
  637. null,
  638. null,
  639. $sessionId
  640. );
  641. }
  642. $file_path = handle_uploaded_document(
  643. $_course,
  644. $_FILES['mp3'],
  645. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  646. '/audio',
  647. $userId,
  648. '',
  649. '',
  650. '',
  651. '',
  652. false
  653. );
  654. // Getting the filename only.
  655. $file_components = explode('/', $file_path);
  656. $file = $file_components[count($file_components) - 1];
  657. // Store the mp3 file in the lp_item table.
  658. $sql = "UPDATE $tbl_lp_item SET
  659. audio = '" . Database::escape_string($file) . "'
  660. WHERE id = '" . intval($new_item_id) . "'";
  661. Database::query($sql);
  662. }
  663. }
  664. return $new_item_id;
  665. }
  666. /**
  667. * Static admin function allowing addition of a learnpath to a course.
  668. * @param string Course code
  669. * @param string Learnpath name
  670. * @param string Learnpath description string, if provided
  671. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  672. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  673. * @param string Zip file containing the learnpath or directory containing the learnpath
  674. * @return integer The new learnpath ID on success, 0 on failure
  675. */
  676. public static function add_lp(
  677. $courseCode,
  678. $name,
  679. $description = '',
  680. $learnpath = 'guess',
  681. $origin = 'zip',
  682. $zipname = '',
  683. $publicated_on = '',
  684. $expired_on = '',
  685. $categoryId = 0,
  686. $userId = 0
  687. ) {
  688. global $charset;
  689. if (!empty($courseCode)) {
  690. $courseInfo = api_get_course_info($courseCode);
  691. $course_id = $courseInfo['real_id'];
  692. } else {
  693. $course_id = api_get_course_int_id();
  694. $courseInfo = api_get_course_info();
  695. }
  696. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  697. // Check course code exists.
  698. // Check lp_name doesn't exist, otherwise append something.
  699. $i = 0;
  700. $name = Database::escape_string($name);
  701. $categoryId = intval($categoryId);
  702. // Session id.
  703. $session_id = api_get_session_id();
  704. $userId = empty($userId) ? api_get_user_id() : $userId;
  705. $check_name = "SELECT * FROM $tbl_lp
  706. WHERE c_id = $course_id AND name = '$name'";
  707. $res_name = Database::query($check_name);
  708. if (empty($publicated_on)) {
  709. //by default the publication date is the same that the creation date
  710. //The behaviour above was changed due BT#2800
  711. global $_custom;
  712. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  713. $publicated_on = '';
  714. } else {
  715. $publicated_on = api_get_utc_datetime();
  716. }
  717. } else {
  718. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  719. }
  720. if (empty($expired_on)) {
  721. $expired_on = null;
  722. } else {
  723. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  724. }
  725. while (Database :: num_rows($res_name)) {
  726. // There is already one such name, update the current one a bit.
  727. $i++;
  728. $name = $name . ' - ' . $i;
  729. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  730. $res_name = Database::query($check_name);
  731. }
  732. // New name does not exist yet; keep it.
  733. // Escape description.
  734. // Kevin: added htmlentities().
  735. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  736. $type = 1;
  737. switch ($learnpath) {
  738. case 'guess':
  739. break;
  740. case 'dokeos':
  741. case 'chamilo':
  742. $type = 1;
  743. break;
  744. case 'aicc':
  745. break;
  746. }
  747. switch ($origin) {
  748. case 'zip':
  749. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  750. break;
  751. case 'manual':
  752. default:
  753. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  754. $res_max = Database::query($get_max);
  755. if (Database :: num_rows($res_max) < 1) {
  756. $dsp = 1;
  757. } else {
  758. $row = Database :: fetch_array($res_max);
  759. $dsp = $row[0] + 1;
  760. }
  761. $params = [
  762. 'c_id' => $course_id,
  763. 'lp_type' => $type,
  764. 'name' => $name,
  765. 'description' => $description,
  766. 'path' => '',
  767. 'default_view_mod' => 'embedded',
  768. 'default_encoding' => 'UTF-8',
  769. 'display_order' => $dsp,
  770. 'content_maker' => 'Chamilo',
  771. 'content_local' => 'local',
  772. 'js_lib' => '',
  773. 'session_id' => $session_id,
  774. 'created_on' => api_get_utc_datetime(),
  775. 'modified_on' => api_get_utc_datetime(),
  776. 'publicated_on' => $publicated_on,
  777. 'expired_on' => $expired_on,
  778. 'category_id' => $categoryId,
  779. 'force_commit' => 0,
  780. 'content_license' => '',
  781. 'debug' => 0,
  782. 'theme' => '',
  783. 'preview_image' => '',
  784. 'author' => '',
  785. 'prerequisite' => 0,
  786. 'hide_toc_frame' => 0,
  787. 'seriousgame_mode' => 0,
  788. 'autolaunch' => 0,
  789. 'max_attempts' => 0,
  790. 'subscribe_users' => 0,
  791. 'accumulate_scorm_time' => 1
  792. ];
  793. $id = Database::insert($tbl_lp, $params);
  794. if ($id > 0) {
  795. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  796. Database::query($sql);
  797. // Insert into item_property.
  798. api_item_property_update(
  799. $courseInfo,
  800. TOOL_LEARNPATH,
  801. $id,
  802. 'LearnpathAdded',
  803. $userId
  804. );
  805. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  806. return $id;
  807. }
  808. break;
  809. }
  810. }
  811. /**
  812. * Auto completes the parents of an item in case it's been completed or passed
  813. * @param integer $item Optional ID of the item from which to look for parents
  814. */
  815. public function autocomplete_parents($item)
  816. {
  817. $debug = $this->debug;
  818. if ($debug) {
  819. error_log('Learnpath::autocomplete_parents()', 0);
  820. }
  821. if (empty($item)) {
  822. $item = $this->current;
  823. }
  824. $currentItem = $this->getItem($item);
  825. if ($currentItem) {
  826. $parent_id = $currentItem->get_parent();
  827. $parent = $this->getItem($parent_id);
  828. if ($parent) {
  829. // if $item points to an object and there is a parent.
  830. if ($debug) {
  831. error_log(
  832. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  833. 0
  834. );
  835. }
  836. // New experiment including failed and browsed in completed status.
  837. //$current_status = $currentItem->get_status();
  838. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  839. // Fixes chapter auto complete
  840. if (true) {
  841. // If the current item is completed or passes or succeeded.
  842. $updateParentStatus = true;
  843. if ($debug) {
  844. error_log('Status of current item is alright', 0);
  845. }
  846. foreach ($parent->get_children() as $childItemId) {
  847. $childItem = $this->getItem($childItemId);
  848. // If children was not set try to get the info
  849. if (empty($childItem->db_item_view_id)) {
  850. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  851. }
  852. // Check all his brothers (parent's children) for completion status.
  853. if ($childItemId != $item) {
  854. if ($debug) {
  855. error_log(
  856. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  857. 0
  858. );
  859. }
  860. // Trying completing parents of failed and browsed items as well.
  861. if ($childItem->status_is(
  862. array(
  863. 'completed',
  864. 'passed',
  865. 'succeeded',
  866. 'browsed',
  867. 'failed'
  868. )
  869. )
  870. ) {
  871. // Keep completion status to true.
  872. continue;
  873. } else {
  874. if ($debug > 2) {
  875. error_log(
  876. '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,
  877. 0
  878. );
  879. }
  880. $updateParentStatus = false;
  881. break;
  882. }
  883. }
  884. }
  885. if ($updateParentStatus) {
  886. // If all the children were completed:
  887. $parent->set_status('completed');
  888. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  889. // Force the status to "completed"
  890. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  891. $this->update_queue[$parent->get_id()] = 'completed';
  892. if ($debug) {
  893. error_log(
  894. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  895. print_r($this->update_queue, 1),
  896. 0
  897. );
  898. }
  899. // Recursive call.
  900. $this->autocomplete_parents($parent->get_id());
  901. }
  902. }
  903. } else {
  904. if ($debug) {
  905. error_log("Parent #$parent_id does not exists");
  906. }
  907. }
  908. } else {
  909. if ($debug) {
  910. error_log("#$item is an item that doesn't have parents");
  911. }
  912. }
  913. }
  914. /**
  915. * Auto saves the current results into the database for the whole learnpath
  916. */
  917. public function autosave()
  918. {
  919. if ($this->debug > 0) {
  920. error_log('New LP - In learnpath::autosave()', 0);
  921. }
  922. // TODO: Add save operations for the learnpath itself.
  923. }
  924. /**
  925. * Closes the current resource
  926. *
  927. * Stops the timer
  928. * Saves into the database if required
  929. * Clears the current resource data from this object
  930. * @return boolean True on success, false on failure
  931. */
  932. public function close()
  933. {
  934. if ($this->debug > 0) {
  935. error_log('New LP - In learnpath::close()', 0);
  936. }
  937. if (empty ($this->lp_id)) {
  938. $this->error = 'Trying to close this learnpath but no ID is set';
  939. return false;
  940. }
  941. $this->current_time_stop = time();
  942. $this->ordered_items = array();
  943. $this->index = 0;
  944. unset ($this->lp_id);
  945. //unset other stuff
  946. return true;
  947. }
  948. /**
  949. * Static admin function allowing removal of a learnpath
  950. * @param array $courseInfo
  951. * @param integer Learnpath ID
  952. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  953. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  954. */
  955. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  956. {
  957. $course_id = api_get_course_int_id();
  958. if (!empty($courseInfo)) {
  959. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  960. }
  961. // TODO: Implement a way of getting this to work when the current object is not set.
  962. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  963. // If an ID is specifically given and the current LP is not the same, prevent delete.
  964. if (!empty ($id) && ($id != $this->lp_id)) {
  965. return false;
  966. }
  967. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  968. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  969. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  970. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  971. // Delete lp item id.
  972. foreach ($this->items as $id => $dummy) {
  973. $sql = "DELETE FROM $lp_item_view
  974. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  975. Database::query($sql);
  976. }
  977. // Proposed by Christophe (nickname: clefevre)
  978. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  979. Database::query($sql);
  980. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  981. Database::query($sql);
  982. self::toggle_publish($this->lp_id, 'i');
  983. if ($this->type == 2 || $this->type == 3) {
  984. // This is a scorm learning path, delete the files as well.
  985. $sql = "SELECT path FROM $lp
  986. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  987. $res = Database::query($sql);
  988. if (Database :: num_rows($res) > 0) {
  989. $row = Database :: fetch_array($res);
  990. $path = $row['path'];
  991. $sql = "SELECT id FROM $lp
  992. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  993. $res = Database::query($sql);
  994. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  995. if ($this->debug > 2) {
  996. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  997. }
  998. } else {
  999. // No other LP uses that directory, delete it.
  1000. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  1001. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  1002. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  1003. if ($this->debug > 2) {
  1004. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1005. }
  1006. // Proposed by Christophe (clefevre).
  1007. if (strcmp(substr($path, -2), "/.") == 0) {
  1008. $path = substr($path, 0, -1); // Remove "." at the end.
  1009. }
  1010. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1011. rmdirr($course_scorm_dir . $path);
  1012. }
  1013. }
  1014. }
  1015. }
  1016. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  1017. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1018. // Delete tools
  1019. $sql = "DELETE FROM $tbl_tool
  1020. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1021. Database::query($sql);
  1022. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1023. Database::query($sql);
  1024. // Updates the display order of all lps.
  1025. $this->update_display_order();
  1026. api_item_property_update(
  1027. api_get_course_info(),
  1028. TOOL_LEARNPATH,
  1029. $this->lp_id,
  1030. 'delete',
  1031. api_get_user_id()
  1032. );
  1033. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1034. api_get_course_id(),
  1035. 4,
  1036. $id,
  1037. api_get_session_id()
  1038. );
  1039. if ($link_info !== false) {
  1040. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1041. }
  1042. if (api_get_setting('search_enabled') == 'true') {
  1043. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1044. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1045. }
  1046. }
  1047. /**
  1048. * Removes all the children of one item - dangerous!
  1049. * @param integer $id Element ID of which children have to be removed
  1050. * @return integer Total number of children removed
  1051. */
  1052. public function delete_children_items($id)
  1053. {
  1054. $course_id = $this->course_info['real_id'];
  1055. if ($this->debug > 0) {
  1056. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1057. }
  1058. $num = 0;
  1059. if (empty ($id) || $id != strval(intval($id))) {
  1060. return false;
  1061. }
  1062. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1063. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1064. $res = Database::query($sql);
  1065. while ($row = Database :: fetch_array($res)) {
  1066. $num += $this->delete_children_items($row['id']);
  1067. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1068. Database::query($sql_del);
  1069. $num++;
  1070. }
  1071. return $num;
  1072. }
  1073. /**
  1074. * Removes an item from the current learnpath
  1075. * @param integer $id Elem ID (0 if first)
  1076. * @param integer $remove Whether to remove the resource/data from the
  1077. * system or leave it (default: 'keep', others 'remove')
  1078. * @return integer Number of elements moved
  1079. * @todo implement resource removal
  1080. */
  1081. public function delete_item($id, $remove = 'keep')
  1082. {
  1083. $course_id = api_get_course_int_id();
  1084. if ($this->debug > 0) {
  1085. error_log('New LP - In learnpath::delete_item()', 0);
  1086. }
  1087. // TODO: Implement the resource removal.
  1088. if (empty ($id) || $id != strval(intval($id))) {
  1089. return false;
  1090. }
  1091. // First select item to get previous, next, and display order.
  1092. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1093. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1094. $res_sel = Database::query($sql_sel);
  1095. if (Database :: num_rows($res_sel) < 1) {
  1096. return false;
  1097. }
  1098. $row = Database :: fetch_array($res_sel);
  1099. $previous = $row['previous_item_id'];
  1100. $next = $row['next_item_id'];
  1101. $display = $row['display_order'];
  1102. $parent = $row['parent_item_id'];
  1103. $lp = $row['lp_id'];
  1104. // Delete children items.
  1105. $num = $this->delete_children_items($id);
  1106. if ($this->debug > 2) {
  1107. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1108. }
  1109. // Now delete the item.
  1110. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1111. if ($this->debug > 2) {
  1112. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1113. }
  1114. Database::query($sql_del);
  1115. // Now update surrounding items.
  1116. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1117. WHERE c_id = ".$course_id." AND id = $previous";
  1118. Database::query($sql_upd);
  1119. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1120. WHERE c_id = ".$course_id." AND id = $next";
  1121. Database::query($sql_upd);
  1122. // Now update all following items with new display order.
  1123. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1124. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1125. Database::query($sql_all);
  1126. //Removing prerequisites since the item will not longer exist
  1127. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1128. Database::query($sql_all);
  1129. // Remove from search engine if enabled.
  1130. if (api_get_setting('search_enabled') == 'true') {
  1131. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1132. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1133. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1134. $res = Database::query($sql);
  1135. if (Database :: num_rows($res) > 0) {
  1136. $row2 = Database :: fetch_array($res);
  1137. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1138. $di = new ChamiloIndexer();
  1139. $di->remove_document((int) $row2['search_did']);
  1140. }
  1141. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1142. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1143. Database::query($sql);
  1144. }
  1145. }
  1146. /**
  1147. * Updates an item's content in place
  1148. * @param integer $id Element ID
  1149. * @param integer $parent Parent item ID
  1150. * @param integer $previous Previous item ID
  1151. * @param string $title Item title
  1152. * @param string $description Item description
  1153. * @param string $prerequisites Prerequisites (optional)
  1154. * @param array $audio The array resulting of the $_FILES[mp3] element
  1155. * @param int $max_time_allowed
  1156. * @param string $url
  1157. * @return boolean True on success, false on error
  1158. */
  1159. public function edit_item(
  1160. $id,
  1161. $parent,
  1162. $previous,
  1163. $title,
  1164. $description,
  1165. $prerequisites = '0',
  1166. $audio = array(),
  1167. $max_time_allowed = 0,
  1168. $url = ''
  1169. ) {
  1170. $course_id = api_get_course_int_id();
  1171. $_course = api_get_course_info();
  1172. if ($this->debug > 0) {
  1173. error_log('New LP - In learnpath::edit_item()', 0);
  1174. }
  1175. if (empty ($max_time_allowed)) {
  1176. $max_time_allowed = 0;
  1177. }
  1178. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1179. return false;
  1180. }
  1181. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1182. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1183. $res_select = Database::query($sql_select);
  1184. $row_select = Database :: fetch_array($res_select);
  1185. $audio_update_sql = '';
  1186. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1187. // Create the audio folder if it does not exist yet.
  1188. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1189. if (!is_dir($filepath . 'audio')) {
  1190. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1191. $audio_id = add_document(
  1192. $_course,
  1193. '/audio',
  1194. 'folder',
  1195. 0,
  1196. 'audio'
  1197. );
  1198. api_item_property_update(
  1199. $_course,
  1200. TOOL_DOCUMENT,
  1201. $audio_id,
  1202. 'FolderCreated',
  1203. api_get_user_id(),
  1204. null,
  1205. null,
  1206. null,
  1207. null,
  1208. api_get_session_id()
  1209. );
  1210. api_item_property_update(
  1211. $_course,
  1212. TOOL_DOCUMENT,
  1213. $audio_id,
  1214. 'invisible',
  1215. api_get_user_id(),
  1216. null,
  1217. null,
  1218. null,
  1219. null,
  1220. api_get_session_id()
  1221. );
  1222. }
  1223. // Upload file in documents.
  1224. $pi = pathinfo($audio['name']);
  1225. if ($pi['extension'] == 'mp3') {
  1226. $c_det = api_get_course_info($this->cc);
  1227. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1228. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1229. $path = substr($path, 7);
  1230. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1231. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1232. }
  1233. }
  1234. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1235. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1236. // TODO: htmlspecialchars to be checked for encoding related problems.
  1237. if ($same_parent && $same_previous) {
  1238. // Only update title and description.
  1239. $sql = "UPDATE " . $tbl_lp_item . "
  1240. SET title = '" . Database::escape_string($title) . "',
  1241. prerequisite = '" . $prerequisites . "',
  1242. description = '" . Database::escape_string($description) . "'
  1243. " . $audio_update_sql . ",
  1244. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1245. WHERE c_id = ".$course_id." AND id = " . $id;
  1246. Database::query($sql);
  1247. } else {
  1248. $old_parent = $row_select['parent_item_id'];
  1249. $old_previous = $row_select['previous_item_id'];
  1250. $old_next = $row_select['next_item_id'];
  1251. $old_order = $row_select['display_order'];
  1252. $old_prerequisite = $row_select['prerequisite'];
  1253. $old_max_time_allowed = $row_select['max_time_allowed'];
  1254. /* BEGIN -- virtually remove the current item id */
  1255. /* for the next and previous item it is like the current item doesn't exist anymore */
  1256. if ($old_previous != 0) {
  1257. // Next
  1258. $sql = "UPDATE " . $tbl_lp_item . "
  1259. SET next_item_id = " . $old_next . "
  1260. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1261. Database::query($sql);
  1262. }
  1263. if ($old_next != 0) {
  1264. // Previous
  1265. $sql = "UPDATE " . $tbl_lp_item . "
  1266. SET previous_item_id = " . $old_previous . "
  1267. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1268. Database::query($sql);
  1269. }
  1270. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1271. $sql = "UPDATE " . $tbl_lp_item . "
  1272. SET display_order = display_order - 1
  1273. WHERE
  1274. c_id = ".$course_id." AND
  1275. display_order > " . $old_order . " AND
  1276. lp_id = " . $this->lp_id . " AND
  1277. parent_item_id = " . $old_parent;
  1278. Database::query($sql);
  1279. /* END -- virtually remove the current item id */
  1280. /* BEGIN -- update the current item id to his new location */
  1281. if ($previous == 0) {
  1282. // Select the data of the item that should come after the current item.
  1283. $sql = "SELECT id, display_order
  1284. FROM " . $tbl_lp_item . "
  1285. WHERE
  1286. c_id = ".$course_id." AND
  1287. lp_id = " . $this->lp_id . " AND
  1288. parent_item_id = " . $parent . " AND
  1289. previous_item_id = " . $previous;
  1290. $res_select_old = Database::query($sql);
  1291. $row_select_old = Database::fetch_array($res_select_old);
  1292. // If the new parent didn't have children before.
  1293. if (Database :: num_rows($res_select_old) == 0) {
  1294. $new_next = 0;
  1295. $new_order = 1;
  1296. } else {
  1297. $new_next = $row_select_old['id'];
  1298. $new_order = $row_select_old['display_order'];
  1299. }
  1300. } else {
  1301. // Select the data of the item that should come before the current item.
  1302. $sql = "SELECT next_item_id, display_order
  1303. FROM " . $tbl_lp_item . "
  1304. WHERE c_id = ".$course_id." AND id = " . $previous;
  1305. $res_select_old = Database::query($sql);
  1306. $row_select_old = Database :: fetch_array($res_select_old);
  1307. $new_next = $row_select_old['next_item_id'];
  1308. $new_order = $row_select_old['display_order'] + 1;
  1309. }
  1310. // TODO: htmlspecialchars to be checked for encoding related problems.
  1311. // Update the current item with the new data.
  1312. $sql = "UPDATE " . $tbl_lp_item . "
  1313. SET
  1314. title = '" . Database::escape_string($title) . "',
  1315. description = '" . Database::escape_string($description) . "',
  1316. parent_item_id = " . $parent . ",
  1317. previous_item_id = " . $previous . ",
  1318. next_item_id = " . $new_next . ",
  1319. display_order = " . $new_order . "
  1320. " . $audio_update_sql . "
  1321. WHERE c_id = ".$course_id." AND id = " . $id;
  1322. Database::query($sql);
  1323. if ($previous != 0) {
  1324. // Update the previous item's next_item_id.
  1325. $sql = "UPDATE " . $tbl_lp_item . "
  1326. SET next_item_id = " . $id . "
  1327. WHERE c_id = ".$course_id." AND id = " . $previous;
  1328. Database::query($sql);
  1329. }
  1330. if ($new_next != 0) {
  1331. // Update the next item's previous_item_id.
  1332. $sql = "UPDATE " . $tbl_lp_item . "
  1333. SET previous_item_id = " . $id . "
  1334. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1335. Database::query($sql);
  1336. }
  1337. if ($old_prerequisite != $prerequisites) {
  1338. $sql = "UPDATE " . $tbl_lp_item . "
  1339. SET prerequisite = '" . $prerequisites . "'
  1340. WHERE c_id = ".$course_id." AND id = " . $id;
  1341. Database::query($sql);
  1342. }
  1343. if ($old_max_time_allowed != $max_time_allowed) {
  1344. // update max time allowed
  1345. $sql = "UPDATE " . $tbl_lp_item . "
  1346. SET max_time_allowed = " . $max_time_allowed . "
  1347. WHERE c_id = ".$course_id." AND id = " . $id;
  1348. Database::query($sql);
  1349. }
  1350. // Update all the items with the same or a bigger display_order than the current item.
  1351. $sql = "UPDATE " . $tbl_lp_item . "
  1352. SET display_order = display_order + 1
  1353. WHERE
  1354. c_id = ".$course_id." AND
  1355. lp_id = " . $this->get_id() . " AND
  1356. id <> " . $id . " AND
  1357. parent_item_id = " . $parent . " AND
  1358. display_order >= " . $new_order;
  1359. Database::query($sql);
  1360. }
  1361. if ($row_select['item_type'] == 'link') {
  1362. $link = new Link();
  1363. $linkId = $row_select['path'];
  1364. $link->updateLink($linkId, $url);
  1365. }
  1366. }
  1367. /**
  1368. * Updates an item's prereq in place
  1369. * @param integer $id Element ID
  1370. * @param string $prerequisite_id Prerequisite Element ID
  1371. * @param int $mastery_score Prerequisite min score
  1372. * @param int $max_score Prerequisite max score
  1373. *
  1374. * @return boolean True on success, false on error
  1375. */
  1376. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1377. {
  1378. $course_id = api_get_course_int_id();
  1379. if ($this->debug > 0) {
  1380. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1381. }
  1382. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1383. return false;
  1384. }
  1385. $prerequisite_id = intval($prerequisite_id);
  1386. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1387. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1388. $mastery_score = 0;
  1389. }
  1390. if (!is_numeric($max_score) || $max_score < 0) {
  1391. $max_score = 100;
  1392. }
  1393. /*if ($mastery_score > $max_score) {
  1394. $max_score = $mastery_score;
  1395. }*/
  1396. if (!is_numeric($prerequisite_id)) {
  1397. $prerequisite_id = 'NULL';
  1398. }
  1399. $mastery_score = floatval($mastery_score);
  1400. $max_score = floatval($max_score);
  1401. $sql = " UPDATE $tbl_lp_item
  1402. SET
  1403. prerequisite = $prerequisite_id ,
  1404. prerequisite_min_score = $mastery_score ,
  1405. prerequisite_max_score = $max_score
  1406. WHERE c_id = $course_id AND id = $id";
  1407. Database::query($sql);
  1408. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1409. // Will this be enough to ensure unicity?
  1410. /*$sql = " UPDATE $tbl_lp_item
  1411. SET mastery_score = $mastery_score
  1412. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1413. Database::query($sql);*/
  1414. }
  1415. // TODO: Update the item object (can be ignored for now because refreshed).
  1416. return true;
  1417. }
  1418. /**
  1419. * Static admin function exporting a learnpath into a zip file
  1420. * @param string Export type (scorm, zip, cd)
  1421. * @param string Course code
  1422. * @param integer Learnpath ID
  1423. * @param string Zip file name
  1424. * @return string Zip file path (or false on error)
  1425. */
  1426. public function export_lp($type, $course, $id, $zipname)
  1427. {
  1428. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1429. return false;
  1430. }
  1431. $url = '';
  1432. switch ($type) {
  1433. case 'scorm':
  1434. break;
  1435. case 'zip':
  1436. break;
  1437. case 'cdrom':
  1438. break;
  1439. }
  1440. return $url;
  1441. }
  1442. /**
  1443. * Gets all the chapters belonging to the same parent as the item/chapter given
  1444. * Can also be called as abstract method
  1445. * @param integer Item ID
  1446. * @return array A list of all the "brother items" (or an empty array on failure)
  1447. */
  1448. public function getSiblingDirectories($id)
  1449. {
  1450. $course_id = api_get_course_int_id();
  1451. if ($this->debug > 0) {
  1452. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1453. }
  1454. if (empty($id) || $id != strval(intval($id))) {
  1455. return array ();
  1456. }
  1457. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1458. $sql_parent = "SELECT * FROM $lp_item
  1459. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1460. $res_parent = Database::query($sql_parent);
  1461. if (Database :: num_rows($res_parent) > 0) {
  1462. $row_parent = Database :: fetch_array($res_parent);
  1463. $parent = $row_parent['parent_item_id'];
  1464. $sql = "SELECT * FROM $lp_item
  1465. WHERE
  1466. c_id = ".$course_id." AND
  1467. parent_item_id = $parent AND
  1468. id = $id AND
  1469. item_type='dir'
  1470. ORDER BY display_order";
  1471. $res_bros = Database::query($sql);
  1472. $list = array();
  1473. while ($row_bro = Database :: fetch_array($res_bros)) {
  1474. $list[] = $row_bro;
  1475. }
  1476. return $list;
  1477. }
  1478. return array();
  1479. }
  1480. /**
  1481. * Gets all the items belonging to the same parent as the item given
  1482. * Can also be called as abstract method
  1483. * @param integer Item ID
  1484. * @return array A list of all the "brother items" (or an empty array on failure)
  1485. */
  1486. public function get_brother_items($id)
  1487. {
  1488. $course_id = api_get_course_int_id();
  1489. if ($this->debug > 0) {
  1490. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1491. }
  1492. if (empty ($id) || $id != strval(intval($id))) {
  1493. return array();
  1494. }
  1495. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1496. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1497. $res_parent = Database::query($sql_parent);
  1498. if (Database :: num_rows($res_parent) > 0) {
  1499. $row_parent = Database :: fetch_array($res_parent);
  1500. $parent = $row_parent['parent_item_id'];
  1501. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1502. ORDER BY display_order";
  1503. $res_bros = Database::query($sql_bros);
  1504. $list = array ();
  1505. while ($row_bro = Database :: fetch_array($res_bros)) {
  1506. $list[] = $row_bro;
  1507. }
  1508. return $list;
  1509. }
  1510. return array ();
  1511. }
  1512. /**
  1513. * Get the specific prefix index terms of this learning path
  1514. * @param string $prefix
  1515. * @return array Array of terms
  1516. */
  1517. public function get_common_index_terms_by_prefix($prefix)
  1518. {
  1519. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1520. $terms = get_specific_field_values_list_by_prefix(
  1521. $prefix,
  1522. $this->cc,
  1523. TOOL_LEARNPATH,
  1524. $this->lp_id
  1525. );
  1526. $prefix_terms = array();
  1527. if (!empty($terms)) {
  1528. foreach ($terms as $term) {
  1529. $prefix_terms[] = $term['value'];
  1530. }
  1531. }
  1532. return $prefix_terms;
  1533. }
  1534. /**
  1535. * Gets the number of items currently completed
  1536. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1537. * @return integer The number of items currently completed
  1538. */
  1539. public function get_complete_items_count($failedStatusException = false)
  1540. {
  1541. if ($this->debug > 0) {
  1542. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1543. }
  1544. $i = 0;
  1545. $completedStatusList = array(
  1546. 'completed',
  1547. 'passed',
  1548. 'succeeded',
  1549. 'browsed'
  1550. );
  1551. if (!$failedStatusException) {
  1552. $completedStatusList[] = 'failed';
  1553. }
  1554. foreach ($this->items as $id => $dummy) {
  1555. // Trying failed and browsed considered "progressed" as well.
  1556. if ($this->items[$id]->status_is($completedStatusList) &&
  1557. $this->items[$id]->get_type() != 'dir'
  1558. ) {
  1559. $i++;
  1560. }
  1561. }
  1562. return $i;
  1563. }
  1564. /**
  1565. * Gets the current item ID
  1566. * @return integer The current learnpath item id
  1567. */
  1568. public function get_current_item_id()
  1569. {
  1570. $current = 0;
  1571. if ($this->debug > 0) {
  1572. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1573. }
  1574. if (!empty($this->current)) {
  1575. $current = $this->current;
  1576. }
  1577. if ($this->debug > 2) {
  1578. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1579. }
  1580. return $current;
  1581. }
  1582. /**
  1583. * Force to get the first learnpath item id
  1584. * @return integer The current learnpath item id
  1585. */
  1586. public function get_first_item_id()
  1587. {
  1588. $current = 0;
  1589. if (is_array($this->ordered_items)) {
  1590. $current = $this->ordered_items[0];
  1591. }
  1592. return $current;
  1593. }
  1594. /**
  1595. * Gets the total number of items available for viewing in this SCORM
  1596. * @return integer The total number of items
  1597. */
  1598. public function get_total_items_count()
  1599. {
  1600. if ($this->debug > 0) {
  1601. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1602. }
  1603. return count($this->items);
  1604. }
  1605. /**
  1606. * Gets the total number of items available for viewing in this SCORM but without chapters
  1607. * @return integer The total no-chapters number of items
  1608. */
  1609. public function getTotalItemsCountWithoutDirs()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1613. }
  1614. $total = 0;
  1615. $typeListNotToCount = self::getChapterTypes();
  1616. foreach ($this->items as $temp2) {
  1617. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1618. $total++;
  1619. }
  1620. }
  1621. return $total;
  1622. }
  1623. /**
  1624. * Gets the first element URL.
  1625. * @return string URL to load into the viewer
  1626. */
  1627. public function first()
  1628. {
  1629. if ($this->debug > 0) {
  1630. error_log('New LP - In learnpath::first()', 0);
  1631. error_log('$this->last_item_seen '.$this->last_item_seen);
  1632. }
  1633. // Test if the last_item_seen exists and is not a dir.
  1634. if (count($this->ordered_items) == 0) {
  1635. $this->index = 0;
  1636. }
  1637. if ($this->debug > 0) {
  1638. if (isset($this->items[$this->last_item_seen])) {
  1639. $status = $this->items[$this->last_item_seen]->get_status();
  1640. }
  1641. error_log('status '.$status);
  1642. }
  1643. if (!empty($this->last_item_seen) &&
  1644. !empty($this->items[$this->last_item_seen]) &&
  1645. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1646. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1647. //&& !$this->items[$this->last_item_seen]->is_done()
  1648. ) {
  1649. if ($this->debug > 2) {
  1650. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1651. }
  1652. $index = -1;
  1653. foreach ($this->ordered_items as $myindex => $item_id) {
  1654. if ($item_id == $this->last_item_seen) {
  1655. $index = $myindex;
  1656. break;
  1657. }
  1658. }
  1659. if ($index == -1) {
  1660. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1661. if ($this->debug > 2) {
  1662. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1663. }
  1664. return false;
  1665. } else {
  1666. $this->last = $this->last_item_seen;
  1667. $this->current = $this->last_item_seen;
  1668. $this->index = $index;
  1669. }
  1670. } else {
  1671. if ($this->debug > 2) {
  1672. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1673. }
  1674. $index = 0;
  1675. // Loop through all ordered items and stop at the first item that is
  1676. // not a directory *and* that has not been completed yet.
  1677. while ( !empty($this->ordered_items[$index]) AND
  1678. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1679. (
  1680. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1681. $this->items[$this->ordered_items[$index]]->is_done() === true
  1682. ) AND $index < $this->max_ordered_items) {
  1683. $index++;
  1684. }
  1685. $this->last = $this->current;
  1686. // current is
  1687. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1688. $this->index = $index;
  1689. if ($this->debug > 2) {
  1690. error_log('$index ' . $index);
  1691. }
  1692. if ($this->debug > 2) {
  1693. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1694. }
  1695. }
  1696. if ($this->debug > 2) {
  1697. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1698. }
  1699. }
  1700. /**
  1701. * Gets the information about an item in a format usable as JavaScript to update
  1702. * the JS API by just printing this content into the <head> section of the message frame
  1703. * @param int $item_id
  1704. * @return string
  1705. */
  1706. public function get_js_info($item_id = '')
  1707. {
  1708. if ($this->debug > 0) {
  1709. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1710. }
  1711. $info = '';
  1712. $item_id = intval($item_id);
  1713. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1714. //if item is defined, return values from DB
  1715. $oItem = $this->items[$item_id];
  1716. $info .= '<script language="javascript">';
  1717. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1718. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1719. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1720. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1721. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1722. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1723. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1724. $info .= "top.set_flag_synchronized();";
  1725. $info .= '</script>';
  1726. if ($this->debug > 2) {
  1727. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1728. }
  1729. return $info;
  1730. } else {
  1731. // If item_id is empty, just update to default SCORM data.
  1732. $info .= '<script language="javascript">';
  1733. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1734. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1735. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1736. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1737. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1738. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1739. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1740. $info .= "top.set_flag_synchronized();";
  1741. $info .= '</script>';
  1742. if ($this->debug > 2) {
  1743. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1744. }
  1745. return $info;
  1746. }
  1747. }
  1748. /**
  1749. * Gets the js library from the database
  1750. * @return string The name of the javascript library to be used
  1751. */
  1752. public function get_js_lib()
  1753. {
  1754. $lib = '';
  1755. if (!empty ($this->js_lib)) {
  1756. $lib = $this->js_lib;
  1757. }
  1758. return $lib;
  1759. }
  1760. /**
  1761. * Gets the learnpath database ID
  1762. * @return integer Learnpath ID in the lp table
  1763. */
  1764. public function get_id()
  1765. {
  1766. if (!empty ($this->lp_id)) {
  1767. return $this->lp_id;
  1768. } else {
  1769. return 0;
  1770. }
  1771. }
  1772. /**
  1773. * Gets the last element URL.
  1774. * @return string URL to load into the viewer
  1775. */
  1776. public function get_last()
  1777. {
  1778. if ($this->debug > 0) {
  1779. error_log('New LP - In learnpath::get_last()', 0);
  1780. }
  1781. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1782. if (count($this->ordered_items) > 0) {
  1783. $this->index = count($this->ordered_items) - 1;
  1784. return $this->ordered_items[$this->index];
  1785. }
  1786. return false;
  1787. }
  1788. /**
  1789. * Gets the navigation bar for the learnpath display screen
  1790. * @return string The HTML string to use as a navigation bar
  1791. */
  1792. public function get_navigation_bar($idBar = null, $display = null) {
  1793. if ($this->debug > 0) {
  1794. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1795. }
  1796. if(empty($idBar)){
  1797. $idBar='control-top';
  1798. }
  1799. /* if(empty($display)){
  1800. $display='display:block';
  1801. } */
  1802. $navbar = null;
  1803. $lp_id = $this->lp_id;
  1804. $mycurrentitemid = $this->get_current_item_id();
  1805. if ($this->mode == 'fullscreen') {
  1806. $navbar = '
  1807. <span id="'.$idBar.'" class="buttons">
  1808. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link">
  1809. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1810. </a>
  1811. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1812. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1813. </a>
  1814. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1815. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1816. </a>
  1817. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1818. <span class="fa fa-columns"></span><span class="sr-only">' . get_lang('ScormExitFullScreen') . '</span>
  1819. </a>
  1820. </span>';
  1821. } else {
  1822. $navbar = '
  1823. <span id="'.$idBar.'" class="buttons text-right">
  1824. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link">
  1825. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1826. </a>
  1827. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1828. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1829. </a>
  1830. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1831. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1832. </a>
  1833. </span>';
  1834. }
  1835. return $navbar;
  1836. }
  1837. /**
  1838. * Gets the next resource in queue (url).
  1839. * @return string URL to load into the viewer
  1840. */
  1841. public function get_next_index()
  1842. {
  1843. if ($this->debug > 0) {
  1844. error_log('New LP - In learnpath::get_next_index()', 0);
  1845. }
  1846. // TODO
  1847. $index = $this->index;
  1848. $index++;
  1849. if ($this->debug > 2) {
  1850. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1851. }
  1852. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1853. $index++;
  1854. if ($index == $this->max_ordered_items){
  1855. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1856. return $this->index;
  1857. } else {
  1858. return $index;
  1859. }
  1860. }
  1861. }
  1862. if (empty ($this->ordered_items[$index])) {
  1863. return $this->index;
  1864. }
  1865. if ($this->debug > 2) {
  1866. error_log('New LP - index is now ' . $index, 0);
  1867. }
  1868. return $index;
  1869. }
  1870. /**
  1871. * Gets item_id for the next element
  1872. * @return integer Next item (DB) ID
  1873. */
  1874. public function get_next_item_id()
  1875. {
  1876. if ($this->debug > 0) {
  1877. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1878. }
  1879. $new_index = $this->get_next_index();
  1880. if (!empty ($new_index)) {
  1881. if (isset ($this->ordered_items[$new_index])) {
  1882. if ($this->debug > 2) {
  1883. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1884. }
  1885. return $this->ordered_items[$new_index];
  1886. }
  1887. }
  1888. if ($this->debug > 2) {
  1889. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1890. }
  1891. return 0;
  1892. }
  1893. /**
  1894. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1895. *
  1896. * Generally, the package provided is in the form of a zip file, so the function
  1897. * has been written to test a zip file. If not a zip, the function will return the
  1898. * default return value: ''
  1899. * @param string the path to the file
  1900. * @param string the original name of the file
  1901. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1902. */
  1903. public static function get_package_type($file_path, $file_name)
  1904. {
  1905. // Get name of the zip file without the extension.
  1906. $file_info = pathinfo($file_name);
  1907. $filename = $file_info['basename']; // Name including extension.
  1908. $extension = $file_info['extension']; // Extension only.
  1909. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1910. 'dll',
  1911. 'exe'
  1912. ))) {
  1913. return 'oogie';
  1914. }
  1915. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1916. 'dll',
  1917. 'exe'
  1918. ))) {
  1919. return 'woogie';
  1920. }
  1921. // Filename without its extension.
  1922. $file_base_name = str_replace('.' . $extension, '', $filename);
  1923. $zipFile = new PclZip($file_path);
  1924. // Check the zip content (real size and file extension).
  1925. $zipContentArray = $zipFile->listContent();
  1926. $package_type = '';
  1927. $at_root = false;
  1928. $manifest = '';
  1929. $aicc_match_crs = 0;
  1930. $aicc_match_au = 0;
  1931. $aicc_match_des = 0;
  1932. $aicc_match_cst = 0;
  1933. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1934. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1935. foreach ($zipContentArray as $thisContent) {
  1936. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1937. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1938. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1939. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1940. $package_type = 'scorm';
  1941. break; // Exit the foreach loop.
  1942. } elseif (
  1943. preg_match('/aicc\//i', $thisContent['filename']) ||
  1944. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1945. ) {
  1946. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1947. switch ($ext) {
  1948. case 'crs':
  1949. $aicc_match_crs = 1;
  1950. break;
  1951. case 'au':
  1952. $aicc_match_au = 1;
  1953. break;
  1954. case 'des':
  1955. $aicc_match_des = 1;
  1956. break;
  1957. case 'cst':
  1958. $aicc_match_cst = 1;
  1959. break;
  1960. default:
  1961. break;
  1962. }
  1963. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1964. } else {
  1965. $package_type = '';
  1966. }
  1967. }
  1968. }
  1969. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1970. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1971. $package_type = 'aicc';
  1972. }
  1973. return $package_type;
  1974. }
  1975. /**
  1976. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1977. * @return string URL to load into the viewer
  1978. */
  1979. public function get_previous_index()
  1980. {
  1981. if ($this->debug > 0) {
  1982. error_log('New LP - In learnpath::get_previous_index()', 0);
  1983. }
  1984. $index = $this->index;
  1985. if (isset ($this->ordered_items[$index -1])) {
  1986. $index--;
  1987. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1988. $index--;
  1989. if ($index < 0) {
  1990. return $this->index;
  1991. }
  1992. }
  1993. } else {
  1994. if ($this->debug > 2) {
  1995. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1996. }
  1997. // There is no previous item.
  1998. }
  1999. return $index;
  2000. }
  2001. /**
  2002. * Gets item_id for the next element
  2003. * @return integer Previous item (DB) ID
  2004. */
  2005. public function get_previous_item_id()
  2006. {
  2007. if ($this->debug > 0) {
  2008. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2009. }
  2010. $new_index = $this->get_previous_index();
  2011. return $this->ordered_items[$new_index];
  2012. }
  2013. /**
  2014. * Gets the progress value from the progress_db attribute
  2015. * @return integer Current progress value
  2016. */
  2017. public function get_progress()
  2018. {
  2019. if ($this->debug > 0) {
  2020. error_log('New LP - In learnpath::get_progress()', 0);
  2021. }
  2022. if (!empty ($this->progress_db)) {
  2023. return $this->progress_db;
  2024. }
  2025. return 0;
  2026. }
  2027. /**
  2028. * Returns the HTML necessary to print a mediaplayer block inside a page
  2029. * @return string The mediaplayer HTML
  2030. */
  2031. public function get_mediaplayer($autostart = 'true')
  2032. {
  2033. $course_id = api_get_course_int_id();
  2034. $_course = api_get_course_info();
  2035. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2036. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2037. // Getting all the information about the item.
  2038. $sql = "SELECT * FROM $tbl_lp_item as lp
  2039. INNER JOIN $tbl_lp_item_view as lp_view
  2040. ON lp.id = lp_view.lp_item_id
  2041. WHERE
  2042. lp.id = '".$_SESSION['oLP']->current."' AND
  2043. lp.c_id = $course_id AND
  2044. lp_view.c_id = $course_id";
  2045. $result = Database::query($sql);
  2046. $row = Database::fetch_assoc($result);
  2047. $output = '';
  2048. if (!empty ($row['audio'])) {
  2049. $list = $_SESSION['oLP']->get_toc();
  2050. $type_quiz = false;
  2051. foreach($list as $toc) {
  2052. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2053. $type_quiz = true;
  2054. }
  2055. }
  2056. if ($type_quiz) {
  2057. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2058. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2059. } else {
  2060. $autostart_audio = $autostart;
  2061. }
  2062. } else {
  2063. $autostart_audio = 'true';
  2064. }
  2065. $courseInfo = api_get_course_info();
  2066. $audio = $row['audio'];
  2067. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2068. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2069. if (!file_exists($file)) {
  2070. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2071. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2072. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2073. }
  2074. $player = Display::getMediaPlayer(
  2075. $file,
  2076. array(
  2077. 'id' => 'lp_audio_media_player',
  2078. 'url' => $url,
  2079. 'autoplay' => $autostart_audio,
  2080. 'width' => '100%'
  2081. )
  2082. );
  2083. // The mp3 player.
  2084. $output = '<div id="container">';
  2085. $output .= $player;
  2086. $output .= '</div>';
  2087. }
  2088. return $output;
  2089. }
  2090. /**
  2091. * Checks if the learning path is visible for student after the progress
  2092. * of its prerequisite is completed, considering the time availability and
  2093. * the LP visibility.
  2094. * @param int $lp_id
  2095. * @param int $student_id
  2096. * @param string Course code (optional)
  2097. * @param int $sessionId
  2098. * @return bool
  2099. */
  2100. public static function is_lp_visible_for_student(
  2101. $lp_id,
  2102. $student_id,
  2103. $courseCode = null,
  2104. $sessionId = null
  2105. ) {
  2106. $lp_id = (int)$lp_id;
  2107. $courseInfo = api_get_course_info($courseCode);
  2108. $sessionId = intval($sessionId);
  2109. if (empty($sessionId)) {
  2110. $sessionId = api_get_session_id();
  2111. }
  2112. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2113. // Get current prerequisite
  2114. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2115. FROM $tbl_learnpath
  2116. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2117. $itemInfo = api_get_item_property_info(
  2118. $courseInfo['real_id'],
  2119. TOOL_LEARNPATH,
  2120. $lp_id,
  2121. $sessionId
  2122. );
  2123. // If the item was deleted.
  2124. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2125. return false;
  2126. }
  2127. $rs = Database::query($sql);
  2128. $now = time();
  2129. if (Database::num_rows($rs) > 0) {
  2130. $row = Database::fetch_array($rs, 'ASSOC');
  2131. $prerequisite = $row['prerequisite'];
  2132. $is_visible = true;
  2133. if (!empty($prerequisite)) {
  2134. $progress = self::getProgress(
  2135. $prerequisite,
  2136. $student_id,
  2137. $courseInfo['real_id'],
  2138. $sessionId
  2139. );
  2140. $progress = intval($progress);
  2141. if ($progress < 100) {
  2142. $is_visible = false;
  2143. }
  2144. }
  2145. // Also check the time availability of the LP
  2146. if ($is_visible) {
  2147. // Adding visibility restrictions
  2148. if (!empty($row['publicated_on'])) {
  2149. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2150. //api_not_allowed();
  2151. $is_visible = false;
  2152. }
  2153. }
  2154. // Blocking empty start times see BT#2800
  2155. global $_custom;
  2156. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2157. $_custom['lps_hidden_when_no_start_date']
  2158. ) {
  2159. if (empty($row['publicated_on'])) {
  2160. //api_not_allowed();
  2161. $is_visible = false;
  2162. }
  2163. }
  2164. if (!empty($row['expired_on'])) {
  2165. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2166. //api_not_allowed();
  2167. $is_visible = false;
  2168. }
  2169. }
  2170. }
  2171. // Check if the subscription users/group to a LP is ON
  2172. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2173. // Try group
  2174. $is_visible = false;
  2175. // Checking only the user visibility
  2176. $userVisibility = api_get_item_visibility(
  2177. $courseInfo,
  2178. 'learnpath',
  2179. $row['id'],
  2180. $sessionId,
  2181. $student_id,
  2182. 'LearnpathSubscription'
  2183. );
  2184. if ($userVisibility == 1) {
  2185. $is_visible = true;
  2186. } else {
  2187. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2188. if (!empty($userGroups)) {
  2189. foreach ($userGroups as $groupInfo) {
  2190. $groupId = $groupInfo['iid'];
  2191. $userVisibility = api_get_item_visibility(
  2192. $courseInfo,
  2193. 'learnpath',
  2194. $row['id'],
  2195. $sessionId,
  2196. null,
  2197. 'LearnpathSubscription',
  2198. $groupId
  2199. );
  2200. if ($userVisibility == 1) {
  2201. $is_visible = true;
  2202. break;
  2203. }
  2204. }
  2205. }
  2206. }
  2207. }
  2208. return $is_visible;
  2209. }
  2210. return false;
  2211. }
  2212. /**
  2213. * @param int $lpId
  2214. * @param int $userId
  2215. * @param int $courseId
  2216. * @param int $sessionId
  2217. * @return int
  2218. */
  2219. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2220. {
  2221. $lpId = intval($lpId);
  2222. $userId = intval($userId);
  2223. $courseId = intval($courseId);
  2224. $sessionId = intval($sessionId);
  2225. $progress = 0;
  2226. $sessionCondition = api_get_session_condition($sessionId);
  2227. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2228. $sql = "SELECT * FROM $table
  2229. WHERE
  2230. c_id = ".$courseId." AND
  2231. lp_id = $lpId AND
  2232. user_id = $userId $sessionCondition";
  2233. $res = Database::query($sql);
  2234. if (Database :: num_rows($res) > 0) {
  2235. $row = Database:: fetch_array($res);
  2236. $progress = $row['progress'];
  2237. }
  2238. return $progress;
  2239. }
  2240. /**
  2241. * Displays a progress bar
  2242. * completed so far.
  2243. * @param integer $percentage Progress value to display
  2244. * @param string $text_add Text to display near the progress value
  2245. * @return string HTML string containing the progress bar
  2246. */
  2247. public static function get_progress_bar($percentage = -1, $text_add = '')
  2248. {
  2249. $text = $percentage . $text_add;
  2250. $output = '<div class="progress">
  2251. <div id="progress_bar_value" class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2252. '. $text .'
  2253. </div>
  2254. </div>';
  2255. return $output;
  2256. }
  2257. /**
  2258. * @param string $mode can be '%' or 'abs'
  2259. * otherwise this value will be used $this->progress_bar_mode
  2260. * @return string
  2261. */
  2262. public function getProgressBar($mode = null)
  2263. {
  2264. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2265. return self::get_progress_bar($percentage, $text_add);
  2266. }
  2267. /**
  2268. * Gets the progress bar info to display inside the progress bar.
  2269. * Also used by scorm_api.php
  2270. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2271. * we display a number of completed elements per total elements
  2272. * @param integer $add Additional steps to fake as completed
  2273. * @return list array Percentage or number and symbol (% or /xx)
  2274. */
  2275. public function get_progress_bar_text($mode = '', $add = 0)
  2276. {
  2277. if ($this->debug > 0) {
  2278. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2279. }
  2280. if (empty($mode)) {
  2281. $mode = $this->progress_bar_mode;
  2282. }
  2283. $total_items = $this->getTotalItemsCountWithoutDirs();
  2284. if ($this->debug > 2) {
  2285. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2286. }
  2287. $completeItems = $this->get_complete_items_count();
  2288. if ($this->debug > 2) {
  2289. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2290. }
  2291. if ($add != 0) {
  2292. $completeItems += $add;
  2293. if ($this->debug > 2) {
  2294. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2295. }
  2296. }
  2297. $text = '';
  2298. if ($completeItems > $total_items) {
  2299. $completeItems = $total_items;
  2300. }
  2301. $percentage = 0;
  2302. if ($mode == '%') {
  2303. if ($total_items > 0) {
  2304. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2305. } else {
  2306. $percentage = 0;
  2307. }
  2308. $percentage = number_format($percentage, 0);
  2309. $text = '%';
  2310. } elseif ($mode == 'abs') {
  2311. $percentage = $completeItems;
  2312. $text = '/' . $total_items;
  2313. }
  2314. return array(
  2315. $percentage,
  2316. $text
  2317. );
  2318. }
  2319. /**
  2320. * Gets the progress bar mode
  2321. * @return string The progress bar mode attribute
  2322. */
  2323. public function get_progress_bar_mode()
  2324. {
  2325. if ($this->debug > 0) {
  2326. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2327. }
  2328. if (!empty ($this->progress_bar_mode)) {
  2329. return $this->progress_bar_mode;
  2330. } else {
  2331. return '%';
  2332. }
  2333. }
  2334. /**
  2335. * Gets the learnpath proximity (remote or local)
  2336. * @return string Learnpath proximity
  2337. */
  2338. public function get_proximity()
  2339. {
  2340. if ($this->debug > 0) {
  2341. error_log('New LP - In learnpath::get_proximity()', 0);
  2342. }
  2343. if (!empty ($this->proximity)) {
  2344. return $this->proximity;
  2345. } else {
  2346. return '';
  2347. }
  2348. }
  2349. /**
  2350. * Gets the learnpath theme (remote or local)
  2351. * @return string Learnpath theme
  2352. */
  2353. public function get_theme()
  2354. {
  2355. if ($this->debug > 0) {
  2356. error_log('New LP - In learnpath::get_theme()', 0);
  2357. }
  2358. if (!empty ($this->theme)) {
  2359. return $this->theme;
  2360. } else {
  2361. return '';
  2362. }
  2363. }
  2364. /**
  2365. * Gets the learnpath session id
  2366. * @return string Learnpath theme
  2367. */
  2368. public function get_lp_session_id()
  2369. {
  2370. if ($this->debug > 0) {
  2371. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2372. }
  2373. if (!empty ($this->lp_session_id)) {
  2374. return $this->lp_session_id;
  2375. } else {
  2376. return 0;
  2377. }
  2378. }
  2379. /**
  2380. * Gets the learnpath image
  2381. * @return string Web URL of the LP image
  2382. */
  2383. public function get_preview_image()
  2384. {
  2385. if ($this->debug > 0) {
  2386. error_log('New LP - In learnpath::get_preview_image()', 0);
  2387. }
  2388. if (!empty($this->preview_image)) {
  2389. return $this->preview_image;
  2390. } else {
  2391. return '';
  2392. }
  2393. }
  2394. /**
  2395. * @param string $size
  2396. * @param string $path_type
  2397. * @return bool|string
  2398. */
  2399. public function get_preview_image_path($size = null, $path_type = 'web')
  2400. {
  2401. $preview_image = $this->get_preview_image();
  2402. if (isset($preview_image) && !empty($preview_image)) {
  2403. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2404. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2405. if (isset($size)) {
  2406. $info = pathinfo($preview_image);
  2407. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2408. if (file_exists($image_sys_path.$image_custom_size)) {
  2409. if ($path_type == 'web') {
  2410. return $image_path.$image_custom_size;
  2411. } else {
  2412. return $image_sys_path.$image_custom_size;
  2413. }
  2414. }
  2415. } else {
  2416. if ($path_type == 'web') {
  2417. return $image_path.$preview_image;
  2418. } else {
  2419. return $image_sys_path.$preview_image;
  2420. }
  2421. }
  2422. }
  2423. return false;
  2424. }
  2425. /**
  2426. * Gets the learnpath author
  2427. * @return string LP's author
  2428. */
  2429. public function get_author()
  2430. {
  2431. if ($this->debug > 0) {
  2432. error_log('New LP - In learnpath::get_author()', 0);
  2433. }
  2434. if (!empty ($this->author)) {
  2435. return $this->author;
  2436. } else {
  2437. return '';
  2438. }
  2439. }
  2440. /**
  2441. * Gets the learnpath author
  2442. * @return string LP's author
  2443. */
  2444. public function get_hide_toc_frame()
  2445. {
  2446. if ($this->debug > 0) {
  2447. error_log('New LP - In learnpath::get_author()', 0);
  2448. }
  2449. if (!empty ($this->hide_toc_frame)) {
  2450. return $this->hide_toc_frame;
  2451. } else {
  2452. return '';
  2453. }
  2454. }
  2455. /**
  2456. * Generate a new prerequisites string for a given item. If this item was a sco and
  2457. * its prerequisites were strings (instead of IDs), then transform those strings into
  2458. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2459. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2460. * same rule as the scorm_export() method
  2461. * @param integer Item ID
  2462. * @return string Prerequisites string ready for the export as SCORM
  2463. */
  2464. public function get_scorm_prereq_string($item_id)
  2465. {
  2466. if ($this->debug > 0) {
  2467. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2468. }
  2469. if (!is_object($this->items[$item_id])) {
  2470. return false;
  2471. }
  2472. /** @var learnpathItem $oItem */
  2473. $oItem = $this->items[$item_id];
  2474. $prereq = $oItem->get_prereq_string();
  2475. if (empty($prereq)) {
  2476. return '';
  2477. }
  2478. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2479. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2480. // then simply return it (with the ITEM_ prefix).
  2481. //return 'ITEM_' . $prereq;
  2482. return $this->items[$prereq]->ref;
  2483. } else {
  2484. if (isset($this->refs_list[$prereq])) {
  2485. // It's a simple string item from which the ID can be found in the refs list,
  2486. // so we can transform it directly to an ID for export.
  2487. return $this->items[$this->refs_list[$prereq]]->ref;
  2488. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2489. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2490. } else {
  2491. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2492. // and replace them, one by one, by the internal IDs (chamilo db)
  2493. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2494. // by a space as well.
  2495. $find = array (
  2496. '&',
  2497. '|',
  2498. '~',
  2499. '=',
  2500. '<>',
  2501. '{',
  2502. '}',
  2503. '*',
  2504. '(',
  2505. ')'
  2506. );
  2507. $replace = array (
  2508. ' ',
  2509. ' ',
  2510. ' ',
  2511. ' ',
  2512. ' ',
  2513. ' ',
  2514. ' ',
  2515. ' ',
  2516. ' ',
  2517. ' '
  2518. );
  2519. $prereq_mod = str_replace($find, $replace, $prereq);
  2520. $ids = explode(' ', $prereq_mod);
  2521. foreach ($ids as $id) {
  2522. $id = trim($id);
  2523. if (isset ($this->refs_list[$id])) {
  2524. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2525. }
  2526. }
  2527. return $prereq;
  2528. }
  2529. }
  2530. }
  2531. /**
  2532. * Returns the XML DOM document's node
  2533. * @param resource Reference to a list of objects to search for the given ITEM_*
  2534. * @param string The identifier to look for
  2535. * @return mixed The reference to the element found with that identifier. False if not found
  2536. */
  2537. public function get_scorm_xml_node(& $children, $id)
  2538. {
  2539. for ($i = 0; $i < $children->length; $i++) {
  2540. $item_temp = $children->item($i);
  2541. if ($item_temp->nodeName == 'item') {
  2542. if ($item_temp->getAttribute('identifier') == $id) {
  2543. return $item_temp;
  2544. }
  2545. }
  2546. $subchildren = $item_temp->childNodes;
  2547. if ($subchildren && $subchildren->length > 0) {
  2548. $val = $this->get_scorm_xml_node($subchildren, $id);
  2549. if (is_object($val)) {
  2550. return $val;
  2551. }
  2552. }
  2553. }
  2554. return false;
  2555. }
  2556. /**
  2557. * Gets the status list for all LP's items
  2558. * @return array Array of [index] => [item ID => current status]
  2559. */
  2560. public function get_items_status_list()
  2561. {
  2562. if ($this->debug > 0) {
  2563. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2564. }
  2565. $list = array ();
  2566. foreach ($this->ordered_items as $item_id) {
  2567. $list[] = array (
  2568. $item_id => $this->items[$item_id]->get_status()
  2569. );
  2570. }
  2571. return $list;
  2572. }
  2573. /**
  2574. * Return the number of interactions for the given learnpath Item View ID.
  2575. * This method can be used as static.
  2576. * @param integer Item View ID
  2577. * @param integer course id
  2578. * @return integer Number of interactions
  2579. */
  2580. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2581. {
  2582. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2583. $lp_iv_id = intval($lp_iv_id);
  2584. $course_id = intval($course_id);
  2585. $sql = "SELECT count(*) FROM $table
  2586. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2587. $res = Database::query($sql);
  2588. $num = 0;
  2589. if (Database::num_rows($res)) {
  2590. $row = Database::fetch_array($res);
  2591. $num = $row[0];
  2592. }
  2593. return $num;
  2594. }
  2595. /**
  2596. * Return the interactions as an array for the given lp_iv_id.
  2597. * This method can be used as static.
  2598. * @param integer Learnpath Item View ID
  2599. * @return array
  2600. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2601. */
  2602. public static function get_iv_interactions_array($lp_iv_id)
  2603. {
  2604. $course_id = api_get_course_int_id();
  2605. $list = array();
  2606. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2607. if (empty($lp_iv_id)) {
  2608. return array();
  2609. }
  2610. $sql = "SELECT * FROM $table
  2611. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2612. ORDER BY order_id ASC";
  2613. $res = Database::query($sql);
  2614. $num = Database :: num_rows($res);
  2615. if ($num > 0) {
  2616. $list[] = array (
  2617. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2618. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2619. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2620. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2621. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2622. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2623. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2624. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2625. );
  2626. while ($row = Database :: fetch_array($res)) {
  2627. $list[] = array (
  2628. 'order_id' => ($row['order_id'] + 1),
  2629. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2630. 'type' => $row['interaction_type'],
  2631. 'time' => $row['completion_time'],
  2632. //'correct_responses' => $row['correct_responses'],
  2633. 'correct_responses' => '', // Hide correct responses from students.
  2634. 'student_response' => $row['student_response'],
  2635. 'result' => $row['result'],
  2636. 'latency' => $row['latency']
  2637. );
  2638. }
  2639. }
  2640. return $list;
  2641. }
  2642. /**
  2643. * Return the number of objectives for the given learnpath Item View ID.
  2644. * This method can be used as static.
  2645. * @param integer Item View ID
  2646. * @return integer Number of objectives
  2647. */
  2648. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2649. {
  2650. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2651. $course_id = intval($course_id);
  2652. $lp_iv_id = intval($lp_iv_id);
  2653. $sql = "SELECT count(*) FROM $table
  2654. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2655. //@todo seems that this always returns 0
  2656. $res = Database::query($sql);
  2657. $num = 0;
  2658. if (Database::num_rows($res)) {
  2659. $row = Database :: fetch_array($res);
  2660. $num = $row[0];
  2661. }
  2662. return $num;
  2663. }
  2664. /**
  2665. * Return the objectives as an array for the given lp_iv_id.
  2666. * This method can be used as static.
  2667. * @param integer Learnpath Item View ID
  2668. * @return array
  2669. * @todo Translate labels
  2670. */
  2671. public static function get_iv_objectives_array($lp_iv_id = 0)
  2672. {
  2673. $course_id = api_get_course_int_id();
  2674. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2675. $sql = "SELECT * FROM $table
  2676. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2677. ORDER BY order_id ASC";
  2678. $res = Database::query($sql);
  2679. $num = Database :: num_rows($res);
  2680. $list = array();
  2681. if ($num > 0) {
  2682. $list[] = array(
  2683. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2684. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2685. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2686. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2687. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2688. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2689. );
  2690. while ($row = Database :: fetch_array($res)) {
  2691. $list[] = array (
  2692. 'order_id' => ($row['order_id'] + 1),
  2693. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2694. 'score_raw' => $row['score_raw'],
  2695. 'score_max' => $row['score_max'],
  2696. 'score_min' => $row['score_min'],
  2697. 'status' => $row['status']
  2698. );
  2699. }
  2700. }
  2701. return $list;
  2702. }
  2703. /**
  2704. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2705. * used by get_html_toc() to be ready to display
  2706. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2707. */
  2708. public function get_toc()
  2709. {
  2710. if ($this->debug > 0) {
  2711. error_log('learnpath::get_toc()', 0);
  2712. }
  2713. $toc = array();
  2714. foreach ($this->ordered_items as $item_id) {
  2715. if ($this->debug > 2) {
  2716. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2717. }
  2718. // TODO: Change this link generation and use new function instead.
  2719. $toc[] = array (
  2720. 'id' => $item_id,
  2721. 'title' => $this->items[$item_id]->get_title(),
  2722. 'status' => $this->items[$item_id]->get_status(),
  2723. 'level' => $this->items[$item_id]->get_level(),
  2724. 'type' => $this->items[$item_id]->get_type(),
  2725. 'description' => $this->items[$item_id]->get_description(),
  2726. 'path' => $this->items[$item_id]->get_path(),
  2727. );
  2728. }
  2729. if ($this->debug > 2) {
  2730. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  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. * @return string A JS array vairiable construction
  2738. */
  2739. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2740. {
  2741. if ($this->debug > 0) {
  2742. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  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. if ($this->debug > 2) {
  2749. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2750. }
  2751. return $toc;
  2752. }
  2753. /**
  2754. * Gets the learning path type
  2755. * @param boolean Return the name? If false, return the ID. Default is false.
  2756. * @return mixed Type ID or name, depending on the parameter
  2757. */
  2758. public function get_type($get_name = false)
  2759. {
  2760. $res = false;
  2761. if ($this->debug > 0) {
  2762. error_log('New LP - In learnpath::get_type()', 0);
  2763. }
  2764. if (!empty ($this->type)) {
  2765. if ($get_name) {
  2766. // Get it from the lp_type table in main db.
  2767. } else {
  2768. $res = $this->type;
  2769. }
  2770. }
  2771. if ($this->debug > 2) {
  2772. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2773. }
  2774. return $res;
  2775. }
  2776. /**
  2777. * Gets the learning path type as static method
  2778. * @param boolean Return the name? If false, return the ID. Default is false.
  2779. * @return mixed Type ID or name, depending on the parameter
  2780. */
  2781. public static function get_type_static($lp_id = 0)
  2782. {
  2783. $course_id = api_get_course_int_id();
  2784. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2785. $lp_id = intval($lp_id);
  2786. $sql = "SELECT lp_type FROM $tbl_lp
  2787. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2788. $res = Database::query($sql);
  2789. if ($res === false) {
  2790. return null;
  2791. }
  2792. if (Database :: num_rows($res) <= 0) {
  2793. return null;
  2794. }
  2795. $row = Database :: fetch_array($res);
  2796. return $row['lp_type'];
  2797. }
  2798. /**
  2799. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2800. * This method can be used as abstract and is recursive
  2801. * @param integer Learnpath ID
  2802. * @param integer Parent ID of the items to look for
  2803. * @return mixed Ordered list of item IDs or false on error
  2804. */
  2805. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2806. {
  2807. if (empty($course_id)) {
  2808. $course_id = api_get_course_int_id();
  2809. } else {
  2810. $course_id = intval($course_id);
  2811. }
  2812. $list = array();
  2813. if (empty($lp)) {
  2814. return false;
  2815. }
  2816. $lp = intval($lp);
  2817. $parent = intval($parent);
  2818. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2819. $sql = "SELECT id FROM $tbl_lp_item
  2820. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2821. ORDER BY display_order";
  2822. $res = Database::query($sql);
  2823. while ($row = Database :: fetch_array($res)) {
  2824. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2825. $list[] = $row['id'];
  2826. foreach ($sublist as $item) {
  2827. $list[] = $item;
  2828. }
  2829. }
  2830. return $list;
  2831. }
  2832. /**
  2833. * @return array
  2834. */
  2835. public static function getChapterTypes()
  2836. {
  2837. return array(
  2838. 'dir'
  2839. );
  2840. }
  2841. /**
  2842. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2843. * @return string HTML TOC ready to display
  2844. */
  2845. public function get_html_toc($toc_list = null)
  2846. {
  2847. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2848. if ($this->debug > 0) {
  2849. error_log('In learnpath::get_html_toc()', 0);
  2850. }
  2851. if (empty($toc_list)) {
  2852. $toc_list = $this->get_toc();
  2853. }
  2854. $html = '<div class="scorm-body">';
  2855. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2856. // Temporary variables.
  2857. $mycurrentitemid = $this->get_current_item_id();
  2858. $color_counter = 0;
  2859. $i = 0;
  2860. foreach ($toc_list as $item) {
  2861. // Style Status
  2862. $class_name = [
  2863. 'not attempted' => 'scorm_not_attempted',
  2864. 'incomplete' => 'scorm_not_attempted',
  2865. 'failed' => 'scorm_failed',
  2866. 'completed' => 'scorm_completed',
  2867. 'passed' => 'scorm_completed',
  2868. 'succeeded' => 'scorm_completed',
  2869. 'browsed' => 'scorm_completed',
  2870. ];
  2871. $rowColor = ' ';
  2872. $dirTypes = self::getChapterTypes();
  2873. if (in_array($item['type'], $dirTypes)) {
  2874. $rowColor ='scorm_item_section ';
  2875. }
  2876. if ($item['id'] == $this->current) {
  2877. $rowColor = 'scorm_item_normal '.$rowColor.' scorm_highlight';
  2878. } elseif (!in_array($item['type'], $dirTypes)) {
  2879. $rowColor = 'scorm_item_normal '.$rowColor.' ';
  2880. }
  2881. $html .= '<div id="toc_' . $item['id'] . '" class="' . $rowColor . ' ' . $class_name[$item['status']] . '">';
  2882. // Learning path title
  2883. $title = $item['title'];
  2884. if (empty ($title)) {
  2885. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2886. }
  2887. $title = Security::remove_XSS($title);
  2888. // Learning path personalization
  2889. // build the LP tree
  2890. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2891. $description = $item['description'];
  2892. if (empty($description)) {
  2893. $description = $title;
  2894. }
  2895. if (in_array($item['type'], $dirTypes)) {
  2896. // Chapters
  2897. $html .= '<div class="section level_'.$item['level'].'" title="'.$description.'" >';
  2898. } else {
  2899. $html .= '<div class="item level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2900. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2901. }
  2902. if (in_array($item['type'], $dirTypes)) {
  2903. // Chapter
  2904. // if you want to put an image before, you should use css
  2905. $html .= stripslashes($title);
  2906. } else {
  2907. $this->get_link('http', $item['id'], $toc_list);
  2908. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2909. }
  2910. $html .= "</div>";
  2911. if ($rowColor != '') {
  2912. $html .= '</div>';
  2913. }
  2914. $color_counter++;
  2915. }
  2916. $html .= "</div>";
  2917. $html .= "</div>";
  2918. return $html;
  2919. }
  2920. /**
  2921. * Returns an HTML-formatted string ready to display with teacher buttons
  2922. * in LP view menu
  2923. * @return string HTML TOC ready to display
  2924. */
  2925. public function get_teacher_toc_buttons()
  2926. {
  2927. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2928. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2929. $html = '';
  2930. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2931. $gradebook = '';
  2932. if (!empty($_GET['gradebook'])) {
  2933. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2934. }
  2935. if ($this->get_lp_session_id() == api_get_session_id()) {
  2936. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2937. $html .= '<div class="btn-group">';
  2938. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  2939. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2940. $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'>" .
  2941. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2942. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'&isStudentView=false">' .
  2943. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2944. $html .= '</div>';
  2945. $html .= '</div>';
  2946. }
  2947. }
  2948. return $html;
  2949. }
  2950. /**
  2951. * Gets the learnpath maker name - generally the editor's name
  2952. * @return string Learnpath maker name
  2953. */
  2954. public function get_maker()
  2955. {
  2956. if ($this->debug > 0) {
  2957. error_log('New LP - In learnpath::get_maker()', 0);
  2958. }
  2959. if (!empty ($this->maker)) {
  2960. return $this->maker;
  2961. } else {
  2962. return '';
  2963. }
  2964. }
  2965. /**
  2966. * Gets the learnpath name/title
  2967. * @return string Learnpath name/title
  2968. */
  2969. public function get_name()
  2970. {
  2971. if ($this->debug > 0) {
  2972. error_log('New LP - In learnpath::get_name()', 0);
  2973. }
  2974. if (!empty ($this->name)) {
  2975. return $this->name;
  2976. } else {
  2977. return 'N/A';
  2978. }
  2979. }
  2980. /**
  2981. * Gets a link to the resource from the present location, depending on item ID.
  2982. * @param string $type Type of link expected
  2983. * @param integer $item_id Learnpath item ID
  2984. * @return string $provided_toc Link to the lp_item resource
  2985. */
  2986. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  2987. {
  2988. $course_id = $this->get_course_int_id();
  2989. if ($this->debug > 0) {
  2990. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  2991. }
  2992. if (empty($item_id)) {
  2993. if ($this->debug > 2) {
  2994. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  2995. }
  2996. $item_id = $this->get_current_item_id();
  2997. }
  2998. if (empty($item_id)) {
  2999. if ($this->debug > 2) {
  3000. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3001. }
  3002. //still empty, this means there was no item_id given and we are not in an object context or
  3003. //the object property is empty, return empty link
  3004. $item_id = $this->first();
  3005. return '';
  3006. }
  3007. $file = '';
  3008. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3009. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3010. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3011. $item_id = intval($item_id);
  3012. $sql = "SELECT
  3013. l.lp_type as ltype,
  3014. l.path as lpath,
  3015. li.item_type as litype,
  3016. li.path as lipath,
  3017. li.parameters as liparams
  3018. FROM $lp_table l
  3019. INNER JOIN $lp_item_table li
  3020. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3021. WHERE li.id = $item_id ";
  3022. if ($this->debug > 2) {
  3023. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3024. }
  3025. $res = Database::query($sql);
  3026. if (Database :: num_rows($res) > 0) {
  3027. $row = Database :: fetch_array($res);
  3028. $lp_type = $row['ltype'];
  3029. $lp_path = $row['lpath'];
  3030. $lp_item_type = $row['litype'];
  3031. $lp_item_path = $row['lipath'];
  3032. $lp_item_params = $row['liparams'];
  3033. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3034. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3035. }
  3036. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3037. if ($type == 'http') {
  3038. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3039. } else {
  3040. $course_path = $sys_course_path; //system path
  3041. }
  3042. // Fixed issue BT#1272 - If the item type is a Chamilo Item (quiz, link, etc), then change the lp type to thread it as a normal Chamilo LP not a SCO.
  3043. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3044. $lp_type = 1;
  3045. }
  3046. if ($this->debug > 2) {
  3047. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3048. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3049. }
  3050. // Now go through the specific cases to get the end of the path
  3051. // @todo Use constants instead of int values.
  3052. switch ($lp_type) {
  3053. case 1 :
  3054. $file = self::rl_get_resource_link_for_learnpath(
  3055. $course_id,
  3056. $this->get_id(),
  3057. $item_id,
  3058. $this->get_view_id()
  3059. );
  3060. if ($this->debug > 0) {
  3061. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3062. }
  3063. switch ($lp_item_type) {
  3064. case 'dir':
  3065. $file = 'lp_content.php?type=dir';
  3066. break;
  3067. case 'link':
  3068. if (Link::is_youtube_link($file)) {
  3069. $src = Link::get_youtube_video_id($file);
  3070. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=youtube&source='.$src;
  3071. } elseif (Link::isVimeoLink($file)) {
  3072. $src = Link::getVimeoLinkId($file);
  3073. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=vimeo&source='.$src;
  3074. } else {
  3075. // If the current site is HTTPS and the link is
  3076. // HTTP, browsers will refuse opening the link
  3077. $urlId = api_get_current_access_url_id();
  3078. $url = api_get_access_url($urlId, false);
  3079. $protocol = substr($url['url'], 0, 5);
  3080. if ($protocol === 'https') {
  3081. $linkProtocol = substr($file, 0, 5);
  3082. if ($linkProtocol === 'http:') {
  3083. //this is the special intervention case
  3084. $file = api_get_path(
  3085. WEB_CODE_PATH
  3086. ) . 'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3087. }
  3088. }
  3089. }
  3090. break;
  3091. case 'quiz':
  3092. // Check how much attempts of a exercise exits in lp
  3093. $lp_item_id = $this->get_current_item_id();
  3094. $lp_view_id = $this->get_view_id();
  3095. $prevent_reinit = null;
  3096. if (isset($this->items[$this->current])) {
  3097. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3098. }
  3099. if (empty($provided_toc)) {
  3100. if ($this->debug > 0) {
  3101. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3102. }
  3103. $list = $this->get_toc();
  3104. } else {
  3105. if ($this->debug > 0) {
  3106. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3107. }
  3108. $list = $provided_toc;
  3109. }
  3110. $type_quiz = false;
  3111. foreach ($list as $toc) {
  3112. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3113. $type_quiz = true;
  3114. }
  3115. }
  3116. if ($type_quiz) {
  3117. $lp_item_id = intval($lp_item_id);
  3118. $lp_view_id = intval($lp_view_id);
  3119. $sql = "SELECT count(*) FROM $lp_item_view_table
  3120. WHERE
  3121. c_id = $course_id AND
  3122. lp_item_id='".$lp_item_id."' AND
  3123. lp_view_id ='".$lp_view_id."' AND
  3124. status='completed'";
  3125. $result = Database::query($sql);
  3126. $row_count = Database:: fetch_row($result);
  3127. $count_item_view = (int)$row_count[0];
  3128. $not_multiple_attempt = 0;
  3129. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3130. $not_multiple_attempt = 1;
  3131. }
  3132. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3133. }
  3134. break;
  3135. }
  3136. $tmp_array = explode('/', $file);
  3137. $document_name = $tmp_array[count($tmp_array) - 1];
  3138. if (strpos($document_name, '_DELETED_')) {
  3139. $file = 'blank.php?error=document_deleted';
  3140. }
  3141. break;
  3142. case 2 :
  3143. if ($this->debug > 2) {
  3144. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3145. }
  3146. if ($lp_item_type != 'dir') {
  3147. // Quite complex here:
  3148. // We want to make sure 'http://' (and similar) links can
  3149. // be loaded as is (withouth the Chamilo path in front) but
  3150. // some contents use this form: resource.htm?resource=http://blablabla
  3151. // which means we have to find a protocol at the path's start, otherwise
  3152. // it should not be considered as an external URL.
  3153. //if ($this->prerequisites_match($item_id)) {
  3154. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3155. if ($this->debug > 2) {
  3156. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3157. }
  3158. // Distant url, return as is.
  3159. $file = $lp_item_path;
  3160. } else {
  3161. if ($this->debug > 2) {
  3162. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3163. }
  3164. // Prevent getting untranslatable urls.
  3165. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3166. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3167. // Prepare the path.
  3168. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3169. // TODO: Fix this for urls with protocol header.
  3170. $file = str_replace('//', '/', $file);
  3171. $file = str_replace(':/', '://', $file);
  3172. if (substr($lp_path, -1) == '/') {
  3173. $lp_path = substr($lp_path, 0, -1);
  3174. }
  3175. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3176. // if file not found.
  3177. $decoded = html_entity_decode($lp_item_path);
  3178. list ($decoded) = explode('?', $decoded);
  3179. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3180. $file = self::rl_get_resource_link_for_learnpath(
  3181. $course_id,
  3182. $this->get_id(),
  3183. $item_id,
  3184. $this->get_view_id()
  3185. );
  3186. if (empty($file)) {
  3187. $file = 'blank.php?error=document_not_found';
  3188. } else {
  3189. $tmp_array = explode('/', $file);
  3190. $document_name = $tmp_array[count($tmp_array) - 1];
  3191. if (strpos($document_name, '_DELETED_')) {
  3192. $file = 'blank.php?error=document_deleted';
  3193. } else {
  3194. $file = 'blank.php?error=document_not_found';
  3195. }
  3196. }
  3197. } else {
  3198. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3199. }
  3200. }
  3201. }
  3202. // We want to use parameters if they were defined in the imsmanifest
  3203. if (strpos($file, 'blank.php') === false) {
  3204. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3205. }
  3206. } else {
  3207. $file = 'lp_content.php?type=dir';
  3208. }
  3209. break;
  3210. case 3 :
  3211. if ($this->debug > 2) {
  3212. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3213. }
  3214. // Formatting AICC HACP append URL.
  3215. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'lp/aicc_hacp.php') . '&';
  3216. if (!empty($lp_item_params)) {
  3217. $aicc_append .= $lp_item_params . '&';
  3218. }
  3219. if ($lp_item_type != 'dir') {
  3220. // Quite complex here:
  3221. // We want to make sure 'http://' (and similar) links can
  3222. // be loaded as is (withouth the Chamilo path in front) but
  3223. // some contents use this form: resource.htm?resource=http://blablabla
  3224. // which means we have to find a protocol at the path's start, otherwise
  3225. // it should not be considered as an external URL.
  3226. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3227. if ($this->debug > 2) {
  3228. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3229. }
  3230. // Distant url, return as is.
  3231. $file = $lp_item_path;
  3232. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3233. /*
  3234. if (stristr($file,'<servername>') !== false) {
  3235. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3236. }
  3237. */
  3238. if (stripos($file, '<servername>') !== false) {
  3239. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3240. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3241. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3242. }
  3243. //
  3244. $file .= $aicc_append;
  3245. } else {
  3246. if ($this->debug > 2) {
  3247. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3248. }
  3249. // Prevent getting untranslatable urls.
  3250. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3251. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3252. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3253. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3254. // TODO: Fix this for urls with protocol header.
  3255. $file = str_replace('//', '/', $file);
  3256. $file = str_replace(':/', '://', $file);
  3257. $file .= $aicc_append;
  3258. }
  3259. } else {
  3260. $file = 'lp_content.php?type=dir';
  3261. }
  3262. break;
  3263. case 4 :
  3264. break;
  3265. default :
  3266. break;
  3267. }
  3268. // Replace &amp; by & because &amp; will break URL with params
  3269. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3270. }
  3271. if ($this->debug > 2) {
  3272. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3273. }
  3274. return $file;
  3275. }
  3276. /**
  3277. * Gets the latest usable view or generate a new one
  3278. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3279. * @return integer DB lp_view id
  3280. */
  3281. public function get_view($attempt_num = 0)
  3282. {
  3283. if ($this->debug > 0) {
  3284. error_log('New LP - In learnpath::get_view()', 0);
  3285. }
  3286. $search = '';
  3287. // Use $attempt_num to enable multi-views management (disabled so far).
  3288. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3289. $search = 'AND view_count = ' . $attempt_num;
  3290. }
  3291. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3292. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3293. $course_id = api_get_course_int_id();
  3294. $sessionId = api_get_session_id();
  3295. $sql = "SELECT id, view_count FROM $lp_view_table
  3296. WHERE
  3297. c_id = " . $course_id . " AND
  3298. lp_id = " . $this->get_id() . " AND
  3299. user_id = " . $this->get_user_id() . " AND
  3300. session_id = $sessionId
  3301. $search
  3302. ORDER BY view_count DESC";
  3303. $res = Database::query($sql);
  3304. if (Database :: num_rows($res) > 0) {
  3305. $row = Database :: fetch_array($res);
  3306. $this->lp_view_id = $row['id'];
  3307. } else if (!api_is_invitee()) {
  3308. // There is no database record, create one.
  3309. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3310. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3311. Database::query($sql);
  3312. $id = Database :: insert_id();
  3313. $this->lp_view_id = $id;
  3314. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3315. Database::query($sql);
  3316. }
  3317. return $this->lp_view_id;
  3318. }
  3319. /**
  3320. * Gets the current view id
  3321. * @return integer View ID (from lp_view)
  3322. */
  3323. public function get_view_id()
  3324. {
  3325. if ($this->debug > 0) {
  3326. error_log('New LP - In learnpath::get_view_id()', 0);
  3327. }
  3328. if (!empty ($this->lp_view_id)) {
  3329. return $this->lp_view_id;
  3330. } else {
  3331. return 0;
  3332. }
  3333. }
  3334. /**
  3335. * Gets the update queue
  3336. * @return array Array containing IDs of items to be updated by JavaScript
  3337. */
  3338. public function get_update_queue()
  3339. {
  3340. if ($this->debug > 0) {
  3341. error_log('New LP - In learnpath::get_update_queue()', 0);
  3342. }
  3343. return $this->update_queue;
  3344. }
  3345. /**
  3346. * Gets the user ID
  3347. * @return integer User ID
  3348. */
  3349. public function get_user_id()
  3350. {
  3351. if ($this->debug > 0) {
  3352. error_log('New LP - In learnpath::get_user_id()', 0);
  3353. }
  3354. if (!empty ($this->user_id)) {
  3355. return $this->user_id;
  3356. } else {
  3357. return false;
  3358. }
  3359. }
  3360. /**
  3361. * Checks if any of the items has an audio element attached
  3362. * @return bool True or false
  3363. */
  3364. public function has_audio()
  3365. {
  3366. if ($this->debug > 1) {
  3367. error_log('New LP - In learnpath::has_audio()', 0);
  3368. }
  3369. $has = false;
  3370. foreach ($this->items as $i => $item) {
  3371. if (!empty ($this->items[$i]->audio)) {
  3372. $has = true;
  3373. break;
  3374. }
  3375. }
  3376. return $has;
  3377. }
  3378. /**
  3379. * Logs a message into a file
  3380. * @param string Message to log
  3381. * @return boolean True on success, false on error or if msg empty
  3382. */
  3383. public function log($msg)
  3384. {
  3385. if ($this->debug > 0) {
  3386. error_log('New LP - In learnpath::log()', 0);
  3387. }
  3388. // TODO
  3389. $this->error .= $msg;
  3390. return true;
  3391. }
  3392. /**
  3393. * Moves an item up and down at its level
  3394. * @param integer Item to move up and down
  3395. * @param string Direction 'up' or 'down'
  3396. * @return integer New display order, or false on error
  3397. */
  3398. public function move_item($id, $direction)
  3399. {
  3400. $course_id = api_get_course_int_id();
  3401. if ($this->debug > 0) {
  3402. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3403. }
  3404. if (empty($id) || empty($direction)) {
  3405. return false;
  3406. }
  3407. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3408. $sql_sel = "SELECT *
  3409. FROM " . $tbl_lp_item . "
  3410. WHERE c_id = ".$course_id." AND id = " . $id;
  3411. $res_sel = Database::query($sql_sel);
  3412. // Check if elem exists.
  3413. if (Database :: num_rows($res_sel) < 1) {
  3414. return false;
  3415. }
  3416. // Gather data.
  3417. $row = Database :: fetch_array($res_sel);
  3418. $previous = $row['previous_item_id'];
  3419. $next = $row['next_item_id'];
  3420. $display = $row['display_order'];
  3421. $parent = $row['parent_item_id'];
  3422. $lp = $row['lp_id'];
  3423. // Update the item (switch with previous/next one).
  3424. switch ($direction) {
  3425. case 'up':
  3426. if ($this->debug > 2) {
  3427. error_log('Movement up detected', 0);
  3428. }
  3429. if ($display <= 1) { /*do nothing*/
  3430. } else {
  3431. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3432. WHERE c_id = ".$course_id." AND id = $previous";
  3433. if ($this->debug > 2) {
  3434. error_log('Selecting previous: ' . $sql_sel2, 0);
  3435. }
  3436. $res_sel2 = Database::query($sql_sel2);
  3437. if (Database :: num_rows($res_sel2) < 1) {
  3438. $previous_previous = 0;
  3439. }
  3440. // Gather data.
  3441. $row2 = Database :: fetch_array($res_sel2);
  3442. $previous_previous = $row2['previous_item_id'];
  3443. // Update previous_previous item (switch "next" with current).
  3444. if ($previous_previous != 0) {
  3445. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3446. next_item_id = $id
  3447. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3448. if ($this->debug > 2) {
  3449. error_log($sql_upd2, 0);
  3450. }
  3451. Database::query($sql_upd2);
  3452. }
  3453. // Update previous item (switch with current).
  3454. if ($previous != 0) {
  3455. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3456. next_item_id = $next,
  3457. previous_item_id = $id,
  3458. display_order = display_order +1
  3459. WHERE c_id = ".$course_id." AND id = $previous";
  3460. if ($this->debug > 2) {
  3461. error_log($sql_upd2, 0);
  3462. }
  3463. Database::query($sql_upd2);
  3464. }
  3465. // Update current item (switch with previous).
  3466. if ($id != 0) {
  3467. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3468. next_item_id = $previous,
  3469. previous_item_id = $previous_previous,
  3470. display_order = display_order-1
  3471. WHERE c_id = ".$course_id." AND id = $id";
  3472. if ($this->debug > 2) {
  3473. error_log($sql_upd2, 0);
  3474. }
  3475. Database::query($sql_upd2);
  3476. }
  3477. // Update next item (new previous item).
  3478. if ($next != 0) {
  3479. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3480. WHERE c_id = ".$course_id." AND id = $next";
  3481. if ($this->debug > 2) {
  3482. error_log($sql_upd2, 0);
  3483. }
  3484. Database::query($sql_upd2);
  3485. }
  3486. $display = $display -1;
  3487. }
  3488. break;
  3489. case 'down':
  3490. if ($this->debug > 2) {
  3491. error_log('Movement down detected', 0);
  3492. }
  3493. if ($next == 0) { /* Do nothing. */
  3494. } else {
  3495. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3496. if ($this->debug > 2) {
  3497. error_log('Selecting next: ' . $sql_sel2, 0);
  3498. }
  3499. $res_sel2 = Database::query($sql_sel2);
  3500. if (Database :: num_rows($res_sel2) < 1) {
  3501. $next_next = 0;
  3502. }
  3503. // Gather data.
  3504. $row2 = Database :: fetch_array($res_sel2);
  3505. $next_next = $row2['next_item_id'];
  3506. // Update previous item (switch with current).
  3507. if ($previous != 0) {
  3508. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3509. WHERE c_id = ".$course_id." AND id = $previous";
  3510. Database::query($sql_upd2);
  3511. }
  3512. // Update current item (switch with previous).
  3513. if ($id != 0) {
  3514. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3515. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3516. WHERE c_id = ".$course_id." AND id = $id";
  3517. Database::query($sql_upd2);
  3518. }
  3519. // Update next item (new previous item).
  3520. if ($next != 0) {
  3521. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3522. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3523. WHERE c_id = ".$course_id." AND id = $next";
  3524. Database::query($sql_upd2);
  3525. }
  3526. // Update next_next item (switch "previous" with current).
  3527. if ($next_next != 0) {
  3528. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3529. previous_item_id = $id
  3530. WHERE c_id = ".$course_id." AND id = $next_next";
  3531. Database::query($sql_upd2);
  3532. }
  3533. $display = $display +1;
  3534. }
  3535. break;
  3536. default :
  3537. return false;
  3538. }
  3539. return $display;
  3540. }
  3541. /**
  3542. * Move a learnpath up (display_order)
  3543. * @param integer $lp_id Learnpath ID
  3544. */
  3545. public static function move_up($lp_id)
  3546. {
  3547. $course_id = api_get_course_int_id();
  3548. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3549. $sql = "SELECT * FROM $lp_table
  3550. WHERE c_id = ".$course_id."
  3551. ORDER BY display_order";
  3552. $res = Database::query($sql);
  3553. if ($res === false)
  3554. return false;
  3555. $lps = array ();
  3556. $lp_order = array ();
  3557. $num = Database :: num_rows($res);
  3558. // First check the order is correct, globally (might be wrong because
  3559. // of versions < 1.8.4)
  3560. if ($num > 0) {
  3561. $i = 1;
  3562. while ($row = Database :: fetch_array($res)) {
  3563. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3564. $need_fix = true;
  3565. $sql_u = "UPDATE $lp_table SET display_order = $i
  3566. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3567. Database::query($sql_u);
  3568. }
  3569. $row['display_order'] = $i;
  3570. $lps[$row['id']] = $row;
  3571. $lp_order[$i] = $row['id'];
  3572. $i++;
  3573. }
  3574. }
  3575. if ($num > 1) { // If there's only one element, no need to sort.
  3576. $order = $lps[$lp_id]['display_order'];
  3577. if ($order > 1) { // If it's the first element, no need to move up.
  3578. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3579. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3580. Database::query($sql_u1);
  3581. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3582. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3583. Database::query($sql_u2);
  3584. }
  3585. }
  3586. }
  3587. /**
  3588. * Move a learnpath down (display_order)
  3589. * @param integer $lp_id Learnpath ID
  3590. */
  3591. public static function move_down($lp_id)
  3592. {
  3593. $course_id = api_get_course_int_id();
  3594. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3595. $sql = "SELECT * FROM $lp_table
  3596. WHERE c_id = ".$course_id."
  3597. ORDER BY display_order";
  3598. $res = Database::query($sql);
  3599. if ($res === false) {
  3600. return false;
  3601. }
  3602. $lps = array ();
  3603. $lp_order = array ();
  3604. $num = Database :: num_rows($res);
  3605. $max = 0;
  3606. // First check the order is correct, globally (might be wrong because
  3607. // of versions < 1.8.4).
  3608. if ($num > 0) {
  3609. $i = 1;
  3610. while ($row = Database :: fetch_array($res)) {
  3611. $max = $i;
  3612. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3613. $need_fix = true;
  3614. $sql_u = "UPDATE $lp_table SET display_order = $i
  3615. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3616. Database::query($sql_u);
  3617. }
  3618. $row['display_order'] = $i;
  3619. $lps[$row['id']] = $row;
  3620. $lp_order[$i] = $row['id'];
  3621. $i++;
  3622. }
  3623. }
  3624. if ($num > 1) { // If there's only one element, no need to sort.
  3625. $order = $lps[$lp_id]['display_order'];
  3626. if ($order < $max) { // If it's the first element, no need to move up.
  3627. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3628. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3629. Database::query($sql_u1);
  3630. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3631. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3632. Database::query($sql_u2);
  3633. }
  3634. }
  3635. }
  3636. /**
  3637. * Updates learnpath attributes to point to the next element
  3638. * The last part is similar to set_current_item but processing the other way around
  3639. */
  3640. public function next()
  3641. {
  3642. if ($this->debug > 0) {
  3643. error_log('New LP - In learnpath::next()', 0);
  3644. }
  3645. $this->last = $this->get_current_item_id();
  3646. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3647. $this->autocomplete_parents($this->last);
  3648. $new_index = $this->get_next_index();
  3649. if ($this->debug > 2) {
  3650. error_log('New LP - New index: ' . $new_index, 0);
  3651. }
  3652. $this->index = $new_index;
  3653. if ($this->debug > 2) {
  3654. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3655. }
  3656. $this->current = $this->ordered_items[$new_index];
  3657. if ($this->debug > 2) {
  3658. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3659. }
  3660. }
  3661. /**
  3662. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3663. * class, this might be redefined to allow several behaviours depending on the document type.
  3664. * @param integer Resource ID
  3665. * @return boolean True on success, false otherwise
  3666. */
  3667. public function open($id)
  3668. {
  3669. if ($this->debug > 0) {
  3670. error_log('New LP - In learnpath::open()', 0);
  3671. }
  3672. // TODO:
  3673. // set the current resource attribute to this resource
  3674. // switch on element type (redefine in child class?)
  3675. // set status for this item to "opened"
  3676. // start timer
  3677. // initialise score
  3678. $this->index = 0; //or = the last item seen (see $this->last)
  3679. }
  3680. /**
  3681. * Check that all prerequisites are fulfilled. Returns true and an
  3682. * empty string on success, returns false
  3683. * and the prerequisite string on error.
  3684. * This function is based on the rules for aicc_script language as
  3685. * described in the SCORM 1.2 CAM documentation page 108.
  3686. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3687. * @return boolean True if prerequisites are matched, false otherwise -
  3688. * Empty string if true returned, prerequisites string otherwise.
  3689. */
  3690. public function prerequisites_match($itemId = null)
  3691. {
  3692. $debug = $this->debug;
  3693. if ($debug > 0) {
  3694. error_log('In learnpath::prerequisites_match()', 0);
  3695. }
  3696. if (empty($itemId)) {
  3697. $itemId = $this->current;
  3698. }
  3699. $currentItem = $this->getItem($itemId);
  3700. if ($currentItem) {
  3701. if ($this->type == 2) {
  3702. // Getting prereq from scorm
  3703. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3704. } else {
  3705. $prereq_string = $currentItem->get_prereq_string();
  3706. }
  3707. if (empty($prereq_string)) {
  3708. if ($debug > 0) {
  3709. error_log('Found prereq_string is empty return true');
  3710. }
  3711. return true;
  3712. }
  3713. // Clean spaces.
  3714. $prereq_string = str_replace(' ', '', $prereq_string);
  3715. if ($debug > 0) {
  3716. error_log('Found prereq_string: ' . $prereq_string, 0);
  3717. }
  3718. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3719. $result = $currentItem->parse_prereq(
  3720. $prereq_string,
  3721. $this->items,
  3722. $this->refs_list,
  3723. $this->get_user_id()
  3724. );
  3725. if ($result === false) {
  3726. $this->set_error_msg($currentItem->prereq_alert);
  3727. }
  3728. } else {
  3729. $result = true;
  3730. if ($debug > 1) {
  3731. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3732. }
  3733. }
  3734. if ($debug > 1) {
  3735. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3736. }
  3737. return $result;
  3738. }
  3739. /**
  3740. * Updates learnpath attributes to point to the previous element
  3741. * The last part is similar to set_current_item but processing the other way around
  3742. */
  3743. public function previous()
  3744. {
  3745. if ($this->debug > 0) {
  3746. error_log('New LP - In learnpath::previous()', 0);
  3747. }
  3748. $this->last = $this->get_current_item_id();
  3749. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3750. $this->autocomplete_parents($this->last);
  3751. $new_index = $this->get_previous_index();
  3752. $this->index = $new_index;
  3753. $this->current = $this->ordered_items[$new_index];
  3754. }
  3755. /**
  3756. * Publishes a learnpath. This basically means show or hide the learnpath
  3757. * to normal users.
  3758. * Can be used as abstract
  3759. * @param integer Learnpath ID
  3760. * @param string New visibility
  3761. */
  3762. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3763. {
  3764. $action = 'visible';
  3765. if ($set_visibility != 1) {
  3766. $action = 'invisible';
  3767. self::toggle_publish($lp_id, 'i');
  3768. }
  3769. return api_item_property_update(
  3770. api_get_course_info(),
  3771. TOOL_LEARNPATH,
  3772. $lp_id,
  3773. $action,
  3774. api_get_user_id()
  3775. );
  3776. }
  3777. /**
  3778. * Publishes a learnpath. This basically means show or hide the learnpath
  3779. * on the course homepage
  3780. * Can be used as abstract
  3781. * @param integer $lp_id Learnpath id
  3782. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3783. * @return bool
  3784. */
  3785. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3786. {
  3787. $course_id = api_get_course_int_id();
  3788. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3789. $lp_id = intval($lp_id);
  3790. $sql = "SELECT * FROM $tbl_lp
  3791. WHERE c_id = ".$course_id." AND id = $lp_id";
  3792. $result = Database::query($sql);
  3793. if (Database::num_rows($result)) {
  3794. $row = Database :: fetch_array($result);
  3795. $name = domesticate($row['name']);
  3796. if ($set_visibility == 'i') {
  3797. $s = $name . " " . get_lang('LearnpathNotPublished');
  3798. $dialogBox = $s;
  3799. $v = 0;
  3800. }
  3801. if ($set_visibility == 'v') {
  3802. $s = $name . " " . get_lang('LearnpathPublished');
  3803. $dialogBox = $s;
  3804. $v = 1;
  3805. }
  3806. } else {
  3807. return false;
  3808. }
  3809. $session_id = api_get_session_id();
  3810. $session_condition = api_get_session_condition($session_id);
  3811. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3812. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3813. $sql = "SELECT * FROM $tbl_tool
  3814. WHERE
  3815. c_id = ".$course_id." AND
  3816. link='$link' and
  3817. image='scormbuilder.gif' and
  3818. link LIKE '$link%'
  3819. $session_condition
  3820. ";
  3821. $result = Database::query($sql);
  3822. $num = Database :: num_rows($result);
  3823. if ($set_visibility == 'i' && $num > 0) {
  3824. $sql = "DELETE FROM $tbl_tool
  3825. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3826. Database::query($sql);
  3827. } elseif ($set_visibility == 'v' && $num == 0) {
  3828. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3829. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3830. Database::query($sql);
  3831. $insertId = Database::insert_id();
  3832. if ($insertId) {
  3833. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3834. Database::query($sql);
  3835. }
  3836. } elseif ($set_visibility == 'v' && $num > 0) {
  3837. $sql = "UPDATE $tbl_tool SET
  3838. c_id = $course_id,
  3839. name = '$name',
  3840. link = '$link',
  3841. image = 'scormbuilder.gif',
  3842. visibility = '$v',
  3843. admin = '0',
  3844. address = 'pastillegris.gif',
  3845. added_tool = 0,
  3846. session_id = $session_id
  3847. WHERE
  3848. c_id = ".$course_id." AND
  3849. (link='$link' and image='scormbuilder.gif' $session_condition)
  3850. ";
  3851. Database::query($sql);
  3852. } else {
  3853. // Parameter and database incompatible, do nothing, exit.
  3854. return false;
  3855. }
  3856. }
  3857. /**
  3858. * Restart the whole learnpath. Return the URL of the first element.
  3859. * Make sure the results are saved with anoter method. This method should probably be
  3860. * redefined in children classes.
  3861. * To use a similar method statically, use the create_new_attempt() method
  3862. * @return string URL to load in the viewer
  3863. */
  3864. public function restart()
  3865. {
  3866. if ($this->debug > 0) {
  3867. error_log('New LP - In learnpath::restart()', 0);
  3868. }
  3869. // TODO
  3870. // Call autosave method to save the current progress.
  3871. //$this->index = 0;
  3872. if (api_is_invitee()) {
  3873. return false;
  3874. }
  3875. $session_id = api_get_session_id();
  3876. $course_id = api_get_course_int_id();
  3877. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3878. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3879. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3880. if ($this->debug > 2) {
  3881. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3882. }
  3883. $res = Database::query($sql);
  3884. $view_id = Database::insert_id();
  3885. if ($view_id) {
  3886. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3887. Database::query($sql);
  3888. $this->lp_view_id = $view_id;
  3889. $this->attempt = $this->attempt + 1;
  3890. } else {
  3891. $this->error = 'Could not insert into item_view table...';
  3892. return false;
  3893. }
  3894. $this->autocomplete_parents($this->current);
  3895. foreach ($this->items as $index => $dummy) {
  3896. $this->items[$index]->restart();
  3897. $this->items[$index]->set_lp_view($this->lp_view_id);
  3898. }
  3899. $this->first();
  3900. return true;
  3901. }
  3902. /**
  3903. * Saves the current item
  3904. * @return boolean
  3905. */
  3906. public function save_current()
  3907. {
  3908. if ($this->debug > 0) {
  3909. error_log('learnpath::save_current()', 0);
  3910. }
  3911. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3912. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3913. if ($this->debug > 2) {
  3914. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3915. }
  3916. if ($this->debug > 2) {
  3917. error_log('' . print_r($this->items, true), 0);
  3918. }
  3919. if (isset($this->items[$this->current]) &&
  3920. is_object($this->items[$this->current])
  3921. ) {
  3922. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3923. $this->autocomplete_parents($this->current);
  3924. $status = $this->items[$this->current]->get_status();
  3925. $this->update_queue[$this->current] = $status;
  3926. return $res;
  3927. }
  3928. return false;
  3929. }
  3930. /**
  3931. * Saves the given item
  3932. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3933. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3934. * @return boolean
  3935. */
  3936. public function save_item($item_id = null, $from_outside = true)
  3937. {
  3938. $debug = $this->debug;
  3939. if ($debug) {
  3940. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3941. }
  3942. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3943. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3944. if (empty($item_id)) {
  3945. $item_id = intval($_REQUEST['id']);
  3946. }
  3947. if (empty($item_id)) {
  3948. $item_id = $this->get_current_item_id();
  3949. }
  3950. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3951. if ($debug) {
  3952. error_log('Object exists');
  3953. }
  3954. // Saving the item.
  3955. $res = $this->items[$item_id]->save(
  3956. $from_outside,
  3957. $this->prerequisites_match($item_id)
  3958. );
  3959. if ($debug) {
  3960. error_log('update_queue before:');
  3961. error_log(print_r($this->update_queue,1));
  3962. }
  3963. $this->autocomplete_parents($item_id);
  3964. $status = $this->items[$item_id]->get_status();
  3965. $this->update_queue[$item_id] = $status;
  3966. if ($debug) {
  3967. error_log('get_status(): ' . $status);
  3968. error_log('update_queue after:');
  3969. error_log(print_r($this->update_queue,1));
  3970. }
  3971. return $res;
  3972. }
  3973. return false;
  3974. }
  3975. /**
  3976. * Saves the last item seen's ID only in case
  3977. */
  3978. public function save_last()
  3979. {
  3980. $course_id = api_get_course_int_id();
  3981. if ($this->debug > 0) {
  3982. error_log('New LP - In learnpath::save_last()', 0);
  3983. }
  3984. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3985. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3986. if (isset($this->current) && !api_is_invitee()) {
  3987. if ($this->debug > 2) {
  3988. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3989. }
  3990. $sql = "UPDATE $table SET
  3991. last_item = " . intval($this->get_current_item_id()). "
  3992. WHERE
  3993. c_id = $course_id AND
  3994. lp_id = " . $this->get_id() . " AND
  3995. user_id = " . $this->get_user_id()." ".$session_condition;
  3996. if ($this->debug > 2) {
  3997. error_log('New LP - Saving last item seen : ' . $sql, 0);
  3998. }
  3999. Database::query($sql);
  4000. }
  4001. if (!api_is_invitee()) {
  4002. // Save progress.
  4003. list($progress, $text) = $this->get_progress_bar_text('%');
  4004. if ($progress >= 0 && $progress <= 100) {
  4005. $progress = (int) $progress;
  4006. $sql = "UPDATE $table SET
  4007. progress = $progress
  4008. WHERE
  4009. c_id = ".$course_id." AND
  4010. lp_id = " . $this->get_id() . " AND
  4011. user_id = " . $this->get_user_id()." ".$session_condition;
  4012. // Ignore errors as some tables might not have the progress field just yet.
  4013. Database::query($sql);
  4014. $this->progress_db = $progress;
  4015. }
  4016. }
  4017. }
  4018. /**
  4019. * Sets the current item ID (checks if valid and authorized first)
  4020. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4021. */
  4022. public function set_current_item($item_id = null)
  4023. {
  4024. if ($this->debug > 0) {
  4025. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4026. }
  4027. if (empty ($item_id)) {
  4028. if ($this->debug > 2) {
  4029. error_log('New LP - No new current item given, ignore...', 0);
  4030. }
  4031. // Do nothing.
  4032. } else {
  4033. if ($this->debug > 2) {
  4034. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4035. }
  4036. if (is_numeric($item_id)) {
  4037. $item_id = intval($item_id);
  4038. // TODO: Check in database here.
  4039. $this->last = $this->current;
  4040. $this->current = $item_id;
  4041. // TODO: Update $this->index as well.
  4042. foreach ($this->ordered_items as $index => $item) {
  4043. if ($item == $this->current) {
  4044. $this->index = $index;
  4045. break;
  4046. }
  4047. }
  4048. if ($this->debug > 2) {
  4049. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4050. }
  4051. } else {
  4052. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4053. }
  4054. }
  4055. }
  4056. /**
  4057. * Sets the encoding
  4058. * @param string New encoding
  4059. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4060. */
  4061. public function set_encoding($enc = 'UTF-8')
  4062. {
  4063. if ($this->debug > 0) {
  4064. error_log('New LP - In learnpath::set_encoding()', 0);
  4065. }
  4066. $course_id = api_get_course_int_id();
  4067. $enc = api_refine_encoding_id($enc);
  4068. if (empty($enc)) {
  4069. $enc = api_get_system_encoding();
  4070. }
  4071. if (api_is_encoding_supported($enc)) {
  4072. $lp = $this->get_id();
  4073. if ($lp != 0) {
  4074. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4075. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4076. $res = Database::query($sql);
  4077. return $res;
  4078. }
  4079. }
  4080. return false;
  4081. }
  4082. /**
  4083. * Sets the JS lib setting in the database directly.
  4084. * This is the JavaScript library file this lp needs to load on startup
  4085. * @param string Proximity setting
  4086. * @return boolean True on update success. False otherwise.
  4087. */
  4088. public function set_jslib($lib = '')
  4089. {
  4090. if ($this->debug > 0) {
  4091. error_log('New LP - In learnpath::set_jslib()', 0);
  4092. }
  4093. $lp = $this->get_id();
  4094. $course_id = api_get_course_int_id();
  4095. if ($lp != 0) {
  4096. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4097. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4098. $res = Database::query($sql);
  4099. return $res;
  4100. } else {
  4101. return false;
  4102. }
  4103. }
  4104. /**
  4105. * Sets the name of the LP maker (publisher) (and save)
  4106. * @param string Optional string giving the new content_maker of this learnpath
  4107. * @return boolean True
  4108. */
  4109. public function set_maker($name = '')
  4110. {
  4111. if ($this->debug > 0) {
  4112. error_log('New LP - In learnpath::set_maker()', 0);
  4113. }
  4114. if (empty ($name))
  4115. return false;
  4116. $this->maker = $name;
  4117. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4118. $course_id = api_get_course_int_id();
  4119. $lp_id = $this->get_id();
  4120. $sql = "UPDATE $lp_table SET
  4121. content_maker = '" . Database::escape_string($this->maker) . "'
  4122. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4123. if ($this->debug > 2) {
  4124. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4125. }
  4126. Database::query($sql);
  4127. return true;
  4128. }
  4129. /**
  4130. * Sets the name of the current learnpath (and save)
  4131. * @param string $name Optional string giving the new name of this learnpath
  4132. * @return boolean True/False
  4133. */
  4134. public function set_name($name = null)
  4135. {
  4136. if ($this->debug > 0) {
  4137. error_log('New LP - In learnpath::set_name()', 0);
  4138. }
  4139. if (empty($name)) {
  4140. return false;
  4141. }
  4142. $this->name = Database::escape_string($name);
  4143. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4144. $lp_id = $this->get_id();
  4145. $course_id = $this->course_info['real_id'];
  4146. $sql = "UPDATE $lp_table SET
  4147. name = '" . Database::escape_string($this->name). "'
  4148. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4149. if ($this->debug > 2) {
  4150. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4151. }
  4152. $result = Database::query($sql);
  4153. // If the lp is visible on the homepage, change his name there.
  4154. if (Database::affected_rows($result)) {
  4155. $session_id = api_get_session_id();
  4156. $session_condition = api_get_session_condition($session_id);
  4157. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4158. $link = 'lp/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4159. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4160. WHERE
  4161. c_id = $course_id AND
  4162. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4163. Database::query($sql);
  4164. return true;
  4165. } else {
  4166. return false;
  4167. }
  4168. }
  4169. /**
  4170. * Set index specified prefix terms for all items in this path
  4171. * @param string Comma-separated list of terms
  4172. * @param char Xapian term prefix
  4173. * @return boolean False on error, true otherwise
  4174. */
  4175. public function set_terms_by_prefix($terms_string, $prefix)
  4176. {
  4177. $course_id = api_get_course_int_id();
  4178. if (api_get_setting('search_enabled') !== 'true')
  4179. return false;
  4180. if (!extension_loaded('xapian')) {
  4181. return false;
  4182. }
  4183. $terms_string = trim($terms_string);
  4184. $terms = explode(',', $terms_string);
  4185. array_walk($terms, 'trim_value');
  4186. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4187. // Don't do anything if no change, verify only at DB, not the search engine.
  4188. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4189. return false;
  4190. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4191. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4192. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4193. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4194. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4195. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4196. $lp_id = intval($_POST['lp_id']);
  4197. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4198. $result = Database::query($sql);
  4199. $di = new ChamiloIndexer();
  4200. while ($lp_item = Database :: fetch_array($result)) {
  4201. // Get search_did.
  4202. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4203. $sql = 'SELECT * FROM %s
  4204. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4205. LIMIT 1';
  4206. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4207. //echo $sql; echo '<br>';
  4208. $res = Database::query($sql);
  4209. if (Database::num_rows($res) > 0) {
  4210. $se_ref = Database :: fetch_array($res);
  4211. // Compare terms.
  4212. $doc = $di->get_document($se_ref['search_did']);
  4213. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4214. $xterms = array();
  4215. foreach ($xapian_terms as $xapian_term) {
  4216. $xterms[] = substr($xapian_term['name'], 1);
  4217. }
  4218. $dterms = $terms;
  4219. $missing_terms = array_diff($dterms, $xterms);
  4220. $deprecated_terms = array_diff($xterms, $dterms);
  4221. // Save it to search engine.
  4222. foreach ($missing_terms as $term) {
  4223. $doc->add_term($prefix . $term, 1);
  4224. }
  4225. foreach ($deprecated_terms as $term) {
  4226. $doc->remove_term($prefix . $term);
  4227. }
  4228. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4229. $di->getDb()->flush();
  4230. } else {
  4231. //@todo What we should do here?
  4232. }
  4233. }
  4234. return true;
  4235. }
  4236. /**
  4237. * Sets the theme of the LP (local/remote) (and save)
  4238. * @param string Optional string giving the new theme of this learnpath
  4239. * @return bool Returns true if theme name is not empty
  4240. */
  4241. public function set_theme($name = '')
  4242. {
  4243. $course_id = api_get_course_int_id();
  4244. if ($this->debug > 0) {
  4245. error_log('New LP - In learnpath::set_theme()', 0);
  4246. }
  4247. $this->theme = $name;
  4248. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4249. $lp_id = $this->get_id();
  4250. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4251. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4252. if ($this->debug > 2) {
  4253. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4254. }
  4255. Database::query($sql);
  4256. return true;
  4257. }
  4258. /**
  4259. * Sets the image of an LP (and save)
  4260. * @param string Optional string giving the new image of this learnpath
  4261. * @return bool Returns true if theme name is not empty
  4262. */
  4263. public function set_preview_image($name = '')
  4264. {
  4265. $course_id = api_get_course_int_id();
  4266. if ($this->debug > 0) {
  4267. error_log('New LP - In learnpath::set_preview_image()', 0);
  4268. }
  4269. $this->preview_image = $name;
  4270. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4271. $lp_id = $this->get_id();
  4272. $sql = "UPDATE $lp_table SET
  4273. preview_image = '" . Database::escape_string($this->preview_image). "'
  4274. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4275. if ($this->debug > 2) {
  4276. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4277. }
  4278. Database::query($sql);
  4279. return true;
  4280. }
  4281. /**
  4282. * Sets the author of a LP (and save)
  4283. * @param string Optional string giving the new author of this learnpath
  4284. * @return bool Returns true if author's name is not empty
  4285. */
  4286. public function set_author($name = '')
  4287. {
  4288. $course_id = api_get_course_int_id();
  4289. if ($this->debug > 0) {
  4290. error_log('New LP - In learnpath::set_author()', 0);
  4291. }
  4292. $this->author = $name;
  4293. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4294. $lp_id = $this->get_id();
  4295. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4296. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4297. if ($this->debug > 2) {
  4298. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4299. }
  4300. Database::query($sql);
  4301. return true;
  4302. }
  4303. /**
  4304. * Sets the hide_toc_frame parameter of a LP (and save)
  4305. * @param int 1 if frame is hidden 0 then else
  4306. * @return bool Returns true if author's name is not empty
  4307. */
  4308. public function set_hide_toc_frame($hide)
  4309. {
  4310. $course_id = api_get_course_int_id();
  4311. if ($this->debug > 0) {
  4312. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4313. }
  4314. if (intval($hide) == $hide) {
  4315. $this->hide_toc_frame = $hide;
  4316. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4317. $lp_id = $this->get_id();
  4318. $sql = "UPDATE $lp_table SET
  4319. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4320. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4321. if ($this->debug > 2) {
  4322. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4323. }
  4324. Database::query($sql);
  4325. return true;
  4326. } else {
  4327. return false;
  4328. }
  4329. }
  4330. /**
  4331. * Sets the prerequisite of a LP (and save)
  4332. * @param int integer giving the new prerequisite of this learnpath
  4333. * @return bool returns true if prerequisite is not empty
  4334. */
  4335. public function set_prerequisite($prerequisite)
  4336. {
  4337. $course_id = api_get_course_int_id();
  4338. if ($this->debug > 0) {
  4339. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4340. }
  4341. $this->prerequisite = intval($prerequisite);
  4342. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4343. $lp_id = $this->get_id();
  4344. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4345. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4346. if ($this->debug > 2) {
  4347. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4348. }
  4349. Database::query($sql);
  4350. return true;
  4351. }
  4352. /**
  4353. * Sets the location/proximity of the LP (local/remote) (and save)
  4354. * @param string Optional string giving the new location of this learnpath
  4355. * @return boolean True on success / False on error
  4356. */
  4357. public function set_proximity($name = '')
  4358. {
  4359. $course_id = api_get_course_int_id();
  4360. if ($this->debug > 0) {
  4361. error_log('New LP - In learnpath::set_proximity()', 0);
  4362. }
  4363. if (empty ($name))
  4364. return false;
  4365. $this->proximity = $name;
  4366. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4367. $lp_id = $this->get_id();
  4368. $sql = "UPDATE $lp_table SET
  4369. content_local = '" . Database::escape_string($name) . "'
  4370. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4371. if ($this->debug > 2) {
  4372. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4373. }
  4374. Database::query($sql);
  4375. return true;
  4376. }
  4377. /**
  4378. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4379. * @param integer DB ID of the item
  4380. */
  4381. public function set_previous_item($id)
  4382. {
  4383. if ($this->debug > 0) {
  4384. error_log('New LP - In learnpath::set_previous_item()', 0);
  4385. }
  4386. $this->last = $id;
  4387. }
  4388. /**
  4389. * Sets use_max_score
  4390. * @param string $use_max_score Optional string giving the new location of this learnpath
  4391. * @return boolean True on success / False on error
  4392. */
  4393. public function set_use_max_score($use_max_score = 1)
  4394. {
  4395. $course_id = api_get_course_int_id();
  4396. if ($this->debug > 0) {
  4397. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4398. }
  4399. $use_max_score = intval($use_max_score);
  4400. $this->use_max_score = $use_max_score;
  4401. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4402. $lp_id = $this->get_id();
  4403. $sql = "UPDATE $lp_table SET
  4404. use_max_score = '" . $this->use_max_score . "'
  4405. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4406. if ($this->debug > 2) {
  4407. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4408. }
  4409. Database::query($sql);
  4410. return true;
  4411. }
  4412. /**
  4413. * Sets and saves the expired_on date
  4414. * @param string $expired_on Optional string giving the new author of this learnpath
  4415. * @return bool Returns true if author's name is not empty
  4416. */
  4417. public function set_expired_on($expired_on)
  4418. {
  4419. $course_id = api_get_course_int_id();
  4420. if ($this->debug > 0) {
  4421. error_log('New LP - In learnpath::set_expired_on()', 0);
  4422. }
  4423. if (!empty($expired_on)) {
  4424. $this->expired_on = api_get_utc_datetime($expired_on);
  4425. } else {
  4426. $this->expired_on = null;
  4427. }
  4428. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4429. $lp_id = $this->get_id();
  4430. if ($this->debug > 2) {
  4431. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4432. }
  4433. $params = [
  4434. 'expired_on' => $this->expired_on,
  4435. ];
  4436. Database::update($lp_table, $params, ['c_id = ? AND id = ?' => [$course_id, $lp_id]], true);
  4437. return true;
  4438. }
  4439. /**
  4440. * Sets and saves the publicated_on date
  4441. * @param string $publicated_on Optional string giving the new author of this learnpath
  4442. * @return bool Returns true if author's name is not empty
  4443. */
  4444. public function set_publicated_on($publicated_on)
  4445. {
  4446. $course_id = api_get_course_int_id();
  4447. if ($this->debug > 0) {
  4448. error_log('New LP - In learnpath::set_expired_on()', 0);
  4449. }
  4450. if (!empty($publicated_on)) {
  4451. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4452. } else {
  4453. $this->publicated_on = '';
  4454. }
  4455. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4456. $lp_id = $this->get_id();
  4457. $sql = "UPDATE $lp_table SET
  4458. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4459. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4460. if ($this->debug > 2) {
  4461. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4462. }
  4463. Database::query($sql);
  4464. return true;
  4465. }
  4466. /**
  4467. * Sets and saves the expired_on date
  4468. * @return bool Returns true if author's name is not empty
  4469. */
  4470. public function set_modified_on()
  4471. {
  4472. $course_id = api_get_course_int_id();
  4473. if ($this->debug > 0) {
  4474. error_log('New LP - In learnpath::set_expired_on()', 0);
  4475. }
  4476. $this->modified_on = api_get_utc_datetime();
  4477. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4478. $lp_id = $this->get_id();
  4479. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4480. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4481. if ($this->debug > 2) {
  4482. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4483. }
  4484. Database::query($sql);
  4485. return true;
  4486. }
  4487. /**
  4488. * Sets the object's error message
  4489. * @param string Error message. If empty, reinits the error string
  4490. * @return void
  4491. */
  4492. public function set_error_msg($error = '')
  4493. {
  4494. if ($this->debug > 0) {
  4495. error_log('New LP - In learnpath::set_error_msg()', 0);
  4496. }
  4497. if (empty ($error)) {
  4498. $this->error = '';
  4499. } else {
  4500. $this->error .= $error;
  4501. }
  4502. }
  4503. /**
  4504. * Launches the current item if not 'sco'
  4505. * (starts timer and make sure there is a record ready in the DB)
  4506. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4507. * @return boolean
  4508. */
  4509. public function start_current_item($allow_new_attempt = false)
  4510. {
  4511. if ($this->debug > 0) {
  4512. error_log('New LP - In learnpath::start_current_item()', 0);
  4513. }
  4514. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4515. $type = $this->get_type();
  4516. $item_type = $this->items[$this->current]->get_type();
  4517. if (($type == 2 && $item_type != 'sco') ||
  4518. ($type == 3 && $item_type != 'au') ||
  4519. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4520. ) {
  4521. $this->items[$this->current]->open($allow_new_attempt);
  4522. $this->autocomplete_parents($this->current);
  4523. $prereq_check = $this->prerequisites_match($this->current);
  4524. $this->items[$this->current]->save(false, $prereq_check);
  4525. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4526. } else {
  4527. // If sco, then it is supposed to have been updated by some other call.
  4528. }
  4529. if ($item_type == 'sco') {
  4530. $this->items[$this->current]->restart();
  4531. }
  4532. }
  4533. if ($this->debug > 0) {
  4534. error_log('New LP - End of learnpath::start_current_item()', 0);
  4535. }
  4536. return true;
  4537. }
  4538. /**
  4539. * Stops the processing and counters for the old item (as held in $this->last)
  4540. * @return boolean True/False
  4541. */
  4542. public function stop_previous_item()
  4543. {
  4544. if ($this->debug > 0) {
  4545. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4546. }
  4547. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4548. if ($this->debug > 2) {
  4549. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4550. }
  4551. switch ($this->get_type()) {
  4552. case '3' :
  4553. if ($this->items[$this->last]->get_type() != 'au') {
  4554. if ($this->debug > 2) {
  4555. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4556. }
  4557. $this->items[$this->last]->close();
  4558. //$this->autocomplete_parents($this->last);
  4559. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4560. } else {
  4561. if ($this->debug > 2) {
  4562. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4563. }
  4564. }
  4565. case '2' :
  4566. if ($this->items[$this->last]->get_type() != 'sco') {
  4567. if ($this->debug > 2) {
  4568. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4569. }
  4570. $this->items[$this->last]->close();
  4571. //$this->autocomplete_parents($this->last);
  4572. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4573. } else {
  4574. if ($this->debug > 2) {
  4575. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4576. }
  4577. }
  4578. break;
  4579. case '1' :
  4580. default :
  4581. if ($this->debug > 2) {
  4582. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4583. }
  4584. $this->items[$this->last]->close();
  4585. break;
  4586. }
  4587. } else {
  4588. if ($this->debug > 2) {
  4589. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4590. }
  4591. return false;
  4592. }
  4593. return true;
  4594. }
  4595. /**
  4596. * Updates the default view mode from fullscreen to embedded and inversely
  4597. * @return string The current default view mode ('fullscreen' or 'embedded')
  4598. */
  4599. public function update_default_view_mode()
  4600. {
  4601. $course_id = api_get_course_int_id();
  4602. if ($this->debug > 0) {
  4603. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4604. }
  4605. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4606. $sql = "SELECT * FROM $lp_table
  4607. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4608. $res = Database::query($sql);
  4609. if (Database :: num_rows($res) > 0) {
  4610. $row = Database :: fetch_array($res);
  4611. $default_view_mode = $row['default_view_mod'];
  4612. $view_mode = $default_view_mode;
  4613. switch ($default_view_mode) {
  4614. case 'fullscreen': // default with popup
  4615. $view_mode = 'embedded';
  4616. break;
  4617. case 'embedded': // default view with left menu
  4618. $view_mode = 'embedframe';
  4619. break;
  4620. case 'embedframe': //folded menu
  4621. $view_mode = 'impress';
  4622. break;
  4623. case 'impress':
  4624. $view_mode = 'fullscreen';
  4625. break;
  4626. }
  4627. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4628. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4629. Database::query($sql);
  4630. $this->mode = $view_mode;
  4631. return $view_mode;
  4632. } else {
  4633. if ($this->debug > 2) {
  4634. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4635. }
  4636. }
  4637. return -1;
  4638. }
  4639. /**
  4640. * Updates the default behaviour about auto-commiting SCORM updates
  4641. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4642. */
  4643. public function update_default_scorm_commit()
  4644. {
  4645. $course_id = api_get_course_int_id();
  4646. if ($this->debug > 0) {
  4647. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4648. }
  4649. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4650. $sql = "SELECT * FROM $lp_table
  4651. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4652. $res = Database::query($sql);
  4653. if (Database :: num_rows($res) > 0) {
  4654. $row = Database :: fetch_array($res);
  4655. $force = $row['force_commit'];
  4656. if ($force == 1) {
  4657. $force = 0;
  4658. $force_return = false;
  4659. } elseif ($force == 0) {
  4660. $force = 1;
  4661. $force_return = true;
  4662. }
  4663. $sql = "UPDATE $lp_table SET force_commit = $force
  4664. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4665. Database::query($sql);
  4666. $this->force_commit = $force_return;
  4667. return $force_return;
  4668. } else {
  4669. if ($this->debug > 2) {
  4670. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4671. }
  4672. }
  4673. return -1;
  4674. }
  4675. /**
  4676. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4677. * @return bool True on success, false on failure
  4678. */
  4679. public function update_display_order()
  4680. {
  4681. $course_id = api_get_course_int_id();
  4682. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4683. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4684. $res = Database::query($sql);
  4685. if ($res === false)
  4686. return false;
  4687. $num = Database :: num_rows($res);
  4688. // First check the order is correct, globally (might be wrong because
  4689. // of versions < 1.8.4).
  4690. if ($num > 0) {
  4691. $i = 1;
  4692. while ($row = Database :: fetch_array($res)) {
  4693. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4694. $need_fix = true;
  4695. $sql = "UPDATE $lp_table SET display_order = $i
  4696. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4697. Database::query($sql);
  4698. }
  4699. $i++;
  4700. }
  4701. }
  4702. return true;
  4703. }
  4704. /**
  4705. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4706. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4707. */
  4708. public function update_reinit()
  4709. {
  4710. $course_id = api_get_course_int_id();
  4711. if ($this->debug > 0) {
  4712. error_log('New LP - In learnpath::update_reinit()', 0);
  4713. }
  4714. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4715. $sql = "SELECT * FROM $lp_table
  4716. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4717. $res = Database::query($sql);
  4718. if (Database :: num_rows($res) > 0) {
  4719. $row = Database :: fetch_array($res);
  4720. $force = $row['prevent_reinit'];
  4721. if ($force == 1) {
  4722. $force = 0;
  4723. } elseif ($force == 0) {
  4724. $force = 1;
  4725. }
  4726. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4727. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4728. Database::query($sql);
  4729. $this->prevent_reinit = $force;
  4730. return $force;
  4731. } else {
  4732. if ($this->debug > 2) {
  4733. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4734. }
  4735. }
  4736. return -1;
  4737. }
  4738. /**
  4739. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4740. *
  4741. * @return string 'single', 'multi' or 'seriousgame'
  4742. * @author ndiechburg <noel@cblue.be>
  4743. **/
  4744. public function get_attempt_mode()
  4745. {
  4746. //Set default value for seriousgame_mode
  4747. if (!isset($this->seriousgame_mode)) {
  4748. $this->seriousgame_mode=0;
  4749. }
  4750. // Set default value for prevent_reinit
  4751. if (!isset($this->prevent_reinit)) {
  4752. $this->prevent_reinit =1;
  4753. }
  4754. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4755. return 'seriousgame';
  4756. }
  4757. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4758. return 'single';
  4759. }
  4760. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4761. return 'multiple';
  4762. }
  4763. return 'single';
  4764. }
  4765. /**
  4766. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4767. *
  4768. * @param string 'seriousgame', 'single' or 'multiple'
  4769. * @return boolean
  4770. * @author ndiechburg <noel@cblue.be>
  4771. **/
  4772. public function set_attempt_mode($mode)
  4773. {
  4774. $course_id = api_get_course_int_id();
  4775. switch ($mode) {
  4776. case 'seriousgame' :
  4777. $sg_mode = 1;
  4778. $prevent_reinit = 1;
  4779. break;
  4780. case 'single' :
  4781. $sg_mode = 0;
  4782. $prevent_reinit = 1;
  4783. break;
  4784. case 'multiple' :
  4785. $sg_mode = 0;
  4786. $prevent_reinit = 0;
  4787. break;
  4788. default :
  4789. $sg_mode = 0;
  4790. $prevent_reinit = 0;
  4791. break;
  4792. }
  4793. $this->prevent_reinit = $prevent_reinit;
  4794. $this->seriousgame_mode = $sg_mode;
  4795. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4796. $sql = "UPDATE $lp_table SET
  4797. prevent_reinit = $prevent_reinit ,
  4798. seriousgame_mode = $sg_mode
  4799. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4800. $res = Database::query($sql);
  4801. if ($res) {
  4802. return true;
  4803. } else {
  4804. return false;
  4805. }
  4806. }
  4807. /**
  4808. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4809. *
  4810. * @return boolean
  4811. * @author ndiechburg <noel@cblue.be>
  4812. **/
  4813. public function switch_attempt_mode()
  4814. {
  4815. if ($this->debug > 0) {
  4816. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4817. }
  4818. $mode = $this->get_attempt_mode();
  4819. switch ($mode) {
  4820. case 'single' :
  4821. $next_mode = 'multiple';
  4822. break;
  4823. case 'multiple' :
  4824. $next_mode = 'seriousgame';
  4825. break;
  4826. case 'seriousgame' :
  4827. $next_mode = 'single';
  4828. break;
  4829. default :
  4830. $next_mode = 'single';
  4831. break;
  4832. }
  4833. $this->set_attempt_mode($next_mode);
  4834. }
  4835. /**
  4836. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4837. * but possibility to do again a completed item.
  4838. *
  4839. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4840. * @author ndiechburg <noel@cblue.be>
  4841. **/
  4842. public function set_seriousgame_mode()
  4843. {
  4844. $course_id = api_get_course_int_id();
  4845. if ($this->debug > 0) {
  4846. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4847. }
  4848. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4849. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4850. $res = Database::query($sql);
  4851. if (Database :: num_rows($res) > 0) {
  4852. $row = Database :: fetch_array($res);
  4853. $force = $row['seriousgame_mode'];
  4854. if ($force == 1) {
  4855. $force = 0;
  4856. } elseif ($force == 0) {
  4857. $force = 1;
  4858. }
  4859. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4860. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4861. Database::query($sql);
  4862. $this->seriousgame_mode = $force;
  4863. return $force;
  4864. } else {
  4865. if ($this->debug > 2) {
  4866. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4867. }
  4868. }
  4869. return -1;
  4870. }
  4871. /**
  4872. * Updates the "scorm_debug" value that shows or hide the debug window
  4873. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4874. */
  4875. public function update_scorm_debug()
  4876. {
  4877. $course_id = api_get_course_int_id();
  4878. if ($this->debug > 0) {
  4879. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4880. }
  4881. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4882. $sql = "SELECT * FROM $lp_table
  4883. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4884. $res = Database::query($sql);
  4885. if (Database :: num_rows($res) > 0) {
  4886. $row = Database :: fetch_array($res);
  4887. $force = $row['debug'];
  4888. if ($force == 1) {
  4889. $force = 0;
  4890. } elseif ($force == 0) {
  4891. $force = 1;
  4892. }
  4893. $sql = "UPDATE $lp_table SET debug = $force
  4894. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4895. Database::query($sql);
  4896. $this->scorm_debug = $force;
  4897. return $force;
  4898. } else {
  4899. if ($this->debug > 2) {
  4900. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4901. }
  4902. }
  4903. return -1;
  4904. }
  4905. /**
  4906. * Function that makes a call to the function sort_tree_array and create_tree_array
  4907. * @author Kevin Van Den Haute
  4908. * @param array
  4909. */
  4910. public function tree_array($array)
  4911. {
  4912. if ($this->debug > 1) {
  4913. error_log('New LP - In learnpath::tree_array()', 0);
  4914. }
  4915. $array = $this->sort_tree_array($array);
  4916. $this->create_tree_array($array);
  4917. }
  4918. /**
  4919. * Creates an array with the elements of the learning path tree in it
  4920. *
  4921. * @author Kevin Van Den Haute
  4922. * @param array $array
  4923. * @param int $parent
  4924. * @param int $depth
  4925. * @param array $tmp
  4926. */
  4927. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4928. {
  4929. if ($this->debug > 1) {
  4930. error_log('New LP - In learnpath::create_tree_array())', 0);
  4931. }
  4932. if (is_array($array)) {
  4933. for ($i = 0; $i < count($array); $i++) {
  4934. if ($array[$i]['parent_item_id'] == $parent) {
  4935. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4936. $tmp[] = $array[$i]['parent_item_id'];
  4937. $depth++;
  4938. }
  4939. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4940. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4941. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4942. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4943. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4944. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4945. $this->arrMenu[] = array(
  4946. 'id' => $array[$i]['id'],
  4947. 'ref' => $ref,
  4948. 'item_type' => $array[$i]['item_type'],
  4949. 'title' => $array[$i]['title'],
  4950. 'path' => $path,
  4951. 'description' => $array[$i]['description'],
  4952. 'parent_item_id' => $array[$i]['parent_item_id'],
  4953. 'previous_item_id' => $array[$i]['previous_item_id'],
  4954. 'next_item_id' => $array[$i]['next_item_id'],
  4955. 'min_score' => $array[$i]['min_score'],
  4956. 'max_score' => $array[$i]['max_score'],
  4957. 'mastery_score' => $array[$i]['mastery_score'],
  4958. 'display_order' => $array[$i]['display_order'],
  4959. 'prerequisite' => $preq,
  4960. 'depth' => $depth,
  4961. 'audio' => $audio,
  4962. 'prerequisite_min_score' => $prerequisiteMinScore,
  4963. 'prerequisite_max_score' => $prerequisiteMaxScore
  4964. );
  4965. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4966. }
  4967. }
  4968. }
  4969. }
  4970. /**
  4971. * Sorts a multi dimensional array by parent id and display order
  4972. * @author Kevin Van Den Haute
  4973. *
  4974. * @param array $array (array with al the learning path items in it)
  4975. *
  4976. * @return array
  4977. */
  4978. public function sort_tree_array($array) {
  4979. foreach ($array as $key => $row) {
  4980. $parent[$key] = $row['parent_item_id'];
  4981. $position[$key] = $row['display_order'];
  4982. }
  4983. if (count($array) > 0)
  4984. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4985. return $array;
  4986. }
  4987. /**
  4988. * Function that creates a html list of learning path items so that we can add audio files to them
  4989. * @author Kevin Van Den Haute
  4990. * @param int $lp_id
  4991. * @return string
  4992. */
  4993. public function overview()
  4994. {
  4995. if ($this->debug > 0) {
  4996. error_log('New LP - In learnpath::overview()', 0);
  4997. }
  4998. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4999. $return = '';
  5000. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5001. // we need to start a form when we want to update all the mp3 files
  5002. if ($update_audio == 'true') {
  5003. $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">';
  5004. }
  5005. $return .= '<div id="message"></div>';
  5006. if (count($this->items) == 0) {
  5007. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5008. } else {
  5009. $return_audio = '<table class="data_table">';
  5010. $return_audio .= '<tr>';
  5011. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5012. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5013. $return_audio .= '</tr>';
  5014. if ($update_audio != 'true') {
  5015. $return .= '<div class="col-md-12">';
  5016. $return .= self::return_new_tree($update_audio);
  5017. $return .='</div>';
  5018. $return .= Display::div(
  5019. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5020. array('style' => 'float:left; margin-top:15px;width:100%')
  5021. );
  5022. } else {
  5023. $return_audio .= self::return_new_tree($update_audio);
  5024. $return .= $return_audio.'</table>';
  5025. }
  5026. // We need to close the form when we are updating the mp3 files.
  5027. if ($update_audio == 'true') {
  5028. $return .= '<div class="footer-audio">';
  5029. $return .= Display::button(
  5030. 'save_audio',
  5031. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5032. array('class' => 'btn btn-primary', 'type' => 'submit')
  5033. );
  5034. $return .= '</div>';
  5035. }
  5036. }
  5037. // We need to close the form when we are updating the mp3 files.
  5038. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5039. $return .= '</form>';
  5040. }
  5041. return $return;
  5042. }
  5043. /**
  5044. * @param string string $update_audio
  5045. * @param bool $drop_element_here
  5046. * @return string
  5047. */
  5048. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5049. {
  5050. $return = '';
  5051. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5052. $course_id = api_get_course_int_id();
  5053. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5054. $sql = "SELECT * FROM $tbl_lp_item
  5055. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5056. $result = Database::query($sql);
  5057. $arrLP = array();
  5058. while ($row = Database :: fetch_array($result)) {
  5059. $arrLP[] = array(
  5060. 'id' => $row['id'],
  5061. 'item_type' => $row['item_type'],
  5062. 'title' => Security :: remove_XSS($row['title']),
  5063. 'path' => $row['path'],
  5064. 'description' => Security::remove_XSS($row['description']),
  5065. 'parent_item_id' => $row['parent_item_id'],
  5066. 'previous_item_id' => $row['previous_item_id'],
  5067. 'next_item_id' => $row['next_item_id'],
  5068. 'max_score' => $row['max_score'],
  5069. 'min_score' => $row['min_score'],
  5070. 'mastery_score' => $row['mastery_score'],
  5071. 'prerequisite' => $row['prerequisite'],
  5072. 'display_order' => $row['display_order'],
  5073. 'audio' => $row['audio'],
  5074. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5075. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5076. );
  5077. }
  5078. $this->tree_array($arrLP);
  5079. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5080. unset ($this->arrMenu);
  5081. $default_data = null;
  5082. $default_content = null;
  5083. $elements = array();
  5084. $return_audio = null;
  5085. for ($i = 0; $i < count($arrLP); $i++) {
  5086. $title = $arrLP[$i]['title'];
  5087. $title_cut = cut($arrLP[$i]['title'], 25);
  5088. // Link for the documents
  5089. if ($arrLP[$i]['item_type'] == 'document') {
  5090. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5091. $title_cut = Display::url(
  5092. $title_cut,
  5093. $url,
  5094. array(
  5095. 'class' => 'ajax moved',
  5096. 'data-title' => $title_cut
  5097. )
  5098. );
  5099. }
  5100. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5101. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5102. Session::write('pathItem', $arrLP[$i]['path']);
  5103. }
  5104. if (($i % 2) == 0) {
  5105. $oddClass = 'row_odd';
  5106. } else {
  5107. $oddClass = 'row_even';
  5108. }
  5109. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5110. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5111. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5112. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5113. } else {
  5114. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5115. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5116. } else {
  5117. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5118. $icon = Display::return_icon('certificate.png');
  5119. } else {
  5120. $icon = Display::return_icon('folder_document.gif');
  5121. }
  5122. }
  5123. }
  5124. // The audio column.
  5125. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5126. $audio = '';
  5127. if (!$update_audio || $update_audio <> 'true') {
  5128. if (!empty($arrLP[$i]['audio'])) {
  5129. } else {
  5130. $audio .= '';
  5131. }
  5132. } else {
  5133. $types = self::getChapterTypes();
  5134. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5135. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5136. if (!empty ($arrLP[$i]['audio'])) {
  5137. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5138. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5139. }
  5140. }
  5141. }
  5142. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5143. $return_audio .= '</td>';
  5144. $move_icon = '';
  5145. $move_item_icon = '';
  5146. $edit_icon = '';
  5147. $delete_icon = '';
  5148. $audio_icon = '';
  5149. $prerequisities_icon = '';
  5150. $forumIcon = '';
  5151. $previewIcon = '';
  5152. if ($is_allowed_to_edit) {
  5153. if (!$update_audio || $update_audio <> 'true') {
  5154. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5155. $move_icon .= '<a class="moved" href="#">';
  5156. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5157. $move_icon .= '</a>';
  5158. }
  5159. }
  5160. // No edit for this item types
  5161. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5162. if ($arrLP[$i]['item_type'] != 'dir') {
  5163. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&view=build&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '" class="btn btn-default">';
  5164. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5165. $edit_icon .= '</a>';
  5166. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5167. if (
  5168. $this->items[$arrLP[$i]['id']]->getForumThread(
  5169. $this->course_int_id,
  5170. $this->lp_session_id
  5171. )
  5172. ) {
  5173. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5174. 'action' => 'dissociate_forum',
  5175. 'id' => $arrLP[$i]['id'],
  5176. 'lp_id' => $this->lp_id
  5177. ]);
  5178. $forumIcon = Display::url(
  5179. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5180. $forumIconUrl,
  5181. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5182. );
  5183. } else {
  5184. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5185. 'action' => 'create_forum',
  5186. 'id' => $arrLP[$i]['id'],
  5187. 'lp_id' => $this->lp_id
  5188. ]);
  5189. $forumIcon = Display::url(
  5190. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5191. $forumIconUrl,
  5192. ['class' => "btn btn-default lp-btn-associate-forum"]
  5193. );
  5194. }
  5195. }
  5196. } else {
  5197. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '" class="btn btn-default">';
  5198. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5199. $edit_icon .= '</a>';
  5200. }
  5201. }
  5202. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '" onclick="return confirmation(\'' . addslashes($title) . '\');" class="btn btn-default">';
  5203. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5204. $delete_icon .= '</a>';
  5205. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5206. if ($arrLP[$i]['item_type'] == 'document') {
  5207. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5208. $previewIcon = Display::url(
  5209. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5210. $urlPreviewLink,
  5211. array(
  5212. 'class' => 'btn btn-default ajax',
  5213. 'data-title' => $arrLP[$i]['title']
  5214. )
  5215. );
  5216. } else {
  5217. $previewIcon = Display::url(
  5218. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5219. $url.'&action=view_item', ['class' => 'btn btn-default']
  5220. );
  5221. }
  5222. if ($arrLP[$i]['item_type'] != 'dir') {
  5223. $prerequisities_icon = Display::url(
  5224. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5225. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5226. );
  5227. $move_item_icon = Display::url(
  5228. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5229. $url.'&action=move_item',
  5230. ['class' => 'btn btn-default']
  5231. );
  5232. $audio_icon = Display::url(
  5233. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5234. $url.'&action=add_audio',
  5235. ['class' => 'btn btn-default']
  5236. );
  5237. }
  5238. }
  5239. if ($update_audio != 'true') {
  5240. $row = $move_icon . ' ' . $icon .
  5241. Display::span($title_cut) .
  5242. Display::tag(
  5243. 'div',
  5244. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5245. array('class'=>'btn-toolbar button_actions')
  5246. );
  5247. } else {
  5248. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5249. }
  5250. $parent_id = $arrLP[$i]['parent_item_id'];
  5251. $default_data[$arrLP[$i]['id']] = $row;
  5252. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5253. if (empty($parent_id)) {
  5254. $elements[$arrLP[$i]['id']]['data'] = $row;
  5255. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5256. } else {
  5257. $parent_arrays = array();
  5258. if ($arrLP[$i]['depth'] > 1) {
  5259. //Getting list of parents
  5260. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5261. foreach($arrLP as $item) {
  5262. if ($item['id'] == $parent_id) {
  5263. if ($item['parent_item_id'] == 0) {
  5264. $parent_id = $item['id'];
  5265. break;
  5266. } else {
  5267. $parent_id = $item['parent_item_id'];
  5268. if (empty($parent_arrays)) {
  5269. $parent_arrays[] = intval($item['id']);
  5270. }
  5271. $parent_arrays[] = $parent_id;
  5272. break;
  5273. }
  5274. }
  5275. }
  5276. }
  5277. }
  5278. if (!empty($parent_arrays)) {
  5279. $parent_arrays = array_reverse($parent_arrays);
  5280. $val = '$elements';
  5281. $x = 0;
  5282. foreach($parent_arrays as $item) {
  5283. if ($x != count($parent_arrays) -1) {
  5284. $val .= '["'.$item.'"]["children"]';
  5285. } else {
  5286. $val .= '["'.$item.'"]["children"]';
  5287. }
  5288. $x++;
  5289. }
  5290. $val .= "";
  5291. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5292. eval($code_str);
  5293. } else {
  5294. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5295. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5296. }
  5297. }
  5298. }
  5299. $list = '<ul id="lp_item_list">';
  5300. $tree = self::print_recursive($elements, $default_data, $default_content);
  5301. if (!empty($tree)) {
  5302. $list .= $tree;
  5303. } else {
  5304. if ($drop_element_here) {
  5305. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5306. }
  5307. }
  5308. $list .= '</ul>';
  5309. $return .= Display::panelCollapse(
  5310. $this->name,
  5311. $list,
  5312. 'scorm-list',
  5313. null,
  5314. 'scorm-list-accordion',
  5315. 'scorm-list-collapse'
  5316. );
  5317. if ($update_audio == 'true') {
  5318. $return = $return_audio;
  5319. }
  5320. return $return;
  5321. }
  5322. /**
  5323. * @param array $elements
  5324. * @param array $default_data
  5325. * @param array $default_content
  5326. * @return string
  5327. */
  5328. public function print_recursive($elements, $default_data, $default_content)
  5329. {
  5330. $return = '';
  5331. foreach ($elements as $key => $item) {
  5332. if (isset($item['load_data']) || empty($item['data'])) {
  5333. $item['data'] = $default_data[$item['load_data']];
  5334. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5335. }
  5336. $sub_list = '';
  5337. if (isset($item['type']) && $item['type'] == 'dir') {
  5338. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5339. }
  5340. if (empty($item['children'])) {
  5341. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5342. $active = null;
  5343. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5344. $active = 'active';
  5345. }
  5346. $return .= Display::tag(
  5347. 'li',
  5348. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5349. array('id'=>$key, 'class'=>'record li_container')
  5350. );
  5351. } else {
  5352. // Sections
  5353. if (isset($item['children'])) {
  5354. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5355. }
  5356. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5357. $return .= Display::tag(
  5358. 'li',
  5359. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5360. array('id'=>$key, 'class'=>'record li_container')
  5361. );
  5362. }
  5363. }
  5364. return $return;
  5365. }
  5366. /**
  5367. * This function builds the action menu
  5368. * @param bool $returnContent Optional
  5369. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5370. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5371. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5372. * @return string
  5373. */
  5374. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5375. {
  5376. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5377. $actionsLeft = '';
  5378. $actionsRight = '';
  5379. $actionsLeft .= Display::url(
  5380. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5381. 'lp_controller.php?'.api_get_cidreq().'&' . http_build_query([
  5382. 'gradebook' => $gradebook,
  5383. 'action' => 'view',
  5384. 'lp_id' => $_SESSION['oLP']->lp_id,
  5385. 'isStudentView' => 'true'
  5386. ])
  5387. );
  5388. $actionsLeft .= Display::url(
  5389. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5390. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5391. 'action' => 'admin_view',
  5392. 'lp_id' => $_SESSION['oLP']->lp_id,
  5393. 'updateaudio' => 'true'
  5394. ])
  5395. );
  5396. if (!$isConfigPage) {
  5397. $actionsLeft .= Display::url(
  5398. Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM),
  5399. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5400. 'action' => 'edit',
  5401. 'lp_id' => $_SESSION['oLP']->lp_id
  5402. ])
  5403. );
  5404. } else {
  5405. $actionsLeft .= Display::url(
  5406. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5407. 'lp_controller.php?' . http_build_query([
  5408. 'action' => 'build',
  5409. 'lp_id' => $_SESSION['oLP']->lp_id
  5410. ]) . '&' . api_get_cidreq()
  5411. );
  5412. }
  5413. if ($allowExpand) {
  5414. $actionsLeft .= Display::url(
  5415. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM) .
  5416. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5417. '#',
  5418. ['role' => 'button', 'id' => 'hide_bar_template']
  5419. );
  5420. }
  5421. if ($showRequirementButtons) {
  5422. $buttons = array(
  5423. array(
  5424. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5425. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5426. 'action' => 'set_previous_step_as_prerequisite',
  5427. 'lp_id' => $_SESSION['oLP']->lp_id
  5428. ])
  5429. ),
  5430. array(
  5431. 'title' => get_lang('ClearAllPrerequisites'),
  5432. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5433. 'action' => 'clear_prerequisites',
  5434. 'lp_id' => $_SESSION['oLP']->lp_id
  5435. ])
  5436. ),
  5437. );
  5438. $actionsRight = Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5439. }
  5440. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5441. if ($returnContent) {
  5442. return $toolbar;
  5443. }
  5444. echo $toolbar;
  5445. }
  5446. /**
  5447. * Creates the default learning path folder
  5448. * @param array $course
  5449. * @param int $creatorId
  5450. *
  5451. * @return bool
  5452. */
  5453. public static function generate_learning_path_folder($course, $creatorId = 0)
  5454. {
  5455. // Creating learning_path folder
  5456. $dir = '/learning_path';
  5457. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5458. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5459. $folder = false;
  5460. if (!is_dir($filepath.'/'.$dir)) {
  5461. $folderData = create_unexisting_directory(
  5462. $course,
  5463. $creatorId,
  5464. api_get_session_id(),
  5465. 0,
  5466. 0,
  5467. $filepath,
  5468. $dir,
  5469. get_lang('LearningPaths'),
  5470. 0
  5471. );
  5472. if (!empty($folderData)) {
  5473. $folder = true;
  5474. }
  5475. } else {
  5476. $folder = true;
  5477. }
  5478. return $folder;
  5479. }
  5480. /**
  5481. * @param array $course
  5482. * @param string $lp_name
  5483. * @param int $creatorId
  5484. *
  5485. * @return array
  5486. */
  5487. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5488. {
  5489. $filepath = '';
  5490. $dir = '/learning_path/';
  5491. if (empty($lp_name)) {
  5492. $lp_name = $this->name;
  5493. }
  5494. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5495. $folder = self::generate_learning_path_folder($course, $creatorId);
  5496. // Limits title size
  5497. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5498. $dir = $dir.$title;
  5499. // Creating LP folder
  5500. $documentId = null;
  5501. if ($folder) {
  5502. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5503. if (!is_dir($filepath.'/'.$dir)) {
  5504. $folderData = create_unexisting_directory(
  5505. $course,
  5506. $creatorId,
  5507. 0,
  5508. 0,
  5509. 0,
  5510. $filepath,
  5511. $dir,
  5512. $lp_name
  5513. );
  5514. if (!empty($folderData)) {
  5515. $folder = true;
  5516. }
  5517. $documentId = $folderData['id'];
  5518. } else {
  5519. $folder = true;
  5520. }
  5521. $dir = $dir.'/';
  5522. if ($folder) {
  5523. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5524. }
  5525. }
  5526. if (empty($documentId)) {
  5527. $dir = api_remove_trailing_slash($dir);
  5528. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5529. }
  5530. $array = array(
  5531. 'dir' => $dir,
  5532. 'filepath' => $filepath,
  5533. 'folder' => $folder,
  5534. 'id' => $documentId
  5535. );
  5536. return $array;
  5537. }
  5538. /**
  5539. * Create a new document //still needs some finetuning
  5540. * @param array $courseInfo
  5541. * @param string $content
  5542. * @param string $title
  5543. * @param string $extension
  5544. * @param int $parentId
  5545. * @param int $creatorId creator id
  5546. *
  5547. * @return string
  5548. */
  5549. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5550. {
  5551. if (!empty($courseInfo)) {
  5552. $course_id = $courseInfo['real_id'];
  5553. } else {
  5554. $course_id = api_get_course_int_id();
  5555. }
  5556. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5557. $sessionId = api_get_session_id();
  5558. // Generates folder
  5559. $result = $this->generate_lp_folder($courseInfo);
  5560. $dir = $result['dir'];
  5561. if (empty($parentId)) {
  5562. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5563. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5564. // Please, do not modify this dirname formatting.
  5565. if (strstr($dir, '..')) {
  5566. $dir = '/';
  5567. }
  5568. if (!empty($dir[0]) && $dir[0] == '.') {
  5569. $dir = substr($dir, 1);
  5570. }
  5571. if (!empty($dir[0]) && $dir[0] != '/') {
  5572. $dir = '/'.$dir;
  5573. }
  5574. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5575. $dir .= '/';
  5576. }
  5577. } else {
  5578. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5579. if (!empty($parentInfo)) {
  5580. $dir = $parentInfo['path'].'/';
  5581. }
  5582. }
  5583. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5584. if (!is_dir($filepath)) {
  5585. $dir = '/';
  5586. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5587. }
  5588. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5589. // is already escaped twice when it gets here.
  5590. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5591. if (!empty($title)) {
  5592. $title = api_replace_dangerous_char(stripslashes($title));
  5593. } else {
  5594. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5595. }
  5596. $title = disable_dangerous_file($title);
  5597. $filename = $title;
  5598. $content = !empty($content) ? $content : $_POST['content_lp'];
  5599. $tmp_filename = $filename;
  5600. $i = 0;
  5601. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5602. $tmp_filename = $filename . '_' . ++ $i;
  5603. }
  5604. $filename = $tmp_filename . '.'.$extension;
  5605. if ($extension == 'html') {
  5606. $content = stripslashes($content);
  5607. $content = str_replace(
  5608. api_get_path(WEB_COURSE_PATH),
  5609. api_get_path(REL_PATH).'courses/',
  5610. $content
  5611. );
  5612. // Change the path of mp3 to absolute.
  5613. // The first regexp deals with :// urls.
  5614. $content = preg_replace(
  5615. "|(flashvars=\"file=)([^:/]+)/|",
  5616. "$1".api_get_path(
  5617. REL_COURSE_PATH
  5618. ).$courseInfo['path'].'/document/',
  5619. $content
  5620. );
  5621. // The second regexp deals with audio/ urls.
  5622. $content = preg_replace(
  5623. "|(flashvars=\"file=)([^/]+)/|",
  5624. "$1".api_get_path(
  5625. REL_COURSE_PATH
  5626. ).$courseInfo['path'].'/document/$2/',
  5627. $content
  5628. );
  5629. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5630. $content = str_replace(
  5631. '</body>',
  5632. '<style type="text/css">body{}</style></body>',
  5633. $content
  5634. );
  5635. }
  5636. if (!file_exists($filepath . $filename)) {
  5637. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5638. fputs($fp, $content);
  5639. fclose($fp);
  5640. $file_size = filesize($filepath . $filename);
  5641. $save_file_path = $dir.$filename;
  5642. $document_id = add_document(
  5643. $courseInfo,
  5644. $save_file_path,
  5645. 'file',
  5646. $file_size,
  5647. $tmp_filename,
  5648. '',
  5649. 0, //readonly
  5650. true,
  5651. null,
  5652. $sessionId,
  5653. $creatorId
  5654. );
  5655. if ($document_id) {
  5656. api_item_property_update(
  5657. $courseInfo,
  5658. TOOL_DOCUMENT,
  5659. $document_id,
  5660. 'DocumentAdded',
  5661. $creatorId,
  5662. null,
  5663. null,
  5664. null,
  5665. null,
  5666. $sessionId
  5667. );
  5668. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5669. $new_title = $originalTitle;
  5670. if ($new_comment || $new_title) {
  5671. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5672. $ct = '';
  5673. if ($new_comment)
  5674. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5675. if ($new_title)
  5676. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5677. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5678. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5679. Database::query($sql);
  5680. }
  5681. }
  5682. return $document_id;
  5683. }
  5684. }
  5685. }
  5686. /**
  5687. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5688. * @param array $_course array
  5689. * @return void
  5690. */
  5691. public function edit_document($_course)
  5692. {
  5693. $course_id = api_get_course_int_id();
  5694. global $_configuration;
  5695. // Please, do not modify this dirname formatting.
  5696. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5697. if (strstr($dir, '..'))
  5698. $dir = '/';
  5699. if ($dir[0] == '.')
  5700. $dir = substr($dir, 1);
  5701. if ($dir[0] != '/')
  5702. $dir = '/' . $dir;
  5703. if ($dir[strlen($dir) - 1] != '/')
  5704. $dir .= '/';
  5705. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5706. if (!is_dir($filepath)) {
  5707. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5708. }
  5709. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5710. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5711. $document_id = intval($_POST['path']);
  5712. $sql = "SELECT path FROM " . $table_doc . "
  5713. WHERE c_id = $course_id AND id = " . $document_id;
  5714. $res = Database::query($sql);
  5715. $row = Database :: fetch_array($res);
  5716. $content = stripslashes($_POST['content_lp']);
  5717. $file = $filepath . $row['path'];
  5718. if ($fp = @ fopen($file, 'w')) {
  5719. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].api_get_path(REL_COURSE_PATH), $content);
  5720. // Change the path of mp3 to absolute.
  5721. // The first regexp deals with :// urls.
  5722. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5723. // The second regexp deals with audio/ urls.
  5724. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5725. fputs($fp, $content);
  5726. fclose($fp);
  5727. $sql = "UPDATE " . $table_doc ." SET
  5728. title='".Database::escape_string($_POST['title'])."'
  5729. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5730. Database::query($sql);
  5731. }
  5732. }
  5733. }
  5734. /**
  5735. * Displays the selected item, with a panel for manipulating the item
  5736. * @param int $item_id
  5737. * @param string $msg
  5738. * @return string
  5739. */
  5740. public function display_item($item_id, $msg = null, $show_actions = true)
  5741. {
  5742. $course_id = api_get_course_int_id();
  5743. $return = '';
  5744. if (is_numeric($item_id)) {
  5745. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5746. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5747. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5748. $result = Database::query($sql);
  5749. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5750. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  5751. // Prevents wrong parent selection for document, see Bug#1251.
  5752. if ($row['item_type'] != 'dir') {
  5753. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5754. }
  5755. if ($show_actions) {
  5756. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5757. }
  5758. $return .= '<div style="padding:10px;">';
  5759. if ($msg != '')
  5760. $return .= $msg;
  5761. $return .= '<h3>'.$row['title'].'</h3>';
  5762. switch ($row['item_type']) {
  5763. case TOOL_QUIZ:
  5764. if (!empty($row['path'])) {
  5765. $exercise = new Exercise();
  5766. $exercise->read($row['path']);
  5767. $return .= $exercise->description.'<br />';
  5768. $return .= Display::url(
  5769. get_lang('GoToExercise'),
  5770. api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . api_get_cidreq() . '&exerciseId=' . $exercise->id,
  5771. ['class' => 'btn btn-primary']
  5772. );
  5773. }
  5774. break;
  5775. case TOOL_DOCUMENT:
  5776. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5777. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5778. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5779. $result = Database::query($sql_doc);
  5780. $path_file = Database::result($result, 0, 0);
  5781. $path_parts = pathinfo($path_file);
  5782. // TODO: Correct the following naive comparisons.
  5783. if (in_array($path_parts['extension'], array(
  5784. 'html',
  5785. 'txt',
  5786. 'png',
  5787. 'jpg',
  5788. 'JPG',
  5789. 'jpeg',
  5790. 'JPEG',
  5791. 'gif',
  5792. 'swf',
  5793. 'pdf',
  5794. 'htm'
  5795. ))) {
  5796. $return .= $this->display_document($row['path'], true, true);
  5797. }
  5798. break;
  5799. case TOOL_HOTPOTATOES:
  5800. $return .= $this->display_document($row['path'], false, true);
  5801. break;
  5802. }
  5803. $return .= '</div>';
  5804. }
  5805. }
  5806. return $return;
  5807. }
  5808. /**
  5809. * Shows the needed forms for editing a specific item
  5810. * @param int $item_id
  5811. * @return string
  5812. */
  5813. public function display_edit_item($item_id)
  5814. {
  5815. $course_id = api_get_course_int_id();
  5816. $return = '';
  5817. if (is_numeric($item_id)) {
  5818. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5819. $sql = "SELECT * FROM $tbl_lp_item
  5820. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5821. $res = Database::query($sql);
  5822. $row = Database::fetch_array($res);
  5823. switch ($row['item_type']) {
  5824. case 'dir':
  5825. case 'asset':
  5826. case 'sco':
  5827. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5828. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5829. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5830. } else {
  5831. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5832. }
  5833. break;
  5834. case TOOL_DOCUMENT:
  5835. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5836. $sql = "SELECT lp.*, doc.path as dir
  5837. FROM " . $tbl_lp_item . " as lp
  5838. LEFT JOIN " . $tbl_doc . " as doc
  5839. ON doc.id = lp.path
  5840. WHERE
  5841. lp.c_id = $course_id AND
  5842. doc.c_id = $course_id AND
  5843. lp.id = " . intval($item_id);
  5844. $res_step = Database::query($sql);
  5845. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5846. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5847. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5848. break;
  5849. case TOOL_LINK:
  5850. $link_id = (string) $row['path'];
  5851. if (ctype_digit($link_id)) {
  5852. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5853. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5854. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5855. $res_link = Database::query($sql_select);
  5856. $row_link = Database :: fetch_array($res_link);
  5857. if (is_array($row_link)) {
  5858. $row['url'] = $row_link['url'];
  5859. }
  5860. }
  5861. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5862. $return .= $this->display_link_form('edit', $item_id, $row);
  5863. break;
  5864. case TOOL_LP_FINAL_ITEM:
  5865. $_SESSION['finalItem'] = true;
  5866. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5867. $sql = "SELECT lp.*, doc.path as dir
  5868. FROM " . $tbl_lp_item . " as lp
  5869. LEFT JOIN " . $tbl_doc . " as doc
  5870. ON doc.id = lp.path
  5871. WHERE
  5872. lp.c_id = $course_id AND
  5873. doc.c_id = $course_id AND
  5874. lp.id = " . intval($item_id);
  5875. $res_step = Database::query($sql);
  5876. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5877. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5878. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5879. break;
  5880. case TOOL_QUIZ:
  5881. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5882. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5883. break;
  5884. case TOOL_HOTPOTATOES:
  5885. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5886. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5887. break;
  5888. case TOOL_STUDENTPUBLICATION:
  5889. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5890. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5891. break;
  5892. case TOOL_FORUM:
  5893. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5894. $return .= $this->display_forum_form('edit', $item_id, $row);
  5895. break;
  5896. case TOOL_THREAD:
  5897. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5898. $return .= $this->display_thread_form('edit', $item_id, $row);
  5899. break;
  5900. }
  5901. }
  5902. return $return;
  5903. }
  5904. /**
  5905. * Function that displays a list with al the resources that
  5906. * could be added to the learning path
  5907. * @return string
  5908. */
  5909. public function display_resources()
  5910. {
  5911. $course_code = api_get_course_id();
  5912. // Get all the docs.
  5913. $documents = $this->get_documents(true);
  5914. // Get all the exercises.
  5915. $exercises = $this->get_exercises();
  5916. // Get all the links.
  5917. $links = $this->get_links();
  5918. // Get all the student publications.
  5919. $works = $this->get_student_publications();
  5920. // Get all the forums.
  5921. $forums = $this->get_forums(null, $course_code);
  5922. // Get the final item form (see BT#11048) .
  5923. $finish = $this->getFinalItemForm();
  5924. $headers = array(
  5925. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5926. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5927. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5928. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5929. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5930. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  5931. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  5932. );
  5933. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5934. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  5935. echo Display::tabs(
  5936. $headers,
  5937. array(
  5938. $documents,
  5939. $exercises,
  5940. $links,
  5941. $works,
  5942. $forums,
  5943. $dir,
  5944. $finish,
  5945. ),
  5946. 'resource_tab'
  5947. );
  5948. return true;
  5949. }
  5950. /**
  5951. * Returns the extension of a document
  5952. * @param string filename
  5953. * @return string Extension (part after the last dot)
  5954. */
  5955. public function get_extension($filename)
  5956. {
  5957. $explode = explode('.', $filename);
  5958. return $explode[count($explode) - 1];
  5959. }
  5960. /**
  5961. * Displays a document by id
  5962. *
  5963. * @param int $id
  5964. * @return string
  5965. */
  5966. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5967. {
  5968. $_course = api_get_course_info();
  5969. $course_id = api_get_course_int_id();
  5970. $return = '';
  5971. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5972. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5973. WHERE c_id = ".$course_id." AND id = " . $id;
  5974. $res_doc = Database::query($sql_doc);
  5975. $row_doc = Database :: fetch_array($res_doc);
  5976. // TODO: Add a path filter.
  5977. if ($iframe) {
  5978. $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>';
  5979. } else {
  5980. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5981. }
  5982. return $return;
  5983. }
  5984. /**
  5985. * Return HTML form to add/edit a quiz
  5986. * @param string Action (add/edit)
  5987. * @param integer Item ID if already exists
  5988. * @param mixed Extra information (quiz ID if integer)
  5989. * @return string HTML form
  5990. */
  5991. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5992. {
  5993. $course_id = api_get_course_int_id();
  5994. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5995. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5996. if ($id != 0 && is_array($extra_info)) {
  5997. $item_title = $extra_info['title'];
  5998. $item_description = $extra_info['description'];
  5999. } elseif (is_numeric($extra_info)) {
  6000. $sql = "SELECT title, description
  6001. FROM " . $tbl_quiz . "
  6002. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6003. $result = Database::query($sql);
  6004. $row = Database::fetch_array($result);
  6005. $item_title = $row['title'];
  6006. $item_description = $row['description'];
  6007. } else {
  6008. $item_title = '';
  6009. $item_description = '';
  6010. }
  6011. $item_title = Security::remove_XSS($item_title);
  6012. $item_description = Security::remove_XSS($item_description);
  6013. if ($id != 0 && is_array($extra_info))
  6014. $parent = $extra_info['parent_item_id'];
  6015. else
  6016. $parent = 0;
  6017. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6018. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6019. $result = Database::query($sql);
  6020. $arrLP = array ();
  6021. while ($row = Database :: fetch_array($result)) {
  6022. $arrLP[] = array (
  6023. 'id' => $row['id'],
  6024. 'item_type' => $row['item_type'],
  6025. 'title' => $row['title'],
  6026. 'path' => $row['path'],
  6027. 'description' => $row['description'],
  6028. 'parent_item_id' => $row['parent_item_id'],
  6029. 'previous_item_id' => $row['previous_item_id'],
  6030. 'next_item_id' => $row['next_item_id'],
  6031. 'display_order' => $row['display_order'],
  6032. 'max_score' => $row['max_score'],
  6033. 'min_score' => $row['min_score'],
  6034. 'mastery_score' => $row['mastery_score'],
  6035. 'prerequisite' => $row['prerequisite'],
  6036. 'max_time_allowed' => $row['max_time_allowed']
  6037. );
  6038. }
  6039. $this->tree_array($arrLP);
  6040. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6041. unset ($this->arrMenu);
  6042. $form = new FormValidator('quiz_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6043. $defaults = [];
  6044. if ($action == 'add') {
  6045. $legend = get_lang('CreateTheExercise');
  6046. } elseif ($action == 'move') {
  6047. $legend = get_lang('MoveTheCurrentExercise');
  6048. } else {
  6049. $legend = get_lang('EditCurrentExecice');
  6050. }
  6051. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6052. $legend .= Display :: return_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6053. }
  6054. $form->addHeader($legend);
  6055. if ($action != 'move') {
  6056. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6057. $defaults['title'] = $item_title;
  6058. }
  6059. // Select for Parent item, root or chapter
  6060. $selectParent = $form->addSelect(
  6061. 'parent',
  6062. get_lang('Parent'),
  6063. [],
  6064. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6065. );
  6066. $selectParent->addOption($this->name, 0);
  6067. $arrHide = array (
  6068. $id
  6069. );
  6070. for ($i = 0; $i < count($arrLP); $i++) {
  6071. if ($action != 'add') {
  6072. if (
  6073. ($arrLP[$i]['item_type'] == 'dir') &&
  6074. !in_array($arrLP[$i]['id'], $arrHide) &&
  6075. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6076. ) {
  6077. $selectParent->addOption(
  6078. $arrLP[$i]['title'],
  6079. $arrLP[$i]['id'],
  6080. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6081. );
  6082. if ($parent == $arrLP[$i]['id']) {
  6083. $selectParent->setSelected($arrLP[$i]['id']);
  6084. }
  6085. } else {
  6086. $arrHide[] = $arrLP[$i]['id'];
  6087. }
  6088. } else {
  6089. if ($arrLP[$i]['item_type'] == 'dir') {
  6090. $selectParent->addOption(
  6091. $arrLP[$i]['title'],
  6092. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6093. );
  6094. if ($parent == $arrLP[$i]['id']) {
  6095. $selectParent->setSelected($arrLP[$i]['id']);
  6096. }
  6097. }
  6098. }
  6099. }
  6100. if (is_array($arrLP)) {
  6101. reset($arrLP);
  6102. }
  6103. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6104. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6105. for ($i = 0; $i < count($arrLP); $i++) {
  6106. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6107. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6108. if (is_array($extra_info)) {
  6109. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6110. $selectPrevious->setSelected($arrLP[$i]['id']);
  6111. }
  6112. } elseif ($action == 'add') {
  6113. $selectPrevious->setSelected($arrLP[$i]['id']);
  6114. }
  6115. }
  6116. }
  6117. if ($action != 'move') {
  6118. $id_prerequisite = 0;
  6119. if (is_array($arrLP)) {
  6120. foreach ($arrLP as $key => $value) {
  6121. if ($value['id'] == $id) {
  6122. $id_prerequisite = $value['prerequisite'];
  6123. break;
  6124. }
  6125. }
  6126. }
  6127. $arrHide = array ();
  6128. for ($i = 0; $i < count($arrLP); $i++) {
  6129. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6130. if (is_array($extra_info)) {
  6131. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6132. $s_selected_position = $arrLP[$i]['id'];
  6133. }
  6134. } elseif ($action == 'add') {
  6135. $s_selected_position = 0;
  6136. }
  6137. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6138. }
  6139. }
  6140. /*// Commented the prerequisites, only visible in edit (exercise).
  6141. $return .= '<tr>';
  6142. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6143. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6144. foreach($arrHide as $key => $value){
  6145. if($key==$s_selected_position && $action == 'add'){
  6146. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6147. }
  6148. elseif($key==$id_prerequisite && $action == 'edit'){
  6149. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6150. }
  6151. else{
  6152. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6153. }
  6154. }
  6155. $return .= "</select></td>";
  6156. */
  6157. /*$return .= '<tr>';
  6158. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6159. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6160. // Remove temporarily the test description.
  6161. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6162. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6163. $return .= '</tr>'; */
  6164. }
  6165. if ($action == 'add') {
  6166. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6167. } else {
  6168. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6169. }
  6170. if ($action == 'move') {
  6171. $form->addHidden('title', $item_title);
  6172. $form->addHidden('description', $item_description);
  6173. }
  6174. if (is_numeric($extra_info)) {
  6175. $form->addHidden('path', $extra_info);
  6176. } elseif (is_array($extra_info)) {
  6177. $form->addHidden('path', $extra_info['path']);
  6178. }
  6179. $form->addHidden('type', TOOL_QUIZ);
  6180. $form->addHidden('post_time', time());
  6181. $form->setDefaults($defaults);
  6182. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6183. }
  6184. /**
  6185. * Addition of Hotpotatoes tests
  6186. * @param string Action
  6187. * @param integer Internal ID of the item
  6188. * @param mixed Extra information - can be an array with title and description indexes
  6189. * @return string HTML structure to display the hotpotatoes addition formular
  6190. */
  6191. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6192. {
  6193. $course_id = api_get_course_int_id();
  6194. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6195. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6196. if ($id != 0 && is_array($extra_info)) {
  6197. $item_title = stripslashes($extra_info['title']);
  6198. $item_description = stripslashes($extra_info['description']);
  6199. } elseif (is_numeric($extra_info)) {
  6200. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6201. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6202. WHERE
  6203. c_id = ".$course_id." AND
  6204. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6205. id = " . (int) $extra_info . "
  6206. ORDER BY id ASC";
  6207. $res_hot = Database::query($sql);
  6208. $row = Database::fetch_array($res_hot);
  6209. $item_title = $row['title'];
  6210. $item_description = $row['description'];
  6211. if (!empty ($row['comment'])) {
  6212. $item_title = $row['comment'];
  6213. }
  6214. } else {
  6215. $item_title = '';
  6216. $item_description = '';
  6217. }
  6218. if ($id != 0 && is_array($extra_info)) {
  6219. $parent = $extra_info['parent_item_id'];
  6220. } else {
  6221. $parent = 0;
  6222. }
  6223. $sql = "SELECT * FROM $tbl_lp_item
  6224. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6225. $result = Database::query($sql);
  6226. $arrLP = array ();
  6227. while ($row = Database :: fetch_array($result)) {
  6228. $arrLP[] = array (
  6229. 'id' => $row['id'],
  6230. 'item_type' => $row['item_type'],
  6231. 'title' => $row['title'],
  6232. 'path' => $row['path'],
  6233. 'description' => $row['description'],
  6234. 'parent_item_id' => $row['parent_item_id'],
  6235. 'previous_item_id' => $row['previous_item_id'],
  6236. 'next_item_id' => $row['next_item_id'],
  6237. 'display_order' => $row['display_order'],
  6238. 'max_score' => $row['max_score'],
  6239. 'min_score' => $row['min_score'],
  6240. 'mastery_score' => $row['mastery_score'],
  6241. 'prerequisite' => $row['prerequisite'],
  6242. 'max_time_allowed' => $row['max_time_allowed']
  6243. );
  6244. }
  6245. $legend = '<legend>';
  6246. if ($action == 'add') {
  6247. $legend .= get_lang('CreateTheExercise');
  6248. } elseif ($action == 'move') {
  6249. $legend .= get_lang('MoveTheCurrentExercise');
  6250. } else {
  6251. $legend .= get_lang('EditCurrentExecice');
  6252. }
  6253. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6254. $legend .= Display:: return_message(
  6255. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6256. );
  6257. }
  6258. $legend .= '</legend>';
  6259. $return = '<form method="POST">';
  6260. $return .= $legend;
  6261. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6262. $return .= '<tr>';
  6263. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6264. $return .= '<td class="input">';
  6265. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6266. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6267. $arrHide = array (
  6268. $id
  6269. );
  6270. if (count($arrLP) > 0) {
  6271. for ($i = 0; $i < count($arrLP); $i++) {
  6272. if ($action != 'add') {
  6273. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6274. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6275. } else {
  6276. $arrHide[] = $arrLP[$i]['id'];
  6277. }
  6278. } else {
  6279. if ($arrLP[$i]['item_type'] == 'dir')
  6280. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6281. }
  6282. }
  6283. reset($arrLP);
  6284. }
  6285. $return .= '</select>';
  6286. $return .= '</td>';
  6287. $return .= '</tr>';
  6288. $return .= '<tr>';
  6289. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6290. $return .= '<td class="input">';
  6291. $return .= '<select id="previous" name="previous" size="1">';
  6292. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6293. for ($i = 0; $i < count($arrLP); $i++) {
  6294. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6295. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6296. $selected = 'selected="selected" ';
  6297. elseif ($action == 'add') $selected = 'selected="selected" ';
  6298. else
  6299. $selected = '';
  6300. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6301. }
  6302. }
  6303. $return .= '</select>';
  6304. $return .= '</td>';
  6305. $return .= '</tr>';
  6306. if ($action != 'move') {
  6307. $return .= '<tr>';
  6308. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6309. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6310. $return .= '</tr>';
  6311. $id_prerequisite = 0;
  6312. if (is_array($arrLP) && count($arrLP) > 0) {
  6313. foreach ($arrLP as $key => $value) {
  6314. if ($value['id'] == $id) {
  6315. $id_prerequisite = $value['prerequisite'];
  6316. break;
  6317. }
  6318. }
  6319. $arrHide = array ();
  6320. for ($i = 0; $i < count($arrLP); $i++) {
  6321. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6322. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6323. $s_selected_position = $arrLP[$i]['id'];
  6324. elseif ($action == 'add') $s_selected_position = 0;
  6325. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6326. }
  6327. }
  6328. }
  6329. }
  6330. $return .= '<tr>';
  6331. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6332. $return .= '</tr>';
  6333. $return .= '</table>';
  6334. if ($action == 'move') {
  6335. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6336. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6337. }
  6338. if (is_numeric($extra_info)) {
  6339. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6340. } elseif (is_array($extra_info)) {
  6341. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6342. }
  6343. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6344. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6345. $return .= '</form>';
  6346. return $return;
  6347. }
  6348. /**
  6349. * Return the form to display the forum edit/add option
  6350. * @param string Action (add/edit)
  6351. * @param integer ID of the lp_item if already exists
  6352. * @param mixed Forum ID or title
  6353. * @return string HTML form
  6354. */
  6355. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6356. {
  6357. $course_id = api_get_course_int_id();
  6358. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6359. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6360. if ($id != 0 && is_array($extra_info)) {
  6361. $item_title = stripslashes($extra_info['title']);
  6362. } elseif (is_numeric($extra_info)) {
  6363. $sql = "SELECT forum_title as title, forum_comment as comment
  6364. FROM " . $tbl_forum . "
  6365. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6366. $result = Database::query($sql);
  6367. $row = Database :: fetch_array($result);
  6368. $item_title = $row['title'];
  6369. $item_description = $row['comment'];
  6370. } else {
  6371. $item_title = '';
  6372. $item_description = '';
  6373. }
  6374. if ($id != 0 && is_array($extra_info)) {
  6375. $parent = $extra_info['parent_item_id'];
  6376. } else {
  6377. $parent = 0;
  6378. }
  6379. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6380. WHERE
  6381. c_id = ".$course_id." AND
  6382. lp_id = " . $this->lp_id;
  6383. $result = Database::query($sql);
  6384. $arrLP = array();
  6385. while ($row = Database :: fetch_array($result)) {
  6386. $arrLP[] = array (
  6387. 'id' => $row['id'],
  6388. 'item_type' => $row['item_type'],
  6389. 'title' => $row['title'],
  6390. 'path' => $row['path'],
  6391. 'description' => $row['description'],
  6392. 'parent_item_id' => $row['parent_item_id'],
  6393. 'previous_item_id' => $row['previous_item_id'],
  6394. 'next_item_id' => $row['next_item_id'],
  6395. 'display_order' => $row['display_order'],
  6396. 'max_score' => $row['max_score'],
  6397. 'min_score' => $row['min_score'],
  6398. 'mastery_score' => $row['mastery_score'],
  6399. 'prerequisite' => $row['prerequisite']
  6400. );
  6401. }
  6402. $this->tree_array($arrLP);
  6403. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6404. unset($this->arrMenu);
  6405. if ($action == 'add') {
  6406. $legend = get_lang('CreateTheForum');
  6407. } elseif ($action == 'move') {
  6408. $legend = get_lang('MoveTheCurrentForum');
  6409. } else {
  6410. $legend = get_lang('EditCurrentForum');
  6411. }
  6412. $form = new FormValidator('forum_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6413. $defaults = [];
  6414. $form->addHeader($legend);
  6415. if ($action != 'move') {
  6416. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  6417. $defaults['title'] = $item_title;
  6418. }
  6419. $selectParent = $form->addSelect(
  6420. 'parent',
  6421. get_lang('Parent'),
  6422. [],
  6423. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6424. );
  6425. $selectParent->addOption($this->name, 0);
  6426. $arrHide = array(
  6427. $id
  6428. );
  6429. //$parent_item_id = $_SESSION['parent_item_id'];
  6430. for ($i = 0; $i < count($arrLP); $i++) {
  6431. if ($action != 'add') {
  6432. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6433. $selectParent->addOption(
  6434. $arrLP[$i]['title'],
  6435. $arrLP[$i]['id'],
  6436. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6437. );
  6438. if ($parent == $arrLP[$i]['id']) {
  6439. $selectParent->setSelected($arrLP[$i]['id']);
  6440. }
  6441. } else {
  6442. $arrHide[] = $arrLP[$i]['id'];
  6443. }
  6444. } else {
  6445. if ($arrLP[$i]['item_type'] == 'dir') {
  6446. $selectParent->addOption(
  6447. $arrLP[$i]['title'],
  6448. $arrLP[$i]['id'],
  6449. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6450. );
  6451. if ($parent == $arrLP[$i]['id']) {
  6452. $selectParent->setSelected($arrLP[$i]['id']);
  6453. }
  6454. }
  6455. }
  6456. }
  6457. if (is_array($arrLP)) {
  6458. reset($arrLP);
  6459. }
  6460. $selectPrevious = $form->addSelect(
  6461. 'previous',
  6462. get_lang('Position'),
  6463. [],
  6464. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6465. );
  6466. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6467. for ($i = 0; $i < count($arrLP); $i++) {
  6468. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6469. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6470. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6471. $selectPrevious->setSelected($arrLP[$i]['id']);
  6472. } elseif ($action == 'add') {
  6473. $selectPrevious->setSelected($arrLP[$i]['id']);
  6474. }
  6475. }
  6476. }
  6477. if ($action != 'move') {
  6478. $id_prerequisite = 0;
  6479. if (is_array($arrLP)) {
  6480. foreach ($arrLP as $key => $value) {
  6481. if ($value['id'] == $id) {
  6482. $id_prerequisite = $value['prerequisite'];
  6483. break;
  6484. }
  6485. }
  6486. }
  6487. $arrHide = array();
  6488. for ($i = 0; $i < count($arrLP); $i++) {
  6489. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6490. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6491. $s_selected_position = $arrLP[$i]['id'];
  6492. elseif ($action == 'add') $s_selected_position = 0;
  6493. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6494. }
  6495. }
  6496. }
  6497. if ($action == 'add') {
  6498. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6499. } else {
  6500. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6501. }
  6502. if ($action == 'move') {
  6503. $form->addHidden('title', $item_title);
  6504. $form->addHidden('description', $item_description);
  6505. }
  6506. if (is_numeric($extra_info)) {
  6507. $form->addHidden('path', $extra_info);
  6508. } elseif (is_array($extra_info)) {
  6509. $form->addHidden('path', $extra_info['path']);
  6510. }
  6511. $form->addHidden('type', TOOL_FORUM);
  6512. $form->addHidden('post_time', time());
  6513. $form->setDefaults($defaults);
  6514. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6515. }
  6516. /**
  6517. * Return HTML form to add/edit forum threads
  6518. * @param string Action (add/edit)
  6519. * @param integer Item ID if already exists in learning path
  6520. * @param mixed Extra information (thread ID if integer)
  6521. * @return string HTML form
  6522. */
  6523. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6524. {
  6525. $course_id = api_get_course_int_id();
  6526. if (empty($course_id)) {
  6527. return null;
  6528. }
  6529. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6530. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6531. if ($id != 0 && is_array($extra_info)) {
  6532. $item_title = stripslashes($extra_info['title']);
  6533. } elseif (is_numeric($extra_info)) {
  6534. $sql = "SELECT thread_title as title FROM $tbl_forum
  6535. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6536. $result = Database::query($sql);
  6537. $row = Database :: fetch_array($result);
  6538. $item_title = $row['title'];
  6539. $item_description = '';
  6540. } else {
  6541. $item_title = '';
  6542. $item_description = '';
  6543. }
  6544. if ($id != 0 && is_array($extra_info)) {
  6545. $parent = $extra_info['parent_item_id'];
  6546. } else {
  6547. $parent = 0;
  6548. }
  6549. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6550. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6551. $result = Database::query($sql);
  6552. $arrLP = array ();
  6553. while ($row = Database :: fetch_array($result)) {
  6554. $arrLP[] = array (
  6555. 'id' => $row['id'],
  6556. 'item_type' => $row['item_type'],
  6557. 'title' => $row['title'],
  6558. 'path' => $row['path'],
  6559. 'description' => $row['description'],
  6560. 'parent_item_id' => $row['parent_item_id'],
  6561. 'previous_item_id' => $row['previous_item_id'],
  6562. 'next_item_id' => $row['next_item_id'],
  6563. 'display_order' => $row['display_order'],
  6564. 'max_score' => $row['max_score'],
  6565. 'min_score' => $row['min_score'],
  6566. 'mastery_score' => $row['mastery_score'],
  6567. 'prerequisite' => $row['prerequisite']
  6568. );
  6569. }
  6570. $this->tree_array($arrLP);
  6571. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6572. unset ($this->arrMenu);
  6573. $form = new FormValidator('thread_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6574. $defaults = [];
  6575. if ($action == 'add') {
  6576. $legend = get_lang('CreateTheForum');
  6577. } elseif ($action == 'move') {
  6578. $legend = get_lang('MoveTheCurrentForum');
  6579. } else {
  6580. $legend = get_lang('EditCurrentForum');
  6581. }
  6582. $form->addHeader($legend);
  6583. $selectParent = $form->addSelect(
  6584. 'parent',
  6585. get_lang('Parent'),
  6586. [],
  6587. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6588. );
  6589. $selectParent->addOption($this->name, 0);
  6590. $arrHide = array (
  6591. $id
  6592. );
  6593. for ($i = 0; $i < count($arrLP); $i++) {
  6594. if ($action != 'add') {
  6595. if (
  6596. ($arrLP[$i]['item_type'] == 'dir') &&
  6597. !in_array($arrLP[$i]['id'], $arrHide) &&
  6598. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6599. ) {
  6600. $selectParent->addOption(
  6601. $arrLP[$i]['title'],
  6602. $arrLP[$i]['id'],
  6603. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6604. );
  6605. if ($parent == $arrLP[$i]['id']) {
  6606. $selectParent->setSelected($arrLP[$i]['id']);
  6607. }
  6608. } else {
  6609. $arrHide[] = $arrLP[$i]['id'];
  6610. }
  6611. } else {
  6612. if ($arrLP[$i]['item_type'] == 'dir') {
  6613. $selectParent->addOption(
  6614. $arrLP[$i]['title'],
  6615. $arrLP[$i]['id'],
  6616. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6617. );
  6618. if ($parent == $arrLP[$i]['id']) {
  6619. $selectParent->setSelected($arrLP[$i]['id']);
  6620. }
  6621. }
  6622. }
  6623. }
  6624. if ($arrLP != null) {
  6625. reset($arrLP);
  6626. }
  6627. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6628. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6629. for ($i = 0; $i < count($arrLP); $i++) {
  6630. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6631. $selectPrevious->addOption(
  6632. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6633. $arrLP[$i]['id']
  6634. );
  6635. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6636. $selectPrevious->setSelected($arrLP[$i]['id']);
  6637. } elseif ($action == 'add') {
  6638. $selectPrevious->setSelected($arrLP[$i]['id']);
  6639. }
  6640. }
  6641. }
  6642. if ($action != 'move') {
  6643. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6644. $defaults['title'] = $item_title;
  6645. $id_prerequisite = 0;
  6646. if ($arrLP != null) {
  6647. foreach ($arrLP as $key => $value) {
  6648. if ($value['id'] == $id) {
  6649. $id_prerequisite = $value['prerequisite'];
  6650. break;
  6651. }
  6652. }
  6653. }
  6654. $arrHide = array();
  6655. $s_selected_position = 0;
  6656. for ($i = 0; $i < count($arrLP); $i++) {
  6657. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6658. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6659. $s_selected_position = $arrLP[$i]['id'];
  6660. elseif ($action == 'add') $s_selected_position = 0;
  6661. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6662. }
  6663. }
  6664. $selectPrerequisites = $form->addSelect(
  6665. 'prerequisites',
  6666. get_lang('LearnpathPrerequisites'),
  6667. [],
  6668. ['id' => 'prerequisites']
  6669. );
  6670. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6671. foreach ($arrHide as $key => $value) {
  6672. $selectPrerequisites->addOption($value['value'], $key);
  6673. if ($key == $s_selected_position && $action == 'add') {
  6674. $selectPrerequisites->setSelected($key);
  6675. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6676. $selectPrerequisites->setSelected($key);
  6677. }
  6678. }
  6679. }
  6680. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6681. if ($action == 'move') {
  6682. $form->addHidden('title', $item_title);
  6683. $form->addHidden('description', $item_description);
  6684. }
  6685. if (is_numeric($extra_info)) {
  6686. $form->addHidden('path', $extra_info);
  6687. }
  6688. elseif (is_array($extra_info)) {
  6689. $form->addHidden('path', $extra_info['path']);
  6690. }
  6691. $form->addHidden('type', TOOL_THREAD);
  6692. $form->addHidden('post_time', time());
  6693. $form->setDefaults($defaults);
  6694. return $form->returnForm();
  6695. }
  6696. /**
  6697. * Return the HTML form to display an item (generally a dir item)
  6698. * @param string Item type (dir)
  6699. * @param string Title (optional, only when creating)
  6700. * @param string Action ('add'/'edit')
  6701. * @param integer lp_item ID
  6702. * @param mixed Extra info
  6703. * @return string HTML form
  6704. */
  6705. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6706. {
  6707. $course_id = api_get_course_int_id();
  6708. $_course = api_get_course_info();
  6709. global $charset;
  6710. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6711. if ($id != 0 && is_array($extra_info)) {
  6712. $item_title = $extra_info['title'];
  6713. $item_description = $extra_info['description'];
  6714. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6715. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6716. } else {
  6717. $item_title = '';
  6718. $item_description = '';
  6719. $item_path_fck = '';
  6720. }
  6721. if ($id != 0 && is_array($extra_info)) {
  6722. $parent = $extra_info['parent_item_id'];
  6723. } else {
  6724. $parent = 0;
  6725. }
  6726. $id = intval($id);
  6727. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6728. WHERE
  6729. c_id = ".$course_id." AND
  6730. lp_id = " . $this->lp_id . " AND
  6731. id != $id";
  6732. if ($item_type == 'dir')
  6733. $sql .= " AND parent_item_id = 0";
  6734. $result = Database::query($sql);
  6735. $arrLP = array ();
  6736. while ($row = Database :: fetch_array($result)) {
  6737. $arrLP[] = array(
  6738. 'id' => $row['id'],
  6739. 'item_type' => $row['item_type'],
  6740. 'title' => $row['title'],
  6741. 'path' => $row['path'],
  6742. 'description' => $row['description'],
  6743. 'parent_item_id' => $row['parent_item_id'],
  6744. 'previous_item_id' => $row['previous_item_id'],
  6745. 'next_item_id' => $row['next_item_id'],
  6746. 'max_score' => $row['max_score'],
  6747. 'min_score' => $row['min_score'],
  6748. 'mastery_score' => $row['mastery_score'],
  6749. 'prerequisite' => $row['prerequisite'],
  6750. 'display_order' => $row['display_order']
  6751. );
  6752. }
  6753. $this->tree_array($arrLP);
  6754. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6755. unset ($this->arrMenu);
  6756. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6757. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6758. $form = new FormValidator('form', 'POST', $url);
  6759. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6760. $defaults['description'] = $item_description;
  6761. $form->addElement('header', $title);
  6762. //$arrHide = array($id);
  6763. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6764. $arrHide[0]['padding'] = 20;
  6765. $charset = api_get_system_encoding();
  6766. for ($i = 0; $i < count($arrLP); $i++) {
  6767. if ($action != 'add') {
  6768. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6769. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6770. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6771. if ($parent == $arrLP[$i]['id']) {
  6772. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6773. }
  6774. }
  6775. } else {
  6776. if ($arrLP[$i]['item_type'] == 'dir') {
  6777. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6778. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6779. if ($parent == $arrLP[$i]['id']) {
  6780. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6781. }
  6782. }
  6783. }
  6784. }
  6785. if ($action != 'move') {
  6786. $form->addElement('text', 'title', get_lang('Title'));
  6787. $form->applyFilter('title', 'html_filter');
  6788. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6789. } else {
  6790. $form->addElement('hidden', 'title');
  6791. }
  6792. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6793. foreach ($arrHide as $key => $value) {
  6794. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6795. }
  6796. if (!empty($s_selected_parent)) {
  6797. $parent_select->setSelected($s_selected_parent);
  6798. }
  6799. if (is_array($arrLP)) {
  6800. reset($arrLP);
  6801. }
  6802. $arrHide = array();
  6803. // POSITION
  6804. for ($i = 0; $i < count($arrLP); $i++) {
  6805. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6806. //this is the same!
  6807. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6808. $s_selected_position = $arrLP[$i]['id'];
  6809. } elseif ($action == 'add') {
  6810. $s_selected_position = $arrLP[$i]['id'];
  6811. }
  6812. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6813. }
  6814. }
  6815. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6816. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6817. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6818. foreach ($arrHide as $key => $value) {
  6819. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6820. }
  6821. if (!empty ($s_selected_position)) {
  6822. $position->setSelected($s_selected_position);
  6823. }
  6824. if (is_array($arrLP)) {
  6825. reset($arrLP);
  6826. }
  6827. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6828. //fix in order to use the tab
  6829. if ($item_type == 'dir') {
  6830. $form->addElement('hidden', 'type', 'dir');
  6831. }
  6832. $extension = null;
  6833. if (!empty($item_path)) {
  6834. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6835. }
  6836. //assets can't be modified
  6837. //$item_type == 'asset' ||
  6838. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6839. if ($item_type == 'sco') {
  6840. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6841. }
  6842. $renderer = $form->defaultRenderer();
  6843. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6844. $relative_prefix = '';
  6845. $editor_config = array(
  6846. 'ToolbarSet' => 'LearningPathDocuments',
  6847. 'Width' => '100%',
  6848. 'Height' => '500',
  6849. 'FullPage' => true,
  6850. 'CreateDocumentDir' => $relative_prefix,
  6851. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  6852. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  6853. );
  6854. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6855. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  6856. $defaults['content_lp'] = file_get_contents($content_path);
  6857. }
  6858. $form->addElement('hidden', 'type', $item_type);
  6859. $form->addElement('hidden', 'post_time', time());
  6860. $form->setDefaults($defaults);
  6861. return $form->return_form();
  6862. }
  6863. /**
  6864. * Returns the form to update or create a document
  6865. * @param string Action (add/edit)
  6866. * @param integer ID of the lp_item (if already exists)
  6867. * @param mixed Integer if document ID, string if info ('new')
  6868. * @return string HTML form
  6869. */
  6870. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6871. {
  6872. $course_id = api_get_course_int_id();
  6873. $_course = api_get_course_info();
  6874. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6875. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6876. $no_display_edit_textarea = false;
  6877. $item_description = '';
  6878. //If action==edit document
  6879. //We don't display the document form if it's not an editable document (html or txt file)
  6880. if ($action == "edit") {
  6881. if (is_array($extra_info)) {
  6882. $path_parts = pathinfo($extra_info['dir']);
  6883. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6884. $no_display_edit_textarea = true;
  6885. }
  6886. }
  6887. }
  6888. $no_display_add = false;
  6889. // If action==add an existing document
  6890. // We don't display the document form if it's not an editable document (html or txt file).
  6891. if ($action == "add") {
  6892. if (is_numeric($extra_info)) {
  6893. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6894. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6895. $result = Database::query($sql_doc);
  6896. $path_file = Database :: result($result, 0, 0);
  6897. $path_parts = pathinfo($path_file);
  6898. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6899. $no_display_add = true;
  6900. }
  6901. }
  6902. }
  6903. if ($id != 0 && is_array($extra_info)) {
  6904. $item_title = stripslashes($extra_info['title']);
  6905. $item_description = stripslashes($extra_info['description']);
  6906. $item_terms = stripslashes($extra_info['terms']);
  6907. if (empty ($item_title)) {
  6908. $path_parts = pathinfo($extra_info['path']);
  6909. $item_title = stripslashes($path_parts['filename']);
  6910. }
  6911. } elseif (is_numeric($extra_info)) {
  6912. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6913. WHERE
  6914. c_id = ".$course_id." AND
  6915. id = " . intval($extra_info);
  6916. $result = Database::query($sql_doc);
  6917. $row = Database::fetch_array($result);
  6918. $item_title = $row['title'];
  6919. $item_title = str_replace('_', ' ', $item_title);
  6920. if (empty ($item_title)) {
  6921. $path_parts = pathinfo($row['path']);
  6922. $item_title = stripslashes($path_parts['filename']);
  6923. }
  6924. } else {
  6925. $item_title = '';
  6926. $item_description = '';
  6927. }
  6928. $return = '<legend>';
  6929. if ($id != 0 && is_array($extra_info)) {
  6930. $parent = $extra_info['parent_item_id'];
  6931. } else {
  6932. $parent = 0;
  6933. }
  6934. $sql = "SELECT * FROM $tbl_lp_item
  6935. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  6936. $result = Database::query($sql);
  6937. $arrLP = array ();
  6938. while ($row = Database :: fetch_array($result)) {
  6939. $arrLP[] = array(
  6940. 'id' => $row['id'],
  6941. 'item_type' => $row['item_type'],
  6942. 'title' => $row['title'],
  6943. 'path' => $row['path'],
  6944. 'description' => $row['description'],
  6945. 'parent_item_id' => $row['parent_item_id'],
  6946. 'previous_item_id' => $row['previous_item_id'],
  6947. 'next_item_id' => $row['next_item_id'],
  6948. 'display_order' => $row['display_order'],
  6949. 'max_score' => $row['max_score'],
  6950. 'min_score' => $row['min_score'],
  6951. 'mastery_score' => $row['mastery_score'],
  6952. 'prerequisite' => $row['prerequisite']
  6953. );
  6954. }
  6955. $this->tree_array($arrLP);
  6956. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6957. unset ($this->arrMenu);
  6958. if ($action == 'add') {
  6959. $return .= get_lang('CreateTheDocument');
  6960. } elseif ($action == 'move') {
  6961. $return .= get_lang('MoveTheCurrentDocument');
  6962. } else {
  6963. $return .= get_lang('EditTheCurrentDocument');
  6964. }
  6965. $return .= '</legend>';
  6966. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6967. $return .= Display :: return_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6968. }
  6969. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6970. $defaults['title'] = Security :: remove_XSS($item_title);
  6971. if (empty($item_title)) {
  6972. $defaults['title'] = Security::remove_XSS($item_title);
  6973. }
  6974. $defaults['description'] = $item_description;
  6975. $form->addElement('html', $return);
  6976. if ($action != 'move') {
  6977. $data = $this->generate_lp_folder($_course);
  6978. $folders = DocumentManager::get_all_document_folders(
  6979. $_course,
  6980. 0,
  6981. true
  6982. );
  6983. DocumentManager::build_directory_selector(
  6984. $folders,
  6985. '',
  6986. array(),
  6987. true,
  6988. $form,
  6989. 'directory_parent_id'
  6990. );
  6991. if (isset($data['id'])) {
  6992. $defaults['directory_parent_id'] = $data['id'];
  6993. }
  6994. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6995. $form->applyFilter('title', 'html_filter');
  6996. }
  6997. $arrHide[0]['value'] = $this->name;
  6998. $arrHide[0]['padding'] = 20;
  6999. for ($i = 0; $i < count($arrLP); $i++) {
  7000. if ($action != 'add') {
  7001. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7002. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7003. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7004. if ($parent == $arrLP[$i]['id']) {
  7005. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7006. }
  7007. }
  7008. } else {
  7009. if ($arrLP[$i]['item_type'] == 'dir') {
  7010. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7011. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7012. if ($parent == $arrLP[$i]['id']) {
  7013. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7014. }
  7015. }
  7016. }
  7017. }
  7018. $parent_select = $form->addSelect('parent', get_lang('Parent'), [], ['id' => 'idParent', 'onchange' => 'javascript: load_cbo(this.value);']);
  7019. $my_count=0;
  7020. foreach ($arrHide as $key => $value) {
  7021. if ($my_count!=0) {
  7022. // The LP name is also the first section and is not in the same charset like the other sections.
  7023. $value['value'] = Security :: remove_XSS($value['value']);
  7024. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7025. } else {
  7026. $value['value'] = Security :: remove_XSS($value['value']);
  7027. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7028. }
  7029. $my_count++;
  7030. }
  7031. if (!empty($id)) {
  7032. $parent_select->setSelected($parent);
  7033. } else {
  7034. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7035. $parent_select->setSelected($parent_item_id);
  7036. }
  7037. if (is_array($arrLP)) {
  7038. reset($arrLP);
  7039. }
  7040. $arrHide = array();
  7041. $s_selected_position = null;
  7042. //POSITION
  7043. for ($i = 0; $i < count($arrLP); $i++) {
  7044. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id || $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  7045. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'] || $action == 'add') {
  7046. $s_selected_position = $arrLP[$i]['id'];
  7047. }
  7048. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7049. }
  7050. }
  7051. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7052. $position->addOption(get_lang('FirstPosition'), 0);
  7053. foreach ($arrHide as $key => $value) {
  7054. $padding = isset($value['padding']) ? $value['padding']: 20;
  7055. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  7056. }
  7057. $position->setSelected($s_selected_position);
  7058. if (is_array($arrLP)) {
  7059. reset($arrLP);
  7060. }
  7061. if ($action != 'move') {
  7062. $id_prerequisite = 0;
  7063. if (is_array($arrLP)) {
  7064. foreach ($arrLP as $key => $value) {
  7065. if ($value['id'] == $id) {
  7066. $id_prerequisite = $value['prerequisite'];
  7067. break;
  7068. }
  7069. }
  7070. }
  7071. $arrHide = array();
  7072. for ($i = 0; $i < count($arrLP); $i++) {
  7073. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7074. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7075. $s_selected_position = $arrLP[$i]['id'];
  7076. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7077. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7078. }
  7079. }
  7080. if (!$no_display_add) {
  7081. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7082. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7083. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true')) {
  7084. if (isset ($_POST['content']))
  7085. $content = stripslashes($_POST['content']);
  7086. elseif (is_array($extra_info)) {
  7087. //If it's an html document or a text file
  7088. if (!$no_display_edit_textarea) {
  7089. $content = $this->display_document($extra_info['path'], false, false);
  7090. }
  7091. } elseif (is_numeric($extra_info))
  7092. $content = $this->display_document($extra_info, false, false);
  7093. else
  7094. $content = '';
  7095. if (!$no_display_edit_textarea) {
  7096. // We need to calculate here some specific settings for the online editor.
  7097. // The calculated settings work for documents in the Documents tool
  7098. // (on the root or in subfolders).
  7099. // For documents in native scorm packages it is unclear whether the
  7100. // online editor should be activated or not.
  7101. // A new document, it is in the root of the repository.
  7102. $relative_path = '';
  7103. $relative_prefix = '';
  7104. if (is_array($extra_info) && $extra_info != 'new') {
  7105. // The document already exists. Whe have to determine its relative path towards the repository root.
  7106. $relative_path = explode('/', $extra_info['dir']);
  7107. $cnt = count($relative_path) - 2;
  7108. if ($cnt < 0) {
  7109. $cnt = 0;
  7110. }
  7111. $relative_prefix = str_repeat('../', $cnt);
  7112. $relative_path = array_slice($relative_path, 1, $cnt);
  7113. $relative_path = implode('/', $relative_path);
  7114. if (strlen($relative_path) > 0) {
  7115. $relative_path = $relative_path . '/';
  7116. }
  7117. } else {
  7118. $result = $this->generate_lp_folder($_course);
  7119. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7120. $relative_prefix = '../../';
  7121. }
  7122. $editor_config = array(
  7123. 'ToolbarSet' => 'LearningPathDocuments',
  7124. 'Width' => '100%',
  7125. 'Height' => '500',
  7126. 'FullPage' => true,
  7127. 'CreateDocumentDir' => $relative_prefix,
  7128. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7129. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7130. );
  7131. if ($_GET['action'] == 'add_item') {
  7132. $class = 'add';
  7133. $text = get_lang('LPCreateDocument');
  7134. } else {
  7135. if ($_GET['action'] == 'edit_item') {
  7136. $class = 'save';
  7137. $text = get_lang('SaveDocument');
  7138. }
  7139. }
  7140. $form->addButtonSave($text, 'submit_button');
  7141. $renderer = $form->defaultRenderer();
  7142. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7143. $form->addElement('html', '<div class="editor-lp">');
  7144. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7145. $form->addElement('html', '</div>');
  7146. $defaults['content_lp'] = $content;
  7147. }
  7148. } elseif (is_numeric($extra_info)) {
  7149. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7150. $return = $this->display_document($extra_info, true, true, true);
  7151. $form->addElement('html', $return);
  7152. }
  7153. }
  7154. }
  7155. if ($action == 'move') {
  7156. $form->addElement('hidden', 'title', $item_title);
  7157. $form->addElement('hidden', 'description', $item_description);
  7158. }
  7159. if (is_numeric($extra_info)) {
  7160. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7161. $form->addElement('hidden', 'path', $extra_info);
  7162. } elseif (is_array($extra_info)) {
  7163. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7164. $form->addElement('hidden', 'path', $extra_info['path']);
  7165. }
  7166. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7167. $form->addElement('hidden', 'post_time', time());
  7168. $form->setDefaults($defaults);
  7169. return $form->returnForm();
  7170. }
  7171. /**
  7172. * Return HTML form to add/edit a link item
  7173. * @param string $action (add/edit)
  7174. * @param integer $id Item ID if exists
  7175. * @param mixed $extra_info
  7176. * @return string HTML form
  7177. */
  7178. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7179. {
  7180. $course_id = api_get_course_int_id();
  7181. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7182. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7183. if ($id != 0 && is_array($extra_info)) {
  7184. $item_title = stripslashes($extra_info['title']);
  7185. $item_description = stripslashes($extra_info['description']);
  7186. $item_url = stripslashes($extra_info['url']);
  7187. } elseif (is_numeric($extra_info)) {
  7188. $extra_info = intval($extra_info);
  7189. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7190. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7191. $result = Database::query($sql);
  7192. $row = Database :: fetch_array($result);
  7193. $item_title = $row['title'];
  7194. $item_description = $row['description'];
  7195. $item_url = $row['url'];
  7196. } else {
  7197. $item_title = '';
  7198. $item_description = '';
  7199. $item_url = '';
  7200. }
  7201. $form = new FormValidator('edit_link', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  7202. $defaults = [];
  7203. if ($id != 0 && is_array($extra_info)) {
  7204. $parent = $extra_info['parent_item_id'];
  7205. } else {
  7206. $parent = 0;
  7207. }
  7208. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7209. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7210. $result = Database::query($sql);
  7211. $arrLP = array();
  7212. while ($row = Database :: fetch_array($result)) {
  7213. $arrLP[] = array(
  7214. 'id' => $row['id'],
  7215. 'item_type' => $row['item_type'],
  7216. 'title' => $row['title'],
  7217. 'path' => $row['path'],
  7218. 'description' => $row['description'],
  7219. 'parent_item_id' => $row['parent_item_id'],
  7220. 'previous_item_id' => $row['previous_item_id'],
  7221. 'next_item_id' => $row['next_item_id'],
  7222. 'display_order' => $row['display_order'],
  7223. 'max_score' => $row['max_score'],
  7224. 'min_score' => $row['min_score'],
  7225. 'mastery_score' => $row['mastery_score'],
  7226. 'prerequisite' => $row['prerequisite']
  7227. );
  7228. }
  7229. $this->tree_array($arrLP);
  7230. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7231. unset ($this->arrMenu);
  7232. if ($action == 'add') {
  7233. $legend = get_lang('CreateTheLink');
  7234. } elseif ($action == 'move') {
  7235. $legend = get_lang('MoveCurrentLink');
  7236. } else {
  7237. $legend = get_lang('EditCurrentLink');
  7238. }
  7239. $form->addHeader($legend);
  7240. if ($action != 'move') {
  7241. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7242. $defaults['title'] = $item_title;
  7243. }
  7244. $selectParent = $form->addSelect(
  7245. 'parent',
  7246. get_lang('Parent'),
  7247. [],
  7248. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7249. );
  7250. $selectParent->addOption($this->name, 0);
  7251. $arrHide = array(
  7252. $id
  7253. );
  7254. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7255. for ($i = 0; $i < count($arrLP); $i++) {
  7256. if ($action != 'add') {
  7257. if (
  7258. ($arrLP[$i]['item_type'] == 'dir') &&
  7259. !in_array($arrLP[$i]['id'], $arrHide) &&
  7260. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7261. ) {
  7262. $selectParent->addOption(
  7263. $arrLP[$i]['title'],
  7264. $arrLP[$i]['id'],
  7265. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7266. );
  7267. if ($parent == $arrLP[$i]['id']) {
  7268. $selectParent->setSelected($arrLP[$i]['id']);
  7269. }
  7270. } else {
  7271. $arrHide[] = $arrLP[$i]['id'];
  7272. }
  7273. } else {
  7274. if ($arrLP[$i]['item_type'] == 'dir') {
  7275. $selectParent->addOption(
  7276. $arrLP[$i]['title'],
  7277. $arrLP[$i]['id'],
  7278. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7279. );
  7280. if ($parent_item_id == $arrLP[$i]['id']) {
  7281. $selectParent->setSelected($arrLP[$i]['id']);
  7282. }
  7283. }
  7284. }
  7285. }
  7286. if (is_array($arrLP)) {
  7287. reset($arrLP);
  7288. }
  7289. $selectPrevious = $form->addSelect(
  7290. 'previous',
  7291. get_lang('Position'),
  7292. [],
  7293. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7294. );
  7295. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7296. for ($i = 0; $i < count($arrLP); $i++) {
  7297. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7298. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7299. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7300. $selectPrevious->setSelected($arrLP[$i]['id']);
  7301. } elseif ($action == 'add') {
  7302. $selectPrevious->setSelected($arrLP[$i]['id']);
  7303. }
  7304. }
  7305. }
  7306. if ($action != 'move') {
  7307. $urlAttributes = ['class' => 'learnpath_item_form'];
  7308. if (is_numeric($extra_info)) {
  7309. $urlAttributes['disabled'] = 'disabled';
  7310. }
  7311. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7312. $defaults['url'] = $item_url;
  7313. $id_prerequisite = 0;
  7314. if (is_array($arrLP)) {
  7315. foreach ($arrLP as $key => $value) {
  7316. if ($value['id'] == $id) {
  7317. $id_prerequisite = $value['prerequisite'];
  7318. break;
  7319. }
  7320. }
  7321. }
  7322. $arrHide = array();
  7323. for ($i = 0; $i < count($arrLP); $i++) {
  7324. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7325. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7326. $s_selected_position = $arrLP[$i]['id'];
  7327. elseif ($action == 'add') $s_selected_position = 0;
  7328. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7329. }
  7330. }
  7331. }
  7332. if ($action == 'add') {
  7333. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7334. } else {
  7335. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7336. }
  7337. if ($action == 'move') {
  7338. $form->addHidden('title', $item_title);
  7339. $form->addHidden('description', $item_description);
  7340. }
  7341. if (is_numeric($extra_info)) {
  7342. $form->addHidden('path', $extra_info);
  7343. } elseif (is_array($extra_info)) {
  7344. $form->addHidden('path', $extra_info['path']);
  7345. }
  7346. $form->addHidden('type', TOOL_LINK);
  7347. $form->addHidden('post_time', time());
  7348. $form->setDefaults($defaults);
  7349. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7350. }
  7351. /**
  7352. * Return HTML form to add/edit a student publication (work)
  7353. * @param string Action (add/edit)
  7354. * @param integer Item ID if already exists
  7355. * @param mixed Extra info (work ID if integer)
  7356. * @return string HTML form
  7357. */
  7358. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7359. {
  7360. $course_id = api_get_course_int_id();
  7361. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7362. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7363. if ($id != 0 && is_array($extra_info)) {
  7364. $item_title = stripslashes($extra_info['title']);
  7365. $item_description = stripslashes($extra_info['description']);
  7366. } elseif (is_numeric($extra_info)) {
  7367. $extra_info = intval($extra_info);
  7368. $sql = "SELECT title, description
  7369. FROM " . $tbl_publication . "
  7370. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7371. $result = Database::query($sql);
  7372. $row = Database :: fetch_array($result);
  7373. $item_title = $row['title'];
  7374. } else {
  7375. $item_title = get_lang('Student_publication');
  7376. }
  7377. if ($id != 0 && is_array($extra_info)) {
  7378. $parent = $extra_info['parent_item_id'];
  7379. } else {
  7380. $parent = 0;
  7381. }
  7382. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7383. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7384. $result = Database::query($sql);
  7385. $arrLP = array();
  7386. while ($row = Database :: fetch_array($result)) {
  7387. $arrLP[] = array(
  7388. 'id' => $row['id'],
  7389. 'item_type' => $row['item_type'],
  7390. 'title' => $row['title'],
  7391. 'path' => $row['path'],
  7392. 'description' => $row['description'],
  7393. 'parent_item_id' => $row['parent_item_id'],
  7394. 'previous_item_id' => $row['previous_item_id'],
  7395. 'next_item_id' => $row['next_item_id'],
  7396. 'display_order' => $row['display_order'],
  7397. 'max_score' => $row['max_score'],
  7398. 'min_score' => $row['min_score'],
  7399. 'mastery_score' => $row['mastery_score'],
  7400. 'prerequisite' => $row['prerequisite']
  7401. );
  7402. }
  7403. $this->tree_array($arrLP);
  7404. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7405. unset ($this->arrMenu);
  7406. $form = new FormValidator('frm_student_publication', 'post', '#');
  7407. if ($action == 'add') {
  7408. $form->addHeader(get_lang('Student_publication'));
  7409. } elseif ($action == 'move') {
  7410. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7411. } else {
  7412. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7413. }
  7414. if ($action != 'move') {
  7415. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7416. }
  7417. $parentSelect = $form->addSelect(
  7418. 'parent',
  7419. get_lang('Parent'),
  7420. ['0' => $this->name],
  7421. [
  7422. 'onchange' => 'javascript: load_cbo(this.value);',
  7423. 'class' => 'learnpath_item_form',
  7424. 'id' => 'idParent'
  7425. ]
  7426. );
  7427. $arrHide = array (
  7428. $id
  7429. );
  7430. for ($i = 0; $i < count($arrLP); $i++) {
  7431. if ($action != 'add') {
  7432. if (
  7433. ($arrLP[$i]['item_type'] == 'dir') &&
  7434. !in_array($arrLP[$i]['id'], $arrHide) &&
  7435. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7436. ) {
  7437. $parentSelect->addOption(
  7438. $arrLP[$i]['title'],
  7439. $arrLP[$i]['id'],
  7440. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7441. );
  7442. if ($parent == $arrLP[$i]['id']) {
  7443. $parentSelect->setSelected($arrLP[$i]['id']);
  7444. }
  7445. } else {
  7446. $arrHide[] = $arrLP[$i]['id'];
  7447. }
  7448. } else {
  7449. if ($arrLP[$i]['item_type'] == 'dir') {
  7450. $parentSelect->addOption(
  7451. $arrLP[$i]['title'],
  7452. $arrLP[$i]['id'],
  7453. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7454. );
  7455. if ($parent == $arrLP[$i]['id']) {
  7456. $parentSelect->setSelected($arrLP[$i]['id']);
  7457. }
  7458. }
  7459. }
  7460. }
  7461. if (is_array($arrLP)) {
  7462. reset($arrLP);
  7463. }
  7464. $previousSelect = $form->addSelect(
  7465. 'previous',
  7466. get_lang('Position'),
  7467. ['0' => get_lang('FirstPosition')],
  7468. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7469. );
  7470. for ($i = 0; $i < count($arrLP); $i++) {
  7471. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7472. $previousSelect->addOption(
  7473. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7474. $arrLP[$i]['id']
  7475. );
  7476. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7477. $previousSelect->setSelected($arrLP[$i]['id']);
  7478. } elseif ($action == 'add') {
  7479. $previousSelect->setSelected($arrLP[$i]['id']);
  7480. }
  7481. }
  7482. }
  7483. if ($action != 'move') {
  7484. $id_prerequisite = 0;
  7485. if (is_array($arrLP)) {
  7486. foreach ($arrLP as $key => $value) {
  7487. if ($value['id'] == $id) {
  7488. $id_prerequisite = $value['prerequisite'];
  7489. break;
  7490. }
  7491. }
  7492. }
  7493. $arrHide = array ();
  7494. for ($i = 0; $i < count($arrLP); $i++) {
  7495. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7496. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7497. $s_selected_position = $arrLP[$i]['id'];
  7498. elseif ($action == 'add') $s_selected_position = 0;
  7499. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7500. }
  7501. }
  7502. }
  7503. if ($action == 'add') {
  7504. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7505. } else {
  7506. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7507. }
  7508. if ($action == 'move') {
  7509. $form->addHidden('title', $item_title);
  7510. $form->addHidden('description', $item_description);
  7511. }
  7512. if (is_numeric($extra_info)) {
  7513. $form->addHidden('path', $extra_info);
  7514. } elseif (is_array($extra_info)) {
  7515. $form->addHidden('path', $extra_info['path']);
  7516. }
  7517. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7518. $form->addHidden('post_time', time());
  7519. $form->setDefaults(['title' => $item_title]);
  7520. $return = '<div class="sectioncomment">';
  7521. $return .= $form->returnForm();
  7522. $return .= '</div>';
  7523. return $return;
  7524. }
  7525. /**
  7526. * Displays the menu for manipulating a step
  7527. *
  7528. * @param $item_id
  7529. * @param string $item_type
  7530. * @return string
  7531. */
  7532. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7533. {
  7534. $_course = api_get_course_info();
  7535. $course_id = api_get_course_int_id();
  7536. $course_code = api_get_course_id();
  7537. $return = '<div class="actions">';
  7538. switch ($item_type) {
  7539. case 'dir':
  7540. // Commented the message cause should not show it.
  7541. //$lang = get_lang('TitleManipulateChapter');
  7542. break;
  7543. case TOOL_LP_FINAL_ITEM:
  7544. case TOOL_DOCUMENT:
  7545. // Commented the message cause should not show it.
  7546. //$lang = get_lang('TitleManipulateDocument');
  7547. break;
  7548. case TOOL_LINK:
  7549. case 'link' :
  7550. // Commented the message cause should not show it.
  7551. //$lang = get_lang('TitleManipulateLink');
  7552. break;
  7553. case TOOL_QUIZ:
  7554. // Commented the message cause should not show it.
  7555. //$lang = get_lang('TitleManipulateQuiz');
  7556. break;
  7557. case TOOL_STUDENTPUBLICATION:
  7558. // Commented the message cause should not show it.
  7559. //$lang = get_lang('TitleManipulateStudentPublication');
  7560. break;
  7561. }
  7562. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7563. $item_id = intval($item_id);
  7564. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7565. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7566. $result = Database::query($sql);
  7567. $row = Database::fetch_assoc($result);
  7568. $audio_player = null;
  7569. // We display an audio player if needed.
  7570. if (!empty($row['audio'])) {
  7571. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7572. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7573. </div>';
  7574. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7575. $audio_player .= '<script>
  7576. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7577. s1.addParam("allowscriptaccess","always");
  7578. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7579. s1.write("container");
  7580. </script>';
  7581. }
  7582. $url = api_get_self().'?cidReq='.api_get_cidreq().'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7583. $return .= Display::url(
  7584. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7585. $url.'&action=edit_item&path_item=' . $row['path']
  7586. );
  7587. $return .= Display::url(
  7588. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7589. $url.'&action=move_item'
  7590. );
  7591. // Commented for now as prerequisites cannot be added to chapters.
  7592. if ($item_type != 'dir') {
  7593. $return .= Display::url(
  7594. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7595. $url.'&action=edit_item_prereq'
  7596. );
  7597. }
  7598. $return .= Display::url(
  7599. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7600. $url.'&action=delete_item'
  7601. );
  7602. if ($item_type == TOOL_HOTPOTATOES ) {
  7603. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7604. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7605. }
  7606. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7607. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7608. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7609. }
  7610. $return .= '</div>';
  7611. if (!empty($audio_player)) {
  7612. $return .= '<br />'.$audio_player;
  7613. }
  7614. return $return;
  7615. }
  7616. /**
  7617. * Creates the javascript needed for filling up the checkboxes without page reload
  7618. * @return string
  7619. */
  7620. public function get_js_dropdown_array()
  7621. {
  7622. $course_id = api_get_course_int_id();
  7623. $return = 'var child_name = new Array();' . "\n";
  7624. $return .= 'var child_value = new Array();' . "\n\n";
  7625. $return .= 'child_name[0] = new Array();' . "\n";
  7626. $return .= 'child_value[0] = new Array();' . "\n\n";
  7627. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7628. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7629. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7630. ORDER BY display_order ASC";
  7631. $res_zero = Database::query($sql_zero);
  7632. $i = 0;
  7633. while ($row_zero = Database :: fetch_array($res_zero)) {
  7634. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7635. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7636. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7637. }
  7638. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7639. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7640. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7641. }
  7642. }
  7643. $return .= "\n";
  7644. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7645. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7646. $res = Database::query($sql);
  7647. while ($row = Database :: fetch_array($res)) {
  7648. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7649. WHERE
  7650. c_id = ".$course_id." AND
  7651. parent_item_id = " . $row['id'] . "
  7652. ORDER BY display_order ASC";
  7653. $res_parent = Database::query($sql_parent);
  7654. $i = 0;
  7655. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7656. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7657. while ($row_parent = Database :: fetch_array($res_parent)) {
  7658. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7659. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7660. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7661. }
  7662. $return .= "\n";
  7663. }
  7664. return $return;
  7665. }
  7666. /**
  7667. * Display the form to allow moving an item
  7668. * @param integer Item ID
  7669. * @return string HTML form
  7670. */
  7671. public function display_move_item($item_id)
  7672. {
  7673. $course_id = api_get_course_int_id();
  7674. $return = '';
  7675. if (is_numeric($item_id)) {
  7676. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7677. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7678. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7679. $res = Database::query($sql);
  7680. $row = Database :: fetch_array($res);
  7681. switch ($row['item_type']) {
  7682. case 'dir' :
  7683. case 'asset' :
  7684. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7685. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7686. break;
  7687. case TOOL_DOCUMENT :
  7688. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7689. $return .= $this->display_document_form('move', $item_id, $row);
  7690. break;
  7691. case TOOL_LINK :
  7692. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7693. $return .= $this->display_link_form('move', $item_id, $row);
  7694. break;
  7695. case TOOL_HOTPOTATOES :
  7696. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7697. $return .= $this->display_link_form('move', $item_id, $row);
  7698. break;
  7699. case TOOL_QUIZ :
  7700. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7701. $return .= $this->display_quiz_form('move', $item_id, $row);
  7702. break;
  7703. case TOOL_STUDENTPUBLICATION :
  7704. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7705. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7706. break;
  7707. case TOOL_FORUM :
  7708. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7709. $return .= $this->display_forum_form('move', $item_id, $row);
  7710. break;
  7711. case TOOL_THREAD :
  7712. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7713. $return .= $this->display_forum_form('move', $item_id, $row);
  7714. break;
  7715. }
  7716. }
  7717. return $return;
  7718. }
  7719. /**
  7720. * Displays a basic form on the overview page for changing the item title and the item description.
  7721. * @param string $item_type
  7722. * @param string $title
  7723. * @param array $data
  7724. * @return string
  7725. */
  7726. public function display_item_small_form($item_type, $title = '', $data = array())
  7727. {
  7728. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7729. $form = new FormValidator('small_form', 'post', $url);
  7730. $form->addElement('header', $title);
  7731. $form->addElement('text', 'title', get_lang('Title'));
  7732. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7733. $form->addElement('hidden', 'id', $data['id']);
  7734. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7735. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7736. $form->setDefaults(array('title' => $data['title']));
  7737. return $form->toHtml();
  7738. }
  7739. /**
  7740. * Return HTML form to allow prerequisites selection
  7741. * @todo use FormValidator
  7742. * @param integer Item ID
  7743. * @return string HTML form
  7744. */
  7745. public function display_item_prerequisites_form($item_id)
  7746. {
  7747. $course_id = api_get_course_int_id();
  7748. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7749. $item_id = intval($item_id);
  7750. /* Current prerequisite */
  7751. $sql = "SELECT * FROM $tbl_lp_item
  7752. WHERE c_id = $course_id AND id = " . $item_id;
  7753. $result = Database::query($sql);
  7754. $row = Database::fetch_array($result);
  7755. $prerequisiteId = $row['prerequisite'];
  7756. $return = '<legend>';
  7757. $return .= get_lang('AddEditPrerequisites');
  7758. $return .= '</legend>';
  7759. $return .= '<form method="POST">';
  7760. $return .= '<table class="data_table">';
  7761. $return .= '<tr>';
  7762. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7763. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7764. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7765. $return .= '</tr>';
  7766. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7767. $return .= '<tr >';
  7768. $return .= '<td colspan="3" class="radio">';
  7769. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7770. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7771. $return .= '</tr>';
  7772. $sql = "SELECT * FROM $tbl_lp_item
  7773. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7774. $result = Database::query($sql);
  7775. $arrLP = array();
  7776. $selectedMinScore = array();
  7777. $selectedMaxScore = array();
  7778. while ($row = Database :: fetch_array($result)) {
  7779. if ($row['id'] == $item_id) {
  7780. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7781. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7782. }
  7783. $arrLP[] = array(
  7784. 'id' => $row['id'],
  7785. 'item_type' => $row['item_type'],
  7786. 'title' => $row['title'],
  7787. 'ref' => $row['ref'],
  7788. 'description' => $row['description'],
  7789. 'parent_item_id' => $row['parent_item_id'],
  7790. 'previous_item_id' => $row['previous_item_id'],
  7791. 'next_item_id' => $row['next_item_id'],
  7792. 'max_score' => $row['max_score'],
  7793. 'min_score' => $row['min_score'],
  7794. 'mastery_score' => $row['mastery_score'],
  7795. 'prerequisite' => $row['prerequisite'],
  7796. 'next_item_id' => $row['next_item_id'],
  7797. 'display_order' => $row['display_order'],
  7798. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7799. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7800. );
  7801. }
  7802. $this->tree_array($arrLP);
  7803. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7804. unset($this->arrMenu);
  7805. for ($i = 0; $i < count($arrLP); $i++) {
  7806. $item = $arrLP[$i];
  7807. if ($item['id'] == $item_id) {
  7808. break;
  7809. }
  7810. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7811. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7812. $return .= '<tr>';
  7813. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7814. $return .= '<label for="id' . $item['id'] . '">';
  7815. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . ($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7816. $icon_name = str_replace(' ', '', $item['item_type']);
  7817. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7818. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  7819. } else {
  7820. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7821. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  7822. } else {
  7823. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7824. }
  7825. }
  7826. $return .= $item['title'] . '</label>';
  7827. $return .= '</td>';
  7828. if ($item['item_type'] == TOOL_QUIZ) {
  7829. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7830. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7831. $tmp_obj_exercice = new Exercise();
  7832. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7833. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7834. $tmp_obj_lp_item->update_in_bdd();
  7835. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7836. $return .= '<td class="exercise">';
  7837. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7838. $return .= '</td>';
  7839. $return .= '<td class="exercise">';
  7840. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7841. $return .= '</td>';
  7842. }
  7843. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7844. $return .= '<td class="exercise">';
  7845. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7846. $return .= '</td>';
  7847. $return .= '<td class="exercise"">';
  7848. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7849. $return .= '</td>';
  7850. }
  7851. $return .= '</tr>';
  7852. }
  7853. $return .= '<tr>';
  7854. $return .= '</tr>';
  7855. $return .= '</table>';
  7856. $return .= '<div style="padding-top:3px;">';
  7857. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7858. $return .= '</form>';
  7859. return $return;
  7860. }
  7861. /**
  7862. * Return HTML list to allow prerequisites selection for lp
  7863. * @param integer Item ID
  7864. * @return string HTML form
  7865. */
  7866. public function display_lp_prerequisites_list()
  7867. {
  7868. $course_id = api_get_course_int_id();
  7869. $lp_id = $this->lp_id;
  7870. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7871. // get current prerequisite
  7872. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7873. $result = Database::query($sql);
  7874. $row = Database :: fetch_array($result);
  7875. $prerequisiteId = $row['prerequisite'];
  7876. $session_id = api_get_session_id();
  7877. $session_condition = api_get_session_condition($session_id);
  7878. $sql = "SELECT * FROM $tbl_lp
  7879. WHERE c_id = $course_id $session_condition
  7880. ORDER BY display_order ";
  7881. $rs = Database::query($sql);
  7882. $return = '';
  7883. $return .= '<select name="prerequisites" class="form-control">';
  7884. $return .= '<option value="0">'.get_lang('None').'</option>';
  7885. if (Database::num_rows($rs) > 0) {
  7886. while ($row = Database::fetch_array($rs)) {
  7887. if ($row['id'] == $lp_id) {
  7888. continue;
  7889. }
  7890. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7891. }
  7892. }
  7893. $return .= '</select>';
  7894. return $return;
  7895. }
  7896. /**
  7897. * Creates a list with all the documents in it
  7898. * @param bool $showInvisibleFiles
  7899. * @return string
  7900. */
  7901. public function get_documents($showInvisibleFiles = false)
  7902. {
  7903. $course_info = api_get_course_info();
  7904. $sessionId = api_get_session_id();
  7905. $documentTree = DocumentManager::get_document_preview(
  7906. $course_info,
  7907. $this->lp_id,
  7908. null,
  7909. $sessionId,
  7910. true,
  7911. null,
  7912. null,
  7913. $showInvisibleFiles,
  7914. true
  7915. );
  7916. $headers = array(
  7917. get_lang('Files'),
  7918. get_lang('CreateTheDocument'),
  7919. get_lang('Upload')
  7920. );
  7921. $form = new FormValidator(
  7922. 'form_upload',
  7923. 'POST',
  7924. api_get_self().'?'.$_SERVER['QUERY_STRING'],
  7925. '',
  7926. array('enctype' => "multipart/form-data")
  7927. );
  7928. $folders = DocumentManager::get_all_document_folders(
  7929. api_get_course_info(),
  7930. 0,
  7931. true
  7932. );
  7933. DocumentManager::build_directory_selector(
  7934. $folders,
  7935. '',
  7936. array(),
  7937. true,
  7938. $form,
  7939. 'directory_parent_id'
  7940. );
  7941. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  7942. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  7943. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  7944. $form->setDefaults(['if_exists' => 'rename']);
  7945. // Check box options
  7946. $form->addElement(
  7947. 'checkbox',
  7948. 'unzip',
  7949. get_lang('Options'),
  7950. get_lang('Uncompress')
  7951. );
  7952. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  7953. $form->addMultipleUpload($url);
  7954. $new = $this->display_document_form('add', 0);
  7955. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  7956. return $tabs;
  7957. }
  7958. /**
  7959. * Creates a list with all the exercises (quiz) in it
  7960. * @return string
  7961. */
  7962. public function get_exercises()
  7963. {
  7964. $course_id = api_get_course_int_id();
  7965. // New for hotpotatoes.
  7966. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7967. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7968. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7969. $session_id = api_get_session_id();
  7970. $condition_session = api_get_session_condition($session_id);
  7971. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  7972. $activeCondition = " active <> -1 ";
  7973. if ($setting) {
  7974. $activeCondition = " active = 1 ";
  7975. }
  7976. $sql_quiz = "SELECT * FROM $tbl_quiz
  7977. WHERE c_id = $course_id AND $activeCondition $condition_session
  7978. ORDER BY title ASC";
  7979. $sql_hot = "SELECT * FROM $tbl_doc
  7980. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7981. ORDER BY id ASC";
  7982. $res_quiz = Database::query($sql_quiz);
  7983. $res_hot = Database::query($sql_hot);
  7984. $return = '<ul class="lp_resource">';
  7985. $return .= '<li class="lp_resource_element">';
  7986. $return .= Display::return_icon('new_exercice.png');
  7987. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7988. get_lang('NewExercise') . '</a>';
  7989. $return .= '</li>';
  7990. // Display hotpotatoes
  7991. while ($row_hot = Database :: fetch_array($res_hot)) {
  7992. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7993. $return .= '<a class="moved" href="#">';
  7994. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7995. $return .= '</a> ';
  7996. $return .= Display::return_icon('hotpotatoes_s.png');
  7997. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7998. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7999. $return .= '</li>';
  8000. }
  8001. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8002. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8003. $return .= '<a class="moved" href="#">';
  8004. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8005. $return .= '</a> ';
  8006. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8007. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8008. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  8009. '</a>';
  8010. $return .= '</li>';
  8011. }
  8012. $return .= '</ul>';
  8013. return $return;
  8014. }
  8015. /**
  8016. * Creates a list with all the links in it
  8017. * @return string
  8018. */
  8019. public function get_links()
  8020. {
  8021. $selfUrl = api_get_self();
  8022. $courseIdReq = api_get_cidreq();
  8023. $course = api_get_course_info();
  8024. $course_id = $course['real_id'];
  8025. $tbl_link = Database::get_course_table(TABLE_LINK);
  8026. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8027. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8028. $session_id = api_get_session_id();
  8029. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  8030. $sql = "SELECT link.id as link_id,
  8031. link.title as link_title,
  8032. link.category_id as category_id,
  8033. link_category.category_title as category_title
  8034. FROM $tbl_link as link
  8035. LEFT JOIN $linkCategoryTable as link_category
  8036. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8037. WHERE link.c_id = ".$course_id." $condition_session
  8038. ORDER BY link_category.category_title ASC, link.title ASC";
  8039. $links = Database::query($sql);
  8040. $categorizedLinks = array();
  8041. $categories = array();
  8042. while ($link = Database :: fetch_array($links)) {
  8043. if (!$link['category_id']) {
  8044. $link['category_title'] = get_lang('Uncategorized');
  8045. }
  8046. $categories[$link['category_id']] = $link['category_title'];
  8047. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  8048. }
  8049. $linksHtmlCode =
  8050. '<script>
  8051. function toggle_tool(tool, id){
  8052. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8053. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8054. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8055. } else {
  8056. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8057. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8058. }
  8059. }
  8060. </script>
  8061. <ul class="lp_resource">
  8062. <li class="lp_resource_element">
  8063. '.Display::return_icon('linksnew.gif').'
  8064. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8065. get_lang('LinkAdd').'
  8066. </a>
  8067. </li>';
  8068. foreach ($categorizedLinks as $categoryId => $links) {
  8069. $linkNodes = null;
  8070. foreach ($links as $key => $title) {
  8071. $link = Display::url(
  8072. Display::return_icon('preview_view.png', get_lang('Preview')),
  8073. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8074. ['target' => '_blank']
  8075. );
  8076. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8077. $linkNodes .=
  8078. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8079. <a class="moved" href="#">'.
  8080. $moveEverywhereIcon.
  8081. '</a>
  8082. '.Display::return_icon('lp_link.png').'
  8083. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8084. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8085. Security::remove_XSS($title).
  8086. '</a>
  8087. </li>';
  8088. }
  8089. }
  8090. $linksHtmlCode .=
  8091. '<li>
  8092. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8093. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8094. align="absbottom" />
  8095. </a>
  8096. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8097. </li>
  8098. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8099. }
  8100. $linksHtmlCode .= '</ul>';
  8101. return $linksHtmlCode;
  8102. }
  8103. /**
  8104. * Creates a list with all the student publications in it
  8105. * @return string
  8106. */
  8107. public function get_student_publications()
  8108. {
  8109. $return = '<ul class="lp_resource">';
  8110. $return .= '<li class="lp_resource_element">';
  8111. $return .= Display::return_icon('works_new.gif');
  8112. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8113. get_lang('AddAssignmentPage') . '</a>';
  8114. $return .= '</li>';
  8115. $sessionId = api_get_session_id();
  8116. if (empty($sessionId)) {
  8117. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8118. $works = getWorkListTeacher(0, 100, null, null, null);
  8119. if (!empty($works)) {
  8120. foreach ($works as $work) {
  8121. $link = Display::url(
  8122. Display::return_icon('preview_view.png', get_lang('Preview')),
  8123. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8124. ['target' => '_blank']
  8125. );
  8126. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security :: remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8127. $return .= '<a class="moved" href="#">';
  8128. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8129. $return .= '</a> ';
  8130. $return .= Display::return_icon('works.gif');
  8131. $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 . '">' .
  8132. Security :: remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8133. </a>';
  8134. $return .= '</li>';
  8135. }
  8136. }
  8137. }
  8138. $return .= '</ul>';
  8139. return $return;
  8140. }
  8141. /**
  8142. * Creates a list with all the forums in it
  8143. * @return string
  8144. */
  8145. public function get_forums()
  8146. {
  8147. require_once '../forum/forumfunction.inc.php';
  8148. require_once '../forum/forumconfig.inc.php';
  8149. $forumCategories = get_forum_categories();
  8150. $forumsInNoCategory = get_forums_in_category(0);
  8151. if (!empty($forumsInNoCategory)) {
  8152. $forumCategories = array_merge(
  8153. $forumCategories,
  8154. array(
  8155. array(
  8156. 'cat_id' => 0,
  8157. 'session_id' => 0,
  8158. 'visibility' => 1,
  8159. 'cat_comment' => null,
  8160. ),
  8161. )
  8162. );
  8163. }
  8164. $forumList = get_forums();
  8165. $a_forums = [];
  8166. foreach ($forumCategories as $forumCategory) {
  8167. // The forums in this category.
  8168. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8169. if (!empty($forumsInCategory)) {
  8170. foreach ($forumList as $forum) {
  8171. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8172. $a_forums[] = $forum;
  8173. }
  8174. }
  8175. }
  8176. }
  8177. $return = '<ul class="lp_resource">';
  8178. // First add link
  8179. $return .= '<li class="lp_resource_element">';
  8180. $return .= Display::return_icon('new_forum.png');
  8181. $return .= Display::url(
  8182. get_lang('CreateANewForum'),
  8183. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8184. 'action' => 'add',
  8185. 'content' => 'forum',
  8186. 'lp_id' => $this->lp_id
  8187. ]),
  8188. ['title' => get_lang('CreateANewForum')]
  8189. );
  8190. $return .= '</li>';
  8191. $return .= '<script>
  8192. function toggle_forum(forum_id) {
  8193. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8194. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8195. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8196. } else {
  8197. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8198. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8199. }
  8200. }
  8201. </script>';
  8202. foreach ($a_forums as $forum) {
  8203. if (!empty($forum['forum_id'])) {
  8204. $link = Display::url(
  8205. Display::return_icon('preview_view.png', get_lang('Preview')),
  8206. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8207. ['target' => '_blank']
  8208. );
  8209. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8210. $return .= '<a class="moved" href="#">';
  8211. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8212. $return .= ' </a>';
  8213. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8214. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  8215. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8216. </a>
  8217. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  8218. Security :: remove_XSS($forum['forum_title']) . '</a>';
  8219. $return .= '</li>';
  8220. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8221. $a_threads = get_threads($forum['forum_id']);
  8222. if (is_array($a_threads)) {
  8223. foreach ($a_threads as $thread) {
  8224. $link = Display::url(
  8225. Display::return_icon('preview_view.png', get_lang('Preview')),
  8226. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8227. ['target' => '_blank']
  8228. );
  8229. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8230. $return .= '&nbsp;<a class="moved" href="#">';
  8231. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8232. $return .= ' </a>';
  8233. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8234. $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 . '">' .
  8235. Security :: remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8236. $return .= '</li>';
  8237. }
  8238. }
  8239. $return .= '</div>';
  8240. }
  8241. }
  8242. $return .= '</ul>';
  8243. return $return;
  8244. }
  8245. /**
  8246. * // TODO: The output encoding should be equal to the system encoding.
  8247. *
  8248. * Exports the learning path as a SCORM package. This is the main function that
  8249. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8250. * whole thing and returns the zip.
  8251. *
  8252. * This method needs to be called in PHP5, as it will fail with non-adequate
  8253. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8254. * you need to call it on a learnpath object.
  8255. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8256. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8257. * path has been modified, it should use the generic method here below.
  8258. * @TODO link this function with the export_lp() function in the same class
  8259. * @param string Optional name of zip file. If none, title of learnpath is
  8260. * domesticated and trailed with ".zip"
  8261. * @return string Returns the zip package string, or null if error
  8262. */
  8263. public function scorm_export()
  8264. {
  8265. $_course = api_get_course_info();
  8266. $course_id = $_course['real_id'];
  8267. // Remove memory and time limits as much as possible as this might be a long process...
  8268. if (function_exists('ini_set')) {
  8269. api_set_memory_limit('256M');
  8270. ini_set('max_execution_time', 600);
  8271. }
  8272. // Create the zip handler (this will remain available throughout the method).
  8273. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8274. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8275. $temp_dir_short = uniqid();
  8276. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8277. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8278. $zip_folder = new PclZip($temp_zip_file);
  8279. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8280. $root_path = $main_path = api_get_path(SYS_PATH);
  8281. $files_cleanup = array();
  8282. // Place to temporarily stash the zip file.
  8283. // create the temp dir if it doesn't exist
  8284. // or do a cleanup before creating the zip file.
  8285. if (!is_dir($temp_zip_dir)) {
  8286. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8287. } else {
  8288. // Cleanup: Check the temp dir for old files and delete them.
  8289. $handle = opendir($temp_zip_dir);
  8290. while (false !== ($file = readdir($handle))) {
  8291. if ($file != '.' && $file != '..') {
  8292. unlink("$temp_zip_dir/$file");
  8293. }
  8294. }
  8295. closedir($handle);
  8296. }
  8297. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8298. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8299. // Remove the possible . at the end of the path.
  8300. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8301. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8302. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8303. copyr(
  8304. $current_course_path.'/scorm/'.$this->path,
  8305. $dest_path_to_scorm_folder,
  8306. array('imsmanifest'),
  8307. $zip_files
  8308. );
  8309. }
  8310. // Build a dummy imsmanifest structure.
  8311. // Do not add to the zip yet (we still need it).
  8312. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8313. // Aggregation Model official document, section "2.3 Content Packaging".
  8314. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8315. $xmldoc = new DOMDocument('1.0');
  8316. $root = $xmldoc->createElement('manifest');
  8317. $root->setAttribute('identifier', 'SingleCourseManifest');
  8318. $root->setAttribute('version', '1.1');
  8319. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8320. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8321. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8322. $root->setAttribute('xsi:schemaLocation', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd');
  8323. // Build mandatory sub-root container elements.
  8324. $metadata = $xmldoc->createElement('metadata');
  8325. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8326. $metadata->appendChild($md_schema);
  8327. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8328. $metadata->appendChild($md_schemaversion);
  8329. $root->appendChild($metadata);
  8330. $organizations = $xmldoc->createElement('organizations');
  8331. $resources = $xmldoc->createElement('resources');
  8332. // Build the only organization we will use in building our learnpaths.
  8333. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8334. $organization = $xmldoc->createElement('organization');
  8335. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8336. // To set the title of the SCORM entity (=organization), we take the name given
  8337. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8338. // learning path charset) as it is the encoding that defines how it is stored
  8339. // in the database. Then we convert it to HTML entities again as the "&" character
  8340. // alone is not authorized in XML (must be &amp;).
  8341. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8342. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8343. $organization->appendChild($org_title);
  8344. $folder_name = 'document';
  8345. // Removes the learning_path/scorm_folder path when exporting see #4841
  8346. $path_to_remove = null;
  8347. $result = $this->generate_lp_folder($_course);
  8348. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8349. $path_to_remove = 'document'.$result['dir'];
  8350. $path_to_replace = $folder_name.'/';
  8351. }
  8352. // Fixes chamilo scorm exports
  8353. if ($this->ref === 'chamilo_scorm_export') {
  8354. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8355. }
  8356. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8357. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8358. $link_updates = array();
  8359. $links_to_create = array();
  8360. //foreach ($this->items as $index => $item) {
  8361. foreach ($this->ordered_items as $index => $itemId) {
  8362. $item = $this->items[$itemId];
  8363. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8364. // Get included documents from this item.
  8365. if ($item->type === 'sco') {
  8366. $inc_docs = $item->get_resources_from_source(
  8367. null,
  8368. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8369. );
  8370. } else {
  8371. $inc_docs = $item->get_resources_from_source();
  8372. }
  8373. // Give a child element <item> to the <organization> element.
  8374. $my_item_id = $item->get_id();
  8375. $my_item = $xmldoc->createElement('item');
  8376. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8377. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8378. $my_item->setAttribute('isvisible', 'true');
  8379. // Give a child element <title> to the <item> element.
  8380. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8381. $my_item->appendChild($my_title);
  8382. // Give a child element <adlcp:prerequisites> to the <item> element.
  8383. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8384. $my_prereqs->setAttribute('type', 'aicc_script');
  8385. $my_item->appendChild($my_prereqs);
  8386. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8387. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8388. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8389. //$xmldoc->createElement('adlcp:timelimitaction','');
  8390. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8391. //$xmldoc->createElement('adlcp:datafromlms','');
  8392. // Give a child element <adlcp:masteryscore> to the <item> element.
  8393. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8394. $my_item->appendChild($my_masteryscore);
  8395. // Attach this item to the organization element or hits parent if there is one.
  8396. if (!empty($item->parent) && $item->parent != 0) {
  8397. $children = $organization->childNodes;
  8398. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8399. if (is_object($possible_parent)) {
  8400. $possible_parent->appendChild($my_item);
  8401. } else {
  8402. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8403. }
  8404. } else {
  8405. if ($this->debug > 0) { error_log('No parent'); }
  8406. $organization->appendChild($my_item);
  8407. }
  8408. // Get the path of the file(s) from the course directory root.
  8409. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8410. if (!empty($path_to_remove)) {
  8411. //From docs
  8412. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8413. //From quiz
  8414. if ($this->ref === 'chamilo_scorm_export') {
  8415. $path_to_remove = 'scorm/'.$this->path.'/';
  8416. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8417. }
  8418. } else {
  8419. $my_xml_file_path = $my_file_path;
  8420. }
  8421. $my_sub_dir = dirname($my_file_path);
  8422. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8423. $my_xml_sub_dir = $my_sub_dir;
  8424. // Give a <resource> child to the <resources> element
  8425. $my_resource = $xmldoc->createElement('resource');
  8426. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8427. $my_resource->setAttribute('type', 'webcontent');
  8428. $my_resource->setAttribute('href', $my_xml_file_path);
  8429. // adlcp:scormtype can be either 'sco' or 'asset'.
  8430. if ($item->type === 'sco') {
  8431. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8432. } else {
  8433. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8434. }
  8435. // xml:base is the base directory to find the files declared in this resource.
  8436. $my_resource->setAttribute('xml:base', '');
  8437. // Give a <file> child to the <resource> element.
  8438. $my_file = $xmldoc->createElement('file');
  8439. $my_file->setAttribute('href', $my_xml_file_path);
  8440. $my_resource->appendChild($my_file);
  8441. // Dependency to other files - not yet supported.
  8442. $i = 1;
  8443. if ($inc_docs)
  8444. foreach ($inc_docs as $doc_info) {
  8445. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8446. continue;
  8447. }
  8448. $my_dep = $xmldoc->createElement('resource');
  8449. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8450. $my_dep->setAttribute('identifier', $res_id);
  8451. $my_dep->setAttribute('type', 'webcontent');
  8452. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8453. $my_dep_file = $xmldoc->createElement('file');
  8454. // Check type of URL.
  8455. if ($doc_info[1] == 'remote') {
  8456. // Remote file. Save url as is.
  8457. $my_dep_file->setAttribute('href', $doc_info[0]);
  8458. $my_dep->setAttribute('xml:base', '');
  8459. } elseif ($doc_info[1] === 'local') {
  8460. switch ($doc_info[2]) {
  8461. case 'url': // Local URL - save path as url for now, don't zip file.
  8462. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8463. $current_dir = dirname($abs_path);
  8464. $current_dir = str_replace('\\', '/', $current_dir);
  8465. $file_path = realpath($abs_path);
  8466. $file_path = str_replace('\\', '/', $file_path);
  8467. $my_dep_file->setAttribute('href', $file_path);
  8468. $my_dep->setAttribute('xml:base', '');
  8469. if (strstr($file_path, $main_path) !== false) {
  8470. // The calculated real path is really inside Chamilo's root path.
  8471. // Reduce file path to what's under the DocumentRoot.
  8472. $file_path = substr($file_path, strlen($root_path) - 1);
  8473. //echo $file_path;echo '<br /><br />';
  8474. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8475. $zip_files_abs[] = $file_path;
  8476. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8477. $my_dep_file->setAttribute('href', $file_path);
  8478. $my_dep->setAttribute('xml:base', '');
  8479. } elseif (empty($file_path)) {
  8480. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8481. if (strpos($document_root, -1) == '/') {
  8482. $document_root = substr(0, -1, $document_root);
  8483. }*/
  8484. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8485. $file_path = str_replace('//', '/', $file_path);
  8486. if (file_exists($file_path)) {
  8487. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8488. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8489. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8490. $my_dep_file->setAttribute('href', $file_path);
  8491. $my_dep->setAttribute('xml:base', '');
  8492. }
  8493. }
  8494. break;
  8495. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8496. $my_dep_file->setAttribute('href', $doc_info[0]);
  8497. $my_dep->setAttribute('xml:base', '');
  8498. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8499. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8500. $abs_img_path_without_subdir = $doc_info[0];
  8501. $relp = api_get_path(REL_PATH); // The url-append config param.
  8502. $pos = strpos($abs_img_path_without_subdir, $relp);
  8503. if ($pos === 0) {
  8504. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8505. }
  8506. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8507. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8508. $file_path = str_replace('\\', '/', $file_path);
  8509. $file_path = str_replace('//', '/', $file_path);
  8510. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8511. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8512. // Check if the current document is in that path.
  8513. if (strstr($file_path, $cur_path) !== false) {
  8514. // The document is in that path, now get the relative path
  8515. // to the containing document.
  8516. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8517. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8518. $relative_path = '';
  8519. if (strstr($file_path, $cur_path) !== false) {
  8520. //$relative_path = substr($file_path, strlen($orig_file_path));
  8521. $relative_path = str_replace($cur_path, '', $file_path);
  8522. $file_path = substr($file_path, strlen($cur_path));
  8523. } else {
  8524. // This case is still a problem as it's difficult to calculate a relative path easily
  8525. // might still generate wrong links.
  8526. //$file_path = substr($file_path,strlen($cur_path));
  8527. // Calculate the directory path to the current file (without trailing slash).
  8528. $my_relative_path = dirname($file_path);
  8529. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8530. $my_relative_file = basename($file_path);
  8531. // Calculate the directory path to the containing file (without trailing slash).
  8532. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8533. $dotdot = '';
  8534. $subdir = '';
  8535. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8536. $my_relative_path2 = dirname($my_relative_path);
  8537. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8538. $my_orig_file_path = dirname($my_orig_file_path);
  8539. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8540. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8541. $dotdot += '../';
  8542. $my_relative_path = $my_relative_path2;
  8543. }
  8544. $relative_path = $dotdot.$subdir.$my_relative_file;
  8545. }
  8546. // Put the current document in the zip (this array is the array
  8547. // that will manage documents already in the course folder - relative).
  8548. $zip_files[] = $file_path;
  8549. // Update the links to the current document in the containing document (make them relative).
  8550. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8551. $my_dep_file->setAttribute('href', $file_path);
  8552. $my_dep->setAttribute('xml:base', '');
  8553. } elseif (strstr($file_path, $main_path) !== false) {
  8554. // The calculated real path is really inside Chamilo's root path.
  8555. // Reduce file path to what's under the DocumentRoot.
  8556. $file_path = substr($file_path, strlen($root_path));
  8557. //echo $file_path;echo '<br /><br />';
  8558. //error_log('Reduced path: '.$file_path, 0);
  8559. $zip_files_abs[] = $file_path;
  8560. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8561. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8562. $my_dep->setAttribute('xml:base', '');
  8563. } elseif (empty($file_path)) {
  8564. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8565. if(strpos($document_root,-1) == '/') {
  8566. $document_root = substr(0, -1, $document_root);
  8567. }*/
  8568. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8569. $file_path = str_replace('//', '/', $file_path);
  8570. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8571. $current_dir = dirname($abs_path);
  8572. $current_dir = str_replace('\\', '/', $current_dir);
  8573. if (file_exists($file_path)) {
  8574. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8575. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8576. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8577. $my_dep_file->setAttribute('href','document/'.$file_path);
  8578. $my_dep->setAttribute('xml:base', '');
  8579. }
  8580. }
  8581. break;
  8582. case 'rel':
  8583. // Path relative to the current document.
  8584. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8585. if (substr($doc_info[0], 0, 2) == '..') {
  8586. // Relative path going up.
  8587. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8588. $current_dir = str_replace('\\', '/', $current_dir);
  8589. $file_path = realpath($current_dir.$doc_info[0]);
  8590. $file_path = str_replace('\\', '/', $file_path);
  8591. //error_log($file_path.' <-> '.$main_path,0);
  8592. if (strstr($file_path, $main_path) !== false) {
  8593. // The calculated real path is really inside Chamilo's root path.
  8594. // Reduce file path to what's under the DocumentRoot.
  8595. $file_path = substr($file_path, strlen($root_path));
  8596. //error_log('Reduced path: '.$file_path, 0);
  8597. $zip_files_abs[] = $file_path;
  8598. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8599. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8600. $my_dep->setAttribute('xml:base', '');
  8601. }
  8602. } else {
  8603. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8604. $my_dep_file->setAttribute('href', $doc_info[0]);
  8605. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8606. }
  8607. break;
  8608. default:
  8609. $my_dep_file->setAttribute('href', $doc_info[0]);
  8610. $my_dep->setAttribute('xml:base', '');
  8611. break;
  8612. }
  8613. }
  8614. $my_dep->appendChild($my_dep_file);
  8615. $resources->appendChild($my_dep);
  8616. $dependency = $xmldoc->createElement('dependency');
  8617. $dependency->setAttribute('identifierref', $res_id);
  8618. $my_resource->appendChild($dependency);
  8619. $i++;
  8620. }
  8621. $resources->appendChild($my_resource);
  8622. $zip_files[] = $my_file_path;
  8623. } else {
  8624. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8625. switch ($item->type) {
  8626. case TOOL_LINK:
  8627. $my_item = $xmldoc->createElement('item');
  8628. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8629. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8630. $my_item->setAttribute('isvisible', 'true');
  8631. // Give a child element <title> to the <item> element.
  8632. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8633. $my_item->appendChild($my_title);
  8634. // Give a child element <adlcp:prerequisites> to the <item> element.
  8635. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8636. $my_prereqs->setAttribute('type', 'aicc_script');
  8637. $my_item->appendChild($my_prereqs);
  8638. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8639. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8640. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8641. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8642. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8643. //$xmldoc->createElement('adlcp:datafromlms', '');
  8644. // Give a child element <adlcp:masteryscore> to the <item> element.
  8645. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8646. $my_item->appendChild($my_masteryscore);
  8647. // Attach this item to the organization element or its parent if there is one.
  8648. if (!empty($item->parent) && $item->parent != 0) {
  8649. $children = $organization->childNodes;
  8650. for ($i = 0; $i < $children->length; $i++) {
  8651. $item_temp = $children->item($i);
  8652. if ($item_temp -> nodeName == 'item') {
  8653. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8654. $item_temp -> appendChild($my_item);
  8655. }
  8656. }
  8657. }
  8658. } else {
  8659. $organization->appendChild($my_item);
  8660. }
  8661. $my_file_path = 'link_'.$item->get_id().'.html';
  8662. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8663. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8664. $rs = Database::query($sql);
  8665. if ($link = Database :: fetch_array($rs)) {
  8666. $url = $link['url'];
  8667. $title = stripslashes($link['title']);
  8668. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8669. $my_xml_file_path = $my_file_path;
  8670. $my_sub_dir = dirname($my_file_path);
  8671. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8672. $my_xml_sub_dir = $my_sub_dir;
  8673. // Give a <resource> child to the <resources> element.
  8674. $my_resource = $xmldoc->createElement('resource');
  8675. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8676. $my_resource->setAttribute('type', 'webcontent');
  8677. $my_resource->setAttribute('href', $my_xml_file_path);
  8678. // adlcp:scormtype can be either 'sco' or 'asset'.
  8679. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8680. // xml:base is the base directory to find the files declared in this resource.
  8681. $my_resource->setAttribute('xml:base', '');
  8682. // give a <file> child to the <resource> element.
  8683. $my_file = $xmldoc->createElement('file');
  8684. $my_file->setAttribute('href', $my_xml_file_path);
  8685. $my_resource->appendChild($my_file);
  8686. $resources->appendChild($my_resource);
  8687. }
  8688. break;
  8689. case TOOL_QUIZ:
  8690. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8691. $exe = new Exercise();
  8692. $exe->read($exe_id);
  8693. $my_item = $xmldoc->createElement('item');
  8694. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8695. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8696. $my_item->setAttribute('isvisible', 'true');
  8697. // Give a child element <title> to the <item> element.
  8698. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8699. $my_item->appendChild($my_title);
  8700. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8701. //$my_item->appendChild($my_max_score);
  8702. // Give a child element <adlcp:prerequisites> to the <item> element.
  8703. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8704. $my_prereqs->setAttribute('type','aicc_script');
  8705. $my_item->appendChild($my_prereqs);
  8706. // Give a child element <adlcp:masteryscore> to the <item> element.
  8707. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8708. $my_item->appendChild($my_masteryscore);
  8709. // Attach this item to the organization element or hits parent if there is one.
  8710. if (!empty($item->parent) && $item->parent != 0) {
  8711. $children = $organization->childNodes;
  8712. /*for ($i = 0; $i < $children->length; $i++) {
  8713. $item_temp = $children->item($i);
  8714. if ($exe_id == 81) {
  8715. error_log($item_temp->nodeName );
  8716. error_log($item_temp->getAttribute('identifier'));
  8717. }
  8718. if ($item_temp->nodeName == 'item') {
  8719. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8720. $item_temp->appendChild($my_item);
  8721. }
  8722. }
  8723. }*/
  8724. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8725. if ($possible_parent) {
  8726. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8727. $possible_parent->appendChild($my_item);
  8728. }
  8729. }
  8730. } else {
  8731. $organization->appendChild($my_item);
  8732. }
  8733. // Get the path of the file(s) from the course directory root
  8734. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8735. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8736. // Write the contents of the exported exercise into a (big) html file
  8737. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8738. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  8739. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8740. $res = file_put_contents($tmp_file_path, $contents);
  8741. if ($res === false) {
  8742. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8743. }
  8744. $files_cleanup[] = $tmp_file_path;
  8745. $my_xml_file_path = $my_file_path;
  8746. $my_sub_dir = dirname($my_file_path);
  8747. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8748. $my_xml_sub_dir = $my_sub_dir;
  8749. // Give a <resource> child to the <resources> element.
  8750. $my_resource = $xmldoc->createElement('resource');
  8751. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8752. $my_resource->setAttribute('type', 'webcontent');
  8753. $my_resource->setAttribute('href', $my_xml_file_path);
  8754. // adlcp:scormtype can be either 'sco' or 'asset'.
  8755. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8756. // xml:base is the base directory to find the files declared in this resource.
  8757. $my_resource->setAttribute('xml:base', '');
  8758. // Give a <file> child to the <resource> element.
  8759. $my_file = $xmldoc->createElement('file');
  8760. $my_file->setAttribute('href', $my_xml_file_path);
  8761. $my_resource->appendChild($my_file);
  8762. // Get included docs.
  8763. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8764. // Dependency to other files - not yet supported.
  8765. $i = 1;
  8766. foreach ($inc_docs as $doc_info) {
  8767. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8768. $my_dep = $xmldoc->createElement('resource');
  8769. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8770. $my_dep->setAttribute('identifier', $res_id);
  8771. $my_dep->setAttribute('type', 'webcontent');
  8772. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8773. $my_dep_file = $xmldoc->createElement('file');
  8774. // Check type of URL.
  8775. if ($doc_info[1] == 'remote') {
  8776. // Remote file. Save url as is.
  8777. $my_dep_file->setAttribute('href', $doc_info[0]);
  8778. $my_dep->setAttribute('xml:base', '');
  8779. } elseif ($doc_info[1] == 'local') {
  8780. switch ($doc_info[2]) {
  8781. case 'url': // Local URL - save path as url for now, don't zip file.
  8782. // Save file but as local file (retrieve from URL).
  8783. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8784. $current_dir = dirname($abs_path);
  8785. $current_dir = str_replace('\\', '/', $current_dir);
  8786. $file_path = realpath($abs_path);
  8787. $file_path = str_replace('\\', '/', $file_path);
  8788. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8789. $my_dep->setAttribute('xml:base', '');
  8790. if (strstr($file_path, $main_path) !== false) {
  8791. // The calculated real path is really inside the chamilo root path.
  8792. // Reduce file path to what's under the DocumentRoot.
  8793. $file_path = substr($file_path, strlen($root_path));
  8794. $zip_files_abs[] = $file_path;
  8795. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8796. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8797. $my_dep->setAttribute('xml:base', '');
  8798. } elseif (empty($file_path)) {
  8799. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8800. if (strpos($document_root,-1) == '/') {
  8801. $document_root = substr(0, -1, $document_root);
  8802. }*/
  8803. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8804. $file_path = str_replace('//', '/', $file_path);
  8805. if (file_exists($file_path)) {
  8806. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8807. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8808. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8809. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8810. $my_dep->setAttribute('xml:base', '');
  8811. }
  8812. }
  8813. break;
  8814. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8815. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8816. $current_dir = str_replace('\\', '/', $current_dir);
  8817. $file_path = realpath($doc_info[0]);
  8818. $file_path = str_replace('\\', '/', $file_path);
  8819. $my_dep_file->setAttribute('href', $file_path);
  8820. $my_dep->setAttribute('xml:base', '');
  8821. if (strstr($file_path,$main_path) !== false) {
  8822. // The calculated real path is really inside the chamilo root path.
  8823. // Reduce file path to what's under the DocumentRoot.
  8824. $file_path = substr($file_path, strlen($root_path));
  8825. //echo $file_path;echo '<br /><br />';
  8826. //error_log('Reduced path: '.$file_path, 0);
  8827. $zip_files_abs[] = $file_path;
  8828. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8829. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8830. $my_dep->setAttribute('xml:base', '');
  8831. } elseif (empty($file_path)) {
  8832. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8833. if (strpos($document_root,-1) == '/') {
  8834. $document_root = substr(0, -1, $document_root);
  8835. }*/
  8836. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8837. $file_path = str_replace('//', '/', $file_path);
  8838. if (file_exists($file_path)) {
  8839. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8840. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8841. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8842. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8843. $my_dep->setAttribute('xml:base', '');
  8844. }
  8845. }
  8846. break;
  8847. case 'rel': // Path relative to the current document. Save xml:base as current document's directory and save file in zip as subdir.file_path
  8848. if (substr($doc_info[0], 0, 2) == '..') {
  8849. // Relative path going up.
  8850. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8851. $current_dir = str_replace('\\', '/', $current_dir);
  8852. $file_path = realpath($current_dir.$doc_info[0]);
  8853. $file_path = str_replace('\\', '/', $file_path);
  8854. //error_log($file_path.' <-> '.$main_path, 0);
  8855. if (strstr($file_path, $main_path) !== false) {
  8856. // The calculated real path is really inside Chamilo's root path.
  8857. // Reduce file path to what's under the DocumentRoot.
  8858. $file_path = substr($file_path, strlen($root_path));
  8859. $file_path_dest = $file_path;
  8860. // File path is courses/CHAMILO/document/....
  8861. $info_file_path = explode('/', $file_path);
  8862. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8863. $file_path_dest = 'document/'.$file_path;
  8864. }
  8865. //error_log('Reduced path: '.$file_path, 0);
  8866. $zip_files_abs[] = $file_path;
  8867. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8868. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8869. $my_dep->setAttribute('xml:base', '');
  8870. }
  8871. } else {
  8872. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8873. $my_dep_file->setAttribute('href', $doc_info[0]);
  8874. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8875. }
  8876. break;
  8877. default:
  8878. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8879. $my_dep->setAttribute('xml:base', '');
  8880. break;
  8881. }
  8882. }
  8883. $my_dep->appendChild($my_dep_file);
  8884. $resources->appendChild($my_dep);
  8885. $dependency = $xmldoc->createElement('dependency');
  8886. $dependency->setAttribute('identifierref', $res_id);
  8887. $my_resource->appendChild($dependency);
  8888. $i++;
  8889. }
  8890. $resources->appendChild($my_resource);
  8891. $zip_files[] = $my_file_path;
  8892. break;
  8893. default:
  8894. // Get the path of the file(s) from the course directory root
  8895. $my_file_path = 'non_exportable.html';
  8896. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8897. $my_xml_file_path = $my_file_path;
  8898. $my_sub_dir = dirname($my_file_path);
  8899. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8900. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8901. $my_xml_sub_dir = $my_sub_dir;
  8902. // Give a <resource> child to the <resources> element.
  8903. $my_resource = $xmldoc->createElement('resource');
  8904. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8905. $my_resource->setAttribute('type', 'webcontent');
  8906. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8907. // adlcp:scormtype can be either 'sco' or 'asset'.
  8908. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8909. // xml:base is the base directory to find the files declared in this resource.
  8910. $my_resource->setAttribute('xml:base', '');
  8911. // Give a <file> child to the <resource> element.
  8912. $my_file = $xmldoc->createElement('file');
  8913. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8914. $my_resource->appendChild($my_file);
  8915. $resources->appendChild($my_resource);
  8916. break;
  8917. }
  8918. }
  8919. }
  8920. $organizations->appendChild($organization);
  8921. $root->appendChild($organizations);
  8922. $root->appendChild($resources);
  8923. $xmldoc->appendChild($root);
  8924. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8925. // then add the file to the zip, then destroy the file (this is done automatically).
  8926. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8927. foreach ($zip_files as $file_path) {
  8928. if (empty($file_path)) {
  8929. continue;
  8930. }
  8931. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8932. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8933. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8934. }
  8935. $this->create_path($dest_file);
  8936. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8937. // Check if the file needs a link update.
  8938. if (in_array($file_path, array_keys($link_updates))) {
  8939. $string = file_get_contents($dest_file);
  8940. unlink($dest_file);
  8941. foreach ($link_updates[$file_path] as $old_new) {
  8942. // This is an ugly hack that allows .flv files to be found by the flv player that
  8943. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8944. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8945. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8946. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8947. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8948. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8949. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8950. }
  8951. //Fix to avoid problems with default_course_document
  8952. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8953. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8954. $newDestination = $old_new['dest'];
  8955. } else {
  8956. $newDestination = str_replace('document/', '', $old_new['dest']);
  8957. }
  8958. $string = str_replace($old_new['orig'], $newDestination, $string);
  8959. // Add files inside the HTMLs
  8960. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  8961. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8962. if (file_exists($sys_course_path.$new_path)) {
  8963. copy($sys_course_path.$new_path, $destinationFile);
  8964. }
  8965. }
  8966. file_put_contents($dest_file, $string);
  8967. }
  8968. }
  8969. foreach ($zip_files_abs as $file_path) {
  8970. if (empty($file_path)) {
  8971. continue;
  8972. }
  8973. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8974. continue;
  8975. }
  8976. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8977. $this->create_path($dest_file);
  8978. copy($main_path.$file_path, $dest_file);
  8979. // Check if the file needs a link update.
  8980. if (in_array($file_path, array_keys($link_updates))) {
  8981. $string = file_get_contents($dest_file);
  8982. unlink($dest_file);
  8983. foreach ($link_updates[$file_path] as $old_new) {
  8984. // This is an ugly hack that allows .flv files to be found by the flv player that
  8985. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8986. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8987. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8988. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8989. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8990. }
  8991. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8992. }
  8993. file_put_contents($dest_file, $string);
  8994. }
  8995. }
  8996. if (is_array($links_to_create)) {
  8997. foreach ($links_to_create as $file => $link) {
  8998. $file_content = '<!DOCTYPE html><head>
  8999. <meta charset="'.api_get_language_isocode().'" />
  9000. <title>'.$link['title'].'</title>
  9001. </head>
  9002. <body dir="'.api_get_text_direction().'">
  9003. <div style="text-align:center">
  9004. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9005. </body>
  9006. </html>';
  9007. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9008. }
  9009. }
  9010. // Add non exportable message explanation.
  9011. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9012. $file_content = '<!DOCTYPE html><head>
  9013. <meta charset="'.api_get_language_isocode().'" />
  9014. <title>'.$lang_not_exportable.'</title>
  9015. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9016. </head>
  9017. <body dir="'.api_get_text_direction().'">';
  9018. $file_content .=
  9019. <<<EOD
  9020. <style>
  9021. .error-message {
  9022. font-family: arial, verdana, helvetica, sans-serif;
  9023. border-width: 1px;
  9024. border-style: solid;
  9025. left: 50%;
  9026. margin: 10px auto;
  9027. min-height: 30px;
  9028. padding: 5px;
  9029. right: 50%;
  9030. width: 500px;
  9031. background-color: #FFD1D1;
  9032. border-color: #FF0000;
  9033. color: #000;
  9034. }
  9035. </style>
  9036. <body>
  9037. <div class="error-message">
  9038. $lang_not_exportable
  9039. </div>
  9040. </body>
  9041. </html>
  9042. EOD;
  9043. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9044. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9045. }
  9046. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9047. // Add the extra files that go along with a SCORM package.
  9048. $main_code_path = api_get_path(SYS_CODE_PATH) . 'lp/packaging/';
  9049. $fs = new \Symfony\Component\Filesystem\Filesystem;
  9050. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9051. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9052. $manifest = @$xmldoc->saveXML();
  9053. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9054. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9055. $zip_folder->add(
  9056. $archive_path.'/'.$temp_dir_short,
  9057. PCLZIP_OPT_REMOVE_PATH,
  9058. $archive_path.'/'.$temp_dir_short.'/'
  9059. );
  9060. // Clean possible temporary files.
  9061. foreach ($files_cleanup as $file) {
  9062. $res = unlink($file);
  9063. if ($res === false) {
  9064. error_log(
  9065. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9066. 0
  9067. );
  9068. }
  9069. }
  9070. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9071. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9072. }
  9073. /**
  9074. * @param int $lp_id
  9075. * @return bool
  9076. */
  9077. public function scorm_export_to_pdf($lp_id)
  9078. {
  9079. $lp_id = intval($lp_id);
  9080. $files_to_export = array();
  9081. $course_data = api_get_course_info($this->cc);
  9082. if (!empty($course_data)) {
  9083. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9084. $list = self::get_flat_ordered_items_list($lp_id);
  9085. if (!empty($list)) {
  9086. foreach ($list as $item_id) {
  9087. $item = $this->items[$item_id];
  9088. switch ($item->type) {
  9089. case 'document':
  9090. //Getting documents from a LP with chamilo documents
  9091. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9092. // Try loading document from the base course.
  9093. if (empty($file_data) && !empty($sessionId)) {
  9094. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9095. }
  9096. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9097. if (file_exists($file_path)) {
  9098. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9099. }
  9100. break;
  9101. case 'asset': //commes from a scorm package generated by chamilo
  9102. case 'sco':
  9103. $file_path = $scorm_path.'/'.$item->path;
  9104. if (file_exists($file_path)) {
  9105. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9106. }
  9107. break;
  9108. case 'dir':
  9109. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9110. break;
  9111. }
  9112. }
  9113. }
  9114. $pdf = new PDF();
  9115. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9116. return $result;
  9117. }
  9118. return false;
  9119. }
  9120. /**
  9121. * Temp function to be moved in main_api or the best place around for this.
  9122. * Creates a file path if it doesn't exist
  9123. * @param string $path
  9124. */
  9125. public function create_path($path)
  9126. {
  9127. $path_bits = explode('/', dirname($path));
  9128. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9129. $path_built = IS_WINDOWS_OS ? '' : '/';
  9130. foreach ($path_bits as $bit) {
  9131. if (!empty ($bit)) {
  9132. $new_path = $path_built . $bit;
  9133. if (is_dir($new_path)) {
  9134. $path_built = $new_path . '/';
  9135. } else {
  9136. mkdir($new_path, api_get_permissions_for_new_directories());
  9137. $path_built = $new_path . '/';
  9138. }
  9139. }
  9140. }
  9141. }
  9142. /**
  9143. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9144. * @return boolean The results of the unlink function, or false if there was no image to start with
  9145. */
  9146. public function delete_lp_image()
  9147. {
  9148. $img = $this->get_preview_image();
  9149. if ($img != '') {
  9150. $del_file = $this->get_preview_image_path(null, 'sys');
  9151. if (isset($del_file) && file_exists($del_file)) {
  9152. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9153. if (file_exists($del_file_2)) {
  9154. unlink($del_file_2);
  9155. }
  9156. $this->set_preview_image('');
  9157. return @unlink($del_file);
  9158. }
  9159. }
  9160. return false;
  9161. }
  9162. /**
  9163. * Uploads an author image to the upload/learning_path/images directory
  9164. * @param array The image array, coming from the $_FILES superglobal
  9165. * @return boolean True on success, false on error
  9166. */
  9167. public function upload_image($image_array)
  9168. {
  9169. $image_moved = false;
  9170. if (!empty ($image_array['name'])) {
  9171. $upload_ok = process_uploaded_file($image_array);
  9172. $has_attachment = true;
  9173. } else {
  9174. $image_moved = true;
  9175. }
  9176. if ($upload_ok) {
  9177. if ($has_attachment) {
  9178. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9179. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9180. $updir = $sys_course_path . $courseDir;
  9181. // Try to add an extension to the file if it hasn't one.
  9182. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9183. if (!filter_extension($new_file_name)) {
  9184. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9185. $image_moved = false;
  9186. } else {
  9187. $file_extension = explode('.', $image_array['name']);
  9188. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9189. $filename = uniqid('');
  9190. $new_file_name = $filename.'.'.$file_extension;
  9191. $new_path = $updir.'/'.$new_file_name;
  9192. // Resize the image.
  9193. $temp = new Image($image_array['tmp_name']);
  9194. $temp->resize(104);
  9195. $result = $temp->send_image($new_path);
  9196. // Storing the image filename.
  9197. if ($result) {
  9198. $image_moved = true;
  9199. $this->set_preview_image($new_file_name);
  9200. //Resize to 64px to use on course homepage
  9201. $temp->resize(64);
  9202. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9203. return true;
  9204. }
  9205. }
  9206. }
  9207. }
  9208. return false;
  9209. }
  9210. /**
  9211. * @param int $lp_id
  9212. * @param string $status
  9213. */
  9214. public function set_autolaunch($lp_id, $status)
  9215. {
  9216. $course_id = api_get_course_int_id();
  9217. $lp_id = intval($lp_id);
  9218. $status = intval($status);
  9219. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9220. // Setting everything to autolaunch = 0
  9221. $attributes['autolaunch'] = 0;
  9222. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9223. Database::update($lp_table, $attributes, $where);
  9224. if ($status == 1) {
  9225. //Setting my lp_id to autolaunch = 1
  9226. $attributes['autolaunch'] = 1;
  9227. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9228. Database::update($lp_table, $attributes, $where );
  9229. }
  9230. }
  9231. /**
  9232. * Gets previous_item_id for the next element of the lp_item table
  9233. * @author Isaac flores paz
  9234. * @return integer Previous item ID
  9235. */
  9236. public function select_previous_item_id()
  9237. {
  9238. $course_id = api_get_course_int_id();
  9239. if ($this->debug > 0) {
  9240. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9241. }
  9242. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9243. // Get the max order of the items
  9244. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9245. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9246. $rs_max_order = Database::query($sql_max_order);
  9247. $row_max_order = Database::fetch_object($rs_max_order);
  9248. $max_order = $row_max_order->display_order;
  9249. // Get the previous item ID
  9250. $sql = "SELECT id as previous FROM $table_lp_item
  9251. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9252. $rs_max = Database::query($sql);
  9253. $row_max = Database::fetch_object($rs_max);
  9254. // Return the previous item ID
  9255. return $row_max->previous;
  9256. }
  9257. /**
  9258. * Copies an LP
  9259. */
  9260. public function copy()
  9261. {
  9262. $main_path = api_get_path(SYS_CODE_PATH);
  9263. //Course builder
  9264. $cb = new CourseBuilder();
  9265. //Setting tools that will be copied
  9266. $cb->set_tools_to_build(array('learnpaths'));
  9267. //Setting elements that will be copied
  9268. $cb->set_tools_specific_id_list(
  9269. array('learnpaths' => array($this->lp_id))
  9270. );
  9271. $course = $cb->build();
  9272. //Course restorer
  9273. $course_restorer = new CourseRestorer($course);
  9274. $course_restorer->set_add_text_in_items(true);
  9275. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9276. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9277. }
  9278. public function verify_document_size($s)
  9279. {
  9280. $post_max = ini_get('post_max_size');
  9281. if (substr($post_max, -1, 1) == 'M') {
  9282. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9283. } elseif (substr($post_max, -1, 1) == 'G') {
  9284. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9285. }
  9286. $upl_max = ini_get('upload_max_filesize');
  9287. if (substr($upl_max, -1, 1) == 'M') {
  9288. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9289. } elseif (substr($upl_max, -1, 1) == 'G') {
  9290. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9291. }
  9292. $documents_total_space = DocumentManager::documents_total_space();
  9293. $course_max_space = DocumentManager::get_course_quota();
  9294. $total_size = filesize($s) + $documents_total_space;
  9295. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9296. return true;
  9297. } else{
  9298. return false;
  9299. }
  9300. }
  9301. /**
  9302. * Clear LP prerequisites
  9303. */
  9304. public function clear_prerequisites()
  9305. {
  9306. $course_id = $this->get_course_int_id();
  9307. if ($this->debug > 0) {
  9308. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9309. }
  9310. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9311. $lp_id = $this->get_id();
  9312. //Cleaning prerequisites
  9313. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9314. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9315. Database::query($sql);
  9316. //Cleaning mastery score for exercises
  9317. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9318. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9319. Database::query($sql);
  9320. }
  9321. public function set_previous_step_as_prerequisite_for_all_items()
  9322. {
  9323. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9324. $course_id = $this->get_course_int_id();
  9325. $lp_id = $this->get_id();
  9326. if (!empty($this->items)) {
  9327. $previous_item_id = null;
  9328. $previous_item_max = 0;
  9329. $previous_item_type = null;
  9330. $last_item_not_dir = null;
  9331. $last_item_not_dir_type = null;
  9332. $last_item_not_dir_max = null;
  9333. foreach ($this->items as $item) {
  9334. // if there was a previous item... (otherwise jump to set it)
  9335. if (!empty($previous_item_id)) {
  9336. $current_item_id = $item->get_id(); //save current id
  9337. if ($item->get_type() != 'dir') {
  9338. // Current item is not a folder, so it qualifies to get a prerequisites
  9339. if ($last_item_not_dir_type == 'quiz') {
  9340. // if previous is quiz, mark its max score as default score to be achieved
  9341. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9342. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9343. Database::query($sql);
  9344. }
  9345. // now simply update the prerequisite to set it to the last non-chapter item
  9346. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9347. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9348. Database::query($sql);
  9349. // record item as 'non-chapter' reference
  9350. $last_item_not_dir = $item->get_id();
  9351. $last_item_not_dir_type = $item->get_type();
  9352. $last_item_not_dir_max = $item->get_max();
  9353. }
  9354. } else {
  9355. if ($item->get_type() != 'dir') {
  9356. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9357. $last_item_not_dir = $item->get_id();
  9358. $last_item_not_dir_type = $item->get_type();
  9359. $last_item_not_dir_max = $item->get_max();
  9360. }
  9361. }
  9362. // Saving the item as "previous item" for the next loop
  9363. $previous_item_id = $item->get_id();
  9364. $previous_item_max = $item->get_max();
  9365. $previous_item_type = $item->get_type();
  9366. }
  9367. }
  9368. }
  9369. /**
  9370. * @param array $params
  9371. */
  9372. public static function createCategory($params)
  9373. {
  9374. $em = Database::getManager();
  9375. $item = new CLpCategory();
  9376. $item->setName($params['name']);
  9377. $item->setCId($params['c_id']);
  9378. $em->persist($item);
  9379. $em->flush();
  9380. }
  9381. /**
  9382. * @param array $params
  9383. */
  9384. public static function updateCategory($params)
  9385. {
  9386. $em = Database::getManager();
  9387. /** @var CLpCategory $item */
  9388. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9389. if ($item) {
  9390. $item->setName($params['name']);
  9391. $em->merge($item);
  9392. $em->flush();
  9393. }
  9394. }
  9395. /**
  9396. * @param int $id
  9397. */
  9398. public static function moveUpCategory($id)
  9399. {
  9400. $em = Database::getManager();
  9401. /** @var CLpCategory $item */
  9402. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9403. if ($item) {
  9404. $position = $item->getPosition() - 1;
  9405. $item->setPosition($position);
  9406. $em->persist($item);
  9407. $em->flush();
  9408. }
  9409. }
  9410. /**
  9411. * @param int $id
  9412. */
  9413. public static function moveDownCategory($id)
  9414. {
  9415. $em = Database::getManager();
  9416. /** @var CLpCategory $item */
  9417. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9418. if ($item) {
  9419. $position = $item->getPosition() + 1;
  9420. $item->setPosition($position);
  9421. $em->persist($item);
  9422. $em->flush();
  9423. }
  9424. }
  9425. /**
  9426. * @param int $courseId
  9427. * @return int|mixed
  9428. */
  9429. public static function getCountCategories($courseId)
  9430. {
  9431. if (empty($course_id)) {
  9432. return 0;
  9433. }
  9434. $em = Database::getManager();
  9435. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9436. $query->setParameter('id', $courseId);
  9437. return $query->getSingleScalarResult();
  9438. }
  9439. /**
  9440. * @param int $courseId
  9441. *
  9442. * @return mixed
  9443. */
  9444. public static function getCategories($courseId)
  9445. {
  9446. $em = Database::getManager();
  9447. //Default behaviour
  9448. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9449. array('cId' => $course_id),
  9450. array('name' => 'ASC')
  9451. );*/
  9452. // Using doctrine extensions
  9453. /** @var SortableRepository $repo */
  9454. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9455. $items = $repo
  9456. ->getBySortableGroupsQuery(['cId' => $courseId])
  9457. ->getResult();
  9458. return $items;
  9459. }
  9460. /**
  9461. * @param int $id
  9462. *
  9463. * @return mixed
  9464. */
  9465. public static function getCategory($id)
  9466. {
  9467. $em = Database::getManager();
  9468. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9469. return $item;
  9470. }
  9471. /**
  9472. * @param int $courseId
  9473. * @return array
  9474. */
  9475. public static function getCategoryByCourse($courseId)
  9476. {
  9477. $em = Database::getManager();
  9478. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9479. return $items;
  9480. }
  9481. /**
  9482. * @param int $id
  9483. *
  9484. * @return mixed
  9485. */
  9486. public static function deleteCategory($id)
  9487. {
  9488. $em = Database::getManager();
  9489. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9490. if ($item) {
  9491. $courseId = $item->getCId();
  9492. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9493. $query->setParameter('id', $courseId);
  9494. $query->setParameter('catId', $item->getId());
  9495. $lps = $query->getResult();
  9496. // Setting category = 0.
  9497. if ($lps) {
  9498. foreach ($lps as $lpItem) {
  9499. $lpItem->setCategoryId(0);
  9500. }
  9501. }
  9502. // Removing category.
  9503. $em->remove($item);
  9504. $em->flush();
  9505. }
  9506. }
  9507. /**
  9508. * @param int $courseId
  9509. * @param bool $addSelectOption
  9510. *
  9511. * @return mixed
  9512. */
  9513. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9514. {
  9515. $items = self::getCategoryByCourse($courseId);
  9516. $cats = array();
  9517. if ($addSelectOption) {
  9518. $cats = array(get_lang('SelectACategory'));
  9519. }
  9520. if (!empty($items)) {
  9521. foreach ($items as $cat) {
  9522. $cats[$cat->getId()] = $cat->getName();
  9523. }
  9524. }
  9525. return $cats;
  9526. }
  9527. /**
  9528. * Return the scorm item type object with spaces replaced with _
  9529. * The return result is use to build a css classname like scorm_type_$return
  9530. * @param $in_type
  9531. * @return mixed
  9532. */
  9533. private static function format_scorm_type_item($in_type)
  9534. {
  9535. return str_replace(' ', '_', $in_type);
  9536. }
  9537. /**
  9538. * @param string $courseCode
  9539. * @param int $lp_id
  9540. * @param int $user_id
  9541. *
  9542. * @return learnpath
  9543. */
  9544. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9545. {
  9546. $learnPath = null;
  9547. $lpObject = Session::read('lpobject');
  9548. if ($lpObject !== null) {
  9549. $learnPath = unserialize($lpObject);
  9550. }
  9551. if (!is_object($learnPath)) {
  9552. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9553. }
  9554. return $learnPath;
  9555. }
  9556. /**
  9557. * @param int $itemId
  9558. * @return learnpathItem|false
  9559. */
  9560. public function getItem($itemId)
  9561. {
  9562. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9563. return $this->items[$itemId];
  9564. }
  9565. return false;
  9566. }
  9567. /**
  9568. * @return int
  9569. */
  9570. public function getCategoryId()
  9571. {
  9572. return $this->categoryId;
  9573. }
  9574. /**
  9575. * @param int $categoryId
  9576. * @return bool
  9577. */
  9578. public function setCategoryId($categoryId)
  9579. {
  9580. $this->categoryId = intval($categoryId);
  9581. $courseId = api_get_course_int_id();
  9582. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9583. $lp_id = $this->get_id();
  9584. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9585. WHERE c_id = $courseId AND id = $lp_id";
  9586. Database::query($sql);
  9587. return true;
  9588. }
  9589. /**
  9590. * Get whether this is a learning path with the possibility to subscribe
  9591. * users or not
  9592. * @return int
  9593. */
  9594. public function getSubscribeUsers()
  9595. {
  9596. return $this->subscribeUsers;
  9597. }
  9598. /**
  9599. * Set whether this is a learning path with the possibility to subscribe
  9600. * users or not
  9601. * @param int $subscribeUsers (0 = false, 1 = true)
  9602. */
  9603. public function setSubscribeUsers($value)
  9604. {
  9605. if ($this->debug > 0) {
  9606. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9607. }
  9608. $this->subscribeUsers = intval($value);;
  9609. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9610. $lp_id = $this->get_id();
  9611. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9612. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9613. Database::query($sql);
  9614. return true;
  9615. }
  9616. /**
  9617. * Calculate the count of stars for a user in this LP
  9618. * This calculation is based on the following rules:
  9619. * - the student gets one star when he gets to 50% of the learning path
  9620. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9621. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9622. * - the student gets the final star when the score for the *last* test is >= 80%
  9623. * @param int $sessionId Optional. The session ID
  9624. * @return int The count of stars
  9625. */
  9626. public function getCalculateStars($sessionId = 0)
  9627. {
  9628. $stars = 0;
  9629. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9630. if ($progress >= 50) {
  9631. $stars++;
  9632. }
  9633. // Calculate stars chapters evaluation
  9634. $exercisesItems = $this->getExercisesItems();
  9635. if (!empty($exercisesItems)) {
  9636. $totalResult = 0;
  9637. foreach ($exercisesItems as $exerciseItem) {
  9638. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9639. $this->user_id,
  9640. $exerciseItem->path,
  9641. $this->course_int_id,
  9642. $sessionId,
  9643. $this->lp_id,
  9644. $exerciseItem->db_id
  9645. );
  9646. $exerciseResultInfo = end($exerciseResultInfo);
  9647. if (!$exerciseResultInfo) {
  9648. continue;
  9649. }
  9650. if (!empty($exerciseResultInfo['exe_weighting'])) {
  9651. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9652. } else {
  9653. $exerciseResult = 0;
  9654. }
  9655. $totalResult += $exerciseResult;
  9656. }
  9657. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9658. if ($totalExerciseAverage >= 50) {
  9659. $stars++;
  9660. }
  9661. if ($totalExerciseAverage >= 80) {
  9662. $stars++;
  9663. }
  9664. }
  9665. // Calculate star for final evaluation
  9666. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9667. if (!empty($finalEvaluationItem)) {
  9668. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9669. $this->user_id,
  9670. $finalEvaluationItem->path,
  9671. $this->course_int_id,
  9672. $sessionId,
  9673. $this->lp_id,
  9674. $finalEvaluationItem->db_id
  9675. );
  9676. $evaluationResultInfo = end($evaluationResultInfo);
  9677. if ($evaluationResultInfo) {
  9678. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9679. if ($evaluationResult >= 80) {
  9680. $stars++;
  9681. }
  9682. }
  9683. }
  9684. return $stars;
  9685. }
  9686. /**
  9687. * Get the items of exercise type
  9688. * @return array The items. Otherwise return false
  9689. */
  9690. public function getExercisesItems()
  9691. {
  9692. $exercises = [];
  9693. foreach ($this->items as $item) {
  9694. if ($item->type != 'quiz') {
  9695. continue;
  9696. }
  9697. $exercises[] = $item;
  9698. }
  9699. array_pop($exercises);
  9700. return $exercises;
  9701. }
  9702. /**
  9703. * Get the item of exercise type (evaluation type)
  9704. * @return array The final evaluation. Otherwise return false
  9705. */
  9706. public function getFinalEvaluationItem()
  9707. {
  9708. $exercises = [];
  9709. foreach ($this->items as $item) {
  9710. if ($item->type != 'quiz') {
  9711. continue;
  9712. }
  9713. $exercises[] = $item;
  9714. }
  9715. return array_pop($exercises);
  9716. }
  9717. /**
  9718. * Calculate the total points achieved for the current user in this learning path
  9719. * @param int $sessionId Optional. The session Id
  9720. * @return int
  9721. */
  9722. public function getCalculateScore($sessionId = 0)
  9723. {
  9724. // Calculate stars chapters evaluation
  9725. $exercisesItems = $this->getExercisesItems();
  9726. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9727. $totalExercisesResult = 0;
  9728. $totalEvaluationResult = 0;
  9729. if ($exercisesItems !== false) {
  9730. foreach ($exercisesItems as $exerciseItem) {
  9731. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9732. $this->user_id,
  9733. $exerciseItem->path,
  9734. $this->course_int_id,
  9735. $sessionId,
  9736. $this->lp_id,
  9737. $exerciseItem->db_id
  9738. );
  9739. $exerciseResultInfo = end($exerciseResultInfo);
  9740. if (!$exerciseResultInfo) {
  9741. continue;
  9742. }
  9743. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  9744. }
  9745. }
  9746. if (!empty($finalEvaluationItem)) {
  9747. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9748. $this->user_id,
  9749. $finalEvaluationItem->path,
  9750. $this->course_int_id,
  9751. $sessionId,
  9752. $this->lp_id,
  9753. $finalEvaluationItem->db_id
  9754. );
  9755. $evaluationResultInfo = end($evaluationResultInfo);
  9756. if ($evaluationResultInfo) {
  9757. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  9758. }
  9759. }
  9760. return $totalExercisesResult + $totalEvaluationResult;
  9761. }
  9762. /**
  9763. * Check if URL is not allowed to be show in a iframe
  9764. * @param string $src
  9765. *
  9766. * @return string
  9767. */
  9768. public function fixBlockedLinks($src)
  9769. {
  9770. $urlInfo = parse_url($src);
  9771. //$platformProtocol = api_get_protocol();
  9772. $platformProtocol = 'https';
  9773. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9774. $platformProtocol = 'http';
  9775. }
  9776. $protocolFixApplied = false;
  9777. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9778. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9779. if ($platformProtocol != $scheme) {
  9780. Session::write('x_frame_source', $src);
  9781. $src = 'blank.php?error=x_frames_options';
  9782. $protocolFixApplied = true;
  9783. }
  9784. if ($protocolFixApplied == false) {
  9785. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9786. // Check X-Frame-Options
  9787. $ch = curl_init();
  9788. $options = array(
  9789. CURLOPT_URL => $src,
  9790. CURLOPT_RETURNTRANSFER => true,
  9791. CURLOPT_HEADER => true,
  9792. CURLOPT_FOLLOWLOCATION => true,
  9793. CURLOPT_ENCODING => "",
  9794. CURLOPT_AUTOREFERER => true,
  9795. CURLOPT_CONNECTTIMEOUT => 120,
  9796. CURLOPT_TIMEOUT => 120,
  9797. CURLOPT_MAXREDIRS => 10,
  9798. );
  9799. curl_setopt_array($ch, $options);
  9800. $response = curl_exec($ch);
  9801. $httpCode = curl_getinfo($ch);
  9802. $headers = substr($response, 0, $httpCode['header_size']);
  9803. $error = false;
  9804. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  9805. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9806. ) {
  9807. $error = true;
  9808. }
  9809. if ($error) {
  9810. Session::write('x_frame_source', $src);
  9811. $src = 'blank.php?error=x_frames_options';
  9812. }
  9813. }
  9814. }
  9815. return $src;
  9816. }
  9817. /**
  9818. * Check if this LP has a created forum in the basis course
  9819. * @return boolean
  9820. */
  9821. public function lpHasForum()
  9822. {
  9823. $forumTable = Database::get_course_table(TABLE_FORUM);
  9824. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9825. $fakeFrom = "
  9826. $forumTable f
  9827. INNER JOIN $itemProperty ip
  9828. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9829. ";
  9830. $resultData = Database::select(
  9831. 'COUNT(f.iid) AS qty',
  9832. $fakeFrom,
  9833. [
  9834. 'where' => [
  9835. 'ip.visibility != ? AND ' => 2,
  9836. 'ip.tool = ? AND ' => TOOL_FORUM,
  9837. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9838. 'f.lp_id = ?' => intval($this->lp_id)
  9839. ]
  9840. ],
  9841. 'first'
  9842. );
  9843. if ($resultData['qty'] > 0) {
  9844. return true;
  9845. }
  9846. return false;
  9847. }
  9848. /**
  9849. * Get the forum for this learning path
  9850. * @return boolean
  9851. */
  9852. public function getForum($sessionId = 0)
  9853. {
  9854. $forumTable = Database::get_course_table(TABLE_FORUM);
  9855. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9856. $fakeFrom = "$forumTable f
  9857. INNER JOIN $itemProperty ip ";
  9858. if ($this->lp_session_id == 0) {
  9859. $fakeFrom .= "
  9860. ON (
  9861. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9862. f.session_id = ip.session_id OR ip.session_id IS NULL
  9863. )
  9864. )
  9865. ";
  9866. } else {
  9867. $fakeFrom .= "
  9868. ON (
  9869. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9870. )
  9871. ";
  9872. }
  9873. $resultData = Database::select(
  9874. 'f.*',
  9875. $fakeFrom,
  9876. [
  9877. 'where' => [
  9878. 'ip.visibility != ? AND ' => 2,
  9879. 'ip.tool = ? AND ' => TOOL_FORUM,
  9880. 'f.session_id = ? AND ' => $sessionId,
  9881. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9882. 'f.lp_id = ?' => intval($this->lp_id)
  9883. ]
  9884. ],
  9885. 'first'
  9886. );
  9887. if (empty($resultData)) {
  9888. return false;
  9889. }
  9890. return $resultData;
  9891. }
  9892. /**
  9893. * Create a forum for this learning path
  9894. * @param type $forumCategoryId
  9895. * @return int The forum ID if was created. Otherwise return false
  9896. */
  9897. public function createForum($forumCategoryId)
  9898. {
  9899. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9900. $forumId = store_forum(
  9901. [
  9902. 'lp_id' => $this->lp_id,
  9903. 'forum_title' => $this->name,
  9904. 'forum_comment' => null,
  9905. 'forum_category' => intval($forumCategoryId),
  9906. 'students_can_edit_group' => ['students_can_edit' => 0],
  9907. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9908. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9909. 'group_forum' => 0,
  9910. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9911. ],
  9912. [],
  9913. true
  9914. );
  9915. return $forumId;
  9916. }
  9917. /**
  9918. * Check and obtain the lp final item if exist
  9919. *
  9920. * @return array lp items
  9921. */
  9922. private function getFinalItem()
  9923. {
  9924. if (empty($this->items)) {
  9925. return null;
  9926. }
  9927. foreach ($this->items as $item) {
  9928. if ($item->type !== 'final_item') {
  9929. continue;
  9930. }
  9931. return $item;
  9932. }
  9933. }
  9934. /**
  9935. * Get the LP Final Item Template
  9936. *
  9937. * @return string
  9938. */
  9939. private function getFinalItemTemplate()
  9940. {
  9941. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'lp/final_item_template/template.html');
  9942. }
  9943. /**
  9944. * Get the LP Final Item Url
  9945. *
  9946. * @return string
  9947. */
  9948. private function getSavedFinalItem()
  9949. {
  9950. $finalItem = $this->getFinalItem();
  9951. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  9952. return file_get_contents($doc['absolute_path']);
  9953. }
  9954. /**
  9955. * Get the LP Final Item form
  9956. *
  9957. * @return string
  9958. */
  9959. public function getFinalItemForm()
  9960. {
  9961. $finalItem = $this->getFinalItem();
  9962. $title = '';
  9963. if ($finalItem) {
  9964. $title = $finalItem->title;
  9965. $buttonText = get_lang('Save');
  9966. $content = $this->getSavedFinalItem();
  9967. } else {
  9968. $buttonText = get_lang('LPCreateDocument');
  9969. $content = $this->getFinalItemTemplate();
  9970. }
  9971. $courseInfo = api_get_course_info();
  9972. $result = $this->generate_lp_folder($courseInfo);
  9973. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  9974. $relative_prefix = '../../';
  9975. $editorConfig = [
  9976. 'ToolbarSet' => 'LearningPathDocuments',
  9977. 'Width' => '100%',
  9978. 'Height' => '500',
  9979. 'FullPage' => true,
  9980. 'CreateDocumentDir' => $relative_prefix,
  9981. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  9982. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  9983. ];
  9984. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  9985. 'type' => 'document',
  9986. 'lp_id' => $this->lp_id
  9987. ]);
  9988. $form = new FormValidator('final_item', 'POST', $url);
  9989. $form->addText('title', get_lang('Title'));
  9990. $form->addButtonSave($buttonText);
  9991. $form->addHtml(
  9992. Display::return_message(
  9993. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  9994. 'normal',
  9995. false
  9996. )
  9997. );
  9998. $renderer = $form->defaultRenderer();
  9999. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10000. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10001. $form->addHidden('action', 'add_final_item');
  10002. $form->addHidden('path', Session::read('pathItem'));
  10003. $form->addHidden('previous', $this->get_last());
  10004. $form->setDefaults(['title' => $title, 'content_lp_certificate' => $content]);
  10005. if ($form->validate()) {
  10006. $values = $form->exportValues();
  10007. $lastItemId = $this->get_last();
  10008. if (!$finalItem) {
  10009. $documentId = $this->create_document(
  10010. $this->course_info,
  10011. $values['content_lp_certificate'],
  10012. $values['title']
  10013. );
  10014. $this->add_item(
  10015. 0,
  10016. $lastItemId,
  10017. 'final_item',
  10018. $documentId,
  10019. $values['title'],
  10020. ''
  10021. );
  10022. } else {
  10023. $this->edit_document($this->course_info);
  10024. }
  10025. }
  10026. return $form->returnForm();
  10027. }
  10028. /**
  10029. * Check if the current lp item is first, both, last or none from lp list
  10030. *
  10031. * @param int $currentItemId
  10032. * @return string
  10033. */
  10034. public function isFirstOrLastItem($currentItemId)
  10035. {
  10036. if ($this->debug > 0) {
  10037. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10038. }
  10039. $lpItemId = [];
  10040. $typeListNotToVerify = self::getChapterTypes();
  10041. // Using get_toc() function instead $this->items because returns the correct order of the items
  10042. foreach ($this->get_toc() as $item) {
  10043. if (!in_array($item['type'], $typeListNotToVerify)) {
  10044. $lpItemId[] = $item['id'];
  10045. }
  10046. }
  10047. $lastLpItemIndex = count($lpItemId) - 1;
  10048. $position = array_search($currentItemId, $lpItemId);
  10049. switch ($position) {
  10050. case 0:
  10051. if (!$lastLpItemIndex) {
  10052. $answer = 'both';
  10053. break;
  10054. }
  10055. $answer = 'first';
  10056. break;
  10057. case $lastLpItemIndex:
  10058. $answer = 'last';
  10059. break;
  10060. default:
  10061. $answer = 'none';
  10062. }
  10063. return $answer;
  10064. }
  10065. /**
  10066. * Get whether this is a learning path with the accumulated SCORM time or not
  10067. * @return int
  10068. */
  10069. public function getAccumulateScormTime()
  10070. {
  10071. return $this->accumulateScormTime;
  10072. }
  10073. /**
  10074. * Set whether this is a learning path with the accumulated SCORM time or not
  10075. * @param int $value (0 = false, 1 = true)
  10076. * @return bool Always returns true
  10077. */
  10078. public function setAccumulateScormTime($value)
  10079. {
  10080. if ($this->debug > 0) {
  10081. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10082. }
  10083. $this->accumulateScormTime = intval($value);
  10084. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  10085. $lp_id = $this->get_id();
  10086. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10087. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10088. Database::query($sql);
  10089. return true;
  10090. }
  10091. /**
  10092. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10093. * the new learning path tool.
  10094. *
  10095. * The function is a big switch on tool type.
  10096. * In each case, we query the corresponding table for information and build the link
  10097. * with that information.
  10098. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on previous work (display_addedresource_link_in_learnpath())
  10099. * @param int $course_id Course code
  10100. * @param int $learningPathId The learning path ID (in lp table)
  10101. * @param int $id_in_path the unique index in the items table
  10102. * @param int $lpViewId
  10103. * @return string
  10104. */
  10105. public static function rl_get_resource_link_for_learnpath($course_id, $learningPathId, $id_in_path, $lpViewId)
  10106. {
  10107. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10108. $course_info = api_get_course_info_by_id($course_id);
  10109. $course_id = $course_info['real_id'];
  10110. $course_code = $course_info['code'];
  10111. $session_id = api_get_session_id();
  10112. $learningPathId = intval($learningPathId);
  10113. $id_in_path = intval($id_in_path);
  10114. $lpViewId = intval($lpViewId);
  10115. $em = Database::getManager();
  10116. $sql = "SELECT * FROM $tbl_lp_item
  10117. WHERE
  10118. c_id = $course_id AND
  10119. lp_id = $learningPathId AND
  10120. id = $id_in_path
  10121. ";
  10122. $res_item = Database::query($sql);
  10123. if (Database::num_rows($res_item) < 1) {
  10124. return -1;
  10125. }
  10126. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10127. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10128. // Get the lp_item_view with the highest view_count.
  10129. $sql = "SELECT * FROM $item_view_table
  10130. WHERE
  10131. c_id = $course_id AND
  10132. lp_item_id = " . $row_item['id'] . " AND
  10133. lp_view_id = " . $lpViewId . "
  10134. ORDER BY view_count DESC";
  10135. $learnpathItemViewResult = Database::query($sql);
  10136. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10137. $learnpathItemViewId = 0;
  10138. if (isset($learnpathItemViewData)) {
  10139. $learnpathItemViewId = $learnpathItemViewData['id'];
  10140. }
  10141. $type = strtolower($row_item['item_type']);
  10142. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10143. $origin = 'learnpath';
  10144. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10145. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10146. $link = '';
  10147. switch ($type) {
  10148. case 'dir':
  10149. $link .= $main_dir_path . 'lp/blank.php';
  10150. case TOOL_CALENDAR_EVENT:
  10151. $link .= $main_dir_path.'calendar/agenda.php?origin='.$origin.'&agenda_id='.$id;
  10152. break;
  10153. case TOOL_ANNOUNCEMENT:
  10154. $link .= $main_dir_path.'announcements/announcements.php?origin='.$origin.'&ann_id='.$id;
  10155. break;
  10156. case TOOL_LINK:
  10157. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10158. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10159. $myrow = Database::fetch_array($result);
  10160. $thelink = $myrow["url"];
  10161. $link .= $thelink;
  10162. break;
  10163. case TOOL_QUIZ:
  10164. if (!empty($id)) {
  10165. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10166. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10167. $result= Database::query($sql);
  10168. $myrow = Database::fetch_array($result);
  10169. if ($row_item['title'] != '') {
  10170. $myrow['title'] = $row_item['title'];
  10171. }
  10172. $link .= $main_dir_path . 'exercise/overview.php?cidReq='.$course_code.'&session_id='.$session_id.'&lp_init=1&origin='.$origin.'&learnpath_item_view_id='.$learnpathItemViewId.'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id;
  10173. }
  10174. break;
  10175. case 'hotpotatoes': //lowercase because of strtolower above
  10176. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10177. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10178. $myrow = Database::fetch_array($result);
  10179. $path = $myrow['path'];
  10180. $link .= $main_dir_path . 'exercise/showinframes.php?file='.$path.'&origin='.$origin.'&cid='.$course_code.'&uid='.api_get_user_id().'' .
  10181. '&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId;
  10182. break;
  10183. case TOOL_FORUM:
  10184. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&origin=learnpath';
  10185. break;
  10186. case TOOL_THREAD: //forum post
  10187. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10188. if (!empty($id)) {
  10189. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10190. $result = Database::query($sql);
  10191. $myrow = Database::fetch_array($result);
  10192. $link .= $main_dir_path.'forum/viewthread.php?origin=learnpath&thread='.$id.'' .
  10193. '&forum='.$myrow['forum_id'].'&lp=true';
  10194. }
  10195. break;
  10196. case TOOL_POST:
  10197. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10198. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10199. $myrow = Database::fetch_array($result);
  10200. $title = $myrow['post_title'];
  10201. //$desc = $row_item['description'];
  10202. $posternom = $myrow['poster_name'];
  10203. $posttime = $myrow['post_date'];
  10204. $posttext = $myrow['post_text'];
  10205. $posttitle = $title;
  10206. $posttext = str_replace('"', "'", $posttext);
  10207. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'' .
  10208. '&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'' .
  10209. '&lp=true';
  10210. break;
  10211. case TOOL_DOCUMENT:
  10212. $document = $em
  10213. ->getRepository('ChamiloCourseBundle:CDocument')
  10214. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10215. if (!$document) {
  10216. break;
  10217. }
  10218. $documentPathInfo = pathinfo($document->getPath());
  10219. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10220. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10221. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10222. if ($showDirectUrl) {
  10223. $link = $main_course_path . 'document' . $document->getPath() . '?' . api_get_cidreq();
  10224. } else {
  10225. $link = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?' . http_build_query([
  10226. 'cidReq' => $course_code,
  10227. 'id' => $id,
  10228. 'origin' => 'learnpathitem'
  10229. ]);
  10230. }
  10231. $openmethod = 2;
  10232. $officedoc = false;
  10233. Session::write('openmethod',$openmethod);
  10234. Session::write('officedoc',$officedoc);
  10235. break;
  10236. case TOOL_LP_FINAL_ITEM:
  10237. $link .= api_get_path(WEB_CODE_PATH) . 'lp/lp_final_item.php?'.api_get_cidreq().'&id='.$id.'&lp_id='.$learningPathId;
  10238. break;
  10239. case 'assignments':
  10240. $link .= $main_dir_path.'work/work.php?origin='.$origin;
  10241. break;
  10242. case TOOL_DROPBOX:
  10243. $link .= $main_dir_path.'dropbox/index.php?origin=learnpath';
  10244. break;
  10245. case 'introduction_text': //DEPRECATED
  10246. $link .= '';
  10247. break;
  10248. case TOOL_COURSE_DESCRIPTION:
  10249. $link .= $main_dir_path.'course_description?origin=learnpath';
  10250. break;
  10251. case TOOL_GROUP:
  10252. $link .= $main_dir_path.'group/group.php?origin='.$origin;
  10253. break;
  10254. case TOOL_USER:
  10255. $link .= $main_dir_path.'user/user.php?origin='.$origin;
  10256. break;
  10257. case TOOL_STUDENTPUBLICATION:
  10258. $link .= $main_dir_path.'work/work_list.php?'.api_get_cidreq().'&id='.$row_item['path'].'&origin=learnpath';
  10259. break;
  10260. } //end switch
  10261. return $link;
  10262. }
  10263. /**
  10264. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10265. *
  10266. * @author Yannick Warnier <ywarnier@beeznest.org>
  10267. * @param string Course code
  10268. * @param string The tool type (using constants declared in main_api.lib.php)
  10269. * @param integer The resource ID
  10270. */
  10271. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10272. {
  10273. $_course = api_get_course_info($course_code);
  10274. $course_id = $_course['real_id'];
  10275. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10276. $learningPathId = intval($learningPathId);
  10277. $id_in_path = intval($id_in_path);
  10278. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10279. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10280. $res_item = Database::query($sql_item);
  10281. if (Database::num_rows($res_item) < 1) {
  10282. return '';
  10283. }
  10284. $row_item = Database::fetch_array($res_item);
  10285. $type = strtolower($row_item['item_type']);
  10286. $id = $row_item['ref'];
  10287. $output = '';
  10288. switch ($type) {
  10289. case TOOL_CALENDAR_EVENT:
  10290. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10291. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10292. $myrow = Database::fetch_array($result);
  10293. $output = $myrow['title'];
  10294. break;
  10295. case TOOL_ANNOUNCEMENT:
  10296. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10297. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10298. $myrow = Database::fetch_array($result);
  10299. $output = $myrow['title'];
  10300. break;
  10301. case TOOL_LINK:
  10302. // Doesn't take $target into account.
  10303. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10304. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10305. $myrow = Database::fetch_array($result);
  10306. $output = $myrow['title'];
  10307. break;
  10308. case TOOL_QUIZ:
  10309. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10310. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10311. $myrow = Database::fetch_array($result);
  10312. $output = $myrow['title'];
  10313. break;
  10314. case TOOL_FORUM:
  10315. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10316. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10317. $myrow = Database::fetch_array($result);
  10318. $output = $myrow['forum_name'];
  10319. break;
  10320. case TOOL_THREAD: //=topics
  10321. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10322. // Grabbing the title of the post.
  10323. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10324. $result_title = Database::query($sql_title);
  10325. $myrow_title = Database::fetch_array($result_title);
  10326. $output = $myrow_title['post_title'];
  10327. break;
  10328. case TOOL_POST:
  10329. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10330. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10331. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10332. $result = Database::query($sql);
  10333. $post = Database::fetch_array($result);
  10334. $output = $post['post_title'];
  10335. break;
  10336. case 'dir':
  10337. $title = $row_item['title'];
  10338. if (!empty($title)) {
  10339. $output = $title;
  10340. } else {
  10341. $output = '-';
  10342. }
  10343. break;
  10344. case TOOL_DOCUMENT:
  10345. $title = $row_item['title'];
  10346. if (!empty($title)) {
  10347. $output = $title;
  10348. } else {
  10349. $output = '-';
  10350. }
  10351. break;
  10352. case 'hotpotatoes':
  10353. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10354. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10355. $myrow = Database::fetch_array($result);
  10356. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10357. $last = count($pathname) - 1; // Making a correct name for the link.
  10358. $filename = $pathname[$last]; // Making a correct name for the link.
  10359. $image = choose_image($filename);
  10360. $ext = explode('.', $filename);
  10361. $ext = strtolower($ext[sizeof($ext) - 1]);
  10362. $myrow['path'] = rawurlencode($myrow['path']);
  10363. $in_frames = in_array($ext, array('htm', 'html', 'gif', 'jpg', 'jpeg', 'png'));
  10364. $output = $filename;
  10365. break;
  10366. }
  10367. return stripslashes($output);
  10368. }
  10369. }
  10370. if (!function_exists('trim_value')) {
  10371. function trim_value(& $value) {
  10372. $value = trim($value);
  10373. }
  10374. }