learnpath.class.php 472 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Finder\Finder;
  10. /**
  11. * Class learnpath
  12. * This class defines the parent attributes and methods for Chamilo learnpaths
  13. * and SCORM learnpaths. It is used by the scorm class.
  14. *
  15. * @package chamilo.learnpath
  16. * @author Yannick Warnier <ywarnier@beeznest.org>
  17. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  18. */
  19. class learnpath
  20. {
  21. public $attempt = 0; // The number for the current ID view.
  22. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  23. public $current; // Id of the current item the user is viewing.
  24. public $current_score; // The score of the current item.
  25. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  26. public $current_time_stop; // The time the user closed this resource.
  27. public $default_status = 'not attempted';
  28. public $encoding = 'UTF-8';
  29. public $error = '';
  30. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  31. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  32. public $index; // The index of the active learnpath_item in $ordered_items array.
  33. public $items = array();
  34. public $last; // item_id of last item viewed in the learning path.
  35. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  36. public $license; // Which license this course has been given - not used yet on 20060522.
  37. public $lp_id; // DB ID for this learnpath.
  38. public $lp_view_id; // DB ID for lp_view
  39. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  40. public $message = '';
  41. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  42. public $name; // Learnpath name (they generally have one).
  43. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  44. public $path = ''; // Path inside the scorm directory (if scorm).
  45. public $theme; // The current theme of the learning path.
  46. public $preview_image; // The current image of the learning path.
  47. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  48. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  49. public $prevent_reinit = 1;
  50. // Describes the mode of progress bar display.
  51. public $seriousgame_mode = 0;
  52. public $progress_bar_mode = '%';
  53. // Percentage progress as saved in the db.
  54. public $progress_db = '0';
  55. public $proximity; // Wether the content is distant or local or unknown.
  56. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  57. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  58. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  59. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  60. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  61. public $user_id; //ID of the user that is viewing/using the course
  62. public $update_queue = array();
  63. public $scorm_debug = 0;
  64. public $arrMenu = array(); // Array for the menu items.
  65. public $debug = 0; // Logging level.
  66. public $lp_session_id = 0;
  67. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  68. public $prerequisite = 0;
  69. public $use_max_score = 1; // 1 or 0
  70. public $subscribeUsers = 0; // Subscribe users or not
  71. public $created_on = '';
  72. public $modified_on = '';
  73. public $publicated_on = '';
  74. public $expired_on = '';
  75. public $ref = null;
  76. public $course_int_id;
  77. public $course_info = array();
  78. public $categoryId;
  79. /**
  80. * Constructor.
  81. * Needs a database handler, a course code and a learnpath id from the database.
  82. * Also builds the list of items into $this->items.
  83. * @param string $course Course code
  84. * @param integer $lp_id
  85. * @param integer $user_id
  86. * @return mixed True on success, false on error
  87. */
  88. public function __construct($course, $lp_id, $user_id)
  89. {
  90. $this->encoding = api_get_system_encoding();
  91. if ($this->debug > 0) {
  92. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  93. }
  94. if (empty($course)) {
  95. $this->error = 'Course code is empty';
  96. return false;
  97. } else {
  98. $course_info = api_get_course_info($course);
  99. if (!empty($course_info)) {
  100. $this->cc = $course_info['code'];
  101. $this->course_info = $course_info;
  102. $course_id = $course_info['real_id'];
  103. } else {
  104. $this->error = 'Course code does not exist in database.';
  105. return false;
  106. }
  107. }
  108. $this->set_course_int_id($course_id);
  109. // Check learnpath ID.
  110. if (empty($lp_id)) {
  111. $this->error = 'Learnpath ID is empty';
  112. return false;
  113. } else {
  114. // TODO: Make it flexible to use any course_code (still using env course code here).
  115. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  116. $lp_id = intval($lp_id);
  117. $sql = "SELECT * FROM $lp_table
  118. WHERE id = '$lp_id' AND c_id = $course_id";
  119. if ($this->debug > 2) {
  120. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  121. }
  122. $res = Database::query($sql);
  123. if (Database::num_rows($res) > 0) {
  124. $this->lp_id = $lp_id;
  125. $row = Database::fetch_array($res);
  126. $this->type = $row['lp_type'];
  127. $this->name = stripslashes($row['name']);
  128. $this->proximity = $row['content_local'];
  129. $this->theme = $row['theme'];
  130. $this->maker = $row['content_maker'];
  131. $this->prevent_reinit = $row['prevent_reinit'];
  132. $this->seriousgame_mode = $row['seriousgame_mode'];
  133. $this->license = $row['content_license'];
  134. $this->scorm_debug = $row['debug'];
  135. $this->js_lib = $row['js_lib'];
  136. $this->path = $row['path'];
  137. $this->preview_image = $row['preview_image'];
  138. $this->author = $row['author'];
  139. $this->hide_toc_frame = $row['hide_toc_frame'];
  140. $this->lp_session_id = $row['session_id'];
  141. $this->use_max_score = $row['use_max_score'];
  142. $this->subscribeUsers = $row['subscribe_users'];
  143. $this->created_on = $row['created_on'];
  144. $this->modified_on = $row['modified_on'];
  145. $this->ref = $row['ref'];
  146. $this->categoryId = $row['category_id'];
  147. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  148. if (!empty($row['publicated_on'])) {
  149. $this->publicated_on = $row['publicated_on'];
  150. }
  151. if (!empty($row['expired_on'])) {
  152. $this->expired_on = $row['expired_on'];
  153. }
  154. if ($this->type == 2) {
  155. if ($row['force_commit'] == 1) {
  156. $this->force_commit = true;
  157. }
  158. }
  159. $this->mode = $row['default_view_mod'];
  160. } else {
  161. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  162. return false;
  163. }
  164. }
  165. // Check user ID.
  166. if (empty($user_id)) {
  167. $this->error = 'User ID is empty';
  168. return false;
  169. } else {
  170. $user_info = api_get_user_info($user_id);
  171. if (!empty($user_info)) {
  172. $this->user_id = $user_info['user_id'];
  173. } else {
  174. $this->error = 'User ID does not exist in database ('.$sql.')';
  175. return false;
  176. }
  177. }
  178. // End of variables checking.
  179. $session_id = api_get_session_id();
  180. // Get the session condition for learning paths of the base + session.
  181. $session = api_get_session_condition($session_id);
  182. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  183. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  184. // Selecting by view_count descending allows to get the highest view_count first.
  185. $sql = "SELECT * FROM $lp_table
  186. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  187. ORDER BY view_count DESC";
  188. $res = Database::query($sql);
  189. if ($this->debug > 2) {
  190. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  191. }
  192. if (Database :: num_rows($res) > 0) {
  193. if ($this->debug > 2) {
  194. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  195. }
  196. $row = Database :: fetch_array($res);
  197. $this->attempt = $row['view_count'];
  198. $this->lp_view_id = $row['id'];
  199. $this->last_item_seen = $row['last_item'];
  200. $this->progress_db = $row['progress'];
  201. $this->lp_view_session_id = $row['session_id'];
  202. } else if (!api_is_invitee()) {
  203. if ($this->debug > 2) {
  204. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  205. }
  206. $this->attempt = 1;
  207. $params = [
  208. 'c_id' => $course_id,
  209. 'lp_id' => $lp_id,
  210. 'user_id' => $user_id,
  211. 'view_count' => 1,
  212. 'session_id' => $session_id,
  213. 'last_item' => 0
  214. ];
  215. Database::insert($lp_table, $params);
  216. $this->lp_view_id = Database::insert_id();
  217. if ($this->debug > 2) {
  218. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  219. }
  220. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  221. Database::query($sql);
  222. }
  223. // Initialise items.
  224. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  225. $sql = "SELECT * FROM $lp_item_table
  226. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  227. ORDER BY parent_item_id, display_order";
  228. $res = Database::query($sql);
  229. if ($this->debug > 2) {
  230. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  231. error_log('-- Start while--', 0);
  232. }
  233. $lp_item_id_list = array();
  234. while ($row = Database::fetch_array($res)) {
  235. $lp_item_id_list[] = $row['id'];
  236. switch ($this->type) {
  237. case 3: //aicc
  238. $oItem = new aiccItem('db', $row['id'], $course_id);
  239. if (is_object($oItem)) {
  240. $my_item_id = $oItem->get_id();
  241. $oItem->set_lp_view($this->lp_view_id, $course_id);
  242. $oItem->set_prevent_reinit($this->prevent_reinit);
  243. // Don't use reference here as the next loop will make the pointed object change.
  244. $this->items[$my_item_id] = $oItem;
  245. $this->refs_list[$oItem->ref] = $my_item_id;
  246. if ($this->debug > 2) {
  247. error_log(
  248. 'New LP - learnpath::__construct() - ' .
  249. 'aicc object with id ' . $my_item_id .
  250. ' set in items[]',
  251. 0
  252. );
  253. }
  254. }
  255. break;
  256. case 2:
  257. $oItem = new scormItem('db', $row['id'], $course_id);
  258. if (is_object($oItem)) {
  259. $my_item_id = $oItem->get_id();
  260. $oItem->set_lp_view($this->lp_view_id, $course_id);
  261. $oItem->set_prevent_reinit($this->prevent_reinit);
  262. // Don't use reference here as the next loop will make the pointed object change.
  263. $this->items[$my_item_id] = $oItem;
  264. $this->refs_list[$oItem->ref] = $my_item_id;
  265. if ($this->debug > 2) {
  266. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  267. }
  268. }
  269. break;
  270. case 1:
  271. default:
  272. if ($this->debug > 2) {
  273. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  274. }
  275. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  276. if ($this->debug > 2) {
  277. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  278. }
  279. if (is_object($oItem)) {
  280. $my_item_id = $oItem->get_id();
  281. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  282. $oItem->set_prevent_reinit($this->prevent_reinit);
  283. // Don't use reference here as the next loop will make the pointed object change.
  284. $this->items[$my_item_id] = $oItem;
  285. $this->refs_list[$my_item_id] = $my_item_id;
  286. if ($this->debug > 2) {
  287. error_log(
  288. 'New LP - learnpath::__construct() ' . __LINE__ .
  289. ' - object with id ' . $my_item_id . ' set in items[]',
  290. 0
  291. );
  292. }
  293. }
  294. break;
  295. }
  296. // Setting the object level with variable $this->items[$i][parent]
  297. foreach ($this->items as $itemLPObject) {
  298. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  299. $itemLPObject->level = $level;
  300. }
  301. // Setting the view in the item object.
  302. if (is_object($this->items[$row['id']])) {
  303. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  304. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  305. $this->items[$row['id']]->current_start_time = 0;
  306. $this->items[$row['id']]->current_stop_time = 0;
  307. }
  308. }
  309. }
  310. if ($this->debug > 2) {
  311. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  312. }
  313. if (!empty($lp_item_id_list)) {
  314. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  315. if (!empty($lp_item_id_list_to_string)) {
  316. // Get last viewing vars.
  317. $lp_item_view_table = Database:: get_course_table(
  318. TABLE_LP_ITEM_VIEW
  319. );
  320. // This query should only return one or zero result.
  321. $sql = "SELECT lp_item_id, status
  322. FROM $lp_item_view_table
  323. WHERE
  324. c_id = $course_id AND
  325. lp_view_id = ".$this->lp_view_id." AND
  326. lp_item_id IN ('".$lp_item_id_list_to_string."')
  327. ORDER BY view_count DESC ";
  328. if ($this->debug > 2) {
  329. error_log(
  330. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  331. 0
  332. );
  333. }
  334. $status_list = array();
  335. $res = Database::query($sql);
  336. while ($row = Database:: fetch_array($res)) {
  337. $status_list[$row['lp_item_id']] = $row['status'];
  338. }
  339. foreach ($lp_item_id_list as $item_id) {
  340. if (isset($status_list[$item_id])) {
  341. $status = $status_list[$item_id];
  342. if (is_object($this->items[$item_id])) {
  343. $this->items[$item_id]->set_status($status);
  344. if (empty($status)) {
  345. $this->items[$item_id]->set_status(
  346. $this->default_status
  347. );
  348. }
  349. }
  350. } else {
  351. if (!api_is_invitee()) {
  352. if (is_object($this->items[$item_id])) {
  353. $this->items[$item_id]->set_status(
  354. $this->default_status
  355. );
  356. }
  357. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  358. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  359. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  360. if ($this->debug > 2) {
  361. error_log(
  362. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  363. 0
  364. );
  365. }
  366. $this->items[$item_id]->set_lp_view(
  367. $this->lp_view_id,
  368. $course_id
  369. );
  370. Database::query($sql);
  371. $insertId = Database::insert_id();
  372. $sql = "UPDATE $lp_item_view_table SET id = iid
  373. WHERE iid = $insertId";
  374. Database::query($sql);
  375. }
  376. }
  377. }
  378. }
  379. }
  380. $this->ordered_items = self::get_flat_ordered_items_list(
  381. $this->get_id(),
  382. 0,
  383. $course_id
  384. );
  385. $this->max_ordered_items = 0;
  386. foreach ($this->ordered_items as $index => $dummy) {
  387. if ($index > $this->max_ordered_items && !empty($dummy)) {
  388. $this->max_ordered_items = $index;
  389. }
  390. }
  391. // TODO: Define the current item better.
  392. $this->first();
  393. if ($this->debug > 2) {
  394. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  395. }
  396. return true;
  397. }
  398. /**
  399. * @return string
  400. */
  401. public function getCourseCode()
  402. {
  403. return $this->cc;
  404. }
  405. /**
  406. * @return int
  407. */
  408. public function get_course_int_id()
  409. {
  410. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  411. }
  412. /**
  413. * @param $course_id
  414. * @return int
  415. */
  416. public function set_course_int_id($course_id)
  417. {
  418. return $this->course_int_id = intval($course_id);
  419. }
  420. /**
  421. * Get the depth level of LP item
  422. * @param $in_tab_items
  423. * @param $in_current_item_id
  424. * @return int
  425. */
  426. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  427. {
  428. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  429. if ($parent_item_id == 0) {
  430. return 0;
  431. } else {
  432. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  433. }
  434. }
  435. /**
  436. * Function rewritten based on old_add_item() from Yannick Warnier.
  437. * Due the fact that users can decide where the item should come, I had to overlook this function and
  438. * I found it better to rewrite it. Old function is still available.
  439. * Added also the possibility to add a description.
  440. *
  441. * @param int $parent
  442. * @param int $previous
  443. * @param string $type
  444. * @param int $id resource ID (ref)
  445. * @param string $title
  446. * @param string $description
  447. * @param int $prerequisites
  448. * @param int $max_time_allowed
  449. * @param int $userId
  450. *
  451. * @return int
  452. */
  453. public function add_item(
  454. $parent,
  455. $previous,
  456. $type = 'dir',
  457. $id,
  458. $title,
  459. $description,
  460. $prerequisites = 0,
  461. $max_time_allowed = 0,
  462. $userId = 0
  463. ) {
  464. $course_id = $this->course_info['real_id'];
  465. if ($this->debug > 0) {
  466. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  467. }
  468. if (empty($course_id)) {
  469. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  470. $this->course_info = api_get_course_info($this->cc);
  471. $course_id = $this->course_info['real_id'];
  472. }
  473. $userId = empty($userId) ? api_get_user_id() : $userId;
  474. $sessionId = api_get_session_id();
  475. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  476. $_course = $this->course_info;
  477. $parent = intval($parent);
  478. $previous = intval($previous);
  479. $id = intval($id);
  480. $max_time_allowed = htmlentities($max_time_allowed);
  481. if (empty($max_time_allowed)) {
  482. $max_time_allowed = 0;
  483. }
  484. $sql = "SELECT COUNT(id) AS num
  485. FROM $tbl_lp_item
  486. WHERE
  487. c_id = $course_id AND
  488. lp_id = " . $this->get_id() . " AND
  489. parent_item_id = " . $parent;
  490. $res_count = Database::query($sql);
  491. $row = Database :: fetch_array($res_count);
  492. $num = $row['num'];
  493. if ($num > 0) {
  494. if ($previous == 0) {
  495. $sql = "SELECT id, next_item_id, display_order
  496. FROM " . $tbl_lp_item . "
  497. WHERE
  498. c_id = $course_id AND
  499. lp_id = " . $this->get_id() . " AND
  500. parent_item_id = " . $parent . " AND
  501. previous_item_id = 0 OR
  502. previous_item_id=" . $parent;
  503. $result = Database::query($sql);
  504. $row = Database :: fetch_array($result);
  505. $tmp_previous = 0;
  506. $next = $row['id'];
  507. $display_order = 0;
  508. } else {
  509. $previous = (int) $previous;
  510. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  511. FROM $tbl_lp_item
  512. WHERE
  513. c_id = $course_id AND
  514. lp_id = " . $this->get_id() . " AND
  515. id = " . $previous;
  516. $result = Database::query($sql);
  517. $row = Database:: fetch_array($result);
  518. $tmp_previous = $row['id'];
  519. $next = $row['next_item_id'];
  520. $display_order = $row['display_order'];
  521. }
  522. } else {
  523. $tmp_previous = 0;
  524. $next = 0;
  525. $display_order = 0;
  526. }
  527. $id = intval($id);
  528. $typeCleaned = Database::escape_string($type);
  529. if ($type == 'quiz') {
  530. $sql = 'SELECT SUM(ponderation)
  531. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  532. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  533. ON
  534. quiz_question.id = quiz_rel_question.question_id AND
  535. quiz_question.c_id = quiz_rel_question.c_id
  536. WHERE
  537. quiz_rel_question.exercice_id = '.$id." AND
  538. quiz_question.c_id = $course_id AND
  539. quiz_rel_question.c_id = $course_id ";
  540. $rsQuiz = Database::query($sql);
  541. $max_score = Database :: result($rsQuiz, 0, 0);
  542. // Disabling the exercise if we add it inside a LP
  543. $exercise = new Exercise($course_id);
  544. $exercise->read($id);
  545. $exercise->disable();
  546. $exercise->save();
  547. } else {
  548. $max_score = 100;
  549. }
  550. $params = array(
  551. "c_id" => $course_id,
  552. "lp_id" => $this->get_id(),
  553. "item_type" => $typeCleaned,
  554. "ref" => '',
  555. "title" => $title,
  556. "description" => $description,
  557. "path" => $id,
  558. "max_score" => $max_score,
  559. "parent_item_id" => $parent,
  560. "previous_item_id" => $previous,
  561. "next_item_id" => intval($next),
  562. "display_order" => $display_order + 1,
  563. "prerequisite" => $prerequisites,
  564. "max_time_allowed" => $max_time_allowed,
  565. 'min_score' => 0,
  566. 'launch_data' => ''
  567. );
  568. if ($prerequisites != 0) {
  569. $params['prerequisite'] = $prerequisites;
  570. }
  571. $new_item_id = Database::insert($tbl_lp_item, $params);
  572. if ($this->debug > 2) {
  573. error_log('New LP - Inserting dir/chapter: ' . $new_item_id, 0);
  574. }
  575. if ($new_item_id) {
  576. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  577. Database::query($sql);
  578. // Update the item that should come after the new item.
  579. $sql = " UPDATE $tbl_lp_item SET
  580. previous_item_id = $new_item_id,
  581. next_item_id = $new_item_id,
  582. id = $new_item_id
  583. WHERE iid = $new_item_id";
  584. Database::query($sql);
  585. // Update all the items after the new item.
  586. $sql = "UPDATE " . $tbl_lp_item . "
  587. SET display_order = display_order + 1
  588. WHERE
  589. c_id = $course_id AND
  590. lp_id = " . $this->get_id() . " AND
  591. id <> " . $new_item_id . " AND
  592. parent_item_id = " . $parent . " AND
  593. display_order > " . $display_order;
  594. Database::query($sql);
  595. // Update the item that should come after the new item.
  596. $sql = "UPDATE " . $tbl_lp_item . "
  597. SET ref = " . $new_item_id . "
  598. WHERE c_id = $course_id AND id = " . $new_item_id;
  599. Database::query($sql);
  600. // Upload audio.
  601. if (!empty($_FILES['mp3']['name'])) {
  602. // Create the audio folder if it does not exist yet.
  603. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  604. if (!is_dir($filepath . 'audio')) {
  605. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  606. $audio_id = add_document(
  607. $_course,
  608. '/audio',
  609. 'folder',
  610. 0,
  611. 'audio',
  612. '',
  613. 0,
  614. true,
  615. null,
  616. $sessionId,
  617. $userId
  618. );
  619. api_item_property_update(
  620. $_course,
  621. TOOL_DOCUMENT,
  622. $audio_id,
  623. 'FolderCreated',
  624. $userId,
  625. null,
  626. null,
  627. null,
  628. null,
  629. $sessionId
  630. );
  631. api_item_property_update(
  632. $_course,
  633. TOOL_DOCUMENT,
  634. $audio_id,
  635. 'invisible',
  636. $userId,
  637. null,
  638. null,
  639. null,
  640. null,
  641. $sessionId
  642. );
  643. }
  644. $file_path = handle_uploaded_document(
  645. $_course,
  646. $_FILES['mp3'],
  647. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  648. '/audio',
  649. $userId,
  650. '',
  651. '',
  652. '',
  653. '',
  654. false
  655. );
  656. // Getting the filename only.
  657. $file_components = explode('/', $file_path);
  658. $file = $file_components[count($file_components) - 1];
  659. // Store the mp3 file in the lp_item table.
  660. $sql = "UPDATE $tbl_lp_item SET
  661. audio = '" . Database::escape_string($file) . "'
  662. WHERE id = '" . intval($new_item_id) . "'";
  663. Database::query($sql);
  664. }
  665. }
  666. return $new_item_id;
  667. }
  668. /**
  669. * Static admin function allowing addition of a learnpath to a course.
  670. * @param string Course code
  671. * @param string Learnpath name
  672. * @param string Learnpath description string, if provided
  673. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  674. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  675. * @param string Zip file containing the learnpath or directory containing the learnpath
  676. * @return integer The new learnpath ID on success, 0 on failure
  677. */
  678. public static function add_lp(
  679. $courseCode,
  680. $name,
  681. $description = '',
  682. $learnpath = 'guess',
  683. $origin = 'zip',
  684. $zipname = '',
  685. $publicated_on = '',
  686. $expired_on = '',
  687. $categoryId = 0,
  688. $userId = 0
  689. ) {
  690. global $charset;
  691. if (!empty($courseCode)) {
  692. $courseInfo = api_get_course_info($courseCode);
  693. $course_id = $courseInfo['real_id'];
  694. } else {
  695. $course_id = api_get_course_int_id();
  696. $courseInfo = api_get_course_info();
  697. }
  698. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  699. // Check course code exists.
  700. // Check lp_name doesn't exist, otherwise append something.
  701. $i = 0;
  702. $name = Database::escape_string($name);
  703. $categoryId = intval($categoryId);
  704. // Session id.
  705. $session_id = api_get_session_id();
  706. $userId = empty($userId) ? api_get_user_id() : $userId;
  707. $check_name = "SELECT * FROM $tbl_lp
  708. WHERE c_id = $course_id AND name = '$name'";
  709. $res_name = Database::query($check_name);
  710. if (empty($publicated_on)) {
  711. //by default the publication date is the same that the creation date
  712. //The behaviour above was changed due BT#2800
  713. // global $_custom;
  714. // if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  715. // $publicated_on = null;
  716. // } else {
  717. // $publicated_on = null;
  718. // }
  719. $publicated_on = null;
  720. } else {
  721. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  722. }
  723. if (empty($expired_on)) {
  724. $expired_on = null;
  725. } else {
  726. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  727. }
  728. while (Database :: num_rows($res_name)) {
  729. // There is already one such name, update the current one a bit.
  730. $i++;
  731. $name = $name . ' - ' . $i;
  732. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  733. $res_name = Database::query($check_name);
  734. }
  735. // New name does not exist yet; keep it.
  736. // Escape description.
  737. // Kevin: added htmlentities().
  738. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  739. $type = 1;
  740. switch ($learnpath) {
  741. case 'guess':
  742. break;
  743. case 'dokeos':
  744. case 'chamilo':
  745. $type = 1;
  746. break;
  747. case 'aicc':
  748. break;
  749. }
  750. switch ($origin) {
  751. case 'zip':
  752. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  753. break;
  754. case 'manual':
  755. default:
  756. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  757. $res_max = Database::query($get_max);
  758. if (Database :: num_rows($res_max) < 1) {
  759. $dsp = 1;
  760. } else {
  761. $row = Database :: fetch_array($res_max);
  762. $dsp = $row[0] + 1;
  763. }
  764. $params = [
  765. 'c_id' => $course_id,
  766. 'lp_type' => $type,
  767. 'name' => $name,
  768. 'description' => $description,
  769. 'path' => '',
  770. 'default_view_mod' => 'embedded',
  771. 'default_encoding' => 'UTF-8',
  772. 'display_order' => $dsp,
  773. 'content_maker' => 'Chamilo',
  774. 'content_local' => 'local',
  775. 'js_lib' => '',
  776. 'session_id' => $session_id,
  777. 'created_on' => api_get_utc_datetime(),
  778. 'modified_on' => api_get_utc_datetime(),
  779. 'publicated_on' => $publicated_on,
  780. 'expired_on' => $expired_on,
  781. 'category_id' => $categoryId,
  782. 'force_commit' => 0,
  783. 'content_license' => '',
  784. 'debug' => 0,
  785. 'theme' => '',
  786. 'preview_image' => '',
  787. 'author' => '',
  788. 'prerequisite' => 0,
  789. 'hide_toc_frame' => 0,
  790. 'seriousgame_mode' => 0,
  791. 'autolaunch' => 0,
  792. 'max_attempts' => 0,
  793. 'subscribe_users' => 0,
  794. 'accumulate_scorm_time' => 1
  795. ];
  796. $id = Database::insert($tbl_lp, $params);
  797. if ($id > 0) {
  798. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  799. Database::query($sql);
  800. // Insert into item_property.
  801. api_item_property_update(
  802. $courseInfo,
  803. TOOL_LEARNPATH,
  804. $id,
  805. 'LearnpathAdded',
  806. $userId
  807. );
  808. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  809. return $id;
  810. }
  811. break;
  812. }
  813. }
  814. /**
  815. * Auto completes the parents of an item in case it's been completed or passed
  816. * @param integer $item Optional ID of the item from which to look for parents
  817. */
  818. public function autocomplete_parents($item)
  819. {
  820. $debug = $this->debug;
  821. if ($debug) {
  822. error_log('Learnpath::autocomplete_parents()', 0);
  823. }
  824. if (empty($item)) {
  825. $item = $this->current;
  826. }
  827. $currentItem = $this->getItem($item);
  828. if ($currentItem) {
  829. $parent_id = $currentItem->get_parent();
  830. $parent = $this->getItem($parent_id);
  831. if ($parent) {
  832. // if $item points to an object and there is a parent.
  833. if ($debug) {
  834. error_log(
  835. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  836. 0
  837. );
  838. }
  839. // New experiment including failed and browsed in completed status.
  840. //$current_status = $currentItem->get_status();
  841. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  842. // Fixes chapter auto complete
  843. if (true) {
  844. // If the current item is completed or passes or succeeded.
  845. $updateParentStatus = true;
  846. if ($debug) {
  847. error_log('Status of current item is alright', 0);
  848. }
  849. foreach ($parent->get_children() as $childItemId) {
  850. $childItem = $this->getItem($childItemId);
  851. // If children was not set try to get the info
  852. if (empty($childItem->db_item_view_id)) {
  853. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  854. }
  855. // Check all his brothers (parent's children) for completion status.
  856. if ($childItemId != $item) {
  857. if ($debug) {
  858. error_log(
  859. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  860. 0
  861. );
  862. }
  863. // Trying completing parents of failed and browsed items as well.
  864. if ($childItem->status_is(
  865. array(
  866. 'completed',
  867. 'passed',
  868. 'succeeded',
  869. 'browsed',
  870. 'failed'
  871. )
  872. )
  873. ) {
  874. // Keep completion status to true.
  875. continue;
  876. } else {
  877. if ($debug > 2) {
  878. error_log(
  879. '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,
  880. 0
  881. );
  882. }
  883. $updateParentStatus = false;
  884. break;
  885. }
  886. }
  887. }
  888. if ($updateParentStatus) {
  889. // If all the children were completed:
  890. $parent->set_status('completed');
  891. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  892. // Force the status to "completed"
  893. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  894. $this->update_queue[$parent->get_id()] = 'completed';
  895. if ($debug) {
  896. error_log(
  897. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  898. print_r($this->update_queue, 1),
  899. 0
  900. );
  901. }
  902. // Recursive call.
  903. $this->autocomplete_parents($parent->get_id());
  904. }
  905. }
  906. } else {
  907. if ($debug) {
  908. error_log("Parent #$parent_id does not exists");
  909. }
  910. }
  911. } else {
  912. if ($debug) {
  913. error_log("#$item is an item that doesn't have parents");
  914. }
  915. }
  916. }
  917. /**
  918. * Auto saves the current results into the database for the whole learnpath
  919. * @todo: Add save operations for the learnpath itself.
  920. */
  921. public function autosave()
  922. {
  923. if ($this->debug > 0) {
  924. error_log('New LP - In learnpath::autosave()', 0);
  925. }
  926. }
  927. /**
  928. * Closes the current resource
  929. *
  930. * Stops the timer
  931. * Saves into the database if required
  932. * Clears the current resource data from this object
  933. * @return boolean True on success, false on failure
  934. */
  935. public function close()
  936. {
  937. if ($this->debug > 0) {
  938. error_log('New LP - In learnpath::close()', 0);
  939. }
  940. if (empty ($this->lp_id)) {
  941. $this->error = 'Trying to close this learnpath but no ID is set';
  942. return false;
  943. }
  944. $this->current_time_stop = time();
  945. $this->ordered_items = array();
  946. $this->index = 0;
  947. unset($this->lp_id);
  948. //unset other stuff
  949. return true;
  950. }
  951. /**
  952. * Static admin function allowing removal of a learnpath
  953. * @param array $courseInfo
  954. * @param integer Learnpath ID
  955. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  956. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  957. */
  958. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  959. {
  960. $course_id = api_get_course_int_id();
  961. if (!empty($courseInfo)) {
  962. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  963. }
  964. // TODO: Implement a way of getting this to work when the current object is not set.
  965. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  966. // If an ID is specifically given and the current LP is not the same, prevent delete.
  967. if (!empty ($id) && ($id != $this->lp_id)) {
  968. return false;
  969. }
  970. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  971. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  972. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  973. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  974. // Delete lp item id.
  975. foreach ($this->items as $id => $dummy) {
  976. $sql = "DELETE FROM $lp_item_view
  977. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  978. Database::query($sql);
  979. }
  980. // Proposed by Christophe (nickname: clefevre)
  981. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  982. Database::query($sql);
  983. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  984. Database::query($sql);
  985. self::toggle_publish($this->lp_id, 'i');
  986. if ($this->type == 2 || $this->type == 3) {
  987. // This is a scorm learning path, delete the files as well.
  988. $sql = "SELECT path FROM $lp
  989. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  990. $res = Database::query($sql);
  991. if (Database :: num_rows($res) > 0) {
  992. $row = Database :: fetch_array($res);
  993. $path = $row['path'];
  994. $sql = "SELECT id FROM $lp
  995. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  996. $res = Database::query($sql);
  997. if (Database :: num_rows($res) > 0) {
  998. // Another learning path uses this directory, so don't delete it.
  999. if ($this->debug > 2) {
  1000. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  1001. }
  1002. } else {
  1003. // No other LP uses that directory, delete it.
  1004. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  1005. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  1006. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  1007. if ($this->debug > 2) {
  1008. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1009. }
  1010. // Proposed by Christophe (clefevre).
  1011. if (strcmp(substr($path, -2), "/.") == 0) {
  1012. $path = substr($path, 0, -1); // Remove "." at the end.
  1013. }
  1014. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1015. rmdirr($course_scorm_dir . $path);
  1016. }
  1017. }
  1018. }
  1019. }
  1020. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  1021. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1022. // Delete tools
  1023. $sql = "DELETE FROM $tbl_tool
  1024. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1025. Database::query($sql);
  1026. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1027. Database::query($sql);
  1028. // Updates the display order of all lps.
  1029. $this->update_display_order();
  1030. api_item_property_update(
  1031. api_get_course_info(),
  1032. TOOL_LEARNPATH,
  1033. $this->lp_id,
  1034. 'delete',
  1035. api_get_user_id()
  1036. );
  1037. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1038. api_get_course_id(),
  1039. 4,
  1040. $id,
  1041. api_get_session_id()
  1042. );
  1043. if ($link_info !== false) {
  1044. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1045. }
  1046. if (api_get_setting('search_enabled') == 'true') {
  1047. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1048. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1049. }
  1050. }
  1051. /**
  1052. * Removes all the children of one item - dangerous!
  1053. * @param integer $id Element ID of which children have to be removed
  1054. * @return integer Total number of children removed
  1055. */
  1056. public function delete_children_items($id)
  1057. {
  1058. $course_id = $this->course_info['real_id'];
  1059. if ($this->debug > 0) {
  1060. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1061. }
  1062. $num = 0;
  1063. if (empty ($id) || $id != strval(intval($id))) {
  1064. return false;
  1065. }
  1066. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1067. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1068. $res = Database::query($sql);
  1069. while ($row = Database :: fetch_array($res)) {
  1070. $num += $this->delete_children_items($row['id']);
  1071. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1072. Database::query($sql_del);
  1073. $num++;
  1074. }
  1075. return $num;
  1076. }
  1077. /**
  1078. * Removes an item from the current learnpath
  1079. * @param integer $id Elem ID (0 if first)
  1080. * @param integer $remove Whether to remove the resource/data from the
  1081. * system or leave it (default: 'keep', others 'remove')
  1082. * @return integer Number of elements moved
  1083. * @todo implement resource removal
  1084. */
  1085. public function delete_item($id, $remove = 'keep')
  1086. {
  1087. $course_id = api_get_course_int_id();
  1088. if ($this->debug > 0) {
  1089. error_log('New LP - In learnpath::delete_item()', 0);
  1090. }
  1091. // TODO: Implement the resource removal.
  1092. if (empty ($id) || $id != strval(intval($id))) {
  1093. return false;
  1094. }
  1095. // First select item to get previous, next, and display order.
  1096. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1097. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1098. $res_sel = Database::query($sql_sel);
  1099. if (Database :: num_rows($res_sel) < 1) {
  1100. return false;
  1101. }
  1102. $row = Database :: fetch_array($res_sel);
  1103. $previous = $row['previous_item_id'];
  1104. $next = $row['next_item_id'];
  1105. $display = $row['display_order'];
  1106. $parent = $row['parent_item_id'];
  1107. $lp = $row['lp_id'];
  1108. // Delete children items.
  1109. $num = $this->delete_children_items($id);
  1110. if ($this->debug > 2) {
  1111. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1112. }
  1113. // Now delete the item.
  1114. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1115. if ($this->debug > 2) {
  1116. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1117. }
  1118. Database::query($sql_del);
  1119. // Now update surrounding items.
  1120. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1121. WHERE c_id = ".$course_id." AND id = $previous";
  1122. Database::query($sql_upd);
  1123. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1124. WHERE c_id = ".$course_id." AND id = $next";
  1125. Database::query($sql_upd);
  1126. // Now update all following items with new display order.
  1127. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1128. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1129. Database::query($sql_all);
  1130. //Removing prerequisites since the item will not longer exist
  1131. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1132. Database::query($sql_all);
  1133. // Remove from search engine if enabled.
  1134. if (api_get_setting('search_enabled') == 'true') {
  1135. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1136. $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';
  1137. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1138. $res = Database::query($sql);
  1139. if (Database :: num_rows($res) > 0) {
  1140. $row2 = Database :: fetch_array($res);
  1141. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1142. $di = new ChamiloIndexer();
  1143. $di->remove_document((int) $row2['search_did']);
  1144. }
  1145. $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';
  1146. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1147. Database::query($sql);
  1148. }
  1149. }
  1150. /**
  1151. * Updates an item's content in place
  1152. * @param integer $id Element ID
  1153. * @param integer $parent Parent item ID
  1154. * @param integer $previous Previous item ID
  1155. * @param string $title Item title
  1156. * @param string $description Item description
  1157. * @param string $prerequisites Prerequisites (optional)
  1158. * @param array $audio The array resulting of the $_FILES[mp3] element
  1159. * @param int $max_time_allowed
  1160. * @param string $url
  1161. * @return boolean True on success, false on error
  1162. */
  1163. public function edit_item(
  1164. $id,
  1165. $parent,
  1166. $previous,
  1167. $title,
  1168. $description,
  1169. $prerequisites = '0',
  1170. $audio = array(),
  1171. $max_time_allowed = 0,
  1172. $url = ''
  1173. ) {
  1174. $course_id = api_get_course_int_id();
  1175. $_course = api_get_course_info();
  1176. if ($this->debug > 0) {
  1177. error_log('New LP - In learnpath::edit_item()', 0);
  1178. }
  1179. if (empty ($max_time_allowed)) {
  1180. $max_time_allowed = 0;
  1181. }
  1182. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1183. return false;
  1184. }
  1185. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1186. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1187. $res_select = Database::query($sql_select);
  1188. $row_select = Database :: fetch_array($res_select);
  1189. $audio_update_sql = '';
  1190. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1191. // Create the audio folder if it does not exist yet.
  1192. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1193. if (!is_dir($filepath . 'audio')) {
  1194. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1195. $audio_id = add_document(
  1196. $_course,
  1197. '/audio',
  1198. 'folder',
  1199. 0,
  1200. 'audio'
  1201. );
  1202. api_item_property_update(
  1203. $_course,
  1204. TOOL_DOCUMENT,
  1205. $audio_id,
  1206. 'FolderCreated',
  1207. api_get_user_id(),
  1208. null,
  1209. null,
  1210. null,
  1211. null,
  1212. api_get_session_id()
  1213. );
  1214. api_item_property_update(
  1215. $_course,
  1216. TOOL_DOCUMENT,
  1217. $audio_id,
  1218. 'invisible',
  1219. api_get_user_id(),
  1220. null,
  1221. null,
  1222. null,
  1223. null,
  1224. api_get_session_id()
  1225. );
  1226. }
  1227. // Upload file in documents.
  1228. $pi = pathinfo($audio['name']);
  1229. if ($pi['extension'] == 'mp3') {
  1230. $c_det = api_get_course_info($this->cc);
  1231. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1232. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1233. $path = substr($path, 7);
  1234. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1235. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1236. }
  1237. }
  1238. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1239. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1240. // TODO: htmlspecialchars to be checked for encoding related problems.
  1241. if ($same_parent && $same_previous) {
  1242. // Only update title and description.
  1243. $sql = "UPDATE " . $tbl_lp_item . "
  1244. SET title = '" . Database::escape_string($title) . "',
  1245. prerequisite = '" . $prerequisites . "',
  1246. description = '" . Database::escape_string($description) . "'
  1247. " . $audio_update_sql . ",
  1248. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1249. WHERE c_id = ".$course_id." AND id = " . $id;
  1250. Database::query($sql);
  1251. } else {
  1252. $old_parent = $row_select['parent_item_id'];
  1253. $old_previous = $row_select['previous_item_id'];
  1254. $old_next = $row_select['next_item_id'];
  1255. $old_order = $row_select['display_order'];
  1256. $old_prerequisite = $row_select['prerequisite'];
  1257. $old_max_time_allowed = $row_select['max_time_allowed'];
  1258. /* BEGIN -- virtually remove the current item id */
  1259. /* for the next and previous item it is like the current item doesn't exist anymore */
  1260. if ($old_previous != 0) {
  1261. // Next
  1262. $sql = "UPDATE " . $tbl_lp_item . "
  1263. SET next_item_id = " . $old_next . "
  1264. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1265. Database::query($sql);
  1266. }
  1267. if ($old_next != 0) {
  1268. // Previous
  1269. $sql = "UPDATE " . $tbl_lp_item . "
  1270. SET previous_item_id = " . $old_previous . "
  1271. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1272. Database::query($sql);
  1273. }
  1274. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1275. $sql = "UPDATE " . $tbl_lp_item . "
  1276. SET display_order = display_order - 1
  1277. WHERE
  1278. c_id = ".$course_id." AND
  1279. display_order > " . $old_order . " AND
  1280. lp_id = " . $this->lp_id . " AND
  1281. parent_item_id = " . $old_parent;
  1282. Database::query($sql);
  1283. /* END -- virtually remove the current item id */
  1284. /* BEGIN -- update the current item id to his new location */
  1285. if ($previous == 0) {
  1286. // Select the data of the item that should come after the current item.
  1287. $sql = "SELECT id, display_order
  1288. FROM " . $tbl_lp_item . "
  1289. WHERE
  1290. c_id = ".$course_id." AND
  1291. lp_id = " . $this->lp_id . " AND
  1292. parent_item_id = " . $parent . " AND
  1293. previous_item_id = " . $previous;
  1294. $res_select_old = Database::query($sql);
  1295. $row_select_old = Database::fetch_array($res_select_old);
  1296. // If the new parent didn't have children before.
  1297. if (Database :: num_rows($res_select_old) == 0) {
  1298. $new_next = 0;
  1299. $new_order = 1;
  1300. } else {
  1301. $new_next = $row_select_old['id'];
  1302. $new_order = $row_select_old['display_order'];
  1303. }
  1304. } else {
  1305. // Select the data of the item that should come before the current item.
  1306. $sql = "SELECT next_item_id, display_order
  1307. FROM " . $tbl_lp_item . "
  1308. WHERE c_id = ".$course_id." AND id = " . $previous;
  1309. $res_select_old = Database::query($sql);
  1310. $row_select_old = Database :: fetch_array($res_select_old);
  1311. $new_next = $row_select_old['next_item_id'];
  1312. $new_order = $row_select_old['display_order'] + 1;
  1313. }
  1314. // TODO: htmlspecialchars to be checked for encoding related problems.
  1315. // Update the current item with the new data.
  1316. $sql = "UPDATE " . $tbl_lp_item . "
  1317. SET
  1318. title = '" . Database::escape_string($title) . "',
  1319. description = '" . Database::escape_string($description) . "',
  1320. parent_item_id = " . $parent . ",
  1321. previous_item_id = " . $previous . ",
  1322. next_item_id = " . $new_next . ",
  1323. display_order = " . $new_order . "
  1324. " . $audio_update_sql . "
  1325. WHERE c_id = ".$course_id." AND id = " . $id;
  1326. Database::query($sql);
  1327. if ($previous != 0) {
  1328. // Update the previous item's next_item_id.
  1329. $sql = "UPDATE " . $tbl_lp_item . "
  1330. SET next_item_id = " . $id . "
  1331. WHERE c_id = ".$course_id." AND id = " . $previous;
  1332. Database::query($sql);
  1333. }
  1334. if ($new_next != 0) {
  1335. // Update the next item's previous_item_id.
  1336. $sql = "UPDATE " . $tbl_lp_item . "
  1337. SET previous_item_id = " . $id . "
  1338. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1339. Database::query($sql);
  1340. }
  1341. if ($old_prerequisite != $prerequisites) {
  1342. $sql = "UPDATE " . $tbl_lp_item . "
  1343. SET prerequisite = '" . $prerequisites . "'
  1344. WHERE c_id = ".$course_id." AND id = " . $id;
  1345. Database::query($sql);
  1346. }
  1347. if ($old_max_time_allowed != $max_time_allowed) {
  1348. // update max time allowed
  1349. $sql = "UPDATE " . $tbl_lp_item . "
  1350. SET max_time_allowed = " . $max_time_allowed . "
  1351. WHERE c_id = ".$course_id." AND id = " . $id;
  1352. Database::query($sql);
  1353. }
  1354. // Update all the items with the same or a bigger display_order than the current item.
  1355. $sql = "UPDATE " . $tbl_lp_item . "
  1356. SET display_order = display_order + 1
  1357. WHERE
  1358. c_id = ".$course_id." AND
  1359. lp_id = " . $this->get_id() . " AND
  1360. id <> " . $id . " AND
  1361. parent_item_id = " . $parent . " AND
  1362. display_order >= " . $new_order;
  1363. Database::query($sql);
  1364. }
  1365. if ($row_select['item_type'] == 'link') {
  1366. $link = new Link();
  1367. $linkId = $row_select['path'];
  1368. $link->updateLink($linkId, $url);
  1369. }
  1370. }
  1371. /**
  1372. * Updates an item's prereq in place
  1373. * @param integer $id Element ID
  1374. * @param string $prerequisite_id Prerequisite Element ID
  1375. * @param int $mastery_score Prerequisite min score
  1376. * @param int $max_score Prerequisite max score
  1377. *
  1378. * @return boolean True on success, false on error
  1379. */
  1380. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1381. {
  1382. $course_id = api_get_course_int_id();
  1383. if ($this->debug > 0) {
  1384. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1385. }
  1386. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1387. return false;
  1388. }
  1389. $prerequisite_id = intval($prerequisite_id);
  1390. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1391. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1392. $mastery_score = 0;
  1393. }
  1394. if (!is_numeric($max_score) || $max_score < 0) {
  1395. $max_score = 100;
  1396. }
  1397. /*if ($mastery_score > $max_score) {
  1398. $max_score = $mastery_score;
  1399. }*/
  1400. if (!is_numeric($prerequisite_id)) {
  1401. $prerequisite_id = 'NULL';
  1402. }
  1403. $mastery_score = floatval($mastery_score);
  1404. $max_score = floatval($max_score);
  1405. $sql = " UPDATE $tbl_lp_item
  1406. SET
  1407. prerequisite = $prerequisite_id ,
  1408. prerequisite_min_score = $mastery_score ,
  1409. prerequisite_max_score = $max_score
  1410. WHERE c_id = $course_id AND id = $id";
  1411. Database::query($sql);
  1412. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1413. // Will this be enough to ensure unicity?
  1414. /*$sql = " UPDATE $tbl_lp_item
  1415. SET mastery_score = $mastery_score
  1416. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1417. Database::query($sql);*/
  1418. }
  1419. // TODO: Update the item object (can be ignored for now because refreshed).
  1420. return true;
  1421. }
  1422. /**
  1423. * Static admin function exporting a learnpath into a zip file
  1424. * @param string Export type (scorm, zip, cd)
  1425. * @param string Course code
  1426. * @param integer Learnpath ID
  1427. * @param string Zip file name
  1428. * @return string Zip file path (or false on error)
  1429. */
  1430. public function export_lp($type, $course, $id, $zipname)
  1431. {
  1432. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1433. return false;
  1434. }
  1435. $url = '';
  1436. switch ($type) {
  1437. case 'scorm':
  1438. break;
  1439. case 'zip':
  1440. break;
  1441. case 'cdrom':
  1442. break;
  1443. }
  1444. return $url;
  1445. }
  1446. /**
  1447. * Gets all the chapters belonging to the same parent as the item/chapter given
  1448. * Can also be called as abstract method
  1449. * @param integer Item ID
  1450. * @return array A list of all the "brother items" (or an empty array on failure)
  1451. */
  1452. public function getSiblingDirectories($id)
  1453. {
  1454. $course_id = api_get_course_int_id();
  1455. if ($this->debug > 0) {
  1456. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1457. }
  1458. if (empty($id) || $id != strval(intval($id))) {
  1459. return array ();
  1460. }
  1461. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1462. $sql_parent = "SELECT * FROM $lp_item
  1463. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1464. $res_parent = Database::query($sql_parent);
  1465. if (Database :: num_rows($res_parent) > 0) {
  1466. $row_parent = Database :: fetch_array($res_parent);
  1467. $parent = $row_parent['parent_item_id'];
  1468. $sql = "SELECT * FROM $lp_item
  1469. WHERE
  1470. c_id = ".$course_id." AND
  1471. parent_item_id = $parent AND
  1472. id = $id AND
  1473. item_type='dir'
  1474. ORDER BY display_order";
  1475. $res_bros = Database::query($sql);
  1476. $list = array();
  1477. while ($row_bro = Database :: fetch_array($res_bros)) {
  1478. $list[] = $row_bro;
  1479. }
  1480. return $list;
  1481. }
  1482. return array();
  1483. }
  1484. /**
  1485. * Gets all the items belonging to the same parent as the item given
  1486. * Can also be called as abstract method
  1487. * @param integer Item ID
  1488. * @return array A list of all the "brother items" (or an empty array on failure)
  1489. */
  1490. public function get_brother_items($id)
  1491. {
  1492. $course_id = api_get_course_int_id();
  1493. if ($this->debug > 0) {
  1494. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1495. }
  1496. if (empty ($id) || $id != strval(intval($id))) {
  1497. return array();
  1498. }
  1499. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1500. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1501. $res_parent = Database::query($sql_parent);
  1502. if (Database :: num_rows($res_parent) > 0) {
  1503. $row_parent = Database :: fetch_array($res_parent);
  1504. $parent = $row_parent['parent_item_id'];
  1505. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1506. ORDER BY display_order";
  1507. $res_bros = Database::query($sql_bros);
  1508. $list = array ();
  1509. while ($row_bro = Database :: fetch_array($res_bros)) {
  1510. $list[] = $row_bro;
  1511. }
  1512. return $list;
  1513. }
  1514. return array ();
  1515. }
  1516. /**
  1517. * Get the specific prefix index terms of this learning path
  1518. * @param string $prefix
  1519. * @return array Array of terms
  1520. */
  1521. public function get_common_index_terms_by_prefix($prefix)
  1522. {
  1523. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1524. $terms = get_specific_field_values_list_by_prefix(
  1525. $prefix,
  1526. $this->cc,
  1527. TOOL_LEARNPATH,
  1528. $this->lp_id
  1529. );
  1530. $prefix_terms = array();
  1531. if (!empty($terms)) {
  1532. foreach ($terms as $term) {
  1533. $prefix_terms[] = $term['value'];
  1534. }
  1535. }
  1536. return $prefix_terms;
  1537. }
  1538. /**
  1539. * Gets the number of items currently completed
  1540. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1541. * @return integer The number of items currently completed
  1542. */
  1543. public function get_complete_items_count($failedStatusException = false)
  1544. {
  1545. if ($this->debug > 0) {
  1546. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1547. }
  1548. $i = 0;
  1549. $completedStatusList = array(
  1550. 'completed',
  1551. 'passed',
  1552. 'succeeded',
  1553. 'browsed'
  1554. );
  1555. if (!$failedStatusException) {
  1556. $completedStatusList[] = 'failed';
  1557. }
  1558. foreach ($this->items as $id => $dummy) {
  1559. // Trying failed and browsed considered "progressed" as well.
  1560. if ($this->items[$id]->status_is($completedStatusList) &&
  1561. $this->items[$id]->get_type() != 'dir'
  1562. ) {
  1563. $i++;
  1564. }
  1565. }
  1566. return $i;
  1567. }
  1568. /**
  1569. * Gets the current item ID
  1570. * @return integer The current learnpath item id
  1571. */
  1572. public function get_current_item_id()
  1573. {
  1574. $current = 0;
  1575. if ($this->debug > 0) {
  1576. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1577. }
  1578. if (!empty($this->current)) {
  1579. $current = $this->current;
  1580. }
  1581. if ($this->debug > 2) {
  1582. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1583. }
  1584. return $current;
  1585. }
  1586. /**
  1587. * Force to get the first learnpath item id
  1588. * @return integer The current learnpath item id
  1589. */
  1590. public function get_first_item_id()
  1591. {
  1592. $current = 0;
  1593. if (is_array($this->ordered_items)) {
  1594. $current = $this->ordered_items[0];
  1595. }
  1596. return $current;
  1597. }
  1598. /**
  1599. * Gets the total number of items available for viewing in this SCORM
  1600. * @return integer The total number of items
  1601. */
  1602. public function get_total_items_count()
  1603. {
  1604. if ($this->debug > 0) {
  1605. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1606. }
  1607. return count($this->items);
  1608. }
  1609. /**
  1610. * Gets the total number of items available for viewing in this SCORM but without chapters
  1611. * @return integer The total no-chapters number of items
  1612. */
  1613. public function getTotalItemsCountWithoutDirs()
  1614. {
  1615. if ($this->debug > 0) {
  1616. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1617. }
  1618. $total = 0;
  1619. $typeListNotToCount = self::getChapterTypes();
  1620. foreach ($this->items as $temp2) {
  1621. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1622. $total++;
  1623. }
  1624. }
  1625. return $total;
  1626. }
  1627. /**
  1628. * Gets the first element URL.
  1629. * @return string URL to load into the viewer
  1630. */
  1631. public function first()
  1632. {
  1633. if ($this->debug > 0) {
  1634. error_log('New LP - In learnpath::first()', 0);
  1635. error_log('$this->last_item_seen '.$this->last_item_seen);
  1636. }
  1637. // Test if the last_item_seen exists and is not a dir.
  1638. if (count($this->ordered_items) == 0) {
  1639. $this->index = 0;
  1640. }
  1641. if ($this->debug > 0) {
  1642. if (isset($this->items[$this->last_item_seen])) {
  1643. $status = $this->items[$this->last_item_seen]->get_status();
  1644. }
  1645. error_log('status '.$status);
  1646. }
  1647. if (!empty($this->last_item_seen) &&
  1648. !empty($this->items[$this->last_item_seen]) &&
  1649. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1650. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1651. //&& !$this->items[$this->last_item_seen]->is_done()
  1652. ) {
  1653. if ($this->debug > 2) {
  1654. 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);
  1655. }
  1656. $index = -1;
  1657. foreach ($this->ordered_items as $myindex => $item_id) {
  1658. if ($item_id == $this->last_item_seen) {
  1659. $index = $myindex;
  1660. break;
  1661. }
  1662. }
  1663. if ($index == -1) {
  1664. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1665. if ($this->debug > 2) {
  1666. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1667. }
  1668. return false;
  1669. } else {
  1670. $this->last = $this->last_item_seen;
  1671. $this->current = $this->last_item_seen;
  1672. $this->index = $index;
  1673. }
  1674. } else {
  1675. if ($this->debug > 2) {
  1676. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1677. }
  1678. $index = 0;
  1679. // Loop through all ordered items and stop at the first item that is
  1680. // not a directory *and* that has not been completed yet.
  1681. while ( !empty($this->ordered_items[$index]) AND
  1682. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1683. (
  1684. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1685. $this->items[$this->ordered_items[$index]]->is_done() === true
  1686. ) AND $index < $this->max_ordered_items) {
  1687. $index++;
  1688. }
  1689. $this->last = $this->current;
  1690. // current is
  1691. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1692. $this->index = $index;
  1693. if ($this->debug > 2) {
  1694. error_log('$index ' . $index);
  1695. }
  1696. if ($this->debug > 2) {
  1697. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1698. }
  1699. }
  1700. if ($this->debug > 2) {
  1701. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1702. }
  1703. }
  1704. /**
  1705. * Gets the information about an item in a format usable as JavaScript to update
  1706. * the JS API by just printing this content into the <head> section of the message frame
  1707. * @param int $item_id
  1708. * @return string
  1709. */
  1710. public function get_js_info($item_id = 0)
  1711. {
  1712. if ($this->debug > 0) {
  1713. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1714. }
  1715. $info = '';
  1716. $item_id = intval($item_id);
  1717. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1718. //if item is defined, return values from DB
  1719. $oItem = $this->items[$item_id];
  1720. $info .= '<script language="javascript">';
  1721. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1722. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1723. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1724. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1725. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1726. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1727. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1728. $info .= "top.set_flag_synchronized();";
  1729. $info .= '</script>';
  1730. if ($this->debug > 2) {
  1731. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1732. }
  1733. return $info;
  1734. } else {
  1735. // If item_id is empty, just update to default SCORM data.
  1736. $info .= '<script language="javascript">';
  1737. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1738. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1739. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1740. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1741. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1742. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1743. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1744. $info .= "top.set_flag_synchronized();";
  1745. $info .= '</script>';
  1746. if ($this->debug > 2) {
  1747. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1748. }
  1749. return $info;
  1750. }
  1751. }
  1752. /**
  1753. * Gets the js library from the database
  1754. * @return string The name of the javascript library to be used
  1755. */
  1756. public function get_js_lib()
  1757. {
  1758. $lib = '';
  1759. if (!empty ($this->js_lib)) {
  1760. $lib = $this->js_lib;
  1761. }
  1762. return $lib;
  1763. }
  1764. /**
  1765. * Gets the learnpath database ID
  1766. * @return integer Learnpath ID in the lp table
  1767. */
  1768. public function get_id()
  1769. {
  1770. if (!empty ($this->lp_id)) {
  1771. return $this->lp_id;
  1772. } else {
  1773. return 0;
  1774. }
  1775. }
  1776. /**
  1777. * Gets the last element URL.
  1778. * @return string URL to load into the viewer
  1779. */
  1780. public function get_last()
  1781. {
  1782. if ($this->debug > 0) {
  1783. error_log('New LP - In learnpath::get_last()', 0);
  1784. }
  1785. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1786. if (count($this->ordered_items) > 0) {
  1787. $this->index = count($this->ordered_items) - 1;
  1788. return $this->ordered_items[$this->index];
  1789. }
  1790. return false;
  1791. }
  1792. /**
  1793. * Gets the navigation bar for the learnpath display screen
  1794. * @return string The HTML string to use as a navigation bar
  1795. */
  1796. public function get_navigation_bar($idBar = null, $display = null)
  1797. {
  1798. if ($this->debug > 0) {
  1799. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1800. }
  1801. if (empty($idBar)) {
  1802. $idBar = 'control-top';
  1803. }
  1804. $navbar = null;
  1805. $lp_id = $this->lp_id;
  1806. $mycurrentitemid = $this->get_current_item_id();
  1807. $reportingText = get_lang('Reporting');
  1808. $previousText = get_lang('ScormPrevious');
  1809. $nextText = get_lang('ScormNext');
  1810. $fullScreenText = get_lang('ScormExitFullScreen');
  1811. if ($this->mode == 'fullscreen') {
  1812. $navbar = '
  1813. <span id="'.$idBar.'" class="buttons">
  1814. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1815. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText . '</span>
  1816. </a>
  1817. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1818. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText . '</span>
  1819. </a>
  1820. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1821. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText. '</span>
  1822. </a>
  1823. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1824. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText . '</span>
  1825. </a>
  1826. </span>';
  1827. } else {
  1828. $navbar = '
  1829. <span id="'.$idBar.'" class="buttons text-right">
  1830. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1831. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText. '</span>
  1832. </a>
  1833. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1834. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText. '</span>
  1835. </a>
  1836. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1837. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText . '</span>
  1838. </a>
  1839. </span>';
  1840. }
  1841. return $navbar;
  1842. }
  1843. /**
  1844. * Gets the next resource in queue (url).
  1845. * @return string URL to load into the viewer
  1846. */
  1847. public function get_next_index()
  1848. {
  1849. if ($this->debug > 0) {
  1850. error_log('New LP - In learnpath::get_next_index()', 0);
  1851. }
  1852. // TODO
  1853. $index = $this->index;
  1854. $index++;
  1855. if ($this->debug > 2) {
  1856. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1857. }
  1858. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1859. $index++;
  1860. if ($index == $this->max_ordered_items){
  1861. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1862. return $this->index;
  1863. } else {
  1864. return $index;
  1865. }
  1866. }
  1867. }
  1868. if (empty ($this->ordered_items[$index])) {
  1869. return $this->index;
  1870. }
  1871. if ($this->debug > 2) {
  1872. error_log('New LP - index is now ' . $index, 0);
  1873. }
  1874. return $index;
  1875. }
  1876. /**
  1877. * Gets item_id for the next element
  1878. * @return integer Next item (DB) ID
  1879. */
  1880. public function get_next_item_id()
  1881. {
  1882. if ($this->debug > 0) {
  1883. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1884. }
  1885. $new_index = $this->get_next_index();
  1886. if (!empty ($new_index)) {
  1887. if (isset ($this->ordered_items[$new_index])) {
  1888. if ($this->debug > 2) {
  1889. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1890. }
  1891. return $this->ordered_items[$new_index];
  1892. }
  1893. }
  1894. if ($this->debug > 2) {
  1895. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1896. }
  1897. return 0;
  1898. }
  1899. /**
  1900. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1901. *
  1902. * Generally, the package provided is in the form of a zip file, so the function
  1903. * has been written to test a zip file. If not a zip, the function will return the
  1904. * default return value: ''
  1905. * @param string the path to the file
  1906. * @param string the original name of the file
  1907. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1908. */
  1909. public static function get_package_type($file_path, $file_name)
  1910. {
  1911. // Get name of the zip file without the extension.
  1912. $file_info = pathinfo($file_name);
  1913. $filename = $file_info['basename']; // Name including extension.
  1914. $extension = $file_info['extension']; // Extension only.
  1915. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1916. 'dll',
  1917. 'exe'
  1918. ))) {
  1919. return 'oogie';
  1920. }
  1921. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1922. 'dll',
  1923. 'exe'
  1924. ))) {
  1925. return 'woogie';
  1926. }
  1927. $zipFile = new PclZip($file_path);
  1928. // Check the zip content (real size and file extension).
  1929. $zipContentArray = $zipFile->listContent();
  1930. $package_type = '';
  1931. $at_root = false;
  1932. $manifest = '';
  1933. $aicc_match_crs = 0;
  1934. $aicc_match_au = 0;
  1935. $aicc_match_des = 0;
  1936. $aicc_match_cst = 0;
  1937. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1938. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1939. foreach ($zipContentArray as $thisContent) {
  1940. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1941. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1942. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1943. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1944. $package_type = 'scorm';
  1945. break; // Exit the foreach loop.
  1946. } elseif (
  1947. preg_match('/aicc\//i', $thisContent['filename']) ||
  1948. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1949. ) {
  1950. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1951. switch ($ext) {
  1952. case 'crs':
  1953. $aicc_match_crs = 1;
  1954. break;
  1955. case 'au':
  1956. $aicc_match_au = 1;
  1957. break;
  1958. case 'des':
  1959. $aicc_match_des = 1;
  1960. break;
  1961. case 'cst':
  1962. $aicc_match_cst = 1;
  1963. break;
  1964. default:
  1965. break;
  1966. }
  1967. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1968. } else {
  1969. $package_type = '';
  1970. }
  1971. }
  1972. }
  1973. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1974. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1975. $package_type = 'aicc';
  1976. }
  1977. return $package_type;
  1978. }
  1979. /**
  1980. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1981. * @return string URL to load into the viewer
  1982. */
  1983. public function get_previous_index()
  1984. {
  1985. if ($this->debug > 0) {
  1986. error_log('New LP - In learnpath::get_previous_index()', 0);
  1987. }
  1988. $index = $this->index;
  1989. if (isset ($this->ordered_items[$index -1])) {
  1990. $index--;
  1991. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1992. $index--;
  1993. if ($index < 0) {
  1994. return $this->index;
  1995. }
  1996. }
  1997. } else {
  1998. if ($this->debug > 2) {
  1999. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2000. }
  2001. // There is no previous item.
  2002. }
  2003. return $index;
  2004. }
  2005. /**
  2006. * Gets item_id for the next element
  2007. * @return integer Previous item (DB) ID
  2008. */
  2009. public function get_previous_item_id()
  2010. {
  2011. if ($this->debug > 0) {
  2012. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2013. }
  2014. $new_index = $this->get_previous_index();
  2015. return $this->ordered_items[$new_index];
  2016. }
  2017. /**
  2018. * Gets the progress value from the progress_db attribute
  2019. * @return integer Current progress value
  2020. */
  2021. public function get_progress()
  2022. {
  2023. if ($this->debug > 0) {
  2024. error_log('New LP - In learnpath::get_progress()', 0);
  2025. }
  2026. if (!empty ($this->progress_db)) {
  2027. return $this->progress_db;
  2028. }
  2029. return 0;
  2030. }
  2031. /**
  2032. * Returns the HTML necessary to print a mediaplayer block inside a page
  2033. * @return string The mediaplayer HTML
  2034. */
  2035. public function get_mediaplayer($autostart = 'true')
  2036. {
  2037. $course_id = api_get_course_int_id();
  2038. $_course = api_get_course_info();
  2039. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2040. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2041. // Getting all the information about the item.
  2042. $sql = "SELECT * FROM $tbl_lp_item as lp
  2043. INNER JOIN $tbl_lp_item_view as lp_view
  2044. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2045. WHERE
  2046. lp.id = '".$_SESSION['oLP']->current."' AND
  2047. lp.c_id = $course_id AND
  2048. lp_view.c_id = $course_id";
  2049. $result = Database::query($sql);
  2050. $row = Database::fetch_assoc($result);
  2051. $output = '';
  2052. if (!empty ($row['audio'])) {
  2053. $list = $_SESSION['oLP']->get_toc();
  2054. $type_quiz = false;
  2055. foreach($list as $toc) {
  2056. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2057. $type_quiz = true;
  2058. }
  2059. }
  2060. if ($type_quiz) {
  2061. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2062. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2063. } else {
  2064. $autostart_audio = $autostart;
  2065. }
  2066. } else {
  2067. $autostart_audio = 'true';
  2068. }
  2069. $courseInfo = api_get_course_info();
  2070. $audio = $row['audio'];
  2071. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2072. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2073. if (!file_exists($file)) {
  2074. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2075. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2076. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2077. }
  2078. $player = Display::getMediaPlayer(
  2079. $file,
  2080. array(
  2081. 'id' => 'lp_audio_media_player',
  2082. 'url' => $url,
  2083. 'autoplay' => $autostart_audio,
  2084. 'width' => '100%'
  2085. )
  2086. );
  2087. // The mp3 player.
  2088. $output = '<div id="container">';
  2089. $output .= $player;
  2090. $output .= '</div>';
  2091. }
  2092. return $output;
  2093. }
  2094. /**
  2095. * @param int $lpId
  2096. * @param array $courseInfo
  2097. * @return bool
  2098. *
  2099. */
  2100. public static function isBlockedByPrerequisite($student_id, $prerequisite, $courseInfo, $sessionId)
  2101. {
  2102. $isBlocked = false;
  2103. if (!empty($prerequisite)) {
  2104. $progress = self::getProgress(
  2105. $prerequisite,
  2106. $student_id,
  2107. $courseInfo['real_id'],
  2108. $sessionId
  2109. );
  2110. $progress = intval($progress);
  2111. if ($progress < 100) {
  2112. $isBlocked = true;
  2113. }
  2114. }
  2115. return $isBlocked;
  2116. }
  2117. /**
  2118. * Checks if the learning path is visible for student after the progress
  2119. * of its prerequisite is completed, considering the time availability and
  2120. * the LP visibility.
  2121. * @param int $lp_id
  2122. * @param int $student_id
  2123. * @param string Course code (optional)
  2124. * @param int $sessionId
  2125. * @return bool
  2126. */
  2127. public static function is_lp_visible_for_student(
  2128. $lp_id,
  2129. $student_id,
  2130. $courseCode = null,
  2131. $sessionId = null
  2132. ) {
  2133. $lp_id = (int)$lp_id;
  2134. $courseInfo = api_get_course_info($courseCode);
  2135. $sessionId = intval($sessionId);
  2136. if (empty($sessionId)) {
  2137. $sessionId = api_get_session_id();
  2138. }
  2139. $itemInfo = api_get_item_property_info(
  2140. $courseInfo['real_id'],
  2141. TOOL_LEARNPATH,
  2142. $lp_id,
  2143. $sessionId
  2144. );
  2145. // If the item was deleted.
  2146. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2147. return false;
  2148. }
  2149. // @todo remove this query and load the row info as a parameter
  2150. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2151. // Get current prerequisite
  2152. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2153. FROM $tbl_learnpath
  2154. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2155. $rs = Database::query($sql);
  2156. $now = time();
  2157. if (Database::num_rows($rs) > 0) {
  2158. $row = Database::fetch_array($rs, 'ASSOC');
  2159. $prerequisite = $row['prerequisite'];
  2160. $is_visible = true;
  2161. $isBlocked = self::isBlockedByPrerequisite(
  2162. $student_id,
  2163. $prerequisite,
  2164. $courseInfo,
  2165. $sessionId
  2166. );
  2167. if ($isBlocked) {
  2168. $is_visible = false;
  2169. }
  2170. // Also check the time availability of the LP
  2171. if ($is_visible) {
  2172. // Adding visibility restrictions
  2173. if (!empty($row['publicated_on'])) {
  2174. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2175. $is_visible = false;
  2176. }
  2177. }
  2178. // Blocking empty start times see BT#2800
  2179. global $_custom;
  2180. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2181. $_custom['lps_hidden_when_no_start_date']
  2182. ) {
  2183. if (empty($row['publicated_on'])) {
  2184. $is_visible = false;
  2185. }
  2186. }
  2187. if (!empty($row['expired_on'])) {
  2188. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2189. $is_visible = false;
  2190. }
  2191. }
  2192. }
  2193. // Check if the subscription users/group to a LP is ON
  2194. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2195. // Try group
  2196. $is_visible = false;
  2197. // Checking only the user visibility
  2198. $userVisibility = api_get_item_visibility(
  2199. $courseInfo,
  2200. 'learnpath',
  2201. $row['id'],
  2202. $sessionId,
  2203. $student_id,
  2204. 'LearnpathSubscription'
  2205. );
  2206. if ($userVisibility == 1) {
  2207. $is_visible = true;
  2208. } else {
  2209. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2210. if (!empty($userGroups)) {
  2211. foreach ($userGroups as $groupInfo) {
  2212. $groupId = $groupInfo['iid'];
  2213. $userVisibility = api_get_item_visibility(
  2214. $courseInfo,
  2215. 'learnpath',
  2216. $row['id'],
  2217. $sessionId,
  2218. null,
  2219. 'LearnpathSubscription',
  2220. $groupId
  2221. );
  2222. if ($userVisibility == 1) {
  2223. $is_visible = true;
  2224. break;
  2225. }
  2226. }
  2227. }
  2228. }
  2229. }
  2230. return $is_visible;
  2231. }
  2232. return false;
  2233. }
  2234. /**
  2235. * @param int $lpId
  2236. * @param int $userId
  2237. * @param int $courseId
  2238. * @param int $sessionId
  2239. * @return int
  2240. */
  2241. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2242. {
  2243. $lpId = intval($lpId);
  2244. $userId = intval($userId);
  2245. $courseId = intval($courseId);
  2246. $sessionId = intval($sessionId);
  2247. $progress = 0;
  2248. $sessionCondition = api_get_session_condition($sessionId);
  2249. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2250. $sql = "SELECT * FROM $table
  2251. WHERE
  2252. c_id = ".$courseId." AND
  2253. lp_id = $lpId AND
  2254. user_id = $userId $sessionCondition";
  2255. $res = Database::query($sql);
  2256. if (Database :: num_rows($res) > 0) {
  2257. $row = Database:: fetch_array($res);
  2258. $progress = $row['progress'];
  2259. }
  2260. return $progress;
  2261. }
  2262. /**
  2263. * Displays a progress bar
  2264. * completed so far.
  2265. * @param integer $percentage Progress value to display
  2266. * @param string $text_add Text to display near the progress value
  2267. * @return string HTML string containing the progress bar
  2268. */
  2269. public static function get_progress_bar($percentage = -1, $text_add = '')
  2270. {
  2271. $text = $percentage . $text_add;
  2272. $output = '<div class="progress">
  2273. <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.';">
  2274. '. $text .'
  2275. </div>
  2276. </div>';
  2277. return $output;
  2278. }
  2279. /**
  2280. * @param string $mode can be '%' or 'abs'
  2281. * otherwise this value will be used $this->progress_bar_mode
  2282. * @return string
  2283. */
  2284. public function getProgressBar($mode = null)
  2285. {
  2286. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2287. return self::get_progress_bar($percentage, $text_add);
  2288. }
  2289. /**
  2290. * Gets the progress bar info to display inside the progress bar.
  2291. * Also used by scorm_api.php
  2292. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2293. * we display a number of completed elements per total elements
  2294. * @param integer $add Additional steps to fake as completed
  2295. * @return list array Percentage or number and symbol (% or /xx)
  2296. */
  2297. public function get_progress_bar_text($mode = '', $add = 0)
  2298. {
  2299. if ($this->debug > 0) {
  2300. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2301. }
  2302. if (empty($mode)) {
  2303. $mode = $this->progress_bar_mode;
  2304. }
  2305. $total_items = $this->getTotalItemsCountWithoutDirs();
  2306. if ($this->debug > 2) {
  2307. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2308. }
  2309. $completeItems = $this->get_complete_items_count();
  2310. if ($this->debug > 2) {
  2311. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2312. }
  2313. if ($add != 0) {
  2314. $completeItems += $add;
  2315. if ($this->debug > 2) {
  2316. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2317. }
  2318. }
  2319. $text = '';
  2320. if ($completeItems > $total_items) {
  2321. $completeItems = $total_items;
  2322. }
  2323. $percentage = 0;
  2324. if ($mode == '%') {
  2325. if ($total_items > 0) {
  2326. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2327. } else {
  2328. $percentage = 0;
  2329. }
  2330. $percentage = number_format($percentage, 0);
  2331. $text = '%';
  2332. } elseif ($mode == 'abs') {
  2333. $percentage = $completeItems;
  2334. $text = '/' . $total_items;
  2335. }
  2336. return array(
  2337. $percentage,
  2338. $text
  2339. );
  2340. }
  2341. /**
  2342. * Gets the progress bar mode
  2343. * @return string The progress bar mode attribute
  2344. */
  2345. public function get_progress_bar_mode()
  2346. {
  2347. if ($this->debug > 0) {
  2348. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2349. }
  2350. if (!empty ($this->progress_bar_mode)) {
  2351. return $this->progress_bar_mode;
  2352. } else {
  2353. return '%';
  2354. }
  2355. }
  2356. /**
  2357. * Gets the learnpath proximity (remote or local)
  2358. * @return string Learnpath proximity
  2359. */
  2360. public function get_proximity()
  2361. {
  2362. if ($this->debug > 0) {
  2363. error_log('New LP - In learnpath::get_proximity()', 0);
  2364. }
  2365. if (!empty ($this->proximity)) {
  2366. return $this->proximity;
  2367. } else {
  2368. return '';
  2369. }
  2370. }
  2371. /**
  2372. * Gets the learnpath theme (remote or local)
  2373. * @return string Learnpath theme
  2374. */
  2375. public function get_theme()
  2376. {
  2377. if ($this->debug > 0) {
  2378. error_log('New LP - In learnpath::get_theme()', 0);
  2379. }
  2380. if (!empty ($this->theme)) {
  2381. return $this->theme;
  2382. } else {
  2383. return '';
  2384. }
  2385. }
  2386. /**
  2387. * Gets the learnpath session id
  2388. * @return string Learnpath theme
  2389. */
  2390. public function get_lp_session_id()
  2391. {
  2392. if ($this->debug > 0) {
  2393. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2394. }
  2395. if (!empty ($this->lp_session_id)) {
  2396. return $this->lp_session_id;
  2397. } else {
  2398. return 0;
  2399. }
  2400. }
  2401. /**
  2402. * Gets the learnpath image
  2403. * @return string Web URL of the LP image
  2404. */
  2405. public function get_preview_image()
  2406. {
  2407. if ($this->debug > 0) {
  2408. error_log('New LP - In learnpath::get_preview_image()', 0);
  2409. }
  2410. if (!empty($this->preview_image)) {
  2411. return $this->preview_image;
  2412. } else {
  2413. return '';
  2414. }
  2415. }
  2416. /**
  2417. * @param string $size
  2418. * @param string $path_type
  2419. * @return bool|string
  2420. */
  2421. public function get_preview_image_path($size = null, $path_type = 'web')
  2422. {
  2423. $preview_image = $this->get_preview_image();
  2424. if (isset($preview_image) && !empty($preview_image)) {
  2425. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2426. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2427. if (isset($size)) {
  2428. $info = pathinfo($preview_image);
  2429. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2430. if (file_exists($image_sys_path.$image_custom_size)) {
  2431. if ($path_type == 'web') {
  2432. return $image_path.$image_custom_size;
  2433. } else {
  2434. return $image_sys_path.$image_custom_size;
  2435. }
  2436. }
  2437. } else {
  2438. if ($path_type == 'web') {
  2439. return $image_path.$preview_image;
  2440. } else {
  2441. return $image_sys_path.$preview_image;
  2442. }
  2443. }
  2444. }
  2445. return false;
  2446. }
  2447. /**
  2448. * Gets the learnpath author
  2449. * @return string LP's author
  2450. */
  2451. public function get_author()
  2452. {
  2453. if ($this->debug > 0) {
  2454. error_log('New LP - In learnpath::get_author()', 0);
  2455. }
  2456. if (!empty ($this->author)) {
  2457. return $this->author;
  2458. } else {
  2459. return '';
  2460. }
  2461. }
  2462. /**
  2463. * Gets the learnpath author
  2464. * @return string LP's author
  2465. */
  2466. public function get_hide_toc_frame()
  2467. {
  2468. if ($this->debug > 0) {
  2469. error_log('New LP - In learnpath::get_author()', 0);
  2470. }
  2471. if (!empty ($this->hide_toc_frame)) {
  2472. return $this->hide_toc_frame;
  2473. } else {
  2474. return '';
  2475. }
  2476. }
  2477. /**
  2478. * Generate a new prerequisites string for a given item. If this item was a sco and
  2479. * its prerequisites were strings (instead of IDs), then transform those strings into
  2480. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2481. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2482. * same rule as the scorm_export() method
  2483. * @param integer Item ID
  2484. * @return string Prerequisites string ready for the export as SCORM
  2485. */
  2486. public function get_scorm_prereq_string($item_id)
  2487. {
  2488. if ($this->debug > 0) {
  2489. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2490. }
  2491. if (!is_object($this->items[$item_id])) {
  2492. return false;
  2493. }
  2494. /** @var learnpathItem $oItem */
  2495. $oItem = $this->items[$item_id];
  2496. $prereq = $oItem->get_prereq_string();
  2497. if (empty($prereq)) {
  2498. return '';
  2499. }
  2500. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2501. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2502. // then simply return it (with the ITEM_ prefix).
  2503. //return 'ITEM_' . $prereq;
  2504. return $this->items[$prereq]->ref;
  2505. } else {
  2506. if (isset($this->refs_list[$prereq])) {
  2507. // It's a simple string item from which the ID can be found in the refs list,
  2508. // so we can transform it directly to an ID for export.
  2509. return $this->items[$this->refs_list[$prereq]]->ref;
  2510. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2511. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2512. } else {
  2513. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2514. // and replace them, one by one, by the internal IDs (chamilo db)
  2515. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2516. // by a space as well.
  2517. $find = array (
  2518. '&',
  2519. '|',
  2520. '~',
  2521. '=',
  2522. '<>',
  2523. '{',
  2524. '}',
  2525. '*',
  2526. '(',
  2527. ')'
  2528. );
  2529. $replace = array (
  2530. ' ',
  2531. ' ',
  2532. ' ',
  2533. ' ',
  2534. ' ',
  2535. ' ',
  2536. ' ',
  2537. ' ',
  2538. ' ',
  2539. ' '
  2540. );
  2541. $prereq_mod = str_replace($find, $replace, $prereq);
  2542. $ids = explode(' ', $prereq_mod);
  2543. foreach ($ids as $id) {
  2544. $id = trim($id);
  2545. if (isset ($this->refs_list[$id])) {
  2546. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2547. }
  2548. }
  2549. return $prereq;
  2550. }
  2551. }
  2552. }
  2553. /**
  2554. * Returns the XML DOM document's node
  2555. * @param resource Reference to a list of objects to search for the given ITEM_*
  2556. * @param string The identifier to look for
  2557. * @return mixed The reference to the element found with that identifier. False if not found
  2558. */
  2559. public function get_scorm_xml_node(& $children, $id)
  2560. {
  2561. for ($i = 0; $i < $children->length; $i++) {
  2562. $item_temp = $children->item($i);
  2563. if ($item_temp->nodeName == 'item') {
  2564. if ($item_temp->getAttribute('identifier') == $id) {
  2565. return $item_temp;
  2566. }
  2567. }
  2568. $subchildren = $item_temp->childNodes;
  2569. if ($subchildren && $subchildren->length > 0) {
  2570. $val = $this->get_scorm_xml_node($subchildren, $id);
  2571. if (is_object($val)) {
  2572. return $val;
  2573. }
  2574. }
  2575. }
  2576. return false;
  2577. }
  2578. /**
  2579. * Gets the status list for all LP's items
  2580. * @return array Array of [index] => [item ID => current status]
  2581. */
  2582. public function get_items_status_list()
  2583. {
  2584. if ($this->debug > 0) {
  2585. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2586. }
  2587. $list = array ();
  2588. foreach ($this->ordered_items as $item_id) {
  2589. $list[] = array (
  2590. $item_id => $this->items[$item_id]->get_status()
  2591. );
  2592. }
  2593. return $list;
  2594. }
  2595. /**
  2596. * Return the number of interactions for the given learnpath Item View ID.
  2597. * This method can be used as static.
  2598. * @param integer Item View ID
  2599. * @param integer course id
  2600. * @return integer Number of interactions
  2601. */
  2602. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2603. {
  2604. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2605. $lp_iv_id = intval($lp_iv_id);
  2606. $course_id = intval($course_id);
  2607. $sql = "SELECT count(*) FROM $table
  2608. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2609. $res = Database::query($sql);
  2610. $num = 0;
  2611. if (Database::num_rows($res)) {
  2612. $row = Database::fetch_array($res);
  2613. $num = $row[0];
  2614. }
  2615. return $num;
  2616. }
  2617. /**
  2618. * Return the interactions as an array for the given lp_iv_id.
  2619. * This method can be used as static.
  2620. * @param integer Learnpath Item View ID
  2621. * @return array
  2622. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2623. */
  2624. public static function get_iv_interactions_array($lp_iv_id)
  2625. {
  2626. $course_id = api_get_course_int_id();
  2627. $list = array();
  2628. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2629. if (empty($lp_iv_id)) {
  2630. return array();
  2631. }
  2632. $sql = "SELECT * FROM $table
  2633. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2634. ORDER BY order_id ASC";
  2635. $res = Database::query($sql);
  2636. $num = Database :: num_rows($res);
  2637. if ($num > 0) {
  2638. $list[] = array (
  2639. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2640. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2641. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2642. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2643. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2644. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2645. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2646. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2647. );
  2648. while ($row = Database :: fetch_array($res)) {
  2649. $list[] = array (
  2650. 'order_id' => ($row['order_id'] + 1),
  2651. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2652. 'type' => $row['interaction_type'],
  2653. 'time' => $row['completion_time'],
  2654. //'correct_responses' => $row['correct_responses'],
  2655. 'correct_responses' => '', // Hide correct responses from students.
  2656. 'student_response' => $row['student_response'],
  2657. 'result' => $row['result'],
  2658. 'latency' => $row['latency']
  2659. );
  2660. }
  2661. }
  2662. return $list;
  2663. }
  2664. /**
  2665. * Return the number of objectives for the given learnpath Item View ID.
  2666. * This method can be used as static.
  2667. * @param integer Item View ID
  2668. * @return integer Number of objectives
  2669. */
  2670. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2671. {
  2672. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2673. $course_id = intval($course_id);
  2674. $lp_iv_id = intval($lp_iv_id);
  2675. $sql = "SELECT count(*) FROM $table
  2676. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2677. //@todo seems that this always returns 0
  2678. $res = Database::query($sql);
  2679. $num = 0;
  2680. if (Database::num_rows($res)) {
  2681. $row = Database :: fetch_array($res);
  2682. $num = $row[0];
  2683. }
  2684. return $num;
  2685. }
  2686. /**
  2687. * Return the objectives as an array for the given lp_iv_id.
  2688. * This method can be used as static.
  2689. * @param integer Learnpath Item View ID
  2690. * @return array
  2691. * @todo Translate labels
  2692. */
  2693. public static function get_iv_objectives_array($lp_iv_id = 0)
  2694. {
  2695. $course_id = api_get_course_int_id();
  2696. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2697. $sql = "SELECT * FROM $table
  2698. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2699. ORDER BY order_id ASC";
  2700. $res = Database::query($sql);
  2701. $num = Database :: num_rows($res);
  2702. $list = array();
  2703. if ($num > 0) {
  2704. $list[] = array(
  2705. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2706. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2707. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2708. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2709. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2710. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2711. );
  2712. while ($row = Database :: fetch_array($res)) {
  2713. $list[] = array (
  2714. 'order_id' => ($row['order_id'] + 1),
  2715. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2716. 'score_raw' => $row['score_raw'],
  2717. 'score_max' => $row['score_max'],
  2718. 'score_min' => $row['score_min'],
  2719. 'status' => $row['status']
  2720. );
  2721. }
  2722. }
  2723. return $list;
  2724. }
  2725. /**
  2726. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2727. * used by get_html_toc() to be ready to display
  2728. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2729. */
  2730. public function get_toc()
  2731. {
  2732. if ($this->debug > 0) {
  2733. error_log('learnpath::get_toc()', 0);
  2734. }
  2735. $toc = array();
  2736. foreach ($this->ordered_items as $item_id) {
  2737. if ($this->debug > 2) {
  2738. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2739. }
  2740. // TODO: Change this link generation and use new function instead.
  2741. $toc[] = array(
  2742. 'id' => $item_id,
  2743. 'title' => $this->items[$item_id]->get_title(),
  2744. 'status' => $this->items[$item_id]->get_status(),
  2745. 'level' => $this->items[$item_id]->get_level(),
  2746. 'type' => $this->items[$item_id]->get_type(),
  2747. 'description' => $this->items[$item_id]->get_description(),
  2748. 'path' => $this->items[$item_id]->get_path(),
  2749. );
  2750. }
  2751. if ($this->debug > 2) {
  2752. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2753. }
  2754. return $toc;
  2755. }
  2756. /**
  2757. * Generate and return the table of contents for this learnpath. The JS
  2758. * table returned is used inside of scorm_api.php
  2759. * @return string A JS array vairiable construction
  2760. */
  2761. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2762. {
  2763. if ($this->debug > 0) {
  2764. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2765. }
  2766. $toc = $varname.' = new Array();';
  2767. foreach ($this->ordered_items as $item_id) {
  2768. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2769. }
  2770. if ($this->debug > 2) {
  2771. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2772. }
  2773. return $toc;
  2774. }
  2775. /**
  2776. * Gets the learning path type
  2777. * @param boolean Return the name? If false, return the ID. Default is false.
  2778. * @return mixed Type ID or name, depending on the parameter
  2779. */
  2780. public function get_type($get_name = false)
  2781. {
  2782. $res = false;
  2783. if ($this->debug > 0) {
  2784. error_log('New LP - In learnpath::get_type()', 0);
  2785. }
  2786. if (!empty ($this->type)) {
  2787. if ($get_name) {
  2788. // Get it from the lp_type table in main db.
  2789. } else {
  2790. $res = $this->type;
  2791. }
  2792. }
  2793. if ($this->debug > 2) {
  2794. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2795. }
  2796. return $res;
  2797. }
  2798. /**
  2799. * Gets the learning path type as static method
  2800. * @param boolean Return the name? If false, return the ID. Default is false.
  2801. * @return mixed Type ID or name, depending on the parameter
  2802. */
  2803. public static function get_type_static($lp_id = 0)
  2804. {
  2805. $course_id = api_get_course_int_id();
  2806. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2807. $lp_id = intval($lp_id);
  2808. $sql = "SELECT lp_type FROM $tbl_lp
  2809. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2810. $res = Database::query($sql);
  2811. if ($res === false) {
  2812. return null;
  2813. }
  2814. if (Database :: num_rows($res) <= 0) {
  2815. return null;
  2816. }
  2817. $row = Database :: fetch_array($res);
  2818. return $row['lp_type'];
  2819. }
  2820. /**
  2821. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2822. * This method can be used as abstract and is recursive
  2823. * @param integer Learnpath ID
  2824. * @param integer Parent ID of the items to look for
  2825. * @return mixed Ordered list of item IDs or false on error
  2826. */
  2827. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2828. {
  2829. if (empty($course_id)) {
  2830. $course_id = api_get_course_int_id();
  2831. } else {
  2832. $course_id = intval($course_id);
  2833. }
  2834. $list = array();
  2835. if (empty($lp)) {
  2836. return false;
  2837. }
  2838. $lp = intval($lp);
  2839. $parent = intval($parent);
  2840. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2841. $sql = "SELECT id FROM $tbl_lp_item
  2842. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2843. ORDER BY display_order";
  2844. $res = Database::query($sql);
  2845. while ($row = Database :: fetch_array($res)) {
  2846. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2847. $list[] = $row['id'];
  2848. foreach ($sublist as $item) {
  2849. $list[] = $item;
  2850. }
  2851. }
  2852. return $list;
  2853. }
  2854. /**
  2855. * @return array
  2856. */
  2857. public static function getChapterTypes()
  2858. {
  2859. return array(
  2860. 'dir'
  2861. );
  2862. }
  2863. /**
  2864. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2865. * @return string HTML TOC ready to display
  2866. */
  2867. public function get_html_toc($toc_list = null)
  2868. {
  2869. if ($this->debug > 0) {
  2870. error_log('In learnpath::get_html_toc()', 0);
  2871. }
  2872. if (empty($toc_list)) {
  2873. $toc_list = $this->get_toc();
  2874. }
  2875. $html = '<div class="scorm-body">';
  2876. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2877. // Temporary variables.
  2878. $mycurrentitemid = $this->get_current_item_id();
  2879. $color_counter = 0;
  2880. $i = 0;
  2881. foreach ($toc_list as $item) {
  2882. // Style Status
  2883. $class_name = [
  2884. 'not attempted' => 'scorm_not_attempted',
  2885. 'incomplete' => 'scorm_not_attempted',
  2886. 'failed' => 'scorm_failed',
  2887. 'completed' => 'scorm_completed',
  2888. 'passed' => 'scorm_completed',
  2889. 'succeeded' => 'scorm_completed',
  2890. 'browsed' => 'scorm_completed',
  2891. ];
  2892. $rowColor = ' ';
  2893. $dirTypes = self::getChapterTypes();
  2894. if (in_array($item['type'], $dirTypes)) {
  2895. $rowColor ='scorm_item_section ';
  2896. }
  2897. if ($item['id'] == $this->current) {
  2898. $rowColor = 'scorm_item_normal '.$rowColor.' scorm_highlight';
  2899. } elseif (!in_array($item['type'], $dirTypes)) {
  2900. $rowColor = 'scorm_item_normal '.$rowColor.' ';
  2901. }
  2902. $html .= '<div id="toc_' . $item['id'] . '" class="' . $rowColor . ' ' . $class_name[$item['status']] . '">';
  2903. // Learning path title
  2904. $title = $item['title'];
  2905. if (empty ($title)) {
  2906. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2907. }
  2908. $title = Security::remove_XSS($title);
  2909. // Learning path personalization
  2910. // build the LP tree
  2911. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2912. $description = $item['description'];
  2913. if (empty($description)) {
  2914. $description = $title;
  2915. }
  2916. if (in_array($item['type'], $dirTypes)) {
  2917. // Chapters
  2918. $html .= '<div class="section level_'.$item['level'].'" title="'.$description.'" >';
  2919. } else {
  2920. $html .= '<div class="item level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2921. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2922. }
  2923. if (in_array($item['type'], $dirTypes)) {
  2924. // Chapter
  2925. // if you want to put an image before, you should use css
  2926. $html .= stripslashes($title);
  2927. } else {
  2928. $this->get_link('http', $item['id'], $toc_list);
  2929. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2930. }
  2931. $html .= "</div>";
  2932. if ($rowColor != '') {
  2933. $html .= '</div>';
  2934. }
  2935. $color_counter++;
  2936. }
  2937. $html .= "</div>";
  2938. $html .= "</div>";
  2939. return $html;
  2940. }
  2941. /**
  2942. * Returns an HTML-formatted string ready to display with teacher buttons
  2943. * in LP view menu
  2944. * @return string HTML TOC ready to display
  2945. */
  2946. public function get_teacher_toc_buttons()
  2947. {
  2948. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2949. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2950. $html = '';
  2951. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2952. $gradebook = '';
  2953. if (!empty($_GET['gradebook'])) {
  2954. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2955. }
  2956. if ($this->get_lp_session_id() == api_get_session_id()) {
  2957. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2958. $html .= '<div class="btn-group">';
  2959. $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'>" .
  2960. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2961. $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'>" .
  2962. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2963. $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">' .
  2964. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2965. $html .= '</div>';
  2966. $html .= '</div>';
  2967. }
  2968. }
  2969. return $html;
  2970. }
  2971. /**
  2972. * Gets the learnpath maker name - generally the editor's name
  2973. * @return string Learnpath maker name
  2974. */
  2975. public function get_maker()
  2976. {
  2977. if ($this->debug > 0) {
  2978. error_log('New LP - In learnpath::get_maker()', 0);
  2979. }
  2980. if (!empty ($this->maker)) {
  2981. return $this->maker;
  2982. } else {
  2983. return '';
  2984. }
  2985. }
  2986. /**
  2987. * Gets the learnpath name/title
  2988. * @return string Learnpath name/title
  2989. */
  2990. public function get_name()
  2991. {
  2992. if ($this->debug > 0) {
  2993. error_log('New LP - In learnpath::get_name()', 0);
  2994. }
  2995. if (!empty ($this->name)) {
  2996. return $this->name;
  2997. } else {
  2998. return 'N/A';
  2999. }
  3000. }
  3001. /**
  3002. * Gets a link to the resource from the present location, depending on item ID.
  3003. * @param string $type Type of link expected
  3004. * @param integer $item_id Learnpath item ID
  3005. * @return string $provided_toc Link to the lp_item resource
  3006. */
  3007. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3008. {
  3009. $course_id = $this->get_course_int_id();
  3010. if ($this->debug > 0) {
  3011. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3012. }
  3013. if (empty($item_id)) {
  3014. if ($this->debug > 2) {
  3015. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3016. }
  3017. $item_id = $this->get_current_item_id();
  3018. }
  3019. if (empty($item_id)) {
  3020. if ($this->debug > 2) {
  3021. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3022. }
  3023. //still empty, this means there was no item_id given and we are not in an object context or
  3024. //the object property is empty, return empty link
  3025. $item_id = $this->first();
  3026. return '';
  3027. }
  3028. $file = '';
  3029. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3030. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3031. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3032. $item_id = intval($item_id);
  3033. $sql = "SELECT
  3034. l.lp_type as ltype,
  3035. l.path as lpath,
  3036. li.item_type as litype,
  3037. li.path as lipath,
  3038. li.parameters as liparams
  3039. FROM $lp_table l
  3040. INNER JOIN $lp_item_table li
  3041. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3042. WHERE li.id = $item_id ";
  3043. if ($this->debug > 2) {
  3044. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3045. }
  3046. $res = Database::query($sql);
  3047. if (Database :: num_rows($res) > 0) {
  3048. $row = Database :: fetch_array($res);
  3049. $lp_type = $row['ltype'];
  3050. $lp_path = $row['lpath'];
  3051. $lp_item_type = $row['litype'];
  3052. $lp_item_path = $row['lipath'];
  3053. $lp_item_params = $row['liparams'];
  3054. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3055. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3056. }
  3057. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3058. if ($type == 'http') {
  3059. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3060. } else {
  3061. $course_path = $sys_course_path; //system path
  3062. }
  3063. // 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.
  3064. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3065. $lp_type = 1;
  3066. }
  3067. if ($this->debug > 2) {
  3068. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3069. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3070. }
  3071. // Now go through the specific cases to get the end of the path
  3072. // @todo Use constants instead of int values.
  3073. switch ($lp_type) {
  3074. case 1 :
  3075. $file = self::rl_get_resource_link_for_learnpath(
  3076. $course_id,
  3077. $this->get_id(),
  3078. $item_id,
  3079. $this->get_view_id()
  3080. );
  3081. if ($this->debug > 0) {
  3082. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3083. }
  3084. switch ($lp_item_type) {
  3085. case 'dir':
  3086. $file = 'lp_content.php?type=dir';
  3087. break;
  3088. case 'link':
  3089. if (Link::is_youtube_link($file)) {
  3090. $src = Link::get_youtube_video_id($file);
  3091. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=youtube&source='.$src;
  3092. } elseif (Link::isVimeoLink($file)) {
  3093. $src = Link::getVimeoLinkId($file);
  3094. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=vimeo&source='.$src;
  3095. } else {
  3096. // If the current site is HTTPS and the link is
  3097. // HTTP, browsers will refuse opening the link
  3098. $urlId = api_get_current_access_url_id();
  3099. $url = api_get_access_url($urlId, false);
  3100. $protocol = substr($url['url'], 0, 5);
  3101. if ($protocol === 'https') {
  3102. $linkProtocol = substr($file, 0, 5);
  3103. if ($linkProtocol === 'http:') {
  3104. //this is the special intervention case
  3105. $file = api_get_path(
  3106. WEB_CODE_PATH
  3107. ) . 'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3108. }
  3109. }
  3110. }
  3111. break;
  3112. case 'quiz':
  3113. // Check how much attempts of a exercise exits in lp
  3114. $lp_item_id = $this->get_current_item_id();
  3115. $lp_view_id = $this->get_view_id();
  3116. $prevent_reinit = null;
  3117. if (isset($this->items[$this->current])) {
  3118. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3119. }
  3120. if (empty($provided_toc)) {
  3121. if ($this->debug > 0) {
  3122. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3123. }
  3124. $list = $this->get_toc();
  3125. } else {
  3126. if ($this->debug > 0) {
  3127. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3128. }
  3129. $list = $provided_toc;
  3130. }
  3131. $type_quiz = false;
  3132. foreach ($list as $toc) {
  3133. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3134. $type_quiz = true;
  3135. }
  3136. }
  3137. if ($type_quiz) {
  3138. $lp_item_id = intval($lp_item_id);
  3139. $lp_view_id = intval($lp_view_id);
  3140. $sql = "SELECT count(*) FROM $lp_item_view_table
  3141. WHERE
  3142. c_id = $course_id AND
  3143. lp_item_id='".$lp_item_id."' AND
  3144. lp_view_id ='".$lp_view_id."' AND
  3145. status='completed'";
  3146. $result = Database::query($sql);
  3147. $row_count = Database:: fetch_row($result);
  3148. $count_item_view = (int)$row_count[0];
  3149. $not_multiple_attempt = 0;
  3150. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3151. $not_multiple_attempt = 1;
  3152. }
  3153. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3154. }
  3155. break;
  3156. }
  3157. $tmp_array = explode('/', $file);
  3158. $document_name = $tmp_array[count($tmp_array) - 1];
  3159. if (strpos($document_name, '_DELETED_')) {
  3160. $file = 'blank.php?error=document_deleted';
  3161. }
  3162. break;
  3163. case 2 :
  3164. if ($this->debug > 2) {
  3165. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3166. }
  3167. if ($lp_item_type != 'dir') {
  3168. // Quite complex here:
  3169. // We want to make sure 'http://' (and similar) links can
  3170. // be loaded as is (withouth the Chamilo path in front) but
  3171. // some contents use this form: resource.htm?resource=http://blablabla
  3172. // which means we have to find a protocol at the path's start, otherwise
  3173. // it should not be considered as an external URL.
  3174. //if ($this->prerequisites_match($item_id)) {
  3175. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3176. if ($this->debug > 2) {
  3177. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3178. }
  3179. // Distant url, return as is.
  3180. $file = $lp_item_path;
  3181. } else {
  3182. if ($this->debug > 2) {
  3183. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3184. }
  3185. // Prevent getting untranslatable urls.
  3186. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3187. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3188. // Prepare the path.
  3189. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3190. // TODO: Fix this for urls with protocol header.
  3191. $file = str_replace('//', '/', $file);
  3192. $file = str_replace(':/', '://', $file);
  3193. if (substr($lp_path, -1) == '/') {
  3194. $lp_path = substr($lp_path, 0, -1);
  3195. }
  3196. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3197. // if file not found.
  3198. $decoded = html_entity_decode($lp_item_path);
  3199. list ($decoded) = explode('?', $decoded);
  3200. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3201. $file = self::rl_get_resource_link_for_learnpath(
  3202. $course_id,
  3203. $this->get_id(),
  3204. $item_id,
  3205. $this->get_view_id()
  3206. );
  3207. if (empty($file)) {
  3208. $file = 'blank.php?error=document_not_found';
  3209. } else {
  3210. $tmp_array = explode('/', $file);
  3211. $document_name = $tmp_array[count($tmp_array) - 1];
  3212. if (strpos($document_name, '_DELETED_')) {
  3213. $file = 'blank.php?error=document_deleted';
  3214. } else {
  3215. $file = 'blank.php?error=document_not_found';
  3216. }
  3217. }
  3218. } else {
  3219. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3220. }
  3221. }
  3222. }
  3223. // We want to use parameters if they were defined in the imsmanifest
  3224. if (strpos($file, 'blank.php') === false) {
  3225. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3226. }
  3227. } else {
  3228. $file = 'lp_content.php?type=dir';
  3229. }
  3230. break;
  3231. case 3 :
  3232. if ($this->debug > 2) {
  3233. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3234. }
  3235. // Formatting AICC HACP append URL.
  3236. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'lp/aicc_hacp.php') . '&';
  3237. if (!empty($lp_item_params)) {
  3238. $aicc_append .= $lp_item_params . '&';
  3239. }
  3240. if ($lp_item_type != 'dir') {
  3241. // Quite complex here:
  3242. // We want to make sure 'http://' (and similar) links can
  3243. // be loaded as is (withouth the Chamilo path in front) but
  3244. // some contents use this form: resource.htm?resource=http://blablabla
  3245. // which means we have to find a protocol at the path's start, otherwise
  3246. // it should not be considered as an external URL.
  3247. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3248. if ($this->debug > 2) {
  3249. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3250. }
  3251. // Distant url, return as is.
  3252. $file = $lp_item_path;
  3253. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3254. /*
  3255. if (stristr($file,'<servername>') !== false) {
  3256. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3257. }
  3258. */
  3259. if (stripos($file, '<servername>') !== false) {
  3260. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3261. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3262. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3263. }
  3264. //
  3265. $file .= $aicc_append;
  3266. } else {
  3267. if ($this->debug > 2) {
  3268. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3269. }
  3270. // Prevent getting untranslatable urls.
  3271. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3272. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3273. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3274. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3275. // TODO: Fix this for urls with protocol header.
  3276. $file = str_replace('//', '/', $file);
  3277. $file = str_replace(':/', '://', $file);
  3278. $file .= $aicc_append;
  3279. }
  3280. } else {
  3281. $file = 'lp_content.php?type=dir';
  3282. }
  3283. break;
  3284. case 4 :
  3285. break;
  3286. default :
  3287. break;
  3288. }
  3289. // Replace &amp; by & because &amp; will break URL with params
  3290. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3291. }
  3292. if ($this->debug > 2) {
  3293. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3294. }
  3295. return $file;
  3296. }
  3297. /**
  3298. * Gets the latest usable view or generate a new one
  3299. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3300. * @return integer DB lp_view id
  3301. */
  3302. public function get_view($attempt_num = 0)
  3303. {
  3304. if ($this->debug > 0) {
  3305. error_log('New LP - In learnpath::get_view()', 0);
  3306. }
  3307. $search = '';
  3308. // Use $attempt_num to enable multi-views management (disabled so far).
  3309. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3310. $search = 'AND view_count = ' . $attempt_num;
  3311. }
  3312. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3313. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3314. $course_id = api_get_course_int_id();
  3315. $sessionId = api_get_session_id();
  3316. $sql = "SELECT id, view_count FROM $lp_view_table
  3317. WHERE
  3318. c_id = " . $course_id . " AND
  3319. lp_id = " . $this->get_id() . " AND
  3320. user_id = " . $this->get_user_id() . " AND
  3321. session_id = $sessionId
  3322. $search
  3323. ORDER BY view_count DESC";
  3324. $res = Database::query($sql);
  3325. if (Database :: num_rows($res) > 0) {
  3326. $row = Database :: fetch_array($res);
  3327. $this->lp_view_id = $row['id'];
  3328. } else if (!api_is_invitee()) {
  3329. // There is no database record, create one.
  3330. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3331. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3332. Database::query($sql);
  3333. $id = Database :: insert_id();
  3334. $this->lp_view_id = $id;
  3335. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3336. Database::query($sql);
  3337. }
  3338. return $this->lp_view_id;
  3339. }
  3340. /**
  3341. * Gets the current view id
  3342. * @return integer View ID (from lp_view)
  3343. */
  3344. public function get_view_id()
  3345. {
  3346. if ($this->debug > 0) {
  3347. error_log('New LP - In learnpath::get_view_id()', 0);
  3348. }
  3349. if (!empty ($this->lp_view_id)) {
  3350. return $this->lp_view_id;
  3351. } else {
  3352. return 0;
  3353. }
  3354. }
  3355. /**
  3356. * Gets the update queue
  3357. * @return array Array containing IDs of items to be updated by JavaScript
  3358. */
  3359. public function get_update_queue()
  3360. {
  3361. if ($this->debug > 0) {
  3362. error_log('New LP - In learnpath::get_update_queue()', 0);
  3363. }
  3364. return $this->update_queue;
  3365. }
  3366. /**
  3367. * Gets the user ID
  3368. * @return integer User ID
  3369. */
  3370. public function get_user_id()
  3371. {
  3372. if ($this->debug > 0) {
  3373. error_log('New LP - In learnpath::get_user_id()', 0);
  3374. }
  3375. if (!empty ($this->user_id)) {
  3376. return $this->user_id;
  3377. } else {
  3378. return false;
  3379. }
  3380. }
  3381. /**
  3382. * Checks if any of the items has an audio element attached
  3383. * @return bool True or false
  3384. */
  3385. public function has_audio()
  3386. {
  3387. if ($this->debug > 1) {
  3388. error_log('New LP - In learnpath::has_audio()', 0);
  3389. }
  3390. $has = false;
  3391. foreach ($this->items as $i => $item) {
  3392. if (!empty ($this->items[$i]->audio)) {
  3393. $has = true;
  3394. break;
  3395. }
  3396. }
  3397. return $has;
  3398. }
  3399. /**
  3400. * Logs a message into a file
  3401. * @param string Message to log
  3402. * @return boolean True on success, false on error or if msg empty
  3403. */
  3404. public function log($msg)
  3405. {
  3406. if ($this->debug > 0) {
  3407. error_log('New LP - In learnpath::log()', 0);
  3408. }
  3409. // TODO
  3410. $this->error .= $msg;
  3411. return true;
  3412. }
  3413. /**
  3414. * Moves an item up and down at its level
  3415. * @param integer Item to move up and down
  3416. * @param string Direction 'up' or 'down'
  3417. * @return integer New display order, or false on error
  3418. */
  3419. public function move_item($id, $direction)
  3420. {
  3421. $course_id = api_get_course_int_id();
  3422. if ($this->debug > 0) {
  3423. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3424. }
  3425. if (empty($id) || empty($direction)) {
  3426. return false;
  3427. }
  3428. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3429. $sql_sel = "SELECT *
  3430. FROM " . $tbl_lp_item . "
  3431. WHERE c_id = ".$course_id." AND id = " . $id;
  3432. $res_sel = Database::query($sql_sel);
  3433. // Check if elem exists.
  3434. if (Database :: num_rows($res_sel) < 1) {
  3435. return false;
  3436. }
  3437. // Gather data.
  3438. $row = Database :: fetch_array($res_sel);
  3439. $previous = $row['previous_item_id'];
  3440. $next = $row['next_item_id'];
  3441. $display = $row['display_order'];
  3442. $parent = $row['parent_item_id'];
  3443. $lp = $row['lp_id'];
  3444. // Update the item (switch with previous/next one).
  3445. switch ($direction) {
  3446. case 'up':
  3447. if ($this->debug > 2) {
  3448. error_log('Movement up detected', 0);
  3449. }
  3450. if ($display <= 1) { /*do nothing*/
  3451. } else {
  3452. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3453. WHERE c_id = ".$course_id." AND id = $previous";
  3454. if ($this->debug > 2) {
  3455. error_log('Selecting previous: ' . $sql_sel2, 0);
  3456. }
  3457. $res_sel2 = Database::query($sql_sel2);
  3458. if (Database :: num_rows($res_sel2) < 1) {
  3459. $previous_previous = 0;
  3460. }
  3461. // Gather data.
  3462. $row2 = Database :: fetch_array($res_sel2);
  3463. $previous_previous = $row2['previous_item_id'];
  3464. // Update previous_previous item (switch "next" with current).
  3465. if ($previous_previous != 0) {
  3466. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3467. next_item_id = $id
  3468. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3469. if ($this->debug > 2) {
  3470. error_log($sql_upd2, 0);
  3471. }
  3472. Database::query($sql_upd2);
  3473. }
  3474. // Update previous item (switch with current).
  3475. if ($previous != 0) {
  3476. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3477. next_item_id = $next,
  3478. previous_item_id = $id,
  3479. display_order = display_order +1
  3480. WHERE c_id = ".$course_id." AND id = $previous";
  3481. if ($this->debug > 2) {
  3482. error_log($sql_upd2, 0);
  3483. }
  3484. Database::query($sql_upd2);
  3485. }
  3486. // Update current item (switch with previous).
  3487. if ($id != 0) {
  3488. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3489. next_item_id = $previous,
  3490. previous_item_id = $previous_previous,
  3491. display_order = display_order-1
  3492. WHERE c_id = ".$course_id." AND id = $id";
  3493. if ($this->debug > 2) {
  3494. error_log($sql_upd2, 0);
  3495. }
  3496. Database::query($sql_upd2);
  3497. }
  3498. // Update next item (new previous item).
  3499. if ($next != 0) {
  3500. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3501. WHERE c_id = ".$course_id." AND id = $next";
  3502. if ($this->debug > 2) {
  3503. error_log($sql_upd2, 0);
  3504. }
  3505. Database::query($sql_upd2);
  3506. }
  3507. $display = $display -1;
  3508. }
  3509. break;
  3510. case 'down':
  3511. if ($this->debug > 2) {
  3512. error_log('Movement down detected', 0);
  3513. }
  3514. if ($next == 0) { /* Do nothing. */
  3515. } else {
  3516. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3517. if ($this->debug > 2) {
  3518. error_log('Selecting next: ' . $sql_sel2, 0);
  3519. }
  3520. $res_sel2 = Database::query($sql_sel2);
  3521. if (Database :: num_rows($res_sel2) < 1) {
  3522. $next_next = 0;
  3523. }
  3524. // Gather data.
  3525. $row2 = Database :: fetch_array($res_sel2);
  3526. $next_next = $row2['next_item_id'];
  3527. // Update previous item (switch with current).
  3528. if ($previous != 0) {
  3529. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3530. WHERE c_id = ".$course_id." AND id = $previous";
  3531. Database::query($sql_upd2);
  3532. }
  3533. // Update current item (switch with previous).
  3534. if ($id != 0) {
  3535. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3536. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3537. WHERE c_id = ".$course_id." AND id = $id";
  3538. Database::query($sql_upd2);
  3539. }
  3540. // Update next item (new previous item).
  3541. if ($next != 0) {
  3542. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3543. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3544. WHERE c_id = ".$course_id." AND id = $next";
  3545. Database::query($sql_upd2);
  3546. }
  3547. // Update next_next item (switch "previous" with current).
  3548. if ($next_next != 0) {
  3549. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3550. previous_item_id = $id
  3551. WHERE c_id = ".$course_id." AND id = $next_next";
  3552. Database::query($sql_upd2);
  3553. }
  3554. $display = $display +1;
  3555. }
  3556. break;
  3557. default :
  3558. return false;
  3559. }
  3560. return $display;
  3561. }
  3562. /**
  3563. * Move a learnpath up (display_order)
  3564. * @param integer $lp_id Learnpath ID
  3565. */
  3566. public static function move_up($lp_id)
  3567. {
  3568. $course_id = api_get_course_int_id();
  3569. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3570. $sql = "SELECT * FROM $lp_table
  3571. WHERE c_id = ".$course_id."
  3572. ORDER BY display_order";
  3573. $res = Database::query($sql);
  3574. if ($res === false)
  3575. return false;
  3576. $lps = array ();
  3577. $lp_order = array ();
  3578. $num = Database :: num_rows($res);
  3579. // First check the order is correct, globally (might be wrong because
  3580. // of versions < 1.8.4)
  3581. if ($num > 0) {
  3582. $i = 1;
  3583. while ($row = Database :: fetch_array($res)) {
  3584. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3585. $need_fix = true;
  3586. $sql_u = "UPDATE $lp_table SET display_order = $i
  3587. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3588. Database::query($sql_u);
  3589. }
  3590. $row['display_order'] = $i;
  3591. $lps[$row['id']] = $row;
  3592. $lp_order[$i] = $row['id'];
  3593. $i++;
  3594. }
  3595. }
  3596. if ($num > 1) { // If there's only one element, no need to sort.
  3597. $order = $lps[$lp_id]['display_order'];
  3598. if ($order > 1) { // If it's the first element, no need to move up.
  3599. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3600. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3601. Database::query($sql_u1);
  3602. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3603. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3604. Database::query($sql_u2);
  3605. }
  3606. }
  3607. }
  3608. /**
  3609. * Move a learnpath down (display_order)
  3610. * @param integer $lp_id Learnpath ID
  3611. */
  3612. public static function move_down($lp_id)
  3613. {
  3614. $course_id = api_get_course_int_id();
  3615. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3616. $sql = "SELECT * FROM $lp_table
  3617. WHERE c_id = ".$course_id."
  3618. ORDER BY display_order";
  3619. $res = Database::query($sql);
  3620. if ($res === false) {
  3621. return false;
  3622. }
  3623. $lps = array();
  3624. $lp_order = array();
  3625. $num = Database :: num_rows($res);
  3626. $max = 0;
  3627. // First check the order is correct, globally (might be wrong because
  3628. // of versions < 1.8.4).
  3629. if ($num > 0) {
  3630. $i = 1;
  3631. while ($row = Database :: fetch_array($res)) {
  3632. $max = $i;
  3633. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3634. $need_fix = true;
  3635. $sql_u = "UPDATE $lp_table SET display_order = $i
  3636. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3637. Database::query($sql_u);
  3638. }
  3639. $row['display_order'] = $i;
  3640. $lps[$row['id']] = $row;
  3641. $lp_order[$i] = $row['id'];
  3642. $i++;
  3643. }
  3644. }
  3645. if ($num > 1) { // If there's only one element, no need to sort.
  3646. $order = $lps[$lp_id]['display_order'];
  3647. if ($order < $max) { // If it's the first element, no need to move up.
  3648. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3649. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3650. Database::query($sql_u1);
  3651. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3652. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3653. Database::query($sql_u2);
  3654. }
  3655. }
  3656. }
  3657. /**
  3658. * Updates learnpath attributes to point to the next element
  3659. * The last part is similar to set_current_item but processing the other way around
  3660. */
  3661. public function next()
  3662. {
  3663. if ($this->debug > 0) {
  3664. error_log('New LP - In learnpath::next()', 0);
  3665. }
  3666. $this->last = $this->get_current_item_id();
  3667. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3668. $this->autocomplete_parents($this->last);
  3669. $new_index = $this->get_next_index();
  3670. if ($this->debug > 2) {
  3671. error_log('New LP - New index: ' . $new_index, 0);
  3672. }
  3673. $this->index = $new_index;
  3674. if ($this->debug > 2) {
  3675. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3676. }
  3677. $this->current = $this->ordered_items[$new_index];
  3678. if ($this->debug > 2) {
  3679. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3680. }
  3681. }
  3682. /**
  3683. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3684. * class, this might be redefined to allow several behaviours depending on the document type.
  3685. * @param integer Resource ID
  3686. * @return boolean True on success, false otherwise
  3687. */
  3688. public function open($id)
  3689. {
  3690. if ($this->debug > 0) {
  3691. error_log('New LP - In learnpath::open()', 0);
  3692. }
  3693. // TODO:
  3694. // set the current resource attribute to this resource
  3695. // switch on element type (redefine in child class?)
  3696. // set status for this item to "opened"
  3697. // start timer
  3698. // initialise score
  3699. $this->index = 0; //or = the last item seen (see $this->last)
  3700. }
  3701. /**
  3702. * Check that all prerequisites are fulfilled. Returns true and an
  3703. * empty string on success, returns false
  3704. * and the prerequisite string on error.
  3705. * This function is based on the rules for aicc_script language as
  3706. * described in the SCORM 1.2 CAM documentation page 108.
  3707. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3708. * @return boolean True if prerequisites are matched, false otherwise -
  3709. * Empty string if true returned, prerequisites string otherwise.
  3710. */
  3711. public function prerequisites_match($itemId = null)
  3712. {
  3713. $debug = $this->debug;
  3714. if ($debug > 0) {
  3715. error_log('In learnpath::prerequisites_match()', 0);
  3716. }
  3717. if (empty($itemId)) {
  3718. $itemId = $this->current;
  3719. }
  3720. $currentItem = $this->getItem($itemId);
  3721. if ($currentItem) {
  3722. if ($this->type == 2) {
  3723. // Getting prereq from scorm
  3724. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3725. } else {
  3726. $prereq_string = $currentItem->get_prereq_string();
  3727. }
  3728. if (empty($prereq_string)) {
  3729. if ($debug > 0) {
  3730. error_log('Found prereq_string is empty return true');
  3731. }
  3732. return true;
  3733. }
  3734. // Clean spaces.
  3735. $prereq_string = str_replace(' ', '', $prereq_string);
  3736. if ($debug > 0) {
  3737. error_log('Found prereq_string: ' . $prereq_string, 0);
  3738. }
  3739. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3740. $result = $currentItem->parse_prereq(
  3741. $prereq_string,
  3742. $this->items,
  3743. $this->refs_list,
  3744. $this->get_user_id()
  3745. );
  3746. if ($result === false) {
  3747. $this->set_error_msg($currentItem->prereq_alert);
  3748. }
  3749. } else {
  3750. $result = true;
  3751. if ($debug > 1) {
  3752. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3753. }
  3754. }
  3755. if ($debug > 1) {
  3756. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3757. }
  3758. return $result;
  3759. }
  3760. /**
  3761. * Updates learnpath attributes to point to the previous element
  3762. * The last part is similar to set_current_item but processing the other way around
  3763. */
  3764. public function previous()
  3765. {
  3766. if ($this->debug > 0) {
  3767. error_log('New LP - In learnpath::previous()', 0);
  3768. }
  3769. $this->last = $this->get_current_item_id();
  3770. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3771. $this->autocomplete_parents($this->last);
  3772. $new_index = $this->get_previous_index();
  3773. $this->index = $new_index;
  3774. $this->current = $this->ordered_items[$new_index];
  3775. }
  3776. /**
  3777. * Publishes a learnpath. This basically means show or hide the learnpath
  3778. * to normal users.
  3779. * Can be used as abstract
  3780. * @param integer Learnpath ID
  3781. * @param string New visibility
  3782. */
  3783. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3784. {
  3785. $action = 'visible';
  3786. if ($set_visibility != 1) {
  3787. $action = 'invisible';
  3788. self::toggle_publish($lp_id, 'i');
  3789. }
  3790. return api_item_property_update(
  3791. api_get_course_info(),
  3792. TOOL_LEARNPATH,
  3793. $lp_id,
  3794. $action,
  3795. api_get_user_id()
  3796. );
  3797. }
  3798. /**
  3799. * Publishes a learnpath. This basically means show or hide the learnpath
  3800. * on the course homepage
  3801. * Can be used as abstract
  3802. * @param integer $lp_id Learnpath id
  3803. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3804. * @return bool
  3805. */
  3806. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3807. {
  3808. $course_id = api_get_course_int_id();
  3809. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3810. $lp_id = intval($lp_id);
  3811. $sql = "SELECT * FROM $tbl_lp
  3812. WHERE c_id = $course_id AND id = $lp_id";
  3813. $result = Database::query($sql);
  3814. if (Database::num_rows($result)) {
  3815. $row = Database :: fetch_array($result);
  3816. $name = domesticate($row['name']);
  3817. if ($set_visibility == 'i') {
  3818. $v = 0;
  3819. }
  3820. if ($set_visibility == 'v') {
  3821. $v = 1;
  3822. }
  3823. $session_id = api_get_session_id();
  3824. $session_condition = api_get_session_condition($session_id);
  3825. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3826. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3827. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3828. $sql = "SELECT * FROM $tbl_tool
  3829. WHERE
  3830. c_id = $course_id AND
  3831. (link = '$link' OR link = '$oldLink') AND
  3832. image = 'scormbuilder.gif' AND
  3833. (
  3834. link LIKE '$link%' OR
  3835. link LIKE '$oldLink%'
  3836. )
  3837. $session_condition
  3838. ";
  3839. $result = Database::query($sql);
  3840. $num = Database :: num_rows($result);
  3841. if ($set_visibility == 'i' && $num > 0) {
  3842. $sql = "DELETE FROM $tbl_tool
  3843. WHERE
  3844. c_id = $course_id AND
  3845. (link = '$link' OR link = '$oldLink') AND
  3846. image='scormbuilder.gif'
  3847. $session_condition";
  3848. Database::query($sql);
  3849. } elseif ($set_visibility == 'v' && $num == 0) {
  3850. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3851. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3852. Database::query($sql);
  3853. $insertId = Database::insert_id();
  3854. if ($insertId) {
  3855. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3856. Database::query($sql);
  3857. }
  3858. } elseif ($set_visibility == 'v' && $num > 0) {
  3859. $sql = "UPDATE $tbl_tool SET
  3860. c_id = $course_id,
  3861. name = '$name',
  3862. link = '$link',
  3863. image = 'scormbuilder.gif',
  3864. visibility = '$v',
  3865. admin = '0',
  3866. address = 'pastillegris.gif',
  3867. added_tool = 0,
  3868. session_id = $session_id
  3869. WHERE
  3870. c_id = ".$course_id." AND
  3871. (link='$link' and image='scormbuilder.gif' $session_condition)
  3872. ";
  3873. Database::query($sql);
  3874. } else {
  3875. // Parameter and database incompatible, do nothing, exit.
  3876. return false;
  3877. }
  3878. } else {
  3879. return false;
  3880. }
  3881. }
  3882. /**
  3883. * Restart the whole learnpath. Return the URL of the first element.
  3884. * Make sure the results are saved with anoter method. This method should probably be
  3885. * redefined in children classes.
  3886. * To use a similar method statically, use the create_new_attempt() method
  3887. * @return string URL to load in the viewer
  3888. */
  3889. public function restart()
  3890. {
  3891. if ($this->debug > 0) {
  3892. error_log('New LP - In learnpath::restart()', 0);
  3893. }
  3894. // TODO
  3895. // Call autosave method to save the current progress.
  3896. //$this->index = 0;
  3897. if (api_is_invitee()) {
  3898. return false;
  3899. }
  3900. $session_id = api_get_session_id();
  3901. $course_id = api_get_course_int_id();
  3902. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3903. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3904. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3905. if ($this->debug > 2) {
  3906. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3907. }
  3908. Database::query($sql);
  3909. $view_id = Database::insert_id();
  3910. if ($view_id) {
  3911. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3912. Database::query($sql);
  3913. $this->lp_view_id = $view_id;
  3914. $this->attempt = $this->attempt + 1;
  3915. } else {
  3916. $this->error = 'Could not insert into item_view table...';
  3917. return false;
  3918. }
  3919. $this->autocomplete_parents($this->current);
  3920. foreach ($this->items as $index => $dummy) {
  3921. $this->items[$index]->restart();
  3922. $this->items[$index]->set_lp_view($this->lp_view_id);
  3923. }
  3924. $this->first();
  3925. return true;
  3926. }
  3927. /**
  3928. * Saves the current item
  3929. * @return boolean
  3930. */
  3931. public function save_current()
  3932. {
  3933. if ($this->debug > 0) {
  3934. error_log('learnpath::save_current()', 0);
  3935. }
  3936. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3937. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3938. if ($this->debug > 2) {
  3939. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3940. }
  3941. if ($this->debug > 2) {
  3942. error_log('' . print_r($this->items, true), 0);
  3943. }
  3944. if (isset($this->items[$this->current]) &&
  3945. is_object($this->items[$this->current])
  3946. ) {
  3947. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3948. $this->autocomplete_parents($this->current);
  3949. $status = $this->items[$this->current]->get_status();
  3950. $this->update_queue[$this->current] = $status;
  3951. return $res;
  3952. }
  3953. return false;
  3954. }
  3955. /**
  3956. * Saves the given item
  3957. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3958. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3959. * @return boolean
  3960. */
  3961. public function save_item($item_id = null, $from_outside = true)
  3962. {
  3963. $debug = $this->debug;
  3964. if ($debug) {
  3965. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3966. }
  3967. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3968. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3969. if (empty($item_id)) {
  3970. $item_id = intval($_REQUEST['id']);
  3971. }
  3972. if (empty($item_id)) {
  3973. $item_id = $this->get_current_item_id();
  3974. }
  3975. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3976. if ($debug) {
  3977. error_log('Object exists');
  3978. }
  3979. // Saving the item.
  3980. $res = $this->items[$item_id]->save(
  3981. $from_outside,
  3982. $this->prerequisites_match($item_id)
  3983. );
  3984. if ($debug) {
  3985. error_log('update_queue before:');
  3986. error_log(print_r($this->update_queue,1));
  3987. }
  3988. $this->autocomplete_parents($item_id);
  3989. $status = $this->items[$item_id]->get_status();
  3990. $this->update_queue[$item_id] = $status;
  3991. if ($debug) {
  3992. error_log('get_status(): ' . $status);
  3993. error_log('update_queue after:');
  3994. error_log(print_r($this->update_queue,1));
  3995. }
  3996. return $res;
  3997. }
  3998. return false;
  3999. }
  4000. /**
  4001. * Saves the last item seen's ID only in case
  4002. */
  4003. public function save_last()
  4004. {
  4005. $course_id = api_get_course_int_id();
  4006. if ($this->debug > 0) {
  4007. error_log('New LP - In learnpath::save_last()', 0);
  4008. }
  4009. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4010. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4011. if (isset($this->current) && !api_is_invitee()) {
  4012. if ($this->debug > 2) {
  4013. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4014. }
  4015. $sql = "UPDATE $table SET
  4016. last_item = " . intval($this->get_current_item_id()). "
  4017. WHERE
  4018. c_id = $course_id AND
  4019. lp_id = " . $this->get_id() . " AND
  4020. user_id = " . $this->get_user_id()." ".$session_condition;
  4021. if ($this->debug > 2) {
  4022. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4023. }
  4024. Database::query($sql);
  4025. }
  4026. if (!api_is_invitee()) {
  4027. // Save progress.
  4028. list($progress, $text) = $this->get_progress_bar_text('%');
  4029. if ($progress >= 0 && $progress <= 100) {
  4030. $progress = (int) $progress;
  4031. $sql = "UPDATE $table SET
  4032. progress = $progress
  4033. WHERE
  4034. c_id = ".$course_id." AND
  4035. lp_id = " . $this->get_id() . " AND
  4036. user_id = " . $this->get_user_id()." ".$session_condition;
  4037. // Ignore errors as some tables might not have the progress field just yet.
  4038. Database::query($sql);
  4039. $this->progress_db = $progress;
  4040. }
  4041. }
  4042. }
  4043. /**
  4044. * Sets the current item ID (checks if valid and authorized first)
  4045. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4046. */
  4047. public function set_current_item($item_id = null)
  4048. {
  4049. if ($this->debug > 0) {
  4050. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4051. }
  4052. if (empty ($item_id)) {
  4053. if ($this->debug > 2) {
  4054. error_log('New LP - No new current item given, ignore...', 0);
  4055. }
  4056. // Do nothing.
  4057. } else {
  4058. if ($this->debug > 2) {
  4059. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4060. }
  4061. if (is_numeric($item_id)) {
  4062. $item_id = intval($item_id);
  4063. // TODO: Check in database here.
  4064. $this->last = $this->current;
  4065. $this->current = $item_id;
  4066. // TODO: Update $this->index as well.
  4067. foreach ($this->ordered_items as $index => $item) {
  4068. if ($item == $this->current) {
  4069. $this->index = $index;
  4070. break;
  4071. }
  4072. }
  4073. if ($this->debug > 2) {
  4074. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4075. }
  4076. } else {
  4077. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4078. }
  4079. }
  4080. }
  4081. /**
  4082. * Sets the encoding
  4083. * @param string New encoding
  4084. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4085. */
  4086. public function set_encoding($enc = 'UTF-8')
  4087. {
  4088. if ($this->debug > 0) {
  4089. error_log('New LP - In learnpath::set_encoding()', 0);
  4090. }
  4091. $course_id = api_get_course_int_id();
  4092. $enc = api_refine_encoding_id($enc);
  4093. if (empty($enc)) {
  4094. $enc = api_get_system_encoding();
  4095. }
  4096. if (api_is_encoding_supported($enc)) {
  4097. $lp = $this->get_id();
  4098. if ($lp != 0) {
  4099. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4100. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4101. WHERE c_id = ".$course_id." AND id = " . $lp;
  4102. $res = Database::query($sql);
  4103. return $res;
  4104. }
  4105. }
  4106. return false;
  4107. }
  4108. /**
  4109. * Sets the JS lib setting in the database directly.
  4110. * This is the JavaScript library file this lp needs to load on startup
  4111. * @param string Proximity setting
  4112. * @return boolean True on update success. False otherwise.
  4113. */
  4114. public function set_jslib($lib = '')
  4115. {
  4116. if ($this->debug > 0) {
  4117. error_log('New LP - In learnpath::set_jslib()', 0);
  4118. }
  4119. $lp = $this->get_id();
  4120. $course_id = api_get_course_int_id();
  4121. if ($lp != 0) {
  4122. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4123. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4124. WHERE c_id = ".$course_id." AND id = " . $lp;
  4125. $res = Database::query($sql);
  4126. return $res;
  4127. } else {
  4128. return false;
  4129. }
  4130. }
  4131. /**
  4132. * Sets the name of the LP maker (publisher) (and save)
  4133. * @param string Optional string giving the new content_maker of this learnpath
  4134. * @return boolean True
  4135. */
  4136. public function set_maker($name = '')
  4137. {
  4138. if ($this->debug > 0) {
  4139. error_log('New LP - In learnpath::set_maker()', 0);
  4140. }
  4141. if (empty ($name))
  4142. return false;
  4143. $this->maker = $name;
  4144. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4145. $course_id = api_get_course_int_id();
  4146. $lp_id = $this->get_id();
  4147. $sql = "UPDATE $lp_table SET
  4148. content_maker = '" . Database::escape_string($this->maker) . "'
  4149. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4150. if ($this->debug > 2) {
  4151. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4152. }
  4153. Database::query($sql);
  4154. return true;
  4155. }
  4156. /**
  4157. * Sets the name of the current learnpath (and save)
  4158. * @param string $name Optional string giving the new name of this learnpath
  4159. * @return boolean True/False
  4160. */
  4161. public function set_name($name = null)
  4162. {
  4163. if ($this->debug > 0) {
  4164. error_log('New LP - In learnpath::set_name()', 0);
  4165. }
  4166. if (empty($name)) {
  4167. return false;
  4168. }
  4169. $this->name = Database::escape_string($name);
  4170. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4171. $lp_id = $this->get_id();
  4172. $course_id = $this->course_info['real_id'];
  4173. $sql = "UPDATE $lp_table SET
  4174. name = '" . Database::escape_string($this->name). "'
  4175. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4176. if ($this->debug > 2) {
  4177. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4178. }
  4179. $result = Database::query($sql);
  4180. // If the lp is visible on the homepage, change his name there.
  4181. if (Database::affected_rows($result)) {
  4182. $session_id = api_get_session_id();
  4183. $session_condition = api_get_session_condition($session_id);
  4184. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4185. $link = 'lp/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4186. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4187. WHERE
  4188. c_id = $course_id AND
  4189. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4190. Database::query($sql);
  4191. return true;
  4192. } else {
  4193. return false;
  4194. }
  4195. }
  4196. /**
  4197. * Set index specified prefix terms for all items in this path
  4198. * @param string Comma-separated list of terms
  4199. * @param char Xapian term prefix
  4200. * @return boolean False on error, true otherwise
  4201. */
  4202. public function set_terms_by_prefix($terms_string, $prefix)
  4203. {
  4204. $course_id = api_get_course_int_id();
  4205. if (api_get_setting('search_enabled') !== 'true')
  4206. return false;
  4207. if (!extension_loaded('xapian')) {
  4208. return false;
  4209. }
  4210. $terms_string = trim($terms_string);
  4211. $terms = explode(',', $terms_string);
  4212. array_walk($terms, 'trim_value');
  4213. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4214. // Don't do anything if no change, verify only at DB, not the search engine.
  4215. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4216. return false;
  4217. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4218. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4219. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4220. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4221. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4222. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4223. $lp_id = intval($_POST['lp_id']);
  4224. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4225. $result = Database::query($sql);
  4226. $di = new ChamiloIndexer();
  4227. while ($lp_item = Database :: fetch_array($result)) {
  4228. // Get search_did.
  4229. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4230. $sql = 'SELECT * FROM %s
  4231. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4232. LIMIT 1';
  4233. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4234. //echo $sql; echo '<br>';
  4235. $res = Database::query($sql);
  4236. if (Database::num_rows($res) > 0) {
  4237. $se_ref = Database :: fetch_array($res);
  4238. // Compare terms.
  4239. $doc = $di->get_document($se_ref['search_did']);
  4240. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4241. $xterms = array();
  4242. foreach ($xapian_terms as $xapian_term) {
  4243. $xterms[] = substr($xapian_term['name'], 1);
  4244. }
  4245. $dterms = $terms;
  4246. $missing_terms = array_diff($dterms, $xterms);
  4247. $deprecated_terms = array_diff($xterms, $dterms);
  4248. // Save it to search engine.
  4249. foreach ($missing_terms as $term) {
  4250. $doc->add_term($prefix . $term, 1);
  4251. }
  4252. foreach ($deprecated_terms as $term) {
  4253. $doc->remove_term($prefix . $term);
  4254. }
  4255. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4256. $di->getDb()->flush();
  4257. } else {
  4258. //@todo What we should do here?
  4259. }
  4260. }
  4261. return true;
  4262. }
  4263. /**
  4264. * Sets the theme of the LP (local/remote) (and save)
  4265. * @param string Optional string giving the new theme of this learnpath
  4266. * @return bool Returns true if theme name is not empty
  4267. */
  4268. public function set_theme($name = '')
  4269. {
  4270. $course_id = api_get_course_int_id();
  4271. if ($this->debug > 0) {
  4272. error_log('New LP - In learnpath::set_theme()', 0);
  4273. }
  4274. $this->theme = $name;
  4275. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4276. $lp_id = $this->get_id();
  4277. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4278. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4279. if ($this->debug > 2) {
  4280. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4281. }
  4282. Database::query($sql);
  4283. return true;
  4284. }
  4285. /**
  4286. * Sets the image of an LP (and save)
  4287. * @param string Optional string giving the new image of this learnpath
  4288. * @return bool Returns true if theme name is not empty
  4289. */
  4290. public function set_preview_image($name = '')
  4291. {
  4292. $course_id = api_get_course_int_id();
  4293. if ($this->debug > 0) {
  4294. error_log('New LP - In learnpath::set_preview_image()', 0);
  4295. }
  4296. $this->preview_image = $name;
  4297. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4298. $lp_id = $this->get_id();
  4299. $sql = "UPDATE $lp_table SET
  4300. preview_image = '" . Database::escape_string($this->preview_image). "'
  4301. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4302. if ($this->debug > 2) {
  4303. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4304. }
  4305. Database::query($sql);
  4306. return true;
  4307. }
  4308. /**
  4309. * Sets the author of a LP (and save)
  4310. * @param string Optional string giving the new author of this learnpath
  4311. * @return bool Returns true if author's name is not empty
  4312. */
  4313. public function set_author($name = '')
  4314. {
  4315. $course_id = api_get_course_int_id();
  4316. if ($this->debug > 0) {
  4317. error_log('New LP - In learnpath::set_author()', 0);
  4318. }
  4319. $this->author = $name;
  4320. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4321. $lp_id = $this->get_id();
  4322. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4323. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4324. if ($this->debug > 2) {
  4325. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4326. }
  4327. Database::query($sql);
  4328. return true;
  4329. }
  4330. /**
  4331. * Sets the hide_toc_frame parameter of a LP (and save)
  4332. * @param int 1 if frame is hidden 0 then else
  4333. * @return bool Returns true if author's name is not empty
  4334. */
  4335. public function set_hide_toc_frame($hide)
  4336. {
  4337. $course_id = api_get_course_int_id();
  4338. if ($this->debug > 0) {
  4339. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4340. }
  4341. if (intval($hide) == $hide) {
  4342. $this->hide_toc_frame = $hide;
  4343. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4344. $lp_id = $this->get_id();
  4345. $sql = "UPDATE $lp_table SET
  4346. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4347. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4348. if ($this->debug > 2) {
  4349. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4350. }
  4351. Database::query($sql);
  4352. return true;
  4353. } else {
  4354. return false;
  4355. }
  4356. }
  4357. /**
  4358. * Sets the prerequisite of a LP (and save)
  4359. * @param int integer giving the new prerequisite of this learnpath
  4360. * @return bool returns true if prerequisite is not empty
  4361. */
  4362. public function set_prerequisite($prerequisite)
  4363. {
  4364. $course_id = api_get_course_int_id();
  4365. if ($this->debug > 0) {
  4366. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4367. }
  4368. $this->prerequisite = intval($prerequisite);
  4369. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4370. $lp_id = $this->get_id();
  4371. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4372. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4373. if ($this->debug > 2) {
  4374. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4375. }
  4376. Database::query($sql);
  4377. return true;
  4378. }
  4379. /**
  4380. * Sets the location/proximity of the LP (local/remote) (and save)
  4381. * @param string Optional string giving the new location of this learnpath
  4382. * @return boolean True on success / False on error
  4383. */
  4384. public function set_proximity($name = '')
  4385. {
  4386. $course_id = api_get_course_int_id();
  4387. if ($this->debug > 0) {
  4388. error_log('New LP - In learnpath::set_proximity()', 0);
  4389. }
  4390. if (empty ($name))
  4391. return false;
  4392. $this->proximity = $name;
  4393. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4394. $lp_id = $this->get_id();
  4395. $sql = "UPDATE $lp_table SET
  4396. content_local = '" . Database::escape_string($name) . "'
  4397. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4398. if ($this->debug > 2) {
  4399. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4400. }
  4401. Database::query($sql);
  4402. return true;
  4403. }
  4404. /**
  4405. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4406. * @param integer DB ID of the item
  4407. */
  4408. public function set_previous_item($id)
  4409. {
  4410. if ($this->debug > 0) {
  4411. error_log('New LP - In learnpath::set_previous_item()', 0);
  4412. }
  4413. $this->last = $id;
  4414. }
  4415. /**
  4416. * Sets use_max_score
  4417. * @param string $use_max_score Optional string giving the new location of this learnpath
  4418. * @return boolean True on success / False on error
  4419. */
  4420. public function set_use_max_score($use_max_score = 1)
  4421. {
  4422. $course_id = api_get_course_int_id();
  4423. if ($this->debug > 0) {
  4424. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4425. }
  4426. $use_max_score = intval($use_max_score);
  4427. $this->use_max_score = $use_max_score;
  4428. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4429. $lp_id = $this->get_id();
  4430. $sql = "UPDATE $lp_table SET
  4431. use_max_score = '" . $this->use_max_score . "'
  4432. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4433. if ($this->debug > 2) {
  4434. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4435. }
  4436. Database::query($sql);
  4437. return true;
  4438. }
  4439. /**
  4440. * Sets and saves the expired_on date
  4441. * @param string $expired_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_expired_on($expired_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($expired_on)) {
  4451. $this->expired_on = api_get_utc_datetime($expired_on);
  4452. } else {
  4453. $this->expired_on = null;
  4454. }
  4455. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4456. $lp_id = $this->get_id();
  4457. if ($this->debug > 2) {
  4458. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4459. }
  4460. $params = [
  4461. 'expired_on' => $this->expired_on,
  4462. ];
  4463. Database::update($lp_table, $params, ['c_id = ? AND id = ?' => [$course_id, $lp_id]]);
  4464. return true;
  4465. }
  4466. /**
  4467. * Sets and saves the publicated_on date
  4468. * @param string $publicated_on Optional string giving the new author of this learnpath
  4469. * @return bool Returns true if author's name is not empty
  4470. */
  4471. public function set_publicated_on($publicated_on)
  4472. {
  4473. $course_id = api_get_course_int_id();
  4474. if ($this->debug > 0) {
  4475. error_log('New LP - In learnpath::set_expired_on()', 0);
  4476. }
  4477. if (!empty($publicated_on)) {
  4478. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4479. } else {
  4480. $this->publicated_on = '';
  4481. }
  4482. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4483. $lp_id = $this->get_id();
  4484. $sql = "UPDATE $lp_table SET
  4485. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4486. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4487. if ($this->debug > 2) {
  4488. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4489. }
  4490. Database::query($sql);
  4491. return true;
  4492. }
  4493. /**
  4494. * Sets and saves the expired_on date
  4495. * @return bool Returns true if author's name is not empty
  4496. */
  4497. public function set_modified_on()
  4498. {
  4499. $course_id = api_get_course_int_id();
  4500. if ($this->debug > 0) {
  4501. error_log('New LP - In learnpath::set_expired_on()', 0);
  4502. }
  4503. $this->modified_on = api_get_utc_datetime();
  4504. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4505. $lp_id = $this->get_id();
  4506. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4507. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4508. if ($this->debug > 2) {
  4509. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4510. }
  4511. Database::query($sql);
  4512. return true;
  4513. }
  4514. /**
  4515. * Sets the object's error message
  4516. * @param string Error message. If empty, reinits the error string
  4517. * @return void
  4518. */
  4519. public function set_error_msg($error = '')
  4520. {
  4521. if ($this->debug > 0) {
  4522. error_log('New LP - In learnpath::set_error_msg()', 0);
  4523. }
  4524. if (empty ($error)) {
  4525. $this->error = '';
  4526. } else {
  4527. $this->error .= $error;
  4528. }
  4529. }
  4530. /**
  4531. * Launches the current item if not 'sco'
  4532. * (starts timer and make sure there is a record ready in the DB)
  4533. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4534. * @return boolean
  4535. */
  4536. public function start_current_item($allow_new_attempt = false)
  4537. {
  4538. if ($this->debug > 0) {
  4539. error_log('New LP - In learnpath::start_current_item()', 0);
  4540. }
  4541. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4542. $type = $this->get_type();
  4543. $item_type = $this->items[$this->current]->get_type();
  4544. if (($type == 2 && $item_type != 'sco') ||
  4545. ($type == 3 && $item_type != 'au') ||
  4546. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4547. ) {
  4548. $this->items[$this->current]->open($allow_new_attempt);
  4549. $this->autocomplete_parents($this->current);
  4550. $prereq_check = $this->prerequisites_match($this->current);
  4551. $this->items[$this->current]->save(false, $prereq_check);
  4552. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4553. } else {
  4554. // If sco, then it is supposed to have been updated by some other call.
  4555. }
  4556. if ($item_type == 'sco') {
  4557. $this->items[$this->current]->restart();
  4558. }
  4559. }
  4560. if ($this->debug > 0) {
  4561. error_log('New LP - End of learnpath::start_current_item()', 0);
  4562. }
  4563. return true;
  4564. }
  4565. /**
  4566. * Stops the processing and counters for the old item (as held in $this->last)
  4567. * @return boolean True/False
  4568. */
  4569. public function stop_previous_item()
  4570. {
  4571. if ($this->debug > 0) {
  4572. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4573. }
  4574. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4575. if ($this->debug > 2) {
  4576. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4577. }
  4578. switch ($this->get_type()) {
  4579. case '3':
  4580. if ($this->items[$this->last]->get_type() != 'au') {
  4581. if ($this->debug > 2) {
  4582. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4583. }
  4584. $this->items[$this->last]->close();
  4585. //$this->autocomplete_parents($this->last);
  4586. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4587. } else {
  4588. if ($this->debug > 2) {
  4589. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4590. }
  4591. }
  4592. case '2':
  4593. if ($this->items[$this->last]->get_type() != 'sco') {
  4594. if ($this->debug > 2) {
  4595. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4596. }
  4597. $this->items[$this->last]->close();
  4598. //$this->autocomplete_parents($this->last);
  4599. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4600. } else {
  4601. if ($this->debug > 2) {
  4602. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4603. }
  4604. }
  4605. break;
  4606. case '1':
  4607. default:
  4608. if ($this->debug > 2) {
  4609. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4610. }
  4611. $this->items[$this->last]->close();
  4612. break;
  4613. }
  4614. } else {
  4615. if ($this->debug > 2) {
  4616. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4617. }
  4618. return false;
  4619. }
  4620. return true;
  4621. }
  4622. /**
  4623. * Updates the default view mode from fullscreen to embedded and inversely
  4624. * @return string The current default view mode ('fullscreen' or 'embedded')
  4625. */
  4626. public function update_default_view_mode()
  4627. {
  4628. $course_id = api_get_course_int_id();
  4629. if ($this->debug > 0) {
  4630. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4631. }
  4632. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4633. $sql = "SELECT * FROM $lp_table
  4634. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4635. $res = Database::query($sql);
  4636. if (Database :: num_rows($res) > 0) {
  4637. $row = Database :: fetch_array($res);
  4638. $default_view_mode = $row['default_view_mod'];
  4639. $view_mode = $default_view_mode;
  4640. switch ($default_view_mode) {
  4641. case 'fullscreen': // default with popup
  4642. $view_mode = 'embedded';
  4643. break;
  4644. case 'embedded': // default view with left menu
  4645. $view_mode = 'embedframe';
  4646. break;
  4647. case 'embedframe': //folded menu
  4648. $view_mode = 'impress';
  4649. break;
  4650. case 'impress':
  4651. $view_mode = 'fullscreen';
  4652. break;
  4653. }
  4654. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4655. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4656. Database::query($sql);
  4657. $this->mode = $view_mode;
  4658. return $view_mode;
  4659. } else {
  4660. if ($this->debug > 2) {
  4661. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4662. }
  4663. }
  4664. return -1;
  4665. }
  4666. /**
  4667. * Updates the default behaviour about auto-commiting SCORM updates
  4668. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4669. */
  4670. public function update_default_scorm_commit()
  4671. {
  4672. $course_id = api_get_course_int_id();
  4673. if ($this->debug > 0) {
  4674. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4675. }
  4676. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4677. $sql = "SELECT * FROM $lp_table
  4678. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4679. $res = Database::query($sql);
  4680. if (Database :: num_rows($res) > 0) {
  4681. $row = Database :: fetch_array($res);
  4682. $force = $row['force_commit'];
  4683. if ($force == 1) {
  4684. $force = 0;
  4685. $force_return = false;
  4686. } elseif ($force == 0) {
  4687. $force = 1;
  4688. $force_return = true;
  4689. }
  4690. $sql = "UPDATE $lp_table SET force_commit = $force
  4691. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4692. Database::query($sql);
  4693. $this->force_commit = $force_return;
  4694. return $force_return;
  4695. } else {
  4696. if ($this->debug > 2) {
  4697. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4698. }
  4699. }
  4700. return -1;
  4701. }
  4702. /**
  4703. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4704. * @return bool True on success, false on failure
  4705. */
  4706. public function update_display_order()
  4707. {
  4708. $course_id = api_get_course_int_id();
  4709. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4710. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4711. $res = Database::query($sql);
  4712. if ($res === false)
  4713. return false;
  4714. $num = Database :: num_rows($res);
  4715. // First check the order is correct, globally (might be wrong because
  4716. // of versions < 1.8.4).
  4717. if ($num > 0) {
  4718. $i = 1;
  4719. while ($row = Database :: fetch_array($res)) {
  4720. if ($row['display_order'] != $i) {
  4721. // If we find a gap in the order, we need to fix it.
  4722. $sql = "UPDATE $lp_table SET display_order = $i
  4723. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4724. Database::query($sql);
  4725. }
  4726. $i++;
  4727. }
  4728. }
  4729. return true;
  4730. }
  4731. /**
  4732. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4733. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4734. */
  4735. public function update_reinit()
  4736. {
  4737. $course_id = api_get_course_int_id();
  4738. if ($this->debug > 0) {
  4739. error_log('New LP - In learnpath::update_reinit()', 0);
  4740. }
  4741. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4742. $sql = "SELECT * FROM $lp_table
  4743. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4744. $res = Database::query($sql);
  4745. if (Database :: num_rows($res) > 0) {
  4746. $row = Database :: fetch_array($res);
  4747. $force = $row['prevent_reinit'];
  4748. if ($force == 1) {
  4749. $force = 0;
  4750. } elseif ($force == 0) {
  4751. $force = 1;
  4752. }
  4753. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4754. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4755. Database::query($sql);
  4756. $this->prevent_reinit = $force;
  4757. return $force;
  4758. } else {
  4759. if ($this->debug > 2) {
  4760. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4761. }
  4762. }
  4763. return -1;
  4764. }
  4765. /**
  4766. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4767. *
  4768. * @return string 'single', 'multi' or 'seriousgame'
  4769. * @author ndiechburg <noel@cblue.be>
  4770. **/
  4771. public function get_attempt_mode()
  4772. {
  4773. //Set default value for seriousgame_mode
  4774. if (!isset($this->seriousgame_mode)) {
  4775. $this->seriousgame_mode=0;
  4776. }
  4777. // Set default value for prevent_reinit
  4778. if (!isset($this->prevent_reinit)) {
  4779. $this->prevent_reinit =1;
  4780. }
  4781. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4782. return 'seriousgame';
  4783. }
  4784. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4785. return 'single';
  4786. }
  4787. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4788. return 'multiple';
  4789. }
  4790. return 'single';
  4791. }
  4792. /**
  4793. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4794. *
  4795. * @param string 'seriousgame', 'single' or 'multiple'
  4796. * @return boolean
  4797. * @author ndiechburg <noel@cblue.be>
  4798. **/
  4799. public function set_attempt_mode($mode)
  4800. {
  4801. $course_id = api_get_course_int_id();
  4802. switch ($mode) {
  4803. case 'seriousgame':
  4804. $sg_mode = 1;
  4805. $prevent_reinit = 1;
  4806. break;
  4807. case 'single':
  4808. $sg_mode = 0;
  4809. $prevent_reinit = 1;
  4810. break;
  4811. case 'multiple':
  4812. $sg_mode = 0;
  4813. $prevent_reinit = 0;
  4814. break;
  4815. default:
  4816. $sg_mode = 0;
  4817. $prevent_reinit = 0;
  4818. break;
  4819. }
  4820. $this->prevent_reinit = $prevent_reinit;
  4821. $this->seriousgame_mode = $sg_mode;
  4822. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4823. $sql = "UPDATE $lp_table SET
  4824. prevent_reinit = $prevent_reinit ,
  4825. seriousgame_mode = $sg_mode
  4826. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4827. $res = Database::query($sql);
  4828. if ($res) {
  4829. return true;
  4830. } else {
  4831. return false;
  4832. }
  4833. }
  4834. /**
  4835. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4836. *
  4837. * @return boolean
  4838. * @author ndiechburg <noel@cblue.be>
  4839. **/
  4840. public function switch_attempt_mode()
  4841. {
  4842. if ($this->debug > 0) {
  4843. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4844. }
  4845. $mode = $this->get_attempt_mode();
  4846. switch ($mode) {
  4847. case 'single':
  4848. $next_mode = 'multiple';
  4849. break;
  4850. case 'multiple':
  4851. $next_mode = 'seriousgame';
  4852. break;
  4853. case 'seriousgame':
  4854. $next_mode = 'single';
  4855. break;
  4856. default:
  4857. $next_mode = 'single';
  4858. break;
  4859. }
  4860. $this->set_attempt_mode($next_mode);
  4861. }
  4862. /**
  4863. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4864. * but possibility to do again a completed item.
  4865. *
  4866. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4867. * @author ndiechburg <noel@cblue.be>
  4868. **/
  4869. public function set_seriousgame_mode()
  4870. {
  4871. $course_id = api_get_course_int_id();
  4872. if ($this->debug > 0) {
  4873. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4874. }
  4875. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4876. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4877. $res = Database::query($sql);
  4878. if (Database :: num_rows($res) > 0) {
  4879. $row = Database :: fetch_array($res);
  4880. $force = $row['seriousgame_mode'];
  4881. if ($force == 1) {
  4882. $force = 0;
  4883. } elseif ($force == 0) {
  4884. $force = 1;
  4885. }
  4886. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4887. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4888. Database::query($sql);
  4889. $this->seriousgame_mode = $force;
  4890. return $force;
  4891. } else {
  4892. if ($this->debug > 2) {
  4893. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4894. }
  4895. }
  4896. return -1;
  4897. }
  4898. /**
  4899. * Updates the "scorm_debug" value that shows or hide the debug window
  4900. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4901. */
  4902. public function update_scorm_debug()
  4903. {
  4904. $course_id = api_get_course_int_id();
  4905. if ($this->debug > 0) {
  4906. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4907. }
  4908. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4909. $sql = "SELECT * FROM $lp_table
  4910. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4911. $res = Database::query($sql);
  4912. if (Database :: num_rows($res) > 0) {
  4913. $row = Database :: fetch_array($res);
  4914. $force = $row['debug'];
  4915. if ($force == 1) {
  4916. $force = 0;
  4917. } elseif ($force == 0) {
  4918. $force = 1;
  4919. }
  4920. $sql = "UPDATE $lp_table SET debug = $force
  4921. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4922. Database::query($sql);
  4923. $this->scorm_debug = $force;
  4924. return $force;
  4925. } else {
  4926. if ($this->debug > 2) {
  4927. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4928. }
  4929. }
  4930. return -1;
  4931. }
  4932. /**
  4933. * Function that makes a call to the function sort_tree_array and create_tree_array
  4934. * @author Kevin Van Den Haute
  4935. * @param array
  4936. */
  4937. public function tree_array($array)
  4938. {
  4939. if ($this->debug > 1) {
  4940. error_log('New LP - In learnpath::tree_array()', 0);
  4941. }
  4942. $array = $this->sort_tree_array($array);
  4943. $this->create_tree_array($array);
  4944. }
  4945. /**
  4946. * Creates an array with the elements of the learning path tree in it
  4947. *
  4948. * @author Kevin Van Den Haute
  4949. * @param array $array
  4950. * @param int $parent
  4951. * @param int $depth
  4952. * @param array $tmp
  4953. */
  4954. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4955. {
  4956. if ($this->debug > 1) {
  4957. error_log('New LP - In learnpath::create_tree_array())', 0);
  4958. }
  4959. if (is_array($array)) {
  4960. for ($i = 0; $i < count($array); $i++) {
  4961. if ($array[$i]['parent_item_id'] == $parent) {
  4962. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4963. $tmp[] = $array[$i]['parent_item_id'];
  4964. $depth++;
  4965. }
  4966. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4967. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4968. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4969. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4970. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4971. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4972. $this->arrMenu[] = array(
  4973. 'id' => $array[$i]['id'],
  4974. 'ref' => $ref,
  4975. 'item_type' => $array[$i]['item_type'],
  4976. 'title' => $array[$i]['title'],
  4977. 'path' => $path,
  4978. 'description' => $array[$i]['description'],
  4979. 'parent_item_id' => $array[$i]['parent_item_id'],
  4980. 'previous_item_id' => $array[$i]['previous_item_id'],
  4981. 'next_item_id' => $array[$i]['next_item_id'],
  4982. 'min_score' => $array[$i]['min_score'],
  4983. 'max_score' => $array[$i]['max_score'],
  4984. 'mastery_score' => $array[$i]['mastery_score'],
  4985. 'display_order' => $array[$i]['display_order'],
  4986. 'prerequisite' => $preq,
  4987. 'depth' => $depth,
  4988. 'audio' => $audio,
  4989. 'prerequisite_min_score' => $prerequisiteMinScore,
  4990. 'prerequisite_max_score' => $prerequisiteMaxScore
  4991. );
  4992. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4993. }
  4994. }
  4995. }
  4996. }
  4997. /**
  4998. * Sorts a multi dimensional array by parent id and display order
  4999. * @author Kevin Van Den Haute
  5000. *
  5001. * @param array $array (array with al the learning path items in it)
  5002. *
  5003. * @return array
  5004. */
  5005. public function sort_tree_array($array) {
  5006. foreach ($array as $key => $row) {
  5007. $parent[$key] = $row['parent_item_id'];
  5008. $position[$key] = $row['display_order'];
  5009. }
  5010. if (count($array) > 0)
  5011. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5012. return $array;
  5013. }
  5014. /**
  5015. * Function that creates a html list of learning path items so that we can add audio files to them
  5016. * @author Kevin Van Den Haute
  5017. * @param int $lp_id
  5018. * @return string
  5019. */
  5020. public function overview()
  5021. {
  5022. if ($this->debug > 0) {
  5023. error_log('New LP - In learnpath::overview()', 0);
  5024. }
  5025. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5026. $return = '';
  5027. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5028. // we need to start a form when we want to update all the mp3 files
  5029. if ($update_audio == 'true') {
  5030. $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">';
  5031. }
  5032. $return .= '<div id="message"></div>';
  5033. if (count($this->items) == 0) {
  5034. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5035. } else {
  5036. $return_audio = '<table class="data_table">';
  5037. $return_audio .= '<tr>';
  5038. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5039. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5040. $return_audio .= '</tr>';
  5041. if ($update_audio != 'true') {
  5042. $return .= '<div class="col-md-12">';
  5043. $return .= self::return_new_tree($update_audio);
  5044. $return .='</div>';
  5045. $return .= Display::div(
  5046. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5047. array('style' => 'float:left; margin-top:15px;width:100%')
  5048. );
  5049. } else {
  5050. $return_audio .= self::return_new_tree($update_audio);
  5051. $return .= $return_audio.'</table>';
  5052. }
  5053. // We need to close the form when we are updating the mp3 files.
  5054. if ($update_audio == 'true') {
  5055. $return .= '<div class="footer-audio">';
  5056. $return .= Display::button(
  5057. 'save_audio',
  5058. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5059. array('class' => 'btn btn-primary', 'type' => 'submit')
  5060. );
  5061. $return .= '</div>';
  5062. }
  5063. }
  5064. // We need to close the form when we are updating the mp3 files.
  5065. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5066. $return .= '</form>';
  5067. }
  5068. return $return;
  5069. }
  5070. /**
  5071. * @param string string $update_audio
  5072. * @param bool $drop_element_here
  5073. * @return string
  5074. */
  5075. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5076. {
  5077. $return = '';
  5078. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5079. $course_id = api_get_course_int_id();
  5080. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5081. $sql = "SELECT * FROM $tbl_lp_item
  5082. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5083. $result = Database::query($sql);
  5084. $arrLP = array();
  5085. while ($row = Database :: fetch_array($result)) {
  5086. $arrLP[] = array(
  5087. 'id' => $row['id'],
  5088. 'item_type' => $row['item_type'],
  5089. 'title' => Security :: remove_XSS($row['title']),
  5090. 'path' => $row['path'],
  5091. 'description' => Security::remove_XSS($row['description']),
  5092. 'parent_item_id' => $row['parent_item_id'],
  5093. 'previous_item_id' => $row['previous_item_id'],
  5094. 'next_item_id' => $row['next_item_id'],
  5095. 'max_score' => $row['max_score'],
  5096. 'min_score' => $row['min_score'],
  5097. 'mastery_score' => $row['mastery_score'],
  5098. 'prerequisite' => $row['prerequisite'],
  5099. 'display_order' => $row['display_order'],
  5100. 'audio' => $row['audio'],
  5101. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5102. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5103. );
  5104. }
  5105. $this->tree_array($arrLP);
  5106. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5107. unset ($this->arrMenu);
  5108. $default_data = null;
  5109. $default_content = null;
  5110. $elements = array();
  5111. $return_audio = null;
  5112. for ($i = 0; $i < count($arrLP); $i++) {
  5113. $title = $arrLP[$i]['title'];
  5114. $title_cut = cut($arrLP[$i]['title'], 25);
  5115. // Link for the documents
  5116. if ($arrLP[$i]['item_type'] == 'document') {
  5117. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5118. $title_cut = Display::url(
  5119. $title_cut,
  5120. $url,
  5121. array(
  5122. 'class' => 'ajax moved',
  5123. 'data-title' => $title_cut
  5124. )
  5125. );
  5126. }
  5127. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5128. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5129. Session::write('pathItem', $arrLP[$i]['path']);
  5130. }
  5131. if (($i % 2) == 0) {
  5132. $oddClass = 'row_odd';
  5133. } else {
  5134. $oddClass = 'row_even';
  5135. }
  5136. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5137. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5138. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5139. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5140. } else {
  5141. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5142. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5143. } else {
  5144. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5145. $icon = Display::return_icon('certificate.png');
  5146. } else {
  5147. $icon = Display::return_icon('folder_document.gif');
  5148. }
  5149. }
  5150. }
  5151. // The audio column.
  5152. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5153. $audio = '';
  5154. if (!$update_audio || $update_audio <> 'true') {
  5155. if (!empty($arrLP[$i]['audio'])) {
  5156. } else {
  5157. $audio .= '';
  5158. }
  5159. } else {
  5160. $types = self::getChapterTypes();
  5161. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5162. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5163. if (!empty ($arrLP[$i]['audio'])) {
  5164. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5165. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5166. }
  5167. }
  5168. }
  5169. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5170. $return_audio .= '</td>';
  5171. $move_icon = '';
  5172. $move_item_icon = '';
  5173. $edit_icon = '';
  5174. $delete_icon = '';
  5175. $audio_icon = '';
  5176. $prerequisities_icon = '';
  5177. $forumIcon = '';
  5178. $previewIcon = '';
  5179. if ($is_allowed_to_edit) {
  5180. if (!$update_audio || $update_audio <> 'true') {
  5181. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5182. $move_icon .= '<a class="moved" href="#">';
  5183. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5184. $move_icon .= '</a>';
  5185. }
  5186. }
  5187. // No edit for this item types
  5188. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5189. if ($arrLP[$i]['item_type'] != 'dir') {
  5190. $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">';
  5191. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5192. $edit_icon .= '</a>';
  5193. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5194. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5195. $this->course_int_id,
  5196. $this->lp_session_id
  5197. );
  5198. if ($forumThread) {
  5199. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5200. 'action' => 'dissociate_forum',
  5201. 'id' => $arrLP[$i]['id'],
  5202. 'lp_id' => $this->lp_id
  5203. ]);
  5204. $forumIcon = Display::url(
  5205. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5206. $forumIconUrl,
  5207. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5208. );
  5209. } else {
  5210. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5211. 'action' => 'create_forum',
  5212. 'id' => $arrLP[$i]['id'],
  5213. 'lp_id' => $this->lp_id
  5214. ]);
  5215. $forumIcon = Display::url(
  5216. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5217. $forumIconUrl,
  5218. ['class' => "btn btn-default lp-btn-associate-forum"]
  5219. );
  5220. }
  5221. }
  5222. } else {
  5223. $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">';
  5224. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5225. $edit_icon .= '</a>';
  5226. }
  5227. }
  5228. $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">';
  5229. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5230. $delete_icon .= '</a>';
  5231. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5232. if (in_array($arrLP[$i]['item_type'], ['document', 'final_item'])) {
  5233. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5234. $previewIcon = Display::url(
  5235. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5236. $urlPreviewLink,
  5237. array(
  5238. 'class' => 'btn btn-default ajax',
  5239. 'data-title' => $arrLP[$i]['title']
  5240. )
  5241. );
  5242. } else {
  5243. $previewIcon = Display::url(
  5244. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5245. $url.'&action=view_item', ['class' => 'btn btn-default']
  5246. );
  5247. }
  5248. if ($arrLP[$i]['item_type'] != 'dir') {
  5249. $prerequisities_icon = Display::url(
  5250. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5251. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5252. );
  5253. if ($arrLP[$i]['item_type'] != 'final_item') {
  5254. $move_item_icon = Display::url(
  5255. Display::return_icon(
  5256. 'move.png',
  5257. get_lang('Move'),
  5258. array(),
  5259. ICON_SIZE_TINY
  5260. ),
  5261. $url.'&action=move_item',
  5262. ['class' => 'btn btn-default']
  5263. );
  5264. }
  5265. $audio_icon = Display::url(
  5266. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5267. $url.'&action=add_audio',
  5268. ['class' => 'btn btn-default']
  5269. );
  5270. }
  5271. }
  5272. if ($update_audio != 'true') {
  5273. $row = $move_icon . ' ' . $icon .
  5274. Display::span($title_cut) .
  5275. Display::tag(
  5276. 'div',
  5277. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5278. array('class'=>'btn-toolbar button_actions')
  5279. );
  5280. } else {
  5281. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5282. }
  5283. $parent_id = $arrLP[$i]['parent_item_id'];
  5284. $default_data[$arrLP[$i]['id']] = $row;
  5285. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5286. if (empty($parent_id)) {
  5287. $elements[$arrLP[$i]['id']]['data'] = $row;
  5288. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5289. } else {
  5290. $parent_arrays = array();
  5291. if ($arrLP[$i]['depth'] > 1) {
  5292. //Getting list of parents
  5293. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5294. foreach($arrLP as $item) {
  5295. if ($item['id'] == $parent_id) {
  5296. if ($item['parent_item_id'] == 0) {
  5297. $parent_id = $item['id'];
  5298. break;
  5299. } else {
  5300. $parent_id = $item['parent_item_id'];
  5301. if (empty($parent_arrays)) {
  5302. $parent_arrays[] = intval($item['id']);
  5303. }
  5304. $parent_arrays[] = $parent_id;
  5305. break;
  5306. }
  5307. }
  5308. }
  5309. }
  5310. }
  5311. if (!empty($parent_arrays)) {
  5312. $parent_arrays = array_reverse($parent_arrays);
  5313. $val = '$elements';
  5314. $x = 0;
  5315. foreach($parent_arrays as $item) {
  5316. if ($x != count($parent_arrays) -1) {
  5317. $val .= '["'.$item.'"]["children"]';
  5318. } else {
  5319. $val .= '["'.$item.'"]["children"]';
  5320. }
  5321. $x++;
  5322. }
  5323. $val .= "";
  5324. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5325. eval($code_str);
  5326. } else {
  5327. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5328. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5329. }
  5330. }
  5331. }
  5332. $list = '<ul id="lp_item_list">';
  5333. $tree = self::print_recursive($elements, $default_data, $default_content);
  5334. if (!empty($tree)) {
  5335. $list .= $tree;
  5336. } else {
  5337. if ($drop_element_here) {
  5338. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5339. }
  5340. }
  5341. $list .= '</ul>';
  5342. $return .= Display::panelCollapse(
  5343. $this->name,
  5344. $list,
  5345. 'scorm-list',
  5346. null,
  5347. 'scorm-list-accordion',
  5348. 'scorm-list-collapse'
  5349. );
  5350. if ($update_audio == 'true') {
  5351. $return = $return_audio;
  5352. }
  5353. return $return;
  5354. }
  5355. /**
  5356. * @param array $elements
  5357. * @param array $default_data
  5358. * @param array $default_content
  5359. * @return string
  5360. */
  5361. public function print_recursive($elements, $default_data, $default_content)
  5362. {
  5363. $return = '';
  5364. foreach ($elements as $key => $item) {
  5365. if (isset($item['load_data']) || empty($item['data'])) {
  5366. $item['data'] = $default_data[$item['load_data']];
  5367. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5368. }
  5369. $sub_list = '';
  5370. if (isset($item['type']) && $item['type'] == 'dir') {
  5371. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5372. }
  5373. if (empty($item['children'])) {
  5374. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5375. $active = null;
  5376. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5377. $active = 'active';
  5378. }
  5379. $return .= Display::tag(
  5380. 'li',
  5381. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5382. array('id'=>$key, 'class'=>'record li_container')
  5383. );
  5384. } else {
  5385. // Sections
  5386. if (isset($item['children'])) {
  5387. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5388. }
  5389. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5390. $return .= Display::tag(
  5391. 'li',
  5392. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5393. array('id'=>$key, 'class'=>'record li_container')
  5394. );
  5395. }
  5396. }
  5397. return $return;
  5398. }
  5399. /**
  5400. * This function builds the action menu
  5401. * @param bool $returnContent Optional
  5402. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5403. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5404. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5405. * @return string
  5406. */
  5407. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5408. {
  5409. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5410. $actionsLeft = '';
  5411. $actionsRight = '';
  5412. $actionsLeft .= Display::url(
  5413. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5414. 'lp_controller.php?'.api_get_cidreq().'&' . http_build_query([
  5415. 'gradebook' => $gradebook,
  5416. 'action' => 'view',
  5417. 'lp_id' => $_SESSION['oLP']->lp_id,
  5418. 'isStudentView' => 'true'
  5419. ])
  5420. );
  5421. $actionsLeft .= Display::url(
  5422. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5423. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5424. 'action' => 'admin_view',
  5425. 'lp_id' => $_SESSION['oLP']->lp_id,
  5426. 'updateaudio' => 'true'
  5427. ])
  5428. );
  5429. if (!$isConfigPage) {
  5430. $actionsLeft .= Display::url(
  5431. Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM),
  5432. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5433. 'action' => 'edit',
  5434. 'lp_id' => $_SESSION['oLP']->lp_id
  5435. ])
  5436. );
  5437. } else {
  5438. $actionsLeft .= Display::url(
  5439. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5440. 'lp_controller.php?' . http_build_query([
  5441. 'action' => 'build',
  5442. 'lp_id' => $_SESSION['oLP']->lp_id
  5443. ]) . '&' . api_get_cidreq()
  5444. );
  5445. }
  5446. if ($allowExpand) {
  5447. $actionsLeft .= Display::url(
  5448. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM) .
  5449. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5450. '#',
  5451. ['role' => 'button', 'id' => 'hide_bar_template']
  5452. );
  5453. }
  5454. if ($showRequirementButtons) {
  5455. $buttons = array(
  5456. array(
  5457. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5458. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5459. 'action' => 'set_previous_step_as_prerequisite',
  5460. 'lp_id' => $_SESSION['oLP']->lp_id
  5461. ])
  5462. ),
  5463. array(
  5464. 'title' => get_lang('ClearAllPrerequisites'),
  5465. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5466. 'action' => 'clear_prerequisites',
  5467. 'lp_id' => $_SESSION['oLP']->lp_id
  5468. ])
  5469. ),
  5470. );
  5471. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons);
  5472. }
  5473. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5474. if ($returnContent) {
  5475. return $toolbar;
  5476. }
  5477. echo $toolbar;
  5478. }
  5479. /**
  5480. * Creates the default learning path folder
  5481. * @param array $course
  5482. * @param int $creatorId
  5483. *
  5484. * @return bool
  5485. */
  5486. public static function generate_learning_path_folder($course, $creatorId = 0)
  5487. {
  5488. // Creating learning_path folder
  5489. $dir = '/learning_path';
  5490. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5491. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5492. $folder = false;
  5493. if (!is_dir($filepath.'/'.$dir)) {
  5494. $folderData = create_unexisting_directory(
  5495. $course,
  5496. $creatorId,
  5497. 0,
  5498. 0,
  5499. 0,
  5500. $filepath,
  5501. $dir,
  5502. get_lang('LearningPaths'),
  5503. 0
  5504. );
  5505. if (!empty($folderData)) {
  5506. $folder = true;
  5507. }
  5508. } else {
  5509. $folder = true;
  5510. }
  5511. return $folder;
  5512. }
  5513. /**
  5514. * @param array $course
  5515. * @param string $lp_name
  5516. * @param int $creatorId
  5517. *
  5518. * @return array
  5519. */
  5520. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5521. {
  5522. $filepath = '';
  5523. $dir = '/learning_path/';
  5524. if (empty($lp_name)) {
  5525. $lp_name = $this->name;
  5526. }
  5527. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5528. $folder = self::generate_learning_path_folder($course, $creatorId);
  5529. // Limits title size
  5530. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5531. $dir = $dir.$title;
  5532. // Creating LP folder
  5533. $documentId = null;
  5534. if ($folder) {
  5535. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5536. if (!is_dir($filepath.'/'.$dir)) {
  5537. $folderData = create_unexisting_directory(
  5538. $course,
  5539. $creatorId,
  5540. 0,
  5541. 0,
  5542. 0,
  5543. $filepath,
  5544. $dir,
  5545. $lp_name
  5546. );
  5547. if (!empty($folderData)) {
  5548. $folder = true;
  5549. }
  5550. $documentId = $folderData['id'];
  5551. } else {
  5552. $folder = true;
  5553. }
  5554. $dir = $dir.'/';
  5555. if ($folder) {
  5556. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5557. }
  5558. }
  5559. if (empty($documentId)) {
  5560. $dir = api_remove_trailing_slash($dir);
  5561. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5562. }
  5563. $array = array(
  5564. 'dir' => $dir,
  5565. 'filepath' => $filepath,
  5566. 'folder' => $folder,
  5567. 'id' => $documentId
  5568. );
  5569. return $array;
  5570. }
  5571. /**
  5572. * Create a new document //still needs some finetuning
  5573. * @param array $courseInfo
  5574. * @param string $content
  5575. * @param string $title
  5576. * @param string $extension
  5577. * @param int $parentId
  5578. * @param int $creatorId creator id
  5579. *
  5580. * @return string
  5581. */
  5582. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5583. {
  5584. if (!empty($courseInfo)) {
  5585. $course_id = $courseInfo['real_id'];
  5586. } else {
  5587. $course_id = api_get_course_int_id();
  5588. }
  5589. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5590. $sessionId = api_get_session_id();
  5591. // Generates folder
  5592. $result = $this->generate_lp_folder($courseInfo);
  5593. $dir = $result['dir'];
  5594. if (empty($parentId)) {
  5595. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5596. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5597. // Please, do not modify this dirname formatting.
  5598. if (strstr($dir, '..')) {
  5599. $dir = '/';
  5600. }
  5601. if (!empty($dir[0]) && $dir[0] == '.') {
  5602. $dir = substr($dir, 1);
  5603. }
  5604. if (!empty($dir[0]) && $dir[0] != '/') {
  5605. $dir = '/'.$dir;
  5606. }
  5607. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5608. $dir .= '/';
  5609. }
  5610. } else {
  5611. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5612. if (!empty($parentInfo)) {
  5613. $dir = $parentInfo['path'].'/';
  5614. }
  5615. }
  5616. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5617. if (!is_dir($filepath)) {
  5618. $dir = '/';
  5619. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5620. }
  5621. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5622. // is already escaped twice when it gets here.
  5623. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5624. if (!empty($title)) {
  5625. $title = api_replace_dangerous_char(stripslashes($title));
  5626. } else {
  5627. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5628. }
  5629. $title = disable_dangerous_file($title);
  5630. $filename = $title;
  5631. $content = !empty($content) ? $content : $_POST['content_lp'];
  5632. $tmp_filename = $filename;
  5633. $i = 0;
  5634. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5635. $tmp_filename = $filename . '_' . ++ $i;
  5636. }
  5637. $filename = $tmp_filename . '.'.$extension;
  5638. if ($extension == 'html') {
  5639. $content = stripslashes($content);
  5640. $content = str_replace(
  5641. api_get_path(WEB_COURSE_PATH),
  5642. api_get_path(REL_PATH).'courses/',
  5643. $content
  5644. );
  5645. // Change the path of mp3 to absolute.
  5646. // The first regexp deals with :// urls.
  5647. $content = preg_replace(
  5648. "|(flashvars=\"file=)([^:/]+)/|",
  5649. "$1".api_get_path(
  5650. REL_COURSE_PATH
  5651. ).$courseInfo['path'].'/document/',
  5652. $content
  5653. );
  5654. // The second regexp deals with audio/ urls.
  5655. $content = preg_replace(
  5656. "|(flashvars=\"file=)([^/]+)/|",
  5657. "$1".api_get_path(
  5658. REL_COURSE_PATH
  5659. ).$courseInfo['path'].'/document/$2/',
  5660. $content
  5661. );
  5662. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5663. $content = str_replace(
  5664. '</body>',
  5665. '<style type="text/css">body{}</style></body>',
  5666. $content
  5667. );
  5668. }
  5669. if (!file_exists($filepath . $filename)) {
  5670. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5671. fputs($fp, $content);
  5672. fclose($fp);
  5673. $file_size = filesize($filepath . $filename);
  5674. $save_file_path = $dir.$filename;
  5675. $document_id = add_document(
  5676. $courseInfo,
  5677. $save_file_path,
  5678. 'file',
  5679. $file_size,
  5680. $tmp_filename,
  5681. '',
  5682. 0, //readonly
  5683. true,
  5684. null,
  5685. $sessionId,
  5686. $creatorId
  5687. );
  5688. if ($document_id) {
  5689. api_item_property_update(
  5690. $courseInfo,
  5691. TOOL_DOCUMENT,
  5692. $document_id,
  5693. 'DocumentAdded',
  5694. $creatorId,
  5695. null,
  5696. null,
  5697. null,
  5698. null,
  5699. $sessionId
  5700. );
  5701. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5702. $new_title = $originalTitle;
  5703. if ($new_comment || $new_title) {
  5704. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5705. $ct = '';
  5706. if ($new_comment)
  5707. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5708. if ($new_title)
  5709. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5710. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5711. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5712. Database::query($sql);
  5713. }
  5714. }
  5715. return $document_id;
  5716. }
  5717. }
  5718. }
  5719. /**
  5720. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5721. * @param array $_course array
  5722. * @return void
  5723. */
  5724. public function edit_document($_course)
  5725. {
  5726. $course_id = api_get_course_int_id();
  5727. $urlAppend = api_get_configuration_value('url_append');
  5728. // Please, do not modify this dirname formatting.
  5729. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5730. if (strstr($dir, '..'))
  5731. $dir = '/';
  5732. if ($dir[0] == '.')
  5733. $dir = substr($dir, 1);
  5734. if ($dir[0] != '/')
  5735. $dir = '/' . $dir;
  5736. if ($dir[strlen($dir) - 1] != '/')
  5737. $dir .= '/';
  5738. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5739. if (!is_dir($filepath)) {
  5740. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5741. }
  5742. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5743. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5744. $document_id = intval($_POST['path']);
  5745. $sql = "SELECT path FROM " . $table_doc . "
  5746. WHERE c_id = $course_id AND id = " . $document_id;
  5747. $res = Database::query($sql);
  5748. $row = Database :: fetch_array($res);
  5749. $content = stripslashes($_POST['content_lp']);
  5750. $file = $filepath . $row['path'];
  5751. if ($fp = @ fopen($file, 'w')) {
  5752. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  5753. // Change the path of mp3 to absolute.
  5754. // The first regexp deals with :// urls.
  5755. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5756. // The second regexp deals with audio/ urls.
  5757. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5758. fputs($fp, $content);
  5759. fclose($fp);
  5760. $sql = "UPDATE " . $table_doc ." SET
  5761. title='".Database::escape_string($_POST['title'])."'
  5762. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5763. Database::query($sql);
  5764. }
  5765. }
  5766. }
  5767. /**
  5768. * Displays the selected item, with a panel for manipulating the item
  5769. * @param int $item_id
  5770. * @param string $msg
  5771. * @return string
  5772. */
  5773. public function display_item($item_id, $msg = null, $show_actions = true)
  5774. {
  5775. $course_id = api_get_course_int_id();
  5776. $return = '';
  5777. if (is_numeric($item_id)) {
  5778. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5779. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  5780. WHERE
  5781. c_id = $course_id AND
  5782. lp.id = " . intval($item_id);
  5783. $result = Database::query($sql);
  5784. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5785. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  5786. // Prevents wrong parent selection for document, see Bug#1251.
  5787. if ($row['item_type'] != 'dir') {
  5788. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5789. }
  5790. if ($show_actions) {
  5791. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5792. }
  5793. $return .= '<div style="padding:10px;">';
  5794. if ($msg != '')
  5795. $return .= $msg;
  5796. $return .= '<h3>'.$row['title'].'</h3>';
  5797. switch ($row['item_type']) {
  5798. case TOOL_QUIZ:
  5799. if (!empty($row['path'])) {
  5800. $exercise = new Exercise();
  5801. $exercise->read($row['path']);
  5802. $return .= $exercise->description.'<br />';
  5803. $return .= Display::url(
  5804. get_lang('GoToExercise'),
  5805. api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . api_get_cidreq() . '&exerciseId=' . $exercise->id,
  5806. ['class' => 'btn btn-primary']
  5807. );
  5808. }
  5809. break;
  5810. case TOOL_DOCUMENT:
  5811. case TOOL_LP_FINAL_ITEM:
  5812. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5813. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5814. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5815. $result = Database::query($sql_doc);
  5816. $path_file = Database::result($result, 0, 0);
  5817. $path_parts = pathinfo($path_file);
  5818. // TODO: Correct the following naive comparisons.
  5819. if (in_array($path_parts['extension'], array(
  5820. 'html',
  5821. 'txt',
  5822. 'png',
  5823. 'jpg',
  5824. 'JPG',
  5825. 'jpeg',
  5826. 'JPEG',
  5827. 'gif',
  5828. 'swf',
  5829. 'pdf',
  5830. 'htm'
  5831. ))) {
  5832. $return .= $this->display_document($row['path'], true, true);
  5833. }
  5834. break;
  5835. case TOOL_HOTPOTATOES:
  5836. $return .= $this->display_document($row['path'], false, true);
  5837. break;
  5838. }
  5839. $return .= '</div>';
  5840. }
  5841. }
  5842. return $return;
  5843. }
  5844. /**
  5845. * Shows the needed forms for editing a specific item
  5846. * @param int $item_id
  5847. * @return string
  5848. */
  5849. public function display_edit_item($item_id)
  5850. {
  5851. $course_id = api_get_course_int_id();
  5852. $return = '';
  5853. if (is_numeric($item_id)) {
  5854. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5855. $sql = "SELECT * FROM $tbl_lp_item
  5856. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5857. $res = Database::query($sql);
  5858. $row = Database::fetch_array($res);
  5859. switch ($row['item_type']) {
  5860. case 'dir':
  5861. case 'asset':
  5862. case 'sco':
  5863. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5864. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5865. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5866. } else {
  5867. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5868. }
  5869. break;
  5870. case TOOL_DOCUMENT:
  5871. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5872. $sql = "SELECT lp.*, doc.path as dir
  5873. FROM " . $tbl_lp_item . " as lp
  5874. LEFT JOIN " . $tbl_doc . " as doc
  5875. ON doc.id = lp.path
  5876. WHERE
  5877. lp.c_id = $course_id AND
  5878. doc.c_id = $course_id AND
  5879. lp.id = " . intval($item_id);
  5880. $res_step = Database::query($sql);
  5881. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5882. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5883. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5884. break;
  5885. case TOOL_LINK:
  5886. $link_id = (string) $row['path'];
  5887. if (ctype_digit($link_id)) {
  5888. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5889. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5890. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5891. $res_link = Database::query($sql_select);
  5892. $row_link = Database :: fetch_array($res_link);
  5893. if (is_array($row_link)) {
  5894. $row['url'] = $row_link['url'];
  5895. }
  5896. }
  5897. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5898. $return .= $this->display_link_form('edit', $item_id, $row);
  5899. break;
  5900. case TOOL_LP_FINAL_ITEM:
  5901. $_SESSION['finalItem'] = true;
  5902. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5903. $sql = "SELECT lp.*, doc.path as dir
  5904. FROM " . $tbl_lp_item . " as lp
  5905. LEFT JOIN " . $tbl_doc . " as doc
  5906. ON doc.id = lp.path
  5907. WHERE
  5908. lp.c_id = $course_id AND
  5909. doc.c_id = $course_id AND
  5910. lp.id = " . intval($item_id);
  5911. $res_step = Database::query($sql);
  5912. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5913. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5914. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5915. break;
  5916. case TOOL_QUIZ:
  5917. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5918. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5919. break;
  5920. case TOOL_HOTPOTATOES:
  5921. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5922. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5923. break;
  5924. case TOOL_STUDENTPUBLICATION:
  5925. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5926. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5927. break;
  5928. case TOOL_FORUM:
  5929. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5930. $return .= $this->display_forum_form('edit', $item_id, $row);
  5931. break;
  5932. case TOOL_THREAD:
  5933. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5934. $return .= $this->display_thread_form('edit', $item_id, $row);
  5935. break;
  5936. }
  5937. }
  5938. return $return;
  5939. }
  5940. /**
  5941. * Function that displays a list with al the resources that
  5942. * could be added to the learning path
  5943. * @return string
  5944. */
  5945. public function display_resources()
  5946. {
  5947. $course_code = api_get_course_id();
  5948. // Get all the docs.
  5949. $documents = $this->get_documents(true);
  5950. // Get all the exercises.
  5951. $exercises = $this->get_exercises();
  5952. // Get all the links.
  5953. $links = $this->get_links();
  5954. // Get all the student publications.
  5955. $works = $this->get_student_publications();
  5956. // Get all the forums.
  5957. $forums = $this->get_forums(null, $course_code);
  5958. // Get the final item form (see BT#11048) .
  5959. $finish = $this->getFinalItemForm();
  5960. $headers = array(
  5961. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5962. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5963. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5964. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5965. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5966. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  5967. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  5968. );
  5969. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5970. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  5971. echo Display::tabs(
  5972. $headers,
  5973. array(
  5974. $documents,
  5975. $exercises,
  5976. $links,
  5977. $works,
  5978. $forums,
  5979. $dir,
  5980. $finish,
  5981. ),
  5982. 'resource_tab'
  5983. );
  5984. return true;
  5985. }
  5986. /**
  5987. * Returns the extension of a document
  5988. * @param string $filename
  5989. * @return string Extension (part after the last dot)
  5990. */
  5991. public function get_extension($filename)
  5992. {
  5993. $explode = explode('.', $filename);
  5994. return $explode[count($explode) - 1];
  5995. }
  5996. /**
  5997. * Displays a document by id
  5998. *
  5999. * @param int $id
  6000. * @return string
  6001. */
  6002. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6003. {
  6004. $_course = api_get_course_info();
  6005. $course_id = api_get_course_int_id();
  6006. $return = '';
  6007. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6008. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  6009. WHERE c_id = ".$course_id." AND id = " . $id;
  6010. $res_doc = Database::query($sql_doc);
  6011. $row_doc = Database :: fetch_array($res_doc);
  6012. // TODO: Add a path filter.
  6013. if ($iframe) {
  6014. $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>';
  6015. } else {
  6016. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  6017. }
  6018. return $return;
  6019. }
  6020. /**
  6021. * Return HTML form to add/edit a quiz
  6022. * @param string Action (add/edit)
  6023. * @param integer Item ID if already exists
  6024. * @param mixed Extra information (quiz ID if integer)
  6025. * @return string HTML form
  6026. */
  6027. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6028. {
  6029. $course_id = api_get_course_int_id();
  6030. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6031. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6032. if ($id != 0 && is_array($extra_info)) {
  6033. $item_title = $extra_info['title'];
  6034. $item_description = $extra_info['description'];
  6035. } elseif (is_numeric($extra_info)) {
  6036. $sql = "SELECT title, description
  6037. FROM " . $tbl_quiz . "
  6038. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6039. $result = Database::query($sql);
  6040. $row = Database::fetch_array($result);
  6041. $item_title = $row['title'];
  6042. $item_description = $row['description'];
  6043. } else {
  6044. $item_title = '';
  6045. $item_description = '';
  6046. }
  6047. $item_title = Security::remove_XSS($item_title);
  6048. $item_description = Security::remove_XSS($item_description);
  6049. if ($id != 0 && is_array($extra_info))
  6050. $parent = $extra_info['parent_item_id'];
  6051. else
  6052. $parent = 0;
  6053. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6054. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6055. $result = Database::query($sql);
  6056. $arrLP = array ();
  6057. while ($row = Database :: fetch_array($result)) {
  6058. $arrLP[] = array (
  6059. 'id' => $row['id'],
  6060. 'item_type' => $row['item_type'],
  6061. 'title' => $row['title'],
  6062. 'path' => $row['path'],
  6063. 'description' => $row['description'],
  6064. 'parent_item_id' => $row['parent_item_id'],
  6065. 'previous_item_id' => $row['previous_item_id'],
  6066. 'next_item_id' => $row['next_item_id'],
  6067. 'display_order' => $row['display_order'],
  6068. 'max_score' => $row['max_score'],
  6069. 'min_score' => $row['min_score'],
  6070. 'mastery_score' => $row['mastery_score'],
  6071. 'prerequisite' => $row['prerequisite'],
  6072. 'max_time_allowed' => $row['max_time_allowed']
  6073. );
  6074. }
  6075. $this->tree_array($arrLP);
  6076. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6077. unset ($this->arrMenu);
  6078. $form = new FormValidator('quiz_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6079. $defaults = [];
  6080. if ($action == 'add') {
  6081. $legend = get_lang('CreateTheExercise');
  6082. } elseif ($action == 'move') {
  6083. $legend = get_lang('MoveTheCurrentExercise');
  6084. } else {
  6085. $legend = get_lang('EditCurrentExecice');
  6086. }
  6087. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6088. $legend .= Display :: return_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6089. }
  6090. $form->addHeader($legend);
  6091. if ($action != 'move') {
  6092. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6093. $defaults['title'] = $item_title;
  6094. }
  6095. // Select for Parent item, root or chapter
  6096. $selectParent = $form->addSelect(
  6097. 'parent',
  6098. get_lang('Parent'),
  6099. [],
  6100. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6101. );
  6102. $selectParent->addOption($this->name, 0);
  6103. $arrHide = array (
  6104. $id
  6105. );
  6106. for ($i = 0; $i < count($arrLP); $i++) {
  6107. if ($action != 'add') {
  6108. if (
  6109. ($arrLP[$i]['item_type'] == 'dir') &&
  6110. !in_array($arrLP[$i]['id'], $arrHide) &&
  6111. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6112. ) {
  6113. $selectParent->addOption(
  6114. $arrLP[$i]['title'],
  6115. $arrLP[$i]['id'],
  6116. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6117. );
  6118. if ($parent == $arrLP[$i]['id']) {
  6119. $selectParent->setSelected($arrLP[$i]['id']);
  6120. }
  6121. } else {
  6122. $arrHide[] = $arrLP[$i]['id'];
  6123. }
  6124. } else {
  6125. if ($arrLP[$i]['item_type'] == 'dir') {
  6126. $selectParent->addOption(
  6127. $arrLP[$i]['title'],
  6128. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6129. );
  6130. if ($parent == $arrLP[$i]['id']) {
  6131. $selectParent->setSelected($arrLP[$i]['id']);
  6132. }
  6133. }
  6134. }
  6135. }
  6136. if (is_array($arrLP)) {
  6137. reset($arrLP);
  6138. }
  6139. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6140. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6141. for ($i = 0; $i < count($arrLP); $i++) {
  6142. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6143. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6144. if (is_array($extra_info)) {
  6145. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6146. $selectPrevious->setSelected($arrLP[$i]['id']);
  6147. }
  6148. } elseif ($action == 'add') {
  6149. $selectPrevious->setSelected($arrLP[$i]['id']);
  6150. }
  6151. }
  6152. }
  6153. if ($action != 'move') {
  6154. $id_prerequisite = 0;
  6155. if (is_array($arrLP)) {
  6156. foreach ($arrLP as $key => $value) {
  6157. if ($value['id'] == $id) {
  6158. $id_prerequisite = $value['prerequisite'];
  6159. break;
  6160. }
  6161. }
  6162. }
  6163. $arrHide = array ();
  6164. for ($i = 0; $i < count($arrLP); $i++) {
  6165. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6166. if (is_array($extra_info)) {
  6167. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6168. $s_selected_position = $arrLP[$i]['id'];
  6169. }
  6170. } elseif ($action == 'add') {
  6171. $s_selected_position = 0;
  6172. }
  6173. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6174. }
  6175. }
  6176. /*// Commented the prerequisites, only visible in edit (exercise).
  6177. $return .= '<tr>';
  6178. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6179. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6180. foreach($arrHide as $key => $value){
  6181. if($key==$s_selected_position && $action == 'add'){
  6182. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6183. }
  6184. elseif($key==$id_prerequisite && $action == 'edit'){
  6185. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6186. }
  6187. else{
  6188. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6189. }
  6190. }
  6191. $return .= "</select></td>";
  6192. */
  6193. /*$return .= '<tr>';
  6194. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6195. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6196. // Remove temporarily the test description.
  6197. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6198. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6199. $return .= '</tr>'; */
  6200. }
  6201. if ($action == 'add') {
  6202. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6203. } else {
  6204. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6205. }
  6206. if ($action == 'move') {
  6207. $form->addHidden('title', $item_title);
  6208. $form->addHidden('description', $item_description);
  6209. }
  6210. if (is_numeric($extra_info)) {
  6211. $form->addHidden('path', $extra_info);
  6212. } elseif (is_array($extra_info)) {
  6213. $form->addHidden('path', $extra_info['path']);
  6214. }
  6215. $form->addHidden('type', TOOL_QUIZ);
  6216. $form->addHidden('post_time', time());
  6217. $form->setDefaults($defaults);
  6218. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6219. }
  6220. /**
  6221. * Addition of Hotpotatoes tests
  6222. * @param string Action
  6223. * @param integer Internal ID of the item
  6224. * @param mixed Extra information - can be an array with title and description indexes
  6225. * @return string HTML structure to display the hotpotatoes addition formular
  6226. */
  6227. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6228. {
  6229. $course_id = api_get_course_int_id();
  6230. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6231. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6232. if ($id != 0 && is_array($extra_info)) {
  6233. $item_title = stripslashes($extra_info['title']);
  6234. $item_description = stripslashes($extra_info['description']);
  6235. } elseif (is_numeric($extra_info)) {
  6236. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6237. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6238. WHERE
  6239. c_id = ".$course_id." AND
  6240. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6241. id = " . (int) $extra_info . "
  6242. ORDER BY id ASC";
  6243. $res_hot = Database::query($sql);
  6244. $row = Database::fetch_array($res_hot);
  6245. $item_title = $row['title'];
  6246. $item_description = $row['description'];
  6247. if (!empty ($row['comment'])) {
  6248. $item_title = $row['comment'];
  6249. }
  6250. } else {
  6251. $item_title = '';
  6252. $item_description = '';
  6253. }
  6254. if ($id != 0 && is_array($extra_info)) {
  6255. $parent = $extra_info['parent_item_id'];
  6256. } else {
  6257. $parent = 0;
  6258. }
  6259. $sql = "SELECT * FROM $tbl_lp_item
  6260. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6261. $result = Database::query($sql);
  6262. $arrLP = array ();
  6263. while ($row = Database :: fetch_array($result)) {
  6264. $arrLP[] = array (
  6265. 'id' => $row['id'],
  6266. 'item_type' => $row['item_type'],
  6267. 'title' => $row['title'],
  6268. 'path' => $row['path'],
  6269. 'description' => $row['description'],
  6270. 'parent_item_id' => $row['parent_item_id'],
  6271. 'previous_item_id' => $row['previous_item_id'],
  6272. 'next_item_id' => $row['next_item_id'],
  6273. 'display_order' => $row['display_order'],
  6274. 'max_score' => $row['max_score'],
  6275. 'min_score' => $row['min_score'],
  6276. 'mastery_score' => $row['mastery_score'],
  6277. 'prerequisite' => $row['prerequisite'],
  6278. 'max_time_allowed' => $row['max_time_allowed']
  6279. );
  6280. }
  6281. $legend = '<legend>';
  6282. if ($action == 'add') {
  6283. $legend .= get_lang('CreateTheExercise');
  6284. } elseif ($action == 'move') {
  6285. $legend .= get_lang('MoveTheCurrentExercise');
  6286. } else {
  6287. $legend .= get_lang('EditCurrentExecice');
  6288. }
  6289. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6290. $legend .= Display:: return_message(
  6291. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6292. );
  6293. }
  6294. $legend .= '</legend>';
  6295. $return = '<form method="POST">';
  6296. $return .= $legend;
  6297. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6298. $return .= '<tr>';
  6299. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6300. $return .= '<td class="input">';
  6301. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6302. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6303. $arrHide = array(
  6304. $id
  6305. );
  6306. if (count($arrLP) > 0) {
  6307. for ($i = 0; $i < count($arrLP); $i++) {
  6308. if ($action != 'add') {
  6309. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6310. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6311. } else {
  6312. $arrHide[] = $arrLP[$i]['id'];
  6313. }
  6314. } else {
  6315. if ($arrLP[$i]['item_type'] == 'dir')
  6316. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6317. }
  6318. }
  6319. reset($arrLP);
  6320. }
  6321. $return .= '</select>';
  6322. $return .= '</td>';
  6323. $return .= '</tr>';
  6324. $return .= '<tr>';
  6325. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6326. $return .= '<td class="input">';
  6327. $return .= '<select id="previous" name="previous" size="1">';
  6328. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6329. for ($i = 0; $i < count($arrLP); $i++) {
  6330. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6331. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6332. $selected = 'selected="selected" ';
  6333. elseif ($action == 'add') $selected = 'selected="selected" ';
  6334. else
  6335. $selected = '';
  6336. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6337. }
  6338. }
  6339. $return .= '</select>';
  6340. $return .= '</td>';
  6341. $return .= '</tr>';
  6342. if ($action != 'move') {
  6343. $return .= '<tr>';
  6344. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6345. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6346. $return .= '</tr>';
  6347. $id_prerequisite = 0;
  6348. if (is_array($arrLP) && count($arrLP) > 0) {
  6349. foreach ($arrLP as $key => $value) {
  6350. if ($value['id'] == $id) {
  6351. $id_prerequisite = $value['prerequisite'];
  6352. break;
  6353. }
  6354. }
  6355. $arrHide = array ();
  6356. for ($i = 0; $i < count($arrLP); $i++) {
  6357. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6358. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6359. $s_selected_position = $arrLP[$i]['id'];
  6360. elseif ($action == 'add') $s_selected_position = 0;
  6361. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6362. }
  6363. }
  6364. }
  6365. }
  6366. $return .= '<tr>';
  6367. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6368. $return .= '</tr>';
  6369. $return .= '</table>';
  6370. if ($action == 'move') {
  6371. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6372. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6373. }
  6374. if (is_numeric($extra_info)) {
  6375. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6376. } elseif (is_array($extra_info)) {
  6377. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6378. }
  6379. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6380. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6381. $return .= '</form>';
  6382. return $return;
  6383. }
  6384. /**
  6385. * Return the form to display the forum edit/add option
  6386. * @param string Action (add/edit)
  6387. * @param integer ID of the lp_item if already exists
  6388. * @param mixed Forum ID or title
  6389. * @return string HTML form
  6390. */
  6391. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6392. {
  6393. $course_id = api_get_course_int_id();
  6394. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6395. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6396. if ($id != 0 && is_array($extra_info)) {
  6397. $item_title = stripslashes($extra_info['title']);
  6398. } elseif (is_numeric($extra_info)) {
  6399. $sql = "SELECT forum_title as title, forum_comment as comment
  6400. FROM " . $tbl_forum . "
  6401. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6402. $result = Database::query($sql);
  6403. $row = Database :: fetch_array($result);
  6404. $item_title = $row['title'];
  6405. $item_description = $row['comment'];
  6406. } else {
  6407. $item_title = '';
  6408. $item_description = '';
  6409. }
  6410. if ($id != 0 && is_array($extra_info)) {
  6411. $parent = $extra_info['parent_item_id'];
  6412. } else {
  6413. $parent = 0;
  6414. }
  6415. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6416. WHERE
  6417. c_id = ".$course_id." AND
  6418. lp_id = " . $this->lp_id;
  6419. $result = Database::query($sql);
  6420. $arrLP = array();
  6421. while ($row = Database :: fetch_array($result)) {
  6422. $arrLP[] = array (
  6423. 'id' => $row['id'],
  6424. 'item_type' => $row['item_type'],
  6425. 'title' => $row['title'],
  6426. 'path' => $row['path'],
  6427. 'description' => $row['description'],
  6428. 'parent_item_id' => $row['parent_item_id'],
  6429. 'previous_item_id' => $row['previous_item_id'],
  6430. 'next_item_id' => $row['next_item_id'],
  6431. 'display_order' => $row['display_order'],
  6432. 'max_score' => $row['max_score'],
  6433. 'min_score' => $row['min_score'],
  6434. 'mastery_score' => $row['mastery_score'],
  6435. 'prerequisite' => $row['prerequisite']
  6436. );
  6437. }
  6438. $this->tree_array($arrLP);
  6439. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6440. unset($this->arrMenu);
  6441. if ($action == 'add') {
  6442. $legend = get_lang('CreateTheForum');
  6443. } elseif ($action == 'move') {
  6444. $legend = get_lang('MoveTheCurrentForum');
  6445. } else {
  6446. $legend = get_lang('EditCurrentForum');
  6447. }
  6448. $form = new FormValidator('forum_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6449. $defaults = [];
  6450. $form->addHeader($legend);
  6451. if ($action != 'move') {
  6452. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  6453. $defaults['title'] = $item_title;
  6454. }
  6455. $selectParent = $form->addSelect(
  6456. 'parent',
  6457. get_lang('Parent'),
  6458. [],
  6459. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6460. );
  6461. $selectParent->addOption($this->name, 0);
  6462. $arrHide = array(
  6463. $id
  6464. );
  6465. //$parent_item_id = $_SESSION['parent_item_id'];
  6466. for ($i = 0; $i < count($arrLP); $i++) {
  6467. if ($action != 'add') {
  6468. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6469. $selectParent->addOption(
  6470. $arrLP[$i]['title'],
  6471. $arrLP[$i]['id'],
  6472. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6473. );
  6474. if ($parent == $arrLP[$i]['id']) {
  6475. $selectParent->setSelected($arrLP[$i]['id']);
  6476. }
  6477. } else {
  6478. $arrHide[] = $arrLP[$i]['id'];
  6479. }
  6480. } else {
  6481. if ($arrLP[$i]['item_type'] == 'dir') {
  6482. $selectParent->addOption(
  6483. $arrLP[$i]['title'],
  6484. $arrLP[$i]['id'],
  6485. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6486. );
  6487. if ($parent == $arrLP[$i]['id']) {
  6488. $selectParent->setSelected($arrLP[$i]['id']);
  6489. }
  6490. }
  6491. }
  6492. }
  6493. if (is_array($arrLP)) {
  6494. reset($arrLP);
  6495. }
  6496. $selectPrevious = $form->addSelect(
  6497. 'previous',
  6498. get_lang('Position'),
  6499. [],
  6500. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6501. );
  6502. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6503. for ($i = 0; $i < count($arrLP); $i++) {
  6504. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6505. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6506. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6507. $selectPrevious->setSelected($arrLP[$i]['id']);
  6508. } elseif ($action == 'add') {
  6509. $selectPrevious->setSelected($arrLP[$i]['id']);
  6510. }
  6511. }
  6512. }
  6513. if ($action != 'move') {
  6514. $id_prerequisite = 0;
  6515. if (is_array($arrLP)) {
  6516. foreach ($arrLP as $key => $value) {
  6517. if ($value['id'] == $id) {
  6518. $id_prerequisite = $value['prerequisite'];
  6519. break;
  6520. }
  6521. }
  6522. }
  6523. $arrHide = array();
  6524. for ($i = 0; $i < count($arrLP); $i++) {
  6525. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6526. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6527. $s_selected_position = $arrLP[$i]['id'];
  6528. elseif ($action == 'add') $s_selected_position = 0;
  6529. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6530. }
  6531. }
  6532. }
  6533. if ($action == 'add') {
  6534. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6535. } else {
  6536. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6537. }
  6538. if ($action == 'move') {
  6539. $form->addHidden('title', $item_title);
  6540. $form->addHidden('description', $item_description);
  6541. }
  6542. if (is_numeric($extra_info)) {
  6543. $form->addHidden('path', $extra_info);
  6544. } elseif (is_array($extra_info)) {
  6545. $form->addHidden('path', $extra_info['path']);
  6546. }
  6547. $form->addHidden('type', TOOL_FORUM);
  6548. $form->addHidden('post_time', time());
  6549. $form->setDefaults($defaults);
  6550. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6551. }
  6552. /**
  6553. * Return HTML form to add/edit forum threads
  6554. * @param string Action (add/edit)
  6555. * @param integer Item ID if already exists in learning path
  6556. * @param mixed Extra information (thread ID if integer)
  6557. * @return string HTML form
  6558. */
  6559. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6560. {
  6561. $course_id = api_get_course_int_id();
  6562. if (empty($course_id)) {
  6563. return null;
  6564. }
  6565. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6566. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6567. if ($id != 0 && is_array($extra_info)) {
  6568. $item_title = stripslashes($extra_info['title']);
  6569. } elseif (is_numeric($extra_info)) {
  6570. $sql = "SELECT thread_title as title FROM $tbl_forum
  6571. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6572. $result = Database::query($sql);
  6573. $row = Database :: fetch_array($result);
  6574. $item_title = $row['title'];
  6575. $item_description = '';
  6576. } else {
  6577. $item_title = '';
  6578. $item_description = '';
  6579. }
  6580. if ($id != 0 && is_array($extra_info)) {
  6581. $parent = $extra_info['parent_item_id'];
  6582. } else {
  6583. $parent = 0;
  6584. }
  6585. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6586. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6587. $result = Database::query($sql);
  6588. $arrLP = array ();
  6589. while ($row = Database :: fetch_array($result)) {
  6590. $arrLP[] = array (
  6591. 'id' => $row['id'],
  6592. 'item_type' => $row['item_type'],
  6593. 'title' => $row['title'],
  6594. 'path' => $row['path'],
  6595. 'description' => $row['description'],
  6596. 'parent_item_id' => $row['parent_item_id'],
  6597. 'previous_item_id' => $row['previous_item_id'],
  6598. 'next_item_id' => $row['next_item_id'],
  6599. 'display_order' => $row['display_order'],
  6600. 'max_score' => $row['max_score'],
  6601. 'min_score' => $row['min_score'],
  6602. 'mastery_score' => $row['mastery_score'],
  6603. 'prerequisite' => $row['prerequisite']
  6604. );
  6605. }
  6606. $this->tree_array($arrLP);
  6607. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6608. unset ($this->arrMenu);
  6609. $form = new FormValidator('thread_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6610. $defaults = [];
  6611. if ($action == 'add') {
  6612. $legend = get_lang('CreateTheForum');
  6613. } elseif ($action == 'move') {
  6614. $legend = get_lang('MoveTheCurrentForum');
  6615. } else {
  6616. $legend = get_lang('EditCurrentForum');
  6617. }
  6618. $form->addHeader($legend);
  6619. $selectParent = $form->addSelect(
  6620. 'parent',
  6621. get_lang('Parent'),
  6622. [],
  6623. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6624. );
  6625. $selectParent->addOption($this->name, 0);
  6626. $arrHide = array (
  6627. $id
  6628. );
  6629. for ($i = 0; $i < count($arrLP); $i++) {
  6630. if ($action != 'add') {
  6631. if (
  6632. ($arrLP[$i]['item_type'] == 'dir') &&
  6633. !in_array($arrLP[$i]['id'], $arrHide) &&
  6634. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6635. ) {
  6636. $selectParent->addOption(
  6637. $arrLP[$i]['title'],
  6638. $arrLP[$i]['id'],
  6639. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6640. );
  6641. if ($parent == $arrLP[$i]['id']) {
  6642. $selectParent->setSelected($arrLP[$i]['id']);
  6643. }
  6644. } else {
  6645. $arrHide[] = $arrLP[$i]['id'];
  6646. }
  6647. } else {
  6648. if ($arrLP[$i]['item_type'] == 'dir') {
  6649. $selectParent->addOption(
  6650. $arrLP[$i]['title'],
  6651. $arrLP[$i]['id'],
  6652. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6653. );
  6654. if ($parent == $arrLP[$i]['id']) {
  6655. $selectParent->setSelected($arrLP[$i]['id']);
  6656. }
  6657. }
  6658. }
  6659. }
  6660. if ($arrLP != null) {
  6661. reset($arrLP);
  6662. }
  6663. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6664. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6665. for ($i = 0; $i < count($arrLP); $i++) {
  6666. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6667. $selectPrevious->addOption(
  6668. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6669. $arrLP[$i]['id']
  6670. );
  6671. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6672. $selectPrevious->setSelected($arrLP[$i]['id']);
  6673. } elseif ($action == 'add') {
  6674. $selectPrevious->setSelected($arrLP[$i]['id']);
  6675. }
  6676. }
  6677. }
  6678. if ($action != 'move') {
  6679. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6680. $defaults['title'] = $item_title;
  6681. $id_prerequisite = 0;
  6682. if ($arrLP != null) {
  6683. foreach ($arrLP as $key => $value) {
  6684. if ($value['id'] == $id) {
  6685. $id_prerequisite = $value['prerequisite'];
  6686. break;
  6687. }
  6688. }
  6689. }
  6690. $arrHide = array();
  6691. $s_selected_position = 0;
  6692. for ($i = 0; $i < count($arrLP); $i++) {
  6693. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6694. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6695. $s_selected_position = $arrLP[$i]['id'];
  6696. elseif ($action == 'add') $s_selected_position = 0;
  6697. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6698. }
  6699. }
  6700. $selectPrerequisites = $form->addSelect(
  6701. 'prerequisites',
  6702. get_lang('LearnpathPrerequisites'),
  6703. [],
  6704. ['id' => 'prerequisites']
  6705. );
  6706. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6707. foreach ($arrHide as $key => $value) {
  6708. $selectPrerequisites->addOption($value['value'], $key);
  6709. if ($key == $s_selected_position && $action == 'add') {
  6710. $selectPrerequisites->setSelected($key);
  6711. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6712. $selectPrerequisites->setSelected($key);
  6713. }
  6714. }
  6715. }
  6716. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6717. if ($action == 'move') {
  6718. $form->addHidden('title', $item_title);
  6719. $form->addHidden('description', $item_description);
  6720. }
  6721. if (is_numeric($extra_info)) {
  6722. $form->addHidden('path', $extra_info);
  6723. }
  6724. elseif (is_array($extra_info)) {
  6725. $form->addHidden('path', $extra_info['path']);
  6726. }
  6727. $form->addHidden('type', TOOL_THREAD);
  6728. $form->addHidden('post_time', time());
  6729. $form->setDefaults($defaults);
  6730. return $form->returnForm();
  6731. }
  6732. /**
  6733. * Return the HTML form to display an item (generally a dir item)
  6734. * @param string Item type (dir)
  6735. * @param string Title (optional, only when creating)
  6736. * @param string Action ('add'/'edit')
  6737. * @param integer lp_item ID
  6738. * @param mixed Extra info
  6739. * @return string HTML form
  6740. */
  6741. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6742. {
  6743. $course_id = api_get_course_int_id();
  6744. $_course = api_get_course_info();
  6745. global $charset;
  6746. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6747. if ($id != 0 && is_array($extra_info)) {
  6748. $item_title = $extra_info['title'];
  6749. $item_description = $extra_info['description'];
  6750. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6751. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6752. } else {
  6753. $item_title = '';
  6754. $item_description = '';
  6755. $item_path_fck = '';
  6756. }
  6757. if ($id != 0 && is_array($extra_info)) {
  6758. $parent = $extra_info['parent_item_id'];
  6759. } else {
  6760. $parent = 0;
  6761. }
  6762. $id = intval($id);
  6763. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6764. WHERE
  6765. c_id = ".$course_id." AND
  6766. lp_id = " . $this->lp_id . " AND
  6767. id != $id";
  6768. if ($item_type == 'dir')
  6769. $sql .= " AND parent_item_id = 0";
  6770. $result = Database::query($sql);
  6771. $arrLP = array ();
  6772. while ($row = Database :: fetch_array($result)) {
  6773. $arrLP[] = array(
  6774. 'id' => $row['id'],
  6775. 'item_type' => $row['item_type'],
  6776. 'title' => $row['title'],
  6777. 'path' => $row['path'],
  6778. 'description' => $row['description'],
  6779. 'parent_item_id' => $row['parent_item_id'],
  6780. 'previous_item_id' => $row['previous_item_id'],
  6781. 'next_item_id' => $row['next_item_id'],
  6782. 'max_score' => $row['max_score'],
  6783. 'min_score' => $row['min_score'],
  6784. 'mastery_score' => $row['mastery_score'],
  6785. 'prerequisite' => $row['prerequisite'],
  6786. 'display_order' => $row['display_order']
  6787. );
  6788. }
  6789. $this->tree_array($arrLP);
  6790. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6791. unset($this->arrMenu);
  6792. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6793. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6794. $form = new FormValidator('form', 'POST', $url);
  6795. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6796. $defaults['description'] = $item_description;
  6797. $form->addElement('header', $title);
  6798. //$arrHide = array($id);
  6799. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6800. $arrHide[0]['padding'] = 20;
  6801. $charset = api_get_system_encoding();
  6802. for ($i = 0; $i < count($arrLP); $i++) {
  6803. if ($action != 'add') {
  6804. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6805. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6806. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6807. if ($parent == $arrLP[$i]['id']) {
  6808. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6809. }
  6810. }
  6811. } else {
  6812. if ($arrLP[$i]['item_type'] == 'dir') {
  6813. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6814. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6815. if ($parent == $arrLP[$i]['id']) {
  6816. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6817. }
  6818. }
  6819. }
  6820. }
  6821. if ($action != 'move') {
  6822. $form->addElement('text', 'title', get_lang('Title'));
  6823. $form->applyFilter('title', 'html_filter');
  6824. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6825. } else {
  6826. $form->addElement('hidden', 'title');
  6827. }
  6828. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6829. foreach ($arrHide as $key => $value) {
  6830. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6831. }
  6832. if (!empty($s_selected_parent)) {
  6833. $parent_select->setSelected($s_selected_parent);
  6834. }
  6835. if (is_array($arrLP)) {
  6836. reset($arrLP);
  6837. }
  6838. $arrHide = array();
  6839. // POSITION
  6840. for ($i = 0; $i < count($arrLP); $i++) {
  6841. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6842. //this is the same!
  6843. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6844. $s_selected_position = $arrLP[$i]['id'];
  6845. } elseif ($action == 'add') {
  6846. $s_selected_position = $arrLP[$i]['id'];
  6847. }
  6848. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6849. }
  6850. }
  6851. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6852. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6853. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6854. foreach ($arrHide as $key => $value) {
  6855. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6856. }
  6857. if (!empty ($s_selected_position)) {
  6858. $position->setSelected($s_selected_position);
  6859. }
  6860. if (is_array($arrLP)) {
  6861. reset($arrLP);
  6862. }
  6863. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6864. //fix in order to use the tab
  6865. if ($item_type == 'dir') {
  6866. $form->addElement('hidden', 'type', 'dir');
  6867. }
  6868. $extension = null;
  6869. if (!empty($item_path)) {
  6870. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6871. }
  6872. //assets can't be modified
  6873. //$item_type == 'asset' ||
  6874. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6875. if ($item_type == 'sco') {
  6876. $form->addElement('html', '<script>alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6877. }
  6878. $renderer = $form->defaultRenderer();
  6879. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6880. $relative_prefix = '';
  6881. $editor_config = array(
  6882. 'ToolbarSet' => 'LearningPathDocuments',
  6883. 'Width' => '100%',
  6884. 'Height' => '500',
  6885. 'FullPage' => true,
  6886. 'CreateDocumentDir' => $relative_prefix,
  6887. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  6888. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  6889. );
  6890. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6891. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  6892. $defaults['content_lp'] = file_get_contents($content_path);
  6893. }
  6894. $form->addElement('hidden', 'type', $item_type);
  6895. $form->addElement('hidden', 'post_time', time());
  6896. $form->setDefaults($defaults);
  6897. return $form->return_form();
  6898. }
  6899. /**
  6900. * Returns the form to update or create a document
  6901. * @param string Action (add/edit)
  6902. * @param integer ID of the lp_item (if already exists)
  6903. * @param mixed Integer if document ID, string if info ('new')
  6904. * @return string HTML form
  6905. */
  6906. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6907. {
  6908. $course_id = api_get_course_int_id();
  6909. $_course = api_get_course_info();
  6910. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6911. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6912. $no_display_edit_textarea = false;
  6913. $item_description = '';
  6914. //If action==edit document
  6915. //We don't display the document form if it's not an editable document (html or txt file)
  6916. if ($action == "edit") {
  6917. if (is_array($extra_info)) {
  6918. $path_parts = pathinfo($extra_info['dir']);
  6919. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6920. $no_display_edit_textarea = true;
  6921. }
  6922. }
  6923. }
  6924. $no_display_add = false;
  6925. // If action==add an existing document
  6926. // We don't display the document form if it's not an editable document (html or txt file).
  6927. if ($action == "add") {
  6928. if (is_numeric($extra_info)) {
  6929. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6930. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6931. $result = Database::query($sql_doc);
  6932. $path_file = Database :: result($result, 0, 0);
  6933. $path_parts = pathinfo($path_file);
  6934. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6935. $no_display_add = true;
  6936. }
  6937. }
  6938. }
  6939. if ($id != 0 && is_array($extra_info)) {
  6940. $item_title = stripslashes($extra_info['title']);
  6941. $item_description = stripslashes($extra_info['description']);
  6942. $item_terms = stripslashes($extra_info['terms']);
  6943. if (empty ($item_title)) {
  6944. $path_parts = pathinfo($extra_info['path']);
  6945. $item_title = stripslashes($path_parts['filename']);
  6946. }
  6947. } elseif (is_numeric($extra_info)) {
  6948. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6949. WHERE
  6950. c_id = ".$course_id." AND
  6951. id = " . intval($extra_info);
  6952. $result = Database::query($sql_doc);
  6953. $row = Database::fetch_array($result);
  6954. $item_title = $row['title'];
  6955. $item_title = str_replace('_', ' ', $item_title);
  6956. if (empty ($item_title)) {
  6957. $path_parts = pathinfo($row['path']);
  6958. $item_title = stripslashes($path_parts['filename']);
  6959. }
  6960. } else {
  6961. $item_title = '';
  6962. $item_description = '';
  6963. }
  6964. $return = '<legend>';
  6965. if ($id != 0 && is_array($extra_info)) {
  6966. $parent = $extra_info['parent_item_id'];
  6967. } else {
  6968. $parent = 0;
  6969. }
  6970. $sql = "SELECT * FROM $tbl_lp_item
  6971. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  6972. $result = Database::query($sql);
  6973. $arrLP = array ();
  6974. while ($row = Database :: fetch_array($result)) {
  6975. $arrLP[] = array(
  6976. 'id' => $row['id'],
  6977. 'item_type' => $row['item_type'],
  6978. 'title' => $row['title'],
  6979. 'path' => $row['path'],
  6980. 'description' => $row['description'],
  6981. 'parent_item_id' => $row['parent_item_id'],
  6982. 'previous_item_id' => $row['previous_item_id'],
  6983. 'next_item_id' => $row['next_item_id'],
  6984. 'display_order' => $row['display_order'],
  6985. 'max_score' => $row['max_score'],
  6986. 'min_score' => $row['min_score'],
  6987. 'mastery_score' => $row['mastery_score'],
  6988. 'prerequisite' => $row['prerequisite']
  6989. );
  6990. }
  6991. $this->tree_array($arrLP);
  6992. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6993. unset ($this->arrMenu);
  6994. if ($action == 'add') {
  6995. $return .= get_lang('CreateTheDocument');
  6996. } elseif ($action == 'move') {
  6997. $return .= get_lang('MoveTheCurrentDocument');
  6998. } else {
  6999. $return .= get_lang('EditTheCurrentDocument');
  7000. }
  7001. $return .= '</legend>';
  7002. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7003. $return .= Display :: return_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7004. }
  7005. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  7006. $defaults['title'] = Security :: remove_XSS($item_title);
  7007. if (empty($item_title)) {
  7008. $defaults['title'] = Security::remove_XSS($item_title);
  7009. }
  7010. $defaults['description'] = $item_description;
  7011. $form->addElement('html', $return);
  7012. if ($action != 'move') {
  7013. $data = $this->generate_lp_folder($_course);
  7014. $folders = DocumentManager::get_all_document_folders(
  7015. $_course,
  7016. 0,
  7017. true
  7018. );
  7019. DocumentManager::build_directory_selector(
  7020. $folders,
  7021. '',
  7022. array(),
  7023. true,
  7024. $form,
  7025. 'directory_parent_id'
  7026. );
  7027. if (isset($data['id'])) {
  7028. $defaults['directory_parent_id'] = $data['id'];
  7029. }
  7030. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  7031. $form->applyFilter('title', 'html_filter');
  7032. }
  7033. $arrHide[0]['value'] = $this->name;
  7034. $arrHide[0]['padding'] = 20;
  7035. for ($i = 0; $i < count($arrLP); $i++) {
  7036. if ($action != 'add') {
  7037. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7038. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7039. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7040. if ($parent == $arrLP[$i]['id']) {
  7041. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7042. }
  7043. }
  7044. } else {
  7045. if ($arrLP[$i]['item_type'] == 'dir') {
  7046. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7047. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7048. if ($parent == $arrLP[$i]['id']) {
  7049. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7050. }
  7051. }
  7052. }
  7053. }
  7054. $parent_select = $form->addSelect('parent', get_lang('Parent'), [], ['id' => 'idParent', 'onchange' => 'javascript: load_cbo(this.value);']);
  7055. $my_count=0;
  7056. foreach ($arrHide as $key => $value) {
  7057. if ($my_count!=0) {
  7058. // The LP name is also the first section and is not in the same charset like the other sections.
  7059. $value['value'] = Security :: remove_XSS($value['value']);
  7060. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7061. } else {
  7062. $value['value'] = Security :: remove_XSS($value['value']);
  7063. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7064. }
  7065. $my_count++;
  7066. }
  7067. if (!empty($id)) {
  7068. $parent_select->setSelected($parent);
  7069. } else {
  7070. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7071. $parent_select->setSelected($parent_item_id);
  7072. }
  7073. if (is_array($arrLP)) {
  7074. reset($arrLP);
  7075. }
  7076. $arrHide = array();
  7077. $s_selected_position = null;
  7078. //POSITION
  7079. for ($i = 0; $i < count($arrLP); $i++) {
  7080. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id || $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  7081. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'] || $action == 'add') {
  7082. $s_selected_position = $arrLP[$i]['id'];
  7083. }
  7084. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7085. }
  7086. }
  7087. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7088. $position->addOption(get_lang('FirstPosition'), 0);
  7089. foreach ($arrHide as $key => $value) {
  7090. $padding = isset($value['padding']) ? $value['padding']: 20;
  7091. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  7092. }
  7093. $position->setSelected($s_selected_position);
  7094. if (is_array($arrLP)) {
  7095. reset($arrLP);
  7096. }
  7097. if ($action != 'move') {
  7098. $id_prerequisite = 0;
  7099. if (is_array($arrLP)) {
  7100. foreach ($arrLP as $key => $value) {
  7101. if ($value['id'] == $id) {
  7102. $id_prerequisite = $value['prerequisite'];
  7103. break;
  7104. }
  7105. }
  7106. }
  7107. $arrHide = array();
  7108. for ($i = 0; $i < count($arrLP); $i++) {
  7109. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7110. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7111. $s_selected_position = $arrLP[$i]['id'];
  7112. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7113. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7114. }
  7115. }
  7116. if (!$no_display_add) {
  7117. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7118. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7119. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true')) {
  7120. if (isset ($_POST['content']))
  7121. $content = stripslashes($_POST['content']);
  7122. elseif (is_array($extra_info)) {
  7123. //If it's an html document or a text file
  7124. if (!$no_display_edit_textarea) {
  7125. $content = $this->display_document($extra_info['path'], false, false);
  7126. }
  7127. } elseif (is_numeric($extra_info))
  7128. $content = $this->display_document($extra_info, false, false);
  7129. else
  7130. $content = '';
  7131. if (!$no_display_edit_textarea) {
  7132. // We need to calculate here some specific settings for the online editor.
  7133. // The calculated settings work for documents in the Documents tool
  7134. // (on the root or in subfolders).
  7135. // For documents in native scorm packages it is unclear whether the
  7136. // online editor should be activated or not.
  7137. // A new document, it is in the root of the repository.
  7138. $relative_path = '';
  7139. $relative_prefix = '';
  7140. if (is_array($extra_info) && $extra_info != 'new') {
  7141. // The document already exists. Whe have to determine its relative path towards the repository root.
  7142. $relative_path = explode('/', $extra_info['dir']);
  7143. $cnt = count($relative_path) - 2;
  7144. if ($cnt < 0) {
  7145. $cnt = 0;
  7146. }
  7147. $relative_prefix = str_repeat('../', $cnt);
  7148. $relative_path = array_slice($relative_path, 1, $cnt);
  7149. $relative_path = implode('/', $relative_path);
  7150. if (strlen($relative_path) > 0) {
  7151. $relative_path = $relative_path . '/';
  7152. }
  7153. } else {
  7154. $result = $this->generate_lp_folder($_course);
  7155. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7156. $relative_prefix = '../../';
  7157. }
  7158. $editor_config = array(
  7159. 'ToolbarSet' => 'LearningPathDocuments',
  7160. 'Width' => '100%',
  7161. 'Height' => '500',
  7162. 'FullPage' => true,
  7163. 'CreateDocumentDir' => $relative_prefix,
  7164. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7165. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7166. );
  7167. if ($_GET['action'] == 'add_item') {
  7168. $class = 'add';
  7169. $text = get_lang('LPCreateDocument');
  7170. } else {
  7171. if ($_GET['action'] == 'edit_item') {
  7172. $class = 'save';
  7173. $text = get_lang('SaveDocument');
  7174. }
  7175. }
  7176. $form->addButtonSave($text, 'submit_button');
  7177. $renderer = $form->defaultRenderer();
  7178. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7179. $form->addElement('html', '<div class="editor-lp">');
  7180. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7181. $form->addElement('html', '</div>');
  7182. $defaults['content_lp'] = $content;
  7183. }
  7184. } elseif (is_numeric($extra_info)) {
  7185. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7186. $return = $this->display_document($extra_info, true, true, true);
  7187. $form->addElement('html', $return);
  7188. }
  7189. }
  7190. }
  7191. if ($action == 'move') {
  7192. $form->addElement('hidden', 'title', $item_title);
  7193. $form->addElement('hidden', 'description', $item_description);
  7194. }
  7195. if (is_numeric($extra_info)) {
  7196. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7197. $form->addElement('hidden', 'path', $extra_info);
  7198. } elseif (is_array($extra_info)) {
  7199. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7200. $form->addElement('hidden', 'path', $extra_info['path']);
  7201. }
  7202. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7203. $form->addElement('hidden', 'post_time', time());
  7204. $form->setDefaults($defaults);
  7205. return $form->returnForm();
  7206. }
  7207. /**
  7208. * Return HTML form to add/edit a link item
  7209. * @param string $action (add/edit)
  7210. * @param integer $id Item ID if exists
  7211. * @param mixed $extra_info
  7212. * @return string HTML form
  7213. */
  7214. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7215. {
  7216. $course_id = api_get_course_int_id();
  7217. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7218. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7219. if ($id != 0 && is_array($extra_info)) {
  7220. $item_title = stripslashes($extra_info['title']);
  7221. $item_description = stripslashes($extra_info['description']);
  7222. $item_url = stripslashes($extra_info['url']);
  7223. } elseif (is_numeric($extra_info)) {
  7224. $extra_info = intval($extra_info);
  7225. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7226. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7227. $result = Database::query($sql);
  7228. $row = Database :: fetch_array($result);
  7229. $item_title = $row['title'];
  7230. $item_description = $row['description'];
  7231. $item_url = $row['url'];
  7232. } else {
  7233. $item_title = '';
  7234. $item_description = '';
  7235. $item_url = '';
  7236. }
  7237. $form = new FormValidator('edit_link', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  7238. $defaults = [];
  7239. if ($id != 0 && is_array($extra_info)) {
  7240. $parent = $extra_info['parent_item_id'];
  7241. } else {
  7242. $parent = 0;
  7243. }
  7244. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7245. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7246. $result = Database::query($sql);
  7247. $arrLP = array();
  7248. while ($row = Database :: fetch_array($result)) {
  7249. $arrLP[] = array(
  7250. 'id' => $row['id'],
  7251. 'item_type' => $row['item_type'],
  7252. 'title' => $row['title'],
  7253. 'path' => $row['path'],
  7254. 'description' => $row['description'],
  7255. 'parent_item_id' => $row['parent_item_id'],
  7256. 'previous_item_id' => $row['previous_item_id'],
  7257. 'next_item_id' => $row['next_item_id'],
  7258. 'display_order' => $row['display_order'],
  7259. 'max_score' => $row['max_score'],
  7260. 'min_score' => $row['min_score'],
  7261. 'mastery_score' => $row['mastery_score'],
  7262. 'prerequisite' => $row['prerequisite']
  7263. );
  7264. }
  7265. $this->tree_array($arrLP);
  7266. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7267. unset ($this->arrMenu);
  7268. if ($action == 'add') {
  7269. $legend = get_lang('CreateTheLink');
  7270. } elseif ($action == 'move') {
  7271. $legend = get_lang('MoveCurrentLink');
  7272. } else {
  7273. $legend = get_lang('EditCurrentLink');
  7274. }
  7275. $form->addHeader($legend);
  7276. if ($action != 'move') {
  7277. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7278. $defaults['title'] = $item_title;
  7279. }
  7280. $selectParent = $form->addSelect(
  7281. 'parent',
  7282. get_lang('Parent'),
  7283. [],
  7284. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7285. );
  7286. $selectParent->addOption($this->name, 0);
  7287. $arrHide = array(
  7288. $id
  7289. );
  7290. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7291. for ($i = 0; $i < count($arrLP); $i++) {
  7292. if ($action != 'add') {
  7293. if (
  7294. ($arrLP[$i]['item_type'] == 'dir') &&
  7295. !in_array($arrLP[$i]['id'], $arrHide) &&
  7296. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7297. ) {
  7298. $selectParent->addOption(
  7299. $arrLP[$i]['title'],
  7300. $arrLP[$i]['id'],
  7301. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7302. );
  7303. if ($parent == $arrLP[$i]['id']) {
  7304. $selectParent->setSelected($arrLP[$i]['id']);
  7305. }
  7306. } else {
  7307. $arrHide[] = $arrLP[$i]['id'];
  7308. }
  7309. } else {
  7310. if ($arrLP[$i]['item_type'] == 'dir') {
  7311. $selectParent->addOption(
  7312. $arrLP[$i]['title'],
  7313. $arrLP[$i]['id'],
  7314. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7315. );
  7316. if ($parent_item_id == $arrLP[$i]['id']) {
  7317. $selectParent->setSelected($arrLP[$i]['id']);
  7318. }
  7319. }
  7320. }
  7321. }
  7322. if (is_array($arrLP)) {
  7323. reset($arrLP);
  7324. }
  7325. $selectPrevious = $form->addSelect(
  7326. 'previous',
  7327. get_lang('Position'),
  7328. [],
  7329. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7330. );
  7331. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7332. for ($i = 0; $i < count($arrLP); $i++) {
  7333. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7334. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7335. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7336. $selectPrevious->setSelected($arrLP[$i]['id']);
  7337. } elseif ($action == 'add') {
  7338. $selectPrevious->setSelected($arrLP[$i]['id']);
  7339. }
  7340. }
  7341. }
  7342. if ($action != 'move') {
  7343. $urlAttributes = ['class' => 'learnpath_item_form'];
  7344. if (is_numeric($extra_info)) {
  7345. $urlAttributes['disabled'] = 'disabled';
  7346. }
  7347. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7348. $defaults['url'] = $item_url;
  7349. $id_prerequisite = 0;
  7350. if (is_array($arrLP)) {
  7351. foreach ($arrLP as $key => $value) {
  7352. if ($value['id'] == $id) {
  7353. $id_prerequisite = $value['prerequisite'];
  7354. break;
  7355. }
  7356. }
  7357. }
  7358. $arrHide = array();
  7359. for ($i = 0; $i < count($arrLP); $i++) {
  7360. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7361. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7362. $s_selected_position = $arrLP[$i]['id'];
  7363. elseif ($action == 'add') $s_selected_position = 0;
  7364. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7365. }
  7366. }
  7367. }
  7368. if ($action == 'add') {
  7369. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7370. } else {
  7371. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7372. }
  7373. if ($action == 'move') {
  7374. $form->addHidden('title', $item_title);
  7375. $form->addHidden('description', $item_description);
  7376. }
  7377. if (is_numeric($extra_info)) {
  7378. $form->addHidden('path', $extra_info);
  7379. } elseif (is_array($extra_info)) {
  7380. $form->addHidden('path', $extra_info['path']);
  7381. }
  7382. $form->addHidden('type', TOOL_LINK);
  7383. $form->addHidden('post_time', time());
  7384. $form->setDefaults($defaults);
  7385. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7386. }
  7387. /**
  7388. * Return HTML form to add/edit a student publication (work)
  7389. * @param string Action (add/edit)
  7390. * @param integer Item ID if already exists
  7391. * @param mixed Extra info (work ID if integer)
  7392. * @return string HTML form
  7393. */
  7394. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7395. {
  7396. $course_id = api_get_course_int_id();
  7397. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7398. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7399. if ($id != 0 && is_array($extra_info)) {
  7400. $item_title = stripslashes($extra_info['title']);
  7401. $item_description = stripslashes($extra_info['description']);
  7402. } elseif (is_numeric($extra_info)) {
  7403. $extra_info = intval($extra_info);
  7404. $sql = "SELECT title, description
  7405. FROM " . $tbl_publication . "
  7406. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7407. $result = Database::query($sql);
  7408. $row = Database :: fetch_array($result);
  7409. $item_title = $row['title'];
  7410. } else {
  7411. $item_title = get_lang('Student_publication');
  7412. }
  7413. if ($id != 0 && is_array($extra_info)) {
  7414. $parent = $extra_info['parent_item_id'];
  7415. } else {
  7416. $parent = 0;
  7417. }
  7418. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7419. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7420. $result = Database::query($sql);
  7421. $arrLP = array();
  7422. while ($row = Database :: fetch_array($result)) {
  7423. $arrLP[] = array(
  7424. 'id' => $row['id'],
  7425. 'item_type' => $row['item_type'],
  7426. 'title' => $row['title'],
  7427. 'path' => $row['path'],
  7428. 'description' => $row['description'],
  7429. 'parent_item_id' => $row['parent_item_id'],
  7430. 'previous_item_id' => $row['previous_item_id'],
  7431. 'next_item_id' => $row['next_item_id'],
  7432. 'display_order' => $row['display_order'],
  7433. 'max_score' => $row['max_score'],
  7434. 'min_score' => $row['min_score'],
  7435. 'mastery_score' => $row['mastery_score'],
  7436. 'prerequisite' => $row['prerequisite']
  7437. );
  7438. }
  7439. $this->tree_array($arrLP);
  7440. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7441. unset($this->arrMenu);
  7442. $form = new FormValidator('frm_student_publication', 'post', '#');
  7443. if ($action == 'add') {
  7444. $form->addHeader(get_lang('Student_publication'));
  7445. } elseif ($action == 'move') {
  7446. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7447. } else {
  7448. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7449. }
  7450. if ($action != 'move') {
  7451. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7452. }
  7453. $parentSelect = $form->addSelect(
  7454. 'parent',
  7455. get_lang('Parent'),
  7456. ['0' => $this->name],
  7457. [
  7458. 'onchange' => 'javascript: load_cbo(this.value);',
  7459. 'class' => 'learnpath_item_form',
  7460. 'id' => 'idParent'
  7461. ]
  7462. );
  7463. $arrHide = array(
  7464. $id
  7465. );
  7466. for ($i = 0; $i < count($arrLP); $i++) {
  7467. if ($action != 'add') {
  7468. if (
  7469. ($arrLP[$i]['item_type'] == 'dir') &&
  7470. !in_array($arrLP[$i]['id'], $arrHide) &&
  7471. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7472. ) {
  7473. $parentSelect->addOption(
  7474. $arrLP[$i]['title'],
  7475. $arrLP[$i]['id'],
  7476. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7477. );
  7478. if ($parent == $arrLP[$i]['id']) {
  7479. $parentSelect->setSelected($arrLP[$i]['id']);
  7480. }
  7481. } else {
  7482. $arrHide[] = $arrLP[$i]['id'];
  7483. }
  7484. } else {
  7485. if ($arrLP[$i]['item_type'] == 'dir') {
  7486. $parentSelect->addOption(
  7487. $arrLP[$i]['title'],
  7488. $arrLP[$i]['id'],
  7489. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7490. );
  7491. if ($parent == $arrLP[$i]['id']) {
  7492. $parentSelect->setSelected($arrLP[$i]['id']);
  7493. }
  7494. }
  7495. }
  7496. }
  7497. if (is_array($arrLP)) {
  7498. reset($arrLP);
  7499. }
  7500. $previousSelect = $form->addSelect(
  7501. 'previous',
  7502. get_lang('Position'),
  7503. ['0' => get_lang('FirstPosition')],
  7504. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7505. );
  7506. for ($i = 0; $i < count($arrLP); $i++) {
  7507. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7508. $previousSelect->addOption(
  7509. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7510. $arrLP[$i]['id']
  7511. );
  7512. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7513. $previousSelect->setSelected($arrLP[$i]['id']);
  7514. } elseif ($action == 'add') {
  7515. $previousSelect->setSelected($arrLP[$i]['id']);
  7516. }
  7517. }
  7518. }
  7519. if ($action != 'move') {
  7520. $id_prerequisite = 0;
  7521. if (is_array($arrLP)) {
  7522. foreach ($arrLP as $key => $value) {
  7523. if ($value['id'] == $id) {
  7524. $id_prerequisite = $value['prerequisite'];
  7525. break;
  7526. }
  7527. }
  7528. }
  7529. $arrHide = array ();
  7530. for ($i = 0; $i < count($arrLP); $i++) {
  7531. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7532. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7533. $s_selected_position = $arrLP[$i]['id'];
  7534. elseif ($action == 'add') $s_selected_position = 0;
  7535. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7536. }
  7537. }
  7538. }
  7539. if ($action == 'add') {
  7540. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7541. } else {
  7542. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7543. }
  7544. if ($action == 'move') {
  7545. $form->addHidden('title', $item_title);
  7546. $form->addHidden('description', $item_description);
  7547. }
  7548. if (is_numeric($extra_info)) {
  7549. $form->addHidden('path', $extra_info);
  7550. } elseif (is_array($extra_info)) {
  7551. $form->addHidden('path', $extra_info['path']);
  7552. }
  7553. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7554. $form->addHidden('post_time', time());
  7555. $form->setDefaults(['title' => $item_title]);
  7556. $return = '<div class="sectioncomment">';
  7557. $return .= $form->returnForm();
  7558. $return .= '</div>';
  7559. return $return;
  7560. }
  7561. /**
  7562. * Displays the menu for manipulating a step
  7563. *
  7564. * @param $item_id
  7565. * @param string $item_type
  7566. * @return string
  7567. */
  7568. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7569. {
  7570. $_course = api_get_course_info();
  7571. $course_id = api_get_course_int_id();
  7572. $course_code = api_get_course_id();
  7573. $return = '<div class="actions">';
  7574. switch ($item_type) {
  7575. case 'dir':
  7576. // Commented the message cause should not show it.
  7577. //$lang = get_lang('TitleManipulateChapter');
  7578. break;
  7579. case TOOL_LP_FINAL_ITEM:
  7580. case TOOL_DOCUMENT:
  7581. // Commented the message cause should not show it.
  7582. //$lang = get_lang('TitleManipulateDocument');
  7583. break;
  7584. case TOOL_LINK:
  7585. case 'link' :
  7586. // Commented the message cause should not show it.
  7587. //$lang = get_lang('TitleManipulateLink');
  7588. break;
  7589. case TOOL_QUIZ:
  7590. // Commented the message cause should not show it.
  7591. //$lang = get_lang('TitleManipulateQuiz');
  7592. break;
  7593. case TOOL_STUDENTPUBLICATION:
  7594. // Commented the message cause should not show it.
  7595. //$lang = get_lang('TitleManipulateStudentPublication');
  7596. break;
  7597. }
  7598. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7599. $item_id = intval($item_id);
  7600. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7601. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7602. $result = Database::query($sql);
  7603. $row = Database::fetch_assoc($result);
  7604. $audio_player = null;
  7605. // We display an audio player if needed.
  7606. if (!empty($row['audio'])) {
  7607. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7608. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7609. </div>';
  7610. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7611. $audio_player .= '<script>
  7612. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7613. s1.addParam("allowscriptaccess","always");
  7614. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7615. s1.write("container");
  7616. </script>';
  7617. }
  7618. $url = api_get_self().'?cidReq='.api_get_cidreq().'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7619. $return .= Display::url(
  7620. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7621. $url.'&action=edit_item&path_item=' . $row['path']
  7622. );
  7623. $return .= Display::url(
  7624. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7625. $url.'&action=move_item'
  7626. );
  7627. // Commented for now as prerequisites cannot be added to chapters.
  7628. if ($item_type != 'dir') {
  7629. $return .= Display::url(
  7630. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7631. $url.'&action=edit_item_prereq'
  7632. );
  7633. }
  7634. $return .= Display::url(
  7635. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7636. $url.'&action=delete_item'
  7637. );
  7638. if ($item_type == TOOL_HOTPOTATOES ) {
  7639. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7640. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7641. }
  7642. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7643. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7644. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7645. }
  7646. $return .= '</div>';
  7647. if (!empty($audio_player)) {
  7648. $return .= '<br />'.$audio_player;
  7649. }
  7650. return $return;
  7651. }
  7652. /**
  7653. * Creates the javascript needed for filling up the checkboxes without page reload
  7654. * @return string
  7655. */
  7656. public function get_js_dropdown_array()
  7657. {
  7658. $course_id = api_get_course_int_id();
  7659. $return = 'var child_name = new Array();' . "\n";
  7660. $return .= 'var child_value = new Array();' . "\n\n";
  7661. $return .= 'child_name[0] = new Array();' . "\n";
  7662. $return .= 'child_value[0] = new Array();' . "\n\n";
  7663. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7664. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7665. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7666. ORDER BY display_order ASC";
  7667. $res_zero = Database::query($sql_zero);
  7668. $i = 0;
  7669. while ($row_zero = Database :: fetch_array($res_zero)) {
  7670. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7671. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7672. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7673. }
  7674. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7675. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7676. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7677. }
  7678. }
  7679. $return .= "\n";
  7680. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7681. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7682. $res = Database::query($sql);
  7683. while ($row = Database :: fetch_array($res)) {
  7684. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7685. WHERE
  7686. c_id = ".$course_id." AND
  7687. parent_item_id = " . $row['id'] . "
  7688. ORDER BY display_order ASC";
  7689. $res_parent = Database::query($sql_parent);
  7690. $i = 0;
  7691. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7692. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7693. while ($row_parent = Database :: fetch_array($res_parent)) {
  7694. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7695. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7696. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7697. }
  7698. $return .= "\n";
  7699. }
  7700. return $return;
  7701. }
  7702. /**
  7703. * Display the form to allow moving an item
  7704. * @param integer Item ID
  7705. * @return string HTML form
  7706. */
  7707. public function display_move_item($item_id)
  7708. {
  7709. $course_id = api_get_course_int_id();
  7710. $return = '';
  7711. if (is_numeric($item_id)) {
  7712. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7713. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7714. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7715. $res = Database::query($sql);
  7716. $row = Database :: fetch_array($res);
  7717. switch ($row['item_type']) {
  7718. case 'dir':
  7719. case 'asset':
  7720. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7721. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7722. break;
  7723. case TOOL_DOCUMENT:
  7724. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7725. $return .= $this->display_document_form('move', $item_id, $row);
  7726. break;
  7727. case TOOL_LINK:
  7728. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7729. $return .= $this->display_link_form('move', $item_id, $row);
  7730. break;
  7731. case TOOL_HOTPOTATOES:
  7732. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7733. $return .= $this->display_link_form('move', $item_id, $row);
  7734. break;
  7735. case TOOL_QUIZ:
  7736. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7737. $return .= $this->display_quiz_form('move', $item_id, $row);
  7738. break;
  7739. case TOOL_STUDENTPUBLICATION:
  7740. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7741. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7742. break;
  7743. case TOOL_FORUM:
  7744. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7745. $return .= $this->display_forum_form('move', $item_id, $row);
  7746. break;
  7747. case TOOL_THREAD:
  7748. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7749. $return .= $this->display_forum_form('move', $item_id, $row);
  7750. break;
  7751. }
  7752. }
  7753. return $return;
  7754. }
  7755. /**
  7756. * Displays a basic form on the overview page for changing the item title and the item description.
  7757. * @param string $item_type
  7758. * @param string $title
  7759. * @param array $data
  7760. * @return string
  7761. */
  7762. public function display_item_small_form($item_type, $title = '', $data = array())
  7763. {
  7764. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7765. $form = new FormValidator('small_form', 'post', $url);
  7766. $form->addElement('header', $title);
  7767. $form->addElement('text', 'title', get_lang('Title'));
  7768. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7769. $form->addElement('hidden', 'id', $data['id']);
  7770. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7771. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7772. $form->setDefaults(array('title' => $data['title']));
  7773. return $form->toHtml();
  7774. }
  7775. /**
  7776. * Return HTML form to allow prerequisites selection
  7777. * @todo use FormValidator
  7778. * @param integer Item ID
  7779. * @return string HTML form
  7780. */
  7781. public function display_item_prerequisites_form($item_id)
  7782. {
  7783. $course_id = api_get_course_int_id();
  7784. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7785. $item_id = intval($item_id);
  7786. /* Current prerequisite */
  7787. $sql = "SELECT * FROM $tbl_lp_item
  7788. WHERE c_id = $course_id AND id = " . $item_id;
  7789. $result = Database::query($sql);
  7790. $row = Database::fetch_array($result);
  7791. $prerequisiteId = $row['prerequisite'];
  7792. $return = '<legend>';
  7793. $return .= get_lang('AddEditPrerequisites');
  7794. $return .= '</legend>';
  7795. $return .= '<form method="POST">';
  7796. $return .= '<table class="data_table">';
  7797. $return .= '<tr>';
  7798. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7799. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7800. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7801. $return .= '</tr>';
  7802. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7803. $return .= '<tr >';
  7804. $return .= '<td colspan="3" class="radio">';
  7805. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7806. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7807. $return .= '</tr>';
  7808. $sql = "SELECT * FROM $tbl_lp_item
  7809. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7810. $result = Database::query($sql);
  7811. $arrLP = array();
  7812. $selectedMinScore = array();
  7813. $selectedMaxScore = array();
  7814. while ($row = Database :: fetch_array($result)) {
  7815. if ($row['id'] == $item_id) {
  7816. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7817. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7818. }
  7819. $arrLP[] = array(
  7820. 'id' => $row['id'],
  7821. 'item_type' => $row['item_type'],
  7822. 'title' => $row['title'],
  7823. 'ref' => $row['ref'],
  7824. 'description' => $row['description'],
  7825. 'parent_item_id' => $row['parent_item_id'],
  7826. 'previous_item_id' => $row['previous_item_id'],
  7827. 'next_item_id' => $row['next_item_id'],
  7828. 'max_score' => $row['max_score'],
  7829. 'min_score' => $row['min_score'],
  7830. 'mastery_score' => $row['mastery_score'],
  7831. 'prerequisite' => $row['prerequisite'],
  7832. 'next_item_id' => $row['next_item_id'],
  7833. 'display_order' => $row['display_order'],
  7834. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7835. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7836. );
  7837. }
  7838. $this->tree_array($arrLP);
  7839. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7840. unset($this->arrMenu);
  7841. for ($i = 0; $i < count($arrLP); $i++) {
  7842. $item = $arrLP[$i];
  7843. if ($item['id'] == $item_id) {
  7844. break;
  7845. }
  7846. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7847. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7848. $return .= '<tr>';
  7849. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7850. $return .= '<label for="id' . $item['id'] . '">';
  7851. $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'] . '" />';
  7852. $icon_name = str_replace(' ', '', $item['item_type']);
  7853. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7854. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  7855. } else {
  7856. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7857. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  7858. } else {
  7859. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7860. }
  7861. }
  7862. $return .= $item['title'] . '</label>';
  7863. $return .= '</td>';
  7864. if ($item['item_type'] == TOOL_QUIZ) {
  7865. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7866. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7867. $tmp_obj_exercice = new Exercise();
  7868. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7869. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7870. $tmp_obj_lp_item->update_in_bdd();
  7871. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7872. $return .= '<td class="exercise">';
  7873. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7874. $return .= '</td>';
  7875. $return .= '<td class="exercise">';
  7876. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7877. $return .= '</td>';
  7878. }
  7879. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7880. $return .= '<td class="exercise">';
  7881. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7882. $return .= '</td>';
  7883. $return .= '<td class="exercise"">';
  7884. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7885. $return .= '</td>';
  7886. }
  7887. $return .= '</tr>';
  7888. }
  7889. $return .= '<tr>';
  7890. $return .= '</tr>';
  7891. $return .= '</table>';
  7892. $return .= '<div style="padding-top:3px;">';
  7893. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7894. $return .= '</form>';
  7895. return $return;
  7896. }
  7897. /**
  7898. * Return HTML list to allow prerequisites selection for lp
  7899. * @param integer Item ID
  7900. * @return string HTML form
  7901. */
  7902. public function display_lp_prerequisites_list()
  7903. {
  7904. $course_id = api_get_course_int_id();
  7905. $lp_id = $this->lp_id;
  7906. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7907. // get current prerequisite
  7908. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7909. $result = Database::query($sql);
  7910. $row = Database :: fetch_array($result);
  7911. $prerequisiteId = $row['prerequisite'];
  7912. $session_id = api_get_session_id();
  7913. $session_condition = api_get_session_condition($session_id, true, true);
  7914. $sql = "SELECT * FROM $tbl_lp
  7915. WHERE c_id = $course_id $session_condition
  7916. ORDER BY display_order ";
  7917. $rs = Database::query($sql);
  7918. $return = '';
  7919. $return .= '<select name="prerequisites" class="form-control">';
  7920. $return .= '<option value="0">'.get_lang('None').'</option>';
  7921. if (Database::num_rows($rs) > 0) {
  7922. while ($row = Database::fetch_array($rs)) {
  7923. if ($row['id'] == $lp_id) {
  7924. continue;
  7925. }
  7926. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7927. }
  7928. }
  7929. $return .= '</select>';
  7930. return $return;
  7931. }
  7932. /**
  7933. * Creates a list with all the documents in it
  7934. * @param bool $showInvisibleFiles
  7935. * @return string
  7936. */
  7937. public function get_documents($showInvisibleFiles = false)
  7938. {
  7939. $course_info = api_get_course_info();
  7940. $sessionId = api_get_session_id();
  7941. $documentTree = DocumentManager::get_document_preview(
  7942. $course_info,
  7943. $this->lp_id,
  7944. null,
  7945. $sessionId,
  7946. true,
  7947. null,
  7948. null,
  7949. $showInvisibleFiles,
  7950. true
  7951. );
  7952. $headers = array(
  7953. get_lang('Files'),
  7954. get_lang('CreateTheDocument'),
  7955. get_lang('Upload')
  7956. );
  7957. $form = new FormValidator(
  7958. 'form_upload',
  7959. 'POST',
  7960. api_get_self().'?'.$_SERVER['QUERY_STRING'],
  7961. '',
  7962. array('enctype' => "multipart/form-data")
  7963. );
  7964. $folders = DocumentManager::get_all_document_folders(
  7965. api_get_course_info(),
  7966. 0,
  7967. true
  7968. );
  7969. DocumentManager::build_directory_selector(
  7970. $folders,
  7971. '',
  7972. array(),
  7973. true,
  7974. $form,
  7975. 'directory_parent_id'
  7976. );
  7977. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  7978. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  7979. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  7980. $form->setDefaults(['if_exists' => 'rename']);
  7981. // Check box options
  7982. $form->addElement(
  7983. 'checkbox',
  7984. 'unzip',
  7985. get_lang('Options'),
  7986. get_lang('Uncompress')
  7987. );
  7988. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  7989. $form->addMultipleUpload($url);
  7990. $new = $this->display_document_form('add', 0);
  7991. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  7992. return $tabs;
  7993. }
  7994. /**
  7995. * Creates a list with all the exercises (quiz) in it
  7996. * @return string
  7997. */
  7998. public function get_exercises()
  7999. {
  8000. $course_id = api_get_course_int_id();
  8001. // New for hotpotatoes.
  8002. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8003. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8004. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8005. $session_id = api_get_session_id();
  8006. $condition_session = api_get_session_condition($session_id, true, true);
  8007. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8008. $activeCondition = " active <> -1 ";
  8009. if ($setting) {
  8010. $activeCondition = " active = 1 ";
  8011. }
  8012. $sql_quiz = "SELECT * FROM $tbl_quiz
  8013. WHERE c_id = $course_id AND $activeCondition $condition_session
  8014. ORDER BY title ASC";
  8015. $sql_hot = "SELECT * FROM $tbl_doc
  8016. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8017. ORDER BY id ASC";
  8018. $res_quiz = Database::query($sql_quiz);
  8019. $res_hot = Database::query($sql_hot);
  8020. $return = '<ul class="lp_resource">';
  8021. $return .= '<li class="lp_resource_element">';
  8022. $return .= Display::return_icon('new_exercice.png');
  8023. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8024. get_lang('NewExercise') . '</a>';
  8025. $return .= '</li>';
  8026. // Display hotpotatoes
  8027. while ($row_hot = Database :: fetch_array($res_hot)) {
  8028. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8029. $return .= '<a class="moved" href="#">';
  8030. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8031. $return .= '</a> ';
  8032. $return .= Display::return_icon('hotpotatoes_s.png');
  8033. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8034. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  8035. $return .= '</li>';
  8036. }
  8037. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8038. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8039. $return .= '<a class="moved" href="#">';
  8040. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8041. $return .= '</a> ';
  8042. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8043. $return .= '<a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8044. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  8045. '</a>';
  8046. $return .= '</li>';
  8047. }
  8048. $return .= '</ul>';
  8049. return $return;
  8050. }
  8051. /**
  8052. * Creates a list with all the links in it
  8053. * @return string
  8054. */
  8055. public function get_links()
  8056. {
  8057. $selfUrl = api_get_self();
  8058. $courseIdReq = api_get_cidreq();
  8059. $course = api_get_course_info();
  8060. $course_id = $course['real_id'];
  8061. $tbl_link = Database::get_course_table(TABLE_LINK);
  8062. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8063. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8064. $session_id = api_get_session_id();
  8065. $condition_session = api_get_session_condition(
  8066. $session_id,
  8067. true,
  8068. true,
  8069. "link.session_id"
  8070. );
  8071. $sql = "SELECT
  8072. link.id as link_id,
  8073. link.title as link_title,
  8074. link.category_id as category_id,
  8075. link_category.category_title as category_title
  8076. FROM $tbl_link as link
  8077. LEFT JOIN $linkCategoryTable as link_category
  8078. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8079. WHERE link.c_id = ".$course_id." $condition_session
  8080. ORDER BY link_category.category_title ASC, link.title ASC";
  8081. $links = Database::query($sql);
  8082. $categorizedLinks = array();
  8083. $categories = array();
  8084. while ($link = Database :: fetch_array($links)) {
  8085. if (!$link['category_id']) {
  8086. $link['category_title'] = get_lang('Uncategorized');
  8087. }
  8088. $categories[$link['category_id']] = $link['category_title'];
  8089. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  8090. }
  8091. $linksHtmlCode =
  8092. '<script>
  8093. function toggle_tool(tool, id){
  8094. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8095. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8096. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8097. } else {
  8098. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8099. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8100. }
  8101. }
  8102. </script>
  8103. <ul class="lp_resource">
  8104. <li class="lp_resource_element">
  8105. '.Display::return_icon('linksnew.gif').'
  8106. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8107. get_lang('LinkAdd').'
  8108. </a>
  8109. </li>';
  8110. foreach ($categorizedLinks as $categoryId => $links) {
  8111. $linkNodes = null;
  8112. foreach ($links as $key => $title) {
  8113. $link = Display::url(
  8114. Display::return_icon('preview_view.png', get_lang('Preview')),
  8115. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8116. ['target' => '_blank']
  8117. );
  8118. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8119. $linkNodes .=
  8120. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8121. <a class="moved" href="#">'.
  8122. $moveEverywhereIcon.
  8123. '</a>
  8124. '.Display::return_icon('lp_link.png').'
  8125. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8126. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8127. Security::remove_XSS($title).
  8128. '</a>
  8129. </li>';
  8130. }
  8131. }
  8132. $linksHtmlCode .=
  8133. '<li>
  8134. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8135. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8136. align="absbottom" />
  8137. </a>
  8138. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8139. </li>
  8140. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8141. }
  8142. $linksHtmlCode .= '</ul>';
  8143. return $linksHtmlCode;
  8144. }
  8145. /**
  8146. * Creates a list with all the student publications in it
  8147. * @return string
  8148. */
  8149. public function get_student_publications()
  8150. {
  8151. $return = '<ul class="lp_resource">';
  8152. $return .= '<li class="lp_resource_element">';
  8153. $return .= Display::return_icon('works_new.gif');
  8154. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8155. get_lang('AddAssignmentPage') . '</a>';
  8156. $return .= '</li>';
  8157. $sessionId = api_get_session_id();
  8158. if (empty($sessionId)) {
  8159. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8160. $works = getWorkListTeacher(0, 100, null, null, null);
  8161. if (!empty($works)) {
  8162. foreach ($works as $work) {
  8163. $link = Display::url(
  8164. Display::return_icon('preview_view.png', get_lang('Preview')),
  8165. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8166. ['target' => '_blank']
  8167. );
  8168. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security :: remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8169. $return .= '<a class="moved" href="#">';
  8170. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8171. $return .= '</a> ';
  8172. $return .= Display::return_icon('works.gif');
  8173. $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 . '">' .
  8174. Security :: remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8175. </a>';
  8176. $return .= '</li>';
  8177. }
  8178. }
  8179. }
  8180. $return .= '</ul>';
  8181. return $return;
  8182. }
  8183. /**
  8184. * Creates a list with all the forums in it
  8185. * @return string
  8186. */
  8187. public function get_forums()
  8188. {
  8189. require_once '../forum/forumfunction.inc.php';
  8190. require_once '../forum/forumconfig.inc.php';
  8191. $forumCategories = get_forum_categories();
  8192. $forumsInNoCategory = get_forums_in_category(0);
  8193. if (!empty($forumsInNoCategory)) {
  8194. $forumCategories = array_merge(
  8195. $forumCategories,
  8196. array(
  8197. array(
  8198. 'cat_id' => 0,
  8199. 'session_id' => 0,
  8200. 'visibility' => 1,
  8201. 'cat_comment' => null,
  8202. ),
  8203. )
  8204. );
  8205. }
  8206. $forumList = get_forums();
  8207. $a_forums = [];
  8208. foreach ($forumCategories as $forumCategory) {
  8209. // The forums in this category.
  8210. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8211. if (!empty($forumsInCategory)) {
  8212. foreach ($forumList as $forum) {
  8213. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8214. $a_forums[] = $forum;
  8215. }
  8216. }
  8217. }
  8218. }
  8219. $return = '<ul class="lp_resource">';
  8220. // First add link
  8221. $return .= '<li class="lp_resource_element">';
  8222. $return .= Display::return_icon('new_forum.png');
  8223. $return .= Display::url(
  8224. get_lang('CreateANewForum'),
  8225. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8226. 'action' => 'add',
  8227. 'content' => 'forum',
  8228. 'lp_id' => $this->lp_id
  8229. ]),
  8230. ['title' => get_lang('CreateANewForum')]
  8231. );
  8232. $return .= '</li>';
  8233. $return .= '<script>
  8234. function toggle_forum(forum_id) {
  8235. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8236. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8237. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8238. } else {
  8239. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8240. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8241. }
  8242. }
  8243. </script>';
  8244. foreach ($a_forums as $forum) {
  8245. if (!empty($forum['forum_id'])) {
  8246. $link = Display::url(
  8247. Display::return_icon('preview_view.png', get_lang('Preview')),
  8248. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8249. ['target' => '_blank']
  8250. );
  8251. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8252. $return .= '<a class="moved" href="#">';
  8253. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8254. $return .= ' </a>';
  8255. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8256. $return .= '<a onclick="toggle_forum(' . $forum['forum_id'] . ');" style="cursor:hand; vertical-align:middle">
  8257. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8258. </a>
  8259. <a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  8260. Security :: remove_XSS($forum['forum_title']) . '</a>';
  8261. $return .= '</li>';
  8262. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8263. $a_threads = get_threads($forum['forum_id']);
  8264. if (is_array($a_threads)) {
  8265. foreach ($a_threads as $thread) {
  8266. $link = Display::url(
  8267. Display::return_icon('preview_view.png', get_lang('Preview')),
  8268. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8269. ['target' => '_blank']
  8270. );
  8271. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8272. $return .= '&nbsp;<a class="moved" href="#">';
  8273. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8274. $return .= ' </a>';
  8275. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8276. $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 . '">' .
  8277. Security :: remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8278. $return .= '</li>';
  8279. }
  8280. }
  8281. $return .= '</div>';
  8282. }
  8283. }
  8284. $return .= '</ul>';
  8285. return $return;
  8286. }
  8287. /**
  8288. * // TODO: The output encoding should be equal to the system encoding.
  8289. *
  8290. * Exports the learning path as a SCORM package. This is the main function that
  8291. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8292. * whole thing and returns the zip.
  8293. *
  8294. * This method needs to be called in PHP5, as it will fail with non-adequate
  8295. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8296. * you need to call it on a learnpath object.
  8297. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8298. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8299. * path has been modified, it should use the generic method here below.
  8300. * @TODO link this function with the export_lp() function in the same class
  8301. * @param string Optional name of zip file. If none, title of learnpath is
  8302. * domesticated and trailed with ".zip"
  8303. * @return string Returns the zip package string, or null if error
  8304. */
  8305. public function scorm_export()
  8306. {
  8307. $_course = api_get_course_info();
  8308. $course_id = $_course['real_id'];
  8309. // Remove memory and time limits as much as possible as this might be a long process...
  8310. if (function_exists('ini_set')) {
  8311. api_set_memory_limit('256M');
  8312. ini_set('max_execution_time', 600);
  8313. }
  8314. // Create the zip handler (this will remain available throughout the method).
  8315. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8316. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8317. $temp_dir_short = uniqid();
  8318. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8319. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8320. $zip_folder = new PclZip($temp_zip_file);
  8321. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8322. $root_path = $main_path = api_get_path(SYS_PATH);
  8323. $files_cleanup = array();
  8324. // Place to temporarily stash the zip file.
  8325. // create the temp dir if it doesn't exist
  8326. // or do a cleanup before creating the zip file.
  8327. if (!is_dir($temp_zip_dir)) {
  8328. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8329. } else {
  8330. // Cleanup: Check the temp dir for old files and delete them.
  8331. $handle = opendir($temp_zip_dir);
  8332. while (false !== ($file = readdir($handle))) {
  8333. if ($file != '.' && $file != '..') {
  8334. unlink("$temp_zip_dir/$file");
  8335. }
  8336. }
  8337. closedir($handle);
  8338. }
  8339. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8340. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8341. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8342. ) {
  8343. // Remove the possible . at the end of the path.
  8344. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8345. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8346. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8347. copyr(
  8348. $current_course_path.'/scorm/'.$this->path,
  8349. $dest_path_to_scorm_folder,
  8350. array('imsmanifest'),
  8351. $zip_files
  8352. );
  8353. }
  8354. // Build a dummy imsmanifest structure.
  8355. // Do not add to the zip yet (we still need it).
  8356. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8357. // Aggregation Model official document, section "2.3 Content Packaging".
  8358. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8359. $xmldoc = new DOMDocument('1.0');
  8360. $root = $xmldoc->createElement('manifest');
  8361. $root->setAttribute('identifier', 'SingleCourseManifest');
  8362. $root->setAttribute('version', '1.1');
  8363. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8364. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8365. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8366. $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');
  8367. // Build mandatory sub-root container elements.
  8368. $metadata = $xmldoc->createElement('metadata');
  8369. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8370. $metadata->appendChild($md_schema);
  8371. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8372. $metadata->appendChild($md_schemaversion);
  8373. $root->appendChild($metadata);
  8374. $organizations = $xmldoc->createElement('organizations');
  8375. $resources = $xmldoc->createElement('resources');
  8376. // Build the only organization we will use in building our learnpaths.
  8377. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8378. $organization = $xmldoc->createElement('organization');
  8379. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8380. // To set the title of the SCORM entity (=organization), we take the name given
  8381. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8382. // learning path charset) as it is the encoding that defines how it is stored
  8383. // in the database. Then we convert it to HTML entities again as the "&" character
  8384. // alone is not authorized in XML (must be &amp;).
  8385. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8386. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8387. $organization->appendChild($org_title);
  8388. $folder_name = 'document';
  8389. // Removes the learning_path/scorm_folder path when exporting see #4841
  8390. $path_to_remove = null;
  8391. $result = $this->generate_lp_folder($_course);
  8392. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8393. $path_to_remove = 'document'.$result['dir'];
  8394. $path_to_replace = $folder_name.'/';
  8395. }
  8396. // Fixes chamilo scorm exports
  8397. if ($this->ref === 'chamilo_scorm_export') {
  8398. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8399. }
  8400. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8401. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8402. $link_updates = array();
  8403. $links_to_create = array();
  8404. //foreach ($this->items as $index => $item) {
  8405. foreach ($this->ordered_items as $index => $itemId) {
  8406. $item = $this->items[$itemId];
  8407. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8408. // Get included documents from this item.
  8409. if ($item->type === 'sco') {
  8410. $inc_docs = $item->get_resources_from_source(
  8411. null,
  8412. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8413. );
  8414. } else {
  8415. $inc_docs = $item->get_resources_from_source();
  8416. }
  8417. // Give a child element <item> to the <organization> element.
  8418. $my_item_id = $item->get_id();
  8419. $my_item = $xmldoc->createElement('item');
  8420. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8421. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8422. $my_item->setAttribute('isvisible', 'true');
  8423. // Give a child element <title> to the <item> element.
  8424. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8425. $my_item->appendChild($my_title);
  8426. // Give a child element <adlcp:prerequisites> to the <item> element.
  8427. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8428. $my_prereqs->setAttribute('type', 'aicc_script');
  8429. $my_item->appendChild($my_prereqs);
  8430. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8431. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8432. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8433. //$xmldoc->createElement('adlcp:timelimitaction','');
  8434. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8435. //$xmldoc->createElement('adlcp:datafromlms','');
  8436. // Give a child element <adlcp:masteryscore> to the <item> element.
  8437. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8438. $my_item->appendChild($my_masteryscore);
  8439. // Attach this item to the organization element or hits parent if there is one.
  8440. if (!empty($item->parent) && $item->parent != 0) {
  8441. $children = $organization->childNodes;
  8442. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8443. if (is_object($possible_parent)) {
  8444. $possible_parent->appendChild($my_item);
  8445. } else {
  8446. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8447. }
  8448. } else {
  8449. if ($this->debug > 0) { error_log('No parent'); }
  8450. $organization->appendChild($my_item);
  8451. }
  8452. // Get the path of the file(s) from the course directory root.
  8453. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8454. if (!empty($path_to_remove)) {
  8455. //From docs
  8456. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8457. //From quiz
  8458. if ($this->ref === 'chamilo_scorm_export') {
  8459. $path_to_remove = 'scorm/'.$this->path.'/';
  8460. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8461. }
  8462. } else {
  8463. $my_xml_file_path = $my_file_path;
  8464. }
  8465. $my_sub_dir = dirname($my_file_path);
  8466. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8467. $my_xml_sub_dir = $my_sub_dir;
  8468. // Give a <resource> child to the <resources> element
  8469. $my_resource = $xmldoc->createElement('resource');
  8470. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8471. $my_resource->setAttribute('type', 'webcontent');
  8472. $my_resource->setAttribute('href', $my_xml_file_path);
  8473. // adlcp:scormtype can be either 'sco' or 'asset'.
  8474. if ($item->type === 'sco') {
  8475. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8476. } else {
  8477. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8478. }
  8479. // xml:base is the base directory to find the files declared in this resource.
  8480. $my_resource->setAttribute('xml:base', '');
  8481. // Give a <file> child to the <resource> element.
  8482. $my_file = $xmldoc->createElement('file');
  8483. $my_file->setAttribute('href', $my_xml_file_path);
  8484. $my_resource->appendChild($my_file);
  8485. // Dependency to other files - not yet supported.
  8486. $i = 1;
  8487. if ($inc_docs)
  8488. foreach ($inc_docs as $doc_info) {
  8489. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8490. continue;
  8491. }
  8492. $my_dep = $xmldoc->createElement('resource');
  8493. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8494. $my_dep->setAttribute('identifier', $res_id);
  8495. $my_dep->setAttribute('type', 'webcontent');
  8496. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8497. $my_dep_file = $xmldoc->createElement('file');
  8498. // Check type of URL.
  8499. if ($doc_info[1] == 'remote') {
  8500. // Remote file. Save url as is.
  8501. $my_dep_file->setAttribute('href', $doc_info[0]);
  8502. $my_dep->setAttribute('xml:base', '');
  8503. } elseif ($doc_info[1] === 'local') {
  8504. switch ($doc_info[2]) {
  8505. case 'url': // Local URL - save path as url for now, don't zip file.
  8506. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8507. $current_dir = dirname($abs_path);
  8508. $current_dir = str_replace('\\', '/', $current_dir);
  8509. $file_path = realpath($abs_path);
  8510. $file_path = str_replace('\\', '/', $file_path);
  8511. $my_dep_file->setAttribute('href', $file_path);
  8512. $my_dep->setAttribute('xml:base', '');
  8513. if (strstr($file_path, $main_path) !== false) {
  8514. // The calculated real path is really inside Chamilo's root path.
  8515. // Reduce file path to what's under the DocumentRoot.
  8516. $file_path = substr($file_path, strlen($root_path) - 1);
  8517. //echo $file_path;echo '<br /><br />';
  8518. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8519. $zip_files_abs[] = $file_path;
  8520. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8521. $my_dep_file->setAttribute('href', $file_path);
  8522. $my_dep->setAttribute('xml:base', '');
  8523. } elseif (empty($file_path)) {
  8524. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8525. if (strpos($document_root, -1) == '/') {
  8526. $document_root = substr(0, -1, $document_root);
  8527. }*/
  8528. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8529. $file_path = str_replace('//', '/', $file_path);
  8530. if (file_exists($file_path)) {
  8531. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8532. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8533. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8534. $my_dep_file->setAttribute('href', $file_path);
  8535. $my_dep->setAttribute('xml:base', '');
  8536. }
  8537. }
  8538. break;
  8539. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8540. $my_dep_file->setAttribute('href', $doc_info[0]);
  8541. $my_dep->setAttribute('xml:base', '');
  8542. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8543. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8544. $abs_img_path_without_subdir = $doc_info[0];
  8545. $relp = api_get_path(REL_PATH); // The url-append config param.
  8546. $pos = strpos($abs_img_path_without_subdir, $relp);
  8547. if ($pos === 0) {
  8548. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8549. }
  8550. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8551. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8552. $file_path = str_replace('\\', '/', $file_path);
  8553. $file_path = str_replace('//', '/', $file_path);
  8554. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8555. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8556. // Check if the current document is in that path.
  8557. if (strstr($file_path, $cur_path) !== false) {
  8558. // The document is in that path, now get the relative path
  8559. // to the containing document.
  8560. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8561. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8562. $relative_path = '';
  8563. if (strstr($file_path, $cur_path) !== false) {
  8564. //$relative_path = substr($file_path, strlen($orig_file_path));
  8565. $relative_path = str_replace($cur_path, '', $file_path);
  8566. $file_path = substr($file_path, strlen($cur_path));
  8567. } else {
  8568. // This case is still a problem as it's difficult to calculate a relative path easily
  8569. // might still generate wrong links.
  8570. //$file_path = substr($file_path,strlen($cur_path));
  8571. // Calculate the directory path to the current file (without trailing slash).
  8572. $my_relative_path = dirname($file_path);
  8573. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8574. $my_relative_file = basename($file_path);
  8575. // Calculate the directory path to the containing file (without trailing slash).
  8576. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8577. $dotdot = '';
  8578. $subdir = '';
  8579. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8580. $my_relative_path2 = dirname($my_relative_path);
  8581. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8582. $my_orig_file_path = dirname($my_orig_file_path);
  8583. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8584. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8585. $dotdot += '../';
  8586. $my_relative_path = $my_relative_path2;
  8587. }
  8588. $relative_path = $dotdot.$subdir.$my_relative_file;
  8589. }
  8590. // Put the current document in the zip (this array is the array
  8591. // that will manage documents already in the course folder - relative).
  8592. $zip_files[] = $file_path;
  8593. // Update the links to the current document in the containing document (make them relative).
  8594. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8595. $my_dep_file->setAttribute('href', $file_path);
  8596. $my_dep->setAttribute('xml:base', '');
  8597. } elseif (strstr($file_path, $main_path) !== false) {
  8598. // The calculated real path is really inside Chamilo's root path.
  8599. // Reduce file path to what's under the DocumentRoot.
  8600. $file_path = substr($file_path, strlen($root_path));
  8601. //echo $file_path;echo '<br /><br />';
  8602. //error_log('Reduced path: '.$file_path, 0);
  8603. $zip_files_abs[] = $file_path;
  8604. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8605. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8606. $my_dep->setAttribute('xml:base', '');
  8607. } elseif (empty($file_path)) {
  8608. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8609. if(strpos($document_root,-1) == '/') {
  8610. $document_root = substr(0, -1, $document_root);
  8611. }*/
  8612. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8613. $file_path = str_replace('//', '/', $file_path);
  8614. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8615. $current_dir = dirname($abs_path);
  8616. $current_dir = str_replace('\\', '/', $current_dir);
  8617. if (file_exists($file_path)) {
  8618. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8619. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8620. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8621. $my_dep_file->setAttribute('href','document/'.$file_path);
  8622. $my_dep->setAttribute('xml:base', '');
  8623. }
  8624. }
  8625. break;
  8626. case 'rel':
  8627. // Path relative to the current document.
  8628. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8629. if (substr($doc_info[0], 0, 2) == '..') {
  8630. // Relative path going up.
  8631. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8632. $current_dir = str_replace('\\', '/', $current_dir);
  8633. $file_path = realpath($current_dir.$doc_info[0]);
  8634. $file_path = str_replace('\\', '/', $file_path);
  8635. //error_log($file_path.' <-> '.$main_path,0);
  8636. if (strstr($file_path, $main_path) !== false) {
  8637. // The calculated real path is really inside Chamilo's root path.
  8638. // Reduce file path to what's under the DocumentRoot.
  8639. $file_path = substr($file_path, strlen($root_path));
  8640. //error_log('Reduced path: '.$file_path, 0);
  8641. $zip_files_abs[] = $file_path;
  8642. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8643. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8644. $my_dep->setAttribute('xml:base', '');
  8645. }
  8646. } else {
  8647. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8648. $my_dep_file->setAttribute('href', $doc_info[0]);
  8649. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8650. }
  8651. break;
  8652. default:
  8653. $my_dep_file->setAttribute('href', $doc_info[0]);
  8654. $my_dep->setAttribute('xml:base', '');
  8655. break;
  8656. }
  8657. }
  8658. $my_dep->appendChild($my_dep_file);
  8659. $resources->appendChild($my_dep);
  8660. $dependency = $xmldoc->createElement('dependency');
  8661. $dependency->setAttribute('identifierref', $res_id);
  8662. $my_resource->appendChild($dependency);
  8663. $i++;
  8664. }
  8665. $resources->appendChild($my_resource);
  8666. $zip_files[] = $my_file_path;
  8667. } else {
  8668. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8669. switch ($item->type) {
  8670. case TOOL_LINK:
  8671. $my_item = $xmldoc->createElement('item');
  8672. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8673. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8674. $my_item->setAttribute('isvisible', 'true');
  8675. // Give a child element <title> to the <item> element.
  8676. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8677. $my_item->appendChild($my_title);
  8678. // Give a child element <adlcp:prerequisites> to the <item> element.
  8679. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8680. $my_prereqs->setAttribute('type', 'aicc_script');
  8681. $my_item->appendChild($my_prereqs);
  8682. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8683. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8684. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8685. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8686. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8687. //$xmldoc->createElement('adlcp:datafromlms', '');
  8688. // Give a child element <adlcp:masteryscore> to the <item> element.
  8689. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8690. $my_item->appendChild($my_masteryscore);
  8691. // Attach this item to the organization element or its parent if there is one.
  8692. if (!empty($item->parent) && $item->parent != 0) {
  8693. $children = $organization->childNodes;
  8694. for ($i = 0; $i < $children->length; $i++) {
  8695. $item_temp = $children->item($i);
  8696. if ($item_temp -> nodeName == 'item') {
  8697. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8698. $item_temp -> appendChild($my_item);
  8699. }
  8700. }
  8701. }
  8702. } else {
  8703. $organization->appendChild($my_item);
  8704. }
  8705. $my_file_path = 'link_'.$item->get_id().'.html';
  8706. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8707. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8708. $rs = Database::query($sql);
  8709. if ($link = Database :: fetch_array($rs)) {
  8710. $url = $link['url'];
  8711. $title = stripslashes($link['title']);
  8712. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8713. $my_xml_file_path = $my_file_path;
  8714. $my_sub_dir = dirname($my_file_path);
  8715. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8716. $my_xml_sub_dir = $my_sub_dir;
  8717. // Give a <resource> child to the <resources> element.
  8718. $my_resource = $xmldoc->createElement('resource');
  8719. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8720. $my_resource->setAttribute('type', 'webcontent');
  8721. $my_resource->setAttribute('href', $my_xml_file_path);
  8722. // adlcp:scormtype can be either 'sco' or 'asset'.
  8723. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8724. // xml:base is the base directory to find the files declared in this resource.
  8725. $my_resource->setAttribute('xml:base', '');
  8726. // give a <file> child to the <resource> element.
  8727. $my_file = $xmldoc->createElement('file');
  8728. $my_file->setAttribute('href', $my_xml_file_path);
  8729. $my_resource->appendChild($my_file);
  8730. $resources->appendChild($my_resource);
  8731. }
  8732. break;
  8733. case TOOL_QUIZ:
  8734. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8735. $exe = new Exercise();
  8736. $exe->read($exe_id);
  8737. $my_item = $xmldoc->createElement('item');
  8738. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8739. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8740. $my_item->setAttribute('isvisible', 'true');
  8741. // Give a child element <title> to the <item> element.
  8742. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8743. $my_item->appendChild($my_title);
  8744. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8745. //$my_item->appendChild($my_max_score);
  8746. // Give a child element <adlcp:prerequisites> to the <item> element.
  8747. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8748. $my_prereqs->setAttribute('type','aicc_script');
  8749. $my_item->appendChild($my_prereqs);
  8750. // Give a child element <adlcp:masteryscore> to the <item> element.
  8751. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8752. $my_item->appendChild($my_masteryscore);
  8753. // Attach this item to the organization element or hits parent if there is one.
  8754. if (!empty($item->parent) && $item->parent != 0) {
  8755. $children = $organization->childNodes;
  8756. /*for ($i = 0; $i < $children->length; $i++) {
  8757. $item_temp = $children->item($i);
  8758. if ($exe_id == 81) {
  8759. error_log($item_temp->nodeName );
  8760. error_log($item_temp->getAttribute('identifier'));
  8761. }
  8762. if ($item_temp->nodeName == 'item') {
  8763. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8764. $item_temp->appendChild($my_item);
  8765. }
  8766. }
  8767. }*/
  8768. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8769. if ($possible_parent) {
  8770. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8771. $possible_parent->appendChild($my_item);
  8772. }
  8773. }
  8774. } else {
  8775. $organization->appendChild($my_item);
  8776. }
  8777. // Get the path of the file(s) from the course directory root
  8778. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8779. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8780. // Write the contents of the exported exercise into a (big) html file
  8781. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8782. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  8783. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8784. $res = file_put_contents($tmp_file_path, $contents);
  8785. if ($res === false) {
  8786. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8787. }
  8788. $files_cleanup[] = $tmp_file_path;
  8789. $my_xml_file_path = $my_file_path;
  8790. $my_sub_dir = dirname($my_file_path);
  8791. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8792. $my_xml_sub_dir = $my_sub_dir;
  8793. // Give a <resource> child to the <resources> element.
  8794. $my_resource = $xmldoc->createElement('resource');
  8795. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8796. $my_resource->setAttribute('type', 'webcontent');
  8797. $my_resource->setAttribute('href', $my_xml_file_path);
  8798. // adlcp:scormtype can be either 'sco' or 'asset'.
  8799. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8800. // xml:base is the base directory to find the files declared in this resource.
  8801. $my_resource->setAttribute('xml:base', '');
  8802. // Give a <file> child to the <resource> element.
  8803. $my_file = $xmldoc->createElement('file');
  8804. $my_file->setAttribute('href', $my_xml_file_path);
  8805. $my_resource->appendChild($my_file);
  8806. // Get included docs.
  8807. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8808. // Dependency to other files - not yet supported.
  8809. $i = 1;
  8810. foreach ($inc_docs as $doc_info) {
  8811. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8812. $my_dep = $xmldoc->createElement('resource');
  8813. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8814. $my_dep->setAttribute('identifier', $res_id);
  8815. $my_dep->setAttribute('type', 'webcontent');
  8816. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8817. $my_dep_file = $xmldoc->createElement('file');
  8818. // Check type of URL.
  8819. if ($doc_info[1] == 'remote') {
  8820. // Remote file. Save url as is.
  8821. $my_dep_file->setAttribute('href', $doc_info[0]);
  8822. $my_dep->setAttribute('xml:base', '');
  8823. } elseif ($doc_info[1] == 'local') {
  8824. switch ($doc_info[2]) {
  8825. case 'url': // Local URL - save path as url for now, don't zip file.
  8826. // Save file but as local file (retrieve from URL).
  8827. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8828. $current_dir = dirname($abs_path);
  8829. $current_dir = str_replace('\\', '/', $current_dir);
  8830. $file_path = realpath($abs_path);
  8831. $file_path = str_replace('\\', '/', $file_path);
  8832. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8833. $my_dep->setAttribute('xml:base', '');
  8834. if (strstr($file_path, $main_path) !== false) {
  8835. // The calculated real path is really inside the chamilo root path.
  8836. // Reduce file path to what's under the DocumentRoot.
  8837. $file_path = substr($file_path, strlen($root_path));
  8838. $zip_files_abs[] = $file_path;
  8839. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8840. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8841. $my_dep->setAttribute('xml:base', '');
  8842. } elseif (empty($file_path)) {
  8843. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8844. if (strpos($document_root,-1) == '/') {
  8845. $document_root = substr(0, -1, $document_root);
  8846. }*/
  8847. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8848. $file_path = str_replace('//', '/', $file_path);
  8849. if (file_exists($file_path)) {
  8850. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8851. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8852. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8853. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8854. $my_dep->setAttribute('xml:base', '');
  8855. }
  8856. }
  8857. break;
  8858. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8859. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8860. $current_dir = str_replace('\\', '/', $current_dir);
  8861. $file_path = realpath($doc_info[0]);
  8862. $file_path = str_replace('\\', '/', $file_path);
  8863. $my_dep_file->setAttribute('href', $file_path);
  8864. $my_dep->setAttribute('xml:base', '');
  8865. if (strstr($file_path,$main_path) !== false) {
  8866. // The calculated real path is really inside the chamilo root path.
  8867. // Reduce file path to what's under the DocumentRoot.
  8868. $file_path = substr($file_path, strlen($root_path));
  8869. //echo $file_path;echo '<br /><br />';
  8870. //error_log('Reduced path: '.$file_path, 0);
  8871. $zip_files_abs[] = $file_path;
  8872. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8873. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8874. $my_dep->setAttribute('xml:base', '');
  8875. } elseif (empty($file_path)) {
  8876. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8877. if (strpos($document_root,-1) == '/') {
  8878. $document_root = substr(0, -1, $document_root);
  8879. }*/
  8880. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8881. $file_path = str_replace('//', '/', $file_path);
  8882. if (file_exists($file_path)) {
  8883. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8884. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8885. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8886. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8887. $my_dep->setAttribute('xml:base', '');
  8888. }
  8889. }
  8890. break;
  8891. 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
  8892. if (substr($doc_info[0], 0, 2) == '..') {
  8893. // Relative path going up.
  8894. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8895. $current_dir = str_replace('\\', '/', $current_dir);
  8896. $file_path = realpath($current_dir.$doc_info[0]);
  8897. $file_path = str_replace('\\', '/', $file_path);
  8898. //error_log($file_path.' <-> '.$main_path, 0);
  8899. if (strstr($file_path, $main_path) !== false) {
  8900. // The calculated real path is really inside Chamilo's root path.
  8901. // Reduce file path to what's under the DocumentRoot.
  8902. $file_path = substr($file_path, strlen($root_path));
  8903. $file_path_dest = $file_path;
  8904. // File path is courses/CHAMILO/document/....
  8905. $info_file_path = explode('/', $file_path);
  8906. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8907. $file_path_dest = 'document/'.$file_path;
  8908. }
  8909. //error_log('Reduced path: '.$file_path, 0);
  8910. $zip_files_abs[] = $file_path;
  8911. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8912. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8913. $my_dep->setAttribute('xml:base', '');
  8914. }
  8915. } else {
  8916. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8917. $my_dep_file->setAttribute('href', $doc_info[0]);
  8918. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8919. }
  8920. break;
  8921. default:
  8922. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8923. $my_dep->setAttribute('xml:base', '');
  8924. break;
  8925. }
  8926. }
  8927. $my_dep->appendChild($my_dep_file);
  8928. $resources->appendChild($my_dep);
  8929. $dependency = $xmldoc->createElement('dependency');
  8930. $dependency->setAttribute('identifierref', $res_id);
  8931. $my_resource->appendChild($dependency);
  8932. $i++;
  8933. }
  8934. $resources->appendChild($my_resource);
  8935. $zip_files[] = $my_file_path;
  8936. break;
  8937. default:
  8938. // Get the path of the file(s) from the course directory root
  8939. $my_file_path = 'non_exportable.html';
  8940. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8941. $my_xml_file_path = $my_file_path;
  8942. $my_sub_dir = dirname($my_file_path);
  8943. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8944. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8945. $my_xml_sub_dir = $my_sub_dir;
  8946. // Give a <resource> child to the <resources> element.
  8947. $my_resource = $xmldoc->createElement('resource');
  8948. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8949. $my_resource->setAttribute('type', 'webcontent');
  8950. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8951. // adlcp:scormtype can be either 'sco' or 'asset'.
  8952. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8953. // xml:base is the base directory to find the files declared in this resource.
  8954. $my_resource->setAttribute('xml:base', '');
  8955. // Give a <file> child to the <resource> element.
  8956. $my_file = $xmldoc->createElement('file');
  8957. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8958. $my_resource->appendChild($my_file);
  8959. $resources->appendChild($my_resource);
  8960. break;
  8961. }
  8962. }
  8963. }
  8964. $organizations->appendChild($organization);
  8965. $root->appendChild($organizations);
  8966. $root->appendChild($resources);
  8967. $xmldoc->appendChild($root);
  8968. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  8969. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8970. // then add the file to the zip, then destroy the file (this is done automatically).
  8971. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8972. foreach ($zip_files as $file_path) {
  8973. if (empty($file_path)) {
  8974. continue;
  8975. }
  8976. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  8977. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8978. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8979. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8980. }
  8981. $this->create_path($dest_file);
  8982. @copy($filePath, $dest_file);
  8983. // Check if the file needs a link update.
  8984. if (in_array($file_path, array_keys($link_updates))) {
  8985. $string = file_get_contents($dest_file);
  8986. unlink($dest_file);
  8987. foreach ($link_updates[$file_path] as $old_new) {
  8988. // This is an ugly hack that allows .flv files to be found by the flv player that
  8989. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8990. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8991. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8992. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8993. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8994. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8995. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8996. }
  8997. //Fix to avoid problems with default_course_document
  8998. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8999. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9000. $newDestination = $old_new['dest'];
  9001. } else {
  9002. $newDestination = str_replace('document/', '', $old_new['dest']);
  9003. }
  9004. $string = str_replace($old_new['orig'], $newDestination, $string);
  9005. // Add files inside the HTMLs
  9006. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9007. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9008. if (file_exists($sys_course_path.$new_path)) {
  9009. copy($sys_course_path.$new_path, $destinationFile);
  9010. }
  9011. }
  9012. file_put_contents($dest_file, $string);
  9013. }
  9014. if (file_exists($filePath) && $copyAll) {
  9015. $extension = $this->get_extension($filePath);
  9016. if (in_array($extension, ['html', 'html'])) {
  9017. $containerOrigin = dirname($filePath);
  9018. $containerDestination = dirname($dest_file);
  9019. $finder = new Finder();
  9020. $finder->files()->in($containerOrigin)
  9021. ->notName('*_DELETED_*')
  9022. ->exclude('share_folder')
  9023. ->exclude('chat_files')
  9024. ->exclude('certificates')
  9025. ;
  9026. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9027. $fs = new Filesystem();
  9028. $fs->mirror(
  9029. $containerOrigin,
  9030. $containerDestination,
  9031. $finder
  9032. );
  9033. }
  9034. }
  9035. }
  9036. }
  9037. foreach ($zip_files_abs as $file_path) {
  9038. if (empty($file_path)) {
  9039. continue;
  9040. }
  9041. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9042. continue;
  9043. }
  9044. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9045. $this->create_path($dest_file);
  9046. copy($main_path.$file_path, $dest_file);
  9047. // Check if the file needs a link update.
  9048. if (in_array($file_path, array_keys($link_updates))) {
  9049. $string = file_get_contents($dest_file);
  9050. unlink($dest_file);
  9051. foreach ($link_updates[$file_path] as $old_new) {
  9052. // This is an ugly hack that allows .flv files to be found by the flv player that
  9053. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9054. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9055. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9056. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9057. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9058. }
  9059. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9060. }
  9061. file_put_contents($dest_file, $string);
  9062. }
  9063. }
  9064. if (is_array($links_to_create)) {
  9065. foreach ($links_to_create as $file => $link) {
  9066. $file_content = '<!DOCTYPE html><head>
  9067. <meta charset="'.api_get_language_isocode().'" />
  9068. <title>'.$link['title'].'</title>
  9069. </head>
  9070. <body dir="'.api_get_text_direction().'">
  9071. <div style="text-align:center">
  9072. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9073. </body>
  9074. </html>';
  9075. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9076. }
  9077. }
  9078. // Add non exportable message explanation.
  9079. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9080. $file_content = '<!DOCTYPE html><head>
  9081. <meta charset="'.api_get_language_isocode().'" />
  9082. <title>'.$lang_not_exportable.'</title>
  9083. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9084. </head>
  9085. <body dir="'.api_get_text_direction().'">';
  9086. $file_content .=
  9087. <<<EOD
  9088. <style>
  9089. .error-message {
  9090. font-family: arial, verdana, helvetica, sans-serif;
  9091. border-width: 1px;
  9092. border-style: solid;
  9093. left: 50%;
  9094. margin: 10px auto;
  9095. min-height: 30px;
  9096. padding: 5px;
  9097. right: 50%;
  9098. width: 500px;
  9099. background-color: #FFD1D1;
  9100. border-color: #FF0000;
  9101. color: #000;
  9102. }
  9103. </style>
  9104. <body>
  9105. <div class="error-message">
  9106. $lang_not_exportable
  9107. </div>
  9108. </body>
  9109. </html>
  9110. EOD;
  9111. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9112. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9113. }
  9114. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9115. // Add the extra files that go along with a SCORM package.
  9116. $main_code_path = api_get_path(SYS_CODE_PATH) . 'lp/packaging/';
  9117. $fs = new Filesystem();
  9118. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9119. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9120. $manifest = @$xmldoc->saveXML();
  9121. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9122. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9123. $zip_folder->add(
  9124. $archive_path.'/'.$temp_dir_short,
  9125. PCLZIP_OPT_REMOVE_PATH,
  9126. $archive_path.'/'.$temp_dir_short.'/'
  9127. );
  9128. // Clean possible temporary files.
  9129. foreach ($files_cleanup as $file) {
  9130. $res = unlink($file);
  9131. if ($res === false) {
  9132. error_log(
  9133. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9134. 0
  9135. );
  9136. }
  9137. }
  9138. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9139. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9140. }
  9141. /**
  9142. * @param int $lp_id
  9143. * @return bool
  9144. */
  9145. public function scorm_export_to_pdf($lp_id)
  9146. {
  9147. $lp_id = intval($lp_id);
  9148. $files_to_export = array();
  9149. $course_data = api_get_course_info($this->cc);
  9150. if (!empty($course_data)) {
  9151. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9152. $list = self::get_flat_ordered_items_list($lp_id);
  9153. if (!empty($list)) {
  9154. foreach ($list as $item_id) {
  9155. $item = $this->items[$item_id];
  9156. switch ($item->type) {
  9157. case 'document':
  9158. //Getting documents from a LP with chamilo documents
  9159. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9160. // Try loading document from the base course.
  9161. if (empty($file_data) && !empty($sessionId)) {
  9162. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9163. }
  9164. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9165. if (file_exists($file_path)) {
  9166. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9167. }
  9168. break;
  9169. case 'asset': //commes from a scorm package generated by chamilo
  9170. case 'sco':
  9171. $file_path = $scorm_path.'/'.$item->path;
  9172. if (file_exists($file_path)) {
  9173. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9174. }
  9175. break;
  9176. case 'dir':
  9177. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9178. break;
  9179. }
  9180. }
  9181. }
  9182. $pdf = new PDF();
  9183. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9184. return $result;
  9185. }
  9186. return false;
  9187. }
  9188. /**
  9189. * Temp function to be moved in main_api or the best place around for this.
  9190. * Creates a file path if it doesn't exist
  9191. * @param string $path
  9192. */
  9193. public function create_path($path)
  9194. {
  9195. $path_bits = explode('/', dirname($path));
  9196. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9197. $path_built = IS_WINDOWS_OS ? '' : '/';
  9198. foreach ($path_bits as $bit) {
  9199. if (!empty ($bit)) {
  9200. $new_path = $path_built . $bit;
  9201. if (is_dir($new_path)) {
  9202. $path_built = $new_path . '/';
  9203. } else {
  9204. mkdir($new_path, api_get_permissions_for_new_directories());
  9205. $path_built = $new_path . '/';
  9206. }
  9207. }
  9208. }
  9209. }
  9210. /**
  9211. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9212. * @return boolean The results of the unlink function, or false if there was no image to start with
  9213. */
  9214. public function delete_lp_image()
  9215. {
  9216. $img = $this->get_preview_image();
  9217. if ($img != '') {
  9218. $del_file = $this->get_preview_image_path(null, 'sys');
  9219. if (isset($del_file) && file_exists($del_file)) {
  9220. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9221. if (file_exists($del_file_2)) {
  9222. unlink($del_file_2);
  9223. }
  9224. $this->set_preview_image('');
  9225. return @unlink($del_file);
  9226. }
  9227. }
  9228. return false;
  9229. }
  9230. /**
  9231. * Uploads an author image to the upload/learning_path/images directory
  9232. * @param array The image array, coming from the $_FILES superglobal
  9233. * @return boolean True on success, false on error
  9234. */
  9235. public function upload_image($image_array)
  9236. {
  9237. if (!empty ($image_array['name'])) {
  9238. $upload_ok = process_uploaded_file($image_array);
  9239. $has_attachment = true;
  9240. }
  9241. if ($upload_ok) {
  9242. if ($has_attachment) {
  9243. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9244. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9245. $updir = $sys_course_path . $courseDir;
  9246. // Try to add an extension to the file if it hasn't one.
  9247. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9248. if (!filter_extension($new_file_name)) {
  9249. } else {
  9250. $file_extension = explode('.', $image_array['name']);
  9251. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9252. $filename = uniqid('');
  9253. $new_file_name = $filename.'.'.$file_extension;
  9254. $new_path = $updir.'/'.$new_file_name;
  9255. // Resize the image.
  9256. $temp = new Image($image_array['tmp_name']);
  9257. $temp->resize(104);
  9258. $result = $temp->send_image($new_path);
  9259. // Storing the image filename.
  9260. if ($result) {
  9261. $this->set_preview_image($new_file_name);
  9262. //Resize to 64px to use on course homepage
  9263. $temp->resize(64);
  9264. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9265. return true;
  9266. }
  9267. }
  9268. }
  9269. }
  9270. return false;
  9271. }
  9272. /**
  9273. * @param int $lp_id
  9274. * @param string $status
  9275. */
  9276. public function set_autolaunch($lp_id, $status)
  9277. {
  9278. $course_id = api_get_course_int_id();
  9279. $lp_id = intval($lp_id);
  9280. $status = intval($status);
  9281. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9282. // Setting everything to autolaunch = 0
  9283. $attributes['autolaunch'] = 0;
  9284. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9285. Database::update($lp_table, $attributes, $where);
  9286. if ($status == 1) {
  9287. //Setting my lp_id to autolaunch = 1
  9288. $attributes['autolaunch'] = 1;
  9289. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9290. Database::update($lp_table, $attributes, $where );
  9291. }
  9292. }
  9293. /**
  9294. * Gets previous_item_id for the next element of the lp_item table
  9295. * @author Isaac flores paz
  9296. * @return integer Previous item ID
  9297. */
  9298. public function select_previous_item_id()
  9299. {
  9300. $course_id = api_get_course_int_id();
  9301. if ($this->debug > 0) {
  9302. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9303. }
  9304. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9305. // Get the max order of the items
  9306. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9307. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9308. $rs_max_order = Database::query($sql);
  9309. $row_max_order = Database::fetch_object($rs_max_order);
  9310. $max_order = $row_max_order->display_order;
  9311. // Get the previous item ID
  9312. $sql = "SELECT id as previous FROM $table_lp_item
  9313. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9314. $rs_max = Database::query($sql);
  9315. $row_max = Database::fetch_object($rs_max);
  9316. // Return the previous item ID
  9317. return $row_max->previous;
  9318. }
  9319. /**
  9320. * Copies an LP
  9321. */
  9322. public function copy()
  9323. {
  9324. // Course builder
  9325. $cb = new CourseBuilder();
  9326. //Setting tools that will be copied
  9327. $cb->set_tools_to_build(array('learnpaths'));
  9328. //Setting elements that will be copied
  9329. $cb->set_tools_specific_id_list(
  9330. array('learnpaths' => array($this->lp_id))
  9331. );
  9332. $course = $cb->build();
  9333. //Course restorer
  9334. $course_restorer = new CourseRestorer($course);
  9335. $course_restorer->set_add_text_in_items(true);
  9336. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9337. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9338. }
  9339. public function verify_document_size($s)
  9340. {
  9341. $post_max = ini_get('post_max_size');
  9342. if (substr($post_max, -1, 1) == 'M') {
  9343. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9344. } elseif (substr($post_max, -1, 1) == 'G') {
  9345. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9346. }
  9347. $upl_max = ini_get('upload_max_filesize');
  9348. if (substr($upl_max, -1, 1) == 'M') {
  9349. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9350. } elseif (substr($upl_max, -1, 1) == 'G') {
  9351. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9352. }
  9353. $documents_total_space = DocumentManager::documents_total_space();
  9354. $course_max_space = DocumentManager::get_course_quota();
  9355. $total_size = filesize($s) + $documents_total_space;
  9356. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9357. return true;
  9358. } else{
  9359. return false;
  9360. }
  9361. }
  9362. /**
  9363. * Clear LP prerequisites
  9364. */
  9365. public function clear_prerequisites()
  9366. {
  9367. $course_id = $this->get_course_int_id();
  9368. if ($this->debug > 0) {
  9369. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9370. }
  9371. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9372. $lp_id = $this->get_id();
  9373. //Cleaning prerequisites
  9374. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9375. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9376. Database::query($sql);
  9377. //Cleaning mastery score for exercises
  9378. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9379. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9380. Database::query($sql);
  9381. }
  9382. public function set_previous_step_as_prerequisite_for_all_items()
  9383. {
  9384. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9385. $course_id = $this->get_course_int_id();
  9386. $lp_id = $this->get_id();
  9387. if (!empty($this->items)) {
  9388. $previous_item_id = null;
  9389. $previous_item_max = 0;
  9390. $previous_item_type = null;
  9391. $last_item_not_dir = null;
  9392. $last_item_not_dir_type = null;
  9393. $last_item_not_dir_max = null;
  9394. foreach ($this->items as $item) {
  9395. // if there was a previous item... (otherwise jump to set it)
  9396. if (!empty($previous_item_id)) {
  9397. $current_item_id = $item->get_id(); //save current id
  9398. if ($item->get_type() != 'dir') {
  9399. // Current item is not a folder, so it qualifies to get a prerequisites
  9400. if ($last_item_not_dir_type == 'quiz') {
  9401. // if previous is quiz, mark its max score as default score to be achieved
  9402. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9403. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9404. Database::query($sql);
  9405. }
  9406. // now simply update the prerequisite to set it to the last non-chapter item
  9407. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9408. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9409. Database::query($sql);
  9410. // record item as 'non-chapter' reference
  9411. $last_item_not_dir = $item->get_id();
  9412. $last_item_not_dir_type = $item->get_type();
  9413. $last_item_not_dir_max = $item->get_max();
  9414. }
  9415. } else {
  9416. if ($item->get_type() != 'dir') {
  9417. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9418. $last_item_not_dir = $item->get_id();
  9419. $last_item_not_dir_type = $item->get_type();
  9420. $last_item_not_dir_max = $item->get_max();
  9421. }
  9422. }
  9423. // Saving the item as "previous item" for the next loop
  9424. $previous_item_id = $item->get_id();
  9425. $previous_item_max = $item->get_max();
  9426. $previous_item_type = $item->get_type();
  9427. }
  9428. }
  9429. }
  9430. /**
  9431. * @param array $params
  9432. */
  9433. public static function createCategory($params)
  9434. {
  9435. $em = Database::getManager();
  9436. $item = new CLpCategory();
  9437. $item->setName($params['name']);
  9438. $item->setCId($params['c_id']);
  9439. $em->persist($item);
  9440. $em->flush();
  9441. }
  9442. /**
  9443. * @param array $params
  9444. */
  9445. public static function updateCategory($params)
  9446. {
  9447. $em = Database::getManager();
  9448. /** @var CLpCategory $item */
  9449. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9450. if ($item) {
  9451. $item->setName($params['name']);
  9452. $em->merge($item);
  9453. $em->flush();
  9454. }
  9455. }
  9456. /**
  9457. * @param int $id
  9458. */
  9459. public static function moveUpCategory($id)
  9460. {
  9461. $em = Database::getManager();
  9462. /** @var CLpCategory $item */
  9463. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9464. if ($item) {
  9465. $position = $item->getPosition() - 1;
  9466. $item->setPosition($position);
  9467. $em->persist($item);
  9468. $em->flush();
  9469. }
  9470. }
  9471. /**
  9472. * @param int $id
  9473. */
  9474. public static function moveDownCategory($id)
  9475. {
  9476. $em = Database::getManager();
  9477. /** @var CLpCategory $item */
  9478. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9479. if ($item) {
  9480. $position = $item->getPosition() + 1;
  9481. $item->setPosition($position);
  9482. $em->persist($item);
  9483. $em->flush();
  9484. }
  9485. }
  9486. /**
  9487. * @param int $courseId
  9488. * @return int|mixed
  9489. */
  9490. public static function getCountCategories($courseId)
  9491. {
  9492. if (empty($course_id)) {
  9493. return 0;
  9494. }
  9495. $em = Database::getManager();
  9496. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9497. $query->setParameter('id', $courseId);
  9498. return $query->getSingleScalarResult();
  9499. }
  9500. /**
  9501. * @param int $courseId
  9502. *
  9503. * @return mixed
  9504. */
  9505. public static function getCategories($courseId)
  9506. {
  9507. $em = Database::getManager();
  9508. //Default behaviour
  9509. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9510. array('cId' => $course_id),
  9511. array('name' => 'ASC')
  9512. );*/
  9513. // Using doctrine extensions
  9514. /** @var SortableRepository $repo */
  9515. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9516. $items = $repo
  9517. ->getBySortableGroupsQuery(['cId' => $courseId])
  9518. ->getResult();
  9519. return $items;
  9520. }
  9521. /**
  9522. * @param int $id
  9523. *
  9524. * @return mixed
  9525. */
  9526. public static function getCategory($id)
  9527. {
  9528. $em = Database::getManager();
  9529. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9530. return $item;
  9531. }
  9532. /**
  9533. * @param int $courseId
  9534. * @return array
  9535. */
  9536. public static function getCategoryByCourse($courseId)
  9537. {
  9538. $em = Database::getManager();
  9539. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9540. array('cId' => $courseId)
  9541. );
  9542. return $items;
  9543. }
  9544. /**
  9545. * @param int $id
  9546. *
  9547. * @return mixed
  9548. */
  9549. public static function deleteCategory($id)
  9550. {
  9551. $em = Database::getManager();
  9552. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9553. if ($item) {
  9554. $courseId = $item->getCId();
  9555. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9556. $query->setParameter('id', $courseId);
  9557. $query->setParameter('catId', $item->getId());
  9558. $lps = $query->getResult();
  9559. // Setting category = 0.
  9560. if ($lps) {
  9561. foreach ($lps as $lpItem) {
  9562. $lpItem->setCategoryId(0);
  9563. }
  9564. }
  9565. // Removing category.
  9566. $em->remove($item);
  9567. $em->flush();
  9568. }
  9569. }
  9570. /**
  9571. * @param int $courseId
  9572. * @param bool $addSelectOption
  9573. *
  9574. * @return mixed
  9575. */
  9576. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9577. {
  9578. $items = self::getCategoryByCourse($courseId);
  9579. $cats = array();
  9580. if ($addSelectOption) {
  9581. $cats = array(get_lang('SelectACategory'));
  9582. }
  9583. if (!empty($items)) {
  9584. foreach ($items as $cat) {
  9585. $cats[$cat->getId()] = $cat->getName();
  9586. }
  9587. }
  9588. return $cats;
  9589. }
  9590. /**
  9591. * Return the scorm item type object with spaces replaced with _
  9592. * The return result is use to build a css classname like scorm_type_$return
  9593. * @param $in_type
  9594. * @return mixed
  9595. */
  9596. private static function format_scorm_type_item($in_type)
  9597. {
  9598. return str_replace(' ', '_', $in_type);
  9599. }
  9600. /**
  9601. * @param string $courseCode
  9602. * @param int $lp_id
  9603. * @param int $user_id
  9604. *
  9605. * @return learnpath
  9606. */
  9607. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9608. {
  9609. $learnPath = null;
  9610. $lpObject = Session::read('lpobject');
  9611. if ($lpObject !== null) {
  9612. $learnPath = unserialize($lpObject);
  9613. }
  9614. if (!is_object($learnPath)) {
  9615. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9616. }
  9617. return $learnPath;
  9618. }
  9619. /**
  9620. * @param int $itemId
  9621. * @return learnpathItem|false
  9622. */
  9623. public function getItem($itemId)
  9624. {
  9625. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9626. return $this->items[$itemId];
  9627. }
  9628. return false;
  9629. }
  9630. /**
  9631. * @return int
  9632. */
  9633. public function getCategoryId()
  9634. {
  9635. return $this->categoryId;
  9636. }
  9637. /**
  9638. * @param int $categoryId
  9639. * @return bool
  9640. */
  9641. public function setCategoryId($categoryId)
  9642. {
  9643. $this->categoryId = intval($categoryId);
  9644. $courseId = api_get_course_int_id();
  9645. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9646. $lp_id = $this->get_id();
  9647. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9648. WHERE c_id = $courseId AND id = $lp_id";
  9649. Database::query($sql);
  9650. return true;
  9651. }
  9652. /**
  9653. * Get whether this is a learning path with the possibility to subscribe
  9654. * users or not
  9655. * @return int
  9656. */
  9657. public function getSubscribeUsers()
  9658. {
  9659. return $this->subscribeUsers;
  9660. }
  9661. /**
  9662. * Set whether this is a learning path with the possibility to subscribe
  9663. * users or not
  9664. * @param int $subscribeUsers (0 = false, 1 = true)
  9665. */
  9666. public function setSubscribeUsers($value)
  9667. {
  9668. if ($this->debug > 0) {
  9669. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9670. }
  9671. $this->subscribeUsers = intval($value);;
  9672. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9673. $lp_id = $this->get_id();
  9674. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9675. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9676. Database::query($sql);
  9677. return true;
  9678. }
  9679. /**
  9680. * Calculate the count of stars for a user in this LP
  9681. * This calculation is based on the following rules:
  9682. * - the student gets one star when he gets to 50% of the learning path
  9683. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9684. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9685. * - the student gets the final star when the score for the *last* test is >= 80%
  9686. * @param int $sessionId Optional. The session ID
  9687. * @return int The count of stars
  9688. */
  9689. public function getCalculateStars($sessionId = 0)
  9690. {
  9691. $stars = 0;
  9692. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9693. if ($progress >= 50) {
  9694. $stars++;
  9695. }
  9696. // Calculate stars chapters evaluation
  9697. $exercisesItems = $this->getExercisesItems();
  9698. if (!empty($exercisesItems)) {
  9699. $totalResult = 0;
  9700. foreach ($exercisesItems as $exerciseItem) {
  9701. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9702. $this->user_id,
  9703. $exerciseItem->path,
  9704. $this->course_int_id,
  9705. $sessionId,
  9706. $this->lp_id,
  9707. $exerciseItem->db_id
  9708. );
  9709. $exerciseResultInfo = end($exerciseResultInfo);
  9710. if (!$exerciseResultInfo) {
  9711. continue;
  9712. }
  9713. if (!empty($exerciseResultInfo['exe_weighting'])) {
  9714. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9715. } else {
  9716. $exerciseResult = 0;
  9717. }
  9718. $totalResult += $exerciseResult;
  9719. }
  9720. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9721. if ($totalExerciseAverage >= 50) {
  9722. $stars++;
  9723. }
  9724. if ($totalExerciseAverage >= 80) {
  9725. $stars++;
  9726. }
  9727. }
  9728. // Calculate star for final evaluation
  9729. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9730. if (!empty($finalEvaluationItem)) {
  9731. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9732. $this->user_id,
  9733. $finalEvaluationItem->path,
  9734. $this->course_int_id,
  9735. $sessionId,
  9736. $this->lp_id,
  9737. $finalEvaluationItem->db_id
  9738. );
  9739. $evaluationResultInfo = end($evaluationResultInfo);
  9740. if ($evaluationResultInfo) {
  9741. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9742. if ($evaluationResult >= 80) {
  9743. $stars++;
  9744. }
  9745. }
  9746. }
  9747. return $stars;
  9748. }
  9749. /**
  9750. * Get the items of exercise type
  9751. * @return array The items. Otherwise return false
  9752. */
  9753. public function getExercisesItems()
  9754. {
  9755. $exercises = [];
  9756. foreach ($this->items as $item) {
  9757. if ($item->type != 'quiz') {
  9758. continue;
  9759. }
  9760. $exercises[] = $item;
  9761. }
  9762. array_pop($exercises);
  9763. return $exercises;
  9764. }
  9765. /**
  9766. * Get the item of exercise type (evaluation type)
  9767. * @return array The final evaluation. Otherwise return false
  9768. */
  9769. public function getFinalEvaluationItem()
  9770. {
  9771. $exercises = [];
  9772. foreach ($this->items as $item) {
  9773. if ($item->type != 'quiz') {
  9774. continue;
  9775. }
  9776. $exercises[] = $item;
  9777. }
  9778. return array_pop($exercises);
  9779. }
  9780. /**
  9781. * Calculate the total points achieved for the current user in this learning path
  9782. * @param int $sessionId Optional. The session Id
  9783. * @return int
  9784. */
  9785. public function getCalculateScore($sessionId = 0)
  9786. {
  9787. // Calculate stars chapters evaluation
  9788. $exercisesItems = $this->getExercisesItems();
  9789. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9790. $totalExercisesResult = 0;
  9791. $totalEvaluationResult = 0;
  9792. if ($exercisesItems !== false) {
  9793. foreach ($exercisesItems as $exerciseItem) {
  9794. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9795. $this->user_id,
  9796. $exerciseItem->path,
  9797. $this->course_int_id,
  9798. $sessionId,
  9799. $this->lp_id,
  9800. $exerciseItem->db_id
  9801. );
  9802. $exerciseResultInfo = end($exerciseResultInfo);
  9803. if (!$exerciseResultInfo) {
  9804. continue;
  9805. }
  9806. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  9807. }
  9808. }
  9809. if (!empty($finalEvaluationItem)) {
  9810. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9811. $this->user_id,
  9812. $finalEvaluationItem->path,
  9813. $this->course_int_id,
  9814. $sessionId,
  9815. $this->lp_id,
  9816. $finalEvaluationItem->db_id
  9817. );
  9818. $evaluationResultInfo = end($evaluationResultInfo);
  9819. if ($evaluationResultInfo) {
  9820. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  9821. }
  9822. }
  9823. return $totalExercisesResult + $totalEvaluationResult;
  9824. }
  9825. /**
  9826. * Check if URL is not allowed to be show in a iframe
  9827. * @param string $src
  9828. *
  9829. * @return string
  9830. */
  9831. public function fixBlockedLinks($src)
  9832. {
  9833. $urlInfo = parse_url($src);
  9834. $platformProtocol = 'https';
  9835. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9836. $platformProtocol = 'http';
  9837. }
  9838. $protocolFixApplied = false;
  9839. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9840. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9841. if ($platformProtocol != $scheme) {
  9842. Session::write('x_frame_source', $src);
  9843. $src = 'blank.php?error=x_frames_options';
  9844. $protocolFixApplied = true;
  9845. }
  9846. if ($protocolFixApplied == false) {
  9847. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9848. // Check X-Frame-Options
  9849. $ch = curl_init();
  9850. $options = array(
  9851. CURLOPT_URL => $src,
  9852. CURLOPT_RETURNTRANSFER => true,
  9853. CURLOPT_HEADER => true,
  9854. CURLOPT_FOLLOWLOCATION => true,
  9855. CURLOPT_ENCODING => "",
  9856. CURLOPT_AUTOREFERER => true,
  9857. CURLOPT_CONNECTTIMEOUT => 120,
  9858. CURLOPT_TIMEOUT => 120,
  9859. CURLOPT_MAXREDIRS => 10,
  9860. );
  9861. curl_setopt_array($ch, $options);
  9862. $response = curl_exec($ch);
  9863. $httpCode = curl_getinfo($ch);
  9864. $headers = substr($response, 0, $httpCode['header_size']);
  9865. $error = false;
  9866. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  9867. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9868. ) {
  9869. $error = true;
  9870. }
  9871. if ($error) {
  9872. Session::write('x_frame_source', $src);
  9873. $src = 'blank.php?error=x_frames_options';
  9874. }
  9875. }
  9876. }
  9877. return $src;
  9878. }
  9879. /**
  9880. * Check if this LP has a created forum in the basis course
  9881. * @return boolean
  9882. */
  9883. public function lpHasForum()
  9884. {
  9885. $forumTable = Database::get_course_table(TABLE_FORUM);
  9886. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9887. $fakeFrom = "
  9888. $forumTable f
  9889. INNER JOIN $itemProperty ip
  9890. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9891. ";
  9892. $resultData = Database::select(
  9893. 'COUNT(f.iid) AS qty',
  9894. $fakeFrom,
  9895. [
  9896. 'where' => [
  9897. 'ip.visibility != ? AND ' => 2,
  9898. 'ip.tool = ? AND ' => TOOL_FORUM,
  9899. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9900. 'f.lp_id = ?' => intval($this->lp_id)
  9901. ]
  9902. ],
  9903. 'first'
  9904. );
  9905. if ($resultData['qty'] > 0) {
  9906. return true;
  9907. }
  9908. return false;
  9909. }
  9910. /**
  9911. * Get the forum for this learning path
  9912. * @return boolean
  9913. */
  9914. public function getForum($sessionId = 0)
  9915. {
  9916. $forumTable = Database::get_course_table(TABLE_FORUM);
  9917. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9918. $fakeFrom = "$forumTable f
  9919. INNER JOIN $itemProperty ip ";
  9920. if ($this->lp_session_id == 0) {
  9921. $fakeFrom .= "
  9922. ON (
  9923. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9924. f.session_id = ip.session_id OR ip.session_id IS NULL
  9925. )
  9926. )
  9927. ";
  9928. } else {
  9929. $fakeFrom .= "
  9930. ON (
  9931. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9932. )
  9933. ";
  9934. }
  9935. $resultData = Database::select(
  9936. 'f.*',
  9937. $fakeFrom,
  9938. [
  9939. 'where' => [
  9940. 'ip.visibility != ? AND ' => 2,
  9941. 'ip.tool = ? AND ' => TOOL_FORUM,
  9942. 'f.session_id = ? AND ' => $sessionId,
  9943. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9944. 'f.lp_id = ?' => intval($this->lp_id)
  9945. ]
  9946. ],
  9947. 'first'
  9948. );
  9949. if (empty($resultData)) {
  9950. return false;
  9951. }
  9952. return $resultData;
  9953. }
  9954. /**
  9955. * Create a forum for this learning path
  9956. * @param type $forumCategoryId
  9957. * @return int The forum ID if was created. Otherwise return false
  9958. */
  9959. public function createForum($forumCategoryId)
  9960. {
  9961. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9962. $forumId = store_forum(
  9963. [
  9964. 'lp_id' => $this->lp_id,
  9965. 'forum_title' => $this->name,
  9966. 'forum_comment' => null,
  9967. 'forum_category' => intval($forumCategoryId),
  9968. 'students_can_edit_group' => ['students_can_edit' => 0],
  9969. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9970. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9971. 'group_forum' => 0,
  9972. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9973. ],
  9974. [],
  9975. true
  9976. );
  9977. return $forumId;
  9978. }
  9979. /**
  9980. * Check and obtain the lp final item if exist
  9981. *
  9982. * @return array lp items
  9983. */
  9984. private function getFinalItem()
  9985. {
  9986. if (empty($this->items)) {
  9987. return null;
  9988. }
  9989. foreach ($this->items as $item) {
  9990. if ($item->type !== 'final_item') {
  9991. continue;
  9992. }
  9993. return $item;
  9994. }
  9995. }
  9996. /**
  9997. * Get the LP Final Item Template
  9998. *
  9999. * @return string
  10000. */
  10001. private function getFinalItemTemplate()
  10002. {
  10003. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'lp/final_item_template/template.html');
  10004. }
  10005. /**
  10006. * Get the LP Final Item Url
  10007. *
  10008. * @return string
  10009. */
  10010. private function getSavedFinalItem()
  10011. {
  10012. $finalItem = $this->getFinalItem();
  10013. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10014. return file_get_contents($doc['absolute_path']);
  10015. }
  10016. /**
  10017. * Get the LP Final Item form
  10018. *
  10019. * @return string
  10020. */
  10021. public function getFinalItemForm()
  10022. {
  10023. $finalItem = $this->getFinalItem();
  10024. $title = '';
  10025. if ($finalItem) {
  10026. $title = $finalItem->title;
  10027. $buttonText = get_lang('Save');
  10028. $content = $this->getSavedFinalItem();
  10029. } else {
  10030. $buttonText = get_lang('LPCreateDocument');
  10031. $content = $this->getFinalItemTemplate();
  10032. }
  10033. $courseInfo = api_get_course_info();
  10034. $result = $this->generate_lp_folder($courseInfo);
  10035. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10036. $relative_prefix = '../../';
  10037. $editorConfig = [
  10038. 'ToolbarSet' => 'LearningPathDocuments',
  10039. 'Width' => '100%',
  10040. 'Height' => '500',
  10041. 'FullPage' => true,
  10042. 'CreateDocumentDir' => $relative_prefix,
  10043. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  10044. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  10045. ];
  10046. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  10047. 'type' => 'document',
  10048. 'lp_id' => $this->lp_id
  10049. ]);
  10050. $form = new FormValidator('final_item', 'POST', $url);
  10051. $form->addText('title', get_lang('Title'));
  10052. $form->addButtonSave($buttonText);
  10053. $form->addHtml(
  10054. Display::return_message(
  10055. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10056. 'normal',
  10057. false
  10058. )
  10059. );
  10060. $renderer = $form->defaultRenderer();
  10061. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10062. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10063. $form->addHidden('action', 'add_final_item');
  10064. $form->addHidden('path', Session::read('pathItem'));
  10065. $form->addHidden('previous', $this->get_last());
  10066. $form->setDefaults(
  10067. ['title' => $title, 'content_lp_certificate' => $content]
  10068. );
  10069. if ($form->validate()) {
  10070. $values = $form->exportValues();
  10071. $lastItemId = $this->get_last();
  10072. if (!$finalItem) {
  10073. $documentId = $this->create_document(
  10074. $this->course_info,
  10075. $values['content_lp_certificate'],
  10076. $values['title']
  10077. );
  10078. $this->add_item(
  10079. 0,
  10080. $lastItemId,
  10081. 'final_item',
  10082. $documentId,
  10083. $values['title'],
  10084. ''
  10085. );
  10086. Display::addFlash(
  10087. Display::return_message(get_lang('Added'))
  10088. );
  10089. } else {
  10090. $this->edit_document($this->course_info);
  10091. }
  10092. }
  10093. return $form->returnForm();
  10094. }
  10095. /**
  10096. * Check if the current lp item is first, both, last or none from lp list
  10097. *
  10098. * @param int $currentItemId
  10099. * @return string
  10100. */
  10101. public function isFirstOrLastItem($currentItemId)
  10102. {
  10103. if ($this->debug > 0) {
  10104. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10105. }
  10106. $lpItemId = [];
  10107. $typeListNotToVerify = self::getChapterTypes();
  10108. // Using get_toc() function instead $this->items because returns the correct order of the items
  10109. foreach ($this->get_toc() as $item) {
  10110. if (!in_array($item['type'], $typeListNotToVerify)) {
  10111. $lpItemId[] = $item['id'];
  10112. }
  10113. }
  10114. $lastLpItemIndex = count($lpItemId) - 1;
  10115. $position = array_search($currentItemId, $lpItemId);
  10116. switch ($position) {
  10117. case 0:
  10118. if (!$lastLpItemIndex) {
  10119. $answer = 'both';
  10120. break;
  10121. }
  10122. $answer = 'first';
  10123. break;
  10124. case $lastLpItemIndex:
  10125. $answer = 'last';
  10126. break;
  10127. default:
  10128. $answer = 'none';
  10129. }
  10130. return $answer;
  10131. }
  10132. /**
  10133. * Get whether this is a learning path with the accumulated SCORM time or not
  10134. * @return int
  10135. */
  10136. public function getAccumulateScormTime()
  10137. {
  10138. return $this->accumulateScormTime;
  10139. }
  10140. /**
  10141. * Set whether this is a learning path with the accumulated SCORM time or not
  10142. * @param int $value (0 = false, 1 = true)
  10143. * @return bool Always returns true
  10144. */
  10145. public function setAccumulateScormTime($value)
  10146. {
  10147. if ($this->debug > 0) {
  10148. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10149. }
  10150. $this->accumulateScormTime = intval($value);
  10151. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  10152. $lp_id = $this->get_id();
  10153. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10154. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10155. Database::query($sql);
  10156. return true;
  10157. }
  10158. /**
  10159. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10160. * the new learning path tool.
  10161. *
  10162. * The function is a big switch on tool type.
  10163. * In each case, we query the corresponding table for information and build the link
  10164. * with that information.
  10165. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on previous work (display_addedresource_link_in_learnpath())
  10166. * @param int $course_id Course code
  10167. * @param int $learningPathId The learning path ID (in lp table)
  10168. * @param int $id_in_path the unique index in the items table
  10169. * @param int $lpViewId
  10170. * @return string
  10171. */
  10172. public static function rl_get_resource_link_for_learnpath($course_id, $learningPathId, $id_in_path, $lpViewId)
  10173. {
  10174. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10175. $course_info = api_get_course_info_by_id($course_id);
  10176. $course_id = $course_info['real_id'];
  10177. $course_code = $course_info['code'];
  10178. $session_id = api_get_session_id();
  10179. $learningPathId = intval($learningPathId);
  10180. $id_in_path = intval($id_in_path);
  10181. $lpViewId = intval($lpViewId);
  10182. $em = Database::getManager();
  10183. $sql = "SELECT * FROM $tbl_lp_item
  10184. WHERE
  10185. c_id = $course_id AND
  10186. lp_id = $learningPathId AND
  10187. id = $id_in_path
  10188. ";
  10189. $res_item = Database::query($sql);
  10190. if (Database::num_rows($res_item) < 1) {
  10191. return -1;
  10192. }
  10193. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10194. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10195. // Get the lp_item_view with the highest view_count.
  10196. $sql = "SELECT * FROM $item_view_table
  10197. WHERE
  10198. c_id = $course_id AND
  10199. lp_item_id = " . $row_item['id'] . " AND
  10200. lp_view_id = " . $lpViewId . "
  10201. ORDER BY view_count DESC";
  10202. $learnpathItemViewResult = Database::query($sql);
  10203. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10204. $learnpathItemViewId = 0;
  10205. if (isset($learnpathItemViewData)) {
  10206. $learnpathItemViewId = $learnpathItemViewData['id'];
  10207. }
  10208. $type = strtolower($row_item['item_type']);
  10209. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10210. $origin = 'learnpath';
  10211. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10212. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10213. $link = '';
  10214. switch ($type) {
  10215. case 'dir':
  10216. $link .= $main_dir_path . 'lp/blank.php';
  10217. case TOOL_CALENDAR_EVENT:
  10218. $link .= $main_dir_path.'calendar/agenda.php?origin='.$origin.'&agenda_id='.$id;
  10219. break;
  10220. case TOOL_ANNOUNCEMENT:
  10221. $link .= $main_dir_path.'announcements/announcements.php?origin='.$origin.'&ann_id='.$id;
  10222. break;
  10223. case TOOL_LINK:
  10224. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10225. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10226. $myrow = Database::fetch_array($result);
  10227. $thelink = $myrow["url"];
  10228. $link .= $thelink;
  10229. break;
  10230. case TOOL_QUIZ:
  10231. if (!empty($id)) {
  10232. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10233. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10234. $result= Database::query($sql);
  10235. $myrow = Database::fetch_array($result);
  10236. if ($row_item['title'] != '') {
  10237. $myrow['title'] = $row_item['title'];
  10238. }
  10239. $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;
  10240. }
  10241. break;
  10242. case 'hotpotatoes': //lowercase because of strtolower above
  10243. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10244. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10245. $myrow = Database::fetch_array($result);
  10246. $path = $myrow['path'];
  10247. $link .= $main_dir_path . 'exercise/showinframes.php?file='.$path.'&origin='.$origin.'&cid='.$course_code.'&uid='.api_get_user_id().'' .
  10248. '&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId;
  10249. break;
  10250. case TOOL_FORUM:
  10251. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&origin=learnpath';
  10252. break;
  10253. case TOOL_THREAD: //forum post
  10254. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10255. if (!empty($id)) {
  10256. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10257. $result = Database::query($sql);
  10258. $myrow = Database::fetch_array($result);
  10259. $link .= $main_dir_path.'forum/viewthread.php?origin=learnpath&thread='.$id.'' .
  10260. '&forum='.$myrow['forum_id'].'&lp=true';
  10261. }
  10262. break;
  10263. case TOOL_POST:
  10264. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10265. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10266. $myrow = Database::fetch_array($result);
  10267. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'' .
  10268. '&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'' .
  10269. '&lp=true';
  10270. break;
  10271. case TOOL_DOCUMENT:
  10272. $document = $em
  10273. ->getRepository('ChamiloCourseBundle:CDocument')
  10274. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10275. if (!$document) {
  10276. break;
  10277. }
  10278. $documentPathInfo = pathinfo($document->getPath());
  10279. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10280. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10281. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10282. if ($showDirectUrl) {
  10283. $link = $main_course_path . 'document' . $document->getPath() . '?' . api_get_cidreq();
  10284. } else {
  10285. $link = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?' . http_build_query([
  10286. 'cidReq' => $course_code,
  10287. 'id' => $id,
  10288. 'origin' => 'learnpathitem'
  10289. ]);
  10290. }
  10291. $openmethod = 2;
  10292. $officedoc = false;
  10293. Session::write('openmethod',$openmethod);
  10294. Session::write('officedoc',$officedoc);
  10295. break;
  10296. case TOOL_LP_FINAL_ITEM:
  10297. $link .= api_get_path(WEB_CODE_PATH) . 'lp/lp_final_item.php?'.api_get_cidreq().'&id='.$id.'&lp_id='.$learningPathId;
  10298. break;
  10299. case 'assignments':
  10300. $link .= $main_dir_path.'work/work.php?origin='.$origin;
  10301. break;
  10302. case TOOL_DROPBOX:
  10303. $link .= $main_dir_path.'dropbox/index.php?origin=learnpath';
  10304. break;
  10305. case 'introduction_text': //DEPRECATED
  10306. $link .= '';
  10307. break;
  10308. case TOOL_COURSE_DESCRIPTION:
  10309. $link .= $main_dir_path.'course_description?origin=learnpath';
  10310. break;
  10311. case TOOL_GROUP:
  10312. $link .= $main_dir_path.'group/group.php?origin='.$origin;
  10313. break;
  10314. case TOOL_USER:
  10315. $link .= $main_dir_path.'user/user.php?origin='.$origin;
  10316. break;
  10317. case TOOL_STUDENTPUBLICATION:
  10318. if (!empty($row_item['path'])) {
  10319. $link .= $main_dir_path.'work/work_list.php?'.api_get_cidreq().'&id='.$row_item['path'].'&origin=learnpath';
  10320. break;
  10321. }
  10322. $link .= $main_dir_path . 'work/work.php?' . api_get_cidreq() . '&' . http_build_query([
  10323. 'id' => $row_item['path'],
  10324. 'origin' => 'learnpath'
  10325. ]);
  10326. break;
  10327. } //end switch
  10328. return $link;
  10329. }
  10330. /**
  10331. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10332. *
  10333. * @author Yannick Warnier <ywarnier@beeznest.org>
  10334. * @param string Course code
  10335. * @param string The tool type (using constants declared in main_api.lib.php)
  10336. * @param integer The resource ID
  10337. */
  10338. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10339. {
  10340. $_course = api_get_course_info($course_code);
  10341. $course_id = $_course['real_id'];
  10342. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10343. $learningPathId = intval($learningPathId);
  10344. $id_in_path = intval($id_in_path);
  10345. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10346. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10347. $res_item = Database::query($sql_item);
  10348. if (Database::num_rows($res_item) < 1) {
  10349. return '';
  10350. }
  10351. $row_item = Database::fetch_array($res_item);
  10352. $type = strtolower($row_item['item_type']);
  10353. $id = $row_item['ref'];
  10354. $output = '';
  10355. switch ($type) {
  10356. case TOOL_CALENDAR_EVENT:
  10357. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10358. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10359. $myrow = Database::fetch_array($result);
  10360. $output = $myrow['title'];
  10361. break;
  10362. case TOOL_ANNOUNCEMENT:
  10363. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10364. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10365. $myrow = Database::fetch_array($result);
  10366. $output = $myrow['title'];
  10367. break;
  10368. case TOOL_LINK:
  10369. // Doesn't take $target into account.
  10370. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10371. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10372. $myrow = Database::fetch_array($result);
  10373. $output = $myrow['title'];
  10374. break;
  10375. case TOOL_QUIZ:
  10376. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10377. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10378. $myrow = Database::fetch_array($result);
  10379. $output = $myrow['title'];
  10380. break;
  10381. case TOOL_FORUM:
  10382. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10383. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10384. $myrow = Database::fetch_array($result);
  10385. $output = $myrow['forum_name'];
  10386. break;
  10387. case TOOL_THREAD: //=topics
  10388. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10389. // Grabbing the title of the post.
  10390. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10391. $result_title = Database::query($sql_title);
  10392. $myrow_title = Database::fetch_array($result_title);
  10393. $output = $myrow_title['post_title'];
  10394. break;
  10395. case TOOL_POST:
  10396. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10397. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10398. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10399. $result = Database::query($sql);
  10400. $post = Database::fetch_array($result);
  10401. $output = $post['post_title'];
  10402. break;
  10403. case 'dir':
  10404. $title = $row_item['title'];
  10405. if (!empty($title)) {
  10406. $output = $title;
  10407. } else {
  10408. $output = '-';
  10409. }
  10410. break;
  10411. case TOOL_DOCUMENT:
  10412. $title = $row_item['title'];
  10413. if (!empty($title)) {
  10414. $output = $title;
  10415. } else {
  10416. $output = '-';
  10417. }
  10418. break;
  10419. case 'hotpotatoes':
  10420. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10421. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10422. $myrow = Database::fetch_array($result);
  10423. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10424. $last = count($pathname) - 1; // Making a correct name for the link.
  10425. $filename = $pathname[$last]; // Making a correct name for the link.
  10426. $ext = explode('.', $filename);
  10427. $ext = strtolower($ext[sizeof($ext) - 1]);
  10428. $myrow['path'] = rawurlencode($myrow['path']);
  10429. $output = $filename;
  10430. break;
  10431. }
  10432. return stripslashes($output);
  10433. }
  10434. }
  10435. if (!function_exists('trim_value')) {
  10436. function trim_value(& $value) {
  10437. $value = trim($value);
  10438. }
  10439. }