learnpath.class.php 450 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $params = [
  202. 'c_id' => $course_id,
  203. 'lp_id' => $lp_id,
  204. 'user_id' => $user_id,
  205. 'view_count' => 1,
  206. 'session_id' => $session_id,
  207. 'last_item' => 0
  208. ];
  209. Database::insert($lp_table, $params);
  210. $this->lp_view_id = Database::insert_id();
  211. if ($this->debug > 2) {
  212. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  213. }
  214. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  215. Database::query($sql);
  216. }
  217. // Initialise items.
  218. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  219. $sql = "SELECT * FROM $lp_item_table
  220. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  221. ORDER BY parent_item_id, display_order";
  222. $res = Database::query($sql);
  223. if ($this->debug > 2) {
  224. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  225. error_log('-- Start while--', 0);
  226. }
  227. $lp_item_id_list = array();
  228. while ($row = Database::fetch_array($res)) {
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - ' .
  243. 'aicc object with id ' . $my_item_id .
  244. ' set in items[]', 0);
  245. }
  246. }
  247. break;
  248. case 2:
  249. $oItem = new scormItem('db', $row['id'], $course_id);
  250. if (is_object($oItem)) {
  251. $my_item_id = $oItem->get_id();
  252. $oItem->set_lp_view($this->lp_view_id, $course_id);
  253. $oItem->set_prevent_reinit($this->prevent_reinit);
  254. // Don't use reference here as the next loop will make the pointed object change.
  255. $this->items[$my_item_id] = $oItem;
  256. $this->refs_list[$oItem->ref] = $my_item_id;
  257. if ($this->debug > 2) {
  258. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  259. }
  260. }
  261. break;
  262. case 1:
  263. default:
  264. if ($this->debug > 2) {
  265. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  266. }
  267. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  268. if ($this->debug > 2) {
  269. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  270. }
  271. if (is_object($oItem)) {
  272. $my_item_id = $oItem->get_id();
  273. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  274. $oItem->set_prevent_reinit($this->prevent_reinit);
  275. // Don't use reference here as the next loop will make the pointed object change.
  276. $this->items[$my_item_id] = $oItem;
  277. $this->refs_list[$my_item_id] = $my_item_id;
  278. if ($this->debug > 2) {
  279. error_log(
  280. 'New LP - learnpath::__construct() ' . __LINE__ .
  281. ' - object with id ' . $my_item_id . ' set in items[]',
  282. 0);
  283. }
  284. }
  285. break;
  286. }
  287. // Setting the object level with variable $this->items[$i][parent]
  288. foreach ($this->items as $itemLPObject) {
  289. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  290. $itemLPObject->level = $level;
  291. }
  292. // Setting the view in the item object.
  293. if (is_object($this->items[$row['id']])) {
  294. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  295. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  296. $this->items[$row['id']]->current_start_time = 0;
  297. $this->items[$row['id']]->current_stop_time = 0;
  298. }
  299. }
  300. }
  301. if ($this->debug > 2) {
  302. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  303. }
  304. if (!empty($lp_item_id_list)) {
  305. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  306. if (!empty($lp_item_id_list_to_string)) {
  307. // Get last viewing vars.
  308. $lp_item_view_table = Database:: get_course_table(
  309. TABLE_LP_ITEM_VIEW
  310. );
  311. // This query should only return one or zero result.
  312. $sql = "SELECT lp_item_id, status
  313. FROM $lp_item_view_table
  314. WHERE
  315. c_id = $course_id AND
  316. lp_view_id = ".$this->lp_view_id." AND
  317. lp_item_id IN ('".$lp_item_id_list_to_string."')
  318. ORDER BY view_count DESC ";
  319. if ($this->debug > 2) {
  320. error_log(
  321. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  322. 0
  323. );
  324. }
  325. $status_list = array();
  326. $res = Database::query($sql);
  327. while ($row = Database:: fetch_array($res)) {
  328. $status_list[$row['lp_item_id']] = $row['status'];
  329. }
  330. foreach ($lp_item_id_list as $item_id) {
  331. if (isset($status_list[$item_id])) {
  332. $status = $status_list[$item_id];
  333. if (is_object($this->items[$item_id])) {
  334. $this->items[$item_id]->set_status($status);
  335. if (empty($status)) {
  336. $this->items[$item_id]->set_status(
  337. $this->default_status
  338. );
  339. }
  340. }
  341. } else {
  342. if (!api_is_invitee()) {
  343. if (is_object($this->items[$item_id])) {
  344. $this->items[$item_id]->set_status(
  345. $this->default_status
  346. );
  347. }
  348. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  349. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  350. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  351. if ($this->debug > 2) {
  352. error_log(
  353. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  354. 0
  355. );
  356. }
  357. $this->items[$item_id]->set_lp_view(
  358. $this->lp_view_id,
  359. $course_id
  360. );
  361. Database::query($sql);
  362. $insertId = Database::insert_id();
  363. $sql = "UPDATE $lp_item_view_table SET id = iid
  364. WHERE iid = $insertId";
  365. Database::query($sql);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. $this->ordered_items = self::get_flat_ordered_items_list(
  372. $this->get_id(),
  373. 0,
  374. $course_id
  375. );
  376. $this->max_ordered_items = 0;
  377. foreach ($this->ordered_items as $index => $dummy) {
  378. if ($index > $this->max_ordered_items && !empty($dummy)) {
  379. $this->max_ordered_items = $index;
  380. }
  381. }
  382. // TODO: Define the current item better.
  383. $this->first();
  384. if ($this->debug > 2) {
  385. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  386. }
  387. return true;
  388. }
  389. /**
  390. * @return string
  391. */
  392. public function getCourseCode()
  393. {
  394. return $this->cc;
  395. }
  396. /**
  397. * @return int
  398. */
  399. public function get_course_int_id()
  400. {
  401. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  402. }
  403. /**
  404. * @param $course_id
  405. * @return int
  406. */
  407. public function set_course_int_id($course_id)
  408. {
  409. return $this->course_int_id = intval($course_id);
  410. }
  411. /**
  412. * Get the depth level of LP item
  413. * @param $in_tab_items
  414. * @param $in_current_item_id
  415. * @return int
  416. */
  417. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  418. {
  419. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  420. if ($parent_item_id == 0) {
  421. return 0;
  422. } else {
  423. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  424. }
  425. }
  426. /**
  427. * Function rewritten based on old_add_item() from Yannick Warnier.
  428. * Due the fact that users can decide where the item should come, I had to overlook this function and
  429. * I found it better to rewrite it. Old function is still available.
  430. * Added also the possibility to add a description.
  431. *
  432. * @param int $parent
  433. * @param int $previous
  434. * @param string $type
  435. * @param int resource ID (ref)
  436. * @param string $title
  437. * @param string $description
  438. * @param int $prerequisites
  439. * @param int $max_time_allowed
  440. * @return int
  441. */
  442. public function add_item(
  443. $parent,
  444. $previous,
  445. $type = 'dokeos_chapter',
  446. $id,
  447. $title,
  448. $description,
  449. $prerequisites = 0,
  450. $max_time_allowed = 0
  451. ) {
  452. $course_id = $this->course_info['real_id'];
  453. if ($this->debug > 0) {
  454. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  455. }
  456. if (empty($course_id)) {
  457. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  458. $this->course_info = api_get_course_info($this->cc);
  459. $course_id = $this->course_info['real_id'];
  460. }
  461. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  462. $_course = $this->course_info;
  463. $parent = intval($parent);
  464. $previous = intval($previous);
  465. $id = intval($id);
  466. $max_time_allowed = htmlentities($max_time_allowed);
  467. if (empty($max_time_allowed)) {
  468. $max_time_allowed = 0;
  469. }
  470. $sql = "SELECT COUNT(id) AS num
  471. FROM $tbl_lp_item
  472. WHERE
  473. c_id = $course_id AND
  474. lp_id = " . $this->get_id() . " AND
  475. parent_item_id = " . $parent;
  476. $res_count = Database::query($sql);
  477. $row = Database :: fetch_array($res_count);
  478. $num = $row['num'];
  479. if ($num > 0) {
  480. if ($previous == 0) {
  481. $sql = "SELECT id, next_item_id, display_order
  482. FROM " . $tbl_lp_item . "
  483. WHERE
  484. c_id = $course_id AND
  485. lp_id = " . $this->get_id() . " AND
  486. parent_item_id = " . $parent . " AND
  487. previous_item_id = 0 OR
  488. previous_item_id=" . $parent;
  489. $result = Database::query($sql);
  490. $row = Database :: fetch_array($result);
  491. $tmp_previous = 0;
  492. $next = $row['id'];
  493. $display_order = 0;
  494. } else {
  495. $previous = (int) $previous;
  496. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  497. FROM $tbl_lp_item
  498. WHERE
  499. c_id = $course_id AND
  500. lp_id = " . $this->get_id() . " AND
  501. id = " . $previous;
  502. $result = Database::query($sql);
  503. $row = Database:: fetch_array($result);
  504. $tmp_previous = $row['id'];
  505. $next = $row['next_item_id'];
  506. $display_order = $row['display_order'];
  507. }
  508. } else {
  509. $tmp_previous = 0;
  510. $next = 0;
  511. $display_order = 0;
  512. }
  513. $id = intval($id);
  514. $typeCleaned = Database::escape_string($type);
  515. if ($type == 'quiz') {
  516. $sql = 'SELECT SUM(ponderation)
  517. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  518. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  519. ON
  520. quiz_question.id = quiz_rel_question.question_id AND
  521. quiz_question.c_id = quiz_rel_question.c_id
  522. WHERE
  523. quiz_rel_question.exercice_id = '.$id." AND
  524. quiz_question.c_id = $course_id AND
  525. quiz_rel_question.c_id = $course_id ";
  526. $rsQuiz = Database::query($sql);
  527. $max_score = Database :: result($rsQuiz, 0, 0);
  528. // Disabling the exercise if we add it inside a LP
  529. $exercise = new Exercise($course_id);
  530. $exercise->read($id);
  531. $exercise->disable();
  532. $exercise->save();
  533. } else {
  534. $max_score = 100;
  535. }
  536. $params = array(
  537. "c_id" => $course_id,
  538. "lp_id" => $this->get_id(),
  539. "item_type" =>$typeCleaned ,
  540. "ref" => '',
  541. "title" =>$title ,
  542. "description" => $description,
  543. "path" => $id,
  544. "max_score" => $max_score,
  545. "parent_item_id" => $parent,
  546. "previous_item_id" => $previous,
  547. "next_item_id" => $next,
  548. "display_order" => $display_order +1,
  549. "prerequisite" => $prerequisites,
  550. "max_time_allowed" => $max_time_allowed,
  551. 'min_score' => 0,
  552. 'launch_data' => ''
  553. );
  554. if ($prerequisites != 0) {
  555. $params['prerequisite'] = $prerequisites;
  556. }
  557. $new_item_id = Database::insert($tbl_lp_item, $params);
  558. if ($this->debug > 2) {
  559. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  560. }
  561. if ($new_item_id) {
  562. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  563. Database::query($sql);
  564. // Update the item that should come after the new item.
  565. $sql = " UPDATE $tbl_lp_item SET
  566. previous_item_id = $new_item_id,
  567. next_item_id = $new_item_id,
  568. id = $new_item_id
  569. WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. // Update all the items after the new item.
  572. $sql = "UPDATE " . $tbl_lp_item . "
  573. SET display_order = display_order + 1
  574. WHERE
  575. c_id = $course_id AND
  576. lp_id = " . $this->get_id() . " AND
  577. id <> " . $new_item_id . " AND
  578. parent_item_id = " . $parent . " AND
  579. display_order > " . $display_order;
  580. Database::query($sql);
  581. // Update the item that should come after the new item.
  582. $sql = "UPDATE " . $tbl_lp_item . "
  583. SET ref = " . $new_item_id . "
  584. WHERE c_id = $course_id AND id = " . $new_item_id;
  585. Database::query($sql);
  586. // Upload audio.
  587. if (!empty($_FILES['mp3']['name'])) {
  588. // Create the audio folder if it does not exist yet.
  589. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  590. if (!is_dir($filepath . 'audio')) {
  591. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  592. $audio_id = add_document(
  593. $_course,
  594. '/audio',
  595. 'folder',
  596. 0,
  597. 'audio'
  598. );
  599. api_item_property_update(
  600. $_course,
  601. TOOL_DOCUMENT,
  602. $audio_id,
  603. 'FolderCreated',
  604. api_get_user_id(),
  605. null,
  606. null,
  607. null,
  608. null,
  609. api_get_session_id()
  610. );
  611. api_item_property_update(
  612. $_course,
  613. TOOL_DOCUMENT,
  614. $audio_id,
  615. 'invisible',
  616. api_get_user_id(),
  617. null,
  618. null,
  619. null,
  620. null,
  621. api_get_session_id()
  622. );
  623. }
  624. $file_path = handle_uploaded_document(
  625. $_course,
  626. $_FILES['mp3'],
  627. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  628. '/audio',
  629. api_get_user_id(),
  630. '',
  631. '',
  632. '',
  633. '',
  634. false
  635. );
  636. // Getting the filename only.
  637. $file_components = explode('/', $file_path);
  638. $file = $file_components[count($file_components) - 1];
  639. // Store the mp3 file in the lp_item table.
  640. $sql = "UPDATE $tbl_lp_item SET
  641. audio = '" . Database::escape_string($file) . "'
  642. WHERE id = '" . intval($new_item_id) . "'";
  643. Database::query($sql);
  644. }
  645. }
  646. return $new_item_id;
  647. }
  648. /**
  649. * Static admin function allowing addition of a learnpath to a course.
  650. * @param string Course code
  651. * @param string Learnpath name
  652. * @param string Learnpath description string, if provided
  653. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  654. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  655. * @param string Zip file containing the learnpath or directory containing the learnpath
  656. * @return integer The new learnpath ID on success, 0 on failure
  657. */
  658. public static function add_lp(
  659. $courseCode,
  660. $name,
  661. $description = '',
  662. $learnpath = 'guess',
  663. $origin = 'zip',
  664. $zipname = '',
  665. $publicated_on = '',
  666. $expired_on = '',
  667. $categoryId = 0
  668. ) {
  669. global $charset;
  670. if (!empty($courseCode)) {
  671. $courseInfo = api_get_course_info($courseCode);
  672. $course_id = $courseInfo['real_id'];
  673. } else {
  674. $course_id = api_get_course_int_id();
  675. $courseInfo = api_get_course_info();
  676. }
  677. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  678. // Check course code exists.
  679. // Check lp_name doesn't exist, otherwise append something.
  680. $i = 0;
  681. $name = Database::escape_string($name);
  682. $categoryId = intval($categoryId);
  683. // Session id.
  684. $session_id = api_get_session_id();
  685. $check_name = "SELECT * FROM $tbl_lp
  686. WHERE c_id = $course_id AND name = '$name'";
  687. $res_name = Database::query($check_name);
  688. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  689. //by default the publication date is the same that the creation date
  690. //The behaviour above was changed due BT#2800
  691. global $_custom;
  692. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  693. $publicated_on = '';
  694. } else {
  695. $publicated_on = api_get_utc_datetime();
  696. }
  697. } else {
  698. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  699. }
  700. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  701. $expired_on = '';
  702. } else {
  703. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  704. }
  705. while (Database :: num_rows($res_name)) {
  706. // There is already one such name, update the current one a bit.
  707. $i++;
  708. $name = $name . ' - ' . $i;
  709. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  710. $res_name = Database::query($check_name);
  711. }
  712. // New name does not exist yet; keep it.
  713. // Escape description.
  714. // Kevin: added htmlentities().
  715. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  716. $type = 1;
  717. switch ($learnpath) {
  718. case 'guess':
  719. break;
  720. case 'dokeos':
  721. case 'chamilo':
  722. $type = 1;
  723. break;
  724. case 'aicc':
  725. break;
  726. }
  727. switch ($origin) {
  728. case 'zip':
  729. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  730. break;
  731. case 'manual':
  732. default:
  733. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  734. $res_max = Database::query($get_max);
  735. if (Database :: num_rows($res_max) < 1) {
  736. $dsp = 1;
  737. } else {
  738. $row = Database :: fetch_array($res_max);
  739. $dsp = $row[0] + 1;
  740. }
  741. $params = [
  742. 'c_id' => $course_id,
  743. 'lp_type' => $type,
  744. 'name' => $name,
  745. 'description' => $description,
  746. 'path' => '',
  747. 'default_view_mod' => 'embedded',
  748. 'default_encoding' => 'UTF-8',
  749. 'display_order' => $dsp,
  750. 'content_maker' => 'Chamilo',
  751. 'content_local' => 'local',
  752. 'js_lib' => '',
  753. 'session_id' => $session_id,
  754. 'created_on' => api_get_utc_datetime(),
  755. 'modified_on' => api_get_utc_datetime(),
  756. 'publicated_on' => $publicated_on,
  757. 'expired_on' => $expired_on,
  758. 'category_id' => $categoryId,
  759. 'force_commit' => 0,
  760. 'content_license' => '',
  761. 'debug' => 0,
  762. 'theme' => '',
  763. 'preview_image' => '',
  764. 'author' => '',
  765. 'prerequisite' => 0,
  766. 'hide_toc_frame' => 0,
  767. 'seriousgame_mode' => 0,
  768. 'autolaunch' => 0,
  769. 'max_attempts' => 0,
  770. 'subscribe_users' => 0
  771. ];
  772. $id = Database::insert($tbl_lp, $params);
  773. if ($id > 0) {
  774. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  775. Database::query($sql);
  776. // Insert into item_property.
  777. api_item_property_update(
  778. $courseInfo,
  779. TOOL_LEARNPATH,
  780. $id,
  781. 'LearnpathAdded',
  782. api_get_user_id()
  783. );
  784. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo);
  785. return $id;
  786. }
  787. break;
  788. }
  789. }
  790. /**
  791. * Auto completes the parents of an item in case it's been completed or passed
  792. * @param integer $item Optional ID of the item from which to look for parents
  793. */
  794. public function autocomplete_parents($item)
  795. {
  796. $debug = $this->debug;
  797. if ($debug) {
  798. error_log('Learnpath::autocomplete_parents()', 0);
  799. }
  800. if (empty($item)) {
  801. $item = $this->current;
  802. }
  803. $currentItem = $this->getItem($item);
  804. if ($currentItem) {
  805. $parent_id = $currentItem->get_parent();
  806. $parent = $this->getItem($parent_id);
  807. if ($parent) {
  808. // if $item points to an object and there is a parent.
  809. if ($debug) {
  810. error_log(
  811. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  812. 0
  813. );
  814. }
  815. // New experiment including failed and browsed in completed status.
  816. //$current_status = $currentItem->get_status();
  817. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  818. // Fixes chapter auto complete
  819. if (true) {
  820. // If the current item is completed or passes or succeeded.
  821. $updateParentStatus = true;
  822. if ($debug) {
  823. error_log('Status of current item is alright', 0);
  824. }
  825. foreach ($parent->get_children() as $childItemId) {
  826. $childItem = $this->getItem($childItemId);
  827. // If children was not set try to get the info
  828. if (empty($childItem->db_item_view_id)) {
  829. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  830. }
  831. // Check all his brothers (parent's children) for completion status.
  832. if ($childItemId != $item) {
  833. if ($debug) {
  834. error_log(
  835. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  836. 0
  837. );
  838. }
  839. // Trying completing parents of failed and browsed items as well.
  840. if ($childItem->status_is(
  841. array(
  842. 'completed',
  843. 'passed',
  844. 'succeeded',
  845. 'browsed',
  846. 'failed'
  847. )
  848. )
  849. ) {
  850. // Keep completion status to true.
  851. continue;
  852. } else {
  853. if ($debug > 2) {
  854. error_log(
  855. '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,
  856. 0
  857. );
  858. }
  859. $updateParentStatus = false;
  860. break;
  861. }
  862. }
  863. }
  864. if ($updateParentStatus) {
  865. // If all the children were completed:
  866. $parent->set_status('completed');
  867. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  868. // Force the status to "completed"
  869. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  870. $this->update_queue[$parent->get_id()] = 'completed';
  871. if ($debug) {
  872. error_log(
  873. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  874. print_r($this->update_queue, 1),
  875. 0
  876. );
  877. }
  878. // Recursive call.
  879. $this->autocomplete_parents($parent->get_id());
  880. }
  881. }
  882. } else {
  883. if ($debug) {
  884. error_log("Parent #$parent_id does not exists");
  885. }
  886. }
  887. } else {
  888. if ($debug) {
  889. error_log("#$item is an item that doesn't have parents");
  890. }
  891. }
  892. }
  893. /**
  894. * Auto saves the current results into the database for the whole learnpath
  895. */
  896. public function autosave()
  897. {
  898. if ($this->debug > 0) {
  899. error_log('New LP - In learnpath::autosave()', 0);
  900. }
  901. // TODO: Add save operations for the learnpath itself.
  902. }
  903. /**
  904. * Closes the current resource
  905. *
  906. * Stops the timer
  907. * Saves into the database if required
  908. * Clears the current resource data from this object
  909. * @return boolean True on success, false on failure
  910. */
  911. public function close()
  912. {
  913. if ($this->debug > 0) {
  914. error_log('New LP - In learnpath::close()', 0);
  915. }
  916. if (empty ($this->lp_id)) {
  917. $this->error = 'Trying to close this learnpath but no ID is set';
  918. return false;
  919. }
  920. $this->current_time_stop = time();
  921. $this->ordered_items = array();
  922. $this->index = 0;
  923. unset ($this->lp_id);
  924. //unset other stuff
  925. return true;
  926. }
  927. /**
  928. * Static admin function allowing removal of a learnpath
  929. * @param array $courseInfo
  930. * @param integer Learnpath ID
  931. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  932. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  933. */
  934. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  935. {
  936. $course_id = api_get_course_int_id();
  937. if (!empty($courseInfo)) {
  938. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  939. }
  940. // TODO: Implement a way of getting this to work when the current object is not set.
  941. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  942. // If an ID is specifically given and the current LP is not the same, prevent delete.
  943. if (!empty ($id) && ($id != $this->lp_id)) {
  944. return false;
  945. }
  946. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  947. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  948. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  949. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  950. // Delete lp item id.
  951. foreach ($this->items as $id => $dummy) {
  952. $sql = "DELETE FROM $lp_item_view
  953. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  954. Database::query($sql);
  955. }
  956. // Proposed by Christophe (nickname: clefevre)
  957. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  958. Database::query($sql);
  959. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  960. Database::query($sql);
  961. self::toggle_publish($this->lp_id, 'i');
  962. if ($this->type == 2 || $this->type == 3) {
  963. // This is a scorm learning path, delete the files as well.
  964. $sql = "SELECT path FROM $lp
  965. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  966. $res = Database::query($sql);
  967. if (Database :: num_rows($res) > 0) {
  968. $row = Database :: fetch_array($res);
  969. $path = $row['path'];
  970. $sql = "SELECT id FROM $lp
  971. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  972. $res = Database::query($sql);
  973. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  974. if ($this->debug > 2) {
  975. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  976. }
  977. } else {
  978. // No other LP uses that directory, delete it.
  979. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  980. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  981. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  982. if ($this->debug > 2) {
  983. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  984. }
  985. // Proposed by Christophe (clefevre).
  986. if (strcmp(substr($path, -2), "/.") == 0) {
  987. $path = substr($path, 0, -1); // Remove "." at the end.
  988. }
  989. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  990. rmdirr($course_scorm_dir . $path);
  991. }
  992. }
  993. }
  994. }
  995. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  996. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  997. // Delete tools
  998. $sql = "DELETE FROM $tbl_tool
  999. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1000. Database::query($sql);
  1001. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1002. Database::query($sql);
  1003. // Updates the display order of all lps.
  1004. $this->update_display_order();
  1005. api_item_property_update(
  1006. api_get_course_info(),
  1007. TOOL_LEARNPATH,
  1008. $this->lp_id,
  1009. 'delete',
  1010. api_get_user_id()
  1011. );
  1012. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1013. if ($link_info !== false) {
  1014. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1015. }
  1016. if (api_get_setting('search_enabled') == 'true') {
  1017. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1018. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1019. }
  1020. }
  1021. /**
  1022. * Removes all the children of one item - dangerous!
  1023. * @param integer $id Element ID of which children have to be removed
  1024. * @return integer Total number of children removed
  1025. */
  1026. public function delete_children_items($id)
  1027. {
  1028. $course_id = $this->course_info['real_id'];
  1029. if ($this->debug > 0) {
  1030. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1031. }
  1032. $num = 0;
  1033. if (empty ($id) || $id != strval(intval($id))) {
  1034. return false;
  1035. }
  1036. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1037. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1038. $res = Database::query($sql);
  1039. while ($row = Database :: fetch_array($res)) {
  1040. $num += $this->delete_children_items($row['id']);
  1041. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1042. Database::query($sql_del);
  1043. $num++;
  1044. }
  1045. return $num;
  1046. }
  1047. /**
  1048. * Removes an item from the current learnpath
  1049. * @param integer $id Elem ID (0 if first)
  1050. * @param integer $remove Whether to remove the resource/data from the
  1051. * system or leave it (default: 'keep', others 'remove')
  1052. * @return integer Number of elements moved
  1053. * @todo implement resource removal
  1054. */
  1055. public function delete_item($id, $remove = 'keep')
  1056. {
  1057. $course_id = api_get_course_int_id();
  1058. if ($this->debug > 0) {
  1059. error_log('New LP - In learnpath::delete_item()', 0);
  1060. }
  1061. // TODO: Implement the resource removal.
  1062. if (empty ($id) || $id != strval(intval($id))) {
  1063. return false;
  1064. }
  1065. // First select item to get previous, next, and display order.
  1066. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1067. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1068. $res_sel = Database::query($sql_sel);
  1069. if (Database :: num_rows($res_sel) < 1) {
  1070. return false;
  1071. }
  1072. $row = Database :: fetch_array($res_sel);
  1073. $previous = $row['previous_item_id'];
  1074. $next = $row['next_item_id'];
  1075. $display = $row['display_order'];
  1076. $parent = $row['parent_item_id'];
  1077. $lp = $row['lp_id'];
  1078. // Delete children items.
  1079. $num = $this->delete_children_items($id);
  1080. if ($this->debug > 2) {
  1081. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1082. }
  1083. // Now delete the item.
  1084. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1085. if ($this->debug > 2) {
  1086. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1087. }
  1088. Database::query($sql_del);
  1089. // Now update surrounding items.
  1090. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1091. WHERE c_id = ".$course_id." AND id = $previous";
  1092. Database::query($sql_upd);
  1093. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1094. WHERE c_id = ".$course_id." AND id = $next";
  1095. Database::query($sql_upd);
  1096. // Now update all following items with new display order.
  1097. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1098. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1099. Database::query($sql_all);
  1100. //Removing prerequisites since the item will not longer exist
  1101. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1102. Database::query($sql_all);
  1103. // Remove from search engine if enabled.
  1104. if (api_get_setting('search_enabled') == 'true') {
  1105. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1106. $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';
  1107. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1108. $res = Database::query($sql);
  1109. if (Database :: num_rows($res) > 0) {
  1110. $row2 = Database :: fetch_array($res);
  1111. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1112. $di = new ChamiloIndexer();
  1113. $di->remove_document((int) $row2['search_did']);
  1114. }
  1115. $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';
  1116. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1117. Database::query($sql);
  1118. }
  1119. }
  1120. /**
  1121. * Updates an item's content in place
  1122. * @param integer $id Element ID
  1123. * @param integer $parent Parent item ID
  1124. * @param integer $previous Previous item ID
  1125. * @param string $title Item title
  1126. * @param string $description Item description
  1127. * @param string $prerequisites Prerequisites (optional)
  1128. * @param array $audio The array resulting of the $_FILES[mp3] element
  1129. * @param int $max_time_allowed
  1130. * @param string $url
  1131. * @return boolean True on success, false on error
  1132. */
  1133. public function edit_item(
  1134. $id,
  1135. $parent,
  1136. $previous,
  1137. $title,
  1138. $description,
  1139. $prerequisites = '0',
  1140. $audio = array(),
  1141. $max_time_allowed = 0,
  1142. $url = ''
  1143. ) {
  1144. $course_id = api_get_course_int_id();
  1145. $_course = api_get_course_info();
  1146. if ($this->debug > 0) {
  1147. error_log('New LP - In learnpath::edit_item()', 0);
  1148. }
  1149. if (empty ($max_time_allowed)) {
  1150. $max_time_allowed = 0;
  1151. }
  1152. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1153. return false;
  1154. }
  1155. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1156. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1157. $res_select = Database::query($sql_select);
  1158. $row_select = Database :: fetch_array($res_select);
  1159. $audio_update_sql = '';
  1160. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1161. // Create the audio folder if it does not exist yet.
  1162. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1163. if (!is_dir($filepath . 'audio')) {
  1164. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1165. $audio_id = add_document(
  1166. $_course,
  1167. '/audio',
  1168. 'folder',
  1169. 0,
  1170. 'audio'
  1171. );
  1172. api_item_property_update(
  1173. $_course,
  1174. TOOL_DOCUMENT,
  1175. $audio_id,
  1176. 'FolderCreated',
  1177. api_get_user_id(),
  1178. null,
  1179. null,
  1180. null,
  1181. null,
  1182. api_get_session_id()
  1183. );
  1184. api_item_property_update(
  1185. $_course,
  1186. TOOL_DOCUMENT,
  1187. $audio_id,
  1188. 'invisible',
  1189. api_get_user_id(),
  1190. null,
  1191. null,
  1192. null,
  1193. null,
  1194. api_get_session_id()
  1195. );
  1196. }
  1197. // Upload file in documents.
  1198. $pi = pathinfo($audio['name']);
  1199. if ($pi['extension'] == 'mp3') {
  1200. $c_det = api_get_course_info($this->cc);
  1201. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1202. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1203. $path = substr($path, 7);
  1204. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1205. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1206. }
  1207. }
  1208. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1209. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1210. // TODO: htmlspecialchars to be checked for encoding related problems.
  1211. if ($same_parent && $same_previous) {
  1212. // Only update title and description.
  1213. $sql = "UPDATE " . $tbl_lp_item . "
  1214. SET title = '" . Database::escape_string($title) . "',
  1215. prerequisite = '" . $prerequisites . "',
  1216. description = '" . Database::escape_string($description) . "'
  1217. " . $audio_update_sql . ",
  1218. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1219. WHERE c_id = ".$course_id." AND id = " . $id;
  1220. Database::query($sql);
  1221. } else {
  1222. $old_parent = $row_select['parent_item_id'];
  1223. $old_previous = $row_select['previous_item_id'];
  1224. $old_next = $row_select['next_item_id'];
  1225. $old_order = $row_select['display_order'];
  1226. $old_prerequisite = $row_select['prerequisite'];
  1227. $old_max_time_allowed = $row_select['max_time_allowed'];
  1228. /* BEGIN -- virtually remove the current item id */
  1229. /* for the next and previous item it is like the current item doesn't exist anymore */
  1230. if ($old_previous != 0) {
  1231. // Next
  1232. $sql = "UPDATE " . $tbl_lp_item . "
  1233. SET next_item_id = " . $old_next . "
  1234. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1235. Database::query($sql);
  1236. }
  1237. if ($old_next != 0) {
  1238. // Previous
  1239. $sql = "UPDATE " . $tbl_lp_item . "
  1240. SET previous_item_id = " . $old_previous . "
  1241. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1242. Database::query($sql);
  1243. }
  1244. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1245. $sql = "UPDATE " . $tbl_lp_item . "
  1246. SET display_order = display_order - 1
  1247. WHERE
  1248. c_id = ".$course_id." AND
  1249. display_order > " . $old_order . " AND
  1250. lp_id = " . $this->lp_id . " AND
  1251. parent_item_id = " . $old_parent;
  1252. Database::query($sql);
  1253. /* END -- virtually remove the current item id */
  1254. /* BEGIN -- update the current item id to his new location */
  1255. if ($previous == 0) {
  1256. // Select the data of the item that should come after the current item.
  1257. $sql = "SELECT id, display_order
  1258. FROM " . $tbl_lp_item . "
  1259. WHERE
  1260. c_id = ".$course_id." AND
  1261. lp_id = " . $this->lp_id . " AND
  1262. parent_item_id = " . $parent . " AND
  1263. previous_item_id = " . $previous;
  1264. $res_select_old = Database::query($sql);
  1265. $row_select_old = Database::fetch_array($res_select_old);
  1266. // If the new parent didn't have children before.
  1267. if (Database :: num_rows($res_select_old) == 0) {
  1268. $new_next = 0;
  1269. $new_order = 1;
  1270. } else {
  1271. $new_next = $row_select_old['id'];
  1272. $new_order = $row_select_old['display_order'];
  1273. }
  1274. } else {
  1275. // Select the data of the item that should come before the current item.
  1276. $sql = "SELECT next_item_id, display_order
  1277. FROM " . $tbl_lp_item . "
  1278. WHERE c_id = ".$course_id." AND id = " . $previous;
  1279. $res_select_old = Database::query($sql);
  1280. $row_select_old = Database :: fetch_array($res_select_old);
  1281. $new_next = $row_select_old['next_item_id'];
  1282. $new_order = $row_select_old['display_order'] + 1;
  1283. }
  1284. // TODO: htmlspecialchars to be checked for encoding related problems.
  1285. // Update the current item with the new data.
  1286. $sql = "UPDATE " . $tbl_lp_item . "
  1287. SET
  1288. title = '" . Database::escape_string($title) . "',
  1289. description = '" . Database::escape_string($description) . "',
  1290. parent_item_id = " . $parent . ",
  1291. previous_item_id = " . $previous . ",
  1292. next_item_id = " . $new_next . ",
  1293. display_order = " . $new_order . "
  1294. " . $audio_update_sql . "
  1295. WHERE c_id = ".$course_id." AND id = " . $id;
  1296. Database::query($sql);
  1297. if ($previous != 0) {
  1298. // Update the previous item's next_item_id.
  1299. $sql = "UPDATE " . $tbl_lp_item . "
  1300. SET next_item_id = " . $id . "
  1301. WHERE c_id = ".$course_id." AND id = " . $previous;
  1302. Database::query($sql);
  1303. }
  1304. if ($new_next != 0) {
  1305. // Update the next item's previous_item_id.
  1306. $sql = "UPDATE " . $tbl_lp_item . "
  1307. SET previous_item_id = " . $id . "
  1308. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1309. Database::query($sql);
  1310. }
  1311. if ($old_prerequisite != $prerequisites) {
  1312. $sql = "UPDATE " . $tbl_lp_item . "
  1313. SET prerequisite = '" . $prerequisites . "'
  1314. WHERE c_id = ".$course_id." AND id = " . $id;
  1315. Database::query($sql);
  1316. }
  1317. if ($old_max_time_allowed != $max_time_allowed) {
  1318. // update max time allowed
  1319. $sql = "UPDATE " . $tbl_lp_item . "
  1320. SET max_time_allowed = " . $max_time_allowed . "
  1321. WHERE c_id = ".$course_id." AND id = " . $id;
  1322. Database::query($sql);
  1323. }
  1324. // Update all the items with the same or a bigger display_order than the current item.
  1325. $sql = "UPDATE " . $tbl_lp_item . "
  1326. SET display_order = display_order + 1
  1327. WHERE
  1328. c_id = ".$course_id." AND
  1329. lp_id = " . $this->get_id() . " AND
  1330. id <> " . $id . " AND
  1331. parent_item_id = " . $parent . " AND
  1332. display_order >= " . $new_order;
  1333. Database::query($sql);
  1334. }
  1335. if ($row_select['item_type'] == 'link') {
  1336. $link = new Link();
  1337. $linkId = $row_select['path'];
  1338. $link->updateLink($linkId, $url);
  1339. }
  1340. }
  1341. /**
  1342. * Updates an item's prereq in place
  1343. * @param integer $id Element ID
  1344. * @param string $prerequisite_id Prerequisite Element ID
  1345. * @param int $mastery_score Prerequisite min score
  1346. * @param int $max_score Prerequisite max score
  1347. *
  1348. * @return boolean True on success, false on error
  1349. */
  1350. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1351. {
  1352. $course_id = api_get_course_int_id();
  1353. if ($this->debug > 0) {
  1354. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1355. }
  1356. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1357. return false;
  1358. }
  1359. $prerequisite_id = intval($prerequisite_id);
  1360. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1361. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1362. $mastery_score = 0;
  1363. }
  1364. if (!is_numeric($max_score) || $max_score < 0) {
  1365. $max_score = 100;
  1366. }
  1367. /*if ($mastery_score > $max_score) {
  1368. $max_score = $mastery_score;
  1369. }*/
  1370. if (!is_numeric($prerequisite_id)) {
  1371. $prerequisite_id = 'NULL';
  1372. }
  1373. $mastery_score = floatval($mastery_score);
  1374. $max_score = floatval($max_score);
  1375. $sql = " UPDATE $tbl_lp_item
  1376. SET
  1377. prerequisite = $prerequisite_id ,
  1378. prerequisite_min_score = $mastery_score ,
  1379. prerequisite_max_score = $max_score
  1380. WHERE c_id = $course_id AND id = $id";
  1381. Database::query($sql);
  1382. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1383. // Will this be enough to ensure unicity?
  1384. /*$sql = " UPDATE $tbl_lp_item
  1385. SET mastery_score = $mastery_score
  1386. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1387. Database::query($sql);*/
  1388. }
  1389. // TODO: Update the item object (can be ignored for now because refreshed).
  1390. return true;
  1391. }
  1392. /**
  1393. * Escapes a string with the available database escape function
  1394. * @param string String to escape
  1395. * @return string String escaped
  1396. * @deprecated use Database::escape_string
  1397. */
  1398. public function escape_string($string)
  1399. {
  1400. return Database::escape_string($string);
  1401. }
  1402. /**
  1403. * Static admin function exporting a learnpath into a zip file
  1404. * @param string Export type (scorm, zip, cd)
  1405. * @param string Course code
  1406. * @param integer Learnpath ID
  1407. * @param string Zip file name
  1408. * @return string Zip file path (or false on error)
  1409. */
  1410. public function export_lp($type, $course, $id, $zipname)
  1411. {
  1412. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1413. return false;
  1414. }
  1415. $url = '';
  1416. switch ($type) {
  1417. case 'scorm':
  1418. break;
  1419. case 'zip':
  1420. break;
  1421. case 'cdrom':
  1422. break;
  1423. }
  1424. return $url;
  1425. }
  1426. /**
  1427. * Gets all the chapters belonging to the same parent as the item/chapter given
  1428. * Can also be called as abstract method
  1429. * @param integer Item ID
  1430. * @return array A list of all the "brother items" (or an empty array on failure)
  1431. */
  1432. public function get_brother_chapters($id)
  1433. {
  1434. $course_id = api_get_course_int_id();
  1435. if ($this->debug > 0) {
  1436. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1437. }
  1438. if (empty($id)|| $id != strval(intval($id))) {
  1439. return array ();
  1440. }
  1441. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1442. $sql_parent = "SELECT * FROM $lp_item
  1443. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1444. $res_parent = Database::query($sql_parent);
  1445. if (Database :: num_rows($res_parent) > 0) {
  1446. $row_parent = Database :: fetch_array($res_parent);
  1447. $parent = $row_parent['parent_item_id'];
  1448. $sql_bros = "SELECT * FROM $lp_item
  1449. WHERE
  1450. c_id = ".$course_id." AND
  1451. parent_item_id = $parent AND
  1452. id = $id AND
  1453. item_type='dokeos_chapter'
  1454. ORDER BY display_order";
  1455. $res_bros = Database::query($sql_bros);
  1456. $list = array ();
  1457. while ($row_bro = Database :: fetch_array($res_bros)) {
  1458. $list[] = $row_bro;
  1459. }
  1460. return $list;
  1461. }
  1462. return array ();
  1463. }
  1464. /**
  1465. * Gets all the items belonging to the same parent as the item given
  1466. * Can also be called as abstract method
  1467. * @param integer Item ID
  1468. * @return array A list of all the "brother items" (or an empty array on failure)
  1469. */
  1470. public function get_brother_items($id)
  1471. {
  1472. $course_id = api_get_course_int_id();
  1473. if ($this->debug > 0) {
  1474. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1475. }
  1476. if (empty ($id) || $id != strval(intval($id))) {
  1477. return array ();
  1478. }
  1479. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1480. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1481. $res_parent = Database::query($sql_parent);
  1482. if (Database :: num_rows($res_parent) > 0) {
  1483. $row_parent = Database :: fetch_array($res_parent);
  1484. $parent = $row_parent['parent_item_id'];
  1485. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1486. ORDER BY display_order";
  1487. $res_bros = Database::query($sql_bros);
  1488. $list = array ();
  1489. while ($row_bro = Database :: fetch_array($res_bros)) {
  1490. $list[] = $row_bro;
  1491. }
  1492. return $list;
  1493. }
  1494. return array ();
  1495. }
  1496. /**
  1497. * Get the specific prefix index terms of this learning path
  1498. * @param string $prefix
  1499. * @return array Array of terms
  1500. */
  1501. public function get_common_index_terms_by_prefix($prefix)
  1502. {
  1503. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1504. $terms = get_specific_field_values_list_by_prefix(
  1505. $prefix,
  1506. $this->cc,
  1507. TOOL_LEARNPATH,
  1508. $this->lp_id
  1509. );
  1510. $prefix_terms = array();
  1511. if (!empty($terms)) {
  1512. foreach ($terms as $term) {
  1513. $prefix_terms[] = $term['value'];
  1514. }
  1515. }
  1516. return $prefix_terms;
  1517. }
  1518. /**
  1519. * Gets the number of items currently completed
  1520. * @return integer The number of items currently completed
  1521. */
  1522. public function get_complete_items_count()
  1523. {
  1524. if ($this->debug > 0) {
  1525. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1526. }
  1527. $i = 0;
  1528. $completedStatusList = array(
  1529. 'completed',
  1530. 'passed',
  1531. 'succeeded',
  1532. 'browsed',
  1533. 'failed'
  1534. );
  1535. foreach ($this->items as $id => $dummy) {
  1536. // Trying failed and browsed considered "progressed" as well.
  1537. if ($this->items[$id]->status_is($completedStatusList) &&
  1538. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1539. $this->items[$id]->get_type() != 'dir'
  1540. ) {
  1541. $i++;
  1542. }
  1543. }
  1544. return $i;
  1545. }
  1546. /**
  1547. * Gets the current item ID
  1548. * @return integer The current learnpath item id
  1549. */
  1550. public function get_current_item_id()
  1551. {
  1552. $current = 0;
  1553. if ($this->debug > 0) {
  1554. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1555. }
  1556. if (!empty($this->current)) {
  1557. $current = $this->current;
  1558. }
  1559. if ($this->debug > 2) {
  1560. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1561. }
  1562. return $current;
  1563. }
  1564. /**
  1565. * Force to get the first learnpath item id
  1566. * @return integer The current learnpath item id
  1567. */
  1568. public function get_first_item_id()
  1569. {
  1570. $current = 0;
  1571. if (is_array($this->ordered_items)) {
  1572. $current = $this->ordered_items[0];
  1573. }
  1574. return $current;
  1575. }
  1576. /**
  1577. * Gets the total number of items available for viewing in this SCORM
  1578. * @return integer The total number of items
  1579. */
  1580. public function get_total_items_count()
  1581. {
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1584. }
  1585. return count($this->items);
  1586. }
  1587. /**
  1588. * Gets the total number of items available for viewing in this SCORM but without chapters
  1589. * @return integer The total no-chapters number of items
  1590. */
  1591. public function get_total_items_count_without_chapters()
  1592. {
  1593. if ($this->debug > 0) {
  1594. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1595. }
  1596. $total = 0;
  1597. $typeListNotToCount = self::getChapterTypes();
  1598. foreach ($this->items as $temp2) {
  1599. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1600. $total++;
  1601. }
  1602. }
  1603. return $total;
  1604. }
  1605. /**
  1606. * Gets the first element URL.
  1607. * @return string URL to load into the viewer
  1608. */
  1609. public function first()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::first()', 0);
  1613. error_log('$this->last_item_seen '.$this->last_item_seen);
  1614. }
  1615. // Test if the last_item_seen exists and is not a dir.
  1616. if (count($this->ordered_items) == 0) {
  1617. $this->index = 0;
  1618. }
  1619. if ($this->debug > 0) {
  1620. if (isset($this->items[$this->last_item_seen])) {
  1621. $status = $this->items[$this->last_item_seen]->get_status();
  1622. }
  1623. error_log('status '.$status);
  1624. }
  1625. if (!empty($this->last_item_seen) &&
  1626. !empty($this->items[$this->last_item_seen]) &&
  1627. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1628. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1629. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1630. //&& !$this->items[$this->last_item_seen]->is_done()
  1631. ) {
  1632. if ($this->debug > 2) {
  1633. 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);
  1634. }
  1635. $index = -1;
  1636. foreach ($this->ordered_items as $myindex => $item_id) {
  1637. if ($item_id == $this->last_item_seen) {
  1638. $index = $myindex;
  1639. break;
  1640. }
  1641. }
  1642. if ($index == -1) {
  1643. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1644. if ($this->debug > 2) {
  1645. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1646. }
  1647. return false;
  1648. } else {
  1649. $this->last = $this->last_item_seen;
  1650. $this->current = $this->last_item_seen;
  1651. $this->index = $index;
  1652. }
  1653. } else {
  1654. if ($this->debug > 2) {
  1655. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1656. }
  1657. $index = 0;
  1658. // Loop through all ordered items and stop at the first item that is
  1659. // not a directory *and* that has not been completed yet.
  1660. while ( !empty($this->ordered_items[$index]) AND
  1661. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1662. (
  1663. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1664. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1665. $this->items[$this->ordered_items[$index]]->is_done() === true
  1666. ) AND $index < $this->max_ordered_items) {
  1667. $index++;
  1668. }
  1669. $this->last = $this->current;
  1670. // current is
  1671. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1672. $this->index = $index;
  1673. if ($this->debug > 2) {
  1674. error_log('$index ' . $index);
  1675. }
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1678. }
  1679. }
  1680. if ($this->debug > 2) {
  1681. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1682. }
  1683. }
  1684. /**
  1685. * Gets the information about an item in a format usable as JavaScript to update
  1686. * the JS API by just printing this content into the <head> section of the message frame
  1687. * @param int $item_id
  1688. * @return string
  1689. */
  1690. public function get_js_info($item_id = '')
  1691. {
  1692. if ($this->debug > 0) {
  1693. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1694. }
  1695. $info = '';
  1696. $item_id = intval($item_id);
  1697. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1698. //if item is defined, return values from DB
  1699. $oItem = $this->items[$item_id];
  1700. $info .= '<script language="javascript">';
  1701. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1702. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1703. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1704. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1705. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1706. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1707. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1708. $info .= "top.set_flag_synchronized();";
  1709. $info .= '</script>';
  1710. if ($this->debug > 2) {
  1711. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1712. }
  1713. return $info;
  1714. } else {
  1715. // If item_id is empty, just update to default SCORM data.
  1716. $info .= '<script language="javascript">';
  1717. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1718. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1719. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1720. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1721. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1722. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1723. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1724. $info .= "top.set_flag_synchronized();";
  1725. $info .= '</script>';
  1726. if ($this->debug > 2) {
  1727. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1728. }
  1729. return $info;
  1730. }
  1731. }
  1732. /**
  1733. * Gets the js library from the database
  1734. * @return string The name of the javascript library to be used
  1735. */
  1736. public function get_js_lib()
  1737. {
  1738. $lib = '';
  1739. if (!empty ($this->js_lib)) {
  1740. $lib = $this->js_lib;
  1741. }
  1742. return $lib;
  1743. }
  1744. /**
  1745. * Gets the learnpath database ID
  1746. * @return integer Learnpath ID in the lp table
  1747. */
  1748. public function get_id()
  1749. {
  1750. if (!empty ($this->lp_id)) {
  1751. return $this->lp_id;
  1752. } else {
  1753. return 0;
  1754. }
  1755. }
  1756. /**
  1757. * Gets the last element URL.
  1758. * @return string URL to load into the viewer
  1759. */
  1760. public function get_last()
  1761. {
  1762. if ($this->debug > 0) {
  1763. error_log('New LP - In learnpath::get_last()', 0);
  1764. }
  1765. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1766. if ($this->index > 0) {
  1767. $this->index = count($this->ordered_items) - 1;
  1768. return $this->ordered_items[$this->index];
  1769. }
  1770. return false;
  1771. }
  1772. /**
  1773. * Gets the navigation bar for the learnpath display screen
  1774. * @return string The HTML string to use as a navigation bar
  1775. */
  1776. public function get_navigation_bar($idBar = null, $display=null) {
  1777. if ($this->debug > 0) {
  1778. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1779. }
  1780. if(empty($idBar)){
  1781. $idBar='control-top';
  1782. }
  1783. if(empty($display)){
  1784. $display='display:block';
  1785. }
  1786. $navbar = null;
  1787. $lp_id = $this->lp_id;
  1788. $mycurrentitemid = $this->get_current_item_id();
  1789. if ($this->mode == 'fullscreen') {
  1790. $navbar = '
  1791. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1792. <a 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_blank" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1793. <a id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1794. <a id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1795. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
  1796. </div>';
  1797. } else {
  1798. $navbar = '
  1799. <span id="'.$idBar.'" class="buttons text-right">
  1800. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link">
  1801. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1802. </a>
  1803. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1804. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1805. </a>
  1806. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1807. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1808. </a>
  1809. </span>';
  1810. }
  1811. return $navbar;
  1812. }
  1813. /**
  1814. * Gets the next resource in queue (url).
  1815. * @return string URL to load into the viewer
  1816. */
  1817. public function get_next_index()
  1818. {
  1819. if ($this->debug > 0) {
  1820. error_log('New LP - In learnpath::get_next_index()', 0);
  1821. }
  1822. // TODO
  1823. $index = $this->index;
  1824. $index++;
  1825. if ($this->debug > 2) {
  1826. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1827. }
  1828. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1829. $index++;
  1830. if ($index == $this->max_ordered_items){
  1831. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1832. return $this->index;
  1833. } else {
  1834. return $index;
  1835. }
  1836. }
  1837. }
  1838. if (empty ($this->ordered_items[$index])) {
  1839. return $this->index;
  1840. }
  1841. if ($this->debug > 2) {
  1842. error_log('New LP - index is now ' . $index, 0);
  1843. }
  1844. return $index;
  1845. }
  1846. /**
  1847. * Gets item_id for the next element
  1848. * @return integer Next item (DB) ID
  1849. */
  1850. public function get_next_item_id()
  1851. {
  1852. if ($this->debug > 0) {
  1853. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1854. }
  1855. $new_index = $this->get_next_index();
  1856. if (!empty ($new_index)) {
  1857. if (isset ($this->ordered_items[$new_index])) {
  1858. if ($this->debug > 2) {
  1859. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1860. }
  1861. return $this->ordered_items[$new_index];
  1862. }
  1863. }
  1864. if ($this->debug > 2) {
  1865. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1866. }
  1867. return 0;
  1868. }
  1869. /**
  1870. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1871. *
  1872. * Generally, the package provided is in the form of a zip file, so the function
  1873. * has been written to test a zip file. If not a zip, the function will return the
  1874. * default return value: ''
  1875. * @param string the path to the file
  1876. * @param string the original name of the file
  1877. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1878. */
  1879. public static function get_package_type($file_path, $file_name)
  1880. {
  1881. // Get name of the zip file without the extension.
  1882. $file_info = pathinfo($file_name);
  1883. $filename = $file_info['basename']; // Name including extension.
  1884. $extension = $file_info['extension']; // Extension only.
  1885. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1886. 'dll',
  1887. 'exe'
  1888. ))) {
  1889. return 'oogie';
  1890. }
  1891. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1892. 'dll',
  1893. 'exe'
  1894. ))) {
  1895. return 'woogie';
  1896. }
  1897. // Filename without its extension.
  1898. $file_base_name = str_replace('.' . $extension, '', $filename);
  1899. $zipFile = new PclZip($file_path);
  1900. // Check the zip content (real size and file extension).
  1901. $zipContentArray = $zipFile->listContent();
  1902. $package_type = '';
  1903. $at_root = false;
  1904. $manifest = '';
  1905. $aicc_match_crs = 0;
  1906. $aicc_match_au = 0;
  1907. $aicc_match_des = 0;
  1908. $aicc_match_cst = 0;
  1909. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1910. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1911. foreach ($zipContentArray as $thisContent) {
  1912. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1913. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1914. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1915. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1916. $package_type = 'scorm';
  1917. break; // Exit the foreach loop.
  1918. } elseif (
  1919. preg_match('/aicc\//i', $thisContent['filename']) ||
  1920. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1921. ) {
  1922. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1923. switch ($ext) {
  1924. case 'crs':
  1925. $aicc_match_crs = 1;
  1926. break;
  1927. case 'au':
  1928. $aicc_match_au = 1;
  1929. break;
  1930. case 'des':
  1931. $aicc_match_des = 1;
  1932. break;
  1933. case 'cst':
  1934. $aicc_match_cst = 1;
  1935. break;
  1936. default:
  1937. break;
  1938. }
  1939. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1940. } else {
  1941. $package_type = '';
  1942. }
  1943. }
  1944. }
  1945. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1946. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1947. $package_type = 'aicc';
  1948. }
  1949. return $package_type;
  1950. }
  1951. /**
  1952. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1953. * @return string URL to load into the viewer
  1954. */
  1955. public function get_previous_index()
  1956. {
  1957. if ($this->debug > 0) {
  1958. error_log('New LP - In learnpath::get_previous_index()', 0);
  1959. }
  1960. $index = $this->index;
  1961. if (isset ($this->ordered_items[$index -1])) {
  1962. $index--;
  1963. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1964. $index--;
  1965. if ($index < 0) {
  1966. return $this->index;
  1967. }
  1968. }
  1969. } else {
  1970. if ($this->debug > 2) {
  1971. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1972. }
  1973. // There is no previous item.
  1974. }
  1975. return $index;
  1976. }
  1977. /**
  1978. * Gets item_id for the next element
  1979. * @return integer Previous item (DB) ID
  1980. */
  1981. public function get_previous_item_id()
  1982. {
  1983. if ($this->debug > 0) {
  1984. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1985. }
  1986. $new_index = $this->get_previous_index();
  1987. return $this->ordered_items[$new_index];
  1988. }
  1989. /**
  1990. * Gets the progress value from the progress_db attribute
  1991. * @return integer Current progress value
  1992. */
  1993. public function get_progress()
  1994. {
  1995. if ($this->debug > 0) {
  1996. error_log('New LP - In learnpath::get_progress()', 0);
  1997. }
  1998. if (!empty ($this->progress_db)) {
  1999. return $this->progress_db;
  2000. }
  2001. return 0;
  2002. }
  2003. /**
  2004. * Returns the HTML necessary to print a mediaplayer block inside a page
  2005. * @return string The mediaplayer HTML
  2006. */
  2007. public function get_mediaplayer($autostart = 'true')
  2008. {
  2009. $course_id = api_get_course_int_id();
  2010. $_course = api_get_course_info();
  2011. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2012. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2013. // Getting all the information about the item.
  2014. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2015. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2016. ON lp.id = lp_view.lp_item_id
  2017. WHERE
  2018. lp.id = '".$_SESSION['oLP']->current."' AND
  2019. lp.c_id = $course_id AND
  2020. lp_view.c_id = $course_id";
  2021. $result = Database::query($sql);
  2022. $row = Database::fetch_assoc($result);
  2023. $output = '';
  2024. if (!empty ($row['audio'])) {
  2025. $list = $_SESSION['oLP']->get_toc();
  2026. $type_quiz = false;
  2027. foreach($list as $toc) {
  2028. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2029. $type_quiz = true;
  2030. }
  2031. }
  2032. if ($type_quiz) {
  2033. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2034. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2035. } else {
  2036. $autostart_audio = $autostart;
  2037. }
  2038. } else {
  2039. $autostart_audio = 'true';
  2040. }
  2041. $courseInfo = api_get_course_info();
  2042. $audio = $row['audio'];
  2043. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2044. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2045. if (!file_exists($file)) {
  2046. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2047. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2048. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2049. }
  2050. $player = Display::getMediaPlayer(
  2051. $file,
  2052. array(
  2053. 'id' => 'lp_audio_media_player',
  2054. 'url' => $url,
  2055. 'autoplay' => $autostart_audio,
  2056. 'width' => '100%'
  2057. )
  2058. );
  2059. // The mp3 player.
  2060. $output = '<div id="container">';
  2061. $output .= $player;
  2062. $output .= '</div>';
  2063. }
  2064. return $output;
  2065. }
  2066. /**
  2067. * Checks if the learning path is visible for student after the progress
  2068. * of its prerequisite is completed, considering the time availability and
  2069. * the LP visibility.
  2070. * @param int $lp_id
  2071. * @param int $student_id
  2072. * @param string Course code (optional)
  2073. * @param int $sessionId
  2074. * @return bool
  2075. */
  2076. public static function is_lp_visible_for_student(
  2077. $lp_id,
  2078. $student_id,
  2079. $courseCode = null,
  2080. $sessionId = null
  2081. ) {
  2082. $lp_id = (int)$lp_id;
  2083. $courseInfo = api_get_course_info($courseCode);
  2084. $sessionId = intval($sessionId);
  2085. if (empty($sessionId)) {
  2086. $sessionId = api_get_session_id();
  2087. }
  2088. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2089. // Get current prerequisite
  2090. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2091. FROM $tbl_learnpath
  2092. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2093. $itemInfo = api_get_item_property_info(
  2094. $courseInfo['real_id'],
  2095. TOOL_LEARNPATH,
  2096. $lp_id,
  2097. $sessionId
  2098. );
  2099. // If the item was deleted.
  2100. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2101. return false;
  2102. }
  2103. $rs = Database::query($sql);
  2104. $now = time();
  2105. if (Database::num_rows($rs) > 0) {
  2106. $row = Database::fetch_array($rs, 'ASSOC');
  2107. $prerequisite = $row['prerequisite'];
  2108. $is_visible = true;
  2109. if (!empty($prerequisite)) {
  2110. $progress = self::getProgress(
  2111. $prerequisite,
  2112. $student_id,
  2113. $courseInfo['real_id'],
  2114. $sessionId
  2115. );
  2116. $progress = intval($progress);
  2117. if ($progress < 100) {
  2118. $is_visible = false;
  2119. }
  2120. }
  2121. // Also check the time availability of the LP
  2122. if ($is_visible) {
  2123. // Adding visibility restrictions
  2124. if (!empty($row['publicated_on']) &&
  2125. $row['publicated_on'] != '0000-00-00 00:00:00'
  2126. ) {
  2127. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2128. //api_not_allowed();
  2129. $is_visible = false;
  2130. }
  2131. }
  2132. // Blocking empty start times see BT#2800
  2133. global $_custom;
  2134. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2135. $_custom['lps_hidden_when_no_start_date']
  2136. ) {
  2137. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2138. //api_not_allowed();
  2139. $is_visible = false;
  2140. }
  2141. }
  2142. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2143. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2144. //api_not_allowed();
  2145. $is_visible = false;
  2146. }
  2147. }
  2148. }
  2149. // Check if the subscription users/group to a LP is ON
  2150. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2151. // Try group
  2152. $is_visible = false;
  2153. // Checking only the user visibility
  2154. $userVisibility = api_get_item_visibility(
  2155. $courseInfo,
  2156. 'learnpath',
  2157. $row['id'],
  2158. $sessionId,
  2159. $student_id,
  2160. 'LearnpathSubscription'
  2161. );
  2162. if ($userVisibility == 1) {
  2163. $is_visible = true;
  2164. } else {
  2165. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2166. if (!empty($userGroups)) {
  2167. foreach ($userGroups as $groupInfo) {
  2168. $groupId = $groupInfo['iid'];
  2169. $userVisibility = api_get_item_visibility(
  2170. $courseInfo,
  2171. 'learnpath',
  2172. $row['id'],
  2173. $sessionId,
  2174. null,
  2175. 'LearnpathSubscription',
  2176. $groupId
  2177. );
  2178. if ($userVisibility == 1) {
  2179. $is_visible = true;
  2180. break;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. }
  2186. return $is_visible;
  2187. }
  2188. return false;
  2189. }
  2190. /**
  2191. * @param int $lpId
  2192. * @param int $userId
  2193. * @param int $courseId
  2194. * @param int $sessionId
  2195. * @return int
  2196. */
  2197. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2198. {
  2199. $lpId = intval($lpId);
  2200. $userId = intval($userId);
  2201. $courseId = intval($courseId);
  2202. $sessionId = intval($sessionId);
  2203. $progress = 0;
  2204. $sessionCondition = api_get_session_condition($sessionId);
  2205. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2206. $sql = "SELECT * FROM $table
  2207. WHERE
  2208. c_id = ".$courseId." AND
  2209. lp_id = $lpId AND
  2210. user_id = $userId $sessionCondition";
  2211. $res = Database::query($sql);
  2212. if (Database :: num_rows($res) > 0) {
  2213. $row = Database:: fetch_array($res);
  2214. $progress = $row['progress'];
  2215. }
  2216. return $progress;
  2217. }
  2218. /**
  2219. * Displays a progress bar
  2220. * completed so far.
  2221. * @param integer $percentage Progress value to display
  2222. * @param string $text_add Text to display near the progress value
  2223. * @return string HTML string containing the progress bar
  2224. */
  2225. public static function get_progress_bar($percentage = -1, $text_add = '')
  2226. {
  2227. $text = $percentage . $text_add;
  2228. $output = '<div class="progress">
  2229. <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.';">
  2230. '. $text .'
  2231. </div>
  2232. </div>';
  2233. return $output;
  2234. }
  2235. /**
  2236. * @param string $mode can be '%' or 'abs'
  2237. * otherwise this value will be used $this->progress_bar_mode
  2238. * @return string
  2239. */
  2240. public function getProgressBar($mode = null)
  2241. {
  2242. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2243. return self::get_progress_bar($percentage, $text_add);
  2244. }
  2245. /**
  2246. * Gets the progress bar info to display inside the progress bar.
  2247. * Also used by scorm_api.php
  2248. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2249. * we display a number of completed elements per total elements
  2250. * @param integer $add Additional steps to fake as completed
  2251. * @return list array Percentage or number and symbol (% or /xx)
  2252. */
  2253. public function get_progress_bar_text($mode = '', $add = 0)
  2254. {
  2255. if ($this->debug > 0) {
  2256. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2257. }
  2258. if (empty($mode)) {
  2259. $mode = $this->progress_bar_mode;
  2260. }
  2261. $total_items = $this->get_total_items_count_without_chapters();
  2262. if ($this->debug > 2) {
  2263. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2264. }
  2265. $completeItems = $this->get_complete_items_count();
  2266. if ($this->debug > 2) {
  2267. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2268. }
  2269. if ($add != 0) {
  2270. $completeItems += $add;
  2271. if ($this->debug > 2) {
  2272. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2273. }
  2274. }
  2275. $text = '';
  2276. if ($completeItems > $total_items) {
  2277. $completeItems = $total_items;
  2278. }
  2279. $percentage = 0;
  2280. if ($mode == '%') {
  2281. if ($total_items > 0) {
  2282. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2283. } else {
  2284. $percentage = 0;
  2285. }
  2286. $percentage = number_format($percentage, 0);
  2287. $text = '%';
  2288. } elseif ($mode == 'abs') {
  2289. $percentage = $completeItems;
  2290. $text = '/' . $total_items;
  2291. }
  2292. return array(
  2293. $percentage,
  2294. $text
  2295. );
  2296. }
  2297. /**
  2298. * Gets the progress bar mode
  2299. * @return string The progress bar mode attribute
  2300. */
  2301. public function get_progress_bar_mode()
  2302. {
  2303. if ($this->debug > 0) {
  2304. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2305. }
  2306. if (!empty ($this->progress_bar_mode)) {
  2307. return $this->progress_bar_mode;
  2308. } else {
  2309. return '%';
  2310. }
  2311. }
  2312. /**
  2313. * Gets the learnpath proximity (remote or local)
  2314. * @return string Learnpath proximity
  2315. */
  2316. public function get_proximity()
  2317. {
  2318. if ($this->debug > 0) {
  2319. error_log('New LP - In learnpath::get_proximity()', 0);
  2320. }
  2321. if (!empty ($this->proximity)) {
  2322. return $this->proximity;
  2323. } else {
  2324. return '';
  2325. }
  2326. }
  2327. /**
  2328. * Gets the learnpath theme (remote or local)
  2329. * @return string Learnpath theme
  2330. */
  2331. public function get_theme()
  2332. {
  2333. if ($this->debug > 0) {
  2334. error_log('New LP - In learnpath::get_theme()', 0);
  2335. }
  2336. if (!empty ($this->theme)) {
  2337. return $this->theme;
  2338. } else {
  2339. return '';
  2340. }
  2341. }
  2342. /**
  2343. * Gets the learnpath session id
  2344. * @return string Learnpath theme
  2345. */
  2346. public function get_lp_session_id()
  2347. {
  2348. if ($this->debug > 0) {
  2349. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2350. }
  2351. if (!empty ($this->lp_session_id)) {
  2352. return $this->lp_session_id;
  2353. } else {
  2354. return 0;
  2355. }
  2356. }
  2357. /**
  2358. * Gets the learnpath image
  2359. * @return string Web URL of the LP image
  2360. */
  2361. public function get_preview_image()
  2362. {
  2363. if ($this->debug > 0) {
  2364. error_log('New LP - In learnpath::get_preview_image()', 0);
  2365. }
  2366. if (!empty($this->preview_image)) {
  2367. return $this->preview_image;
  2368. } else {
  2369. return '';
  2370. }
  2371. }
  2372. /**
  2373. * @param string $size
  2374. * @param string $path_type
  2375. * @return bool|string
  2376. */
  2377. public function get_preview_image_path($size = null, $path_type = 'web')
  2378. {
  2379. $preview_image = $this->get_preview_image();
  2380. if (isset($preview_image) && !empty($preview_image)) {
  2381. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2382. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2383. if (isset($size)) {
  2384. $info = pathinfo($preview_image);
  2385. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2386. if (file_exists($image_sys_path.$image_custom_size)) {
  2387. if ($path_type == 'web') {
  2388. return $image_path.$image_custom_size;
  2389. } else {
  2390. return $image_sys_path.$image_custom_size;
  2391. }
  2392. }
  2393. } else {
  2394. if ($path_type == 'web') {
  2395. return $image_path.$preview_image;
  2396. } else {
  2397. return $image_sys_path.$preview_image;
  2398. }
  2399. }
  2400. }
  2401. return false;
  2402. }
  2403. /**
  2404. * Gets the learnpath author
  2405. * @return string LP's author
  2406. */
  2407. public function get_author()
  2408. {
  2409. if ($this->debug > 0) {
  2410. error_log('New LP - In learnpath::get_author()', 0);
  2411. }
  2412. if (!empty ($this->author)) {
  2413. return $this->author;
  2414. } else {
  2415. return '';
  2416. }
  2417. }
  2418. /**
  2419. * Gets the learnpath author
  2420. * @return string LP's author
  2421. */
  2422. public function get_hide_toc_frame()
  2423. {
  2424. if ($this->debug > 0) {
  2425. error_log('New LP - In learnpath::get_author()', 0);
  2426. }
  2427. if (!empty ($this->hide_toc_frame)) {
  2428. return $this->hide_toc_frame;
  2429. } else {
  2430. return '';
  2431. }
  2432. }
  2433. /**
  2434. * Generate a new prerequisites string for a given item. If this item was a sco and
  2435. * its prerequisites were strings (instead of IDs), then transform those strings into
  2436. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2437. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2438. * same rule as the scorm_export() method
  2439. * @param integer Item ID
  2440. * @return string Prerequisites string ready for the export as SCORM
  2441. */
  2442. public function get_scorm_prereq_string($item_id)
  2443. {
  2444. if ($this->debug > 0) {
  2445. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2446. }
  2447. if (!is_object($this->items[$item_id])) {
  2448. return false;
  2449. }
  2450. /** @var learnpathItem $oItem */
  2451. $oItem = $this->items[$item_id];
  2452. $prereq = $oItem->get_prereq_string();
  2453. if (empty($prereq)) {
  2454. return '';
  2455. }
  2456. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2457. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2458. // then simply return it (with the ITEM_ prefix).
  2459. //return 'ITEM_' . $prereq;
  2460. return $this->items[$prereq]->ref;
  2461. } else {
  2462. if (isset($this->refs_list[$prereq])) {
  2463. // It's a simple string item from which the ID can be found in the refs list,
  2464. // so we can transform it directly to an ID for export.
  2465. return $this->items[$this->refs_list[$prereq]]->ref;
  2466. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2467. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2468. } else {
  2469. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2470. // and replace them, one by one, by the internal IDs (chamilo db)
  2471. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2472. // by a space as well.
  2473. $find = array (
  2474. '&',
  2475. '|',
  2476. '~',
  2477. '=',
  2478. '<>',
  2479. '{',
  2480. '}',
  2481. '*',
  2482. '(',
  2483. ')'
  2484. );
  2485. $replace = array (
  2486. ' ',
  2487. ' ',
  2488. ' ',
  2489. ' ',
  2490. ' ',
  2491. ' ',
  2492. ' ',
  2493. ' ',
  2494. ' ',
  2495. ' '
  2496. );
  2497. $prereq_mod = str_replace($find, $replace, $prereq);
  2498. $ids = explode(' ', $prereq_mod);
  2499. foreach ($ids as $id) {
  2500. $id = trim($id);
  2501. if (isset ($this->refs_list[$id])) {
  2502. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2503. }
  2504. }
  2505. return $prereq;
  2506. }
  2507. }
  2508. }
  2509. /**
  2510. * Returns the XML DOM document's node
  2511. * @param resource Reference to a list of objects to search for the given ITEM_*
  2512. * @param string The identifier to look for
  2513. * @return mixed The reference to the element found with that identifier. False if not found
  2514. */
  2515. public function get_scorm_xml_node(& $children, $id)
  2516. {
  2517. for ($i = 0; $i < $children->length; $i++) {
  2518. $item_temp = $children->item($i);
  2519. if ($item_temp->nodeName == 'item') {
  2520. if ($item_temp->getAttribute('identifier') == $id) {
  2521. return $item_temp;
  2522. }
  2523. }
  2524. $subchildren = $item_temp->childNodes;
  2525. if ($subchildren->length > 0) {
  2526. $val = $this->get_scorm_xml_node($subchildren, $id);
  2527. if (is_object($val)) {
  2528. return $val;
  2529. }
  2530. }
  2531. }
  2532. return false;
  2533. }
  2534. /**
  2535. * Returns a usable array of stats related to the current learnpath and user
  2536. * @return array Well-formatted array containing status for the current learnpath
  2537. */
  2538. public function get_stats()
  2539. {
  2540. if ($this->debug > 0) {
  2541. error_log('New LP - In learnpath::get_stats()', 0);
  2542. }
  2543. }
  2544. /**
  2545. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2546. * the given course (for all learnpaths and all users)
  2547. * @param string Course code
  2548. * @return array Well-formatted array containing status for the course's learnpaths
  2549. */
  2550. public function get_stats_course($course)
  2551. {
  2552. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2553. // TODO
  2554. }
  2555. /**
  2556. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2557. * the given course and learnpath (for all users)
  2558. * @param string Course code
  2559. * @param integer Learnpath ID
  2560. * @return array Well-formatted array containing status for the specified learnpath
  2561. */
  2562. public function get_stats_lp($course, $lp)
  2563. {
  2564. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2565. // TODO
  2566. }
  2567. /**
  2568. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2569. * the given course, learnpath and user.
  2570. * @param string Course code
  2571. * @param integer Learnpath ID
  2572. * @param integer User ID
  2573. * @return array Well-formatted array containing status for the specified learnpath and user
  2574. */
  2575. public function get_stats_lp_user($course, $lp, $user)
  2576. {
  2577. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2578. // TODO
  2579. }
  2580. /**
  2581. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2582. * the given course and learnpath (for all users)
  2583. * @param string Course code
  2584. * @param integer User ID
  2585. * @return array Well-formatted array containing status for the user's learnpaths
  2586. */
  2587. public function get_stats_user($course, $user) {
  2588. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2589. // TODO
  2590. }
  2591. /**
  2592. * Gets the status list for all LP's items
  2593. * @return array Array of [index] => [item ID => current status]
  2594. */
  2595. public function get_items_status_list()
  2596. {
  2597. if ($this->debug > 0) {
  2598. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2599. }
  2600. $list = array ();
  2601. foreach ($this->ordered_items as $item_id) {
  2602. $list[] = array (
  2603. $item_id => $this->items[$item_id]->get_status()
  2604. );
  2605. }
  2606. return $list;
  2607. }
  2608. /**
  2609. * Return the number of interactions for the given learnpath Item View ID.
  2610. * This method can be used as static.
  2611. * @param integer Item View ID
  2612. * @param integer course id
  2613. * @return integer Number of interactions
  2614. */
  2615. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2616. {
  2617. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2618. $lp_iv_id = intval($lp_iv_id);
  2619. $course_id = intval($course_id);
  2620. $sql = "SELECT count(*) FROM $table
  2621. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2622. $res = Database::query($sql);
  2623. $num = 0;
  2624. if (Database::num_rows($res)) {
  2625. $row = Database::fetch_array($res);
  2626. $num = $row[0];
  2627. }
  2628. return $num;
  2629. }
  2630. /**
  2631. * Return the interactions as an array for the given lp_iv_id.
  2632. * This method can be used as static.
  2633. * @param integer Learnpath Item View ID
  2634. * @return array
  2635. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2636. */
  2637. public static function get_iv_interactions_array($lp_iv_id)
  2638. {
  2639. $course_id = api_get_course_int_id();
  2640. $list = array();
  2641. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2642. if (empty($lp_iv_id)) {
  2643. return array();
  2644. }
  2645. $sql = "SELECT * FROM $table
  2646. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2647. ORDER BY order_id ASC";
  2648. $res = Database::query($sql);
  2649. $num = Database :: num_rows($res);
  2650. if ($num > 0) {
  2651. $list[] = array (
  2652. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2653. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2654. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2655. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2656. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2657. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2658. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2659. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2660. );
  2661. while ($row = Database :: fetch_array($res)) {
  2662. $list[] = array (
  2663. 'order_id' => ($row['order_id'] + 1),
  2664. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2665. 'type' => $row['interaction_type'],
  2666. 'time' => $row['completion_time'],
  2667. //'correct_responses' => $row['correct_responses'],
  2668. 'correct_responses' => '', // Hide correct responses from students.
  2669. 'student_response' => $row['student_response'],
  2670. 'result' => $row['result'],
  2671. 'latency' => $row['latency']
  2672. );
  2673. }
  2674. }
  2675. return $list;
  2676. }
  2677. /**
  2678. * Return the number of objectives for the given learnpath Item View ID.
  2679. * This method can be used as static.
  2680. * @param integer Item View ID
  2681. * @return integer Number of objectives
  2682. */
  2683. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2684. {
  2685. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2686. $course_id = intval($course_id);
  2687. $lp_iv_id = intval($lp_iv_id);
  2688. $sql = "SELECT count(*) FROM $table
  2689. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2690. //@todo seems that this always returns 0
  2691. $res = Database::query($sql);
  2692. $num = 0;
  2693. if (Database::num_rows($res)) {
  2694. $row = Database :: fetch_array($res);
  2695. $num = $row[0];
  2696. }
  2697. return $num;
  2698. }
  2699. /**
  2700. * Return the objectives as an array for the given lp_iv_id.
  2701. * This method can be used as static.
  2702. * @param integer Learnpath Item View ID
  2703. * @return array
  2704. * @todo Translate labels
  2705. */
  2706. public static function get_iv_objectives_array($lp_iv_id = 0)
  2707. {
  2708. $course_id = api_get_course_int_id();
  2709. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2710. $sql = "SELECT * FROM $table
  2711. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2712. ORDER BY order_id ASC";
  2713. $res = Database::query($sql);
  2714. $num = Database :: num_rows($res);
  2715. $list = array();
  2716. if ($num > 0) {
  2717. $list[] = array(
  2718. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2719. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2720. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2721. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2722. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2723. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2724. );
  2725. while ($row = Database :: fetch_array($res)) {
  2726. $list[] = array (
  2727. 'order_id' => ($row['order_id'] + 1),
  2728. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2729. 'score_raw' => $row['score_raw'],
  2730. 'score_max' => $row['score_max'],
  2731. 'score_min' => $row['score_min'],
  2732. 'status' => $row['status']
  2733. );
  2734. }
  2735. }
  2736. return $list;
  2737. }
  2738. /**
  2739. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2740. * used by get_html_toc() to be ready to display
  2741. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2742. */
  2743. public function get_toc()
  2744. {
  2745. if ($this->debug > 0) {
  2746. error_log('learnpath::get_toc()', 0);
  2747. }
  2748. $toc = array();
  2749. foreach ($this->ordered_items as $item_id) {
  2750. if ($this->debug > 2) {
  2751. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2752. }
  2753. // TODO: Change this link generation and use new function instead.
  2754. $toc[] = array (
  2755. 'id' => $item_id,
  2756. 'title' => $this->items[$item_id]->get_title(),
  2757. 'status' => $this->items[$item_id]->get_status(),
  2758. 'level' => $this->items[$item_id]->get_level(),
  2759. 'type' => $this->items[$item_id]->get_type(),
  2760. 'description' => $this->items[$item_id]->get_description(),
  2761. 'path' => $this->items[$item_id]->get_path(),
  2762. );
  2763. }
  2764. if ($this->debug > 2) {
  2765. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2766. }
  2767. return $toc;
  2768. }
  2769. /**
  2770. * Generate and return the table of contents for this learnpath. The JS
  2771. * table returned is used inside of scorm_api.php
  2772. * @return string A JS array vairiable construction
  2773. */
  2774. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2775. {
  2776. if ($this->debug > 0) {
  2777. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2778. }
  2779. $toc = $varname.' = new Array();';
  2780. foreach ($this->ordered_items as $item_id) {
  2781. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2782. }
  2783. if ($this->debug > 2) {
  2784. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2785. }
  2786. return $toc;
  2787. }
  2788. /**
  2789. * Gets the learning path type
  2790. * @param boolean Return the name? If false, return the ID. Default is false.
  2791. * @return mixed Type ID or name, depending on the parameter
  2792. */
  2793. public function get_type($get_name = false)
  2794. {
  2795. $res = false;
  2796. if ($this->debug > 0) {
  2797. error_log('New LP - In learnpath::get_type()', 0);
  2798. }
  2799. if (!empty ($this->type)) {
  2800. if ($get_name) {
  2801. // Get it from the lp_type table in main db.
  2802. } else {
  2803. $res = $this->type;
  2804. }
  2805. }
  2806. if ($this->debug > 2) {
  2807. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2808. }
  2809. return $res;
  2810. }
  2811. /**
  2812. * Gets the learning path type as static method
  2813. * @param boolean Return the name? If false, return the ID. Default is false.
  2814. * @return mixed Type ID or name, depending on the parameter
  2815. */
  2816. public static function get_type_static($lp_id = 0)
  2817. {
  2818. $course_id = api_get_course_int_id();
  2819. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2820. $lp_id = intval($lp_id);
  2821. $sql = "SELECT lp_type FROM $tbl_lp
  2822. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2823. $res = Database::query($sql);
  2824. if ($res === false) {
  2825. return null;
  2826. }
  2827. if (Database :: num_rows($res) <= 0) {
  2828. return null;
  2829. }
  2830. $row = Database :: fetch_array($res);
  2831. return $row['lp_type'];
  2832. }
  2833. /**
  2834. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2835. * This method can be used as abstract and is recursive
  2836. * @param integer Learnpath ID
  2837. * @param integer Parent ID of the items to look for
  2838. * @return mixed Ordered list of item IDs or false on error
  2839. */
  2840. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2841. {
  2842. if (empty($course_id)) {
  2843. $course_id = api_get_course_int_id();
  2844. } else {
  2845. $course_id = intval($course_id);
  2846. }
  2847. $list = array();
  2848. if (empty($lp)) {
  2849. return false;
  2850. }
  2851. $lp = intval($lp);
  2852. $parent = intval($parent);
  2853. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2854. $sql = "SELECT id FROM $tbl_lp_item
  2855. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2856. ORDER BY display_order";
  2857. $res = Database::query($sql);
  2858. while ($row = Database :: fetch_array($res)) {
  2859. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2860. $list[] = $row['id'];
  2861. foreach ($sublist as $item) {
  2862. $list[] = $item;
  2863. }
  2864. }
  2865. return $list;
  2866. }
  2867. /**
  2868. * @return array
  2869. */
  2870. public static function getChapterTypes()
  2871. {
  2872. return array(
  2873. 'dokeos_chapter',
  2874. 'dokeos_module',
  2875. 'chapter',
  2876. 'dir'
  2877. );
  2878. }
  2879. /**
  2880. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2881. * @return string HTML TOC ready to display
  2882. */
  2883. public function get_html_toc($toc_list = null)
  2884. {
  2885. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2886. if ($this->debug > 0) {
  2887. error_log('In learnpath::get_html_toc()', 0);
  2888. }
  2889. if (empty($toc_list)) {
  2890. $toc_list = $this->get_toc();
  2891. }
  2892. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2893. $html = '<div class="scorm-body">';
  2894. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2895. require_once 'resourcelinker.inc.php';
  2896. // Temporary variables.
  2897. $mycurrentitemid = $this->get_current_item_id();
  2898. $color_counter = 0;
  2899. $i = 0;
  2900. foreach ($toc_list as $item) {
  2901. // TODO: Complete this
  2902. $icon_name = array (
  2903. 'not attempted' => '../img/notattempted.gif',
  2904. 'incomplete' => '../img/incomplete.png',
  2905. 'failed' => '../img/delete.png',
  2906. 'completed' => '../img/completed.png',
  2907. 'passed' => '../img/passed.png',
  2908. 'succeeded' => '../img/succeeded.png',
  2909. 'browsed' => '../img/completed.png',
  2910. );
  2911. // Style Status
  2912. $class_name = array (
  2913. 'not attempted' => 'scorm_not_attempted',
  2914. 'incomplete' => 'scorm_not_attempted',
  2915. 'failed' => 'scorm_failed',
  2916. 'completed' => 'scorm_completed',
  2917. 'passed' => 'scorm_completed',
  2918. 'succeeded' => 'scorm_completed',
  2919. 'browsed' => 'scorm_completed',
  2920. );
  2921. $scorm_color_background = 'row_odd';
  2922. $style_item = '';
  2923. if ($color_counter % 2 == 0) {
  2924. $scorm_color_background = 'row_even';
  2925. }
  2926. $dirTypes = self::getChapterTypes();
  2927. if (in_array($item['type'], $dirTypes)) {
  2928. $scorm_color_background ='scorm_item_section ';
  2929. $style_item = '';
  2930. }
  2931. if ($item['id'] == $this->current) {
  2932. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2933. } elseif (!in_array($item['type'], $dirTypes)) {
  2934. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2935. }
  2936. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2937. // Learning path title
  2938. $title = $item['title'];
  2939. if (empty ($title)) {
  2940. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2941. }
  2942. $title = Security::remove_XSS($title);
  2943. // Learning path personalization
  2944. // build the LP tree
  2945. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2946. $description = $item['description'];
  2947. if (empty($description)) {
  2948. $description = $title;
  2949. }
  2950. if (in_array($item['type'], $dirTypes)) {
  2951. // Chapters
  2952. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2953. } else {
  2954. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2955. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2956. }
  2957. if (in_array($item['type'], $dirTypes)) {
  2958. // Chapter
  2959. // if you want to put an image before, you should use css
  2960. $html .= stripslashes($title);
  2961. } else {
  2962. $this->get_link('http', $item['id'], $toc_list);
  2963. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2964. }
  2965. $html .= "</div>";
  2966. if ($scorm_color_background != '') {
  2967. $html .= '</div>';
  2968. }
  2969. $color_counter++;
  2970. }
  2971. $html .= "</div>";
  2972. $html .= "</div>";
  2973. return $html;
  2974. }
  2975. /**
  2976. * Returns an HTML-formatted string ready to display with teacher buttons
  2977. * in LP view menu
  2978. * @return string HTML TOC ready to display
  2979. */
  2980. public function get_teacher_toc_buttons()
  2981. {
  2982. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2983. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2984. $html = '';
  2985. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2986. $gradebook = '';
  2987. if (!empty($_GET['gradebook'])) {
  2988. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2989. }
  2990. if ($this->get_lp_session_id() == api_get_session_id()) {
  2991. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2992. $html .= '<div class="btn-group">';
  2993. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" .
  2994. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2995. $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 . "' target='_parent'>" .
  2996. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2997. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'">' .
  2998. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2999. $html .= '</div>';
  3000. $html .= '</div>';
  3001. }
  3002. }
  3003. return $html;
  3004. }
  3005. /**
  3006. * Gets the learnpath maker name - generally the editor's name
  3007. * @return string Learnpath maker name
  3008. */
  3009. public function get_maker()
  3010. {
  3011. if ($this->debug > 0) {
  3012. error_log('New LP - In learnpath::get_maker()', 0);
  3013. }
  3014. if (!empty ($this->maker)) {
  3015. return $this->maker;
  3016. } else {
  3017. return '';
  3018. }
  3019. }
  3020. /**
  3021. * Gets the learnpath name/title
  3022. * @return string Learnpath name/title
  3023. */
  3024. public function get_name()
  3025. {
  3026. if ($this->debug > 0) {
  3027. error_log('New LP - In learnpath::get_name()', 0);
  3028. }
  3029. if (!empty ($this->name)) {
  3030. return $this->name;
  3031. } else {
  3032. return 'N/A';
  3033. }
  3034. }
  3035. /**
  3036. * Gets a link to the resource from the present location, depending on item ID.
  3037. * @param string $type Type of link expected
  3038. * @param integer $item_id Learnpath item ID
  3039. * @return string $provided_toc Link to the lp_item resource
  3040. */
  3041. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3042. {
  3043. $course_id = $this->get_course_int_id();
  3044. if ($this->debug > 0) {
  3045. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3046. }
  3047. if (empty($item_id)) {
  3048. if ($this->debug > 2) {
  3049. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3050. }
  3051. $item_id = $this->get_current_item_id();
  3052. }
  3053. if (empty($item_id)) {
  3054. if ($this->debug > 2) {
  3055. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3056. }
  3057. //still empty, this means there was no item_id given and we are not in an object context or
  3058. //the object property is empty, return empty link
  3059. $item_id = $this->first();
  3060. return '';
  3061. }
  3062. $file = '';
  3063. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3064. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3065. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3066. $item_id = intval($item_id);
  3067. $sql = "SELECT
  3068. l.lp_type as ltype,
  3069. l.path as lpath,
  3070. li.item_type as litype,
  3071. li.path as lipath,
  3072. li.parameters as liparams
  3073. FROM $lp_table l
  3074. INNER JOIN $lp_item_table li
  3075. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3076. WHERE li.id = $item_id ";
  3077. if ($this->debug > 2) {
  3078. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3079. }
  3080. $res = Database::query($sql);
  3081. if (Database :: num_rows($res) > 0) {
  3082. $row = Database :: fetch_array($res);
  3083. $lp_type = $row['ltype'];
  3084. $lp_path = $row['lpath'];
  3085. $lp_item_type = $row['litype'];
  3086. $lp_item_path = $row['lipath'];
  3087. $lp_item_params = $row['liparams'];
  3088. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3089. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3090. }
  3091. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3092. if ($type == 'http') {
  3093. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3094. } else {
  3095. $course_path = $sys_course_path; //system path
  3096. }
  3097. // 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.
  3098. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3099. $lp_type = 1;
  3100. }
  3101. if ($this->debug > 2) {
  3102. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3103. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3104. }
  3105. // Now go through the specific cases to get the end of the path
  3106. // @todo Use constants instead of int values.
  3107. switch ($lp_type) {
  3108. case 1 :
  3109. if ($lp_item_type == 'dokeos_chapter') {
  3110. $file = 'lp_content.php?type=dir';
  3111. } else {
  3112. require_once 'resourcelinker.inc.php';
  3113. $file = rl_get_resource_link_for_learnpath(
  3114. $course_id,
  3115. $this->get_id(),
  3116. $item_id,
  3117. $this->get_view_id()
  3118. );
  3119. if ($this->debug > 0) {
  3120. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3121. }
  3122. if ($lp_item_type == 'link') {
  3123. if (Link::is_youtube_link($file)) {
  3124. $src = Link::get_youtube_video_id($file);
  3125. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3126. } elseif (Link::isVimeoLink($file)) {
  3127. $src = Link::getVimeoLinkId($file);
  3128. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3129. } else {
  3130. // If the current site is HTTPS and the link is
  3131. // HTTP, browsers will refuse opening the link
  3132. $urlId = api_get_current_access_url_id();
  3133. $url = api_get_access_url($urlId, false);
  3134. $protocol = substr($url['url'], 0, 5);
  3135. if ($protocol === 'https') {
  3136. $linkProtocol = substr($file, 0, 5);
  3137. if ($linkProtocol === 'http:') {
  3138. //this is the special intervention case
  3139. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&source=' . urlencode($file);
  3140. }
  3141. }
  3142. }
  3143. } else {
  3144. // Check how much attempts of a exercise exits in lp
  3145. $lp_item_id = $this->get_current_item_id();
  3146. $lp_view_id = $this->get_view_id();
  3147. $prevent_reinit = null;
  3148. if (isset($this->items[$this->current])) {
  3149. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3150. }
  3151. if (empty($provided_toc)) {
  3152. if ($this->debug > 0) {
  3153. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3154. }
  3155. $list = $this->get_toc();
  3156. } else {
  3157. if ($this->debug > 0) {
  3158. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3159. }
  3160. $list = $provided_toc;
  3161. }
  3162. $type_quiz = false;
  3163. foreach ($list as $toc) {
  3164. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3165. $type_quiz = true;
  3166. }
  3167. }
  3168. if ($type_quiz) {
  3169. $lp_item_id = intval($lp_item_id);
  3170. $lp_view_id = intval($lp_view_id);
  3171. $sql = "SELECT count(*) FROM $lp_item_view_table
  3172. WHERE
  3173. c_id = $course_id AND
  3174. lp_item_id='" . $lp_item_id . "' AND
  3175. lp_view_id ='" . $lp_view_id . "' AND
  3176. status='completed'";
  3177. $result = Database::query($sql);
  3178. $row_count = Database :: fetch_row($result);
  3179. $count_item_view = (int) $row_count[0];
  3180. $not_multiple_attempt = 0;
  3181. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3182. $not_multiple_attempt = 1;
  3183. }
  3184. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3185. }
  3186. $tmp_array = explode('/', $file);
  3187. $document_name = $tmp_array[count($tmp_array) - 1];
  3188. if (strpos($document_name, '_DELETED_')) {
  3189. $file = 'blank.php?error=document_deleted';
  3190. }
  3191. }
  3192. }
  3193. break;
  3194. case 2 :
  3195. if ($this->debug > 2) {
  3196. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3197. }
  3198. if ($lp_item_type != 'dir') {
  3199. // Quite complex here:
  3200. // We want to make sure 'http://' (and similar) links can
  3201. // be loaded as is (withouth the Chamilo path in front) but
  3202. // some contents use this form: resource.htm?resource=http://blablabla
  3203. // which means we have to find a protocol at the path's start, otherwise
  3204. // it should not be considered as an external URL.
  3205. //if ($this->prerequisites_match($item_id)) {
  3206. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3207. if ($this->debug > 2) {
  3208. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3209. }
  3210. // Distant url, return as is.
  3211. $file = $lp_item_path;
  3212. } else {
  3213. if ($this->debug > 2) {
  3214. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3215. }
  3216. // Prevent getting untranslatable urls.
  3217. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3218. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3219. // Prepare the path.
  3220. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3221. // TODO: Fix this for urls with protocol header.
  3222. $file = str_replace('//', '/', $file);
  3223. $file = str_replace(':/', '://', $file);
  3224. if (substr($lp_path, -1) == '/') {
  3225. $lp_path = substr($lp_path, 0, -1);
  3226. }
  3227. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3228. // if file not found.
  3229. $decoded = html_entity_decode($lp_item_path);
  3230. list ($decoded) = explode('?', $decoded);
  3231. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3232. require_once 'resourcelinker.inc.php';
  3233. $file = rl_get_resource_link_for_learnpath(
  3234. $course_id,
  3235. $this->get_id(),
  3236. $item_id,
  3237. $this->get_view_id()
  3238. );
  3239. if (empty($file)) {
  3240. $file = 'blank.php?error=document_not_found';
  3241. } else {
  3242. $tmp_array = explode('/', $file);
  3243. $document_name = $tmp_array[count($tmp_array) - 1];
  3244. if (strpos($document_name, '_DELETED_')) {
  3245. $file = 'blank.php?error=document_deleted';
  3246. } else {
  3247. $file = 'blank.php?error=document_not_found';
  3248. }
  3249. }
  3250. } else {
  3251. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3252. }
  3253. }
  3254. }
  3255. // We want to use parameters if they were defined in the imsmanifest
  3256. if (strpos($file, 'blank.php') === false) {
  3257. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3258. }
  3259. } else {
  3260. $file = 'lp_content.php?type=dir';
  3261. }
  3262. break;
  3263. case 3 :
  3264. if ($this->debug > 2) {
  3265. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3266. }
  3267. // Formatting AICC HACP append URL.
  3268. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3269. if (!empty($lp_item_params)) {
  3270. $aicc_append .= $lp_item_params . '&';
  3271. }
  3272. if ($lp_item_type != 'dir') {
  3273. // Quite complex here:
  3274. // We want to make sure 'http://' (and similar) links can
  3275. // be loaded as is (withouth the Chamilo path in front) but
  3276. // some contents use this form: resource.htm?resource=http://blablabla
  3277. // which means we have to find a protocol at the path's start, otherwise
  3278. // it should not be considered as an external URL.
  3279. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3280. if ($this->debug > 2) {
  3281. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3282. }
  3283. // Distant url, return as is.
  3284. $file = $lp_item_path;
  3285. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3286. /*
  3287. if (stristr($file,'<servername>') !== false) {
  3288. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3289. }
  3290. */
  3291. if (stripos($file, '<servername>') !== false) {
  3292. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3293. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3294. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3295. }
  3296. //
  3297. $file .= $aicc_append;
  3298. } else {
  3299. if ($this->debug > 2) {
  3300. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3301. }
  3302. // Prevent getting untranslatable urls.
  3303. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3304. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3305. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3306. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3307. // TODO: Fix this for urls with protocol header.
  3308. $file = str_replace('//', '/', $file);
  3309. $file = str_replace(':/', '://', $file);
  3310. $file .= $aicc_append;
  3311. }
  3312. } else {
  3313. $file = 'lp_content.php?type=dir';
  3314. }
  3315. break;
  3316. case 4 :
  3317. break;
  3318. default :
  3319. break;
  3320. }
  3321. // Replace &amp; by & because &amp; will break URL with params
  3322. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3323. }
  3324. if ($this->debug > 2) {
  3325. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3326. }
  3327. return $file;
  3328. }
  3329. /**
  3330. * Gets the latest usable view or generate a new one
  3331. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3332. * @return integer DB lp_view id
  3333. */
  3334. public function get_view($attempt_num = 0)
  3335. {
  3336. if ($this->debug > 0) {
  3337. error_log('New LP - In learnpath::get_view()', 0);
  3338. }
  3339. $search = '';
  3340. // Use $attempt_num to enable multi-views management (disabled so far).
  3341. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3342. $search = 'AND view_count = ' . $attempt_num;
  3343. }
  3344. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3345. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3346. $course_id = api_get_course_int_id();
  3347. $sessionId = api_get_session_id();
  3348. $sql = "SELECT id, view_count FROM $lp_view_table
  3349. WHERE
  3350. c_id = " . $course_id . " AND
  3351. lp_id = " . $this->get_id() . " AND
  3352. user_id = " . $this->get_user_id() . " AND
  3353. session_id = $sessionId
  3354. $search
  3355. ORDER BY view_count DESC";
  3356. $res = Database::query($sql);
  3357. if (Database :: num_rows($res) > 0) {
  3358. $row = Database :: fetch_array($res);
  3359. $this->lp_view_id = $row['id'];
  3360. } else if (!api_is_invitee()) {
  3361. // There is no database record, create one.
  3362. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3363. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3364. Database::query($sql);
  3365. $id = Database :: insert_id();
  3366. $this->lp_view_id = $id;
  3367. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3368. Database::query($sql);
  3369. }
  3370. return $this->lp_view_id;
  3371. }
  3372. /**
  3373. * Gets the current view id
  3374. * @return integer View ID (from lp_view)
  3375. */
  3376. public function get_view_id()
  3377. {
  3378. if ($this->debug > 0) {
  3379. error_log('New LP - In learnpath::get_view_id()', 0);
  3380. }
  3381. if (!empty ($this->lp_view_id)) {
  3382. return $this->lp_view_id;
  3383. } else {
  3384. return 0;
  3385. }
  3386. }
  3387. /**
  3388. * Gets the update queue
  3389. * @return array Array containing IDs of items to be updated by JavaScript
  3390. */
  3391. public function get_update_queue()
  3392. {
  3393. if ($this->debug > 0) {
  3394. error_log('New LP - In learnpath::get_update_queue()', 0);
  3395. }
  3396. return $this->update_queue;
  3397. }
  3398. /**
  3399. * Gets the user ID
  3400. * @return integer User ID
  3401. */
  3402. public function get_user_id()
  3403. {
  3404. if ($this->debug > 0) {
  3405. error_log('New LP - In learnpath::get_user_id()', 0);
  3406. }
  3407. if (!empty ($this->user_id)) {
  3408. return $this->user_id;
  3409. } else {
  3410. return false;
  3411. }
  3412. }
  3413. /**
  3414. * Checks if any of the items has an audio element attached
  3415. * @return bool True or false
  3416. */
  3417. public function has_audio()
  3418. {
  3419. if ($this->debug > 1) {
  3420. error_log('New LP - In learnpath::has_audio()', 0);
  3421. }
  3422. $has = false;
  3423. foreach ($this->items as $i => $item) {
  3424. if (!empty ($this->items[$i]->audio)) {
  3425. $has = true;
  3426. break;
  3427. }
  3428. }
  3429. return $has;
  3430. }
  3431. /**
  3432. * Logs a message into a file
  3433. * @param string Message to log
  3434. * @return boolean True on success, false on error or if msg empty
  3435. */
  3436. public function log($msg)
  3437. {
  3438. if ($this->debug > 0) {
  3439. error_log('New LP - In learnpath::log()', 0);
  3440. }
  3441. // TODO
  3442. $this->error .= $msg;
  3443. return true;
  3444. }
  3445. /**
  3446. * Moves an item up and down at its level
  3447. * @param integer Item to move up and down
  3448. * @param string Direction 'up' or 'down'
  3449. * @return integer New display order, or false on error
  3450. */
  3451. public function move_item($id, $direction)
  3452. {
  3453. $course_id = api_get_course_int_id();
  3454. if ($this->debug > 0) {
  3455. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3456. }
  3457. if (empty($id) || empty($direction)) {
  3458. return false;
  3459. }
  3460. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3461. $sql_sel = "SELECT *
  3462. FROM " . $tbl_lp_item . "
  3463. WHERE c_id = ".$course_id." AND id = " . $id;
  3464. $res_sel = Database::query($sql_sel);
  3465. // Check if elem exists.
  3466. if (Database :: num_rows($res_sel) < 1) {
  3467. return false;
  3468. }
  3469. // Gather data.
  3470. $row = Database :: fetch_array($res_sel);
  3471. $previous = $row['previous_item_id'];
  3472. $next = $row['next_item_id'];
  3473. $display = $row['display_order'];
  3474. $parent = $row['parent_item_id'];
  3475. $lp = $row['lp_id'];
  3476. // Update the item (switch with previous/next one).
  3477. switch ($direction) {
  3478. case 'up':
  3479. if ($this->debug > 2) {
  3480. error_log('Movement up detected', 0);
  3481. }
  3482. if ($display <= 1) { /*do nothing*/
  3483. } else {
  3484. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3485. WHERE c_id = ".$course_id." AND id = $previous";
  3486. if ($this->debug > 2) {
  3487. error_log('Selecting previous: ' . $sql_sel2, 0);
  3488. }
  3489. $res_sel2 = Database::query($sql_sel2);
  3490. if (Database :: num_rows($res_sel2) < 1) {
  3491. $previous_previous = 0;
  3492. }
  3493. // Gather data.
  3494. $row2 = Database :: fetch_array($res_sel2);
  3495. $previous_previous = $row2['previous_item_id'];
  3496. // Update previous_previous item (switch "next" with current).
  3497. if ($previous_previous != 0) {
  3498. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3499. next_item_id = $id
  3500. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3501. if ($this->debug > 2) {
  3502. error_log($sql_upd2, 0);
  3503. }
  3504. Database::query($sql_upd2);
  3505. }
  3506. // Update previous item (switch with current).
  3507. if ($previous != 0) {
  3508. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3509. next_item_id = $next,
  3510. previous_item_id = $id,
  3511. display_order = display_order +1
  3512. WHERE c_id = ".$course_id." AND id = $previous";
  3513. if ($this->debug > 2) {
  3514. error_log($sql_upd2, 0);
  3515. }
  3516. Database::query($sql_upd2);
  3517. }
  3518. // Update current item (switch with previous).
  3519. if ($id != 0) {
  3520. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3521. next_item_id = $previous,
  3522. previous_item_id = $previous_previous,
  3523. display_order = display_order-1
  3524. WHERE c_id = ".$course_id." AND id = $id";
  3525. if ($this->debug > 2) {
  3526. error_log($sql_upd2, 0);
  3527. }
  3528. Database::query($sql_upd2);
  3529. }
  3530. // Update next item (new previous item).
  3531. if ($next != 0) {
  3532. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3533. WHERE c_id = ".$course_id." AND id = $next";
  3534. if ($this->debug > 2) {
  3535. error_log($sql_upd2, 0);
  3536. }
  3537. Database::query($sql_upd2);
  3538. }
  3539. $display = $display -1;
  3540. }
  3541. break;
  3542. case 'down':
  3543. if ($this->debug > 2) {
  3544. error_log('Movement down detected', 0);
  3545. }
  3546. if ($next == 0) { /* Do nothing. */
  3547. } else {
  3548. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3549. if ($this->debug > 2) {
  3550. error_log('Selecting next: ' . $sql_sel2, 0);
  3551. }
  3552. $res_sel2 = Database::query($sql_sel2);
  3553. if (Database :: num_rows($res_sel2) < 1) {
  3554. $next_next = 0;
  3555. }
  3556. // Gather data.
  3557. $row2 = Database :: fetch_array($res_sel2);
  3558. $next_next = $row2['next_item_id'];
  3559. // Update previous item (switch with current).
  3560. if ($previous != 0) {
  3561. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3562. WHERE c_id = ".$course_id." AND id = $previous";
  3563. Database::query($sql_upd2);
  3564. }
  3565. // Update current item (switch with previous).
  3566. if ($id != 0) {
  3567. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3568. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3569. WHERE c_id = ".$course_id." AND id = $id";
  3570. Database::query($sql_upd2);
  3571. }
  3572. // Update next item (new previous item).
  3573. if ($next != 0) {
  3574. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3575. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3576. WHERE c_id = ".$course_id." AND id = $next";
  3577. Database::query($sql_upd2);
  3578. }
  3579. // Update next_next item (switch "previous" with current).
  3580. if ($next_next != 0) {
  3581. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3582. previous_item_id = $id
  3583. WHERE c_id = ".$course_id." AND id = $next_next";
  3584. Database::query($sql_upd2);
  3585. }
  3586. $display = $display +1;
  3587. }
  3588. break;
  3589. default :
  3590. return false;
  3591. }
  3592. return $display;
  3593. }
  3594. /**
  3595. * Move a learnpath up (display_order)
  3596. * @param integer $lp_id Learnpath ID
  3597. */
  3598. public static function move_up($lp_id)
  3599. {
  3600. $course_id = api_get_course_int_id();
  3601. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3602. $sql = "SELECT * FROM $lp_table
  3603. WHERE c_id = ".$course_id."
  3604. ORDER BY display_order";
  3605. $res = Database::query($sql);
  3606. if ($res === false)
  3607. return false;
  3608. $lps = array ();
  3609. $lp_order = array ();
  3610. $num = Database :: num_rows($res);
  3611. // First check the order is correct, globally (might be wrong because
  3612. // of versions < 1.8.4)
  3613. if ($num > 0) {
  3614. $i = 1;
  3615. while ($row = Database :: fetch_array($res)) {
  3616. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3617. $need_fix = true;
  3618. $sql_u = "UPDATE $lp_table SET display_order = $i
  3619. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3620. Database::query($sql_u);
  3621. }
  3622. $row['display_order'] = $i;
  3623. $lps[$row['id']] = $row;
  3624. $lp_order[$i] = $row['id'];
  3625. $i++;
  3626. }
  3627. }
  3628. if ($num > 1) { // If there's only one element, no need to sort.
  3629. $order = $lps[$lp_id]['display_order'];
  3630. if ($order > 1) { // If it's the first element, no need to move up.
  3631. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3632. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3633. Database::query($sql_u1);
  3634. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3635. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3636. Database::query($sql_u2);
  3637. }
  3638. }
  3639. }
  3640. /**
  3641. * Move a learnpath down (display_order)
  3642. * @param integer $lp_id Learnpath ID
  3643. */
  3644. public static function move_down($lp_id)
  3645. {
  3646. $course_id = api_get_course_int_id();
  3647. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3648. $sql = "SELECT * FROM $lp_table
  3649. WHERE c_id = ".$course_id."
  3650. ORDER BY display_order";
  3651. $res = Database::query($sql);
  3652. if ($res === false) {
  3653. return false;
  3654. }
  3655. $lps = array ();
  3656. $lp_order = array ();
  3657. $num = Database :: num_rows($res);
  3658. $max = 0;
  3659. // First check the order is correct, globally (might be wrong because
  3660. // of versions < 1.8.4).
  3661. if ($num > 0) {
  3662. $i = 1;
  3663. while ($row = Database :: fetch_array($res)) {
  3664. $max = $i;
  3665. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3666. $need_fix = true;
  3667. $sql_u = "UPDATE $lp_table SET display_order = $i
  3668. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3669. Database::query($sql_u);
  3670. }
  3671. $row['display_order'] = $i;
  3672. $lps[$row['id']] = $row;
  3673. $lp_order[$i] = $row['id'];
  3674. $i++;
  3675. }
  3676. }
  3677. if ($num > 1) { // If there's only one element, no need to sort.
  3678. $order = $lps[$lp_id]['display_order'];
  3679. if ($order < $max) { // If it's the first element, no need to move up.
  3680. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3681. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3682. Database::query($sql_u1);
  3683. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3684. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3685. Database::query($sql_u2);
  3686. }
  3687. }
  3688. }
  3689. /**
  3690. * Updates learnpath attributes to point to the next element
  3691. * The last part is similar to set_current_item but processing the other way around
  3692. */
  3693. public function next()
  3694. {
  3695. if ($this->debug > 0) {
  3696. error_log('New LP - In learnpath::next()', 0);
  3697. }
  3698. $this->last = $this->get_current_item_id();
  3699. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3700. $this->autocomplete_parents($this->last);
  3701. $new_index = $this->get_next_index();
  3702. if ($this->debug > 2) {
  3703. error_log('New LP - New index: ' . $new_index, 0);
  3704. }
  3705. $this->index = $new_index;
  3706. if ($this->debug > 2) {
  3707. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3708. }
  3709. $this->current = $this->ordered_items[$new_index];
  3710. if ($this->debug > 2) {
  3711. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3712. }
  3713. }
  3714. /**
  3715. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3716. * class, this might be redefined to allow several behaviours depending on the document type.
  3717. * @param integer Resource ID
  3718. * @return boolean True on success, false otherwise
  3719. */
  3720. public function open($id)
  3721. {
  3722. if ($this->debug > 0) {
  3723. error_log('New LP - In learnpath::open()', 0);
  3724. }
  3725. // TODO:
  3726. // set the current resource attribute to this resource
  3727. // switch on element type (redefine in child class?)
  3728. // set status for this item to "opened"
  3729. // start timer
  3730. // initialise score
  3731. $this->index = 0; //or = the last item seen (see $this->last)
  3732. }
  3733. /**
  3734. * Check that all prerequisites are fulfilled. Returns true and an
  3735. * empty string on succes, returns false
  3736. * and the prerequisite string on error.
  3737. * This function is based on the rules for aicc_script language as
  3738. * described in the SCORM 1.2 CAM documentation page 108.
  3739. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3740. * @return boolean True if prerequisites are matched, false otherwise -
  3741. * Empty string if true returned, prerequisites string otherwise.
  3742. */
  3743. public function prerequisites_match($itemId = null)
  3744. {
  3745. $debug = $this->debug;
  3746. if ($debug > 0) {
  3747. error_log('In learnpath::prerequisites_match()', 0);
  3748. }
  3749. if (empty($itemId)) {
  3750. $itemId = $this->current;
  3751. }
  3752. $currentItem = $this->getItem($itemId);
  3753. if ($currentItem) {
  3754. if ($this->type == 2) {
  3755. // Getting prereq from scorm
  3756. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3757. } else {
  3758. $prereq_string = $currentItem->get_prereq_string();
  3759. }
  3760. if (empty($prereq_string)) {
  3761. if ($debug > 0) {
  3762. error_log('Found prereq_string is empty return true');
  3763. }
  3764. return true;
  3765. }
  3766. // Clean spaces.
  3767. $prereq_string = str_replace(' ', '', $prereq_string);
  3768. if ($debug > 0) {
  3769. error_log('Found prereq_string: ' . $prereq_string, 0);
  3770. }
  3771. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3772. $result = $currentItem->parse_prereq(
  3773. $prereq_string,
  3774. $this->items,
  3775. $this->refs_list,
  3776. $this->get_user_id()
  3777. );
  3778. if ($result === false) {
  3779. $this->set_error_msg($currentItem->prereq_alert);
  3780. }
  3781. } else {
  3782. $result = true;
  3783. if ($debug > 1) {
  3784. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3785. }
  3786. }
  3787. if ($debug > 1) {
  3788. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3789. }
  3790. return $result;
  3791. }
  3792. /**
  3793. * Updates learnpath attributes to point to the previous element
  3794. * The last part is similar to set_current_item but processing the other way around
  3795. */
  3796. public function previous()
  3797. {
  3798. if ($this->debug > 0) {
  3799. error_log('New LP - In learnpath::previous()', 0);
  3800. }
  3801. $this->last = $this->get_current_item_id();
  3802. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3803. $this->autocomplete_parents($this->last);
  3804. $new_index = $this->get_previous_index();
  3805. $this->index = $new_index;
  3806. $this->current = $this->ordered_items[$new_index];
  3807. }
  3808. /**
  3809. * Publishes a learnpath. This basically means show or hide the learnpath
  3810. * to normal users.
  3811. * Can be used as abstract
  3812. * @param integer Learnpath ID
  3813. * @param string New visibility
  3814. */
  3815. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3816. {
  3817. $action = 'visible';
  3818. if ($set_visibility != 1) {
  3819. $action = 'invisible';
  3820. self::toggle_publish($lp_id, 'i');
  3821. }
  3822. return api_item_property_update(
  3823. api_get_course_info(),
  3824. TOOL_LEARNPATH,
  3825. $lp_id,
  3826. $action,
  3827. api_get_user_id()
  3828. );
  3829. }
  3830. /**
  3831. * Publishes a learnpath. This basically means show or hide the learnpath
  3832. * on the course homepage
  3833. * Can be used as abstract
  3834. * @param integer $lp_id Learnpath id
  3835. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3836. * @return bool
  3837. */
  3838. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3839. {
  3840. $course_id = api_get_course_int_id();
  3841. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3842. $lp_id = intval($lp_id);
  3843. $sql = "SELECT * FROM $tbl_lp
  3844. WHERE c_id = ".$course_id." AND id = $lp_id";
  3845. $result = Database::query($sql);
  3846. if (Database::num_rows($result)) {
  3847. $row = Database :: fetch_array($result);
  3848. $name = domesticate($row['name']);
  3849. if ($set_visibility == 'i') {
  3850. $s = $name . " " . get_lang('LearnpathNotPublished');
  3851. $dialogBox = $s;
  3852. $v = 0;
  3853. }
  3854. if ($set_visibility == 'v') {
  3855. $s = $name . " " . get_lang('LearnpathPublished');
  3856. $dialogBox = $s;
  3857. $v = 1;
  3858. }
  3859. } else {
  3860. return false;
  3861. }
  3862. $session_id = api_get_session_id();
  3863. $session_condition = api_get_session_condition($session_id);
  3864. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3865. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3866. $sql = "SELECT * FROM $tbl_tool
  3867. WHERE
  3868. c_id = ".$course_id." AND
  3869. link='$link' and
  3870. image='scormbuilder.gif' and
  3871. link LIKE '$link%'
  3872. $session_condition
  3873. ";
  3874. $result = Database::query($sql);
  3875. $num = Database :: num_rows($result);
  3876. if ($set_visibility == 'i' && $num > 0) {
  3877. $sql = "DELETE FROM $tbl_tool
  3878. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3879. Database::query($sql);
  3880. } elseif ($set_visibility == 'v' && $num == 0) {
  3881. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3882. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3883. Database::query($sql);
  3884. $insertId = Database::insert_id();
  3885. if ($insertId) {
  3886. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3887. Database::query($sql);
  3888. }
  3889. } elseif ($set_visibility == 'v' && $num > 0) {
  3890. $sql = "UPDATE $tbl_tool SET
  3891. c_id = $course_id,
  3892. name = '$name',
  3893. link = '$link',
  3894. image = 'scormbuilder.gif',
  3895. visibility = '$v',
  3896. admin = '0',
  3897. address = 'pastillegris.gif',
  3898. added_tool = 0,
  3899. session_id = $session_id
  3900. WHERE
  3901. c_id = ".$course_id." AND
  3902. (link='$link' and image='scormbuilder.gif' $session_condition)
  3903. ";
  3904. Database::query($sql);
  3905. } else {
  3906. // Parameter and database incompatible, do nothing, exit.
  3907. return false;
  3908. }
  3909. }
  3910. /**
  3911. * Restart the whole learnpath. Return the URL of the first element.
  3912. * Make sure the results are saved with anoter method. This method should probably be
  3913. * redefined in children classes.
  3914. * To use a similar method statically, use the create_new_attempt() method
  3915. * @return string URL to load in the viewer
  3916. */
  3917. public function restart()
  3918. {
  3919. if ($this->debug > 0) {
  3920. error_log('New LP - In learnpath::restart()', 0);
  3921. }
  3922. // TODO
  3923. // Call autosave method to save the current progress.
  3924. //$this->index = 0;
  3925. if (api_is_invitee()) {
  3926. return false;
  3927. }
  3928. $session_id = api_get_session_id();
  3929. $course_id = api_get_course_int_id();
  3930. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3931. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3932. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3933. if ($this->debug > 2) {
  3934. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3935. }
  3936. $res = Database::query($sql);
  3937. $view_id = Database::insert_id();
  3938. if ($view_id) {
  3939. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3940. Database::query($sql);
  3941. $this->lp_view_id = $view_id;
  3942. $this->attempt = $this->attempt + 1;
  3943. } else {
  3944. $this->error = 'Could not insert into item_view table...';
  3945. return false;
  3946. }
  3947. $this->autocomplete_parents($this->current);
  3948. foreach ($this->items as $index => $dummy) {
  3949. $this->items[$index]->restart();
  3950. $this->items[$index]->set_lp_view($this->lp_view_id);
  3951. }
  3952. $this->first();
  3953. return true;
  3954. }
  3955. /**
  3956. * Saves the current item
  3957. * @return boolean
  3958. */
  3959. public function save_current()
  3960. {
  3961. if ($this->debug > 0) {
  3962. error_log('learnpath::save_current()', 0);
  3963. }
  3964. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3965. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3966. if ($this->debug > 2) {
  3967. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3968. }
  3969. if ($this->debug > 2) {
  3970. error_log('' . print_r($this->items, true), 0);
  3971. }
  3972. if (isset($this->items[$this->current]) &&
  3973. is_object($this->items[$this->current])
  3974. ) {
  3975. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3976. $this->autocomplete_parents($this->current);
  3977. $status = $this->items[$this->current]->get_status();
  3978. $this->update_queue[$this->current] = $status;
  3979. return $res;
  3980. }
  3981. return false;
  3982. }
  3983. /**
  3984. * Saves the given item
  3985. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3986. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3987. * @return boolean
  3988. */
  3989. public function save_item($item_id = null, $from_outside = true)
  3990. {
  3991. $debug = $this->debug;
  3992. if ($debug) {
  3993. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3994. }
  3995. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3996. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3997. if (empty($item_id)) {
  3998. $item_id = intval($_REQUEST['id']);
  3999. }
  4000. if (empty($item_id)) {
  4001. $item_id = $this->get_current_item_id();
  4002. }
  4003. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4004. if ($debug) {
  4005. error_log('Object exists');
  4006. }
  4007. // Saving the item.
  4008. $res = $this->items[$item_id]->save(
  4009. $from_outside,
  4010. $this->prerequisites_match($item_id)
  4011. );
  4012. if ($debug) {
  4013. error_log('update_queue before:');
  4014. error_log(print_r($this->update_queue,1));
  4015. }
  4016. $this->autocomplete_parents($item_id);
  4017. $status = $this->items[$item_id]->get_status();
  4018. $this->update_queue[$item_id] = $status;
  4019. if ($debug) {
  4020. error_log('get_status(): ' . $status);
  4021. error_log('update_queue after:');
  4022. error_log(print_r($this->update_queue,1));
  4023. }
  4024. return $res;
  4025. }
  4026. return false;
  4027. }
  4028. /**
  4029. * Saves the last item seen's ID only in case
  4030. */
  4031. public function save_last()
  4032. {
  4033. $course_id = api_get_course_int_id();
  4034. if ($this->debug > 0) {
  4035. error_log('New LP - In learnpath::save_last()', 0);
  4036. }
  4037. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4038. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4039. if (isset($this->current) && !api_is_invitee()) {
  4040. if ($this->debug > 2) {
  4041. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4042. }
  4043. $sql = "UPDATE $table SET
  4044. last_item = " . intval($this->get_current_item_id()). "
  4045. WHERE
  4046. c_id = $course_id AND
  4047. lp_id = " . $this->get_id() . " AND
  4048. user_id = " . $this->get_user_id()." ".$session_condition;
  4049. if ($this->debug > 2) {
  4050. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4051. }
  4052. Database::query($sql);
  4053. }
  4054. if (!api_is_invitee()) {
  4055. // Save progress.
  4056. list($progress, $text) = $this->get_progress_bar_text('%');
  4057. if ($progress >= 0 && $progress <= 100) {
  4058. $progress = (int) $progress;
  4059. $sql = "UPDATE $table SET
  4060. progress = $progress
  4061. WHERE
  4062. c_id = ".$course_id." AND
  4063. lp_id = " . $this->get_id() . " AND
  4064. user_id = " . $this->get_user_id()." ".$session_condition;
  4065. // Ignore errors as some tables might not have the progress field just yet.
  4066. Database::query($sql);
  4067. $this->progress_db = $progress;
  4068. }
  4069. }
  4070. }
  4071. /**
  4072. * Sets the current item ID (checks if valid and authorized first)
  4073. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4074. */
  4075. public function set_current_item($item_id = null)
  4076. {
  4077. if ($this->debug > 0) {
  4078. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4079. }
  4080. if (empty ($item_id)) {
  4081. if ($this->debug > 2) {
  4082. error_log('New LP - No new current item given, ignore...', 0);
  4083. }
  4084. // Do nothing.
  4085. } else {
  4086. if ($this->debug > 2) {
  4087. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4088. }
  4089. if (is_numeric($item_id)) {
  4090. $item_id = intval($item_id);
  4091. // TODO: Check in database here.
  4092. $this->last = $this->current;
  4093. $this->current = $item_id;
  4094. // TODO: Update $this->index as well.
  4095. foreach ($this->ordered_items as $index => $item) {
  4096. if ($item == $this->current) {
  4097. $this->index = $index;
  4098. break;
  4099. }
  4100. }
  4101. if ($this->debug > 2) {
  4102. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4103. }
  4104. } else {
  4105. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4106. }
  4107. }
  4108. }
  4109. /**
  4110. * Sets the encoding
  4111. * @param string New encoding
  4112. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4113. */
  4114. public function set_encoding($enc = 'UTF-8')
  4115. {
  4116. if ($this->debug > 0) {
  4117. error_log('New LP - In learnpath::set_encoding()', 0);
  4118. }
  4119. $course_id = api_get_course_int_id();
  4120. $enc = api_refine_encoding_id($enc);
  4121. if (empty($enc)) {
  4122. $enc = api_get_system_encoding();
  4123. }
  4124. if (api_is_encoding_supported($enc)) {
  4125. $lp = $this->get_id();
  4126. if ($lp != 0) {
  4127. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4128. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4129. $res = Database::query($sql);
  4130. return $res;
  4131. }
  4132. }
  4133. return false;
  4134. }
  4135. /**
  4136. * Sets the JS lib setting in the database directly.
  4137. * This is the JavaScript library file this lp needs to load on startup
  4138. * @param string Proximity setting
  4139. * @return boolean True on update success. False otherwise.
  4140. */
  4141. public function set_jslib($lib = '')
  4142. {
  4143. if ($this->debug > 0) {
  4144. error_log('New LP - In learnpath::set_jslib()', 0);
  4145. }
  4146. $lp = $this->get_id();
  4147. $course_id = api_get_course_int_id();
  4148. if ($lp != 0) {
  4149. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4150. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4151. $res = Database::query($sql);
  4152. return $res;
  4153. } else {
  4154. return false;
  4155. }
  4156. }
  4157. /**
  4158. * Sets the name of the LP maker (publisher) (and save)
  4159. * @param string Optional string giving the new content_maker of this learnpath
  4160. * @return boolean True
  4161. */
  4162. public function set_maker($name = '')
  4163. {
  4164. if ($this->debug > 0) {
  4165. error_log('New LP - In learnpath::set_maker()', 0);
  4166. }
  4167. if (empty ($name))
  4168. return false;
  4169. $this->maker = $name;
  4170. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4171. $course_id = api_get_course_int_id();
  4172. $lp_id = $this->get_id();
  4173. $sql = "UPDATE $lp_table SET
  4174. content_maker = '" . Database::escape_string($this->maker) . "'
  4175. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4176. if ($this->debug > 2) {
  4177. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4178. }
  4179. Database::query($sql);
  4180. return true;
  4181. }
  4182. /**
  4183. * Sets the name of the current learnpath (and save)
  4184. * @param string $name Optional string giving the new name of this learnpath
  4185. * @return boolean True/False
  4186. */
  4187. public function set_name($name = null)
  4188. {
  4189. if ($this->debug > 0) {
  4190. error_log('New LP - In learnpath::set_name()', 0);
  4191. }
  4192. if (empty($name)) {
  4193. return false;
  4194. }
  4195. $this->name = Database::escape_string($name);
  4196. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4197. $lp_id = $this->get_id();
  4198. $course_id = $this->course_info['real_id'];
  4199. $sql = "UPDATE $lp_table SET
  4200. name = '" . Database::escape_string($this->name). "'
  4201. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4202. if ($this->debug > 2) {
  4203. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4204. }
  4205. $result = Database::query($sql);
  4206. // If the lp is visible on the homepage, change his name there.
  4207. if (Database::affected_rows($result)) {
  4208. $session_id = api_get_session_id();
  4209. $session_condition = api_get_session_condition($session_id);
  4210. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4211. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4212. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4213. WHERE
  4214. c_id = $course_id AND
  4215. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4216. Database::query($sql);
  4217. return true;
  4218. } else {
  4219. return false;
  4220. }
  4221. }
  4222. /**
  4223. * Set index specified prefix terms for all items in this path
  4224. * @param string Comma-separated list of terms
  4225. * @param char Xapian term prefix
  4226. * @return boolean False on error, true otherwise
  4227. */
  4228. public function set_terms_by_prefix($terms_string, $prefix)
  4229. {
  4230. $course_id = api_get_course_int_id();
  4231. if (api_get_setting('search_enabled') !== 'true')
  4232. return false;
  4233. if (!extension_loaded('xapian')) {
  4234. return false;
  4235. }
  4236. $terms_string = trim($terms_string);
  4237. $terms = explode(',', $terms_string);
  4238. array_walk($terms, 'trim_value');
  4239. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4240. // Don't do anything if no change, verify only at DB, not the search engine.
  4241. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4242. return false;
  4243. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4244. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4245. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4246. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4247. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4248. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4249. $lp_id = intval($_POST['lp_id']);
  4250. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4251. $result = Database::query($sql);
  4252. $di = new ChamiloIndexer();
  4253. while ($lp_item = Database :: fetch_array($result)) {
  4254. // Get search_did.
  4255. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4256. $sql = 'SELECT * FROM %s
  4257. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4258. LIMIT 1';
  4259. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4260. //echo $sql; echo '<br>';
  4261. $res = Database::query($sql);
  4262. if (Database::num_rows($res) > 0) {
  4263. $se_ref = Database :: fetch_array($res);
  4264. // Compare terms.
  4265. $doc = $di->get_document($se_ref['search_did']);
  4266. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4267. $xterms = array();
  4268. foreach ($xapian_terms as $xapian_term) {
  4269. $xterms[] = substr($xapian_term['name'], 1);
  4270. }
  4271. $dterms = $terms;
  4272. $missing_terms = array_diff($dterms, $xterms);
  4273. $deprecated_terms = array_diff($xterms, $dterms);
  4274. // Save it to search engine.
  4275. foreach ($missing_terms as $term) {
  4276. $doc->add_term($prefix . $term, 1);
  4277. }
  4278. foreach ($deprecated_terms as $term) {
  4279. $doc->remove_term($prefix . $term);
  4280. }
  4281. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4282. $di->getDb()->flush();
  4283. } else {
  4284. //@todo What we should do here?
  4285. }
  4286. }
  4287. return true;
  4288. }
  4289. /**
  4290. * Sets the theme of the LP (local/remote) (and save)
  4291. * @param string Optional string giving the new theme of this learnpath
  4292. * @return bool Returns true if theme name is not empty
  4293. */
  4294. public function set_theme($name = '')
  4295. {
  4296. $course_id = api_get_course_int_id();
  4297. if ($this->debug > 0) {
  4298. error_log('New LP - In learnpath::set_theme()', 0);
  4299. }
  4300. $this->theme = $name;
  4301. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4302. $lp_id = $this->get_id();
  4303. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4304. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4305. if ($this->debug > 2) {
  4306. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4307. }
  4308. Database::query($sql);
  4309. return true;
  4310. }
  4311. /**
  4312. * Sets the image of an LP (and save)
  4313. * @param string Optional string giving the new image of this learnpath
  4314. * @return bool Returns true if theme name is not empty
  4315. */
  4316. public function set_preview_image($name = '')
  4317. {
  4318. $course_id = api_get_course_int_id();
  4319. if ($this->debug > 0) {
  4320. error_log('New LP - In learnpath::set_preview_image()', 0);
  4321. }
  4322. $this->preview_image = $name;
  4323. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4324. $lp_id = $this->get_id();
  4325. $sql = "UPDATE $lp_table SET
  4326. preview_image = '" . Database::escape_string($this->preview_image). "'
  4327. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4328. if ($this->debug > 2) {
  4329. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4330. }
  4331. Database::query($sql);
  4332. return true;
  4333. }
  4334. /**
  4335. * Sets the author of a LP (and save)
  4336. * @param string Optional string giving the new author of this learnpath
  4337. * @return bool Returns true if author's name is not empty
  4338. */
  4339. public function set_author($name = '')
  4340. {
  4341. $course_id = api_get_course_int_id();
  4342. if ($this->debug > 0) {
  4343. error_log('New LP - In learnpath::set_author()', 0);
  4344. }
  4345. $this->author = $name;
  4346. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4347. $lp_id = $this->get_id();
  4348. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4349. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4350. if ($this->debug > 2) {
  4351. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4352. }
  4353. Database::query($sql);
  4354. return true;
  4355. }
  4356. /**
  4357. * Sets the hide_toc_frame parameter of a LP (and save)
  4358. * @param int 1 if frame is hidden 0 then else
  4359. * @return bool Returns true if author's name is not empty
  4360. */
  4361. public function set_hide_toc_frame($hide)
  4362. {
  4363. $course_id = api_get_course_int_id();
  4364. if ($this->debug > 0) {
  4365. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4366. }
  4367. if (intval($hide) == $hide){
  4368. $this->hide_toc_frame = $hide;
  4369. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4370. $lp_id = $this->get_id();
  4371. $sql = "UPDATE $lp_table SET
  4372. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4373. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4374. if ($this->debug > 2) {
  4375. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4376. }
  4377. Database::query($sql);
  4378. return true;
  4379. } else {
  4380. return false;
  4381. }
  4382. }
  4383. /**
  4384. * Sets the prerequisite of a LP (and save)
  4385. * @param int integer giving the new prerequisite of this learnpath
  4386. * @return bool returns true if prerequisite is not empty
  4387. */
  4388. public function set_prerequisite($prerequisite)
  4389. {
  4390. $course_id = api_get_course_int_id();
  4391. if ($this->debug > 0) {
  4392. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4393. }
  4394. $this->prerequisite = intval($prerequisite);
  4395. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4396. $lp_id = $this->get_id();
  4397. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4398. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4399. if ($this->debug > 2) {
  4400. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4401. }
  4402. Database::query($sql);
  4403. return true;
  4404. }
  4405. /**
  4406. * Sets the location/proximity of the LP (local/remote) (and save)
  4407. * @param string Optional string giving the new location of this learnpath
  4408. * @return boolean True on success / False on error
  4409. */
  4410. public function set_proximity($name = '')
  4411. {
  4412. $course_id = api_get_course_int_id();
  4413. if ($this->debug > 0) {
  4414. error_log('New LP - In learnpath::set_proximity()', 0);
  4415. }
  4416. if (empty ($name))
  4417. return false;
  4418. $this->proximity = $name;
  4419. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4420. $lp_id = $this->get_id();
  4421. $sql = "UPDATE $lp_table SET
  4422. content_local = '" . Database::escape_string($name) . "'
  4423. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4424. if ($this->debug > 2) {
  4425. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4426. }
  4427. Database::query($sql);
  4428. return true;
  4429. }
  4430. /**
  4431. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4432. * @param integer DB ID of the item
  4433. */
  4434. public function set_previous_item($id)
  4435. {
  4436. if ($this->debug > 0) {
  4437. error_log('New LP - In learnpath::set_previous_item()', 0);
  4438. }
  4439. $this->last = $id;
  4440. }
  4441. /**
  4442. * Sets use_max_score
  4443. * @param string $use_max_score Optional string giving the new location of this learnpath
  4444. * @return boolean True on success / False on error
  4445. */
  4446. public function set_use_max_score($use_max_score = 1)
  4447. {
  4448. $course_id = api_get_course_int_id();
  4449. if ($this->debug > 0) {
  4450. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4451. }
  4452. $use_max_score = intval($use_max_score);
  4453. $this->use_max_score = $use_max_score;
  4454. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4455. $lp_id = $this->get_id();
  4456. $sql = "UPDATE $lp_table SET
  4457. use_max_score = '" . $this->use_max_score . "'
  4458. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4459. if ($this->debug > 2) {
  4460. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4461. }
  4462. Database::query($sql);
  4463. return true;
  4464. }
  4465. /**
  4466. * Sets and saves the expired_on date
  4467. * @param string $expired_on Optional string giving the new author of this learnpath
  4468. * @return bool Returns true if author's name is not empty
  4469. */
  4470. public function set_expired_on($expired_on)
  4471. {
  4472. $course_id = api_get_course_int_id();
  4473. if ($this->debug > 0) {
  4474. error_log('New LP - In learnpath::set_expired_on()', 0);
  4475. }
  4476. if (!empty($expired_on)) {
  4477. $this->expired_on = api_get_utc_datetime($expired_on);
  4478. } else {
  4479. $this->expired_on = '';
  4480. }
  4481. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4482. $lp_id = $this->get_id();
  4483. $sql = "UPDATE $lp_table SET
  4484. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4485. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4486. if ($this->debug > 2) {
  4487. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4488. }
  4489. Database::query($sql);
  4490. return true;
  4491. }
  4492. /**
  4493. * Sets and saves the publicated_on date
  4494. * @param string $publicated_on Optional string giving the new author of this learnpath
  4495. * @return bool Returns true if author's name is not empty
  4496. */
  4497. public function set_publicated_on($publicated_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. if (!empty($publicated_on)) {
  4504. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4505. } else {
  4506. $this->publicated_on = '';
  4507. }
  4508. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4509. $lp_id = $this->get_id();
  4510. $sql = "UPDATE $lp_table SET
  4511. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4512. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4513. if ($this->debug > 2) {
  4514. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4515. }
  4516. Database::query($sql);
  4517. return true;
  4518. }
  4519. /**
  4520. * Sets and saves the expired_on date
  4521. * @return bool Returns true if author's name is not empty
  4522. */
  4523. public function set_modified_on()
  4524. {
  4525. $course_id = api_get_course_int_id();
  4526. if ($this->debug > 0) {
  4527. error_log('New LP - In learnpath::set_expired_on()', 0);
  4528. }
  4529. $this->modified_on = api_get_utc_datetime();
  4530. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4531. $lp_id = $this->get_id();
  4532. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4533. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4534. if ($this->debug > 2) {
  4535. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4536. }
  4537. Database::query($sql);
  4538. return true;
  4539. }
  4540. /**
  4541. * Sets the object's error message
  4542. * @param string Error message. If empty, reinits the error string
  4543. * @return void
  4544. */
  4545. public function set_error_msg($error = '')
  4546. {
  4547. if ($this->debug > 0) {
  4548. error_log('New LP - In learnpath::set_error_msg()', 0);
  4549. }
  4550. if (empty ($error)) {
  4551. $this->error = '';
  4552. } else {
  4553. $this->error .= $error;
  4554. }
  4555. }
  4556. /**
  4557. * Launches the current item if not 'sco'
  4558. * (starts timer and make sure there is a record ready in the DB)
  4559. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4560. * @return boolean
  4561. */
  4562. public function start_current_item($allow_new_attempt = false)
  4563. {
  4564. if ($this->debug > 0) {
  4565. error_log('New LP - In learnpath::start_current_item()', 0);
  4566. }
  4567. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4568. $type = $this->get_type();
  4569. $item_type = $this->items[$this->current]->get_type();
  4570. if (($type == 2 && $item_type != 'sco') ||
  4571. ($type == 3 && $item_type != 'au') ||
  4572. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4573. ) {
  4574. $this->items[$this->current]->open($allow_new_attempt);
  4575. $this->autocomplete_parents($this->current);
  4576. $prereq_check = $this->prerequisites_match($this->current);
  4577. $this->items[$this->current]->save(false, $prereq_check);
  4578. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4579. } else {
  4580. // If sco, then it is supposed to have been updated by some other call.
  4581. }
  4582. if ($item_type == 'sco') {
  4583. $this->items[$this->current]->restart();
  4584. }
  4585. }
  4586. if ($this->debug > 0) {
  4587. error_log('New LP - End of learnpath::start_current_item()', 0);
  4588. }
  4589. return true;
  4590. }
  4591. /**
  4592. * Stops the processing and counters for the old item (as held in $this->last)
  4593. * @return boolean True/False
  4594. */
  4595. public function stop_previous_item()
  4596. {
  4597. if ($this->debug > 0) {
  4598. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4599. }
  4600. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4601. if ($this->debug > 2) {
  4602. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4603. }
  4604. switch ($this->get_type()) {
  4605. case '3' :
  4606. if ($this->items[$this->last]->get_type() != 'au') {
  4607. if ($this->debug > 2) {
  4608. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4609. }
  4610. $this->items[$this->last]->close();
  4611. //$this->autocomplete_parents($this->last);
  4612. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4613. } else {
  4614. if ($this->debug > 2) {
  4615. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4616. }
  4617. }
  4618. case '2' :
  4619. if ($this->items[$this->last]->get_type() != 'sco') {
  4620. if ($this->debug > 2) {
  4621. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4622. }
  4623. $this->items[$this->last]->close();
  4624. //$this->autocomplete_parents($this->last);
  4625. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4626. } else {
  4627. if ($this->debug > 2) {
  4628. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4629. }
  4630. }
  4631. break;
  4632. case '1' :
  4633. default :
  4634. if ($this->debug > 2) {
  4635. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4636. }
  4637. $this->items[$this->last]->close();
  4638. break;
  4639. }
  4640. } else {
  4641. if ($this->debug > 2) {
  4642. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4643. }
  4644. return false;
  4645. }
  4646. return true;
  4647. }
  4648. /**
  4649. * Updates the default view mode from fullscreen to embedded and inversely
  4650. * @return string The current default view mode ('fullscreen' or 'embedded')
  4651. */
  4652. public function update_default_view_mode()
  4653. {
  4654. $course_id = api_get_course_int_id();
  4655. if ($this->debug > 0) {
  4656. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4657. }
  4658. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4659. $sql = "SELECT * FROM $lp_table
  4660. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4661. $res = Database::query($sql);
  4662. if (Database :: num_rows($res) > 0) {
  4663. $row = Database :: fetch_array($res);
  4664. $default_view_mode = $row['default_view_mod'];
  4665. $view_mode = $default_view_mode;
  4666. switch ($default_view_mode) {
  4667. case 'fullscreen':
  4668. $view_mode = 'embedded';
  4669. break;
  4670. case 'embedded':
  4671. $view_mode = 'embedframe';
  4672. break;
  4673. case 'embedframe':
  4674. $view_mode = 'impress';
  4675. break;
  4676. case 'impress':
  4677. $view_mode = 'fullscreen';
  4678. break;
  4679. }
  4680. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4681. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4682. Database::query($sql);
  4683. $this->mode = $view_mode;
  4684. return $view_mode;
  4685. } else {
  4686. if ($this->debug > 2) {
  4687. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4688. }
  4689. }
  4690. return -1;
  4691. }
  4692. /**
  4693. * Updates the default behaviour about auto-commiting SCORM updates
  4694. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4695. */
  4696. public function update_default_scorm_commit()
  4697. {
  4698. $course_id = api_get_course_int_id();
  4699. if ($this->debug > 0) {
  4700. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4701. }
  4702. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4703. $sql = "SELECT * FROM $lp_table
  4704. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4705. $res = Database::query($sql);
  4706. if (Database :: num_rows($res) > 0) {
  4707. $row = Database :: fetch_array($res);
  4708. $force = $row['force_commit'];
  4709. if ($force == 1) {
  4710. $force = 0;
  4711. $force_return = false;
  4712. } elseif ($force == 0) {
  4713. $force = 1;
  4714. $force_return = true;
  4715. }
  4716. $sql = "UPDATE $lp_table SET force_commit = $force
  4717. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4718. Database::query($sql);
  4719. $this->force_commit = $force_return;
  4720. return $force_return;
  4721. } else {
  4722. if ($this->debug > 2) {
  4723. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4724. }
  4725. }
  4726. return -1;
  4727. }
  4728. /**
  4729. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4730. * @return bool True on success, false on failure
  4731. */
  4732. public function update_display_order()
  4733. {
  4734. $course_id = api_get_course_int_id();
  4735. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4736. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4737. $res = Database::query($sql);
  4738. if ($res === false)
  4739. return false;
  4740. $num = Database :: num_rows($res);
  4741. // First check the order is correct, globally (might be wrong because
  4742. // of versions < 1.8.4).
  4743. if ($num > 0) {
  4744. $i = 1;
  4745. while ($row = Database :: fetch_array($res)) {
  4746. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4747. $need_fix = true;
  4748. $sql = "UPDATE $lp_table SET display_order = $i
  4749. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4750. Database::query($sql);
  4751. }
  4752. $i++;
  4753. }
  4754. }
  4755. return true;
  4756. }
  4757. /**
  4758. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4759. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4760. */
  4761. public function update_reinit()
  4762. {
  4763. $course_id = api_get_course_int_id();
  4764. if ($this->debug > 0) {
  4765. error_log('New LP - In learnpath::update_reinit()', 0);
  4766. }
  4767. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4768. $sql = "SELECT * FROM $lp_table
  4769. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4770. $res = Database::query($sql);
  4771. if (Database :: num_rows($res) > 0) {
  4772. $row = Database :: fetch_array($res);
  4773. $force = $row['prevent_reinit'];
  4774. if ($force == 1) {
  4775. $force = 0;
  4776. } elseif ($force == 0) {
  4777. $force = 1;
  4778. }
  4779. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4780. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4781. Database::query($sql);
  4782. $this->prevent_reinit = $force;
  4783. return $force;
  4784. } else {
  4785. if ($this->debug > 2) {
  4786. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4787. }
  4788. }
  4789. return -1;
  4790. }
  4791. /**
  4792. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4793. *
  4794. * @return string 'single', 'multi' or 'seriousgame'
  4795. * @author ndiechburg <noel@cblue.be>
  4796. **/
  4797. public function get_attempt_mode()
  4798. {
  4799. //Set default value for seriousgame_mode
  4800. if (!isset($this->seriousgame_mode)) {
  4801. $this->seriousgame_mode=0;
  4802. }
  4803. // Set default value for prevent_reinit
  4804. if (!isset($this->prevent_reinit)) {
  4805. $this->prevent_reinit =1;
  4806. }
  4807. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4808. return 'seriousgame';
  4809. }
  4810. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4811. return 'single';
  4812. }
  4813. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4814. return 'multiple';
  4815. }
  4816. return 'single';
  4817. }
  4818. /**
  4819. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4820. *
  4821. * @param string 'seriousgame', 'single' or 'multiple'
  4822. * @return boolean
  4823. * @author ndiechburg <noel@cblue.be>
  4824. **/
  4825. public function set_attempt_mode($mode)
  4826. {
  4827. $course_id = api_get_course_int_id();
  4828. switch ($mode) {
  4829. case 'seriousgame' :
  4830. $sg_mode = 1;
  4831. $prevent_reinit = 1;
  4832. break;
  4833. case 'single' :
  4834. $sg_mode = 0;
  4835. $prevent_reinit = 1;
  4836. break;
  4837. case 'multiple' :
  4838. $sg_mode = 0;
  4839. $prevent_reinit = 0;
  4840. break;
  4841. default :
  4842. $sg_mode = 0;
  4843. $prevent_reinit = 0;
  4844. break;
  4845. }
  4846. $this->prevent_reinit = $prevent_reinit;
  4847. $this->seriousgame_mode = $sg_mode;
  4848. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4849. $sql = "UPDATE $lp_table SET
  4850. prevent_reinit = $prevent_reinit ,
  4851. seriousgame_mode = $sg_mode
  4852. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4853. $res = Database::query($sql);
  4854. if ($res) {
  4855. return true;
  4856. } else {
  4857. return false;
  4858. }
  4859. }
  4860. /**
  4861. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4862. *
  4863. * @return boolean
  4864. * @author ndiechburg <noel@cblue.be>
  4865. **/
  4866. public function switch_attempt_mode()
  4867. {
  4868. if ($this->debug > 0) {
  4869. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4870. }
  4871. $mode = $this->get_attempt_mode();
  4872. switch ($mode) {
  4873. case 'single' :
  4874. $next_mode = 'multiple';
  4875. break;
  4876. case 'multiple' :
  4877. $next_mode = 'seriousgame';
  4878. break;
  4879. case 'seriousgame' :
  4880. $next_mode = 'single';
  4881. break;
  4882. default :
  4883. $next_mode = 'single';
  4884. break;
  4885. }
  4886. $this->set_attempt_mode($next_mode);
  4887. }
  4888. /**
  4889. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4890. * but possibility to do again a completed item.
  4891. *
  4892. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4893. * @author ndiechburg <noel@cblue.be>
  4894. **/
  4895. public function set_seriousgame_mode()
  4896. {
  4897. $course_id = api_get_course_int_id();
  4898. if ($this->debug > 0) {
  4899. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4900. }
  4901. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4902. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4903. $res = Database::query($sql);
  4904. if (Database :: num_rows($res) > 0) {
  4905. $row = Database :: fetch_array($res);
  4906. $force = $row['seriousgame_mode'];
  4907. if ($force == 1) {
  4908. $force = 0;
  4909. } elseif ($force == 0) {
  4910. $force = 1;
  4911. }
  4912. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4913. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4914. Database::query($sql);
  4915. $this->seriousgame_mode = $force;
  4916. return $force;
  4917. } else {
  4918. if ($this->debug > 2) {
  4919. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4920. }
  4921. }
  4922. return -1;
  4923. }
  4924. /**
  4925. * Updates the "scorm_debug" value that shows or hide the debug window
  4926. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4927. */
  4928. public function update_scorm_debug()
  4929. {
  4930. $course_id = api_get_course_int_id();
  4931. if ($this->debug > 0) {
  4932. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4933. }
  4934. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4935. $sql = "SELECT * FROM $lp_table
  4936. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4937. $res = Database::query($sql);
  4938. if (Database :: num_rows($res) > 0) {
  4939. $row = Database :: fetch_array($res);
  4940. $force = $row['debug'];
  4941. if ($force == 1) {
  4942. $force = 0;
  4943. } elseif ($force == 0) {
  4944. $force = 1;
  4945. }
  4946. $sql = "UPDATE $lp_table SET debug = $force
  4947. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4948. $res = Database::query($sql);
  4949. $this->scorm_debug = $force;
  4950. return $force;
  4951. } else {
  4952. if ($this->debug > 2) {
  4953. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4954. }
  4955. }
  4956. return -1;
  4957. }
  4958. /**
  4959. * Function that makes a call to the function sort_tree_array and create_tree_array
  4960. * @author Kevin Van Den Haute
  4961. * @param array
  4962. */
  4963. public function tree_array($array)
  4964. {
  4965. if ($this->debug > 1) {
  4966. error_log('New LP - In learnpath::tree_array()', 0);
  4967. }
  4968. $array = $this->sort_tree_array($array);
  4969. $this->create_tree_array($array);
  4970. }
  4971. /**
  4972. * Creates an array with the elements of the learning path tree in it
  4973. *
  4974. * @author Kevin Van Den Haute
  4975. * @param array $array
  4976. * @param int $parent
  4977. * @param int $depth
  4978. * @param array $tmp
  4979. */
  4980. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4981. {
  4982. if ($this->debug > 1) {
  4983. error_log('New LP - In learnpath::create_tree_array())', 0);
  4984. }
  4985. if (is_array($array)) {
  4986. for ($i = 0; $i < count($array); $i++) {
  4987. if ($array[$i]['parent_item_id'] == $parent) {
  4988. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4989. $tmp[] = $array[$i]['parent_item_id'];
  4990. $depth++;
  4991. }
  4992. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4993. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4994. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4995. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4996. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4997. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4998. $this->arrMenu[] = array(
  4999. 'id' => $array[$i]['id'],
  5000. 'ref' => $ref,
  5001. 'item_type' => $array[$i]['item_type'],
  5002. 'title' => $array[$i]['title'],
  5003. 'path' => $path,
  5004. 'description' => $array[$i]['description'],
  5005. 'parent_item_id' => $array[$i]['parent_item_id'],
  5006. 'previous_item_id' => $array[$i]['previous_item_id'],
  5007. 'next_item_id' => $array[$i]['next_item_id'],
  5008. 'min_score' => $array[$i]['min_score'],
  5009. 'max_score' => $array[$i]['max_score'],
  5010. 'mastery_score' => $array[$i]['mastery_score'],
  5011. 'display_order' => $array[$i]['display_order'],
  5012. 'prerequisite' => $preq,
  5013. 'depth' => $depth,
  5014. 'audio' => $audio,
  5015. 'prerequisite_min_score' => $prerequisiteMinScore,
  5016. 'prerequisite_max_score' => $prerequisiteMaxScore
  5017. );
  5018. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5019. }
  5020. }
  5021. }
  5022. }
  5023. /**
  5024. * Sorts a multi dimensional array by parent id and display order
  5025. * @author Kevin Van Den Haute
  5026. *
  5027. * @param array $array (array with al the learning path items in it)
  5028. *
  5029. * @return array
  5030. */
  5031. public function sort_tree_array($array) {
  5032. foreach ($array as $key => $row) {
  5033. $parent[$key] = $row['parent_item_id'];
  5034. $position[$key] = $row['display_order'];
  5035. }
  5036. if (count($array) > 0)
  5037. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5038. return $array;
  5039. }
  5040. /**
  5041. * Function that creates a html list of learning path items so that we can add audio files to them
  5042. * @author Kevin Van Den Haute
  5043. * @param int $lp_id
  5044. * @return string
  5045. */
  5046. public function overview()
  5047. {
  5048. if ($this->debug > 0) {
  5049. error_log('New LP - In learnpath::overview()', 0);
  5050. }
  5051. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5052. $return = '';
  5053. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5054. // we need to start a form when we want to update all the mp3 files
  5055. if ($update_audio == 'true') {
  5056. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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">';
  5057. }
  5058. $return .= '<div id="message"></div>';
  5059. if (count($this->items) == 0) {
  5060. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5061. } else {
  5062. $return_audio = '<table class="data_table">';
  5063. $return_audio .= '<tr>';
  5064. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5065. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5066. $return_audio .= '</tr>';
  5067. if ($update_audio != 'true') {
  5068. $return .= '<div class="col-md-12">';
  5069. $return .= self::return_new_tree($update_audio);
  5070. $return .='</div>';
  5071. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5072. } else {
  5073. $return_audio .= self::return_new_tree($update_audio);
  5074. $return .= $return_audio.'</table>';
  5075. }
  5076. // We need to close the form when we are updating the mp3 files.
  5077. if ($update_audio == 'true') {
  5078. $return .= '<div class="footer-audio">';
  5079. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5080. $return .= '</div>';
  5081. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5082. }
  5083. }
  5084. // We need to close the form when we are updating the mp3 files.
  5085. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5086. $return .= '</form>';
  5087. }
  5088. return $return;
  5089. }
  5090. /**
  5091. * @param string string $update_audio
  5092. * @param bool $drop_element_here
  5093. * @return string
  5094. */
  5095. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5096. {
  5097. $return = '';
  5098. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5099. $course_id = api_get_course_int_id();
  5100. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5101. $sql = "SELECT * FROM $tbl_lp_item
  5102. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5103. $result = Database::query($sql);
  5104. $arrLP = array();
  5105. while ($row = Database :: fetch_array($result)) {
  5106. $arrLP[] = array(
  5107. 'id' => $row['id'],
  5108. 'item_type' => $row['item_type'],
  5109. 'title' => Security :: remove_XSS($row['title']),
  5110. 'path' => $row['path'],
  5111. 'description' => Security::remove_XSS($row['description']),
  5112. 'parent_item_id' => $row['parent_item_id'],
  5113. 'previous_item_id' => $row['previous_item_id'],
  5114. 'next_item_id' => $row['next_item_id'],
  5115. 'max_score' => $row['max_score'],
  5116. 'min_score' => $row['min_score'],
  5117. 'mastery_score' => $row['mastery_score'],
  5118. 'prerequisite' => $row['prerequisite'],
  5119. 'display_order' => $row['display_order'],
  5120. 'audio' => $row['audio'],
  5121. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5122. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5123. );
  5124. }
  5125. $this->tree_array($arrLP);
  5126. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5127. unset ($this->arrMenu);
  5128. $default_data = null;
  5129. $default_content = null;
  5130. $elements = array();
  5131. $return_audio = null;
  5132. for ($i = 0; $i < count($arrLP); $i++) {
  5133. $title = $arrLP[$i]['title'];
  5134. $title_cut = cut($arrLP[$i]['title'], 25);
  5135. //Link for the documents
  5136. if ($arrLP[$i]['item_type'] == 'document') {
  5137. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5138. $title_cut = Display::url(
  5139. $title_cut,
  5140. $url,
  5141. array(
  5142. 'class' => 'ajax',
  5143. 'data-title' => $title_cut
  5144. )
  5145. );
  5146. }
  5147. if (($i % 2) == 0) {
  5148. $oddClass = 'row_odd';
  5149. } else {
  5150. $oddClass = 'row_even';
  5151. }
  5152. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5153. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5154. $icon = '';
  5155. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5156. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5157. } else {
  5158. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5159. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5160. } else {
  5161. $icon = '<img src="../img/folder_document.gif" />';
  5162. }
  5163. }
  5164. // The audio column.
  5165. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5166. $audio = '';
  5167. if (!$update_audio || $update_audio <> 'true') {
  5168. if (!empty($arrLP[$i]['audio'])) {
  5169. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5170. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5171. $audio .= '<script type="text/javascript">
  5172. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5173. s1.addParam("allowscriptaccess","always");
  5174. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5175. s1.write("container' . $i . '");
  5176. </script>';*/
  5177. } else {
  5178. $audio .= '';
  5179. }
  5180. } else {
  5181. $types = self::getChapterTypes();
  5182. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5183. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5184. if (!empty ($arrLP[$i]['audio'])) {
  5185. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5186. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5187. }
  5188. }
  5189. }
  5190. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5191. $return_audio .= '</td>';
  5192. $move_icon = '';
  5193. $move_item_icon = '';
  5194. $edit_icon = '';
  5195. $delete_icon = '';
  5196. $audio_icon = '';
  5197. $prerequisities_icon = '';
  5198. $forumIcon = '';
  5199. if ($is_allowed_to_edit) {
  5200. if (!$update_audio || $update_audio <> 'true') {
  5201. $move_icon .= '<a class="moved" href="#">';
  5202. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5203. $move_icon .= '</a>';
  5204. }
  5205. // No edit for this item types
  5206. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5207. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5208. $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">';
  5209. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5210. $edit_icon .= '</a>';
  5211. if (
  5212. !in_array($arrLP[$i]['item_type'], ['forum', 'thread'])
  5213. ) {
  5214. if (
  5215. $this->items[$arrLP[$i]['id']]->getForumThread(
  5216. $this->course_int_id,
  5217. $this->lp_session_id
  5218. )
  5219. ) {
  5220. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5221. 'action' => 'dissociate_forum',
  5222. 'id' => $arrLP[$i]['id'],
  5223. 'lp_id' => $this->lp_id
  5224. ]);
  5225. $forumIcon = Display::url(
  5226. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5227. $forumIconUrl,
  5228. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5229. );
  5230. } else {
  5231. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5232. 'action' => 'create_forum',
  5233. 'id' => $arrLP[$i]['id'],
  5234. 'lp_id' => $this->lp_id
  5235. ]);
  5236. $forumIcon = Display::url(
  5237. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5238. $forumIconUrl,
  5239. ['class' => "btn btn-default lp-btn-associate-forum"]
  5240. );
  5241. }
  5242. }
  5243. } else {
  5244. $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">';
  5245. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5246. $edit_icon .= '</a>';
  5247. }
  5248. }
  5249. $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">';
  5250. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5251. $delete_icon .= '</a>';
  5252. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5253. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5254. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']);
  5255. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item', ['class' => 'btn btn-default']);
  5256. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio', ['class' => 'btn btn-default']);
  5257. }
  5258. }
  5259. if ($update_audio != 'true') {
  5260. $row = $move_icon . ' ' . $icon .
  5261. Display::span($title_cut) .
  5262. Display::tag(
  5263. 'div',
  5264. "<div class=\"btn-group btn-group-xs\">$audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5265. array('class'=>'btn-toolbar button_actions')
  5266. );
  5267. } else {
  5268. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5269. }
  5270. $parent_id = $arrLP[$i]['parent_item_id'];
  5271. $default_data[$arrLP[$i]['id']] = $row;
  5272. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5273. if (empty($parent_id)) {
  5274. $elements[$arrLP[$i]['id']]['data'] = $row;
  5275. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5276. } else {
  5277. $parent_arrays = array();
  5278. if ($arrLP[$i]['depth'] > 1) {
  5279. //Getting list of parents
  5280. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5281. foreach($arrLP as $item) {
  5282. if ($item['id'] == $parent_id) {
  5283. if ($item['parent_item_id'] == 0) {
  5284. $parent_id = $item['id'];
  5285. break;
  5286. } else {
  5287. $parent_id = $item['parent_item_id'];
  5288. if (empty($parent_arrays)) {
  5289. $parent_arrays[] = intval($item['id']);
  5290. }
  5291. $parent_arrays[] = $parent_id;
  5292. break;
  5293. }
  5294. }
  5295. }
  5296. }
  5297. }
  5298. if (!empty($parent_arrays)) {
  5299. $parent_arrays = array_reverse($parent_arrays);
  5300. $val = '$elements';
  5301. $x = 0;
  5302. foreach($parent_arrays as $item) {
  5303. if ($x != count($parent_arrays) -1) {
  5304. $val .= '["'.$item.'"]["children"]';
  5305. } else {
  5306. $val .= '["'.$item.'"]["children"]';
  5307. }
  5308. $x++;
  5309. }
  5310. $val .= "";
  5311. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5312. eval($code_str);
  5313. } else {
  5314. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5315. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5316. }
  5317. }
  5318. }
  5319. $list = '<ul id="lp_item_list">';
  5320. $tree = self::print_recursive($elements, $default_data, $default_content);
  5321. if (!empty($tree)) {
  5322. $list .= $tree;
  5323. } else {
  5324. if ($drop_element_here) {
  5325. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5326. }
  5327. }
  5328. $list .= '</ul>';
  5329. //$return .= Display::panel($list, $this->name);
  5330. $return .= Display::panelCollapse($this->name, $list, 'scorm-list', null, 'scorm-list-accordion', 'scorm-list-collapse');
  5331. if ($update_audio == 'true') {
  5332. $return = $return_audio;
  5333. }
  5334. return $return;
  5335. }
  5336. /**
  5337. * @param array $elements
  5338. * @param array $default_data
  5339. * @param array $default_content
  5340. * @return string
  5341. */
  5342. public function print_recursive($elements, $default_data, $default_content)
  5343. {
  5344. $return = '';
  5345. foreach ($elements as $key => $item) {
  5346. if (isset($item['load_data']) || empty($item['data'])) {
  5347. $item['data'] = $default_data[$item['load_data']];
  5348. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5349. }
  5350. $sub_list = '';
  5351. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5352. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5353. }
  5354. if (empty($item['children'])) {
  5355. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5356. $active = null;
  5357. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5358. $active = 'active';
  5359. }
  5360. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5361. } else {
  5362. //sections
  5363. if (isset($item['children'])) {
  5364. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5365. }
  5366. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5367. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5368. }
  5369. }
  5370. return $return;
  5371. }
  5372. /**
  5373. * This function builds the action menu
  5374. * @param bool $returnContent
  5375. * @return void
  5376. */
  5377. public function build_action_menu($returnContent = false)
  5378. {
  5379. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5380. $return = '<div class="actions">';
  5381. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '&isStudentView=true">' . Display :: return_icon('preview_view.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  5382. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=admin_view&lp_id=' . $_SESSION['oLP']->lp_id . '&updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5383. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&action=edit&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5384. $buttons = array(
  5385. array(
  5386. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5387. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5388. ),
  5389. array(
  5390. 'title' => get_lang('ClearAllPrerequisites'),
  5391. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5392. ),
  5393. );
  5394. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5395. $return .= '</div>';
  5396. if ($returnContent) {
  5397. return $return;
  5398. }
  5399. echo $return;
  5400. }
  5401. /**
  5402. * Creates the default learning path folder
  5403. * @param array $course
  5404. * @return bool
  5405. */
  5406. public static function generate_learning_path_folder($course)
  5407. {
  5408. // Creating learning_path folder
  5409. $dir = '/learning_path';
  5410. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5411. $folder = false;
  5412. if (!is_dir($filepath.'/'.$dir)) {
  5413. $folderData = create_unexisting_directory(
  5414. $course,
  5415. api_get_user_id(),
  5416. api_get_session_id(),
  5417. 0,
  5418. 0,
  5419. $filepath,
  5420. $dir,
  5421. get_lang('LearningPaths'),
  5422. 0
  5423. );
  5424. if (!empty($folderData)) {
  5425. $folder = true;
  5426. }
  5427. } else {
  5428. $folder = true;
  5429. }
  5430. return $folder;
  5431. }
  5432. /**
  5433. * @param array $course
  5434. * @param string $lp_name
  5435. * @return array
  5436. */
  5437. public function generate_lp_folder($course, $lp_name = null)
  5438. {
  5439. $filepath = '';
  5440. $dir = '/learning_path/';
  5441. if (empty($lp_name)) {
  5442. $lp_name = $this->name;
  5443. }
  5444. $folder = self::generate_learning_path_folder($course);
  5445. // Creating LP folder
  5446. if ($folder) {
  5447. //Limits title size
  5448. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5449. $dir = $dir.$title;
  5450. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5451. if (!is_dir($filepath.'/'.$dir)) {
  5452. $folderData = create_unexisting_directory(
  5453. $course,
  5454. api_get_user_id(),
  5455. 0,
  5456. 0,
  5457. 0,
  5458. $filepath,
  5459. $dir,
  5460. $lp_name
  5461. );
  5462. if (!empty($folderData)) {
  5463. $folder = true;
  5464. }
  5465. } else {
  5466. $folder = true;
  5467. }
  5468. $dir = $dir.'/';
  5469. if ($folder) {
  5470. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5471. }
  5472. }
  5473. $array = array(
  5474. 'dir' => $dir,
  5475. 'filepath' => $filepath,
  5476. 'folder' => $folder
  5477. );
  5478. return $array;
  5479. }
  5480. /**
  5481. * Create a new document //still needs some finetuning
  5482. * @param array $courseInfo
  5483. * @param string $content
  5484. * @param string $title
  5485. * @param string $extension
  5486. *
  5487. * @return string
  5488. */
  5489. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html')
  5490. {
  5491. if (!empty($courseInfo)) {
  5492. $course_id = $courseInfo['real_id'];
  5493. } else {
  5494. $course_id = api_get_course_int_id();
  5495. }
  5496. global $charset;
  5497. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5498. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5499. // Please, do not modify this dirname formatting.
  5500. if (strstr($dir, '..')) {
  5501. $dir = '/';
  5502. }
  5503. if (!empty($dir[0]) && $dir[0] == '.') {
  5504. $dir = substr($dir, 1);
  5505. }
  5506. if (!empty($dir[0]) && $dir[0] != '/') {
  5507. $dir = '/' . $dir;
  5508. }
  5509. if ($dir[strlen($dir) - 1] != '/') {
  5510. $dir .= '/';
  5511. }
  5512. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document' . $dir;
  5513. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5514. //Generates folder
  5515. $result = $this->generate_lp_folder($courseInfo);
  5516. $dir = $result['dir'];
  5517. $filepath = $result['filepath'];
  5518. }
  5519. if (!is_dir($filepath)) {
  5520. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/';
  5521. $dir = '/';
  5522. }
  5523. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5524. // is already escaped twice when it gets here.
  5525. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5526. if (!empty($title)) {
  5527. $title = api_replace_dangerous_char(stripslashes($title));
  5528. } else {
  5529. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5530. }
  5531. $title = disable_dangerous_file($title);
  5532. $filename = $title;
  5533. $content = isset($content) ? $content : $_POST['content_lp'];
  5534. $tmp_filename = $filename;
  5535. $i = 0;
  5536. while (file_exists($filepath . $tmp_filename . '.'.$extension))
  5537. $tmp_filename = $filename . '_' . ++ $i;
  5538. $filename = $tmp_filename . '.'.$extension;
  5539. if ($extension == 'html') {
  5540. $content = stripslashes($content);
  5541. $content = str_replace(
  5542. api_get_path(WEB_COURSE_PATH),
  5543. api_get_path(REL_PATH).'courses/',
  5544. $content
  5545. );
  5546. // Change the path of mp3 to absolute.
  5547. // The first regexp deals with :// urls.
  5548. $content = preg_replace(
  5549. "|(flashvars=\"file=)([^:/]+)/|",
  5550. "$1".api_get_path(
  5551. REL_COURSE_PATH
  5552. ).$courseInfo['path'].'/document/',
  5553. $content
  5554. );
  5555. // The second regexp deals with audio/ urls.
  5556. $content = preg_replace(
  5557. "|(flashvars=\"file=)([^/]+)/|",
  5558. "$1".api_get_path(
  5559. REL_COURSE_PATH
  5560. ).$courseInfo['path'].'/document/$2/',
  5561. $content
  5562. );
  5563. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5564. $content = str_replace(
  5565. '</body>',
  5566. '<style type="text/css">body{}</style></body>',
  5567. $content
  5568. );
  5569. }
  5570. if (!file_exists($filepath . $filename)) {
  5571. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5572. fputs($fp, $content);
  5573. fclose($fp);
  5574. $file_size = filesize($filepath . $filename);
  5575. $save_file_path = $dir.$filename;
  5576. $document_id = add_document(
  5577. $courseInfo,
  5578. $save_file_path,
  5579. 'file',
  5580. $file_size,
  5581. $tmp_filename
  5582. );
  5583. if ($document_id) {
  5584. api_item_property_update(
  5585. $courseInfo,
  5586. TOOL_DOCUMENT,
  5587. $document_id,
  5588. 'DocumentAdded',
  5589. api_get_user_id(),
  5590. null,
  5591. null,
  5592. null,
  5593. null,
  5594. api_get_session_id()
  5595. );
  5596. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5597. $new_title = $originalTitle;
  5598. if ($new_comment || $new_title) {
  5599. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5600. $ct = '';
  5601. if ($new_comment)
  5602. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5603. if ($new_title)
  5604. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5605. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5606. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5607. Database::query($sql);
  5608. }
  5609. }
  5610. return $document_id;
  5611. }
  5612. }
  5613. }
  5614. /**
  5615. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5616. * @param array $_course array
  5617. * @return void
  5618. */
  5619. public function edit_document($_course)
  5620. {
  5621. $course_id = api_get_course_int_id();
  5622. global $_configuration;
  5623. // Please, do not modify this dirname formatting.
  5624. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5625. if (strstr($dir, '..'))
  5626. $dir = '/';
  5627. if ($dir[0] == '.')
  5628. $dir = substr($dir, 1);
  5629. if ($dir[0] != '/')
  5630. $dir = '/' . $dir;
  5631. if ($dir[strlen($dir) - 1] != '/')
  5632. $dir .= '/';
  5633. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5634. if (!is_dir($filepath)) {
  5635. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5636. $dir = '/';
  5637. }
  5638. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5639. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5640. $document_id = intval($_POST['path']);
  5641. $sql = "SELECT path FROM " . $table_doc . "
  5642. WHERE c_id = $course_id AND id = " . $document_id;
  5643. $res = Database::query($sql);
  5644. $row = Database :: fetch_array($res);
  5645. $content = stripslashes($_POST['content_lp']);
  5646. $file = $filepath . $row['path'];
  5647. if ($fp = @ fopen($file, 'w')) {
  5648. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5649. // Change the path of mp3 to absolute.
  5650. // The first regexp deals with :// urls.
  5651. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5652. // The second regexp deals with audio/ urls.
  5653. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5654. fputs($fp, $content);
  5655. fclose($fp);
  5656. $sql = "UPDATE " . $table_doc ." SET
  5657. title='".Database::escape_string($_POST['title'])."'
  5658. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5659. Database::query($sql);
  5660. }
  5661. }
  5662. }
  5663. /**
  5664. * Displays the selected item, with a panel for manipulating the item
  5665. * @param int $item_id
  5666. * @param string $msg
  5667. * @return string
  5668. */
  5669. public function display_item($item_id, $msg = null, $show_actions = true)
  5670. {
  5671. $course_id = api_get_course_int_id();
  5672. $return = '';
  5673. if (is_numeric($item_id)) {
  5674. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5675. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5676. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5677. $result = Database::query($sql);
  5678. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5679. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5680. // Prevents wrong parent selection for document, see Bug#1251.
  5681. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5682. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5683. }
  5684. if ($show_actions) {
  5685. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5686. }
  5687. $return .= '<div style="padding:10px;">';
  5688. if ($msg != '')
  5689. $return .= $msg;
  5690. $return .= '<h3>'.$row['title'].'</h3>';
  5691. switch ($row['item_type']) {
  5692. case TOOL_QUIZ:
  5693. if (!empty($row['path'])) {
  5694. $exercise = new Exercise();
  5695. $exercise->read($row['path']);
  5696. $return .= $exercise->description.'<br />';
  5697. }
  5698. break;
  5699. case TOOL_DOCUMENT:
  5700. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5701. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5702. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5703. $result = Database::query($sql_doc);
  5704. $path_file = Database::result($result, 0, 0);
  5705. $path_parts = pathinfo($path_file);
  5706. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5707. if (in_array($path_parts['extension'], array(
  5708. 'html',
  5709. 'txt',
  5710. 'png',
  5711. 'jpg',
  5712. 'JPG',
  5713. 'jpeg',
  5714. 'JPEG',
  5715. 'gif',
  5716. 'swf'
  5717. ))) {
  5718. $return .= $this->display_document($row['path'], true, true);
  5719. }
  5720. break;
  5721. }
  5722. $return .= '</div>';
  5723. }
  5724. }
  5725. return $return;
  5726. }
  5727. /**
  5728. * Shows the needed forms for editing a specific item
  5729. * @param int $item_id
  5730. * @return string
  5731. */
  5732. public function display_edit_item($item_id)
  5733. {
  5734. $course_id = api_get_course_int_id();
  5735. $return = '';
  5736. if (is_numeric($item_id)) {
  5737. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5738. $sql = "SELECT * FROM $tbl_lp_item
  5739. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5740. $res = Database::query($sql);
  5741. $row = Database::fetch_array($res);
  5742. switch ($row['item_type']) {
  5743. case 'dokeos_chapter' :
  5744. case 'dir' :
  5745. case 'asset' :
  5746. case 'sco' :
  5747. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5748. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5749. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5750. } else {
  5751. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5752. }
  5753. break;
  5754. case TOOL_DOCUMENT :
  5755. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5756. $sql = "SELECT lp.*, doc.path as dir
  5757. FROM " . $tbl_lp_item . " as lp
  5758. LEFT JOIN " . $tbl_doc . " as doc
  5759. ON doc.id = lp.path
  5760. WHERE
  5761. lp.c_id = $course_id AND
  5762. doc.c_id = $course_id AND
  5763. lp.id = " . intval($item_id);
  5764. $res_step = Database::query($sql);
  5765. $row_step = Database :: fetch_array($res_step);
  5766. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5767. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5768. break;
  5769. case TOOL_LINK :
  5770. $link_id = (string) $row['path'];
  5771. if (ctype_digit($link_id)) {
  5772. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5773. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5774. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5775. $res_link = Database::query($sql_select);
  5776. $row_link = Database :: fetch_array($res_link);
  5777. if (is_array($row_link)) {
  5778. $row['url'] = $row_link['url'];
  5779. }
  5780. }
  5781. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5782. $return .= $this->display_link_form('edit', $item_id, $row);
  5783. break;
  5784. case 'dokeos_module' :
  5785. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5786. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5787. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5788. } else {
  5789. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5790. }
  5791. break;
  5792. case TOOL_QUIZ :
  5793. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5794. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5795. break;
  5796. case TOOL_HOTPOTATOES :
  5797. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5798. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5799. break;
  5800. case TOOL_STUDENTPUBLICATION :
  5801. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5802. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5803. break;
  5804. case TOOL_FORUM :
  5805. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5806. $return .= $this->display_forum_form('edit', $item_id, $row);
  5807. break;
  5808. case TOOL_THREAD :
  5809. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5810. $return .= $this->display_thread_form('edit', $item_id, $row);
  5811. break;
  5812. }
  5813. }
  5814. return $return;
  5815. }
  5816. /**
  5817. * Function that displays a list with al the resources that
  5818. * could be added to the learning path
  5819. * @return string
  5820. */
  5821. public function display_resources()
  5822. {
  5823. $course_code = api_get_course_id();
  5824. // Get all the docs.
  5825. $documents = $this->get_documents(true);
  5826. // Get all the exercises.
  5827. $exercises = $this->get_exercises();
  5828. // Get all the links.
  5829. $links = $this->get_links();
  5830. // Get al the student publications.
  5831. $works = $this->get_student_publications();
  5832. // Get al the forums.
  5833. $forums = $this->get_forums(null, $course_code);
  5834. $headers = array(
  5835. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5836. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5837. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5838. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5839. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5840. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5841. );
  5842. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5843. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5844. echo Display::tabs(
  5845. $headers,
  5846. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5847. );
  5848. return true;
  5849. }
  5850. /**
  5851. * Returns the extension of a document
  5852. * @param string filename
  5853. * @return string Extension (part after the last dot)
  5854. */
  5855. public function get_extension($filename)
  5856. {
  5857. $explode = explode('.', $filename);
  5858. return $explode[count($explode) - 1];
  5859. }
  5860. /**
  5861. * Displays a document by id
  5862. *
  5863. * @param int $id
  5864. * @return string
  5865. */
  5866. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5867. {
  5868. $_course = api_get_course_info();
  5869. $course_id = api_get_course_int_id();
  5870. $return = '';
  5871. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5872. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5873. WHERE c_id = ".$course_id." AND id = " . $id;
  5874. $res_doc = Database::query($sql_doc);
  5875. $row_doc = Database :: fetch_array($res_doc);
  5876. // TODO: Add a path filter.
  5877. if ($iframe) {
  5878. $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>';
  5879. } else {
  5880. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5881. }
  5882. return $return;
  5883. }
  5884. /**
  5885. * Return HTML form to add/edit a quiz
  5886. * @param string Action (add/edit)
  5887. * @param integer Item ID if already exists
  5888. * @param mixed Extra information (quiz ID if integer)
  5889. * @return string HTML form
  5890. */
  5891. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5892. {
  5893. $course_id = api_get_course_int_id();
  5894. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5895. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5896. if ($id != 0 && is_array($extra_info)) {
  5897. $item_title = $extra_info['title'];
  5898. $item_description = $extra_info['description'];
  5899. } elseif (is_numeric($extra_info)) {
  5900. $sql = "SELECT title, description
  5901. FROM " . $tbl_quiz . "
  5902. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5903. $result = Database::query($sql);
  5904. $row = Database::fetch_array($result);
  5905. $item_title = $row['title'];
  5906. $item_description = $row['description'];
  5907. } else {
  5908. $item_title = '';
  5909. $item_description = '';
  5910. }
  5911. $item_title = Security::remove_XSS($item_title);
  5912. $item_description = Security::remove_XSS($item_description);
  5913. $legend = '<legend>';
  5914. if ($id != 0 && is_array($extra_info))
  5915. $parent = $extra_info['parent_item_id'];
  5916. else
  5917. $parent = 0;
  5918. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5919. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5920. $result = Database::query($sql);
  5921. $arrLP = array ();
  5922. while ($row = Database :: fetch_array($result)) {
  5923. $arrLP[] = array (
  5924. 'id' => $row['id'],
  5925. 'item_type' => $row['item_type'],
  5926. 'title' => $row['title'],
  5927. 'path' => $row['path'],
  5928. 'description' => $row['description'],
  5929. 'parent_item_id' => $row['parent_item_id'],
  5930. 'previous_item_id' => $row['previous_item_id'],
  5931. 'next_item_id' => $row['next_item_id'],
  5932. 'display_order' => $row['display_order'],
  5933. 'max_score' => $row['max_score'],
  5934. 'min_score' => $row['min_score'],
  5935. 'mastery_score' => $row['mastery_score'],
  5936. 'prerequisite' => $row['prerequisite'],
  5937. 'max_time_allowed' => $row['max_time_allowed']
  5938. );
  5939. }
  5940. $this->tree_array($arrLP);
  5941. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5942. unset ($this->arrMenu);
  5943. if ($action == 'add') {
  5944. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5945. } elseif ($action == 'move') {
  5946. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5947. } else {
  5948. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5949. }
  5950. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5951. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5952. }
  5953. $legend .= '</legend>';
  5954. $return = '';
  5955. $return .= '<div class="sectioncomment">';
  5956. $return .= '<form method="POST">';
  5957. $return .= $legend;
  5958. $return .= '<table class="lp_form">';
  5959. if ($action != 'move') {
  5960. $return .= '<tr>';
  5961. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5962. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5963. $return .= '</tr>';
  5964. }
  5965. $return .= '<tr>';
  5966. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5967. $return .= '<td class="input">';
  5968. // Select for Parent item, root or chapter
  5969. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5970. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5971. $arrHide = array (
  5972. $id
  5973. );
  5974. for ($i = 0; $i < count($arrLP); $i++) {
  5975. if ($action != 'add') {
  5976. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5977. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5978. } else {
  5979. $arrHide[] = $arrLP[$i]['id'];
  5980. }
  5981. } else {
  5982. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5983. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5984. }
  5985. }
  5986. if (is_array($arrLP)) {
  5987. reset($arrLP);
  5988. }
  5989. $return .= '</select>';
  5990. $return .= '</td>';
  5991. $return .= '</tr>';
  5992. $return .= '<tr>';
  5993. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5994. $return .= '<td class="input">';
  5995. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5996. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5997. for ($i = 0; $i < count($arrLP); $i++) {
  5998. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5999. if (is_array($extra_info)) {
  6000. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6001. $selected = 'selected="selected" ';
  6002. }
  6003. } elseif ($action == 'add') {
  6004. $selected = 'selected="selected" ';
  6005. } else {
  6006. $selected = '';
  6007. }
  6008. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6009. }
  6010. }
  6011. $return .= '</select>';
  6012. $return .= '</td>';
  6013. $return .= '</tr>';
  6014. if ($action != 'move') {
  6015. $id_prerequisite = 0;
  6016. if (is_array($arrLP)) {
  6017. foreach ($arrLP as $key => $value) {
  6018. if ($value['id'] == $id) {
  6019. $id_prerequisite = $value['prerequisite'];
  6020. break;
  6021. }
  6022. }
  6023. }
  6024. $arrHide = array ();
  6025. for ($i = 0; $i < count($arrLP); $i++) {
  6026. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6027. if (is_array($extra_info)) {
  6028. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6029. $s_selected_position = $arrLP[$i]['id'];
  6030. }
  6031. } elseif ($action == 'add') {
  6032. $s_selected_position = 0;
  6033. }
  6034. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6035. }
  6036. }
  6037. /*// Commented the prerequisites, only visible in edit (exercise).
  6038. $return .= '<tr>';
  6039. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6040. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6041. foreach($arrHide as $key => $value){
  6042. if($key==$s_selected_position && $action == 'add'){
  6043. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6044. }
  6045. elseif($key==$id_prerequisite && $action == 'edit'){
  6046. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6047. }
  6048. else{
  6049. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6050. }
  6051. }
  6052. $return .= "</select></td>";
  6053. */
  6054. $return .= '</tr>';
  6055. /*$return .= '<tr>';
  6056. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6057. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6058. // Remove temporarily the test description.
  6059. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6060. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6061. $return .= '</tr>'; */
  6062. }
  6063. $return .= '<tr>';
  6064. if ($action == 'add') {
  6065. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  6066. } else {
  6067. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  6068. }
  6069. $return .= '</tr>';
  6070. $return .= '</table>';
  6071. if ($action == 'move') {
  6072. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6073. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6074. }
  6075. if (is_numeric($extra_info)) {
  6076. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6077. } elseif (is_array($extra_info)) {
  6078. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6079. }
  6080. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  6081. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6082. $return .= '</form>';
  6083. $return .= '</div>';
  6084. return $return;
  6085. }
  6086. /**
  6087. * Addition of Hotpotatoes tests
  6088. * @param string Action
  6089. * @param integer Internal ID of the item
  6090. * @param mixed Extra information - can be an array with title and description indexes
  6091. * @return string HTML structure to display the hotpotatoes addition formular
  6092. */
  6093. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6094. {
  6095. $course_id = api_get_course_int_id();
  6096. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6097. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6098. if ($id != 0 && is_array($extra_info)) {
  6099. $item_title = stripslashes($extra_info['title']);
  6100. $item_description = stripslashes($extra_info['description']);
  6101. } elseif (is_numeric($extra_info)) {
  6102. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6103. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6104. WHERE
  6105. c_id = ".$course_id." AND
  6106. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6107. id = " . (int) $extra_info . "
  6108. ORDER BY id ASC";
  6109. $res_hot = Database::query($sql);
  6110. $row = Database::fetch_array($res_hot);
  6111. $item_title = $row['title'];
  6112. $item_description = $row['description'];
  6113. if (!empty ($row['comment'])) {
  6114. $item_title = $row['comment'];
  6115. }
  6116. } else {
  6117. $item_title = '';
  6118. $item_description = '';
  6119. }
  6120. if ($id != 0 && is_array($extra_info)) {
  6121. $parent = $extra_info['parent_item_id'];
  6122. } else {
  6123. $parent = 0;
  6124. }
  6125. $sql = "SELECT * FROM $tbl_lp_item
  6126. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6127. $result = Database::query($sql);
  6128. $arrLP = array ();
  6129. while ($row = Database :: fetch_array($result)) {
  6130. $arrLP[] = array (
  6131. 'id' => $row['id'],
  6132. 'item_type' => $row['item_type'],
  6133. 'title' => $row['title'],
  6134. 'path' => $row['path'],
  6135. 'description' => $row['description'],
  6136. 'parent_item_id' => $row['parent_item_id'],
  6137. 'previous_item_id' => $row['previous_item_id'],
  6138. 'next_item_id' => $row['next_item_id'],
  6139. 'display_order' => $row['display_order'],
  6140. 'max_score' => $row['max_score'],
  6141. 'min_score' => $row['min_score'],
  6142. 'mastery_score' => $row['mastery_score'],
  6143. 'prerequisite' => $row['prerequisite'],
  6144. 'max_time_allowed' => $row['max_time_allowed']
  6145. );
  6146. }
  6147. $legend = '<legend>';
  6148. if ($action == 'add')
  6149. $legend .= get_lang('CreateTheExercise');
  6150. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6151. else
  6152. $legend .= get_lang('EditCurrentExecice');
  6153. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6154. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6155. }
  6156. $legend .= '</legend>';
  6157. $return = '<form method="POST">';
  6158. $return .= $legend;
  6159. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6160. $return .= '<tr>';
  6161. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6162. $return .= '<td class="input">';
  6163. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6164. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6165. $arrHide = array (
  6166. $id
  6167. );
  6168. if (count($arrLP) > 0) {
  6169. for ($i = 0; $i < count($arrLP); $i++) {
  6170. if ($action != 'add') {
  6171. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6172. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6173. } else {
  6174. $arrHide[] = $arrLP[$i]['id'];
  6175. }
  6176. } else {
  6177. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6178. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6179. }
  6180. }
  6181. reset($arrLP);
  6182. }
  6183. $return .= '</select>';
  6184. $return .= '</td>';
  6185. $return .= '</tr>';
  6186. $return .= '<tr>';
  6187. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6188. $return .= '<td class="input">';
  6189. $return .= '<select id="previous" name="previous" size="1">';
  6190. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6191. for ($i = 0; $i < count($arrLP); $i++) {
  6192. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6193. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6194. $selected = 'selected="selected" ';
  6195. elseif ($action == 'add') $selected = 'selected="selected" ';
  6196. else
  6197. $selected = '';
  6198. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6199. }
  6200. }
  6201. $return .= '</select>';
  6202. $return .= '</td>';
  6203. $return .= '</tr>';
  6204. if ($action != 'move') {
  6205. $return .= '<tr>';
  6206. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6207. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6208. $return .= '</tr>';
  6209. $id_prerequisite = 0;
  6210. if (is_array($arrLP) && count($arrLP) > 0) {
  6211. foreach ($arrLP as $key => $value) {
  6212. if ($value['id'] == $id) {
  6213. $id_prerequisite = $value['prerequisite'];
  6214. break;
  6215. }
  6216. }
  6217. $arrHide = array ();
  6218. for ($i = 0; $i < count($arrLP); $i++) {
  6219. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6220. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6221. $s_selected_position = $arrLP[$i]['id'];
  6222. elseif ($action == 'add') $s_selected_position = 0;
  6223. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6224. }
  6225. }
  6226. }
  6227. }
  6228. $return .= '<tr>';
  6229. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6230. $return .= '</tr>';
  6231. $return .= '</table>';
  6232. if ($action == 'move') {
  6233. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6234. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6235. }
  6236. if (is_numeric($extra_info)) {
  6237. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6238. } elseif (is_array($extra_info)) {
  6239. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6240. }
  6241. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6242. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6243. $return .= '</form>';
  6244. return $return;
  6245. }
  6246. /**
  6247. * Return the form to display the forum edit/add option
  6248. * @param string Action (add/edit)
  6249. * @param integer ID of the lp_item if already exists
  6250. * @param mixed Forum ID or title
  6251. * @return string HTML form
  6252. */
  6253. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6254. {
  6255. $course_id = api_get_course_int_id();
  6256. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6257. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6258. if ($id != 0 && is_array($extra_info)) {
  6259. $item_title = stripslashes($extra_info['title']);
  6260. } elseif (is_numeric($extra_info)) {
  6261. $sql = "SELECT forum_title as title, forum_comment as comment
  6262. FROM " . $tbl_forum . "
  6263. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6264. $result = Database::query($sql);
  6265. $row = Database :: fetch_array($result);
  6266. $item_title = $row['title'];
  6267. $item_description = $row['comment'];
  6268. } else {
  6269. $item_title = '';
  6270. $item_description = '';
  6271. }
  6272. $legend = '<legend>';
  6273. if ($id != 0 && is_array($extra_info)) {
  6274. $parent = $extra_info['parent_item_id'];
  6275. } else {
  6276. $parent = 0;
  6277. }
  6278. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6279. WHERE
  6280. c_id = ".$course_id." AND
  6281. lp_id = " . $this->lp_id;
  6282. $result = Database::query($sql);
  6283. $arrLP = array();
  6284. while ($row = Database :: fetch_array($result)) {
  6285. $arrLP[] = array (
  6286. 'id' => $row['id'],
  6287. 'item_type' => $row['item_type'],
  6288. 'title' => $row['title'],
  6289. 'path' => $row['path'],
  6290. 'description' => $row['description'],
  6291. 'parent_item_id' => $row['parent_item_id'],
  6292. 'previous_item_id' => $row['previous_item_id'],
  6293. 'next_item_id' => $row['next_item_id'],
  6294. 'display_order' => $row['display_order'],
  6295. 'max_score' => $row['max_score'],
  6296. 'min_score' => $row['min_score'],
  6297. 'mastery_score' => $row['mastery_score'],
  6298. 'prerequisite' => $row['prerequisite']
  6299. );
  6300. }
  6301. $this->tree_array($arrLP);
  6302. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6303. unset($this->arrMenu);
  6304. if ($action == 'add')
  6305. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6306. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6307. else
  6308. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6309. $legend .= '</legend>';
  6310. $return = '<div class="sectioncomment">';
  6311. $return .= '<form method="POST">';
  6312. $return .= $legend;
  6313. $return .= '<table class="lp_form">';
  6314. if ($action != 'move') {
  6315. $return .= '<tr>';
  6316. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6317. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6318. $return .= '</tr>';
  6319. }
  6320. $return .= '<tr>';
  6321. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6322. $return .= '<td class="input">';
  6323. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6324. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6325. $arrHide = array(
  6326. $id
  6327. );
  6328. //$parent_item_id = $_SESSION['parent_item_id'];
  6329. for ($i = 0; $i < count($arrLP); $i++) {
  6330. if ($action != 'add') {
  6331. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6332. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6333. } else {
  6334. $arrHide[] = $arrLP[$i]['id'];
  6335. }
  6336. } else {
  6337. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6338. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6339. }
  6340. }
  6341. if (is_array($arrLP)) {
  6342. reset($arrLP);
  6343. }
  6344. $return .= '</select>';
  6345. $return .= '</td>';
  6346. $return .= '</tr>';
  6347. $return .= '<tr>';
  6348. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6349. $return .= '<td class="input">';
  6350. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6351. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6352. for ($i = 0; $i < count($arrLP); $i++) {
  6353. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6354. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6355. $selected = 'selected="selected" ';
  6356. elseif ($action == 'add') $selected = 'selected="selected" ';
  6357. else
  6358. $selected = '';
  6359. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6360. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6361. }
  6362. }
  6363. $return .= '</select>';
  6364. $return .= '</td>';
  6365. $return .= '</tr>';
  6366. if ($action != 'move') {
  6367. $return .= '<tr>';
  6368. $return .= '</tr>';
  6369. $id_prerequisite = 0;
  6370. if (is_array($arrLP)) {
  6371. foreach ($arrLP as $key => $value) {
  6372. if ($value['id'] == $id) {
  6373. $id_prerequisite = $value['prerequisite'];
  6374. break;
  6375. }
  6376. }
  6377. }
  6378. $arrHide = array();
  6379. for ($i = 0; $i < count($arrLP); $i++) {
  6380. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6381. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6382. $s_selected_position = $arrLP[$i]['id'];
  6383. elseif ($action == 'add') $s_selected_position = 0;
  6384. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6385. }
  6386. }
  6387. $return .= '</tr>';
  6388. }
  6389. $return .= '<tr>';
  6390. if ($action == 'add') {
  6391. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6392. } else {
  6393. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6394. }
  6395. $return .= '</tr>';
  6396. $return .= '</table>';
  6397. if ($action == 'move') {
  6398. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6399. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6400. }
  6401. if (is_numeric($extra_info)) {
  6402. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6403. } elseif (is_array($extra_info)) {
  6404. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6405. }
  6406. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6407. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6408. $return .= '</form>';
  6409. $return .= '</div>';
  6410. return $return;
  6411. }
  6412. /**
  6413. * Return HTML form to add/edit forum threads
  6414. * @param string Action (add/edit)
  6415. * @param integer Item ID if already exists in learning path
  6416. * @param mixed Extra information (thread ID if integer)
  6417. * @return string HTML form
  6418. */
  6419. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6420. {
  6421. $course_id = api_get_course_int_id();
  6422. if (empty($course_id)) {
  6423. return null;
  6424. }
  6425. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6426. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6427. if ($id != 0 && is_array($extra_info)) {
  6428. $item_title = stripslashes($extra_info['title']);
  6429. } elseif (is_numeric($extra_info)) {
  6430. $sql = "SELECT thread_title as title FROM $tbl_forum
  6431. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6432. $result = Database::query($sql);
  6433. $row = Database :: fetch_array($result);
  6434. $item_title = $row['title'];
  6435. $item_description = '';
  6436. } else {
  6437. $item_title = '';
  6438. $item_description = '';
  6439. }
  6440. $return = null;
  6441. if ($id != 0 && is_array($extra_info)) {
  6442. $parent = $extra_info['parent_item_id'];
  6443. } else {
  6444. $parent = 0;
  6445. }
  6446. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6447. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6448. $result = Database::query($sql);
  6449. $arrLP = array ();
  6450. while ($row = Database :: fetch_array($result)) {
  6451. $arrLP[] = array (
  6452. 'id' => $row['id'],
  6453. 'item_type' => $row['item_type'],
  6454. 'title' => $row['title'],
  6455. 'path' => $row['path'],
  6456. 'description' => $row['description'],
  6457. 'parent_item_id' => $row['parent_item_id'],
  6458. 'previous_item_id' => $row['previous_item_id'],
  6459. 'next_item_id' => $row['next_item_id'],
  6460. 'display_order' => $row['display_order'],
  6461. 'max_score' => $row['max_score'],
  6462. 'min_score' => $row['min_score'],
  6463. 'mastery_score' => $row['mastery_score'],
  6464. 'prerequisite' => $row['prerequisite']
  6465. );
  6466. }
  6467. $this->tree_array($arrLP);
  6468. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6469. unset ($this->arrMenu);
  6470. $return .= '<form method="POST">';
  6471. if ($action == 'add')
  6472. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6473. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6474. else
  6475. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6476. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6477. $return .= '<tr>';
  6478. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6479. $return .= '<td class="input">';
  6480. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6481. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6482. $arrHide = array (
  6483. $id
  6484. );
  6485. for ($i = 0; $i < count($arrLP); $i++) {
  6486. if ($action != 'add') {
  6487. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6488. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6489. } else {
  6490. $arrHide[] = $arrLP[$i]['id'];
  6491. }
  6492. } else {
  6493. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6494. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6495. }
  6496. }
  6497. if ($arrLP != null) {
  6498. reset($arrLP);
  6499. }
  6500. $return .= '</select>';
  6501. $return .= '</td>';
  6502. $return .= '</tr>';
  6503. $return .= '<tr>';
  6504. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6505. $return .= '<td class="input">';
  6506. $return .= '<select id="previous" name="previous" size="1">';
  6507. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6508. for ($i = 0; $i < count($arrLP); $i++) {
  6509. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6510. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6511. $selected = 'selected="selected" ';
  6512. elseif ($action == 'add') $selected = 'selected="selected" ';
  6513. else
  6514. $selected = '';
  6515. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6516. }
  6517. }
  6518. $return .= '</select>';
  6519. $return .= '</td>';
  6520. $return .= '</tr>';
  6521. if ($action != 'move') {
  6522. $return .= '<tr>';
  6523. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6524. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6525. $return .= '</tr>';
  6526. $return .= '<tr>';
  6527. $return .= '</tr>';
  6528. $id_prerequisite = 0;
  6529. if ($arrLP != null) {
  6530. foreach ($arrLP as $key => $value) {
  6531. if ($value['id'] == $id) {
  6532. $id_prerequisite = $value['prerequisite'];
  6533. break;
  6534. }
  6535. }
  6536. }
  6537. $arrHide = array();
  6538. for ($i = 0; $i < count($arrLP); $i++) {
  6539. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6540. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6541. $s_selected_position = $arrLP[$i]['id'];
  6542. elseif ($action == 'add') $s_selected_position = 0;
  6543. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6544. }
  6545. }
  6546. $return .= '<tr>';
  6547. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6548. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6549. foreach ($arrHide as $key => $value) {
  6550. if ($key == $s_selected_position && $action == 'add') {
  6551. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6552. }
  6553. elseif ($key == $id_prerequisite && $action == 'edit') {
  6554. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6555. } else {
  6556. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6557. }
  6558. }
  6559. $return .= "</select></td>";
  6560. $return .= '</tr>';
  6561. }
  6562. $return .= '<tr>';
  6563. $return .= '<td></td><td>
  6564. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6565. $return .= '</tr>';
  6566. $return .= '</table>';
  6567. if ($action == 'move') {
  6568. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6569. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6570. }
  6571. if (is_numeric($extra_info)) {
  6572. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6573. }
  6574. elseif (is_array($extra_info)) {
  6575. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6576. }
  6577. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6578. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6579. $return .= '</form>';
  6580. $return .= '</div>';
  6581. return $return;
  6582. }
  6583. /**
  6584. * Return the HTML form to display an item (generally a section/module item)
  6585. * @param string Item type (module/dokeos_module)
  6586. * @param string Title (optional, only when creating)
  6587. * @param string Action ('add'/'edit')
  6588. * @param integer lp_item ID
  6589. * @param mixed Extra info
  6590. * @return string HTML form
  6591. */
  6592. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6593. {
  6594. $course_id = api_get_course_int_id();
  6595. $_course = api_get_course_info();
  6596. global $charset;
  6597. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6598. if ($id != 0 && is_array($extra_info)) {
  6599. $item_title = $extra_info['title'];
  6600. $item_description = $extra_info['description'];
  6601. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6602. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6603. } else {
  6604. $item_title = '';
  6605. $item_description = '';
  6606. $item_path_fck = '';
  6607. }
  6608. if ($id != 0 && is_array($extra_info)) {
  6609. $parent = $extra_info['parent_item_id'];
  6610. } else {
  6611. $parent = 0;
  6612. }
  6613. $id = intval($id);
  6614. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6615. WHERE
  6616. c_id = ".$course_id." AND
  6617. lp_id = " . $this->lp_id . " AND
  6618. id != $id";
  6619. if ($item_type == 'module')
  6620. $sql .= " AND parent_item_id = 0";
  6621. $result = Database::query($sql);
  6622. $arrLP = array ();
  6623. while ($row = Database :: fetch_array($result)) {
  6624. $arrLP[] = array(
  6625. 'id' => $row['id'],
  6626. 'item_type' => $row['item_type'],
  6627. 'title' => $row['title'],
  6628. 'path' => $row['path'],
  6629. 'description' => $row['description'],
  6630. 'parent_item_id' => $row['parent_item_id'],
  6631. 'previous_item_id' => $row['previous_item_id'],
  6632. 'next_item_id' => $row['next_item_id'],
  6633. 'max_score' => $row['max_score'],
  6634. 'min_score' => $row['min_score'],
  6635. 'mastery_score' => $row['mastery_score'],
  6636. 'prerequisite' => $row['prerequisite'],
  6637. 'display_order' => $row['display_order']
  6638. );
  6639. }
  6640. $this->tree_array($arrLP);
  6641. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6642. unset ($this->arrMenu);
  6643. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6644. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6645. $form = new FormValidator('form', 'POST', $url);
  6646. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6647. $defaults['description'] = $item_description;
  6648. $form->addElement('header', $title);
  6649. //$arrHide = array($id);
  6650. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6651. $arrHide[0]['padding'] = 3;
  6652. $charset = api_get_system_encoding();
  6653. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6654. for ($i = 0; $i < count($arrLP); $i++) {
  6655. if ($action != 'add') {
  6656. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6657. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6658. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6659. if ($parent == $arrLP[$i]['id']) {
  6660. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6661. }
  6662. }
  6663. } else {
  6664. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6665. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6666. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6667. if ($parent == $arrLP[$i]['id']) {
  6668. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6669. }
  6670. }
  6671. }
  6672. }
  6673. if ($action != 'move') {
  6674. $form->addElement('text', 'title', get_lang('Title'));
  6675. $form->applyFilter('title', 'html_filter');
  6676. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6677. } else {
  6678. $form->addElement('hidden', 'title');
  6679. }
  6680. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6681. foreach ($arrHide as $key => $value) {
  6682. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6683. }
  6684. if (!empty($s_selected_parent)) {
  6685. $parent_select->setSelected($s_selected_parent);
  6686. }
  6687. }
  6688. if (is_array($arrLP)) {
  6689. reset($arrLP);
  6690. }
  6691. $arrHide = array();
  6692. // POSITION
  6693. for ($i = 0; $i < count($arrLP); $i++) {
  6694. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6695. //this is the same!
  6696. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6697. $s_selected_position = $arrLP[$i]['id'];
  6698. } elseif ($action == 'add') {
  6699. $s_selected_position = $arrLP[$i]['id'];
  6700. }
  6701. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6702. }
  6703. }
  6704. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6705. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6706. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6707. foreach ($arrHide as $key => $value) {
  6708. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6709. }
  6710. if (!empty ($s_selected_position)) {
  6711. $position->setSelected($s_selected_position);
  6712. }
  6713. if (is_array($arrLP)) {
  6714. reset($arrLP);
  6715. }
  6716. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6717. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6718. $form->addElement('hidden', 'parent', '0');
  6719. }
  6720. //fix in order to use the tab
  6721. if ($item_type == 'chapter') {
  6722. $form->addElement('hidden', 'type', 'chapter');
  6723. }
  6724. $extension = null;
  6725. if (!empty($item_path)) {
  6726. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6727. }
  6728. //assets can't be modified
  6729. //$item_type == 'asset' ||
  6730. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6731. if ($item_type == 'sco') {
  6732. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6733. }
  6734. $renderer = $form->defaultRenderer();
  6735. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6736. $relative_prefix = '';
  6737. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6738. 'Width' => '100%',
  6739. 'Height' => '500',
  6740. 'FullPage' => true,
  6741. 'CreateDocumentDir' => $relative_prefix,
  6742. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6743. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6744. );
  6745. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6746. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6747. //$defaults['content_lp'] = file_get_contents($item_path);
  6748. $defaults['content_lp'] = file_get_contents($content_path);
  6749. }
  6750. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6751. $form->addElement('hidden', 'post_time', time());
  6752. $form->setDefaults($defaults);
  6753. return $form->return_form();
  6754. }
  6755. /**
  6756. * Returns the form to update or create a document
  6757. * @param string Action (add/edit)
  6758. * @param integer ID of the lp_item (if already exists)
  6759. * @param mixed Integer if document ID, string if info ('new')
  6760. * @return string HTML form
  6761. */
  6762. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6763. {
  6764. $course_id = api_get_course_int_id();
  6765. $_course = api_get_course_info();
  6766. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6767. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6768. $no_display_edit_textarea = false;
  6769. $item_description = '';
  6770. //If action==edit document
  6771. //We don't display the document form if it's not an editable document (html or txt file)
  6772. if ($action == "edit") {
  6773. if (is_array($extra_info)) {
  6774. $path_parts = pathinfo($extra_info['dir']);
  6775. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6776. $no_display_edit_textarea = true;
  6777. }
  6778. }
  6779. }
  6780. $no_display_add = false;
  6781. // If action==add an existing document
  6782. // We don't display the document form if it's not an editable document (html or txt file).
  6783. if ($action == "add") {
  6784. if (is_numeric($extra_info)) {
  6785. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6786. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6787. $result = Database::query($sql_doc);
  6788. $path_file = Database :: result($result, 0, 0);
  6789. $path_parts = pathinfo($path_file);
  6790. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6791. $no_display_add = true;
  6792. }
  6793. }
  6794. }
  6795. if ($id != 0 && is_array($extra_info)) {
  6796. $item_title = stripslashes($extra_info['title']);
  6797. $item_description = stripslashes($extra_info['description']);
  6798. $item_terms = stripslashes($extra_info['terms']);
  6799. if (empty ($item_title)) {
  6800. $path_parts = pathinfo($extra_info['path']);
  6801. $item_title = stripslashes($path_parts['filename']);
  6802. }
  6803. } elseif (is_numeric($extra_info)) {
  6804. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6805. WHERE
  6806. c_id = ".$course_id." AND
  6807. id = " . intval($extra_info);
  6808. $result = Database::query($sql_doc);
  6809. $row = Database::fetch_array($result);
  6810. $item_title = $row['title'];
  6811. $item_title = str_replace('_', ' ', $item_title);
  6812. if (empty ($item_title)) {
  6813. $path_parts = pathinfo($row['path']);
  6814. $item_title = stripslashes($path_parts['filename']);
  6815. }
  6816. } else {
  6817. $item_title = '';
  6818. $item_description = '';
  6819. }
  6820. $return = '<legend>';
  6821. if ($id != 0 && is_array($extra_info)) {
  6822. $parent = $extra_info['parent_item_id'];
  6823. } else {
  6824. $parent = 0;
  6825. }
  6826. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6827. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6828. $result = Database::query($sql);
  6829. $arrLP = array ();
  6830. while ($row = Database :: fetch_array($result)) {
  6831. $arrLP[] = array(
  6832. 'id' => $row['id'],
  6833. 'item_type' => $row['item_type'],
  6834. 'title' => $row['title'],
  6835. 'path' => $row['path'],
  6836. 'description' => $row['description'],
  6837. 'parent_item_id' => $row['parent_item_id'],
  6838. 'previous_item_id' => $row['previous_item_id'],
  6839. 'next_item_id' => $row['next_item_id'],
  6840. 'display_order' => $row['display_order'],
  6841. 'max_score' => $row['max_score'],
  6842. 'min_score' => $row['min_score'],
  6843. 'mastery_score' => $row['mastery_score'],
  6844. 'prerequisite' => $row['prerequisite']
  6845. );
  6846. }
  6847. $this->tree_array($arrLP);
  6848. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6849. unset ($this->arrMenu);
  6850. if ($action == 'add') {
  6851. $return .= get_lang('CreateTheDocument');
  6852. } elseif ($action == 'move') {
  6853. $return .= get_lang('MoveTheCurrentDocument');
  6854. } else {
  6855. $return .= get_lang('EditTheCurrentDocument');
  6856. }
  6857. $return .= '</legend>';
  6858. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6859. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6860. }
  6861. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6862. $defaults['title'] = Security :: remove_XSS($item_title);
  6863. if (empty($item_title)) {
  6864. $defaults['title'] = Security::remove_XSS($item_title);
  6865. }
  6866. $defaults['description'] = $item_description;
  6867. $form->addElement('html', $return);
  6868. if ($action != 'move') {
  6869. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6870. $form->applyFilter('title', 'html_filter');
  6871. }
  6872. $arrHide[0]['value'] = $this->name;
  6873. $arrHide[0]['padding'] = 3;
  6874. for ($i = 0; $i < count($arrLP); $i++) {
  6875. if ($action != 'add') {
  6876. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6877. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6878. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6879. if ($parent == $arrLP[$i]['id']) {
  6880. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6881. }
  6882. }
  6883. } else {
  6884. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6885. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6886. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6887. if ($parent == $arrLP[$i]['id']) {
  6888. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6889. }
  6890. }
  6891. }
  6892. }
  6893. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="form-control" id="idParent" " onchange="javascript: load_cbo(this.value);"');
  6894. $my_count=0;
  6895. foreach ($arrHide as $key => $value) {
  6896. if ($my_count!=0) {
  6897. // The LP name is also the first section and is not in the same charset like the other sections.
  6898. $value['value'] = Security :: remove_XSS($value['value']);
  6899. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6900. } else {
  6901. $value['value'] = Security :: remove_XSS($value['value']);
  6902. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6903. }
  6904. $my_count++;
  6905. }
  6906. if (!empty($id)) {
  6907. $parent_select->setSelected($parent);
  6908. } else {
  6909. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  6910. $parent_select->setSelected($parent_item_id);
  6911. }
  6912. if (is_array($arrLP)) {
  6913. reset($arrLP);
  6914. }
  6915. $arrHide = array();
  6916. $s_selected_position = null;
  6917. //POSITION
  6918. for ($i = 0; $i < count($arrLP); $i++) {
  6919. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6920. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6921. $s_selected_position = $arrLP[$i]['id'];
  6922. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6923. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6924. }
  6925. }
  6926. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="form-control"');
  6927. $position->addOption(get_lang('FirstPosition'), 0);
  6928. foreach ($arrHide as $key => $value) {
  6929. $padding = isset($value['padding']) ? $value['padding']: 0;
  6930. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6931. }
  6932. $position->setSelected($s_selected_position);
  6933. if (is_array($arrLP)) {
  6934. reset($arrLP);
  6935. }
  6936. if ($action != 'move') {
  6937. $id_prerequisite = 0;
  6938. if (is_array($arrLP)) {
  6939. foreach ($arrLP as $key => $value) {
  6940. if ($value['id'] == $id) {
  6941. $id_prerequisite = $value['prerequisite'];
  6942. break;
  6943. }
  6944. }
  6945. }
  6946. $arrHide = array();
  6947. for ($i = 0; $i < count($arrLP); $i++) {
  6948. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6949. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6950. $s_selected_position = $arrLP[$i]['id'];
  6951. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6952. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6953. }
  6954. }
  6955. if (!$no_display_add) {
  6956. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  6957. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  6958. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $edit == 'true')) {
  6959. if (isset ($_POST['content']))
  6960. $content = stripslashes($_POST['content']);
  6961. elseif (is_array($extra_info)) {
  6962. //If it's an html document or a text file
  6963. if (!$no_display_edit_textarea) {
  6964. $content = $this->display_document($extra_info['path'], false, false);
  6965. }
  6966. } elseif (is_numeric($extra_info))
  6967. $content = $this->display_document($extra_info, false, false);
  6968. else
  6969. $content = '';
  6970. if (!$no_display_edit_textarea) {
  6971. // We need to calculate here some specific settings for the online editor.
  6972. // The calculated settings work for documents in the Documents tool
  6973. // (on the root or in subfolders).
  6974. // For documents in native scorm packages it is unclear whether the
  6975. // online editor should be activated or not.
  6976. // A new document, it is in the root of the repository.
  6977. $relative_path = '';
  6978. $relative_prefix = '';
  6979. if (is_array($extra_info) && $extra_info != 'new') {
  6980. // The document already exists. Whe have to determine its relative path towards the repository root.
  6981. $relative_path = explode('/', $extra_info['dir']);
  6982. $cnt = count($relative_path) - 2;
  6983. if ($cnt < 0) {
  6984. $cnt = 0;
  6985. }
  6986. $relative_prefix = str_repeat('../', $cnt);
  6987. $relative_path = array_slice($relative_path, 1, $cnt);
  6988. $relative_path = implode('/', $relative_path);
  6989. if (strlen($relative_path) > 0) {
  6990. $relative_path = $relative_path . '/';
  6991. }
  6992. } else {
  6993. $result = $this->generate_lp_folder($_course);
  6994. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6995. $relative_prefix = '../../';
  6996. }
  6997. $editor_config = array(
  6998. 'ToolbarSet'=> 'LearningPathDocuments',
  6999. 'Width' => '100%',
  7000. 'Height' => '500',
  7001. 'FullPage' => true,
  7002. 'CreateDocumentDir' => $relative_prefix,
  7003. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  7004. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  7005. );
  7006. if ($_GET['action'] == 'add_item') {
  7007. $class = 'add';
  7008. $text = get_lang('LPCreateDocument');
  7009. } else {
  7010. if ($_GET['action'] == 'edit_item') {
  7011. $class = 'save';
  7012. $text = get_lang('SaveDocument');
  7013. }
  7014. }
  7015. $form->addButtonSave($text, 'submit_button');
  7016. $renderer = $form->defaultRenderer();
  7017. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7018. $form->addElement('html', '<div class="editor-lp">');
  7019. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7020. $form->addElement('html', '</div>');
  7021. $defaults['content_lp'] = $content;
  7022. }
  7023. } elseif (is_numeric($extra_info)) {
  7024. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7025. $return = $this->display_document($extra_info, true, true, true);
  7026. $form->addElement('html', $return);
  7027. }
  7028. }
  7029. }
  7030. if ($action == 'move') {
  7031. $form->addElement('hidden', 'title', $item_title);
  7032. $form->addElement('hidden', 'description', $item_description);
  7033. }
  7034. if (is_numeric($extra_info)) {
  7035. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7036. $form->addElement('hidden', 'path', $extra_info);
  7037. } elseif (is_array($extra_info)) {
  7038. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7039. $form->addElement('hidden', 'path', $extra_info['path']);
  7040. }
  7041. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7042. $form->addElement('hidden', 'post_time', time());
  7043. $form->setDefaults($defaults);
  7044. return $form->return_form();
  7045. }
  7046. /**
  7047. * Return HTML form to add/edit a link item
  7048. * @param string $action (add/edit)
  7049. * @param integer $id Item ID if exists
  7050. * @param mixed $extra_info
  7051. * @return string HTML form
  7052. */
  7053. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7054. {
  7055. $course_id = api_get_course_int_id();
  7056. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7057. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7058. if ($id != 0 && is_array($extra_info)) {
  7059. $item_title = stripslashes($extra_info['title']);
  7060. $item_description = stripslashes($extra_info['description']);
  7061. $item_url = stripslashes($extra_info['url']);
  7062. } elseif (is_numeric($extra_info)) {
  7063. $extra_info = intval($extra_info);
  7064. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7065. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7066. $result = Database::query($sql);
  7067. $row = Database :: fetch_array($result);
  7068. $item_title = $row['title'];
  7069. $item_description = $row['description'];
  7070. $item_url = $row['url'];
  7071. } else {
  7072. $item_title = '';
  7073. $item_description = '';
  7074. $item_url = '';
  7075. }
  7076. $legend = '<legend>';
  7077. if ($id != 0 && is_array($extra_info)) {
  7078. $parent = $extra_info['parent_item_id'];
  7079. } else {
  7080. $parent = 0;
  7081. }
  7082. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7083. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7084. $result = Database::query($sql);
  7085. $arrLP = array();
  7086. while ($row = Database :: fetch_array($result)) {
  7087. $arrLP[] = array(
  7088. 'id' => $row['id'],
  7089. 'item_type' => $row['item_type'],
  7090. 'title' => $row['title'],
  7091. 'path' => $row['path'],
  7092. 'description' => $row['description'],
  7093. 'parent_item_id' => $row['parent_item_id'],
  7094. 'previous_item_id' => $row['previous_item_id'],
  7095. 'next_item_id' => $row['next_item_id'],
  7096. 'display_order' => $row['display_order'],
  7097. 'max_score' => $row['max_score'],
  7098. 'min_score' => $row['min_score'],
  7099. 'mastery_score' => $row['mastery_score'],
  7100. 'prerequisite' => $row['prerequisite']
  7101. );
  7102. }
  7103. $this->tree_array($arrLP);
  7104. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7105. unset ($this->arrMenu);
  7106. if ($action == 'add')
  7107. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  7108. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  7109. else
  7110. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7111. $legend .= '</legend>';
  7112. $return = '<div class="sectioncomment">';
  7113. $return .= '<form method="POST">';
  7114. $return .= $legend;
  7115. $return .= '<table>';
  7116. if ($action != 'move') {
  7117. $return .= '<tr>';
  7118. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7119. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7120. $return .= '</tr>';
  7121. }
  7122. $return .= '<tr>';
  7123. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7124. $return .= '<td class="input">';
  7125. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7126. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7127. $arrHide = array(
  7128. $id
  7129. );
  7130. $parent_item_id = $_SESSION['parent_item_id'];
  7131. for ($i = 0; $i < count($arrLP); $i++) {
  7132. if ($action != 'add') {
  7133. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7134. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7135. } else {
  7136. $arrHide[] = $arrLP[$i]['id'];
  7137. }
  7138. } else {
  7139. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7140. $return .= '<option ' . (($parent_item_id == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7141. }
  7142. }
  7143. if (is_array($arrLP)) {
  7144. reset($arrLP);
  7145. }
  7146. $return .= '</select>';
  7147. $return .= '</td>';
  7148. $return .= '</tr>';
  7149. $return .= '<tr>';
  7150. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7151. $return .= '<td class="input">';
  7152. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7153. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7154. for ($i = 0; $i < count($arrLP); $i++) {
  7155. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7156. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7157. $selected = 'selected="selected" ';
  7158. elseif ($action == 'add')
  7159. $selected = 'selected="selected" ';
  7160. else
  7161. $selected = '';
  7162. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7163. }
  7164. }
  7165. $return .= '</select>';
  7166. $return .= '</td>';
  7167. $return .= '</tr>';
  7168. if ($action != 'move') {
  7169. $return .= '<tr>';
  7170. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7171. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  7172. $return .= '</tr>';
  7173. $id_prerequisite = 0;
  7174. if (is_array($arrLP)) {
  7175. foreach ($arrLP as $key => $value) {
  7176. if ($value['id'] == $id) {
  7177. $id_prerequisite = $value['prerequisite'];
  7178. break;
  7179. }
  7180. }
  7181. }
  7182. $arrHide = array();
  7183. for ($i = 0; $i < count($arrLP); $i++) {
  7184. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7185. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7186. $s_selected_position = $arrLP[$i]['id'];
  7187. elseif ($action == 'add') $s_selected_position = 0;
  7188. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7189. }
  7190. }
  7191. $return .= '</tr>';
  7192. }
  7193. $return .= '<tr>';
  7194. if ($action == 'add') {
  7195. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7196. } else {
  7197. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7198. }
  7199. $return .= '</tr>';
  7200. $return .= '</table>';
  7201. if ($action == 'move') {
  7202. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7203. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7204. }
  7205. if (is_numeric($extra_info)) {
  7206. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7207. } elseif (is_array($extra_info)) {
  7208. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7209. }
  7210. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7211. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7212. $return .= '</form>';
  7213. $return .= '</div>';
  7214. return $return;
  7215. }
  7216. /**
  7217. * Return HTML form to add/edit a student publication (work)
  7218. * @param string Action (add/edit)
  7219. * @param integer Item ID if already exists
  7220. * @param mixed Extra info (work ID if integer)
  7221. * @return string HTML form
  7222. */
  7223. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7224. {
  7225. $course_id = api_get_course_int_id();
  7226. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7227. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7228. if ($id != 0 && is_array($extra_info)) {
  7229. $item_title = stripslashes($extra_info['title']);
  7230. $item_description = stripslashes($extra_info['description']);
  7231. } elseif (is_numeric($extra_info)) {
  7232. $extra_info = intval($extra_info);
  7233. $sql = "SELECT title, description
  7234. FROM " . $tbl_publication . "
  7235. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7236. $result = Database::query($sql);
  7237. $row = Database :: fetch_array($result);
  7238. $item_title = $row['title'];
  7239. } else {
  7240. $item_title = get_lang('Student_publication');
  7241. }
  7242. if ($id != 0 && is_array($extra_info)) {
  7243. $parent = $extra_info['parent_item_id'];
  7244. } else {
  7245. $parent = 0;
  7246. }
  7247. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7248. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7249. $result = Database::query($sql);
  7250. $arrLP = array();
  7251. while ($row = Database :: fetch_array($result)) {
  7252. $arrLP[] = array (
  7253. 'id' => $row['id'],
  7254. 'item_type' => $row['item_type'],
  7255. 'title' => $row['title'],
  7256. 'path' => $row['path'],
  7257. 'description' => $row['description'],
  7258. 'parent_item_id' => $row['parent_item_id'],
  7259. 'previous_item_id' => $row['previous_item_id'],
  7260. 'next_item_id' => $row['next_item_id'],
  7261. 'display_order' => $row['display_order'],
  7262. 'max_score' => $row['max_score'],
  7263. 'min_score' => $row['min_score'],
  7264. 'mastery_score' => $row['mastery_score'],
  7265. 'prerequisite' => $row['prerequisite']
  7266. );
  7267. }
  7268. $this->tree_array($arrLP);
  7269. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7270. unset ($this->arrMenu);
  7271. $form = new FormValidator('frm_student_publication', 'post', '#');
  7272. if ($action == 'add') {
  7273. $form->addHeader(get_lang('Student_publication'));
  7274. } elseif ($action == 'move') {
  7275. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7276. } else {
  7277. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7278. }
  7279. if ($action != 'move') {
  7280. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7281. }
  7282. $parentSelect = $form->addSelect(
  7283. 'parent',
  7284. get_lang('Parent'),
  7285. ['0' => $this->name],
  7286. [
  7287. 'onchange' => 'javascript: load_cbo(this.value);',
  7288. 'class' => 'learnpath_item_form',
  7289. 'id' => 'idParent'
  7290. ]
  7291. );
  7292. $arrHide = array (
  7293. $id
  7294. );
  7295. for ($i = 0; $i < count($arrLP); $i++) {
  7296. if ($action != 'add') {
  7297. if (
  7298. (
  7299. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7300. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7301. $arrLP[$i]['item_type'] == 'dir'
  7302. ) &&
  7303. !in_array($arrLP[$i]['id'], $arrHide) &&
  7304. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7305. ) {
  7306. $parentSelect->addOption(
  7307. $arrLP[$i]['title'],
  7308. $arrLP[$i]['id'],
  7309. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7310. );
  7311. if ($parent == $arrLP[$i]['id']) {
  7312. $parentSelect->setSelected($arrLP[$i]['id']);
  7313. }
  7314. } else {
  7315. $arrHide[] = $arrLP[$i]['id'];
  7316. }
  7317. } else {
  7318. if (
  7319. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7320. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7321. ) {
  7322. $parentSelect->addOption(
  7323. $arrLP[$i]['title'],
  7324. $arrLP[$i]['id'],
  7325. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7326. );
  7327. if ($parent == $arrLP[$i]['id']) {
  7328. $parentSelect->setSelected($arrLP[$i]['id']);
  7329. }
  7330. }
  7331. }
  7332. }
  7333. if (is_array($arrLP)) {
  7334. reset($arrLP);
  7335. }
  7336. $previousSelect = $form->addSelect(
  7337. 'previous',
  7338. get_lang('Position'),
  7339. ['0' => get_lang('FirstPosition')],
  7340. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7341. );
  7342. for ($i = 0; $i < count($arrLP); $i++) {
  7343. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7344. $previousSelect->addOption(
  7345. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7346. $arrLP[$i]['id']
  7347. );
  7348. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7349. $previousSelect->setSelected($arrLP[$i]['id']);
  7350. } elseif ($action == 'add') {
  7351. $previousSelect->setSelected($arrLP[$i]['id']);
  7352. }
  7353. }
  7354. }
  7355. if ($action != 'move') {
  7356. $id_prerequisite = 0;
  7357. if (is_array($arrLP)) {
  7358. foreach ($arrLP as $key => $value) {
  7359. if ($value['id'] == $id) {
  7360. $id_prerequisite = $value['prerequisite'];
  7361. break;
  7362. }
  7363. }
  7364. }
  7365. $arrHide = array ();
  7366. for ($i = 0; $i < count($arrLP); $i++) {
  7367. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7368. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7369. $s_selected_position = $arrLP[$i]['id'];
  7370. elseif ($action == 'add') $s_selected_position = 0;
  7371. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7372. }
  7373. }
  7374. }
  7375. if ($action == 'add') {
  7376. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7377. } else {
  7378. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7379. }
  7380. if ($action == 'move') {
  7381. $form->addHidden('title', $item_title);
  7382. $form->addHidden('description', $item_description);
  7383. }
  7384. if (is_numeric($extra_info)) {
  7385. $form->addHidden('path', $extra_info);
  7386. } elseif (is_array($extra_info)) {
  7387. $form->addHidden('path', $extra_info['path']);
  7388. }
  7389. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7390. $form->addHidden('post_time', time());
  7391. $form->setDefaults(['title' => $item_title]);
  7392. $return = '<div class="sectioncomment">';
  7393. $return .= $form->returnForm();
  7394. $return .= '</div>';
  7395. return $return;
  7396. }
  7397. /**
  7398. * Displays the menu for manipulating a step
  7399. *
  7400. * @param $item_id
  7401. * @param string $item_type
  7402. * @return string
  7403. */
  7404. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7405. {
  7406. $_course = api_get_course_info();
  7407. $course_id = api_get_course_int_id();
  7408. $course_code = api_get_course_id();
  7409. $return = '<div class="actions">';
  7410. switch ($item_type) {
  7411. case 'dokeos_chapter' :
  7412. case 'chapter' :
  7413. // Commented the message cause should not show it.
  7414. //$lang = get_lang('TitleManipulateChapter');
  7415. break;
  7416. case 'dokeos_module' :
  7417. case 'module' :
  7418. // Commented the message cause should not show it.
  7419. //$lang = get_lang('TitleManipulateModule');
  7420. break;
  7421. case TOOL_DOCUMENT :
  7422. // Commented the message cause should not show it.
  7423. //$lang = get_lang('TitleManipulateDocument');
  7424. break;
  7425. case TOOL_LINK :
  7426. case 'link' :
  7427. // Commented the message cause should not show it.
  7428. //$lang = get_lang('TitleManipulateLink');
  7429. break;
  7430. case TOOL_QUIZ :
  7431. // Commented the message cause should not show it.
  7432. //$lang = get_lang('TitleManipulateQuiz');
  7433. break;
  7434. case TOOL_STUDENTPUBLICATION :
  7435. // Commented the message cause should not show it.
  7436. //$lang = get_lang('TitleManipulateStudentPublication');
  7437. break;
  7438. }
  7439. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7440. $item_id = intval($item_id);
  7441. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7442. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7443. $result = Database::query($sql);
  7444. $row = Database::fetch_assoc($result);
  7445. $audio_player = null;
  7446. // We display an audio player if needed.
  7447. if (!empty($row['audio'])) {
  7448. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7449. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7450. </div>';
  7451. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7452. $audio_player .= '<script>
  7453. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7454. s1.addParam("allowscriptaccess","always");
  7455. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7456. s1.write("container");
  7457. </script>';
  7458. }
  7459. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7460. $return .= Display::url(
  7461. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7462. $url.'&action=edit_item&path_item=' . $row['path']
  7463. );
  7464. $return .= Display::url(
  7465. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7466. $url.'&action=move_item'
  7467. );
  7468. // Commented for now as prerequisites cannot be added to chapters.
  7469. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7470. $return .= Display::url(
  7471. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7472. $url.'&action=edit_item_prereq'
  7473. );
  7474. }
  7475. $return .= Display::url(
  7476. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7477. $url.'&action=delete_item'
  7478. );
  7479. if ($item_type == TOOL_HOTPOTATOES ) {
  7480. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7481. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7482. }
  7483. if ($item_type == TOOL_DOCUMENT ) {
  7484. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7485. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7486. }
  7487. $return .= '</div>';
  7488. if (!empty($audio_player)) {
  7489. $return .= '<br />'.$audio_player;
  7490. }
  7491. return $return;
  7492. }
  7493. /**
  7494. * Creates the javascript needed for filling up the checkboxes without page reload
  7495. * @return string
  7496. */
  7497. public function get_js_dropdown_array()
  7498. {
  7499. $course_id = api_get_course_int_id();
  7500. $return = 'var child_name = new Array();' . "\n";
  7501. $return .= 'var child_value = new Array();' . "\n\n";
  7502. $return .= 'child_name[0] = new Array();' . "\n";
  7503. $return .= 'child_value[0] = new Array();' . "\n\n";
  7504. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7505. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7506. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7507. ORDER BY display_order ASC";
  7508. $res_zero = Database::query($sql_zero);
  7509. $i = 0;
  7510. while ($row_zero = Database :: fetch_array($res_zero)) {
  7511. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7512. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7513. }
  7514. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7515. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7516. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7517. }
  7518. $return .= "\n";
  7519. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7520. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7521. $res = Database::query($sql);
  7522. while ($row = Database :: fetch_array($res)) {
  7523. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7524. WHERE
  7525. c_id = ".$course_id." AND
  7526. parent_item_id = " . $row['id'] . "
  7527. ORDER BY display_order ASC";
  7528. $res_parent = Database::query($sql_parent);
  7529. $i = 0;
  7530. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7531. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7532. while ($row_parent = Database :: fetch_array($res_parent)) {
  7533. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7534. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7535. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7536. }
  7537. $return .= "\n";
  7538. }
  7539. return $return;
  7540. }
  7541. /**
  7542. * Display the form to allow moving an item
  7543. * @param integer Item ID
  7544. * @return string HTML form
  7545. */
  7546. public function display_move_item($item_id)
  7547. {
  7548. $course_id = api_get_course_int_id();
  7549. $return = '';
  7550. if (is_numeric($item_id)) {
  7551. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7552. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7553. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7554. $res = Database::query($sql);
  7555. $row = Database :: fetch_array($res);
  7556. switch ($row['item_type']) {
  7557. case 'dokeos_chapter' :
  7558. case 'dir' :
  7559. case 'asset' :
  7560. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7561. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7562. break;
  7563. case 'dokeos_module' :
  7564. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7565. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7566. break;
  7567. case TOOL_DOCUMENT :
  7568. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7569. $return .= $this->display_document_form('move', $item_id, $row);
  7570. break;
  7571. case TOOL_LINK :
  7572. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7573. $return .= $this->display_link_form('move', $item_id, $row);
  7574. break;
  7575. case TOOL_HOTPOTATOES :
  7576. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7577. $return .= $this->display_link_form('move', $item_id, $row);
  7578. break;
  7579. case TOOL_QUIZ :
  7580. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7581. $return .= $this->display_quiz_form('move', $item_id, $row);
  7582. break;
  7583. case TOOL_STUDENTPUBLICATION :
  7584. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7585. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7586. break;
  7587. case TOOL_FORUM :
  7588. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7589. $return .= $this->display_forum_form('move', $item_id, $row);
  7590. break;
  7591. case TOOL_THREAD :
  7592. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7593. $return .= $this->display_forum_form('move', $item_id, $row);
  7594. break;
  7595. }
  7596. }
  7597. return $return;
  7598. }
  7599. /**
  7600. * Displays a basic form on the overview page for changing the item title and the item description.
  7601. * @param string $item_type
  7602. * @param string $title
  7603. * @param array $data
  7604. * @return string
  7605. */
  7606. public function display_item_small_form($item_type, $title = '', $data = array())
  7607. {
  7608. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7609. $form = new FormValidator('small_form', 'post', $url);
  7610. $form->addElement('header', $title);
  7611. $form->addElement('text', 'title', get_lang('Title'));
  7612. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7613. $form->addElement('hidden', 'id', $data['id']);
  7614. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7615. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7616. $form->setDefaults(array('title' => $data['title']));
  7617. return $form->toHtml();
  7618. }
  7619. /**
  7620. * Return HTML form to allow prerequisites selection
  7621. * @todo use FormValidator
  7622. * @param integer Item ID
  7623. * @return string HTML form
  7624. */
  7625. public function display_item_prerequisites_form($item_id)
  7626. {
  7627. $course_id = api_get_course_int_id();
  7628. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7629. $item_id = intval($item_id);
  7630. /* Current prerequisite */
  7631. $sql = "SELECT * FROM $tbl_lp_item
  7632. WHERE c_id = $course_id AND id = " . $item_id;
  7633. $result = Database::query($sql);
  7634. $row = Database::fetch_array($result);
  7635. $prerequisiteId = $row['prerequisite'];
  7636. $return = '<legend>';
  7637. $return .= get_lang('AddEditPrerequisites');
  7638. $return .= '</legend>';
  7639. $return .= '<form method="POST">';
  7640. $return .= '<table class="data_table">';
  7641. $return .= '<tr>';
  7642. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7643. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7644. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7645. $return .= '</tr>';
  7646. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7647. $return .= '<tr >';
  7648. $return .= '<td colspan="3" class="radio">';
  7649. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7650. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7651. $return .= '</tr>';
  7652. $sql = "SELECT * FROM $tbl_lp_item
  7653. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7654. $result = Database::query($sql);
  7655. $arrLP = array();
  7656. $selectedMinScore = array();
  7657. $selectedMaxScore = array();
  7658. while ($row = Database :: fetch_array($result)) {
  7659. if ($row['id'] == $item_id) {
  7660. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7661. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7662. }
  7663. $arrLP[] = array(
  7664. 'id' => $row['id'],
  7665. 'item_type' => $row['item_type'],
  7666. 'title' => $row['title'],
  7667. 'ref' => $row['ref'],
  7668. 'description' => $row['description'],
  7669. 'parent_item_id' => $row['parent_item_id'],
  7670. 'previous_item_id' => $row['previous_item_id'],
  7671. 'next_item_id' => $row['next_item_id'],
  7672. 'max_score' => $row['max_score'],
  7673. 'min_score' => $row['min_score'],
  7674. 'mastery_score' => $row['mastery_score'],
  7675. 'prerequisite' => $row['prerequisite'],
  7676. 'next_item_id' => $row['next_item_id'],
  7677. 'display_order' => $row['display_order'],
  7678. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7679. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7680. );
  7681. }
  7682. $this->tree_array($arrLP);
  7683. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7684. unset($this->arrMenu);
  7685. for ($i = 0; $i < count($arrLP); $i++) {
  7686. $item = $arrLP[$i];
  7687. if ($item['id'] == $item_id) {
  7688. break;
  7689. }
  7690. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7691. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7692. $return .= '<tr>';
  7693. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7694. $return .= '<label for="id' . $item['id'] . '">';
  7695. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . (($item['item_type'] == 'dokeos_module' || $item['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7696. $icon_name = str_replace(' ', '', $item['item_type']);
  7697. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7698. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7699. } else {
  7700. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7701. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7702. } else {
  7703. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7704. }
  7705. }
  7706. $return .= $item['title'] . '</label>';
  7707. $return .= '</td>';
  7708. if ($item['item_type'] == TOOL_QUIZ) {
  7709. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7710. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7711. $tmp_obj_exercice = new Exercise();
  7712. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7713. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7714. $tmp_obj_lp_item->update_in_bdd();
  7715. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7716. $return .= '<td class="exercise">';
  7717. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7718. $return .= '</td>';
  7719. $return .= '<td class="exercise">';
  7720. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7721. $return .= '</td>';
  7722. }
  7723. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7724. $return .= '<td class="exercise">';
  7725. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7726. $return .= '</td>';
  7727. $return .= '<td class="exercise"">';
  7728. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7729. $return .= '</td>';
  7730. }
  7731. $return .= '</tr>';
  7732. }
  7733. $return .= '<tr>';
  7734. $return .= '</tr>';
  7735. $return .= '</table>';
  7736. $return .= '<div style="padding-top:3px;">';
  7737. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7738. $return .= '</form>';
  7739. return $return;
  7740. }
  7741. /**
  7742. * Return HTML list to allow prerequisites selection for lp
  7743. * @param integer Item ID
  7744. * @return string HTML form
  7745. */
  7746. public function display_lp_prerequisites_list()
  7747. {
  7748. $course_id = api_get_course_int_id();
  7749. $lp_id = $this->lp_id;
  7750. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7751. // get current prerequisite
  7752. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7753. $result = Database::query($sql);
  7754. $row = Database :: fetch_array($result);
  7755. $prerequisiteId = $row['prerequisite'];
  7756. $session_id = api_get_session_id();
  7757. $session_condition = api_get_session_condition($session_id);
  7758. $sql = "SELECT * FROM $tbl_lp
  7759. WHERE c_id = $course_id $session_condition
  7760. ORDER BY display_order ";
  7761. $rs = Database::query($sql);
  7762. $return = '';
  7763. $return .= '<select name="prerequisites" class="form-control">';
  7764. $return .= '<option value="0">'.get_lang('None').'</option>';
  7765. if (Database::num_rows($rs) > 0) {
  7766. while ($row = Database::fetch_array($rs)) {
  7767. if ($row['id'] == $lp_id) {
  7768. continue;
  7769. }
  7770. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7771. }
  7772. }
  7773. $return .= '</select>';
  7774. return $return;
  7775. }
  7776. /**
  7777. * Creates a list with all the documents in it
  7778. * @param bool $showInvisibleFiles
  7779. * @return string
  7780. */
  7781. public function get_documents($showInvisibleFiles = false)
  7782. {
  7783. $course_info = api_get_course_info();
  7784. $sessionId = api_get_session_id();
  7785. $document_tree = DocumentManager::get_document_preview(
  7786. $course_info,
  7787. $this->lp_id,
  7788. null,
  7789. $sessionId,
  7790. true,
  7791. null,
  7792. null,
  7793. $showInvisibleFiles,
  7794. true
  7795. );
  7796. return $document_tree;
  7797. }
  7798. /**
  7799. * Creates a list with all the exercises (quiz) in it
  7800. * @return string
  7801. */
  7802. public function get_exercises()
  7803. {
  7804. $course_id = api_get_course_int_id();
  7805. // New for hotpotatoes.
  7806. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7807. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7808. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7809. $session_id = api_get_session_id();
  7810. $condition_session = api_get_session_condition($session_id);
  7811. $sql_quiz = "SELECT * FROM $tbl_quiz
  7812. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7813. ORDER BY title ASC";
  7814. $sql_hot = "SELECT * FROM $tbl_doc
  7815. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7816. ORDER BY id ASC";
  7817. $res_quiz = Database::query($sql_quiz);
  7818. $res_hot = Database::query($sql_hot);
  7819. $return = '<ul class="lp_resource">';
  7820. $return .= '<li class="lp_resource_element">';
  7821. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7822. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7823. get_lang('NewExercise') . '</a>';
  7824. $return .= '</li>';
  7825. // Display hotpotatoes
  7826. while ($row_hot = Database :: fetch_array($res_hot)) {
  7827. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7828. $return .= '<a class="moved" href="#">';
  7829. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7830. $return .= '</a> ';
  7831. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7832. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7833. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7834. $return .= '</li>';
  7835. }
  7836. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7837. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7838. $return .= '<a class="moved" href="#">';
  7839. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7840. $return .= '</a> ';
  7841. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7842. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7843. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7844. '</a>';
  7845. $return .= '</li>';
  7846. }
  7847. $return .= '</ul>';
  7848. return $return;
  7849. }
  7850. /**
  7851. * Creates a list with all the links in it
  7852. * @return string
  7853. */
  7854. public function get_links()
  7855. {
  7856. $selfUrl = api_get_self();
  7857. $courseIdReq = api_get_cidreq();
  7858. $course = api_get_course_info();
  7859. $course_id = $course['real_id'];
  7860. $tbl_link = Database::get_course_table(TABLE_LINK);
  7861. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7862. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7863. $session_id = api_get_session_id();
  7864. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7865. $sql = "SELECT link.id as link_id,
  7866. link.title as link_title,
  7867. link.category_id as category_id,
  7868. link_category.category_title as category_title
  7869. FROM $tbl_link as link
  7870. LEFT JOIN $linkCategoryTable as link_category
  7871. ON link.category_id = link_category.id
  7872. WHERE link.c_id = ".$course_id." $condition_session
  7873. ORDER BY link_category.category_title ASC, link.title ASC";
  7874. $links = Database::query($sql);
  7875. $categorizedLinks = array();
  7876. $categories = array();
  7877. while ($link = Database :: fetch_array($links)) {
  7878. if (!$link['category_id']) {
  7879. $link['category_title'] = get_lang('Uncategorized');
  7880. }
  7881. $categories[$link['category_id']] = $link['category_title'];
  7882. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7883. }
  7884. $linksHtmlCode =
  7885. '<script>
  7886. function toggle_tool(tool, id){
  7887. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7888. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7889. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7890. } else {
  7891. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7892. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7893. }
  7894. }
  7895. </script>
  7896. <ul class="lp_resource">
  7897. <li class="lp_resource_element">
  7898. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7899. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7900. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7901. </li>';
  7902. foreach ($categorizedLinks as $categoryId => $links) {
  7903. $linkNodes = null;
  7904. foreach ($links as $key => $title) {
  7905. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7906. $linkNodes .=
  7907. '<li class="lp_resource_element" data_id="'.$key.
  7908. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7909. <a class="moved" href="#">'.
  7910. $moveEverywhereIcon.
  7911. '</a>
  7912. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7913. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7914. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7915. Security::remove_XSS($title).
  7916. '</a>
  7917. </li>';
  7918. }
  7919. }
  7920. $linksHtmlCode .=
  7921. '<li>
  7922. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7923. style="vertical-align:middle">
  7924. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7925. align="absbottom" />
  7926. </a>
  7927. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7928. </li>
  7929. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7930. }
  7931. $linksHtmlCode .= '</ul>';
  7932. return $linksHtmlCode;
  7933. }
  7934. /**
  7935. * Creates a list with all the student publications in it
  7936. * @return unknown
  7937. */
  7938. public function get_student_publications()
  7939. {
  7940. $return = '<div class="lp_resource" >';
  7941. $return .= '<div class="lp_resource_element">';
  7942. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7943. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7944. $return .= '</div>';
  7945. $return .= '</div>';
  7946. return $return;
  7947. }
  7948. /**
  7949. * Creates a list with all the forums in it
  7950. * @return string
  7951. */
  7952. public function get_forums()
  7953. {
  7954. require_once '../forum/forumfunction.inc.php';
  7955. require_once '../forum/forumconfig.inc.php';
  7956. $a_forums = get_forums();
  7957. $return = '<ul class="lp_resource">';
  7958. //First add link
  7959. $return .= '<li class="lp_resource_element">';
  7960. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7961. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&content=forum&origin=learnpath&lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7962. $return .= '</li>';
  7963. $return .= '<script>
  7964. function toggle_forum(forum_id){
  7965. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7966. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7967. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7968. } else {
  7969. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7970. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7971. }
  7972. }
  7973. </script>';
  7974. foreach ($a_forums as $forum) {
  7975. if (!empty($forum['forum_id'])) {
  7976. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7977. $return .= '<a class="moved" href="#">';
  7978. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7979. $return .= ' </a>';
  7980. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7981. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7982. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7983. </a>
  7984. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  7985. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7986. $return .= '</li>';
  7987. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7988. $a_threads = get_threads($forum['forum_id']);
  7989. if (is_array($a_threads)) {
  7990. foreach ($a_threads as $thread) {
  7991. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7992. $return .= '&nbsp;<a class="moved" href="#">';
  7993. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7994. $return .= ' </a>';
  7995. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7996. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  7997. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7998. $return .= '</li>';
  7999. }
  8000. }
  8001. $return .= '</div>';
  8002. }
  8003. }
  8004. $return .= '</ul>';
  8005. return $return;
  8006. }
  8007. /**
  8008. * // TODO: The output encoding should be equal to the system encoding.
  8009. *
  8010. * Exports the learning path as a SCORM package. This is the main function that
  8011. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8012. * whole thing and returns the zip.
  8013. *
  8014. * This method needs to be called in PHP5, as it will fail with non-adequate
  8015. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8016. * you need to call it on a learnpath object.
  8017. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8018. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8019. * path has been modified, it should use the generic method here below.
  8020. * @TODO link this function with the export_lp() function in the same class
  8021. * @param string Optional name of zip file. If none, title of learnpath is
  8022. * domesticated and trailed with ".zip"
  8023. * @return string Returns the zip package string, or null if error
  8024. */
  8025. public function scorm_export()
  8026. {
  8027. $_course = api_get_course_info();
  8028. $course_id = $_course['real_id'];
  8029. // Remove memory and time limits as much as possible as this might be a long process...
  8030. if (function_exists('ini_set')) {
  8031. api_set_memory_limit('128M');
  8032. ini_set('max_execution_time', 600);
  8033. }
  8034. // Create the zip handler (this will remain available throughout the method).
  8035. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8036. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8037. $temp_dir_short = uniqid();
  8038. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8039. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8040. $zip_folder = new PclZip($temp_zip_file);
  8041. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8042. $root_path = $main_path = api_get_path(SYS_PATH);
  8043. $files_cleanup = array();
  8044. // Place to temporarily stash the zip file.
  8045. // create the temp dir if it doesn't exist
  8046. // or do a cleanup before creating the zip file.
  8047. if (!is_dir($temp_zip_dir)) {
  8048. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8049. } else {
  8050. // Cleanup: Check the temp dir for old files and delete them.
  8051. $handle = opendir($temp_zip_dir);
  8052. while (false !== ($file = readdir($handle))) {
  8053. if ($file != '.' && $file != '..') {
  8054. unlink("$temp_zip_dir/$file");
  8055. }
  8056. }
  8057. closedir($handle);
  8058. }
  8059. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8060. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8061. // Remove the possible . at the end of the path.
  8062. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8063. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8064. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8065. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  8066. }
  8067. // Build a dummy imsmanifest structure.
  8068. // Do not add to the zip yet (we still need it).
  8069. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8070. // Aggregation Model official document, section "2.3 Content Packaging".
  8071. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8072. $xmldoc = new DOMDocument('1.0');
  8073. $root = $xmldoc->createElement('manifest');
  8074. $root->setAttribute('identifier', 'SingleCourseManifest');
  8075. $root->setAttribute('version', '1.1');
  8076. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8077. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8078. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8079. $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');
  8080. // Build mandatory sub-root container elements.
  8081. $metadata = $xmldoc->createElement('metadata');
  8082. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8083. $metadata->appendChild($md_schema);
  8084. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8085. $metadata->appendChild($md_schemaversion);
  8086. $root->appendChild($metadata);
  8087. $organizations = $xmldoc->createElement('organizations');
  8088. $resources = $xmldoc->createElement('resources');
  8089. // Build the only organization we will use in building our learnpaths.
  8090. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8091. $organization = $xmldoc->createElement('organization');
  8092. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8093. // To set the title of the SCORM entity (=organization), we take the name given
  8094. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8095. // learning path charset) as it is the encoding that defines how it is stored
  8096. // in the database. Then we convert it to HTML entities again as the "&" character
  8097. // alone is not authorized in XML (must be &amp;).
  8098. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8099. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8100. $organization->appendChild($org_title);
  8101. $folder_name = 'document';
  8102. // Removes the learning_path/scorm_folder path when exporting see #4841
  8103. $path_to_remove = null;
  8104. $result = $this->generate_lp_folder($_course);
  8105. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8106. $path_to_remove = 'document'.$result['dir'];
  8107. $path_to_replace = $folder_name.'/';
  8108. }
  8109. //Fixes chamilo scorm exports
  8110. if ($this->ref == 'chamilo_scorm_export') {
  8111. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8112. }
  8113. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8114. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8115. $link_updates = array();
  8116. $links_to_create = array();
  8117. foreach ($this->items as $index => $item) {
  8118. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8119. // Get included documents from this item.
  8120. if ($item->type == 'sco') {
  8121. $inc_docs = $item->get_resources_from_source(
  8122. null,
  8123. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8124. );
  8125. } else {
  8126. $inc_docs = $item->get_resources_from_source();
  8127. }
  8128. // Give a child element <item> to the <organization> element.
  8129. $my_item_id = $item->get_id();
  8130. $my_item = $xmldoc->createElement('item');
  8131. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8132. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8133. $my_item->setAttribute('isvisible', 'true');
  8134. // Give a child element <title> to the <item> element.
  8135. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8136. $my_item->appendChild($my_title);
  8137. // Give a child element <adlcp:prerequisites> to the <item> element.
  8138. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8139. $my_prereqs->setAttribute('type', 'aicc_script');
  8140. $my_item->appendChild($my_prereqs);
  8141. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8142. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8143. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8144. //$xmldoc->createElement('adlcp:timelimitaction','');
  8145. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8146. //$xmldoc->createElement('adlcp:datafromlms','');
  8147. // Give a child element <adlcp:masteryscore> to the <item> element.
  8148. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8149. $my_item->appendChild($my_masteryscore);
  8150. // Attach this item to the organization element or hits parent if there is one.
  8151. if (!empty($item->parent) && $item->parent != 0) {
  8152. $children = $organization->childNodes;
  8153. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8154. if (is_object($possible_parent)) {
  8155. $possible_parent->appendChild($my_item);
  8156. } else {
  8157. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8158. }
  8159. } else {
  8160. if ($this->debug > 0) { error_log('No parent'); }
  8161. $organization->appendChild($my_item);
  8162. }
  8163. // Get the path of the file(s) from the course directory root.
  8164. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8165. if (!empty($path_to_remove)) {
  8166. //From docs
  8167. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8168. //From quiz
  8169. if ($this->ref == 'chamilo_scorm_export') {
  8170. $path_to_remove = 'scorm/'.$this->path.'/';
  8171. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8172. }
  8173. } else {
  8174. $my_xml_file_path = $my_file_path;
  8175. }
  8176. $my_sub_dir = dirname($my_file_path);
  8177. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8178. $my_xml_sub_dir = $my_sub_dir;
  8179. // Give a <resource> child to the <resources> element
  8180. $my_resource = $xmldoc->createElement('resource');
  8181. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8182. $my_resource->setAttribute('type', 'webcontent');
  8183. $my_resource->setAttribute('href', $my_xml_file_path);
  8184. // adlcp:scormtype can be either 'sco' or 'asset'.
  8185. if ($item->type == 'sco') {
  8186. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8187. } else {
  8188. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8189. }
  8190. // xml:base is the base directory to find the files declared in this resource.
  8191. $my_resource->setAttribute('xml:base', '');
  8192. // Give a <file> child to the <resource> element.
  8193. $my_file = $xmldoc->createElement('file');
  8194. $my_file->setAttribute('href', $my_xml_file_path);
  8195. $my_resource->appendChild($my_file);
  8196. // Dependency to other files - not yet supported.
  8197. $i = 1;
  8198. foreach ($inc_docs as $doc_info) {
  8199. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8200. $my_dep = $xmldoc->createElement('resource');
  8201. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8202. $my_dep->setAttribute('identifier', $res_id);
  8203. $my_dep->setAttribute('type', 'webcontent');
  8204. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8205. $my_dep_file = $xmldoc->createElement('file');
  8206. // Check type of URL.
  8207. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8208. if ($doc_info[1] == 'remote') {
  8209. // Remote file. Save url as is.
  8210. $my_dep_file->setAttribute('href', $doc_info[0]);
  8211. $my_dep->setAttribute('xml:base', '');
  8212. } elseif ($doc_info[1] == 'local') {
  8213. switch ($doc_info[2]) {
  8214. case 'url': // Local URL - save path as url for now, don't zip file.
  8215. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8216. $current_dir = dirname($abs_path);
  8217. $current_dir = str_replace('\\', '/', $current_dir);
  8218. $file_path = realpath($abs_path);
  8219. $file_path = str_replace('\\', '/', $file_path);
  8220. $my_dep_file->setAttribute('href', $file_path);
  8221. $my_dep->setAttribute('xml:base', '');
  8222. if (strstr($file_path, $main_path) !== false) {
  8223. // The calculated real path is really inside Chamilo's root path.
  8224. // Reduce file path to what's under the DocumentRoot.
  8225. $file_path = substr($file_path, strlen($root_path) - 1);
  8226. //echo $file_path;echo '<br /><br />';
  8227. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8228. $zip_files_abs[] = $file_path;
  8229. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8230. $my_dep_file->setAttribute('href', $file_path);
  8231. $my_dep->setAttribute('xml:base', '');
  8232. } elseif (empty($file_path)) {
  8233. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8234. if (strpos($document_root, -1) == '/') {
  8235. $document_root = substr(0, -1, $document_root);
  8236. }*/
  8237. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8238. $file_path = str_replace('//', '/', $file_path);
  8239. if (file_exists($file_path)) {
  8240. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8241. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8242. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8243. $my_dep_file->setAttribute('href', $file_path);
  8244. $my_dep->setAttribute('xml:base', '');
  8245. }
  8246. }
  8247. break;
  8248. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8249. $my_dep_file->setAttribute('href', $doc_info[0]);
  8250. $my_dep->setAttribute('xml:base', '');
  8251. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8252. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8253. $abs_img_path_without_subdir = $doc_info[0];
  8254. $relp = api_get_path(REL_PATH); // The url-append config param.
  8255. $pos = strpos($abs_img_path_without_subdir, $relp);
  8256. if ($pos === 0) {
  8257. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8258. }
  8259. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8260. $file_path = str_replace('\\', '/', $file_path);
  8261. $file_path = str_replace('//', '/', $file_path);
  8262. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8263. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8264. // Check if the current document is in that path.
  8265. if (strstr($file_path, $cur_path) !== false) {
  8266. // The document is in that path, now get the relative path
  8267. // to the containing document.
  8268. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8269. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8270. $relative_path = '';
  8271. if (strstr($file_path, $cur_path) !== false) {
  8272. //$relative_path = substr($file_path, strlen($orig_file_path));
  8273. $relative_path = str_replace($cur_path, '', $file_path);
  8274. $file_path = substr($file_path, strlen($cur_path));
  8275. } else {
  8276. // This case is still a problem as it's difficult to calculate a relative path easily
  8277. // might still generate wrong links.
  8278. //$file_path = substr($file_path,strlen($cur_path));
  8279. // Calculate the directory path to the current file (without trailing slash).
  8280. $my_relative_path = dirname($file_path);
  8281. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8282. $my_relative_file = basename($file_path);
  8283. // Calculate the directory path to the containing file (without trailing slash).
  8284. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8285. $dotdot = '';
  8286. $subdir = '';
  8287. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8288. $my_relative_path2 = dirname($my_relative_path);
  8289. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8290. $my_orig_file_path = dirname($my_orig_file_path);
  8291. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8292. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8293. $dotdot += '../';
  8294. $my_relative_path = $my_relative_path2;
  8295. }
  8296. $relative_path = $dotdot.$subdir.$my_relative_file;
  8297. }
  8298. // Put the current document in the zip (this array is the array
  8299. // that will manage documents already in the course folder - relative).
  8300. $zip_files[] = $file_path;
  8301. // Update the links to the current document in the containing document (make them relative).
  8302. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8303. $my_dep_file->setAttribute('href', $file_path);
  8304. $my_dep->setAttribute('xml:base', '');
  8305. } elseif (strstr($file_path, $main_path) !== false) {
  8306. // The calculated real path is really inside Chamilo's root path.
  8307. // Reduce file path to what's under the DocumentRoot.
  8308. $file_path = substr($file_path, strlen($root_path));
  8309. //echo $file_path;echo '<br /><br />';
  8310. //error_log('Reduced path: '.$file_path, 0);
  8311. $zip_files_abs[] = $file_path;
  8312. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8313. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8314. $my_dep->setAttribute('xml:base', '');
  8315. } elseif (empty($file_path)) {
  8316. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8317. if(strpos($document_root,-1) == '/') {
  8318. $document_root = substr(0, -1, $document_root);
  8319. }*/
  8320. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8321. $file_path = str_replace('//', '/', $file_path);
  8322. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8323. $current_dir = dirname($abs_path);
  8324. $current_dir = str_replace('\\', '/', $current_dir);
  8325. if (file_exists($file_path)) {
  8326. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8327. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8328. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8329. $my_dep_file->setAttribute('href','document/'.$file_path);
  8330. $my_dep->setAttribute('xml:base', '');
  8331. }
  8332. }
  8333. break;
  8334. case 'rel':
  8335. // Path relative to the current document.
  8336. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8337. if (substr($doc_info[0], 0, 2) == '..') {
  8338. // Relative path going up.
  8339. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8340. $current_dir = str_replace('\\', '/', $current_dir);
  8341. $file_path = realpath($current_dir.$doc_info[0]);
  8342. $file_path = str_replace('\\', '/', $file_path);
  8343. //error_log($file_path.' <-> '.$main_path,0);
  8344. if (strstr($file_path, $main_path) !== false) {
  8345. // The calculated real path is really inside Chamilo's root path.
  8346. // Reduce file path to what's under the DocumentRoot.
  8347. $file_path = substr($file_path, strlen($root_path));
  8348. //error_log('Reduced path: '.$file_path, 0);
  8349. $zip_files_abs[] = $file_path;
  8350. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8351. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8352. $my_dep->setAttribute('xml:base', '');
  8353. }
  8354. } else {
  8355. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8356. $my_dep_file->setAttribute('href', $doc_info[0]);
  8357. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8358. }
  8359. break;
  8360. default:
  8361. $my_dep_file->setAttribute('href', $doc_info[0]);
  8362. $my_dep->setAttribute('xml:base', '');
  8363. break;
  8364. }
  8365. }
  8366. $my_dep->appendChild($my_dep_file);
  8367. $resources->appendChild($my_dep);
  8368. $dependency = $xmldoc->createElement('dependency');
  8369. $dependency->setAttribute('identifierref', $res_id);
  8370. $my_resource->appendChild($dependency);
  8371. $i++;
  8372. }
  8373. $resources->appendChild($my_resource);
  8374. $zip_files[] = $my_file_path;
  8375. } else {
  8376. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8377. switch ($item->type) {
  8378. case TOOL_LINK:
  8379. $my_item = $xmldoc->createElement('item');
  8380. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8381. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8382. $my_item->setAttribute('isvisible', 'true');
  8383. // Give a child element <title> to the <item> element.
  8384. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8385. $my_item->appendChild($my_title);
  8386. // Give a child element <adlcp:prerequisites> to the <item> element.
  8387. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8388. $my_prereqs->setAttribute('type', 'aicc_script');
  8389. $my_item->appendChild($my_prereqs);
  8390. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8391. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8392. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8393. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8394. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8395. //$xmldoc->createElement('adlcp:datafromlms', '');
  8396. // Give a child element <adlcp:masteryscore> to the <item> element.
  8397. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8398. $my_item->appendChild($my_masteryscore);
  8399. // Attach this item to the organization element or its parent if there is one.
  8400. if (!empty($item->parent) && $item->parent != 0) {
  8401. $children = $organization->childNodes;
  8402. for ($i = 0; $i < $children->length; $i++) {
  8403. $item_temp = $children->item($i);
  8404. if ($item_temp -> nodeName == 'item') {
  8405. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8406. $item_temp -> appendChild($my_item);
  8407. }
  8408. }
  8409. }
  8410. } else {
  8411. $organization->appendChild($my_item);
  8412. }
  8413. $my_file_path = 'link_'.$item->get_id().'.html';
  8414. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8415. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8416. $rs = Database::query($sql);
  8417. if ($link = Database :: fetch_array($rs)) {
  8418. $url = $link['url'];
  8419. $title = stripslashes($link['title']);
  8420. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8421. $my_xml_file_path = $my_file_path;
  8422. $my_sub_dir = dirname($my_file_path);
  8423. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8424. $my_xml_sub_dir = $my_sub_dir;
  8425. // Give a <resource> child to the <resources> element.
  8426. $my_resource = $xmldoc->createElement('resource');
  8427. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8428. $my_resource->setAttribute('type', 'webcontent');
  8429. $my_resource->setAttribute('href', $my_xml_file_path);
  8430. // adlcp:scormtype can be either 'sco' or 'asset'.
  8431. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8432. // xml:base is the base directory to find the files declared in this resource.
  8433. $my_resource->setAttribute('xml:base', '');
  8434. // give a <file> child to the <resource> element.
  8435. $my_file = $xmldoc->createElement('file');
  8436. $my_file->setAttribute('href', $my_xml_file_path);
  8437. $my_resource->appendChild($my_file);
  8438. $resources->appendChild($my_resource);
  8439. }
  8440. break;
  8441. case TOOL_QUIZ:
  8442. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8443. $exe = new Exercise();
  8444. $exe->read($exe_id);
  8445. $my_item = $xmldoc->createElement('item');
  8446. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8447. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8448. $my_item->setAttribute('isvisible', 'true');
  8449. // Give a child element <title> to the <item> element.
  8450. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8451. $my_item->appendChild($my_title);
  8452. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8453. //$my_item->appendChild($my_max_score);
  8454. // Give a child element <adlcp:prerequisites> to the <item> element.
  8455. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8456. $my_prereqs->setAttribute('type','aicc_script');
  8457. $my_item->appendChild($my_prereqs);
  8458. // Give a child element <adlcp:masteryscore> to the <item> element.
  8459. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8460. $my_item->appendChild($my_masteryscore);
  8461. // Attach this item to the organization element or hits parent if there is one.
  8462. if (!empty($item->parent) && $item->parent != 0) {
  8463. $children = $organization->childNodes;
  8464. for ($i = 0; $i < $children->length; $i++) {
  8465. $item_temp = $children->item($i);
  8466. if ($item_temp -> nodeName == 'item') {
  8467. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8468. $item_temp -> appendChild($my_item);
  8469. }
  8470. }
  8471. }
  8472. } else {
  8473. $organization->appendChild($my_item);
  8474. }
  8475. // Get the path of the file(s) from the course directory root
  8476. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8477. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8478. // Write the contents of the exported exercise into a (big) html file
  8479. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8480. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8481. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8482. $res = file_put_contents($tmp_file_path, $contents);
  8483. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8484. $files_cleanup[] = $tmp_file_path;
  8485. //error_log($tmp_path); die();
  8486. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8487. $my_xml_file_path = $my_file_path;
  8488. $my_sub_dir = dirname($my_file_path);
  8489. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8490. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8491. $my_xml_sub_dir = $my_sub_dir;
  8492. // Give a <resource> child to the <resources> element.
  8493. $my_resource = $xmldoc->createElement('resource');
  8494. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8495. $my_resource->setAttribute('type', 'webcontent');
  8496. $my_resource->setAttribute('href', $my_xml_file_path);
  8497. // adlcp:scormtype can be either 'sco' or 'asset'.
  8498. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8499. // xml:base is the base directory to find the files declared in this resource.
  8500. $my_resource->setAttribute('xml:base', '');
  8501. // Give a <file> child to the <resource> element.
  8502. $my_file = $xmldoc->createElement('file');
  8503. $my_file->setAttribute('href', $my_xml_file_path);
  8504. $my_resource->appendChild($my_file);
  8505. // Get included docs.
  8506. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8507. // Dependency to other files - not yet supported.
  8508. $i = 1;
  8509. foreach ($inc_docs as $doc_info) {
  8510. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8511. $my_dep = $xmldoc->createElement('resource');
  8512. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8513. $my_dep->setAttribute('identifier', $res_id);
  8514. $my_dep->setAttribute('type', 'webcontent');
  8515. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8516. $my_dep_file = $xmldoc->createElement('file');
  8517. // Check type of URL.
  8518. if ($doc_info[1] == 'remote') {
  8519. // Remote file. Save url as is.
  8520. $my_dep_file->setAttribute('href', $doc_info[0]);
  8521. $my_dep->setAttribute('xml:base', '');
  8522. } elseif ($doc_info[1] == 'local') {
  8523. switch ($doc_info[2]) {
  8524. case 'url': // Local URL - save path as url for now, don't zip file.
  8525. // Save file but as local file (retrieve from URL).
  8526. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8527. $current_dir = dirname($abs_path);
  8528. $current_dir = str_replace('\\', '/', $current_dir);
  8529. $file_path = realpath($abs_path);
  8530. $file_path = str_replace('\\', '/', $file_path);
  8531. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8532. $my_dep->setAttribute('xml:base', '');
  8533. if (strstr($file_path, $main_path) !== false) {
  8534. // The calculated real path is really inside the chamilo root path.
  8535. // Reduce file path to what's under the DocumentRoot.
  8536. $file_path = substr($file_path, strlen($root_path));
  8537. //echo $file_path;echo '<br /><br />';
  8538. //error_log('Reduced path: '.$file_path, 0);
  8539. $zip_files_abs[] = $file_path;
  8540. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8541. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8542. $my_dep->setAttribute('xml:base', '');
  8543. } elseif (empty($file_path)) {
  8544. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8545. if (strpos($document_root,-1) == '/') {
  8546. $document_root = substr(0, -1, $document_root);
  8547. }*/
  8548. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8549. $file_path = str_replace('//', '/', $file_path);
  8550. if (file_exists($file_path)) {
  8551. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8552. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8553. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8554. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8555. $my_dep->setAttribute('xml:base', '');
  8556. }
  8557. }
  8558. break;
  8559. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8560. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8561. $current_dir = str_replace('\\', '/', $current_dir);
  8562. $file_path = realpath($doc_info[0]);
  8563. $file_path = str_replace('\\', '/', $file_path);
  8564. $my_dep_file->setAttribute('href', $file_path);
  8565. $my_dep->setAttribute('xml:base', '');
  8566. if (strstr($file_path,$main_path) !== false) {
  8567. // The calculated real path is really inside the chamilo root path.
  8568. // Reduce file path to what's under the DocumentRoot.
  8569. $file_path = substr($file_path, strlen($root_path));
  8570. //echo $file_path;echo '<br /><br />';
  8571. //error_log('Reduced path: '.$file_path, 0);
  8572. $zip_files_abs[] = $file_path;
  8573. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8574. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8575. $my_dep->setAttribute('xml:base', '');
  8576. } elseif (empty($file_path)) {
  8577. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8578. if (strpos($document_root,-1) == '/') {
  8579. $document_root = substr(0, -1, $document_root);
  8580. }*/
  8581. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8582. $file_path = str_replace('//', '/', $file_path);
  8583. if (file_exists($file_path)) {
  8584. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8585. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8586. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8587. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8588. $my_dep->setAttribute('xml:base', '');
  8589. }
  8590. }
  8591. break;
  8592. 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
  8593. if (substr($doc_info[0], 0, 2) == '..') {
  8594. // Relative path going up.
  8595. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8596. $current_dir = str_replace('\\', '/', $current_dir);
  8597. $file_path = realpath($current_dir.$doc_info[0]);
  8598. $file_path = str_replace('\\', '/', $file_path);
  8599. //error_log($file_path.' <-> '.$main_path, 0);
  8600. if (strstr($file_path, $main_path) !== false) {
  8601. // The calculated real path is really inside Chamilo's root path.
  8602. // Reduce file path to what's under the DocumentRoot.
  8603. $file_path = substr($file_path, strlen($root_path));
  8604. $file_path_dest = $file_path;
  8605. // File path is courses/CHAMILO/document/....
  8606. $info_file_path = explode('/', $file_path);
  8607. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8608. $file_path_dest = 'document/'.$file_path;
  8609. }
  8610. //error_log('Reduced path: '.$file_path, 0);
  8611. $zip_files_abs[] = $file_path;
  8612. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8613. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8614. $my_dep->setAttribute('xml:base', '');
  8615. }
  8616. } else {
  8617. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8618. $my_dep_file->setAttribute('href', $doc_info[0]);
  8619. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8620. }
  8621. break;
  8622. default:
  8623. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8624. $my_dep->setAttribute('xml:base', '');
  8625. break;
  8626. }
  8627. }
  8628. $my_dep->appendChild($my_dep_file);
  8629. $resources->appendChild($my_dep);
  8630. $dependency = $xmldoc->createElement('dependency');
  8631. $dependency->setAttribute('identifierref', $res_id);
  8632. $my_resource->appendChild($dependency);
  8633. $i++;
  8634. }
  8635. $resources->appendChild($my_resource);
  8636. $zip_files[] = $my_file_path;
  8637. break;
  8638. default:
  8639. // Get the path of the file(s) from the course directory root
  8640. $my_file_path = 'non_exportable.html';
  8641. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8642. $my_xml_file_path = $my_file_path;
  8643. $my_sub_dir = dirname($my_file_path);
  8644. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8645. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8646. $my_xml_sub_dir = $my_sub_dir;
  8647. // Give a <resource> child to the <resources> element.
  8648. $my_resource = $xmldoc->createElement('resource');
  8649. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8650. $my_resource->setAttribute('type', 'webcontent');
  8651. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8652. // adlcp:scormtype can be either 'sco' or 'asset'.
  8653. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8654. // xml:base is the base directory to find the files declared in this resource.
  8655. $my_resource->setAttribute('xml:base', '');
  8656. // Give a <file> child to the <resource> element.
  8657. $my_file = $xmldoc->createElement('file');
  8658. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8659. $my_resource->appendChild($my_file);
  8660. $resources->appendChild($my_resource);
  8661. break;
  8662. }
  8663. }
  8664. }
  8665. $organizations->appendChild($organization);
  8666. $root->appendChild($organizations);
  8667. $root->appendChild($resources);
  8668. $xmldoc->appendChild($root);
  8669. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8670. // then add the file to the zip, then destroy the file (this is done automatically).
  8671. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8672. foreach ($zip_files as $file_path) {
  8673. if (empty($file_path)) {
  8674. continue;
  8675. }
  8676. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8677. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8678. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8679. }
  8680. $this->create_path($dest_file);
  8681. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8682. // Check if the file needs a link update.
  8683. if (in_array($file_path, array_keys($link_updates))) {
  8684. $string = file_get_contents($dest_file);
  8685. unlink($dest_file);
  8686. foreach ($link_updates[$file_path] as $old_new) {
  8687. // This is an ugly hack that allows .flv files to be found by the flv player that
  8688. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8689. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8690. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8691. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8692. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8693. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8694. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8695. }
  8696. //Fix to avoid problems with default_course_document
  8697. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8698. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8699. $newDestination = $old_new['dest'];
  8700. } else {
  8701. $newDestination = str_replace('document/', '', $old_new['dest']);
  8702. }
  8703. $string = str_replace($old_new['orig'], $newDestination, $string);
  8704. //Add files inside the HTMLs
  8705. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8706. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8707. if (file_exists($sys_course_path.$new_path)) {
  8708. copy($sys_course_path.$new_path, $destinationFile);
  8709. }
  8710. }
  8711. file_put_contents($dest_file, $string);
  8712. }
  8713. }
  8714. foreach ($zip_files_abs as $file_path) {
  8715. if (empty($file_path)) {
  8716. continue;
  8717. }
  8718. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8719. continue;
  8720. }
  8721. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8722. $this->create_path($dest_file);
  8723. copy($main_path.$file_path, $dest_file);
  8724. // Check if the file needs a link update.
  8725. if (in_array($file_path, array_keys($link_updates))) {
  8726. $string = file_get_contents($dest_file);
  8727. unlink($dest_file);
  8728. foreach ($link_updates[$file_path] as $old_new) {
  8729. // This is an ugly hack that allows .flv files to be found by the flv player that
  8730. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8731. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8732. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8733. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8734. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8735. }
  8736. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8737. }
  8738. file_put_contents($dest_file, $string);
  8739. }
  8740. }
  8741. if (is_array($links_to_create)) {
  8742. foreach ($links_to_create as $file => $link) {
  8743. $file_content = '<!DOCTYPE html><head>
  8744. <meta charset="'.api_get_language_isocode().'" />
  8745. <title>'.$link['title'].'</title>
  8746. </head>
  8747. <body dir="'.api_get_text_direction().'">
  8748. <div style="text-align:center">
  8749. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8750. </body>
  8751. </html>';
  8752. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8753. }
  8754. }
  8755. // Add non exportable message explanation.
  8756. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8757. $file_content = '<!DOCTYPE html><head>
  8758. <meta charset="'.api_get_language_isocode().'" />
  8759. <title>'.$lang_not_exportable.'</title>
  8760. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8761. </head>
  8762. <body dir="'.api_get_text_direction().'">';
  8763. $file_content .=
  8764. <<<EOD
  8765. <style>
  8766. .error-message {
  8767. font-family: arial, verdana, helvetica, sans-serif;
  8768. border-width: 1px;
  8769. border-style: solid;
  8770. left: 50%;
  8771. margin: 10px auto;
  8772. min-height: 30px;
  8773. padding: 5px;
  8774. right: 50%;
  8775. width: 500px;
  8776. background-color: #FFD1D1;
  8777. border-color: #FF0000;
  8778. color: #000;
  8779. }
  8780. </style>
  8781. <body>
  8782. <div class="error-message">
  8783. $lang_not_exportable
  8784. </div>
  8785. </body>
  8786. </html>
  8787. EOD;
  8788. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8789. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8790. }
  8791. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8792. // Add the extra files that go along with a SCORM package.
  8793. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8794. $extra_files = scandir($main_code_path);
  8795. foreach ($extra_files as $extra_file) {
  8796. if (strpos($extra_file, '.') === 0)
  8797. continue;
  8798. else {
  8799. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8800. $this->create_path($dest_file);
  8801. copy($main_code_path.$extra_file, $dest_file);
  8802. }
  8803. }
  8804. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8805. $manifest = @$xmldoc->saveXML();
  8806. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8807. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8808. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8809. // Clean possible temporary files.
  8810. foreach ($files_cleanup as $file) {
  8811. $res = unlink($file);
  8812. if ($res === false) {
  8813. error_log(
  8814. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8815. 0
  8816. );
  8817. }
  8818. }
  8819. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8820. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8821. }
  8822. /**
  8823. * @param int $lp_id
  8824. * @return bool
  8825. */
  8826. public function scorm_export_to_pdf($lp_id)
  8827. {
  8828. $lp_id = intval($lp_id);
  8829. $files_to_export = array();
  8830. $course_data = api_get_course_info($this->cc);
  8831. if (!empty($course_data)) {
  8832. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8833. $list = self::get_flat_ordered_items_list($lp_id);
  8834. if (!empty($list)) {
  8835. foreach ($list as $item_id) {
  8836. $item = $this->items[$item_id];
  8837. switch ($item->type) {
  8838. case 'document':
  8839. //Getting documents from a LP with chamilo documents
  8840. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8841. // Try loading document from the base course.
  8842. if (empty($file_data) && !empty($sessionId)) {
  8843. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8844. }
  8845. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8846. if (file_exists($file_path)) {
  8847. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8848. }
  8849. break;
  8850. case 'asset': //commes from a scorm package generated by chamilo
  8851. case 'sco':
  8852. $file_path = $scorm_path.'/'.$item->path;
  8853. if (file_exists($file_path)) {
  8854. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8855. }
  8856. break;
  8857. case 'dokeos_chapter':
  8858. case 'dir':
  8859. case 'chapter':
  8860. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8861. break;
  8862. }
  8863. }
  8864. }
  8865. $pdf = new PDF();
  8866. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8867. return $result;
  8868. }
  8869. return false;
  8870. }
  8871. /**
  8872. * Temp function to be moved in main_api or the best place around for this.
  8873. * Creates a file path if it doesn't exist
  8874. * @param string $path
  8875. */
  8876. public function create_path($path)
  8877. {
  8878. $path_bits = explode('/', dirname($path));
  8879. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8880. $path_built = IS_WINDOWS_OS ? '' : '/';
  8881. foreach ($path_bits as $bit) {
  8882. if (!empty ($bit)) {
  8883. $new_path = $path_built . $bit;
  8884. if (is_dir($new_path)) {
  8885. $path_built = $new_path . '/';
  8886. } else {
  8887. mkdir($new_path, api_get_permissions_for_new_directories());
  8888. $path_built = $new_path . '/';
  8889. }
  8890. }
  8891. }
  8892. }
  8893. /**
  8894. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8895. * @return boolean The results of the unlink function, or false if there was no image to start with
  8896. */
  8897. public function delete_lp_image()
  8898. {
  8899. $img = $this->get_preview_image();
  8900. if ($img != '') {
  8901. $del_file = $this->get_preview_image_path(null, 'sys');
  8902. if (isset($del_file) && file_exists($del_file)) {
  8903. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8904. if (file_exists($del_file_2)) {
  8905. unlink($del_file_2);
  8906. }
  8907. $this->set_preview_image('');
  8908. return @unlink($del_file);
  8909. }
  8910. }
  8911. return false;
  8912. }
  8913. /**
  8914. * Uploads an author image to the upload/learning_path/images directory
  8915. * @param array The image array, coming from the $_FILES superglobal
  8916. * @return boolean True on success, false on error
  8917. */
  8918. public function upload_image($image_array)
  8919. {
  8920. $image_moved = false;
  8921. if (!empty ($image_array['name'])) {
  8922. $upload_ok = process_uploaded_file($image_array);
  8923. $has_attachment = true;
  8924. } else {
  8925. $image_moved = true;
  8926. }
  8927. if ($upload_ok) {
  8928. if ($has_attachment) {
  8929. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8930. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8931. $updir = $sys_course_path . $courseDir;
  8932. // Try to add an extension to the file if it hasn't one.
  8933. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8934. if (!filter_extension($new_file_name)) {
  8935. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8936. $image_moved = false;
  8937. } else {
  8938. $file_extension = explode('.', $image_array['name']);
  8939. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8940. $filename = uniqid('');
  8941. $new_file_name = $filename.'.'.$file_extension;
  8942. $new_path = $updir.'/'.$new_file_name;
  8943. // Resize the image.
  8944. $temp = new Image($image_array['tmp_name']);
  8945. $temp->resize(104);
  8946. $result = $temp->send_image($new_path);
  8947. // Storing the image filename.
  8948. if ($result) {
  8949. $image_moved = true;
  8950. $this->set_preview_image($new_file_name);
  8951. //Resize to 64px to use on course homepage
  8952. $temp->resize(64);
  8953. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8954. return true;
  8955. }
  8956. }
  8957. }
  8958. }
  8959. return false;
  8960. }
  8961. /**
  8962. * @param int $lp_id
  8963. * @param string $status
  8964. */
  8965. public function set_autolaunch($lp_id, $status)
  8966. {
  8967. $course_id = api_get_course_int_id();
  8968. $lp_id = intval($lp_id);
  8969. $status = intval($status);
  8970. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8971. // Setting everything to autolaunch = 0
  8972. $attributes['autolaunch'] = 0;
  8973. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8974. Database::update($lp_table, $attributes, $where);
  8975. if ($status == 1) {
  8976. //Setting my lp_id to autolaunch = 1
  8977. $attributes['autolaunch'] = 1;
  8978. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8979. Database::update($lp_table, $attributes, $where );
  8980. }
  8981. }
  8982. /**
  8983. * Gets previous_item_id for the next element of the lp_item table
  8984. * @author Isaac flores paz
  8985. * @return integer Previous item ID
  8986. */
  8987. public function select_previous_item_id()
  8988. {
  8989. $course_id = api_get_course_int_id();
  8990. if ($this->debug > 0) {
  8991. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8992. }
  8993. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8994. // Get the max order of the items
  8995. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8996. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8997. $rs_max_order = Database::query($sql_max_order);
  8998. $row_max_order = Database::fetch_object($rs_max_order);
  8999. $max_order = $row_max_order->display_order;
  9000. // Get the previous item ID
  9001. $sql = "SELECT id as previous FROM $table_lp_item
  9002. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9003. $rs_max = Database::query($sql);
  9004. $row_max = Database::fetch_object($rs_max);
  9005. // Return the previous item ID
  9006. return $row_max->previous;
  9007. }
  9008. /**
  9009. * Copies an LP
  9010. */
  9011. public function copy()
  9012. {
  9013. $main_path = api_get_path(SYS_CODE_PATH);
  9014. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9015. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9016. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9017. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9018. //Course builder
  9019. $cb = new CourseBuilder();
  9020. //Setting tools that will be copied
  9021. $cb->set_tools_to_build(array('learnpaths'));
  9022. //Setting elements that will be copied
  9023. $cb->set_tools_specific_id_list(
  9024. array('learnpaths' => array($this->lp_id))
  9025. );
  9026. $course = $cb->build();
  9027. //Course restorer
  9028. $course_restorer = new CourseRestorer($course);
  9029. $course_restorer->set_add_text_in_items(true);
  9030. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9031. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9032. }
  9033. public function verify_document_size($s)
  9034. {
  9035. $post_max = ini_get('post_max_size');
  9036. if (substr($post_max, -1, 1) == 'M') {
  9037. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9038. } elseif (substr($post_max, -1, 1) == 'G') {
  9039. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9040. }
  9041. $upl_max = ini_get('upload_max_filesize');
  9042. if (substr($upl_max, -1, 1) == 'M') {
  9043. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9044. } elseif (substr($upl_max, -1, 1) == 'G') {
  9045. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9046. }
  9047. $documents_total_space = DocumentManager::documents_total_space();
  9048. $course_max_space = DocumentManager::get_course_quota();
  9049. $total_size = filesize($s) + $documents_total_space;
  9050. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9051. return true;
  9052. } else{
  9053. return false;
  9054. }
  9055. }
  9056. /**
  9057. * Clear LP prerequisites
  9058. */
  9059. public function clear_prerequisites()
  9060. {
  9061. $course_id = $this->get_course_int_id();
  9062. if ($this->debug > 0) {
  9063. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9064. }
  9065. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9066. $lp_id = $this->get_id();
  9067. //Cleaning prerequisites
  9068. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9069. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9070. Database::query($sql);
  9071. //Cleaning mastery score for exercises
  9072. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9073. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9074. Database::query($sql);
  9075. }
  9076. public function set_previous_step_as_prerequisite_for_all_items()
  9077. {
  9078. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9079. $course_id = $this->get_course_int_id();
  9080. $lp_id = $this->get_id();
  9081. if (!empty($this->items)) {
  9082. $previous_item_id = null;
  9083. $previous_item_max = 0;
  9084. $previous_item_type = null;
  9085. $last_item_not_chapter = null;
  9086. $last_item_not_chapter_type = null;
  9087. $last_item_not_chapter_max = null;
  9088. foreach ($this->items as $item) {
  9089. // if there was a previous item... (otherwise jump to set it)
  9090. if (!empty($previous_item_id)) {
  9091. $current_item_id = $item->get_id(); //save current id
  9092. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9093. // Current item is not a folder, so it qualifies to get a prerequisites
  9094. if ($last_item_not_chapter_type == 'quiz') {
  9095. // if previous is quiz, mark its max score as default score to be achieved
  9096. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9097. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9098. Database::query($sql);
  9099. }
  9100. // now simply update the prerequisite to set it to the last non-chapter item
  9101. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9102. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9103. Database::query($sql);
  9104. // record item as 'non-chapter' reference
  9105. $last_item_not_chapter = $item->get_id();
  9106. $last_item_not_chapter_type = $item->get_type();
  9107. $last_item_not_chapter_max = $item->get_max();
  9108. }
  9109. } else {
  9110. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9111. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9112. $last_item_not_chapter = $item->get_id();
  9113. $last_item_not_chapter_type = $item->get_type();
  9114. $last_item_not_chapter_max = $item->get_max();
  9115. }
  9116. }
  9117. // Saving the item as "previous item" for the next loop
  9118. $previous_item_id = $item->get_id();
  9119. $previous_item_max = $item->get_max();
  9120. $previous_item_type = $item->get_type();
  9121. }
  9122. }
  9123. }
  9124. /**
  9125. * @param array $params
  9126. */
  9127. public static function createCategory($params)
  9128. {
  9129. $em = Database::getManager();
  9130. $item = new CLpCategory();
  9131. $item->setName($params['name']);
  9132. $item->setCId($params['c_id']);
  9133. $em->persist($item);
  9134. $em->flush();
  9135. }
  9136. /**
  9137. * @param array $params
  9138. */
  9139. public static function updateCategory($params)
  9140. {
  9141. $em = Database::getManager();
  9142. /** @var CLpCategory $item */
  9143. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9144. if ($item) {
  9145. $item->setName($params['name']);
  9146. $em->merge($item);
  9147. $em->flush();
  9148. }
  9149. }
  9150. /**
  9151. * @param int $id
  9152. */
  9153. public static function moveUpCategory($id)
  9154. {
  9155. $em = Database::getManager();
  9156. /** @var CLpCategory $item */
  9157. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9158. if ($item) {
  9159. $position = $item->getPosition() - 1;
  9160. $item->setPosition($position);
  9161. $em->persist($item);
  9162. $em->flush();
  9163. }
  9164. }
  9165. /**
  9166. * @param int $id
  9167. */
  9168. public static function moveDownCategory($id)
  9169. {
  9170. $em = Database::getManager();
  9171. /** @var CLpCategory $item */
  9172. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9173. if ($item) {
  9174. $position = $item->getPosition() + 1;
  9175. $item->setPosition($position);
  9176. $em->persist($item);
  9177. $em->flush();
  9178. }
  9179. }
  9180. /**
  9181. * @param int $courseId
  9182. * @return int|mixed
  9183. */
  9184. public static function getCountCategories($courseId)
  9185. {
  9186. if (empty($course_id)) {
  9187. return 0;
  9188. }
  9189. $em = Database::getManager();
  9190. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9191. $query->setParameter('id', $courseId);
  9192. return $query->getSingleScalarResult();
  9193. }
  9194. /**
  9195. * @param int $courseId
  9196. *
  9197. * @return mixed
  9198. */
  9199. public static function getCategories($courseId)
  9200. {
  9201. $em = Database::getManager();
  9202. //Default behaviour
  9203. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9204. array('cId' => $course_id),
  9205. array('name' => 'ASC')
  9206. );*/
  9207. // Using doctrine extensions
  9208. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9209. array('cId' => $courseId)
  9210. )->getResult();
  9211. return $items;
  9212. }
  9213. /**
  9214. * @param int $id
  9215. *
  9216. * @return mixed
  9217. */
  9218. public static function getCategory($id)
  9219. {
  9220. $em = Database::getManager();
  9221. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9222. return $item;
  9223. }
  9224. /**
  9225. * @param int $courseId
  9226. * @return array
  9227. */
  9228. public static function getCategoryByCourse($courseId)
  9229. {
  9230. $em = Database::getManager();
  9231. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9232. return $items;
  9233. }
  9234. /**
  9235. * @param int $id
  9236. *
  9237. * @return mixed
  9238. */
  9239. public static function deleteCategory($id)
  9240. {
  9241. $em = Database::getManager();
  9242. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9243. if ($item) {
  9244. $courseId = $item->getCId();
  9245. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9246. $query->setParameter('id', $courseId);
  9247. $query->setParameter('catId', $item->getId());
  9248. $lps = $query->getResult();
  9249. // Setting category = 0.
  9250. if ($lps) {
  9251. foreach ($lps as $lpItem) {
  9252. $lpItem->setCategoryId(0);
  9253. }
  9254. }
  9255. // Removing category.
  9256. $em->remove($item);
  9257. $em->flush();
  9258. }
  9259. }
  9260. /**
  9261. * @param int $courseId
  9262. * @param bool $addSelectOption
  9263. *
  9264. * @return mixed
  9265. */
  9266. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9267. {
  9268. $items = self::getCategoryByCourse($courseId);
  9269. $cats = array();
  9270. if ($addSelectOption) {
  9271. $cats = array(get_lang('SelectACategory'));
  9272. }
  9273. if (!empty($items)) {
  9274. foreach ($items as $cat) {
  9275. $cats[$cat->getId()] = $cat->getName();
  9276. }
  9277. }
  9278. return $cats;
  9279. }
  9280. /**
  9281. * Return the scorm item type object with spaces replaced with _
  9282. * The return result is use to build a css classname like scorm_type_$return
  9283. * @param $in_type
  9284. * @return mixed
  9285. */
  9286. private static function format_scorm_type_item($in_type)
  9287. {
  9288. return str_replace(' ', '_', $in_type);
  9289. }
  9290. /**
  9291. * @return \learnpath
  9292. */
  9293. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9294. {
  9295. $lpObject = Session::read('lpobject');
  9296. $learnPath = null;
  9297. if (isset($lpObject)) {
  9298. $learnPath = unserialize($lpObject);
  9299. }
  9300. if (!is_object($learnPath)) {
  9301. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9302. }
  9303. return $learnPath;
  9304. }
  9305. /**
  9306. * @param int $itemId
  9307. * @return learnpathItem|false
  9308. */
  9309. public function getItem($itemId)
  9310. {
  9311. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9312. return $this->items[$itemId];
  9313. }
  9314. return false;
  9315. }
  9316. /**
  9317. * @return int
  9318. */
  9319. public function getCategoryId()
  9320. {
  9321. return $this->categoryId;
  9322. }
  9323. /**
  9324. * @param int $categoryId
  9325. * @return bool
  9326. */
  9327. public function setCategoryId($categoryId)
  9328. {
  9329. $this->categoryId = intval($categoryId);
  9330. $courseId = api_get_course_int_id();
  9331. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9332. $lp_id = $this->get_id();
  9333. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9334. WHERE c_id = $courseId AND id = $lp_id";
  9335. Database::query($sql);
  9336. return true;
  9337. }
  9338. /**
  9339. * Get whether this is a learning path with the possibility to subscribe
  9340. * users or not
  9341. * @return int
  9342. */
  9343. public function getSubscribeUsers()
  9344. {
  9345. return $this->subscribeUsers;
  9346. }
  9347. /**
  9348. * Set whether this is a learning path with the possibility to subscribe
  9349. * users or not
  9350. * @param int $subscribeUsers (0 = false, 1 = true)
  9351. */
  9352. public function setSubscribeUsers($value)
  9353. {
  9354. if ($this->debug > 0) {
  9355. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9356. }
  9357. $this->subscribeUsers = intval($value);;
  9358. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9359. $lp_id = $this->get_id();
  9360. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9361. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9362. Database::query($sql);
  9363. return true;
  9364. }
  9365. /**
  9366. * Calculate the count of stars for a user in this LP
  9367. * This calculation is based on the following rules:
  9368. * - the student gets one star when he gets to 50% of the learning path
  9369. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9370. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9371. * - the student gets the final star when the score for the *last* test is >= 80%
  9372. * @param int $sessionId Optional. The session ID
  9373. * @return int The count of stars
  9374. */
  9375. public function getCalculateStars($sessionId = 0)
  9376. {
  9377. $stars = 0;
  9378. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9379. if ($progress >= 50) {
  9380. $stars++;
  9381. }
  9382. // Calculate stars chapters evaluation
  9383. $exercisesItems = $this->getExercisesItems();
  9384. if (!empty($exercisesItems)) {
  9385. $totalResult = 0;
  9386. foreach ($exercisesItems as $exerciseItem) {
  9387. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9388. $this->user_id,
  9389. $exerciseItem->path,
  9390. $this->course_int_id,
  9391. $sessionId,
  9392. $this->lp_id,
  9393. $exerciseItem->db_id
  9394. );
  9395. $exerciseResult = 0;
  9396. foreach ($exerciseResultInfo as $result) {
  9397. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9398. }
  9399. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9400. $totalResult += $exerciseAverage;
  9401. }
  9402. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9403. if ($totalExerciseAverage >= 50) {
  9404. $stars++;
  9405. }
  9406. if ($totalExerciseAverage >= 80) {
  9407. $stars++;
  9408. }
  9409. }
  9410. // Calculate star for final evaluation
  9411. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9412. if (!empty($finalEvaluationItem)) {
  9413. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9414. $this->user_id,
  9415. $finalEvaluationItem->path,
  9416. $this->course_int_id,
  9417. $sessionId,
  9418. $this->lp_id,
  9419. $finalEvaluationItem->db_id
  9420. );
  9421. $evaluationResult = 0;
  9422. foreach ($evaluationResultInfo as $result) {
  9423. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9424. }
  9425. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9426. $evaluationAverage = $evaluationResult / $averageDivisor;
  9427. if ($evaluationAverage >= 80) {
  9428. $stars++;
  9429. }
  9430. }
  9431. return $stars;
  9432. }
  9433. /**
  9434. * Get the items of exercise type
  9435. * @return array The items. Otherwise return false
  9436. */
  9437. public function getExercisesItems()
  9438. {
  9439. $exercises = [];
  9440. foreach ($this->items as $item) {
  9441. if ($item->type != 'quiz') {
  9442. continue;
  9443. }
  9444. $exercises[] = $item;
  9445. }
  9446. array_pop($exercises);
  9447. return $exercises;
  9448. }
  9449. /**
  9450. * Get the item of exercise type (evaluation type)
  9451. * @return array The final evaluation. Otherwise return false
  9452. */
  9453. public function getFinalEvaluationItem()
  9454. {
  9455. $exercises = [];
  9456. foreach ($this->items as $item) {
  9457. if ($item->type != 'quiz') {
  9458. continue;
  9459. }
  9460. $exercises[] = $item;
  9461. }
  9462. return array_pop($exercises);
  9463. }
  9464. /**
  9465. * Calculate the total points achieved for the current user in this learning path
  9466. * @param int $sessionId Optional. The session Id
  9467. * @return int
  9468. */
  9469. public function getCalculateScore($sessionId = 0)
  9470. {
  9471. // Calculate stars chapters evaluation
  9472. $exercisesItems = $this->getExercisesItems();
  9473. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9474. $totalExercisesResult = 0;
  9475. $totalEvaluationResult = 0;
  9476. if ($exercisesItems !== false) {
  9477. foreach ($exercisesItems as $exerciseItem) {
  9478. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9479. $this->user_id,
  9480. $exerciseItem->path,
  9481. $this->course_int_id,
  9482. $sessionId,
  9483. $this->lp_id,
  9484. $exerciseItem->db_id
  9485. );
  9486. $exerciseResult = 0;
  9487. foreach ($exerciseResultInfo as $result) {
  9488. $exerciseResult += $result['exe_result'];
  9489. }
  9490. $totalExercisesResult += $exerciseResult;
  9491. }
  9492. }
  9493. if (!empty($finalEvaluationItem)) {
  9494. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9495. $this->user_id,
  9496. $finalEvaluationItem->path,
  9497. $this->course_int_id,
  9498. $sessionId,
  9499. $this->lp_id,
  9500. $finalEvaluationItem->db_id
  9501. );
  9502. foreach ($evaluationResultInfo as $result) {
  9503. $totalEvaluationResult += $result['exe_result'];
  9504. }
  9505. }
  9506. return $totalExercisesResult + $totalEvaluationResult;
  9507. }
  9508. /**
  9509. * Check if URL is not allowed to be show in a iframe
  9510. * @param string $src
  9511. *
  9512. * @return string
  9513. */
  9514. public function fixBlockedLinks($src)
  9515. {
  9516. $urlInfo = parse_url($src);
  9517. //$platformProtocol = api_get_protocol();
  9518. $platformProtocol = 'https';
  9519. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9520. $platformProtocol = 'http';
  9521. }
  9522. $protocolFixApplied = false;
  9523. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9524. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9525. if ($platformProtocol != $scheme) {
  9526. $_SESSION['x_frame_source'] = $src;
  9527. $src = 'blank.php?error=x_frames_options';
  9528. $protocolFixApplied = true;
  9529. }
  9530. if ($protocolFixApplied == false) {
  9531. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9532. // Check X-Frame-Options
  9533. $ch = curl_init();
  9534. $options = array(
  9535. CURLOPT_URL => $src,
  9536. CURLOPT_RETURNTRANSFER => true,
  9537. CURLOPT_HEADER => true,
  9538. CURLOPT_FOLLOWLOCATION => true,
  9539. CURLOPT_ENCODING => "",
  9540. CURLOPT_AUTOREFERER => true,
  9541. CURLOPT_CONNECTTIMEOUT => 120,
  9542. CURLOPT_TIMEOUT => 120,
  9543. CURLOPT_MAXREDIRS => 10,
  9544. );
  9545. curl_setopt_array($ch, $options);
  9546. $response = curl_exec($ch);
  9547. $httpCode = curl_getinfo($ch);
  9548. $headers = substr($response, 0, $httpCode['header_size']);
  9549. $error = false;
  9550. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9551. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9552. ) {
  9553. $error = true;
  9554. }
  9555. if ($error) {
  9556. $_SESSION['x_frame_source'] = $src;
  9557. $src = 'blank.php?error=x_frames_options';
  9558. }
  9559. }
  9560. }
  9561. return $src;
  9562. }
  9563. /**
  9564. * Check if this LP has a created forum in the basis course
  9565. * @return boolean
  9566. */
  9567. public function lpHasForum()
  9568. {
  9569. $forumTable = Database::get_course_table(TABLE_FORUM);
  9570. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9571. $fakeFrom = "
  9572. $forumTable f
  9573. INNER JOIN $itemProperty ip
  9574. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9575. ";
  9576. $resultData = Database::select(
  9577. 'COUNT(f.iid) AS qty',
  9578. $fakeFrom,
  9579. [
  9580. 'where' => [
  9581. 'ip.visibility != ? AND ' => 2,
  9582. 'ip.tool = ? AND ' => TOOL_FORUM,
  9583. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9584. 'f.lp_id = ?' => intval($this->lp_id)
  9585. ]
  9586. ],
  9587. 'first'
  9588. );
  9589. if ($resultData['qty'] > 0) {
  9590. return true;
  9591. }
  9592. return false;
  9593. }
  9594. /**
  9595. * Get the forum for this learning path
  9596. * @return boolean
  9597. */
  9598. public function getForum($sessionId = 0)
  9599. {
  9600. $forumTable = Database::get_course_table(TABLE_FORUM);
  9601. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9602. $fakeFrom = "$forumTable f
  9603. INNER JOIN $itemProperty ip ";
  9604. if ($this->lp_session_id == 0) {
  9605. $fakeFrom .= "
  9606. ON (
  9607. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9608. f.session_id = ip.session_id OR ip.session_id IS NULL
  9609. )
  9610. )
  9611. ";
  9612. } else {
  9613. $fakeFrom .= "
  9614. ON (
  9615. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9616. )
  9617. ";
  9618. }
  9619. $resultData = Database::select(
  9620. 'f.*',
  9621. $fakeFrom,
  9622. [
  9623. 'where' => [
  9624. 'ip.visibility != ? AND ' => 2,
  9625. 'ip.tool = ? AND ' => TOOL_FORUM,
  9626. 'f.session_id = ? AND ' => $sessionId,
  9627. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9628. 'f.lp_id = ?' => intval($this->lp_id)
  9629. ]
  9630. ],
  9631. 'first'
  9632. );
  9633. if (empty($resultData)) {
  9634. return false;
  9635. }
  9636. return $resultData;
  9637. }
  9638. /**
  9639. * Create a forum for this learning path
  9640. * @param type $forumCategoryId
  9641. * @return int The forum ID if was created. Otherwise return false
  9642. */
  9643. public function createForum($forumCategoryId)
  9644. {
  9645. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9646. $forumId = store_forum(
  9647. [
  9648. 'lp_id' => $this->lp_id,
  9649. 'forum_title' => $this->name,
  9650. 'forum_comment' => null,
  9651. 'forum_category' => intval($forumCategoryId),
  9652. 'students_can_edit_group' => ['students_can_edit' => 0],
  9653. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9654. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9655. 'group_forum' => 0,
  9656. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9657. ],
  9658. [],
  9659. true
  9660. );
  9661. return $forumId;
  9662. }
  9663. }
  9664. if (!function_exists('trim_value')) {
  9665. function trim_value(& $value) {
  9666. $value = trim($value);
  9667. }
  9668. }