learnpath.class.php 481 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Finder\Finder;
  10. use Chamilo\CourseBundle\Entity\CLp;
  11. /**
  12. * Class learnpath
  13. * This class defines the parent attributes and methods for Chamilo learnpaths
  14. * and SCORM learnpaths. It is used by the scorm class.
  15. *
  16. * @package chamilo.learnpath
  17. * @author Yannick Warnier <ywarnier@beeznest.org>
  18. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  19. */
  20. class learnpath
  21. {
  22. public $attempt = 0; // The number for the current ID view.
  23. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  24. public $current; // Id of the current item the user is viewing.
  25. public $current_score; // The score of the current item.
  26. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  27. public $current_time_stop; // The time the user closed this resource.
  28. public $default_status = 'not attempted';
  29. public $encoding = 'UTF-8';
  30. public $error = '';
  31. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  32. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  33. public $index; // The index of the active learnpath_item in $ordered_items array.
  34. public $items = array();
  35. public $last; // item_id of last item viewed in the learning path.
  36. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  37. public $license; // Which license this course has been given - not used yet on 20060522.
  38. public $lp_id; // DB ID for this learnpath.
  39. public $lp_view_id; // DB ID for lp_view
  40. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  41. public $message = '';
  42. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  43. public $name; // Learnpath name (they generally have one).
  44. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  45. public $path = ''; // Path inside the scorm directory (if scorm).
  46. public $theme; // The current theme of the learning path.
  47. public $preview_image; // The current image of the learning path.
  48. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  49. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  50. public $prevent_reinit = 1;
  51. // Describes the mode of progress bar display.
  52. public $seriousgame_mode = 0;
  53. public $progress_bar_mode = '%';
  54. // Percentage progress as saved in the db.
  55. public $progress_db = '0';
  56. public $proximity; // Wether the content is distant or local or unknown.
  57. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  58. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  59. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  60. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  61. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  62. public $user_id; //ID of the user that is viewing/using the course
  63. public $update_queue = array();
  64. public $scorm_debug = 0;
  65. public $arrMenu = array(); // Array for the menu items.
  66. public $debug = 0; // Logging level.
  67. public $lp_session_id = 0;
  68. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  69. public $prerequisite = 0;
  70. public $use_max_score = 1; // 1 or 0
  71. public $subscribeUsers = 0; // Subscribe users or not
  72. public $created_on = '';
  73. public $modified_on = '';
  74. public $publicated_on = '';
  75. public $expired_on = '';
  76. public $ref = null;
  77. public $course_int_id;
  78. public $course_info = array();
  79. public $categoryId;
  80. /**
  81. * Constructor.
  82. * Needs a database handler, a course code and a learnpath id from the database.
  83. * Also builds the list of items into $this->items.
  84. * @param string $course Course code
  85. * @param integer $lp_id
  86. * @param integer $user_id
  87. */
  88. public function __construct($course, $lp_id, $user_id)
  89. {
  90. $this->encoding = api_get_system_encoding();
  91. if ($this->debug > 0) {
  92. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  93. }
  94. if (empty($course)) {
  95. $course = api_get_course_id();
  96. }
  97. $course_info = api_get_course_info($course);
  98. if (!empty($course_info)) {
  99. $this->cc = $course_info['code'];
  100. $this->course_info = $course_info;
  101. $course_id = $course_info['real_id'];
  102. } else {
  103. $this->error = 'Course code does not exist in database.';
  104. }
  105. $this->set_course_int_id($course_id);
  106. $lp_id = (int) $lp_id;
  107. // Check learnpath ID.
  108. if (empty($lp_id)) {
  109. $this->error = 'Learnpath ID is empty';
  110. } else {
  111. // TODO: Make it flexible to use any course_code (still using env course code here).
  112. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  113. $sql = "SELECT * FROM $lp_table
  114. WHERE id = '$lp_id' AND c_id = $course_id";
  115. if ($this->debug > 2) {
  116. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  117. }
  118. $res = Database::query($sql);
  119. if (Database::num_rows($res) > 0) {
  120. $this->lp_id = $lp_id;
  121. $row = Database::fetch_array($res);
  122. $this->type = $row['lp_type'];
  123. $this->name = stripslashes($row['name']);
  124. $this->proximity = $row['content_local'];
  125. $this->theme = $row['theme'];
  126. $this->maker = $row['content_maker'];
  127. $this->prevent_reinit = $row['prevent_reinit'];
  128. $this->seriousgame_mode = $row['seriousgame_mode'];
  129. $this->license = $row['content_license'];
  130. $this->scorm_debug = $row['debug'];
  131. $this->js_lib = $row['js_lib'];
  132. $this->path = $row['path'];
  133. $this->preview_image = $row['preview_image'];
  134. $this->author = $row['author'];
  135. $this->hide_toc_frame = $row['hide_toc_frame'];
  136. $this->lp_session_id = $row['session_id'];
  137. $this->use_max_score = $row['use_max_score'];
  138. $this->subscribeUsers = $row['subscribe_users'];
  139. $this->created_on = $row['created_on'];
  140. $this->modified_on = $row['modified_on'];
  141. $this->ref = $row['ref'];
  142. $this->categoryId = $row['category_id'];
  143. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  144. if (!empty($row['publicated_on'])) {
  145. $this->publicated_on = $row['publicated_on'];
  146. }
  147. if (!empty($row['expired_on'])) {
  148. $this->expired_on = $row['expired_on'];
  149. }
  150. if ($this->type == 2) {
  151. if ($row['force_commit'] == 1) {
  152. $this->force_commit = true;
  153. }
  154. }
  155. $this->mode = $row['default_view_mod'];
  156. // Check user ID.
  157. if (empty($user_id)) {
  158. $this->error = 'User ID is empty';
  159. } else {
  160. $user_info = api_get_user_info($user_id);
  161. if (!empty($user_info)) {
  162. $this->user_id = $user_info['user_id'];
  163. } else {
  164. $this->error = 'User ID does not exist in database ('.$sql.')';
  165. }
  166. }
  167. // End of variables checking.
  168. $session_id = api_get_session_id();
  169. // Get the session condition for learning paths of the base + session.
  170. $session = api_get_session_condition($session_id);
  171. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  172. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  173. // Selecting by view_count descending allows to get the highest view_count first.
  174. $sql = "SELECT * FROM $lp_table
  175. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  176. ORDER BY view_count DESC";
  177. $res = Database::query($sql);
  178. if ($this->debug > 2) {
  179. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  180. }
  181. if (Database::num_rows($res) > 0) {
  182. if ($this->debug > 2) {
  183. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  184. }
  185. $row = Database::fetch_array($res);
  186. $this->attempt = $row['view_count'];
  187. $this->lp_view_id = $row['id'];
  188. $this->last_item_seen = $row['last_item'];
  189. $this->progress_db = $row['progress'];
  190. $this->lp_view_session_id = $row['session_id'];
  191. } else if (!api_is_invitee()) {
  192. if ($this->debug > 2) {
  193. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  194. }
  195. $this->attempt = 1;
  196. $params = [
  197. 'c_id' => $course_id,
  198. 'lp_id' => $lp_id,
  199. 'user_id' => $user_id,
  200. 'view_count' => 1,
  201. 'session_id' => $session_id,
  202. 'last_item' => 0
  203. ];
  204. $this->lp_view_id = Database::insert($lp_table, $params);
  205. if (!empty($this->lp_view_id)) {
  206. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  207. Database::query($sql);
  208. }
  209. }
  210. // Initialise items.
  211. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  212. $sql = "SELECT * FROM $lp_item_table
  213. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  214. ORDER BY parent_item_id, display_order";
  215. $res = Database::query($sql);
  216. if ($this->debug > 2) {
  217. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  218. error_log('-- Start while--', 0);
  219. }
  220. $lp_item_id_list = array();
  221. while ($row = Database::fetch_array($res)) {
  222. $lp_item_id_list[] = $row['id'];
  223. switch ($this->type) {
  224. case 3: //aicc
  225. $oItem = new aiccItem('db', $row['id'], $course_id);
  226. if (is_object($oItem)) {
  227. $my_item_id = $oItem->get_id();
  228. $oItem->set_lp_view($this->lp_view_id, $course_id);
  229. $oItem->set_prevent_reinit($this->prevent_reinit);
  230. // Don't use reference here as the next loop will make the pointed object change.
  231. $this->items[$my_item_id] = $oItem;
  232. $this->refs_list[$oItem->ref] = $my_item_id;
  233. if ($this->debug > 2) {
  234. error_log(
  235. 'New LP - learnpath::__construct() - ' .
  236. 'aicc object with id ' . $my_item_id .
  237. ' set in items[]',
  238. 0
  239. );
  240. }
  241. }
  242. break;
  243. case 2:
  244. $oItem = new scormItem('db', $row['id'], $course_id);
  245. if (is_object($oItem)) {
  246. $my_item_id = $oItem->get_id();
  247. $oItem->set_lp_view($this->lp_view_id, $course_id);
  248. $oItem->set_prevent_reinit($this->prevent_reinit);
  249. // Don't use reference here as the next loop will make the pointed object change.
  250. $this->items[$my_item_id] = $oItem;
  251. $this->refs_list[$oItem->ref] = $my_item_id;
  252. if ($this->debug > 2) {
  253. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  254. }
  255. }
  256. break;
  257. case 1:
  258. default:
  259. if ($this->debug > 2) {
  260. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  261. }
  262. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  263. if ($this->debug > 2) {
  264. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  265. }
  266. if (is_object($oItem)) {
  267. $my_item_id = $oItem->get_id();
  268. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  269. $oItem->set_prevent_reinit($this->prevent_reinit);
  270. // Don't use reference here as the next loop will make the pointed object change.
  271. $this->items[$my_item_id] = $oItem;
  272. $this->refs_list[$my_item_id] = $my_item_id;
  273. if ($this->debug > 2) {
  274. error_log(
  275. 'New LP - learnpath::__construct() ' . __LINE__ .
  276. ' - object with id ' . $my_item_id . ' set in items[]',
  277. 0
  278. );
  279. }
  280. }
  281. break;
  282. }
  283. // Setting the object level with variable $this->items[$i][parent]
  284. foreach ($this->items as $itemLPObject) {
  285. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  286. $itemLPObject->level = $level;
  287. }
  288. // Setting the view in the item object.
  289. if (is_object($this->items[$row['id']])) {
  290. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  291. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  292. $this->items[$row['id']]->current_start_time = 0;
  293. $this->items[$row['id']]->current_stop_time = 0;
  294. }
  295. }
  296. }
  297. if ($this->debug > 2) {
  298. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  299. }
  300. if (!empty($lp_item_id_list)) {
  301. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  302. if (!empty($lp_item_id_list_to_string)) {
  303. // Get last viewing vars.
  304. $itemViewTable = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $itemViewTable
  308. WHERE
  309. c_id = $course_id AND
  310. lp_view_id = ".$this->lp_view_id." AND
  311. lp_item_id IN ('".$lp_item_id_list_to_string."')
  312. ORDER BY view_count DESC ";
  313. if ($this->debug > 2) {
  314. error_log(
  315. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  316. 0
  317. );
  318. }
  319. $status_list = array();
  320. $res = Database::query($sql);
  321. while ($row = Database:: fetch_array($res)) {
  322. $status_list[$row['lp_item_id']] = $row['status'];
  323. }
  324. foreach ($lp_item_id_list as $item_id) {
  325. if (isset($status_list[$item_id])) {
  326. $status = $status_list[$item_id];
  327. if (is_object($this->items[$item_id])) {
  328. $this->items[$item_id]->set_status($status);
  329. if (empty($status)) {
  330. $this->items[$item_id]->set_status(
  331. $this->default_status
  332. );
  333. }
  334. }
  335. } else {
  336. if (!api_is_invitee()) {
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. if (!empty($this->lp_view_id)) {
  343. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  344. $params = [
  345. 'c_id' => $course_id,
  346. 'lp_item_id' => $course_id,
  347. 'lp_view_id' => $this->lp_view_id,
  348. 'view_count' => 1,
  349. 'status' => 'not attempted',
  350. 'start_time' => api_get_utc_datetime(),
  351. 'total_time' => 0,
  352. 'score' => 0
  353. ];
  354. $insertId = Database::insert($itemViewTable, $params);
  355. if ($insertId) {
  356. $sql = "UPDATE $itemViewTable SET id = iid
  357. WHERE iid = $insertId";
  358. Database::query($sql);
  359. }
  360. $this->items[$item_id]->set_lp_view(
  361. $this->lp_view_id,
  362. $course_id
  363. );
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. $this->ordered_items = self::get_flat_ordered_items_list(
  371. $this->get_id(),
  372. 0,
  373. $course_id
  374. );
  375. $this->max_ordered_items = 0;
  376. foreach ($this->ordered_items as $index => $dummy) {
  377. if ($index > $this->max_ordered_items && !empty($dummy)) {
  378. $this->max_ordered_items = $index;
  379. }
  380. }
  381. // TODO: Define the current item better.
  382. $this->first();
  383. if ($this->debug > 2) {
  384. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  385. }
  386. } else {
  387. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  388. }
  389. }
  390. }
  391. /**
  392. * @return string
  393. */
  394. public function getCourseCode()
  395. {
  396. return $this->cc;
  397. }
  398. /**
  399. * @return int
  400. */
  401. public function get_course_int_id()
  402. {
  403. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  404. }
  405. /**
  406. * @param $course_id
  407. * @return int
  408. */
  409. public function set_course_int_id($course_id)
  410. {
  411. return $this->course_int_id = (int) $course_id;
  412. }
  413. /**
  414. * Get the depth level of LP item
  415. * @param array $items
  416. * @param int $currentItemId
  417. * @return int
  418. */
  419. private static function get_level_for_item($items, $currentItemId)
  420. {
  421. $parentItemId = $items[$currentItemId]->parent;
  422. if ($parentItemId == 0) {
  423. return 0;
  424. } else {
  425. return learnpath::get_level_for_item($items, $parentItemId) + 1;
  426. }
  427. }
  428. /**
  429. * Function rewritten based on old_add_item() from Yannick Warnier.
  430. * Due the fact that users can decide where the item should come, I had to overlook this function and
  431. * I found it better to rewrite it. Old function is still available.
  432. * Added also the possibility to add a description.
  433. *
  434. * @param int $parent
  435. * @param int $previous
  436. * @param string $type
  437. * @param int $id resource ID (ref)
  438. * @param string $title
  439. * @param string $description
  440. * @param int $prerequisites
  441. * @param int $max_time_allowed
  442. * @param int $userId
  443. *
  444. * @return int
  445. */
  446. public function add_item(
  447. $parent,
  448. $previous,
  449. $type = 'dir',
  450. $id,
  451. $title,
  452. $description,
  453. $prerequisites = 0,
  454. $max_time_allowed = 0,
  455. $userId = 0
  456. ) {
  457. $course_id = $this->course_info['real_id'];
  458. if ($this->debug > 0) {
  459. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  460. }
  461. if (empty($course_id)) {
  462. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  463. $this->course_info = api_get_course_info($this->cc);
  464. $course_id = $this->course_info['real_id'];
  465. }
  466. $userId = empty($userId) ? api_get_user_id() : $userId;
  467. $sessionId = api_get_session_id();
  468. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  469. $_course = $this->course_info;
  470. $parent = intval($parent);
  471. $previous = intval($previous);
  472. $id = intval($id);
  473. $max_time_allowed = htmlentities($max_time_allowed);
  474. if (empty($max_time_allowed)) {
  475. $max_time_allowed = 0;
  476. }
  477. $sql = "SELECT COUNT(id) AS num
  478. FROM $tbl_lp_item
  479. WHERE
  480. c_id = $course_id AND
  481. lp_id = " . $this->get_id() . " AND
  482. parent_item_id = " . $parent;
  483. $res_count = Database::query($sql);
  484. $row = Database::fetch_array($res_count);
  485. $num = $row['num'];
  486. if ($num > 0) {
  487. if (empty($previous)) {
  488. $sql = "SELECT id, next_item_id, display_order
  489. FROM " . $tbl_lp_item . "
  490. WHERE
  491. c_id = $course_id AND
  492. lp_id = " . $this->get_id() . " AND
  493. parent_item_id = " . $parent . " AND
  494. previous_item_id = 0 OR
  495. previous_item_id=" . $parent;
  496. $result = Database::query($sql);
  497. $row = Database::fetch_array($result);
  498. $tmp_previous = 0;
  499. $next = $row['id'];
  500. $display_order = 0;
  501. } else {
  502. $previous = (int) $previous;
  503. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  504. FROM $tbl_lp_item
  505. WHERE
  506. c_id = $course_id AND
  507. lp_id = " . $this->get_id() . " AND
  508. id = " . $previous;
  509. $result = Database::query($sql);
  510. $row = Database:: fetch_array($result);
  511. $tmp_previous = $row['id'];
  512. $next = $row['next_item_id'];
  513. $display_order = $row['display_order'];
  514. }
  515. } else {
  516. $tmp_previous = 0;
  517. $next = 0;
  518. $display_order = 0;
  519. }
  520. $id = intval($id);
  521. $typeCleaned = Database::escape_string($type);
  522. if ($type == 'quiz') {
  523. $sql = 'SELECT SUM(ponderation)
  524. FROM ' . Database::get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  525. INNER JOIN ' . Database::get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  526. ON
  527. quiz_question.id = quiz_rel_question.question_id AND
  528. quiz_question.c_id = quiz_rel_question.c_id
  529. WHERE
  530. quiz_rel_question.exercice_id = '.$id." AND
  531. quiz_question.c_id = $course_id AND
  532. quiz_rel_question.c_id = $course_id ";
  533. $rsQuiz = Database::query($sql);
  534. $max_score = Database::result($rsQuiz, 0, 0);
  535. // Disabling the exercise if we add it inside a LP
  536. $exercise = new Exercise($course_id);
  537. $exercise->read($id);
  538. $exercise->disable();
  539. $exercise->save();
  540. } else {
  541. $max_score = 100;
  542. }
  543. $params = array(
  544. "c_id" => $course_id,
  545. "lp_id" => $this->get_id(),
  546. "item_type" => $typeCleaned,
  547. "ref" => '',
  548. "title" => $title,
  549. "description" => $description,
  550. "path" => $id,
  551. "max_score" => $max_score,
  552. "parent_item_id" => $parent,
  553. "previous_item_id" => $previous,
  554. "next_item_id" => intval($next),
  555. "display_order" => $display_order + 1,
  556. "prerequisite" => $prerequisites,
  557. "max_time_allowed" => $max_time_allowed,
  558. 'min_score' => 0,
  559. 'launch_data' => ''
  560. );
  561. if ($prerequisites != 0) {
  562. $params['prerequisite'] = $prerequisites;
  563. }
  564. $new_item_id = Database::insert($tbl_lp_item, $params);
  565. if ($this->debug > 2) {
  566. error_log('New LP - Inserting dir/chapter: ' . $new_item_id, 0);
  567. }
  568. if ($new_item_id) {
  569. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. $sql = "UPDATE $tbl_lp_item
  572. SET previous_item_id = $new_item_id
  573. WHERE c_id = $course_id AND id = $next";
  574. Database::query($sql);
  575. // Update the item that should be before the new item.
  576. $sql = "UPDATE $tbl_lp_item
  577. SET next_item_id = $new_item_id
  578. WHERE c_id = $course_id AND id = $tmp_previous";
  579. Database::query($sql);
  580. // Update all the items after the new item.
  581. $sql = "UPDATE " . $tbl_lp_item . "
  582. SET display_order = display_order + 1
  583. WHERE
  584. c_id = $course_id AND
  585. lp_id = " . $this->get_id() . " AND
  586. id <> " . $new_item_id . " AND
  587. parent_item_id = " . $parent . " AND
  588. display_order > " . $display_order;
  589. Database::query($sql);
  590. // Update the item that should come after the new item.
  591. $sql = "UPDATE " . $tbl_lp_item . "
  592. SET ref = " . $new_item_id . "
  593. WHERE c_id = $course_id AND id = " . $new_item_id;
  594. Database::query($sql);
  595. // Upload audio.
  596. if (!empty($_FILES['mp3']['name'])) {
  597. // Create the audio folder if it does not exist yet.
  598. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  599. if (!is_dir($filepath . 'audio')) {
  600. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  601. $audio_id = add_document(
  602. $_course,
  603. '/audio',
  604. 'folder',
  605. 0,
  606. 'audio',
  607. '',
  608. 0,
  609. true,
  610. null,
  611. $sessionId,
  612. $userId
  613. );
  614. api_item_property_update(
  615. $_course,
  616. TOOL_DOCUMENT,
  617. $audio_id,
  618. 'FolderCreated',
  619. $userId,
  620. null,
  621. null,
  622. null,
  623. null,
  624. $sessionId
  625. );
  626. api_item_property_update(
  627. $_course,
  628. TOOL_DOCUMENT,
  629. $audio_id,
  630. 'invisible',
  631. $userId,
  632. null,
  633. null,
  634. null,
  635. null,
  636. $sessionId
  637. );
  638. }
  639. $file_path = handle_uploaded_document(
  640. $_course,
  641. $_FILES['mp3'],
  642. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  643. '/audio',
  644. $userId,
  645. '',
  646. '',
  647. '',
  648. '',
  649. false
  650. );
  651. // Getting the filename only.
  652. $file_components = explode('/', $file_path);
  653. $file = $file_components[count($file_components) - 1];
  654. // Store the mp3 file in the lp_item table.
  655. $sql = "UPDATE $tbl_lp_item SET
  656. audio = '" . Database::escape_string($file) . "'
  657. WHERE id = '" . intval($new_item_id) . "'";
  658. Database::query($sql);
  659. }
  660. }
  661. return $new_item_id;
  662. }
  663. /**
  664. * Static admin function allowing addition of a learnpath to a course.
  665. * @param string Course code
  666. * @param string Learnpath name
  667. * @param string Learnpath description string, if provided
  668. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  669. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  670. * @param string Zip file containing the learnpath or directory containing the learnpath
  671. * @return integer The new learnpath ID on success, 0 on failure
  672. */
  673. public static function add_lp(
  674. $courseCode,
  675. $name,
  676. $description = '',
  677. $learnpath = 'guess',
  678. $origin = 'zip',
  679. $zipname = '',
  680. $publicated_on = '',
  681. $expired_on = '',
  682. $categoryId = 0,
  683. $userId = 0
  684. ) {
  685. global $charset;
  686. if (!empty($courseCode)) {
  687. $courseInfo = api_get_course_info($courseCode);
  688. $course_id = $courseInfo['real_id'];
  689. } else {
  690. $course_id = api_get_course_int_id();
  691. $courseInfo = api_get_course_info();
  692. }
  693. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  694. // Check course code exists.
  695. // Check lp_name doesn't exist, otherwise append something.
  696. $i = 0;
  697. $name = Database::escape_string($name);
  698. $categoryId = intval($categoryId);
  699. // Session id.
  700. $session_id = api_get_session_id();
  701. $userId = empty($userId) ? api_get_user_id() : $userId;
  702. $check_name = "SELECT * FROM $tbl_lp
  703. WHERE c_id = $course_id AND name = '$name'";
  704. $res_name = Database::query($check_name);
  705. if (empty($publicated_on)) {
  706. //by default the publication date is the same that the creation date
  707. //The behaviour above was changed due BT#2800
  708. // global $_custom;
  709. // if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  710. // $publicated_on = null;
  711. // } else {
  712. // $publicated_on = null;
  713. // }
  714. $publicated_on = null;
  715. } else {
  716. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  717. }
  718. if (empty($expired_on)) {
  719. $expired_on = null;
  720. } else {
  721. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  722. }
  723. while (Database::num_rows($res_name)) {
  724. // There is already one such name, update the current one a bit.
  725. $i++;
  726. $name = $name . ' - ' . $i;
  727. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  728. $res_name = Database::query($check_name);
  729. }
  730. // New name does not exist yet; keep it.
  731. // Escape description.
  732. // Kevin: added htmlentities().
  733. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  734. $type = 1;
  735. switch ($learnpath) {
  736. case 'guess':
  737. break;
  738. case 'dokeos':
  739. case 'chamilo':
  740. $type = 1;
  741. break;
  742. case 'aicc':
  743. break;
  744. }
  745. switch ($origin) {
  746. case 'zip':
  747. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  748. break;
  749. case 'manual':
  750. default:
  751. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  752. $res_max = Database::query($get_max);
  753. if (Database::num_rows($res_max) < 1) {
  754. $dsp = 1;
  755. } else {
  756. $row = Database::fetch_array($res_max);
  757. $dsp = $row[0] + 1;
  758. }
  759. $params = [
  760. 'c_id' => $course_id,
  761. 'lp_type' => $type,
  762. 'name' => $name,
  763. 'description' => $description,
  764. 'path' => '',
  765. 'default_view_mod' => 'embedded',
  766. 'default_encoding' => 'UTF-8',
  767. 'display_order' => $dsp,
  768. 'content_maker' => 'Chamilo',
  769. 'content_local' => 'local',
  770. 'js_lib' => '',
  771. 'session_id' => $session_id,
  772. 'created_on' => api_get_utc_datetime(),
  773. 'modified_on' => api_get_utc_datetime(),
  774. 'publicated_on' => $publicated_on,
  775. 'expired_on' => $expired_on,
  776. 'category_id' => $categoryId,
  777. 'force_commit' => 0,
  778. 'content_license' => '',
  779. 'debug' => 0,
  780. 'theme' => '',
  781. 'preview_image' => '',
  782. 'author' => '',
  783. 'prerequisite' => 0,
  784. 'hide_toc_frame' => 0,
  785. 'seriousgame_mode' => 0,
  786. 'autolaunch' => 0,
  787. 'max_attempts' => 0,
  788. 'subscribe_users' => 0,
  789. 'accumulate_scorm_time' => 1
  790. ];
  791. $id = Database::insert($tbl_lp, $params);
  792. if ($id > 0) {
  793. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  794. Database::query($sql);
  795. // Insert into item_property.
  796. api_item_property_update(
  797. $courseInfo,
  798. TOOL_LEARNPATH,
  799. $id,
  800. 'LearnpathAdded',
  801. $userId
  802. );
  803. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  804. return $id;
  805. }
  806. break;
  807. }
  808. }
  809. /**
  810. * Auto completes the parents of an item in case it's been completed or passed
  811. * @param int $item Optional ID of the item from which to look for parents
  812. */
  813. public function autocomplete_parents($item)
  814. {
  815. $debug = $this->debug;
  816. if ($debug) {
  817. error_log('Learnpath::autocomplete_parents()', 0);
  818. }
  819. if (empty($item)) {
  820. $item = $this->current;
  821. }
  822. $currentItem = $this->getItem($item);
  823. if ($currentItem) {
  824. $parent_id = $currentItem->get_parent();
  825. $parent = $this->getItem($parent_id);
  826. if ($parent) {
  827. // if $item points to an object and there is a parent.
  828. if ($debug) {
  829. error_log(
  830. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  831. 0
  832. );
  833. }
  834. // New experiment including failed and browsed in completed status.
  835. //$current_status = $currentItem->get_status();
  836. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  837. // Fixes chapter auto complete
  838. if (true) {
  839. // If the current item is completed or passes or succeeded.
  840. $updateParentStatus = true;
  841. if ($debug) {
  842. error_log('Status of current item is alright', 0);
  843. }
  844. foreach ($parent->get_children() as $childItemId) {
  845. $childItem = $this->getItem($childItemId);
  846. // If children was not set try to get the info
  847. if (empty($childItem->db_item_view_id)) {
  848. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  849. }
  850. // Check all his brothers (parent's children) for completion status.
  851. if ($childItemId != $item) {
  852. if ($debug) {
  853. error_log(
  854. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  855. 0
  856. );
  857. }
  858. // Trying completing parents of failed and browsed items as well.
  859. if ($childItem->status_is(
  860. array(
  861. 'completed',
  862. 'passed',
  863. 'succeeded',
  864. 'browsed',
  865. 'failed'
  866. )
  867. )
  868. ) {
  869. // Keep completion status to true.
  870. continue;
  871. } else {
  872. if ($debug > 2) {
  873. error_log(
  874. 'Found one incomplete child of parent #' . $parent_id . ': child #'.$childItemId . ' "' . $childItem->get_title() . '", is ' . $childItem->get_status().' db_item_view_id:#'.$childItem->db_item_view_id,
  875. 0
  876. );
  877. }
  878. $updateParentStatus = false;
  879. break;
  880. }
  881. }
  882. }
  883. if ($updateParentStatus) {
  884. // If all the children were completed:
  885. $parent->set_status('completed');
  886. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  887. // Force the status to "completed"
  888. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  889. $this->update_queue[$parent->get_id()] = 'completed';
  890. if ($debug) {
  891. error_log(
  892. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  893. print_r($this->update_queue, 1),
  894. 0
  895. );
  896. }
  897. // Recursive call.
  898. $this->autocomplete_parents($parent->get_id());
  899. }
  900. }
  901. } else {
  902. if ($debug) {
  903. error_log("Parent #$parent_id does not exists");
  904. }
  905. }
  906. } else {
  907. if ($debug) {
  908. error_log("#$item is an item that doesn't have parents");
  909. }
  910. }
  911. }
  912. /**
  913. * Auto saves the current results into the database for the whole learnpath
  914. * @todo: Add save operations for the learnpath itself.
  915. */
  916. public function autosave()
  917. {
  918. if ($this->debug > 0) {
  919. error_log('New LP - In learnpath::autosave()', 0);
  920. }
  921. }
  922. /**
  923. * Closes the current resource
  924. *
  925. * Stops the timer
  926. * Saves into the database if required
  927. * Clears the current resource data from this object
  928. * @return boolean True on success, false on failure
  929. */
  930. public function close()
  931. {
  932. if ($this->debug > 0) {
  933. error_log('New LP - In learnpath::close()', 0);
  934. }
  935. if (empty($this->lp_id)) {
  936. $this->error = 'Trying to close this learnpath but no ID is set';
  937. return false;
  938. }
  939. $this->current_time_stop = time();
  940. $this->ordered_items = array();
  941. $this->index = 0;
  942. unset($this->lp_id);
  943. //unset other stuff
  944. return true;
  945. }
  946. /**
  947. * Static admin function allowing removal of a learnpath
  948. * @param array $courseInfo
  949. * @param integer Learnpath ID
  950. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  951. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  952. */
  953. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  954. {
  955. $course_id = api_get_course_int_id();
  956. if (!empty($courseInfo)) {
  957. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  958. }
  959. // TODO: Implement a way of getting this to work when the current object is not set.
  960. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  961. // If an ID is specifically given and the current LP is not the same, prevent delete.
  962. if (!empty($id) && ($id != $this->lp_id)) {
  963. return false;
  964. }
  965. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  966. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  967. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  968. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  969. // Delete lp item id.
  970. foreach ($this->items as $id => $dummy) {
  971. $sql = "DELETE FROM $lp_item_view
  972. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  973. Database::query($sql);
  974. }
  975. // Proposed by Christophe (nickname: clefevre)
  976. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  977. Database::query($sql);
  978. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  979. Database::query($sql);
  980. self::toggle_publish($this->lp_id, 'i');
  981. if ($this->type == 2 || $this->type == 3) {
  982. // This is a scorm learning path, delete the files as well.
  983. $sql = "SELECT path FROM $lp
  984. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  985. $res = Database::query($sql);
  986. if (Database::num_rows($res) > 0) {
  987. $row = Database::fetch_array($res);
  988. $path = $row['path'];
  989. $sql = "SELECT id FROM $lp
  990. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  991. $res = Database::query($sql);
  992. if (Database::num_rows($res) > 0) {
  993. // Another learning path uses this directory, so don't delete it.
  994. if ($this->debug > 2) {
  995. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  996. }
  997. } else {
  998. // No other LP uses that directory, delete it.
  999. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  1000. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  1001. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  1002. if ($this->debug > 2) {
  1003. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1004. }
  1005. // Proposed by Christophe (clefevre).
  1006. if (strcmp(substr($path, -2), "/.") == 0) {
  1007. $path = substr($path, 0, -1); // Remove "." at the end.
  1008. }
  1009. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1010. rmdirr($course_scorm_dir . $path);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1016. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1017. // Delete tools
  1018. $sql = "DELETE FROM $tbl_tool
  1019. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1020. Database::query($sql);
  1021. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1022. Database::query($sql);
  1023. // Updates the display order of all lps.
  1024. $this->update_display_order();
  1025. api_item_property_update(
  1026. api_get_course_info(),
  1027. TOOL_LEARNPATH,
  1028. $this->lp_id,
  1029. 'delete',
  1030. api_get_user_id()
  1031. );
  1032. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1033. api_get_course_id(),
  1034. 4,
  1035. $id,
  1036. api_get_session_id()
  1037. );
  1038. if ($link_info !== false) {
  1039. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1040. }
  1041. if (api_get_setting('search_enabled') == 'true') {
  1042. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1043. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1044. }
  1045. }
  1046. /**
  1047. * Removes all the children of one item - dangerous!
  1048. * @param integer $id Element ID of which children have to be removed
  1049. * @return integer Total number of children removed
  1050. */
  1051. public function delete_children_items($id)
  1052. {
  1053. $course_id = $this->course_info['real_id'];
  1054. if ($this->debug > 0) {
  1055. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1056. }
  1057. $num = 0;
  1058. if (empty ($id) || $id != strval(intval($id))) {
  1059. return false;
  1060. }
  1061. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1062. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1063. $res = Database::query($sql);
  1064. while ($row = Database::fetch_array($res)) {
  1065. $num += $this->delete_children_items($row['id']);
  1066. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1067. Database::query($sql_del);
  1068. $num++;
  1069. }
  1070. return $num;
  1071. }
  1072. /**
  1073. * Removes an item from the current learnpath
  1074. * @param integer $id Elem ID (0 if first)
  1075. * @param integer $remove Whether to remove the resource/data from the
  1076. * system or leave it (default: 'keep', others 'remove')
  1077. * @return integer Number of elements moved
  1078. * @todo implement resource removal
  1079. */
  1080. public function delete_item($id, $remove = 'keep')
  1081. {
  1082. $course_id = api_get_course_int_id();
  1083. if ($this->debug > 0) {
  1084. error_log('New LP - In learnpath::delete_item()', 0);
  1085. }
  1086. // TODO: Implement the resource removal.
  1087. if (empty ($id) || $id != strval(intval($id))) {
  1088. return false;
  1089. }
  1090. // First select item to get previous, next, and display order.
  1091. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1092. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1093. $res_sel = Database::query($sql_sel);
  1094. if (Database::num_rows($res_sel) < 1) {
  1095. return false;
  1096. }
  1097. $row = Database::fetch_array($res_sel);
  1098. $previous = $row['previous_item_id'];
  1099. $next = $row['next_item_id'];
  1100. $display = $row['display_order'];
  1101. $parent = $row['parent_item_id'];
  1102. $lp = $row['lp_id'];
  1103. // Delete children items.
  1104. $num = $this->delete_children_items($id);
  1105. if ($this->debug > 2) {
  1106. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1107. }
  1108. // Now delete the item.
  1109. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1110. if ($this->debug > 2) {
  1111. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1112. }
  1113. Database::query($sql_del);
  1114. // Now update surrounding items.
  1115. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1116. WHERE c_id = ".$course_id." AND id = $previous";
  1117. Database::query($sql_upd);
  1118. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1119. WHERE c_id = ".$course_id." AND id = $next";
  1120. Database::query($sql_upd);
  1121. // Now update all following items with new display order.
  1122. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1123. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1124. Database::query($sql_all);
  1125. //Removing prerequisites since the item will not longer exist
  1126. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1127. Database::query($sql_all);
  1128. // Remove from search engine if enabled.
  1129. if (api_get_setting('search_enabled') == 'true') {
  1130. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1131. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1132. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1133. $res = Database::query($sql);
  1134. if (Database::num_rows($res) > 0) {
  1135. $row2 = Database::fetch_array($res);
  1136. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1137. $di = new ChamiloIndexer();
  1138. $di->remove_document((int) $row2['search_did']);
  1139. }
  1140. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1141. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1142. Database::query($sql);
  1143. }
  1144. }
  1145. /**
  1146. * Updates an item's content in place
  1147. * @param integer $id Element ID
  1148. * @param integer $parent Parent item ID
  1149. * @param integer $previous Previous item ID
  1150. * @param string $title Item title
  1151. * @param string $description Item description
  1152. * @param string $prerequisites Prerequisites (optional)
  1153. * @param array $audio The array resulting of the $_FILES[mp3] element
  1154. * @param int $max_time_allowed
  1155. * @param string $url
  1156. * @return boolean True on success, false on error
  1157. */
  1158. public function edit_item(
  1159. $id,
  1160. $parent,
  1161. $previous,
  1162. $title,
  1163. $description,
  1164. $prerequisites = '0',
  1165. $audio = array(),
  1166. $max_time_allowed = 0,
  1167. $url = ''
  1168. ) {
  1169. $course_id = api_get_course_int_id();
  1170. $_course = api_get_course_info();
  1171. if ($this->debug > 0) {
  1172. error_log('New LP - In learnpath::edit_item()', 0);
  1173. }
  1174. if (empty ($max_time_allowed)) {
  1175. $max_time_allowed = 0;
  1176. }
  1177. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1178. return false;
  1179. }
  1180. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1181. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1182. $res_select = Database::query($sql_select);
  1183. $row_select = Database::fetch_array($res_select);
  1184. $audio_update_sql = '';
  1185. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1186. // Create the audio folder if it does not exist yet.
  1187. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1188. if (!is_dir($filepath . 'audio')) {
  1189. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1190. $audio_id = add_document(
  1191. $_course,
  1192. '/audio',
  1193. 'folder',
  1194. 0,
  1195. 'audio'
  1196. );
  1197. api_item_property_update(
  1198. $_course,
  1199. TOOL_DOCUMENT,
  1200. $audio_id,
  1201. 'FolderCreated',
  1202. api_get_user_id(),
  1203. null,
  1204. null,
  1205. null,
  1206. null,
  1207. api_get_session_id()
  1208. );
  1209. api_item_property_update(
  1210. $_course,
  1211. TOOL_DOCUMENT,
  1212. $audio_id,
  1213. 'invisible',
  1214. api_get_user_id(),
  1215. null,
  1216. null,
  1217. null,
  1218. null,
  1219. api_get_session_id()
  1220. );
  1221. }
  1222. // Upload file in documents.
  1223. $pi = pathinfo($audio['name']);
  1224. if ($pi['extension'] == 'mp3') {
  1225. $c_det = api_get_course_info($this->cc);
  1226. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1227. $path = handle_uploaded_document(
  1228. $c_det,
  1229. $audio,
  1230. $bp,
  1231. '/audio',
  1232. api_get_user_id(),
  1233. 0,
  1234. null,
  1235. 0,
  1236. 'rename',
  1237. false,
  1238. 0
  1239. );
  1240. $path = substr($path, 7);
  1241. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1242. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1243. }
  1244. }
  1245. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1246. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1247. // TODO: htmlspecialchars to be checked for encoding related problems.
  1248. if ($same_parent && $same_previous) {
  1249. // Only update title and description.
  1250. $sql = "UPDATE " . $tbl_lp_item . "
  1251. SET title = '" . Database::escape_string($title) . "',
  1252. prerequisite = '" . $prerequisites . "',
  1253. description = '" . Database::escape_string($description) . "'
  1254. " . $audio_update_sql . ",
  1255. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1256. WHERE c_id = ".$course_id." AND id = " . $id;
  1257. Database::query($sql);
  1258. } else {
  1259. $old_parent = $row_select['parent_item_id'];
  1260. $old_previous = $row_select['previous_item_id'];
  1261. $old_next = $row_select['next_item_id'];
  1262. $old_order = $row_select['display_order'];
  1263. $old_prerequisite = $row_select['prerequisite'];
  1264. $old_max_time_allowed = $row_select['max_time_allowed'];
  1265. /* BEGIN -- virtually remove the current item id */
  1266. /* for the next and previous item it is like the current item doesn't exist anymore */
  1267. if ($old_previous != 0) {
  1268. // Next
  1269. $sql = "UPDATE " . $tbl_lp_item . "
  1270. SET next_item_id = " . $old_next . "
  1271. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1272. Database::query($sql);
  1273. }
  1274. if ($old_next != 0) {
  1275. // Previous
  1276. $sql = "UPDATE " . $tbl_lp_item . "
  1277. SET previous_item_id = " . $old_previous . "
  1278. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1279. Database::query($sql);
  1280. }
  1281. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1282. $sql = "UPDATE " . $tbl_lp_item . "
  1283. SET display_order = display_order - 1
  1284. WHERE
  1285. c_id = ".$course_id." AND
  1286. display_order > " . $old_order . " AND
  1287. lp_id = " . $this->lp_id . " AND
  1288. parent_item_id = " . $old_parent;
  1289. Database::query($sql);
  1290. /* END -- virtually remove the current item id */
  1291. /* BEGIN -- update the current item id to his new location */
  1292. if ($previous == 0) {
  1293. // Select the data of the item that should come after the current item.
  1294. $sql = "SELECT id, display_order
  1295. FROM " . $tbl_lp_item . "
  1296. WHERE
  1297. c_id = ".$course_id." AND
  1298. lp_id = " . $this->lp_id . " AND
  1299. parent_item_id = " . $parent . " AND
  1300. previous_item_id = " . $previous;
  1301. $res_select_old = Database::query($sql);
  1302. $row_select_old = Database::fetch_array($res_select_old);
  1303. // If the new parent didn't have children before.
  1304. if (Database::num_rows($res_select_old) == 0) {
  1305. $new_next = 0;
  1306. $new_order = 1;
  1307. } else {
  1308. $new_next = $row_select_old['id'];
  1309. $new_order = $row_select_old['display_order'];
  1310. }
  1311. } else {
  1312. // Select the data of the item that should come before the current item.
  1313. $sql = "SELECT next_item_id, display_order
  1314. FROM " . $tbl_lp_item . "
  1315. WHERE c_id = ".$course_id." AND id = " . $previous;
  1316. $res_select_old = Database::query($sql);
  1317. $row_select_old = Database::fetch_array($res_select_old);
  1318. $new_next = $row_select_old['next_item_id'];
  1319. $new_order = $row_select_old['display_order'] + 1;
  1320. }
  1321. // TODO: htmlspecialchars to be checked for encoding related problems.
  1322. // Update the current item with the new data.
  1323. $sql = "UPDATE " . $tbl_lp_item . "
  1324. SET
  1325. title = '" . Database::escape_string($title) . "',
  1326. description = '" . Database::escape_string($description) . "',
  1327. parent_item_id = " . $parent . ",
  1328. previous_item_id = " . $previous . ",
  1329. next_item_id = " . $new_next . ",
  1330. display_order = " . $new_order . "
  1331. " . $audio_update_sql . "
  1332. WHERE c_id = ".$course_id." AND id = " . $id;
  1333. Database::query($sql);
  1334. if ($previous != 0) {
  1335. // Update the previous item's next_item_id.
  1336. $sql = "UPDATE " . $tbl_lp_item . "
  1337. SET next_item_id = " . $id . "
  1338. WHERE c_id = ".$course_id." AND id = " . $previous;
  1339. Database::query($sql);
  1340. }
  1341. if ($new_next != 0) {
  1342. // Update the next item's previous_item_id.
  1343. $sql = "UPDATE " . $tbl_lp_item . "
  1344. SET previous_item_id = " . $id . "
  1345. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1346. Database::query($sql);
  1347. }
  1348. if ($old_prerequisite != $prerequisites) {
  1349. $sql = "UPDATE " . $tbl_lp_item . "
  1350. SET prerequisite = '" . $prerequisites . "'
  1351. WHERE c_id = ".$course_id." AND id = " . $id;
  1352. Database::query($sql);
  1353. }
  1354. if ($old_max_time_allowed != $max_time_allowed) {
  1355. // update max time allowed
  1356. $sql = "UPDATE " . $tbl_lp_item . "
  1357. SET max_time_allowed = " . $max_time_allowed . "
  1358. WHERE c_id = ".$course_id." AND id = " . $id;
  1359. Database::query($sql);
  1360. }
  1361. // Update all the items with the same or a bigger display_order than the current item.
  1362. $sql = "UPDATE " . $tbl_lp_item . "
  1363. SET display_order = display_order + 1
  1364. WHERE
  1365. c_id = ".$course_id." AND
  1366. lp_id = " . $this->get_id() . " AND
  1367. id <> " . $id . " AND
  1368. parent_item_id = " . $parent . " AND
  1369. display_order >= " . $new_order;
  1370. Database::query($sql);
  1371. }
  1372. if ($row_select['item_type'] == 'link') {
  1373. $link = new Link();
  1374. $linkId = $row_select['path'];
  1375. $link->updateLink($linkId, $url);
  1376. }
  1377. }
  1378. /**
  1379. * Updates an item's prereq in place
  1380. * @param integer $id Element ID
  1381. * @param string $prerequisite_id Prerequisite Element ID
  1382. * @param int $mastery_score Prerequisite min score
  1383. * @param int $max_score Prerequisite max score
  1384. *
  1385. * @return boolean True on success, false on error
  1386. */
  1387. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1388. {
  1389. $course_id = api_get_course_int_id();
  1390. if ($this->debug > 0) {
  1391. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1392. }
  1393. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1394. return false;
  1395. }
  1396. $prerequisite_id = intval($prerequisite_id);
  1397. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1398. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1399. $mastery_score = 0;
  1400. }
  1401. if (!is_numeric($max_score) || $max_score < 0) {
  1402. $max_score = 100;
  1403. }
  1404. /*if ($mastery_score > $max_score) {
  1405. $max_score = $mastery_score;
  1406. }*/
  1407. if (!is_numeric($prerequisite_id)) {
  1408. $prerequisite_id = 'NULL';
  1409. }
  1410. $mastery_score = floatval($mastery_score);
  1411. $max_score = floatval($max_score);
  1412. $sql = " UPDATE $tbl_lp_item
  1413. SET
  1414. prerequisite = $prerequisite_id ,
  1415. prerequisite_min_score = $mastery_score ,
  1416. prerequisite_max_score = $max_score
  1417. WHERE c_id = $course_id AND id = $id";
  1418. Database::query($sql);
  1419. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1420. // Will this be enough to ensure unicity?
  1421. /*$sql = " UPDATE $tbl_lp_item
  1422. SET mastery_score = $mastery_score
  1423. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1424. Database::query($sql);*/
  1425. }
  1426. // TODO: Update the item object (can be ignored for now because refreshed).
  1427. return true;
  1428. }
  1429. /**
  1430. * Static admin function exporting a learnpath into a zip file
  1431. * @param string Export type (scorm, zip, cd)
  1432. * @param string Course code
  1433. * @param integer Learnpath ID
  1434. * @param string Zip file name
  1435. * @return string Zip file path (or false on error)
  1436. */
  1437. public function export_lp($type, $course, $id, $zipname)
  1438. {
  1439. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1440. return false;
  1441. }
  1442. $url = '';
  1443. switch ($type) {
  1444. case 'scorm':
  1445. break;
  1446. case 'zip':
  1447. break;
  1448. case 'cdrom':
  1449. break;
  1450. }
  1451. return $url;
  1452. }
  1453. /**
  1454. * Gets all the chapters belonging to the same parent as the item/chapter given
  1455. * Can also be called as abstract method
  1456. * @param integer $id Item ID
  1457. * @return array A list of all the "brother items" (or an empty array on failure)
  1458. */
  1459. public function getSiblingDirectories($id)
  1460. {
  1461. $course_id = api_get_course_int_id();
  1462. if ($this->debug > 0) {
  1463. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1464. }
  1465. if (empty($id) || $id != strval(intval($id))) {
  1466. return array ();
  1467. }
  1468. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1469. $sql_parent = "SELECT * FROM $lp_item
  1470. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1471. $res_parent = Database::query($sql_parent);
  1472. if (Database::num_rows($res_parent) > 0) {
  1473. $row_parent = Database::fetch_array($res_parent);
  1474. $parent = $row_parent['parent_item_id'];
  1475. $sql = "SELECT * FROM $lp_item
  1476. WHERE
  1477. c_id = ".$course_id." AND
  1478. parent_item_id = $parent AND
  1479. id = $id AND
  1480. item_type='dir'
  1481. ORDER BY display_order";
  1482. $res_bros = Database::query($sql);
  1483. $list = array();
  1484. while ($row_bro = Database::fetch_array($res_bros)) {
  1485. $list[] = $row_bro;
  1486. }
  1487. return $list;
  1488. }
  1489. return array();
  1490. }
  1491. /**
  1492. * Gets all the items belonging to the same parent as the item given
  1493. * Can also be called as abstract method
  1494. * @param integer $id Item ID
  1495. * @return array A list of all the "brother items" (or an empty array on failure)
  1496. */
  1497. public function get_brother_items($id)
  1498. {
  1499. $course_id = api_get_course_int_id();
  1500. if ($this->debug > 0) {
  1501. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1502. }
  1503. if (empty ($id) || $id != strval(intval($id))) {
  1504. return array();
  1505. }
  1506. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1507. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1508. $res_parent = Database::query($sql_parent);
  1509. if (Database::num_rows($res_parent) > 0) {
  1510. $row_parent = Database::fetch_array($res_parent);
  1511. $parent = $row_parent['parent_item_id'];
  1512. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1513. ORDER BY display_order";
  1514. $res_bros = Database::query($sql_bros);
  1515. $list = [];
  1516. while ($row_bro = Database::fetch_array($res_bros)) {
  1517. $list[] = $row_bro;
  1518. }
  1519. return $list;
  1520. }
  1521. return [];
  1522. }
  1523. /**
  1524. * Get the specific prefix index terms of this learning path
  1525. * @param string $prefix
  1526. * @return array Array of terms
  1527. */
  1528. public function get_common_index_terms_by_prefix($prefix)
  1529. {
  1530. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1531. $terms = get_specific_field_values_list_by_prefix(
  1532. $prefix,
  1533. $this->cc,
  1534. TOOL_LEARNPATH,
  1535. $this->lp_id
  1536. );
  1537. $prefix_terms = array();
  1538. if (!empty($terms)) {
  1539. foreach ($terms as $term) {
  1540. $prefix_terms[] = $term['value'];
  1541. }
  1542. }
  1543. return $prefix_terms;
  1544. }
  1545. /**
  1546. * Gets the number of items currently completed
  1547. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1548. * @return integer The number of items currently completed
  1549. */
  1550. public function get_complete_items_count($failedStatusException = false)
  1551. {
  1552. if ($this->debug > 0) {
  1553. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1554. }
  1555. $i = 0;
  1556. $completedStatusList = array(
  1557. 'completed',
  1558. 'passed',
  1559. 'succeeded',
  1560. 'browsed'
  1561. );
  1562. if (!$failedStatusException) {
  1563. $completedStatusList[] = 'failed';
  1564. }
  1565. foreach ($this->items as $id => $dummy) {
  1566. // Trying failed and browsed considered "progressed" as well.
  1567. if ($this->items[$id]->status_is($completedStatusList) &&
  1568. $this->items[$id]->get_type() != 'dir'
  1569. ) {
  1570. $i++;
  1571. }
  1572. }
  1573. return $i;
  1574. }
  1575. /**
  1576. * Gets the current item ID
  1577. * @return integer The current learnpath item id
  1578. */
  1579. public function get_current_item_id()
  1580. {
  1581. $current = 0;
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1584. }
  1585. if (!empty($this->current)) {
  1586. $current = $this->current;
  1587. }
  1588. if ($this->debug > 2) {
  1589. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1590. }
  1591. return $current;
  1592. }
  1593. /**
  1594. * Force to get the first learnpath item id
  1595. * @return integer The current learnpath item id
  1596. */
  1597. public function get_first_item_id()
  1598. {
  1599. $current = 0;
  1600. if (is_array($this->ordered_items)) {
  1601. $current = $this->ordered_items[0];
  1602. }
  1603. return $current;
  1604. }
  1605. /**
  1606. * Gets the total number of items available for viewing in this SCORM
  1607. * @return integer The total number of items
  1608. */
  1609. public function get_total_items_count()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1613. }
  1614. return count($this->items);
  1615. }
  1616. /**
  1617. * Gets the total number of items available for viewing in this SCORM but without chapters
  1618. * @return integer The total no-chapters number of items
  1619. */
  1620. public function getTotalItemsCountWithoutDirs()
  1621. {
  1622. if ($this->debug > 0) {
  1623. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1624. }
  1625. $total = 0;
  1626. $typeListNotToCount = self::getChapterTypes();
  1627. foreach ($this->items as $temp2) {
  1628. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1629. $total++;
  1630. }
  1631. }
  1632. return $total;
  1633. }
  1634. /**
  1635. * Gets the first element URL.
  1636. * @return string URL to load into the viewer
  1637. */
  1638. public function first()
  1639. {
  1640. if ($this->debug > 0) {
  1641. error_log('New LP - In learnpath::first()', 0);
  1642. error_log('$this->last_item_seen '.$this->last_item_seen);
  1643. }
  1644. // Test if the last_item_seen exists and is not a dir.
  1645. if (count($this->ordered_items) == 0) {
  1646. $this->index = 0;
  1647. }
  1648. if ($this->debug > 0) {
  1649. if (isset($this->items[$this->last_item_seen])) {
  1650. $status = $this->items[$this->last_item_seen]->get_status();
  1651. }
  1652. error_log('status '.$status);
  1653. }
  1654. if (!empty($this->last_item_seen) &&
  1655. !empty($this->items[$this->last_item_seen]) &&
  1656. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1657. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1658. //&& !$this->items[$this->last_item_seen]->is_done()
  1659. ) {
  1660. if ($this->debug > 2) {
  1661. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1662. }
  1663. $index = -1;
  1664. foreach ($this->ordered_items as $myindex => $item_id) {
  1665. if ($item_id == $this->last_item_seen) {
  1666. $index = $myindex;
  1667. break;
  1668. }
  1669. }
  1670. if ($index == -1) {
  1671. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1672. if ($this->debug > 2) {
  1673. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1674. }
  1675. return false;
  1676. } else {
  1677. $this->last = $this->last_item_seen;
  1678. $this->current = $this->last_item_seen;
  1679. $this->index = $index;
  1680. }
  1681. } else {
  1682. if ($this->debug > 2) {
  1683. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1684. }
  1685. $index = 0;
  1686. // Loop through all ordered items and stop at the first item that is
  1687. // not a directory *and* that has not been completed yet.
  1688. while ( !empty($this->ordered_items[$index]) AND
  1689. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1690. (
  1691. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1692. $this->items[$this->ordered_items[$index]]->is_done() === true
  1693. ) AND $index < $this->max_ordered_items) {
  1694. $index++;
  1695. }
  1696. $this->last = $this->current;
  1697. // current is
  1698. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1699. $this->index = $index;
  1700. if ($this->debug > 2) {
  1701. error_log('$index ' . $index);
  1702. }
  1703. if ($this->debug > 2) {
  1704. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1705. }
  1706. }
  1707. if ($this->debug > 2) {
  1708. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1709. }
  1710. }
  1711. /**
  1712. * Gets the information about an item in a format usable as JavaScript to update
  1713. * the JS API by just printing this content into the <head> section of the message frame
  1714. * @param int $item_id
  1715. * @return string
  1716. */
  1717. public function get_js_info($item_id = 0)
  1718. {
  1719. if ($this->debug > 0) {
  1720. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1721. }
  1722. $info = '';
  1723. $item_id = intval($item_id);
  1724. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1725. //if item is defined, return values from DB
  1726. $oItem = $this->items[$item_id];
  1727. $info .= '<script language="javascript">';
  1728. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1729. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1730. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1731. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1732. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1733. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1734. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1735. $info .= "top.set_flag_synchronized();";
  1736. $info .= '</script>';
  1737. if ($this->debug > 2) {
  1738. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1739. }
  1740. return $info;
  1741. } else {
  1742. // If item_id is empty, just update to default SCORM data.
  1743. $info .= '<script language="javascript">';
  1744. $info .= "top.set_score(" . learnpathItem::get_score() . ");\n";
  1745. $info .= "top.set_max(" . learnpathItem::get_max() . ");\n";
  1746. $info .= "top.set_min(" . learnpathItem::get_min() . ");\n";
  1747. $info .= "top.set_lesson_status('" . learnpathItem::get_status() . "');";
  1748. $info .= "top.set_session_time('" . learnpathItem::getScormTimeFromParameter('js') . "');";
  1749. $info .= "top.set_suspend_data('" . learnpathItem::get_suspend_data() . "');";
  1750. $info .= "top.set_saved_lesson_status('" . learnpathItem::get_status() . "');";
  1751. $info .= "top.set_flag_synchronized();";
  1752. $info .= '</script>';
  1753. if ($this->debug > 2) {
  1754. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1755. }
  1756. return $info;
  1757. }
  1758. }
  1759. /**
  1760. * Gets the js library from the database
  1761. * @return string The name of the javascript library to be used
  1762. */
  1763. public function get_js_lib()
  1764. {
  1765. $lib = '';
  1766. if (!empty ($this->js_lib)) {
  1767. $lib = $this->js_lib;
  1768. }
  1769. return $lib;
  1770. }
  1771. /**
  1772. * Gets the learnpath database ID
  1773. * @return integer Learnpath ID in the lp table
  1774. */
  1775. public function get_id()
  1776. {
  1777. if (!empty ($this->lp_id)) {
  1778. return $this->lp_id;
  1779. } else {
  1780. return 0;
  1781. }
  1782. }
  1783. /**
  1784. * Gets the last element URL.
  1785. * @return string URL to load into the viewer
  1786. */
  1787. public function get_last()
  1788. {
  1789. if ($this->debug > 0) {
  1790. error_log('New LP - In learnpath::get_last()', 0);
  1791. }
  1792. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1793. if (count($this->ordered_items) > 0) {
  1794. $this->index = count($this->ordered_items) - 1;
  1795. return $this->ordered_items[$this->index];
  1796. }
  1797. return false;
  1798. }
  1799. /**
  1800. * Gets the navigation bar for the learnpath display screen
  1801. * @return string The HTML string to use as a navigation bar
  1802. */
  1803. public function get_navigation_bar($idBar = null, $display = null)
  1804. {
  1805. if ($this->debug > 0) {
  1806. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1807. }
  1808. if (empty($idBar)) {
  1809. $idBar = 'control-top';
  1810. }
  1811. $navbar = null;
  1812. $lp_id = $this->lp_id;
  1813. $mycurrentitemid = $this->get_current_item_id();
  1814. $reportingText = get_lang('Reporting');
  1815. $previousText = get_lang('ScormPrevious');
  1816. $nextText = get_lang('ScormNext');
  1817. $fullScreenText = get_lang('ScormExitFullScreen');
  1818. if ($this->mode == 'fullscreen') {
  1819. $navbar = '
  1820. <span id="'.$idBar.'" class="buttons">
  1821. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1822. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText . '</span>
  1823. </a>
  1824. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1825. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText . '</span>
  1826. </a>
  1827. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1828. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText. '</span>
  1829. </a>
  1830. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1831. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText . '</span>
  1832. </a>
  1833. </span>';
  1834. } else {
  1835. $navbar = '
  1836. <span id="'.$idBar.'" class="buttons text-right">
  1837. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1838. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText. '</span>
  1839. </a>
  1840. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1841. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText. '</span>
  1842. </a>
  1843. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1844. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText . '</span>
  1845. </a>
  1846. </span>';
  1847. }
  1848. return $navbar;
  1849. }
  1850. /**
  1851. * Gets the next resource in queue (url).
  1852. * @return string URL to load into the viewer
  1853. */
  1854. public function get_next_index()
  1855. {
  1856. if ($this->debug > 0) {
  1857. error_log('New LP - In learnpath::get_next_index()', 0);
  1858. }
  1859. // TODO
  1860. $index = $this->index;
  1861. $index++;
  1862. if ($this->debug > 2) {
  1863. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1864. }
  1865. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1866. $index++;
  1867. if ($index == $this->max_ordered_items){
  1868. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1869. return $this->index;
  1870. } else {
  1871. return $index;
  1872. }
  1873. }
  1874. }
  1875. if (empty ($this->ordered_items[$index])) {
  1876. return $this->index;
  1877. }
  1878. if ($this->debug > 2) {
  1879. error_log('New LP - index is now ' . $index, 0);
  1880. }
  1881. return $index;
  1882. }
  1883. /**
  1884. * Gets item_id for the next element
  1885. * @return integer Next item (DB) ID
  1886. */
  1887. public function get_next_item_id()
  1888. {
  1889. if ($this->debug > 0) {
  1890. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1891. }
  1892. $new_index = $this->get_next_index();
  1893. if (!empty ($new_index)) {
  1894. if (isset ($this->ordered_items[$new_index])) {
  1895. if ($this->debug > 2) {
  1896. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1897. }
  1898. return $this->ordered_items[$new_index];
  1899. }
  1900. }
  1901. if ($this->debug > 2) {
  1902. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1908. *
  1909. * Generally, the package provided is in the form of a zip file, so the function
  1910. * has been written to test a zip file. If not a zip, the function will return the
  1911. * default return value: ''
  1912. * @param string the path to the file
  1913. * @param string the original name of the file
  1914. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1915. */
  1916. public static function get_package_type($file_path, $file_name)
  1917. {
  1918. // Get name of the zip file without the extension.
  1919. $file_info = pathinfo($file_name);
  1920. $filename = $file_info['basename']; // Name including extension.
  1921. $extension = $file_info['extension']; // Extension only.
  1922. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1923. 'dll',
  1924. 'exe'
  1925. ))) {
  1926. return 'oogie';
  1927. }
  1928. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1929. 'dll',
  1930. 'exe'
  1931. ))) {
  1932. return 'woogie';
  1933. }
  1934. $zipFile = new PclZip($file_path);
  1935. // Check the zip content (real size and file extension).
  1936. $zipContentArray = $zipFile->listContent();
  1937. $package_type = '';
  1938. $at_root = false;
  1939. $manifest = '';
  1940. $aicc_match_crs = 0;
  1941. $aicc_match_au = 0;
  1942. $aicc_match_des = 0;
  1943. $aicc_match_cst = 0;
  1944. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1945. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1946. foreach ($zipContentArray as $thisContent) {
  1947. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1948. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1949. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1950. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1951. $package_type = 'scorm';
  1952. break; // Exit the foreach loop.
  1953. } elseif (
  1954. preg_match('/aicc\//i', $thisContent['filename']) ||
  1955. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1956. ) {
  1957. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1958. switch ($ext) {
  1959. case 'crs':
  1960. $aicc_match_crs = 1;
  1961. break;
  1962. case 'au':
  1963. $aicc_match_au = 1;
  1964. break;
  1965. case 'des':
  1966. $aicc_match_des = 1;
  1967. break;
  1968. case 'cst':
  1969. $aicc_match_cst = 1;
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1975. } else {
  1976. $package_type = '';
  1977. }
  1978. }
  1979. }
  1980. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1981. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1982. $package_type = 'aicc';
  1983. }
  1984. return $package_type;
  1985. }
  1986. /**
  1987. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1988. * @return string URL to load into the viewer
  1989. */
  1990. public function get_previous_index()
  1991. {
  1992. if ($this->debug > 0) {
  1993. error_log('New LP - In learnpath::get_previous_index()', 0);
  1994. }
  1995. $index = $this->index;
  1996. if (isset ($this->ordered_items[$index -1])) {
  1997. $index--;
  1998. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1999. $index--;
  2000. if ($index < 0) {
  2001. return $this->index;
  2002. }
  2003. }
  2004. } else {
  2005. if ($this->debug > 2) {
  2006. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2007. }
  2008. // There is no previous item.
  2009. }
  2010. return $index;
  2011. }
  2012. /**
  2013. * Gets item_id for the next element
  2014. * @return integer Previous item (DB) ID
  2015. */
  2016. public function get_previous_item_id()
  2017. {
  2018. if ($this->debug > 0) {
  2019. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2020. }
  2021. $new_index = $this->get_previous_index();
  2022. return $this->ordered_items[$new_index];
  2023. }
  2024. /**
  2025. * Gets the progress value from the progress_db attribute
  2026. * @return integer Current progress value
  2027. */
  2028. public function get_progress()
  2029. {
  2030. if ($this->debug > 0) {
  2031. error_log('New LP - In learnpath::get_progress()', 0);
  2032. }
  2033. if (!empty ($this->progress_db)) {
  2034. return $this->progress_db;
  2035. }
  2036. return 0;
  2037. }
  2038. /**
  2039. * Returns the HTML necessary to print a mediaplayer block inside a page
  2040. * @return string The mediaplayer HTML
  2041. */
  2042. public function get_mediaplayer($lpItemId, $autostart = 'true')
  2043. {
  2044. $course_id = api_get_course_int_id();
  2045. $_course = api_get_course_info();
  2046. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2047. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2048. // Getting all the information about the item.
  2049. $sql = "SELECT * FROM $tbl_lp_item as lp
  2050. INNER JOIN $tbl_lp_item_view as lp_view
  2051. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2052. WHERE
  2053. lp.id = '$lpItemId' AND
  2054. lp.c_id = $course_id AND
  2055. lp_view.c_id = $course_id";
  2056. $result = Database::query($sql);
  2057. $row = Database::fetch_assoc($result);
  2058. $output = '';
  2059. if (!empty ($row['audio'])) {
  2060. $list = $_SESSION['oLP']->get_toc();
  2061. $type_quiz = false;
  2062. foreach($list as $toc) {
  2063. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2064. $type_quiz = true;
  2065. }
  2066. }
  2067. if ($type_quiz) {
  2068. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2069. $autostart_audio = $row['status'] === 'completed' ? 'false' : 'true';
  2070. } else {
  2071. $autostart_audio = $autostart;
  2072. }
  2073. } else {
  2074. $autostart_audio = 'true';
  2075. }
  2076. $courseInfo = api_get_course_info();
  2077. $audio = $row['audio'];
  2078. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2079. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2080. if (!file_exists($file)) {
  2081. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2082. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2083. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2084. }
  2085. $player = Display::getMediaPlayer(
  2086. $file,
  2087. array(
  2088. 'id' => 'lp_audio_media_player',
  2089. 'url' => $url,
  2090. 'autoplay' => $autostart_audio,
  2091. 'width' => '100%'
  2092. )
  2093. );
  2094. // The mp3 player.
  2095. $output = '<div id="container">';
  2096. $output .= $player;
  2097. $output .= '</div>';
  2098. }
  2099. return $output;
  2100. }
  2101. /**
  2102. * @param int $studentId
  2103. * @param int $prerequisite
  2104. * @param array $courseInfo
  2105. * @param int $sessionId
  2106. *
  2107. * @return bool
  2108. *
  2109. */
  2110. public static function isBlockedByPrerequisite(
  2111. $studentId,
  2112. $prerequisite,
  2113. $courseInfo,
  2114. $sessionId
  2115. ) {
  2116. $isBlocked = false;
  2117. if (!empty($prerequisite)) {
  2118. $progress = self::getProgress(
  2119. $prerequisite,
  2120. $studentId,
  2121. $courseInfo['real_id'],
  2122. $sessionId
  2123. );
  2124. if ($progress < 100) {
  2125. $isBlocked = true;
  2126. }
  2127. }
  2128. return $isBlocked;
  2129. }
  2130. /**
  2131. * Checks if the learning path is visible for student after the progress
  2132. * of its prerequisite is completed, considering the time availability and
  2133. * the LP visibility.
  2134. * @param int $lp_id
  2135. * @param int $student_id
  2136. * @param string Course code (optional)
  2137. * @param int $sessionId
  2138. * @return bool
  2139. */
  2140. public static function is_lp_visible_for_student(
  2141. $lp_id,
  2142. $student_id,
  2143. $courseCode = null,
  2144. $sessionId = 0
  2145. ) {
  2146. $courseInfo = api_get_course_info($courseCode);
  2147. $lp_id = (int) $lp_id;
  2148. $sessionId = (int) $sessionId;
  2149. if (empty($sessionId)) {
  2150. $sessionId = api_get_session_id();
  2151. }
  2152. if (empty($courseInfo)) {
  2153. return false;
  2154. }
  2155. $itemInfo = api_get_item_property_info(
  2156. $courseInfo['real_id'],
  2157. TOOL_LEARNPATH,
  2158. $lp_id,
  2159. $sessionId
  2160. );
  2161. // If the item was deleted.
  2162. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2163. return false;
  2164. }
  2165. // @todo remove this query and load the row info as a parameter
  2166. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2167. // Get current prerequisite
  2168. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2169. FROM $tbl_learnpath
  2170. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2171. $rs = Database::query($sql);
  2172. $now = time();
  2173. if (Database::num_rows($rs) > 0) {
  2174. $row = Database::fetch_array($rs, 'ASSOC');
  2175. $prerequisite = $row['prerequisite'];
  2176. $is_visible = true;
  2177. $isBlocked = self::isBlockedByPrerequisite(
  2178. $student_id,
  2179. $prerequisite,
  2180. $courseInfo,
  2181. $sessionId
  2182. );
  2183. if ($isBlocked) {
  2184. $is_visible = false;
  2185. }
  2186. // Also check the time availability of the LP
  2187. if ($is_visible) {
  2188. // Adding visibility restrictions
  2189. if (!empty($row['publicated_on'])) {
  2190. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2191. $is_visible = false;
  2192. }
  2193. }
  2194. // Blocking empty start times see BT#2800
  2195. global $_custom;
  2196. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2197. $_custom['lps_hidden_when_no_start_date']
  2198. ) {
  2199. if (empty($row['publicated_on'])) {
  2200. $is_visible = false;
  2201. }
  2202. }
  2203. if (!empty($row['expired_on'])) {
  2204. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2205. $is_visible = false;
  2206. }
  2207. }
  2208. }
  2209. // Check if the subscription users/group to a LP is ON
  2210. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2211. // Try group
  2212. $is_visible = false;
  2213. // Checking only the user visibility
  2214. $userVisibility = api_get_item_visibility(
  2215. $courseInfo,
  2216. 'learnpath',
  2217. $row['id'],
  2218. $sessionId,
  2219. $student_id,
  2220. 'LearnpathSubscription'
  2221. );
  2222. if ($userVisibility == 1) {
  2223. $is_visible = true;
  2224. } else {
  2225. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2226. if (!empty($userGroups)) {
  2227. foreach ($userGroups as $groupInfo) {
  2228. $groupId = $groupInfo['iid'];
  2229. $userVisibility = api_get_item_visibility(
  2230. $courseInfo,
  2231. 'learnpath',
  2232. $row['id'],
  2233. $sessionId,
  2234. null,
  2235. 'LearnpathSubscription',
  2236. $groupId
  2237. );
  2238. if ($userVisibility == 1) {
  2239. $is_visible = true;
  2240. break;
  2241. }
  2242. }
  2243. }
  2244. }
  2245. }
  2246. return $is_visible;
  2247. }
  2248. return false;
  2249. }
  2250. /**
  2251. * @param int $lpId
  2252. * @param int $userId
  2253. * @param int $courseId
  2254. * @param int $sessionId
  2255. * @return int
  2256. */
  2257. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2258. {
  2259. $lpId = intval($lpId);
  2260. $userId = intval($userId);
  2261. $courseId = intval($courseId);
  2262. $sessionId = intval($sessionId);
  2263. $progress = 0;
  2264. $sessionCondition = api_get_session_condition($sessionId);
  2265. $table = Database::get_course_table(TABLE_LP_VIEW);
  2266. $sql = "SELECT * FROM $table
  2267. WHERE
  2268. c_id = ".$courseId." AND
  2269. lp_id = $lpId AND
  2270. user_id = $userId $sessionCondition";
  2271. $res = Database::query($sql);
  2272. if (Database::num_rows($res) > 0) {
  2273. $row = Database:: fetch_array($res);
  2274. $progress = $row['progress'];
  2275. }
  2276. return (int) $progress;
  2277. }
  2278. /**
  2279. * Displays a progress bar
  2280. * completed so far.
  2281. * @param integer $percentage Progress value to display
  2282. * @param string $text_add Text to display near the progress value
  2283. * @return string HTML string containing the progress bar
  2284. */
  2285. public static function get_progress_bar($percentage = -1, $text_add = '')
  2286. {
  2287. $text = $percentage . $text_add;
  2288. $output = '<div class="progress">
  2289. <div id="progress_bar_value" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2290. '. $text .'
  2291. </div>
  2292. </div>';
  2293. return $output;
  2294. }
  2295. /**
  2296. * @param string $mode can be '%' or 'abs'
  2297. * otherwise this value will be used $this->progress_bar_mode
  2298. * @return string
  2299. */
  2300. public function getProgressBar($mode = null)
  2301. {
  2302. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2303. return self::get_progress_bar($percentage, $text_add);
  2304. }
  2305. /**
  2306. * Gets the progress bar info to display inside the progress bar.
  2307. * Also used by scorm_api.php
  2308. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2309. * we display a number of completed elements per total elements
  2310. * @param integer $add Additional steps to fake as completed
  2311. * @return list array Percentage or number and symbol (% or /xx)
  2312. */
  2313. public function get_progress_bar_text($mode = '', $add = 0)
  2314. {
  2315. if ($this->debug > 0) {
  2316. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2317. }
  2318. if (empty($mode)) {
  2319. $mode = $this->progress_bar_mode;
  2320. }
  2321. $total_items = $this->getTotalItemsCountWithoutDirs();
  2322. if ($this->debug > 2) {
  2323. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2324. }
  2325. $completeItems = $this->get_complete_items_count();
  2326. if ($this->debug > 2) {
  2327. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2328. }
  2329. if ($add != 0) {
  2330. $completeItems += $add;
  2331. if ($this->debug > 2) {
  2332. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2333. }
  2334. }
  2335. $text = '';
  2336. if ($completeItems > $total_items) {
  2337. $completeItems = $total_items;
  2338. }
  2339. $percentage = 0;
  2340. if ($mode == '%') {
  2341. if ($total_items > 0) {
  2342. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2343. } else {
  2344. $percentage = 0;
  2345. }
  2346. $percentage = number_format($percentage, 0);
  2347. $text = '%';
  2348. } elseif ($mode == 'abs') {
  2349. $percentage = $completeItems;
  2350. $text = '/' . $total_items;
  2351. }
  2352. return array(
  2353. $percentage,
  2354. $text
  2355. );
  2356. }
  2357. /**
  2358. * Gets the progress bar mode
  2359. * @return string The progress bar mode attribute
  2360. */
  2361. public function get_progress_bar_mode()
  2362. {
  2363. if ($this->debug > 0) {
  2364. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2365. }
  2366. if (!empty ($this->progress_bar_mode)) {
  2367. return $this->progress_bar_mode;
  2368. } else {
  2369. return '%';
  2370. }
  2371. }
  2372. /**
  2373. * Gets the learnpath proximity (remote or local)
  2374. * @return string Learnpath proximity
  2375. */
  2376. public function get_proximity()
  2377. {
  2378. if ($this->debug > 0) {
  2379. error_log('New LP - In learnpath::get_proximity()', 0);
  2380. }
  2381. if (!empty ($this->proximity)) {
  2382. return $this->proximity;
  2383. } else {
  2384. return '';
  2385. }
  2386. }
  2387. /**
  2388. * Gets the learnpath theme (remote or local)
  2389. * @return string Learnpath theme
  2390. */
  2391. public function get_theme()
  2392. {
  2393. if ($this->debug > 0) {
  2394. error_log('New LP - In learnpath::get_theme()', 0);
  2395. }
  2396. if (!empty ($this->theme)) {
  2397. return $this->theme;
  2398. } else {
  2399. return '';
  2400. }
  2401. }
  2402. /**
  2403. * Gets the learnpath session id
  2404. * @return int
  2405. */
  2406. public function get_lp_session_id()
  2407. {
  2408. if ($this->debug > 0) {
  2409. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2410. }
  2411. if (!empty ($this->lp_session_id)) {
  2412. return (int) $this->lp_session_id;
  2413. } else {
  2414. return 0;
  2415. }
  2416. }
  2417. /**
  2418. * Gets the learnpath image
  2419. * @return string Web URL of the LP image
  2420. */
  2421. public function get_preview_image()
  2422. {
  2423. if ($this->debug > 0) {
  2424. error_log('New LP - In learnpath::get_preview_image()', 0);
  2425. }
  2426. if (!empty($this->preview_image)) {
  2427. return $this->preview_image;
  2428. } else {
  2429. return '';
  2430. }
  2431. }
  2432. /**
  2433. * @param string $size
  2434. * @param string $path_type
  2435. * @return bool|string
  2436. */
  2437. public function get_preview_image_path($size = null, $path_type = 'web')
  2438. {
  2439. $preview_image = $this->get_preview_image();
  2440. if (isset($preview_image) && !empty($preview_image)) {
  2441. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2442. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2443. if (isset($size)) {
  2444. $info = pathinfo($preview_image);
  2445. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2446. if (file_exists($image_sys_path.$image_custom_size)) {
  2447. if ($path_type == 'web') {
  2448. return $image_path.$image_custom_size;
  2449. } else {
  2450. return $image_sys_path.$image_custom_size;
  2451. }
  2452. }
  2453. } else {
  2454. if ($path_type == 'web') {
  2455. return $image_path.$preview_image;
  2456. } else {
  2457. return $image_sys_path.$preview_image;
  2458. }
  2459. }
  2460. }
  2461. return false;
  2462. }
  2463. /**
  2464. * Gets the learnpath author
  2465. * @return string LP's author
  2466. */
  2467. public function get_author()
  2468. {
  2469. if ($this->debug > 0) {
  2470. error_log('New LP - In learnpath::get_author()', 0);
  2471. }
  2472. if (!empty ($this->author)) {
  2473. return $this->author;
  2474. } else {
  2475. return '';
  2476. }
  2477. }
  2478. /**
  2479. * Gets the learnpath author
  2480. * @return string LP's author
  2481. */
  2482. public function get_hide_toc_frame()
  2483. {
  2484. if ($this->debug > 0) {
  2485. error_log('New LP - In learnpath::get_author()', 0);
  2486. }
  2487. if (!empty ($this->hide_toc_frame)) {
  2488. return $this->hide_toc_frame;
  2489. } else {
  2490. return '';
  2491. }
  2492. }
  2493. /**
  2494. * Generate a new prerequisites string for a given item. If this item was a sco and
  2495. * its prerequisites were strings (instead of IDs), then transform those strings into
  2496. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2497. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2498. * same rule as the scorm_export() method
  2499. * @param integer Item ID
  2500. * @return string Prerequisites string ready for the export as SCORM
  2501. */
  2502. public function get_scorm_prereq_string($item_id)
  2503. {
  2504. if ($this->debug > 0) {
  2505. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2506. }
  2507. if (!is_object($this->items[$item_id])) {
  2508. return false;
  2509. }
  2510. /** @var learnpathItem $oItem */
  2511. $oItem = $this->items[$item_id];
  2512. $prereq = $oItem->get_prereq_string();
  2513. if (empty($prereq)) {
  2514. return '';
  2515. }
  2516. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2517. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2518. // then simply return it (with the ITEM_ prefix).
  2519. //return 'ITEM_' . $prereq;
  2520. return $this->items[$prereq]->ref;
  2521. } else {
  2522. if (isset($this->refs_list[$prereq])) {
  2523. // It's a simple string item from which the ID can be found in the refs list,
  2524. // so we can transform it directly to an ID for export.
  2525. return $this->items[$this->refs_list[$prereq]]->ref;
  2526. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2527. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2528. } else {
  2529. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2530. // and replace them, one by one, by the internal IDs (chamilo db)
  2531. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2532. // by a space as well.
  2533. $find = array (
  2534. '&',
  2535. '|',
  2536. '~',
  2537. '=',
  2538. '<>',
  2539. '{',
  2540. '}',
  2541. '*',
  2542. '(',
  2543. ')'
  2544. );
  2545. $replace = array (
  2546. ' ',
  2547. ' ',
  2548. ' ',
  2549. ' ',
  2550. ' ',
  2551. ' ',
  2552. ' ',
  2553. ' ',
  2554. ' ',
  2555. ' '
  2556. );
  2557. $prereq_mod = str_replace($find, $replace, $prereq);
  2558. $ids = explode(' ', $prereq_mod);
  2559. foreach ($ids as $id) {
  2560. $id = trim($id);
  2561. if (isset ($this->refs_list[$id])) {
  2562. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2563. }
  2564. }
  2565. return $prereq;
  2566. }
  2567. }
  2568. }
  2569. /**
  2570. * Returns the XML DOM document's node
  2571. * @param resource Reference to a list of objects to search for the given ITEM_*
  2572. * @param string The identifier to look for
  2573. * @return mixed The reference to the element found with that identifier. False if not found
  2574. */
  2575. public function get_scorm_xml_node(& $children, $id)
  2576. {
  2577. for ($i = 0; $i < $children->length; $i++) {
  2578. $item_temp = $children->item($i);
  2579. if ($item_temp->nodeName == 'item') {
  2580. if ($item_temp->getAttribute('identifier') == $id) {
  2581. return $item_temp;
  2582. }
  2583. }
  2584. $subchildren = $item_temp->childNodes;
  2585. if ($subchildren && $subchildren->length > 0) {
  2586. $val = $this->get_scorm_xml_node($subchildren, $id);
  2587. if (is_object($val)) {
  2588. return $val;
  2589. }
  2590. }
  2591. }
  2592. return false;
  2593. }
  2594. /**
  2595. * Gets the status list for all LP's items
  2596. * @return array Array of [index] => [item ID => current status]
  2597. */
  2598. public function get_items_status_list()
  2599. {
  2600. if ($this->debug > 0) {
  2601. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2602. }
  2603. $list = array ();
  2604. foreach ($this->ordered_items as $item_id) {
  2605. $list[] = array (
  2606. $item_id => $this->items[$item_id]->get_status()
  2607. );
  2608. }
  2609. return $list;
  2610. }
  2611. /**
  2612. * Return the number of interactions for the given learnpath Item View ID.
  2613. * This method can be used as static.
  2614. * @param integer Item View ID
  2615. * @param integer course id
  2616. * @return integer Number of interactions
  2617. */
  2618. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2619. {
  2620. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2621. $lp_iv_id = intval($lp_iv_id);
  2622. $course_id = intval($course_id);
  2623. $sql = "SELECT count(*) FROM $table
  2624. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2625. $res = Database::query($sql);
  2626. $num = 0;
  2627. if (Database::num_rows($res)) {
  2628. $row = Database::fetch_array($res);
  2629. $num = $row[0];
  2630. }
  2631. return $num;
  2632. }
  2633. /**
  2634. * Return the interactions as an array for the given lp_iv_id.
  2635. * This method can be used as static.
  2636. * @param integer Learnpath Item View ID
  2637. * @return array
  2638. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2639. */
  2640. public static function get_iv_interactions_array($lp_iv_id)
  2641. {
  2642. $course_id = api_get_course_int_id();
  2643. $list = array();
  2644. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2645. if (empty($lp_iv_id)) {
  2646. return array();
  2647. }
  2648. $sql = "SELECT * FROM $table
  2649. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2650. ORDER BY order_id ASC";
  2651. $res = Database::query($sql);
  2652. $num = Database::num_rows($res);
  2653. if ($num > 0) {
  2654. $list[] = array (
  2655. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2656. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2657. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2658. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2659. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2660. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2661. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2662. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2663. );
  2664. while ($row = Database::fetch_array($res)) {
  2665. $list[] = array (
  2666. 'order_id' => ($row['order_id'] + 1),
  2667. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2668. 'type' => $row['interaction_type'],
  2669. 'time' => $row['completion_time'],
  2670. //'correct_responses' => $row['correct_responses'],
  2671. 'correct_responses' => '', // Hide correct responses from students.
  2672. 'student_response' => $row['student_response'],
  2673. 'result' => $row['result'],
  2674. 'latency' => $row['latency']
  2675. );
  2676. }
  2677. }
  2678. return $list;
  2679. }
  2680. /**
  2681. * Return the number of objectives for the given learnpath Item View ID.
  2682. * This method can be used as static.
  2683. * @param integer Item View ID
  2684. * @return integer Number of objectives
  2685. */
  2686. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2687. {
  2688. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2689. $course_id = intval($course_id);
  2690. $lp_iv_id = intval($lp_iv_id);
  2691. $sql = "SELECT count(*) FROM $table
  2692. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2693. //@todo seems that this always returns 0
  2694. $res = Database::query($sql);
  2695. $num = 0;
  2696. if (Database::num_rows($res)) {
  2697. $row = Database::fetch_array($res);
  2698. $num = $row[0];
  2699. }
  2700. return $num;
  2701. }
  2702. /**
  2703. * Return the objectives as an array for the given lp_iv_id.
  2704. * This method can be used as static.
  2705. * @param integer Learnpath Item View ID
  2706. * @return array
  2707. * @todo Translate labels
  2708. */
  2709. public static function get_iv_objectives_array($lp_iv_id = 0)
  2710. {
  2711. $course_id = api_get_course_int_id();
  2712. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2713. $sql = "SELECT * FROM $table
  2714. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2715. ORDER BY order_id ASC";
  2716. $res = Database::query($sql);
  2717. $num = Database::num_rows($res);
  2718. $list = array();
  2719. if ($num > 0) {
  2720. $list[] = array(
  2721. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2722. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2723. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2724. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2725. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2726. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2727. );
  2728. while ($row = Database::fetch_array($res)) {
  2729. $list[] = array (
  2730. 'order_id' => ($row['order_id'] + 1),
  2731. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2732. 'score_raw' => $row['score_raw'],
  2733. 'score_max' => $row['score_max'],
  2734. 'score_min' => $row['score_min'],
  2735. 'status' => $row['status']
  2736. );
  2737. }
  2738. }
  2739. return $list;
  2740. }
  2741. /**
  2742. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2743. * used by get_html_toc() to be ready to display
  2744. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2745. */
  2746. public function get_toc()
  2747. {
  2748. if ($this->debug > 0) {
  2749. error_log('learnpath::get_toc()', 0);
  2750. }
  2751. $toc = array();
  2752. foreach ($this->ordered_items as $item_id) {
  2753. if ($this->debug > 2) {
  2754. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2755. }
  2756. // TODO: Change this link generation and use new function instead.
  2757. $toc[] = array(
  2758. 'id' => $item_id,
  2759. 'title' => $this->items[$item_id]->get_title(),
  2760. 'status' => $this->items[$item_id]->get_status(),
  2761. 'level' => $this->items[$item_id]->get_level(),
  2762. 'type' => $this->items[$item_id]->get_type(),
  2763. 'description' => $this->items[$item_id]->get_description(),
  2764. 'path' => $this->items[$item_id]->get_path(),
  2765. );
  2766. }
  2767. if ($this->debug > 2) {
  2768. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2769. }
  2770. return $toc;
  2771. }
  2772. /**
  2773. * Generate and return the table of contents for this learnpath. The JS
  2774. * table returned is used inside of scorm_api.php
  2775. * @return string A JS array vairiable construction
  2776. */
  2777. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2778. {
  2779. if ($this->debug > 0) {
  2780. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2781. }
  2782. $toc = $varname.' = new Array();';
  2783. foreach ($this->ordered_items as $item_id) {
  2784. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2785. }
  2786. if ($this->debug > 2) {
  2787. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2788. }
  2789. return $toc;
  2790. }
  2791. /**
  2792. * Gets the learning path type
  2793. * @param boolean Return the name? If false, return the ID. Default is false.
  2794. * @return mixed Type ID or name, depending on the parameter
  2795. */
  2796. public function get_type($get_name = false)
  2797. {
  2798. $res = false;
  2799. if ($this->debug > 0) {
  2800. error_log('New LP - In learnpath::get_type()', 0);
  2801. }
  2802. if (!empty ($this->type)) {
  2803. if ($get_name) {
  2804. // Get it from the lp_type table in main db.
  2805. } else {
  2806. $res = $this->type;
  2807. }
  2808. }
  2809. if ($this->debug > 2) {
  2810. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2811. }
  2812. return $res;
  2813. }
  2814. /**
  2815. * Gets the learning path type as static method
  2816. * @param boolean Return the name? If false, return the ID. Default is false.
  2817. * @return mixed Type ID or name, depending on the parameter
  2818. */
  2819. public static function get_type_static($lp_id = 0)
  2820. {
  2821. $course_id = api_get_course_int_id();
  2822. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2823. $lp_id = intval($lp_id);
  2824. $sql = "SELECT lp_type FROM $tbl_lp
  2825. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2826. $res = Database::query($sql);
  2827. if ($res === false) {
  2828. return null;
  2829. }
  2830. if (Database::num_rows($res) <= 0) {
  2831. return null;
  2832. }
  2833. $row = Database::fetch_array($res);
  2834. return $row['lp_type'];
  2835. }
  2836. /**
  2837. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2838. * This method can be used as abstract and is recursive
  2839. * @param integer Learnpath ID
  2840. * @param integer Parent ID of the items to look for
  2841. * @return mixed Ordered list of item IDs or false on error
  2842. */
  2843. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2844. {
  2845. if (empty($course_id)) {
  2846. $course_id = api_get_course_int_id();
  2847. } else {
  2848. $course_id = intval($course_id);
  2849. }
  2850. $list = array();
  2851. if (empty($lp)) {
  2852. return false;
  2853. }
  2854. $lp = intval($lp);
  2855. $parent = intval($parent);
  2856. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2857. $sql = "SELECT id FROM $tbl_lp_item
  2858. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2859. ORDER BY display_order";
  2860. $res = Database::query($sql);
  2861. while ($row = Database::fetch_array($res)) {
  2862. $sublist = learnpath::get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2863. $list[] = $row['id'];
  2864. foreach ($sublist as $item) {
  2865. $list[] = $item;
  2866. }
  2867. }
  2868. return $list;
  2869. }
  2870. /**
  2871. * @return array
  2872. */
  2873. public static function getChapterTypes()
  2874. {
  2875. return array(
  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. if ($this->debug > 0) {
  2886. error_log('In learnpath::get_html_toc()', 0);
  2887. }
  2888. if (empty($toc_list)) {
  2889. $toc_list = $this->get_toc();
  2890. }
  2891. $html = '<div class="scorm-body">';
  2892. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2893. // Temporary variables.
  2894. $mycurrentitemid = $this->get_current_item_id();
  2895. $color_counter = 0;
  2896. $i = 0;
  2897. foreach ($toc_list as $item) {
  2898. // Style Status
  2899. $class_name = [
  2900. 'not attempted' => 'scorm_not_attempted',
  2901. 'incomplete' => 'scorm_not_attempted',
  2902. 'failed' => 'scorm_failed',
  2903. 'completed' => 'scorm_completed',
  2904. 'passed' => 'scorm_completed',
  2905. 'succeeded' => 'scorm_completed',
  2906. 'browsed' => 'scorm_completed',
  2907. ];
  2908. $rowColor = ' ';
  2909. $dirTypes = self::getChapterTypes();
  2910. if (in_array($item['type'], $dirTypes)) {
  2911. $rowColor ='scorm_item_section ';
  2912. }
  2913. if ($item['id'] == $this->current) {
  2914. $rowColor = 'scorm_item_normal '.$rowColor.' scorm_highlight';
  2915. } elseif (!in_array($item['type'], $dirTypes)) {
  2916. $rowColor = 'scorm_item_normal '.$rowColor.' ';
  2917. }
  2918. $html .= '<div id="toc_' . $item['id'] . '" class="' . $rowColor . ' ' . $class_name[$item['status']] . '">';
  2919. // Learning path title
  2920. $title = $item['title'];
  2921. if (empty ($title)) {
  2922. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2923. }
  2924. $title = Security::remove_XSS($title);
  2925. // Learning path personalization
  2926. // build the LP tree
  2927. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2928. $description = $item['description'];
  2929. if (empty($description)) {
  2930. $description = $title;
  2931. }
  2932. if (in_array($item['type'], $dirTypes)) {
  2933. // Chapters
  2934. $html .= '<div class="section level_'.$item['level'].'" title="'.$description.'" >';
  2935. } else {
  2936. $html .= '<div class="item level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2937. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2938. }
  2939. if (in_array($item['type'], $dirTypes)) {
  2940. // Chapter
  2941. // if you want to put an image before, you should use css
  2942. $html .= stripslashes($title);
  2943. } else {
  2944. $this->get_link('http', $item['id'], $toc_list);
  2945. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2946. }
  2947. $html .= "</div>";
  2948. if ($rowColor != '') {
  2949. $html .= '</div>';
  2950. }
  2951. $color_counter++;
  2952. }
  2953. $html .= "</div>";
  2954. $html .= "</div>";
  2955. return $html;
  2956. }
  2957. /**
  2958. * Returns an HTML-formatted string ready to display with teacher buttons
  2959. * in LP view menu
  2960. * @return string HTML TOC ready to display
  2961. */
  2962. public function get_teacher_toc_buttons()
  2963. {
  2964. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2965. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2966. $html = '';
  2967. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2968. $gradebook = '';
  2969. if (!empty($_GET['gradebook'])) {
  2970. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2971. }
  2972. if ($this->get_lp_session_id() == api_get_session_id()) {
  2973. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2974. $html .= '<div class="btn-group">';
  2975. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  2976. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2977. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  2978. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2979. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'&isStudentView=false">' .
  2980. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2981. $html .= '</div>';
  2982. $html .= '</div>';
  2983. }
  2984. }
  2985. return $html;
  2986. }
  2987. /**
  2988. * Gets the learnpath maker name - generally the editor's name
  2989. * @return string Learnpath maker name
  2990. */
  2991. public function get_maker()
  2992. {
  2993. if ($this->debug > 0) {
  2994. error_log('New LP - In learnpath::get_maker()', 0);
  2995. }
  2996. if (!empty ($this->maker)) {
  2997. return $this->maker;
  2998. } else {
  2999. return '';
  3000. }
  3001. }
  3002. /**
  3003. * Gets the learnpath name/title
  3004. * @return string Learnpath name/title
  3005. */
  3006. public function get_name()
  3007. {
  3008. if ($this->debug > 0) {
  3009. error_log('New LP - In learnpath::get_name()', 0);
  3010. }
  3011. if (!empty ($this->name)) {
  3012. return $this->name;
  3013. } else {
  3014. return 'N/A';
  3015. }
  3016. }
  3017. /**
  3018. * Gets a link to the resource from the present location, depending on item ID.
  3019. * @param string $type Type of link expected
  3020. * @param integer $item_id Learnpath item ID
  3021. * @return string $provided_toc Link to the lp_item resource
  3022. */
  3023. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3024. {
  3025. $course_id = $this->get_course_int_id();
  3026. if ($this->debug > 0) {
  3027. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3028. }
  3029. if (empty($item_id)) {
  3030. if ($this->debug > 2) {
  3031. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3032. }
  3033. $item_id = $this->get_current_item_id();
  3034. }
  3035. if (empty($item_id)) {
  3036. if ($this->debug > 2) {
  3037. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3038. }
  3039. //still empty, this means there was no item_id given and we are not in an object context or
  3040. //the object property is empty, return empty link
  3041. $item_id = $this->first();
  3042. return '';
  3043. }
  3044. $file = '';
  3045. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3046. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3047. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3048. $item_id = intval($item_id);
  3049. $sql = "SELECT
  3050. l.lp_type as ltype,
  3051. l.path as lpath,
  3052. li.item_type as litype,
  3053. li.path as lipath,
  3054. li.parameters as liparams
  3055. FROM $lp_table l
  3056. INNER JOIN $lp_item_table li
  3057. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3058. WHERE li.id = $item_id ";
  3059. if ($this->debug > 2) {
  3060. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3061. }
  3062. $res = Database::query($sql);
  3063. if (Database::num_rows($res) > 0) {
  3064. $row = Database::fetch_array($res);
  3065. $lp_type = $row['ltype'];
  3066. $lp_path = $row['lpath'];
  3067. $lp_item_type = $row['litype'];
  3068. $lp_item_path = $row['lipath'];
  3069. $lp_item_params = $row['liparams'];
  3070. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3071. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3072. }
  3073. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3074. if ($type == 'http') {
  3075. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3076. } else {
  3077. $course_path = $sys_course_path; //system path
  3078. }
  3079. // 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.
  3080. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3081. $lp_type = 1;
  3082. }
  3083. if ($this->debug > 2) {
  3084. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3085. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3086. }
  3087. // Now go through the specific cases to get the end of the path
  3088. // @todo Use constants instead of int values.
  3089. switch ($lp_type) {
  3090. case 1 :
  3091. $file = self::rl_get_resource_link_for_learnpath(
  3092. $course_id,
  3093. $this->get_id(),
  3094. $item_id,
  3095. $this->get_view_id()
  3096. );
  3097. if ($this->debug > 0) {
  3098. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3099. }
  3100. switch ($lp_item_type) {
  3101. case 'dir':
  3102. $file = 'lp_content.php?type=dir';
  3103. break;
  3104. case 'link':
  3105. if (Link::is_youtube_link($file)) {
  3106. $src = Link::get_youtube_video_id($file);
  3107. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=youtube&source='.$src;
  3108. } elseif (Link::isVimeoLink($file)) {
  3109. $src = Link::getVimeoLinkId($file);
  3110. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=vimeo&source='.$src;
  3111. } else {
  3112. // If the current site is HTTPS and the link is
  3113. // HTTP, browsers will refuse opening the link
  3114. $urlId = api_get_current_access_url_id();
  3115. $url = api_get_access_url($urlId, false);
  3116. $protocol = substr($url['url'], 0, 5);
  3117. if ($protocol === 'https') {
  3118. $linkProtocol = substr($file, 0, 5);
  3119. if ($linkProtocol === 'http:') {
  3120. //this is the special intervention case
  3121. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3122. }
  3123. }
  3124. }
  3125. break;
  3126. case 'quiz':
  3127. // Check how much attempts of a exercise exits in lp
  3128. $lp_item_id = $this->get_current_item_id();
  3129. $lp_view_id = $this->get_view_id();
  3130. $prevent_reinit = null;
  3131. if (isset($this->items[$this->current])) {
  3132. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3133. }
  3134. if (empty($provided_toc)) {
  3135. if ($this->debug > 0) {
  3136. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3137. }
  3138. $list = $this->get_toc();
  3139. } else {
  3140. if ($this->debug > 0) {
  3141. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3142. }
  3143. $list = $provided_toc;
  3144. }
  3145. $type_quiz = false;
  3146. foreach ($list as $toc) {
  3147. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3148. $type_quiz = true;
  3149. }
  3150. }
  3151. if ($type_quiz) {
  3152. $lp_item_id = intval($lp_item_id);
  3153. $lp_view_id = intval($lp_view_id);
  3154. $sql = "SELECT count(*) FROM $lp_item_view_table
  3155. WHERE
  3156. c_id = $course_id AND
  3157. lp_item_id='".$lp_item_id."' AND
  3158. lp_view_id ='".$lp_view_id."' AND
  3159. status='completed'";
  3160. $result = Database::query($sql);
  3161. $row_count = Database:: fetch_row($result);
  3162. $count_item_view = (int)$row_count[0];
  3163. $not_multiple_attempt = 0;
  3164. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3165. $not_multiple_attempt = 1;
  3166. }
  3167. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3168. }
  3169. break;
  3170. }
  3171. $tmp_array = explode('/', $file);
  3172. $document_name = $tmp_array[count($tmp_array) - 1];
  3173. if (strpos($document_name, '_DELETED_')) {
  3174. $file = 'blank.php?error=document_deleted';
  3175. }
  3176. break;
  3177. case 2 :
  3178. if ($this->debug > 2) {
  3179. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3180. }
  3181. if ($lp_item_type != 'dir') {
  3182. // Quite complex here:
  3183. // We want to make sure 'http://' (and similar) links can
  3184. // be loaded as is (withouth the Chamilo path in front) but
  3185. // some contents use this form: resource.htm?resource=http://blablabla
  3186. // which means we have to find a protocol at the path's start, otherwise
  3187. // it should not be considered as an external URL.
  3188. //if ($this->prerequisites_match($item_id)) {
  3189. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3190. if ($this->debug > 2) {
  3191. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3192. }
  3193. // Distant url, return as is.
  3194. $file = $lp_item_path;
  3195. } else {
  3196. if ($this->debug > 2) {
  3197. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3198. }
  3199. // Prevent getting untranslatable urls.
  3200. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3201. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3202. // Prepare the path.
  3203. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3204. // TODO: Fix this for urls with protocol header.
  3205. $file = str_replace('//', '/', $file);
  3206. $file = str_replace(':/', '://', $file);
  3207. if (substr($lp_path, -1) == '/') {
  3208. $lp_path = substr($lp_path, 0, -1);
  3209. }
  3210. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3211. // if file not found.
  3212. $decoded = html_entity_decode($lp_item_path);
  3213. list ($decoded) = explode('?', $decoded);
  3214. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3215. $file = self::rl_get_resource_link_for_learnpath(
  3216. $course_id,
  3217. $this->get_id(),
  3218. $item_id,
  3219. $this->get_view_id()
  3220. );
  3221. if (empty($file)) {
  3222. $file = 'blank.php?error=document_not_found';
  3223. } else {
  3224. $tmp_array = explode('/', $file);
  3225. $document_name = $tmp_array[count($tmp_array) - 1];
  3226. if (strpos($document_name, '_DELETED_')) {
  3227. $file = 'blank.php?error=document_deleted';
  3228. } else {
  3229. $file = 'blank.php?error=document_not_found';
  3230. }
  3231. }
  3232. } else {
  3233. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3234. }
  3235. }
  3236. }
  3237. // We want to use parameters if they were defined in the imsmanifest
  3238. if (strpos($file, 'blank.php') === false) {
  3239. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3240. }
  3241. } else {
  3242. $file = 'lp_content.php?type=dir';
  3243. }
  3244. break;
  3245. case 3 :
  3246. if ($this->debug > 2) {
  3247. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3248. }
  3249. // Formatting AICC HACP append URL.
  3250. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'lp/aicc_hacp.php') . '&';
  3251. if (!empty($lp_item_params)) {
  3252. $aicc_append .= $lp_item_params . '&';
  3253. }
  3254. if ($lp_item_type != 'dir') {
  3255. // Quite complex here:
  3256. // We want to make sure 'http://' (and similar) links can
  3257. // be loaded as is (withouth the Chamilo path in front) but
  3258. // some contents use this form: resource.htm?resource=http://blablabla
  3259. // which means we have to find a protocol at the path's start, otherwise
  3260. // it should not be considered as an external URL.
  3261. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3262. if ($this->debug > 2) {
  3263. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3264. }
  3265. // Distant url, return as is.
  3266. $file = $lp_item_path;
  3267. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3268. /*
  3269. if (stristr($file,'<servername>') !== false) {
  3270. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3271. }
  3272. */
  3273. if (stripos($file, '<servername>') !== false) {
  3274. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3275. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3276. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3277. }
  3278. //
  3279. $file .= $aicc_append;
  3280. } else {
  3281. if ($this->debug > 2) {
  3282. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3283. }
  3284. // Prevent getting untranslatable urls.
  3285. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3286. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3287. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3288. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3289. // TODO: Fix this for urls with protocol header.
  3290. $file = str_replace('//', '/', $file);
  3291. $file = str_replace(':/', '://', $file);
  3292. $file .= $aicc_append;
  3293. }
  3294. } else {
  3295. $file = 'lp_content.php?type=dir';
  3296. }
  3297. break;
  3298. case 4 :
  3299. break;
  3300. default :
  3301. break;
  3302. }
  3303. // Replace &amp; by & because &amp; will break URL with params
  3304. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3305. }
  3306. if ($this->debug > 2) {
  3307. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3308. }
  3309. return $file;
  3310. }
  3311. /**
  3312. * Gets the latest usable view or generate a new one
  3313. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3314. * @return integer DB lp_view id
  3315. */
  3316. public function get_view($attempt_num = 0)
  3317. {
  3318. if ($this->debug > 0) {
  3319. error_log('New LP - In learnpath::get_view()', 0);
  3320. }
  3321. $search = '';
  3322. // Use $attempt_num to enable multi-views management (disabled so far).
  3323. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3324. $search = 'AND view_count = ' . $attempt_num;
  3325. }
  3326. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3327. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3328. $course_id = api_get_course_int_id();
  3329. $sessionId = api_get_session_id();
  3330. $sql = "SELECT id, view_count FROM $lp_view_table
  3331. WHERE
  3332. c_id = " . $course_id . " AND
  3333. lp_id = " . $this->get_id() . " AND
  3334. user_id = " . $this->get_user_id() . " AND
  3335. session_id = $sessionId
  3336. $search
  3337. ORDER BY view_count DESC";
  3338. $res = Database::query($sql);
  3339. if (Database::num_rows($res) > 0) {
  3340. $row = Database::fetch_array($res);
  3341. $this->lp_view_id = $row['id'];
  3342. } else if (!api_is_invitee()) {
  3343. // There is no database record, create one.
  3344. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3345. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3346. Database::query($sql);
  3347. $id = Database::insert_id();
  3348. $this->lp_view_id = $id;
  3349. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3350. Database::query($sql);
  3351. }
  3352. return $this->lp_view_id;
  3353. }
  3354. /**
  3355. * Gets the current view id
  3356. * @return integer View ID (from lp_view)
  3357. */
  3358. public function get_view_id()
  3359. {
  3360. if ($this->debug > 0) {
  3361. error_log('New LP - In learnpath::get_view_id()', 0);
  3362. }
  3363. if (!empty ($this->lp_view_id)) {
  3364. return $this->lp_view_id;
  3365. } else {
  3366. return 0;
  3367. }
  3368. }
  3369. /**
  3370. * Gets the update queue
  3371. * @return array Array containing IDs of items to be updated by JavaScript
  3372. */
  3373. public function get_update_queue()
  3374. {
  3375. if ($this->debug > 0) {
  3376. error_log('New LP - In learnpath::get_update_queue()', 0);
  3377. }
  3378. return $this->update_queue;
  3379. }
  3380. /**
  3381. * Gets the user ID
  3382. * @return integer User ID
  3383. */
  3384. public function get_user_id()
  3385. {
  3386. if ($this->debug > 0) {
  3387. error_log('New LP - In learnpath::get_user_id()', 0);
  3388. }
  3389. if (!empty ($this->user_id)) {
  3390. return $this->user_id;
  3391. } else {
  3392. return false;
  3393. }
  3394. }
  3395. /**
  3396. * Checks if any of the items has an audio element attached
  3397. * @return bool True or false
  3398. */
  3399. public function has_audio()
  3400. {
  3401. if ($this->debug > 1) {
  3402. error_log('New LP - In learnpath::has_audio()', 0);
  3403. }
  3404. $has = false;
  3405. foreach ($this->items as $i => $item) {
  3406. if (!empty ($this->items[$i]->audio)) {
  3407. $has = true;
  3408. break;
  3409. }
  3410. }
  3411. return $has;
  3412. }
  3413. /**
  3414. * Logs a message into a file
  3415. * @param string Message to log
  3416. * @return boolean True on success, false on error or if msg empty
  3417. */
  3418. public function log($msg)
  3419. {
  3420. if ($this->debug > 0) {
  3421. error_log('New LP - In learnpath::log()', 0);
  3422. }
  3423. // TODO
  3424. $this->error .= $msg;
  3425. return true;
  3426. }
  3427. /**
  3428. * Moves an item up and down at its level
  3429. * @param integer Item to move up and down
  3430. * @param string Direction 'up' or 'down'
  3431. * @return integer New display order, or false on error
  3432. */
  3433. public function move_item($id, $direction)
  3434. {
  3435. $course_id = api_get_course_int_id();
  3436. if ($this->debug > 0) {
  3437. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3438. }
  3439. if (empty($id) || empty($direction)) {
  3440. return false;
  3441. }
  3442. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3443. $sql_sel = "SELECT *
  3444. FROM " . $tbl_lp_item . "
  3445. WHERE c_id = ".$course_id." AND id = " . $id;
  3446. $res_sel = Database::query($sql_sel);
  3447. // Check if elem exists.
  3448. if (Database::num_rows($res_sel) < 1) {
  3449. return false;
  3450. }
  3451. // Gather data.
  3452. $row = Database::fetch_array($res_sel);
  3453. $previous = $row['previous_item_id'];
  3454. $next = $row['next_item_id'];
  3455. $display = $row['display_order'];
  3456. $parent = $row['parent_item_id'];
  3457. $lp = $row['lp_id'];
  3458. // Update the item (switch with previous/next one).
  3459. switch ($direction) {
  3460. case 'up':
  3461. if ($this->debug > 2) {
  3462. error_log('Movement up detected', 0);
  3463. }
  3464. if ($display <= 1) { /*do nothing*/
  3465. } else {
  3466. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3467. WHERE c_id = ".$course_id." AND id = $previous";
  3468. if ($this->debug > 2) {
  3469. error_log('Selecting previous: ' . $sql_sel2, 0);
  3470. }
  3471. $res_sel2 = Database::query($sql_sel2);
  3472. if (Database::num_rows($res_sel2) < 1) {
  3473. $previous_previous = 0;
  3474. }
  3475. // Gather data.
  3476. $row2 = Database::fetch_array($res_sel2);
  3477. $previous_previous = $row2['previous_item_id'];
  3478. // Update previous_previous item (switch "next" with current).
  3479. if ($previous_previous != 0) {
  3480. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3481. next_item_id = $id
  3482. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3483. if ($this->debug > 2) {
  3484. error_log($sql_upd2, 0);
  3485. }
  3486. Database::query($sql_upd2);
  3487. }
  3488. // Update previous item (switch with current).
  3489. if ($previous != 0) {
  3490. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3491. next_item_id = $next,
  3492. previous_item_id = $id,
  3493. display_order = display_order +1
  3494. WHERE c_id = ".$course_id." AND id = $previous";
  3495. if ($this->debug > 2) {
  3496. error_log($sql_upd2, 0);
  3497. }
  3498. Database::query($sql_upd2);
  3499. }
  3500. // Update current item (switch with previous).
  3501. if ($id != 0) {
  3502. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3503. next_item_id = $previous,
  3504. previous_item_id = $previous_previous,
  3505. display_order = display_order-1
  3506. WHERE c_id = ".$course_id." AND id = $id";
  3507. if ($this->debug > 2) {
  3508. error_log($sql_upd2, 0);
  3509. }
  3510. Database::query($sql_upd2);
  3511. }
  3512. // Update next item (new previous item).
  3513. if ($next != 0) {
  3514. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3515. WHERE c_id = ".$course_id." AND id = $next";
  3516. if ($this->debug > 2) {
  3517. error_log($sql_upd2, 0);
  3518. }
  3519. Database::query($sql_upd2);
  3520. }
  3521. $display = $display -1;
  3522. }
  3523. break;
  3524. case 'down':
  3525. if ($this->debug > 2) {
  3526. error_log('Movement down detected', 0);
  3527. }
  3528. if ($next == 0) { /* Do nothing. */
  3529. } else {
  3530. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3531. if ($this->debug > 2) {
  3532. error_log('Selecting next: ' . $sql_sel2, 0);
  3533. }
  3534. $res_sel2 = Database::query($sql_sel2);
  3535. if (Database::num_rows($res_sel2) < 1) {
  3536. $next_next = 0;
  3537. }
  3538. // Gather data.
  3539. $row2 = Database::fetch_array($res_sel2);
  3540. $next_next = $row2['next_item_id'];
  3541. // Update previous item (switch with current).
  3542. if ($previous != 0) {
  3543. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3544. WHERE c_id = ".$course_id." AND id = $previous";
  3545. Database::query($sql_upd2);
  3546. }
  3547. // Update current item (switch with previous).
  3548. if ($id != 0) {
  3549. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3550. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3551. WHERE c_id = ".$course_id." AND id = $id";
  3552. Database::query($sql_upd2);
  3553. }
  3554. // Update next item (new previous item).
  3555. if ($next != 0) {
  3556. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3557. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3558. WHERE c_id = ".$course_id." AND id = $next";
  3559. Database::query($sql_upd2);
  3560. }
  3561. // Update next_next item (switch "previous" with current).
  3562. if ($next_next != 0) {
  3563. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3564. previous_item_id = $id
  3565. WHERE c_id = ".$course_id." AND id = $next_next";
  3566. Database::query($sql_upd2);
  3567. }
  3568. $display = $display +1;
  3569. }
  3570. break;
  3571. default :
  3572. return false;
  3573. }
  3574. return $display;
  3575. }
  3576. /**
  3577. * Move a learnpath up (display_order)
  3578. * @param integer $lp_id Learnpath ID
  3579. */
  3580. public static function move_up($lp_id)
  3581. {
  3582. $course_id = api_get_course_int_id();
  3583. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3584. $sql = "SELECT * FROM $lp_table
  3585. WHERE c_id = $course_id
  3586. ORDER BY display_order";
  3587. $res = Database::query($sql);
  3588. if ($res === false) {
  3589. return false;
  3590. }
  3591. $lps = array ();
  3592. $lp_order = array();
  3593. $num = Database::num_rows($res);
  3594. // First check the order is correct, globally (might be wrong because
  3595. // of versions < 1.8.4)
  3596. if ($num > 0) {
  3597. $i = 1;
  3598. while ($row = Database::fetch_array($res)) {
  3599. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3600. $need_fix = true;
  3601. $sql_u = "UPDATE $lp_table SET display_order = $i
  3602. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3603. Database::query($sql_u);
  3604. }
  3605. $row['display_order'] = $i;
  3606. $lps[$row['id']] = $row;
  3607. $lp_order[$i] = $row['id'];
  3608. $i++;
  3609. }
  3610. }
  3611. if ($num > 1) { // If there's only one element, no need to sort.
  3612. $order = $lps[$lp_id]['display_order'];
  3613. if ($order > 1) { // If it's the first element, no need to move up.
  3614. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3615. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3616. Database::query($sql_u1);
  3617. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3618. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3619. Database::query($sql_u2);
  3620. }
  3621. }
  3622. }
  3623. /**
  3624. * Move a learnpath down (display_order)
  3625. * @param integer $lp_id Learnpath ID
  3626. */
  3627. public static function move_down($lp_id)
  3628. {
  3629. $course_id = api_get_course_int_id();
  3630. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3631. $sql = "SELECT * FROM $lp_table
  3632. WHERE c_id = $course_id
  3633. ORDER BY display_order";
  3634. $res = Database::query($sql);
  3635. if ($res === false) {
  3636. return false;
  3637. }
  3638. $lps = array();
  3639. $lp_order = array();
  3640. $num = Database::num_rows($res);
  3641. $max = 0;
  3642. // First check the order is correct, globally (might be wrong because
  3643. // of versions < 1.8.4).
  3644. if ($num > 0) {
  3645. $i = 1;
  3646. while ($row = Database::fetch_array($res)) {
  3647. $max = $i;
  3648. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3649. $need_fix = true;
  3650. $sql_u = "UPDATE $lp_table SET display_order = $i
  3651. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3652. Database::query($sql_u);
  3653. }
  3654. $row['display_order'] = $i;
  3655. $lps[$row['id']] = $row;
  3656. $lp_order[$i] = $row['id'];
  3657. $i++;
  3658. }
  3659. }
  3660. if ($num > 1) { // If there's only one element, no need to sort.
  3661. $order = $lps[$lp_id]['display_order'];
  3662. if ($order < $max) { // If it's the first element, no need to move up.
  3663. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3664. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3665. Database::query($sql_u1);
  3666. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3667. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3668. Database::query($sql_u2);
  3669. }
  3670. }
  3671. }
  3672. /**
  3673. * Updates learnpath attributes to point to the next element
  3674. * The last part is similar to set_current_item but processing the other way around
  3675. */
  3676. public function next()
  3677. {
  3678. if ($this->debug > 0) {
  3679. error_log('New LP - In learnpath::next()', 0);
  3680. }
  3681. $this->last = $this->get_current_item_id();
  3682. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3683. $this->autocomplete_parents($this->last);
  3684. $new_index = $this->get_next_index();
  3685. if ($this->debug > 2) {
  3686. error_log('New LP - New index: ' . $new_index, 0);
  3687. }
  3688. $this->index = $new_index;
  3689. if ($this->debug > 2) {
  3690. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3691. }
  3692. $this->current = $this->ordered_items[$new_index];
  3693. if ($this->debug > 2) {
  3694. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3695. }
  3696. }
  3697. /**
  3698. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3699. * class, this might be redefined to allow several behaviours depending on the document type.
  3700. * @param integer Resource ID
  3701. * @return boolean True on success, false otherwise
  3702. */
  3703. public function open($id)
  3704. {
  3705. if ($this->debug > 0) {
  3706. error_log('New LP - In learnpath::open()', 0);
  3707. }
  3708. // TODO:
  3709. // set the current resource attribute to this resource
  3710. // switch on element type (redefine in child class?)
  3711. // set status for this item to "opened"
  3712. // start timer
  3713. // initialise score
  3714. $this->index = 0; //or = the last item seen (see $this->last)
  3715. }
  3716. /**
  3717. * Check that all prerequisites are fulfilled. Returns true and an
  3718. * empty string on success, returns false
  3719. * and the prerequisite string on error.
  3720. * This function is based on the rules for aicc_script language as
  3721. * described in the SCORM 1.2 CAM documentation page 108.
  3722. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3723. * @return boolean True if prerequisites are matched, false otherwise -
  3724. * Empty string if true returned, prerequisites string otherwise.
  3725. */
  3726. public function prerequisites_match($itemId = null)
  3727. {
  3728. $debug = $this->debug;
  3729. if ($debug > 0) {
  3730. error_log('In learnpath::prerequisites_match()', 0);
  3731. }
  3732. if (empty($itemId)) {
  3733. $itemId = $this->current;
  3734. }
  3735. $currentItem = $this->getItem($itemId);
  3736. if ($currentItem) {
  3737. if ($this->type == 2) {
  3738. // Getting prereq from scorm
  3739. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3740. } else {
  3741. $prereq_string = $currentItem->get_prereq_string();
  3742. }
  3743. if (empty($prereq_string)) {
  3744. if ($debug > 0) {
  3745. error_log('Found prereq_string is empty return true');
  3746. }
  3747. return true;
  3748. }
  3749. // Clean spaces.
  3750. $prereq_string = str_replace(' ', '', $prereq_string);
  3751. if ($debug > 0) {
  3752. error_log('Found prereq_string: ' . $prereq_string, 0);
  3753. }
  3754. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3755. $result = $currentItem->parse_prereq(
  3756. $prereq_string,
  3757. $this->items,
  3758. $this->refs_list,
  3759. $this->get_user_id()
  3760. );
  3761. if ($result === false) {
  3762. $this->set_error_msg($currentItem->prereq_alert);
  3763. }
  3764. } else {
  3765. $result = true;
  3766. if ($debug > 1) {
  3767. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3768. }
  3769. }
  3770. if ($debug > 1) {
  3771. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3772. }
  3773. return $result;
  3774. }
  3775. /**
  3776. * Updates learnpath attributes to point to the previous element
  3777. * The last part is similar to set_current_item but processing the other way around
  3778. */
  3779. public function previous()
  3780. {
  3781. if ($this->debug > 0) {
  3782. error_log('New LP - In learnpath::previous()', 0);
  3783. }
  3784. $this->last = $this->get_current_item_id();
  3785. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3786. $this->autocomplete_parents($this->last);
  3787. $new_index = $this->get_previous_index();
  3788. $this->index = $new_index;
  3789. $this->current = $this->ordered_items[$new_index];
  3790. }
  3791. /**
  3792. * Publishes a learnpath. This basically means show or hide the learnpath
  3793. * to normal users.
  3794. * Can be used as abstract
  3795. * @param integer Learnpath ID
  3796. * @param string New visibility
  3797. */
  3798. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3799. {
  3800. $action = 'visible';
  3801. if ($set_visibility != 1) {
  3802. $action = 'invisible';
  3803. self::toggle_publish($lp_id, 'i');
  3804. }
  3805. return api_item_property_update(
  3806. api_get_course_info(),
  3807. TOOL_LEARNPATH,
  3808. $lp_id,
  3809. $action,
  3810. api_get_user_id()
  3811. );
  3812. }
  3813. /**
  3814. * Publishes a learnpath. This basically means show or hide the learnpath
  3815. * on the course homepage
  3816. * Can be used as abstract
  3817. * @param integer $lp_id Learnpath id
  3818. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3819. * @return bool
  3820. */
  3821. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3822. {
  3823. $course_id = api_get_course_int_id();
  3824. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  3825. $lp_id = intval($lp_id);
  3826. $sql = "SELECT * FROM $tbl_lp
  3827. WHERE c_id = $course_id AND id = $lp_id";
  3828. $result = Database::query($sql);
  3829. if (Database::num_rows($result)) {
  3830. $row = Database::fetch_array($result);
  3831. $name = Database::escape_string($row['name']);
  3832. if ($set_visibility == 'i') {
  3833. $v = 0;
  3834. }
  3835. if ($set_visibility == 'v') {
  3836. $v = 1;
  3837. }
  3838. $session_id = api_get_session_id();
  3839. $session_condition = api_get_session_condition($session_id);
  3840. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  3841. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3842. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3843. $sql = "SELECT * FROM $tbl_tool
  3844. WHERE
  3845. c_id = $course_id AND
  3846. (link = '$link' OR link = '$oldLink') AND
  3847. image = 'scormbuilder.gif' AND
  3848. (
  3849. link LIKE '$link%' OR
  3850. link LIKE '$oldLink%'
  3851. )
  3852. $session_condition
  3853. ";
  3854. $result = Database::query($sql);
  3855. $num = Database::num_rows($result);
  3856. if ($set_visibility == 'i' && $num > 0) {
  3857. $sql = "DELETE FROM $tbl_tool
  3858. WHERE
  3859. c_id = $course_id AND
  3860. (link = '$link' OR link = '$oldLink') AND
  3861. image='scormbuilder.gif'
  3862. $session_condition";
  3863. Database::query($sql);
  3864. } elseif ($set_visibility == 'v' && $num == 0) {
  3865. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3866. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3867. Database::query($sql);
  3868. $insertId = Database::insert_id();
  3869. if ($insertId) {
  3870. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3871. Database::query($sql);
  3872. }
  3873. } elseif ($set_visibility == 'v' && $num > 0) {
  3874. $sql = "UPDATE $tbl_tool SET
  3875. c_id = $course_id,
  3876. name = '$name',
  3877. link = '$link',
  3878. image = 'scormbuilder.gif',
  3879. visibility = '$v',
  3880. admin = '0',
  3881. address = 'pastillegris.gif',
  3882. added_tool = 0,
  3883. session_id = $session_id
  3884. WHERE
  3885. c_id = ".$course_id." AND
  3886. (link = '$link' OR link = '$oldLink') AND
  3887. image='scormbuilder.gif'
  3888. $session_condition
  3889. ";
  3890. Database::query($sql);
  3891. } else {
  3892. // Parameter and database incompatible, do nothing, exit.
  3893. return false;
  3894. }
  3895. } else {
  3896. return false;
  3897. }
  3898. }
  3899. /**
  3900. * Restart the whole learnpath. Return the URL of the first element.
  3901. * Make sure the results are saved with anoter method. This method should probably be
  3902. * redefined in children classes.
  3903. * To use a similar method statically, use the create_new_attempt() method
  3904. * @return string URL to load in the viewer
  3905. */
  3906. public function restart()
  3907. {
  3908. if ($this->debug > 0) {
  3909. error_log('New LP - In learnpath::restart()', 0);
  3910. }
  3911. // TODO
  3912. // Call autosave method to save the current progress.
  3913. //$this->index = 0;
  3914. if (api_is_invitee()) {
  3915. return false;
  3916. }
  3917. $session_id = api_get_session_id();
  3918. $course_id = api_get_course_int_id();
  3919. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3920. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3921. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3922. if ($this->debug > 2) {
  3923. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3924. }
  3925. Database::query($sql);
  3926. $view_id = Database::insert_id();
  3927. if ($view_id) {
  3928. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3929. Database::query($sql);
  3930. $this->lp_view_id = $view_id;
  3931. $this->attempt = $this->attempt + 1;
  3932. } else {
  3933. $this->error = 'Could not insert into item_view table...';
  3934. return false;
  3935. }
  3936. $this->autocomplete_parents($this->current);
  3937. foreach ($this->items as $index => $dummy) {
  3938. $this->items[$index]->restart();
  3939. $this->items[$index]->set_lp_view($this->lp_view_id);
  3940. }
  3941. $this->first();
  3942. return true;
  3943. }
  3944. /**
  3945. * Saves the current item
  3946. * @return boolean
  3947. */
  3948. public function save_current()
  3949. {
  3950. if ($this->debug > 0) {
  3951. error_log('learnpath::save_current()', 0);
  3952. }
  3953. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3954. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3955. if ($this->debug > 2) {
  3956. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3957. }
  3958. if ($this->debug > 2) {
  3959. error_log('' . print_r($this->items, true), 0);
  3960. }
  3961. if (isset($this->items[$this->current]) &&
  3962. is_object($this->items[$this->current])
  3963. ) {
  3964. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3965. $this->autocomplete_parents($this->current);
  3966. $status = $this->items[$this->current]->get_status();
  3967. $this->update_queue[$this->current] = $status;
  3968. return $res;
  3969. }
  3970. return false;
  3971. }
  3972. /**
  3973. * Saves the given item
  3974. * @param integer $item_id Optional (will take from $_REQUEST if null)
  3975. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3976. * @return boolean
  3977. */
  3978. public function save_item($item_id = null, $from_outside = true)
  3979. {
  3980. $debug = $this->debug;
  3981. if ($debug) {
  3982. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3983. }
  3984. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3985. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3986. if (empty($item_id)) {
  3987. $item_id = intval($_REQUEST['id']);
  3988. }
  3989. if (empty($item_id)) {
  3990. $item_id = $this->get_current_item_id();
  3991. }
  3992. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3993. if ($debug) {
  3994. error_log('Object exists');
  3995. }
  3996. // Saving the item.
  3997. $res = $this->items[$item_id]->save(
  3998. $from_outside,
  3999. $this->prerequisites_match($item_id)
  4000. );
  4001. if ($debug) {
  4002. error_log('update_queue before:');
  4003. error_log(print_r($this->update_queue,1));
  4004. }
  4005. $this->autocomplete_parents($item_id);
  4006. $status = $this->items[$item_id]->get_status();
  4007. $this->update_queue[$item_id] = $status;
  4008. if ($debug) {
  4009. error_log('get_status(): ' . $status);
  4010. error_log('update_queue after:');
  4011. error_log(print_r($this->update_queue,1));
  4012. }
  4013. return $res;
  4014. }
  4015. return false;
  4016. }
  4017. /**
  4018. * Saves the last item seen's ID only in case
  4019. */
  4020. public function save_last()
  4021. {
  4022. $course_id = api_get_course_int_id();
  4023. if ($this->debug > 0) {
  4024. error_log('New LP - In learnpath::save_last()', 0);
  4025. }
  4026. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4027. $table = Database::get_course_table(TABLE_LP_VIEW);
  4028. if (isset($this->current) && !api_is_invitee()) {
  4029. if ($this->debug > 2) {
  4030. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4031. }
  4032. $sql = "UPDATE $table SET
  4033. last_item = " . intval($this->get_current_item_id()). "
  4034. WHERE
  4035. c_id = $course_id AND
  4036. lp_id = " . $this->get_id() . " AND
  4037. user_id = " . $this->get_user_id()." ".$session_condition;
  4038. if ($this->debug > 2) {
  4039. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4040. }
  4041. Database::query($sql);
  4042. }
  4043. if (!api_is_invitee()) {
  4044. // Save progress.
  4045. list($progress, $text) = $this->get_progress_bar_text('%');
  4046. if ($progress >= 0 && $progress <= 100) {
  4047. $progress = (int) $progress;
  4048. $sql = "UPDATE $table SET
  4049. progress = $progress
  4050. WHERE
  4051. c_id = ".$course_id." AND
  4052. lp_id = " . $this->get_id() . " AND
  4053. user_id = " . $this->get_user_id()." ".$session_condition;
  4054. // Ignore errors as some tables might not have the progress field just yet.
  4055. Database::query($sql);
  4056. $this->progress_db = $progress;
  4057. }
  4058. }
  4059. }
  4060. /**
  4061. * Sets the current item ID (checks if valid and authorized first)
  4062. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4063. */
  4064. public function set_current_item($item_id = null)
  4065. {
  4066. if ($this->debug > 0) {
  4067. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4068. }
  4069. if (empty ($item_id)) {
  4070. if ($this->debug > 2) {
  4071. error_log('New LP - No new current item given, ignore...', 0);
  4072. }
  4073. // Do nothing.
  4074. } else {
  4075. if ($this->debug > 2) {
  4076. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4077. }
  4078. if (is_numeric($item_id)) {
  4079. $item_id = intval($item_id);
  4080. // TODO: Check in database here.
  4081. $this->last = $this->current;
  4082. $this->current = $item_id;
  4083. // TODO: Update $this->index as well.
  4084. foreach ($this->ordered_items as $index => $item) {
  4085. if ($item == $this->current) {
  4086. $this->index = $index;
  4087. break;
  4088. }
  4089. }
  4090. if ($this->debug > 2) {
  4091. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4092. }
  4093. } else {
  4094. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4095. }
  4096. }
  4097. }
  4098. /**
  4099. * Sets the encoding
  4100. * @param string New encoding
  4101. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4102. */
  4103. public function set_encoding($enc = 'UTF-8')
  4104. {
  4105. if ($this->debug > 0) {
  4106. error_log('New LP - In learnpath::set_encoding()', 0);
  4107. }
  4108. $course_id = api_get_course_int_id();
  4109. $enc = api_refine_encoding_id($enc);
  4110. if (empty($enc)) {
  4111. $enc = api_get_system_encoding();
  4112. }
  4113. if (api_is_encoding_supported($enc)) {
  4114. $lp = $this->get_id();
  4115. if ($lp != 0) {
  4116. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4117. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4118. WHERE c_id = ".$course_id." AND id = " . $lp;
  4119. $res = Database::query($sql);
  4120. return $res;
  4121. }
  4122. }
  4123. return false;
  4124. }
  4125. /**
  4126. * Sets the JS lib setting in the database directly.
  4127. * This is the JavaScript library file this lp needs to load on startup
  4128. * @param string Proximity setting
  4129. * @return boolean True on update success. False otherwise.
  4130. */
  4131. public function set_jslib($lib = '')
  4132. {
  4133. if ($this->debug > 0) {
  4134. error_log('New LP - In learnpath::set_jslib()', 0);
  4135. }
  4136. $lp = $this->get_id();
  4137. $course_id = api_get_course_int_id();
  4138. if ($lp != 0) {
  4139. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4140. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4141. WHERE c_id = ".$course_id." AND id = " . $lp;
  4142. $res = Database::query($sql);
  4143. return $res;
  4144. } else {
  4145. return false;
  4146. }
  4147. }
  4148. /**
  4149. * Sets the name of the LP maker (publisher) (and save)
  4150. * @param string Optional string giving the new content_maker of this learnpath
  4151. * @return boolean True
  4152. */
  4153. public function set_maker($name = '')
  4154. {
  4155. if ($this->debug > 0) {
  4156. error_log('New LP - In learnpath::set_maker()', 0);
  4157. }
  4158. if (empty ($name))
  4159. return false;
  4160. $this->maker = $name;
  4161. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4162. $course_id = api_get_course_int_id();
  4163. $lp_id = $this->get_id();
  4164. $sql = "UPDATE $lp_table SET
  4165. content_maker = '" . Database::escape_string($this->maker) . "'
  4166. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4167. if ($this->debug > 2) {
  4168. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4169. }
  4170. Database::query($sql);
  4171. return true;
  4172. }
  4173. /**
  4174. * Sets the name of the current learnpath (and save)
  4175. * @param string $name Optional string giving the new name of this learnpath
  4176. * @return boolean True/False
  4177. */
  4178. public function set_name($name = null)
  4179. {
  4180. if ($this->debug > 0) {
  4181. error_log('New LP - In learnpath::set_name()', 0);
  4182. }
  4183. if (empty($name)) {
  4184. return false;
  4185. }
  4186. $this->name = Database::escape_string($name);
  4187. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4188. $lp_id = $this->get_id();
  4189. $course_id = $this->course_info['real_id'];
  4190. $sql = "UPDATE $lp_table SET
  4191. name = '" . Database::escape_string($this->name). "'
  4192. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4193. if ($this->debug > 2) {
  4194. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4195. }
  4196. $result = Database::query($sql);
  4197. // If the lp is visible on the homepage, change his name there.
  4198. if (Database::affected_rows($result)) {
  4199. $session_id = api_get_session_id();
  4200. $session_condition = api_get_session_condition($session_id);
  4201. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  4202. $link = 'lp/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4203. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4204. WHERE
  4205. c_id = $course_id AND
  4206. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4207. Database::query($sql);
  4208. return true;
  4209. } else {
  4210. return false;
  4211. }
  4212. }
  4213. /**
  4214. * Set index specified prefix terms for all items in this path
  4215. * @param string Comma-separated list of terms
  4216. * @param char Xapian term prefix
  4217. * @return boolean False on error, true otherwise
  4218. */
  4219. public function set_terms_by_prefix($terms_string, $prefix)
  4220. {
  4221. $course_id = api_get_course_int_id();
  4222. if (api_get_setting('search_enabled') !== 'true')
  4223. return false;
  4224. if (!extension_loaded('xapian')) {
  4225. return false;
  4226. }
  4227. $terms_string = trim($terms_string);
  4228. $terms = explode(',', $terms_string);
  4229. array_walk($terms, 'trim_value');
  4230. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4231. // Don't do anything if no change, verify only at DB, not the search engine.
  4232. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4233. return false;
  4234. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4235. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4236. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4237. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4238. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  4239. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4240. $lp_id = intval($_POST['lp_id']);
  4241. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4242. $result = Database::query($sql);
  4243. $di = new ChamiloIndexer();
  4244. while ($lp_item = Database::fetch_array($result)) {
  4245. // Get search_did.
  4246. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4247. $sql = 'SELECT * FROM %s
  4248. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4249. LIMIT 1';
  4250. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4251. //echo $sql; echo '<br>';
  4252. $res = Database::query($sql);
  4253. if (Database::num_rows($res) > 0) {
  4254. $se_ref = Database::fetch_array($res);
  4255. // Compare terms.
  4256. $doc = $di->get_document($se_ref['search_did']);
  4257. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4258. $xterms = array();
  4259. foreach ($xapian_terms as $xapian_term) {
  4260. $xterms[] = substr($xapian_term['name'], 1);
  4261. }
  4262. $dterms = $terms;
  4263. $missing_terms = array_diff($dterms, $xterms);
  4264. $deprecated_terms = array_diff($xterms, $dterms);
  4265. // Save it to search engine.
  4266. foreach ($missing_terms as $term) {
  4267. $doc->add_term($prefix . $term, 1);
  4268. }
  4269. foreach ($deprecated_terms as $term) {
  4270. $doc->remove_term($prefix . $term);
  4271. }
  4272. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4273. $di->getDb()->flush();
  4274. }
  4275. }
  4276. return true;
  4277. }
  4278. /**
  4279. * Sets the theme of the LP (local/remote) (and save)
  4280. * @param string Optional string giving the new theme of this learnpath
  4281. * @return bool Returns true if theme name is not empty
  4282. */
  4283. public function set_theme($name = '')
  4284. {
  4285. $course_id = api_get_course_int_id();
  4286. if ($this->debug > 0) {
  4287. error_log('New LP - In learnpath::set_theme()', 0);
  4288. }
  4289. $this->theme = $name;
  4290. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4291. $lp_id = $this->get_id();
  4292. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4293. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4294. if ($this->debug > 2) {
  4295. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4296. }
  4297. Database::query($sql);
  4298. return true;
  4299. }
  4300. /**
  4301. * Sets the image of an LP (and save)
  4302. * @param string Optional string giving the new image of this learnpath
  4303. * @return bool Returns true if theme name is not empty
  4304. */
  4305. public function set_preview_image($name = '')
  4306. {
  4307. $course_id = api_get_course_int_id();
  4308. if ($this->debug > 0) {
  4309. error_log('New LP - In learnpath::set_preview_image()', 0);
  4310. }
  4311. $this->preview_image = $name;
  4312. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4313. $lp_id = $this->get_id();
  4314. $sql = "UPDATE $lp_table SET
  4315. preview_image = '" . Database::escape_string($this->preview_image). "'
  4316. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4317. if ($this->debug > 2) {
  4318. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4319. }
  4320. Database::query($sql);
  4321. return true;
  4322. }
  4323. /**
  4324. * Sets the author of a LP (and save)
  4325. * @param string Optional string giving the new author of this learnpath
  4326. * @return bool Returns true if author's name is not empty
  4327. */
  4328. public function set_author($name = '')
  4329. {
  4330. $course_id = api_get_course_int_id();
  4331. if ($this->debug > 0) {
  4332. error_log('New LP - In learnpath::set_author()', 0);
  4333. }
  4334. $this->author = $name;
  4335. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4336. $lp_id = $this->get_id();
  4337. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4338. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4339. if ($this->debug > 2) {
  4340. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4341. }
  4342. Database::query($sql);
  4343. return true;
  4344. }
  4345. /**
  4346. * Sets the hide_toc_frame parameter of a LP (and save)
  4347. * @param int 1 if frame is hidden 0 then else
  4348. * @return bool Returns true if author's name is not empty
  4349. */
  4350. public function set_hide_toc_frame($hide)
  4351. {
  4352. $course_id = api_get_course_int_id();
  4353. if ($this->debug > 0) {
  4354. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4355. }
  4356. if (intval($hide) == $hide) {
  4357. $this->hide_toc_frame = $hide;
  4358. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4359. $lp_id = $this->get_id();
  4360. $sql = "UPDATE $lp_table SET
  4361. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4362. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4363. if ($this->debug > 2) {
  4364. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4365. }
  4366. Database::query($sql);
  4367. return true;
  4368. } else {
  4369. return false;
  4370. }
  4371. }
  4372. /**
  4373. * Sets the prerequisite of a LP (and save)
  4374. * @param int integer giving the new prerequisite of this learnpath
  4375. * @return bool returns true if prerequisite is not empty
  4376. */
  4377. public function set_prerequisite($prerequisite)
  4378. {
  4379. $course_id = api_get_course_int_id();
  4380. if ($this->debug > 0) {
  4381. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4382. }
  4383. $this->prerequisite = intval($prerequisite);
  4384. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4385. $lp_id = $this->get_id();
  4386. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4387. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4388. if ($this->debug > 2) {
  4389. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4390. }
  4391. Database::query($sql);
  4392. return true;
  4393. }
  4394. /**
  4395. * Sets the location/proximity of the LP (local/remote) (and save)
  4396. * @param string Optional string giving the new location of this learnpath
  4397. * @return boolean True on success / False on error
  4398. */
  4399. public function set_proximity($name = '')
  4400. {
  4401. $course_id = api_get_course_int_id();
  4402. if ($this->debug > 0) {
  4403. error_log('New LP - In learnpath::set_proximity()', 0);
  4404. }
  4405. if (empty ($name))
  4406. return false;
  4407. $this->proximity = $name;
  4408. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4409. $lp_id = $this->get_id();
  4410. $sql = "UPDATE $lp_table SET
  4411. content_local = '" . Database::escape_string($name) . "'
  4412. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4413. if ($this->debug > 2) {
  4414. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4415. }
  4416. Database::query($sql);
  4417. return true;
  4418. }
  4419. /**
  4420. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4421. * @param integer DB ID of the item
  4422. */
  4423. public function set_previous_item($id)
  4424. {
  4425. if ($this->debug > 0) {
  4426. error_log('New LP - In learnpath::set_previous_item()', 0);
  4427. }
  4428. $this->last = $id;
  4429. }
  4430. /**
  4431. * Sets use_max_score
  4432. * @param string $use_max_score Optional string giving the new location of this learnpath
  4433. * @return boolean True on success / False on error
  4434. */
  4435. public function set_use_max_score($use_max_score = 1)
  4436. {
  4437. $course_id = api_get_course_int_id();
  4438. if ($this->debug > 0) {
  4439. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4440. }
  4441. $use_max_score = intval($use_max_score);
  4442. $this->use_max_score = $use_max_score;
  4443. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4444. $lp_id = $this->get_id();
  4445. $sql = "UPDATE $lp_table SET
  4446. use_max_score = '" . $this->use_max_score . "'
  4447. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4448. if ($this->debug > 2) {
  4449. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4450. }
  4451. Database::query($sql);
  4452. return true;
  4453. }
  4454. /**
  4455. * Sets and saves the expired_on date
  4456. * @param string $expired_on Optional string giving the new author of this learnpath
  4457. * @return bool Returns true if author's name is not empty
  4458. */
  4459. public function set_expired_on($expired_on)
  4460. {
  4461. if ($this->debug > 0) {
  4462. error_log('New LP - In learnpath::set_expired_on()', 0);
  4463. }
  4464. $em = Database::getManager();
  4465. /** @var CLp $lp */
  4466. $lp = $em
  4467. ->getRepository('ChamiloCourseBundle:CLp')
  4468. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4469. if (!$lp) {
  4470. return false;
  4471. }
  4472. $this->expired_on = !empty($expired_on) ? api_get_utc_datetime($expired_on, false, true) : null;
  4473. $lp->setExpiredOn($this->expired_on);
  4474. $em->persist($lp);
  4475. $em->flush();
  4476. if ($this->debug > 2) {
  4477. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4478. }
  4479. return true;
  4480. }
  4481. /**
  4482. * Sets and saves the publicated_on date
  4483. * @param string $publicated_on Optional string giving the new author of this learnpath
  4484. * @return bool Returns true if author's name is not empty
  4485. */
  4486. public function set_publicated_on($publicated_on)
  4487. {
  4488. if ($this->debug > 0) {
  4489. error_log('New LP - In learnpath::set_expired_on()', 0);
  4490. }
  4491. $em = Database::getManager();
  4492. /** @var CLp $lp */
  4493. $lp = $em
  4494. ->getRepository('ChamiloCourseBundle:CLp')
  4495. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4496. if (!$lp) {
  4497. return false;
  4498. }
  4499. $this->publicated_on = !empty($publicated_on) ? api_get_utc_datetime($publicated_on, false, true) : null;
  4500. $lp->setPublicatedOn($this->publicated_on);
  4501. $em->persist($lp);
  4502. $em->flush();
  4503. if ($this->debug > 2) {
  4504. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4505. }
  4506. return true;
  4507. }
  4508. /**
  4509. * Sets and saves the expired_on date
  4510. * @return bool Returns true if author's name is not empty
  4511. */
  4512. public function set_modified_on()
  4513. {
  4514. $course_id = api_get_course_int_id();
  4515. if ($this->debug > 0) {
  4516. error_log('New LP - In learnpath::set_expired_on()', 0);
  4517. }
  4518. $this->modified_on = api_get_utc_datetime();
  4519. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4520. $lp_id = $this->get_id();
  4521. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4522. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4523. if ($this->debug > 2) {
  4524. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4525. }
  4526. Database::query($sql);
  4527. return true;
  4528. }
  4529. /**
  4530. * Sets the object's error message
  4531. * @param string Error message. If empty, reinits the error string
  4532. * @return void
  4533. */
  4534. public function set_error_msg($error = '')
  4535. {
  4536. if ($this->debug > 0) {
  4537. error_log('New LP - In learnpath::set_error_msg()', 0);
  4538. }
  4539. if (empty ($error)) {
  4540. $this->error = '';
  4541. } else {
  4542. $this->error .= $error;
  4543. }
  4544. }
  4545. /**
  4546. * Launches the current item if not 'sco'
  4547. * (starts timer and make sure there is a record ready in the DB)
  4548. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4549. * @return boolean
  4550. */
  4551. public function start_current_item($allow_new_attempt = false)
  4552. {
  4553. if ($this->debug > 0) {
  4554. error_log('New LP - In learnpath::start_current_item()', 0);
  4555. }
  4556. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4557. $type = $this->get_type();
  4558. $item_type = $this->items[$this->current]->get_type();
  4559. if (($type == 2 && $item_type != 'sco') ||
  4560. ($type == 3 && $item_type != 'au') ||
  4561. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4562. ) {
  4563. $this->items[$this->current]->open($allow_new_attempt);
  4564. $this->autocomplete_parents($this->current);
  4565. $prereq_check = $this->prerequisites_match($this->current);
  4566. $this->items[$this->current]->save(false, $prereq_check);
  4567. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4568. } else {
  4569. // If sco, then it is supposed to have been updated by some other call.
  4570. }
  4571. if ($item_type == 'sco') {
  4572. $this->items[$this->current]->restart();
  4573. }
  4574. }
  4575. if ($this->debug > 0) {
  4576. error_log('New LP - End of learnpath::start_current_item()', 0);
  4577. }
  4578. return true;
  4579. }
  4580. /**
  4581. * Stops the processing and counters for the old item (as held in $this->last)
  4582. * @return boolean True/False
  4583. */
  4584. public function stop_previous_item()
  4585. {
  4586. if ($this->debug > 0) {
  4587. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4588. }
  4589. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4590. if ($this->debug > 2) {
  4591. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4592. }
  4593. switch ($this->get_type()) {
  4594. case '3':
  4595. if ($this->items[$this->last]->get_type() != 'au') {
  4596. if ($this->debug > 2) {
  4597. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4598. }
  4599. $this->items[$this->last]->close();
  4600. //$this->autocomplete_parents($this->last);
  4601. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4602. } else {
  4603. if ($this->debug > 2) {
  4604. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4605. }
  4606. }
  4607. case '2':
  4608. if ($this->items[$this->last]->get_type() != 'sco') {
  4609. if ($this->debug > 2) {
  4610. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4611. }
  4612. $this->items[$this->last]->close();
  4613. //$this->autocomplete_parents($this->last);
  4614. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4615. } else {
  4616. if ($this->debug > 2) {
  4617. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4618. }
  4619. }
  4620. break;
  4621. case '1':
  4622. default:
  4623. if ($this->debug > 2) {
  4624. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4625. }
  4626. $this->items[$this->last]->close();
  4627. break;
  4628. }
  4629. } else {
  4630. if ($this->debug > 2) {
  4631. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4632. }
  4633. return false;
  4634. }
  4635. return true;
  4636. }
  4637. /**
  4638. * Updates the default view mode from fullscreen to embedded and inversely
  4639. * @return string The current default view mode ('fullscreen' or 'embedded')
  4640. */
  4641. public function update_default_view_mode()
  4642. {
  4643. $course_id = api_get_course_int_id();
  4644. if ($this->debug > 0) {
  4645. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4646. }
  4647. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4648. $sql = "SELECT * FROM $lp_table
  4649. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4650. $res = Database::query($sql);
  4651. if (Database::num_rows($res) > 0) {
  4652. $row = Database::fetch_array($res);
  4653. $default_view_mode = $row['default_view_mod'];
  4654. $view_mode = $default_view_mode;
  4655. switch ($default_view_mode) {
  4656. case 'fullscreen': // default with popup
  4657. $view_mode = 'embedded';
  4658. break;
  4659. case 'embedded': // default view with left menu
  4660. $view_mode = 'embedframe';
  4661. break;
  4662. case 'embedframe': //folded menu
  4663. $view_mode = 'impress';
  4664. break;
  4665. case 'impress':
  4666. $view_mode = 'fullscreen';
  4667. break;
  4668. }
  4669. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4670. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4671. Database::query($sql);
  4672. $this->mode = $view_mode;
  4673. return $view_mode;
  4674. } else {
  4675. if ($this->debug > 2) {
  4676. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4677. }
  4678. }
  4679. return -1;
  4680. }
  4681. /**
  4682. * Updates the default behaviour about auto-commiting SCORM updates
  4683. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4684. */
  4685. public function update_default_scorm_commit()
  4686. {
  4687. $course_id = api_get_course_int_id();
  4688. if ($this->debug > 0) {
  4689. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4690. }
  4691. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4692. $sql = "SELECT * FROM $lp_table
  4693. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4694. $res = Database::query($sql);
  4695. if (Database::num_rows($res) > 0) {
  4696. $row = Database::fetch_array($res);
  4697. $force = $row['force_commit'];
  4698. if ($force == 1) {
  4699. $force = 0;
  4700. $force_return = false;
  4701. } elseif ($force == 0) {
  4702. $force = 1;
  4703. $force_return = true;
  4704. }
  4705. $sql = "UPDATE $lp_table SET force_commit = $force
  4706. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4707. Database::query($sql);
  4708. $this->force_commit = $force_return;
  4709. return $force_return;
  4710. } else {
  4711. if ($this->debug > 2) {
  4712. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4713. }
  4714. }
  4715. return -1;
  4716. }
  4717. /**
  4718. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4719. * @return bool True on success, false on failure
  4720. */
  4721. public function update_display_order()
  4722. {
  4723. $course_id = api_get_course_int_id();
  4724. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4725. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4726. $res = Database::query($sql);
  4727. if ($res === false) {
  4728. return false;
  4729. }
  4730. $num = Database::num_rows($res);
  4731. // First check the order is correct, globally (might be wrong because
  4732. // of versions < 1.8.4).
  4733. if ($num > 0) {
  4734. $i = 1;
  4735. while ($row = Database::fetch_array($res)) {
  4736. if ($row['display_order'] != $i) {
  4737. // If we find a gap in the order, we need to fix it.
  4738. $sql = "UPDATE $lp_table SET display_order = $i
  4739. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4740. Database::query($sql);
  4741. }
  4742. $i++;
  4743. }
  4744. }
  4745. return true;
  4746. }
  4747. /**
  4748. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4749. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4750. */
  4751. public function update_reinit()
  4752. {
  4753. $course_id = api_get_course_int_id();
  4754. if ($this->debug > 0) {
  4755. error_log('New LP - In learnpath::update_reinit()', 0);
  4756. }
  4757. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4758. $sql = "SELECT * FROM $lp_table
  4759. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4760. $res = Database::query($sql);
  4761. if (Database::num_rows($res) > 0) {
  4762. $row = Database::fetch_array($res);
  4763. $force = $row['prevent_reinit'];
  4764. if ($force == 1) {
  4765. $force = 0;
  4766. } elseif ($force == 0) {
  4767. $force = 1;
  4768. }
  4769. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4770. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4771. Database::query($sql);
  4772. $this->prevent_reinit = $force;
  4773. return $force;
  4774. } else {
  4775. if ($this->debug > 2) {
  4776. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4777. }
  4778. }
  4779. return -1;
  4780. }
  4781. /**
  4782. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4783. *
  4784. * @return string 'single', 'multi' or 'seriousgame'
  4785. * @author ndiechburg <noel@cblue.be>
  4786. **/
  4787. public function get_attempt_mode()
  4788. {
  4789. //Set default value for seriousgame_mode
  4790. if (!isset($this->seriousgame_mode)) {
  4791. $this->seriousgame_mode=0;
  4792. }
  4793. // Set default value for prevent_reinit
  4794. if (!isset($this->prevent_reinit)) {
  4795. $this->prevent_reinit =1;
  4796. }
  4797. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4798. return 'seriousgame';
  4799. }
  4800. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4801. return 'single';
  4802. }
  4803. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4804. return 'multiple';
  4805. }
  4806. return 'single';
  4807. }
  4808. /**
  4809. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4810. *
  4811. * @param string 'seriousgame', 'single' or 'multiple'
  4812. * @return boolean
  4813. * @author ndiechburg <noel@cblue.be>
  4814. **/
  4815. public function set_attempt_mode($mode)
  4816. {
  4817. $course_id = api_get_course_int_id();
  4818. switch ($mode) {
  4819. case 'seriousgame':
  4820. $sg_mode = 1;
  4821. $prevent_reinit = 1;
  4822. break;
  4823. case 'single':
  4824. $sg_mode = 0;
  4825. $prevent_reinit = 1;
  4826. break;
  4827. case 'multiple':
  4828. $sg_mode = 0;
  4829. $prevent_reinit = 0;
  4830. break;
  4831. default:
  4832. $sg_mode = 0;
  4833. $prevent_reinit = 0;
  4834. break;
  4835. }
  4836. $this->prevent_reinit = $prevent_reinit;
  4837. $this->seriousgame_mode = $sg_mode;
  4838. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4839. $sql = "UPDATE $lp_table SET
  4840. prevent_reinit = $prevent_reinit ,
  4841. seriousgame_mode = $sg_mode
  4842. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4843. $res = Database::query($sql);
  4844. if ($res) {
  4845. return true;
  4846. } else {
  4847. return false;
  4848. }
  4849. }
  4850. /**
  4851. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4852. *
  4853. * @return boolean
  4854. * @author ndiechburg <noel@cblue.be>
  4855. **/
  4856. public function switch_attempt_mode()
  4857. {
  4858. if ($this->debug > 0) {
  4859. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4860. }
  4861. $mode = $this->get_attempt_mode();
  4862. switch ($mode) {
  4863. case 'single':
  4864. $next_mode = 'multiple';
  4865. break;
  4866. case 'multiple':
  4867. $next_mode = 'seriousgame';
  4868. break;
  4869. case 'seriousgame':
  4870. $next_mode = 'single';
  4871. break;
  4872. default:
  4873. $next_mode = 'single';
  4874. break;
  4875. }
  4876. $this->set_attempt_mode($next_mode);
  4877. }
  4878. /**
  4879. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4880. * but possibility to do again a completed item.
  4881. *
  4882. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4883. * @author ndiechburg <noel@cblue.be>
  4884. **/
  4885. public function set_seriousgame_mode()
  4886. {
  4887. $course_id = api_get_course_int_id();
  4888. if ($this->debug > 0) {
  4889. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4890. }
  4891. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4892. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4893. $res = Database::query($sql);
  4894. if (Database::num_rows($res) > 0) {
  4895. $row = Database::fetch_array($res);
  4896. $force = $row['seriousgame_mode'];
  4897. if ($force == 1) {
  4898. $force = 0;
  4899. } elseif ($force == 0) {
  4900. $force = 1;
  4901. }
  4902. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4903. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4904. Database::query($sql);
  4905. $this->seriousgame_mode = $force;
  4906. return $force;
  4907. } else {
  4908. if ($this->debug > 2) {
  4909. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4910. }
  4911. }
  4912. return -1;
  4913. }
  4914. /**
  4915. * Updates the "scorm_debug" value that shows or hide the debug window
  4916. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4917. */
  4918. public function update_scorm_debug()
  4919. {
  4920. $course_id = api_get_course_int_id();
  4921. if ($this->debug > 0) {
  4922. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4923. }
  4924. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4925. $sql = "SELECT * FROM $lp_table
  4926. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4927. $res = Database::query($sql);
  4928. if (Database::num_rows($res) > 0) {
  4929. $row = Database::fetch_array($res);
  4930. $force = $row['debug'];
  4931. if ($force == 1) {
  4932. $force = 0;
  4933. } elseif ($force == 0) {
  4934. $force = 1;
  4935. }
  4936. $sql = "UPDATE $lp_table SET debug = $force
  4937. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4938. Database::query($sql);
  4939. $this->scorm_debug = $force;
  4940. return $force;
  4941. } else {
  4942. if ($this->debug > 2) {
  4943. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4944. }
  4945. }
  4946. return -1;
  4947. }
  4948. /**
  4949. * Function that makes a call to the function sort_tree_array and create_tree_array
  4950. * @author Kevin Van Den Haute
  4951. * @param array
  4952. */
  4953. public function tree_array($array)
  4954. {
  4955. if ($this->debug > 1) {
  4956. error_log('New LP - In learnpath::tree_array()', 0);
  4957. }
  4958. $array = $this->sort_tree_array($array);
  4959. $this->create_tree_array($array);
  4960. }
  4961. /**
  4962. * Creates an array with the elements of the learning path tree in it
  4963. *
  4964. * @author Kevin Van Den Haute
  4965. * @param array $array
  4966. * @param int $parent
  4967. * @param int $depth
  4968. * @param array $tmp
  4969. */
  4970. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4971. {
  4972. if ($this->debug > 1) {
  4973. error_log('New LP - In learnpath::create_tree_array())', 0);
  4974. }
  4975. if (is_array($array)) {
  4976. for ($i = 0; $i < count($array); $i++) {
  4977. if ($array[$i]['parent_item_id'] == $parent) {
  4978. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4979. $tmp[] = $array[$i]['parent_item_id'];
  4980. $depth++;
  4981. }
  4982. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4983. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4984. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4985. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4986. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4987. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4988. $this->arrMenu[] = array(
  4989. 'id' => $array[$i]['id'],
  4990. 'ref' => $ref,
  4991. 'item_type' => $array[$i]['item_type'],
  4992. 'title' => $array[$i]['title'],
  4993. 'path' => $path,
  4994. 'description' => $array[$i]['description'],
  4995. 'parent_item_id' => $array[$i]['parent_item_id'],
  4996. 'previous_item_id' => $array[$i]['previous_item_id'],
  4997. 'next_item_id' => $array[$i]['next_item_id'],
  4998. 'min_score' => $array[$i]['min_score'],
  4999. 'max_score' => $array[$i]['max_score'],
  5000. 'mastery_score' => $array[$i]['mastery_score'],
  5001. 'display_order' => $array[$i]['display_order'],
  5002. 'prerequisite' => $preq,
  5003. 'depth' => $depth,
  5004. 'audio' => $audio,
  5005. 'prerequisite_min_score' => $prerequisiteMinScore,
  5006. 'prerequisite_max_score' => $prerequisiteMaxScore
  5007. );
  5008. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5009. }
  5010. }
  5011. }
  5012. }
  5013. /**
  5014. * Sorts a multi dimensional array by parent id and display order
  5015. * @author Kevin Van Den Haute
  5016. *
  5017. * @param array $array (array with al the learning path items in it)
  5018. *
  5019. * @return array
  5020. */
  5021. public function sort_tree_array($array)
  5022. {
  5023. foreach ($array as $key => $row) {
  5024. $parent[$key] = $row['parent_item_id'];
  5025. $position[$key] = $row['display_order'];
  5026. }
  5027. if (count($array) > 0) {
  5028. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5029. }
  5030. return $array;
  5031. }
  5032. /**
  5033. * Function that creates a html list of learning path items so that we can add audio files to them
  5034. * @author Kevin Van Den Haute
  5035. * @return string
  5036. */
  5037. public function overview()
  5038. {
  5039. if ($this->debug > 0) {
  5040. error_log('New LP - In learnpath::overview()', 0);
  5041. }
  5042. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5043. $return = '';
  5044. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5045. // we need to start a form when we want to update all the mp3 files
  5046. if ($update_audio == 'true') {
  5047. $return .= '<form action="' . api_get_self() . '?'.api_get_cidreq().'&updateaudio=' . Security::remove_XSS($_GET['updateaudio']) .'&action=' . Security::remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5048. }
  5049. $return .= '<div id="message"></div>';
  5050. if (count($this->items) == 0) {
  5051. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5052. } else {
  5053. $return_audio = '<table class="data_table">';
  5054. $return_audio .= '<tr>';
  5055. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5056. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5057. $return_audio .= '</tr>';
  5058. if ($update_audio != 'true') {
  5059. $return .= '<div class="col-md-12">';
  5060. $return .= self::return_new_tree($update_audio);
  5061. $return .='</div>';
  5062. $return .= Display::div(
  5063. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5064. array('style' => 'float:left; margin-top:15px;width:100%')
  5065. );
  5066. } else {
  5067. $return_audio .= self::return_new_tree($update_audio);
  5068. $return .= $return_audio.'</table>';
  5069. }
  5070. // We need to close the form when we are updating the mp3 files.
  5071. if ($update_audio == 'true') {
  5072. $return .= '<div class="footer-audio">';
  5073. $return .= Display::button(
  5074. 'save_audio',
  5075. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5076. array('class' => 'btn btn-primary', 'type' => 'submit')
  5077. );
  5078. $return .= '</div>';
  5079. }
  5080. }
  5081. // We need to close the form when we are updating the mp3 files.
  5082. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5083. $return .= '</form>';
  5084. }
  5085. return $return;
  5086. }
  5087. /**
  5088. * @param string string $update_audio
  5089. * @param bool $drop_element_here
  5090. * @return string
  5091. */
  5092. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5093. {
  5094. $return = '';
  5095. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5096. $course_id = api_get_course_int_id();
  5097. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5098. $sql = "SELECT * FROM $tbl_lp_item
  5099. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5100. $result = Database::query($sql);
  5101. $arrLP = array();
  5102. while ($row = Database::fetch_array($result)) {
  5103. $arrLP[] = array(
  5104. 'id' => $row['id'],
  5105. 'item_type' => $row['item_type'],
  5106. 'title' => Security::remove_XSS($row['title']),
  5107. 'path' => $row['path'],
  5108. 'description' => Security::remove_XSS($row['description']),
  5109. 'parent_item_id' => $row['parent_item_id'],
  5110. 'previous_item_id' => $row['previous_item_id'],
  5111. 'next_item_id' => $row['next_item_id'],
  5112. 'max_score' => $row['max_score'],
  5113. 'min_score' => $row['min_score'],
  5114. 'mastery_score' => $row['mastery_score'],
  5115. 'prerequisite' => $row['prerequisite'],
  5116. 'display_order' => $row['display_order'],
  5117. 'audio' => $row['audio'],
  5118. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5119. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5120. );
  5121. }
  5122. $this->tree_array($arrLP);
  5123. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5124. unset ($this->arrMenu);
  5125. $default_data = null;
  5126. $default_content = null;
  5127. $elements = array();
  5128. $return_audio = null;
  5129. for ($i = 0; $i < count($arrLP); $i++) {
  5130. $title = $arrLP[$i]['title'];
  5131. $title_cut = cut($arrLP[$i]['title'], 25);
  5132. // Link for the documents
  5133. if ($arrLP[$i]['item_type'] == 'document') {
  5134. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5135. $title_cut = Display::url(
  5136. $title_cut,
  5137. $url,
  5138. array(
  5139. 'class' => 'ajax moved',
  5140. 'data-title' => $title_cut
  5141. )
  5142. );
  5143. }
  5144. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5145. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5146. Session::write('pathItem', $arrLP[$i]['path']);
  5147. }
  5148. if (($i % 2) == 0) {
  5149. $oddClass = 'row_odd';
  5150. } else {
  5151. $oddClass = 'row_even';
  5152. }
  5153. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5154. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5155. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5156. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5157. } else {
  5158. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5159. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5160. } else {
  5161. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5162. $icon = Display::return_icon('certificate.png');
  5163. } else {
  5164. $icon = Display::return_icon('folder_document.gif');
  5165. }
  5166. }
  5167. }
  5168. // The audio column.
  5169. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5170. $audio = '';
  5171. if (!$update_audio || $update_audio <> 'true') {
  5172. if (!empty($arrLP[$i]['audio'])) {
  5173. } else {
  5174. $audio .= '';
  5175. }
  5176. } else {
  5177. $types = self::getChapterTypes();
  5178. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5179. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5180. if (!empty ($arrLP[$i]['audio'])) {
  5181. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5182. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5183. }
  5184. }
  5185. }
  5186. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5187. $return_audio .= '</td>';
  5188. $move_icon = '';
  5189. $move_item_icon = '';
  5190. $edit_icon = '';
  5191. $delete_icon = '';
  5192. $audio_icon = '';
  5193. $prerequisities_icon = '';
  5194. $forumIcon = '';
  5195. $previewIcon = '';
  5196. if ($is_allowed_to_edit) {
  5197. if (!$update_audio || $update_audio <> 'true') {
  5198. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5199. $move_icon .= '<a class="moved" href="#">';
  5200. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5201. $move_icon .= '</a>';
  5202. }
  5203. }
  5204. // No edit for this item types
  5205. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5206. if ($arrLP[$i]['item_type'] != 'dir') {
  5207. $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">';
  5208. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5209. $edit_icon .= '</a>';
  5210. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5211. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5212. $this->course_int_id,
  5213. $this->lp_session_id
  5214. );
  5215. if ($forumThread) {
  5216. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5217. 'action' => 'dissociate_forum',
  5218. 'id' => $arrLP[$i]['id'],
  5219. 'lp_id' => $this->lp_id
  5220. ]);
  5221. $forumIcon = Display::url(
  5222. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5223. $forumIconUrl,
  5224. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5225. );
  5226. } else {
  5227. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5228. 'action' => 'create_forum',
  5229. 'id' => $arrLP[$i]['id'],
  5230. 'lp_id' => $this->lp_id
  5231. ]);
  5232. $forumIcon = Display::url(
  5233. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5234. $forumIconUrl,
  5235. ['class' => "btn btn-default lp-btn-associate-forum"]
  5236. );
  5237. }
  5238. }
  5239. } else {
  5240. $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">';
  5241. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5242. $edit_icon .= '</a>';
  5243. }
  5244. } else {
  5245. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5246. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id.'" class="btn btn-default">';
  5247. $edit_icon .= Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_TINY);
  5248. $edit_icon .= '</a>';
  5249. }
  5250. }
  5251. $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">';
  5252. $delete_icon .= Display::return_icon(
  5253. 'delete.png',
  5254. get_lang('LearnpathDeleteModule'),
  5255. [],
  5256. ICON_SIZE_TINY
  5257. );
  5258. $delete_icon .= '</a>';
  5259. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5260. $previewImage = Display::return_icon(
  5261. 'preview_view.png',
  5262. get_lang('Preview'),
  5263. [],
  5264. ICON_SIZE_TINY
  5265. );
  5266. switch ($arrLP[$i]['item_type']) {
  5267. case TOOL_DOCUMENT:
  5268. case TOOL_LP_FINAL_ITEM:
  5269. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5270. $previewIcon = Display::url(
  5271. $previewImage,
  5272. $urlPreviewLink,
  5273. array(
  5274. 'class' => 'btn btn-default ajax',
  5275. 'data-title' => $arrLP[$i]['title']
  5276. )
  5277. );
  5278. break;
  5279. case TOOL_THREAD:
  5280. case TOOL_FORUM:
  5281. case TOOL_LP_FINAL_ITEM:
  5282. case TOOL_LINK:
  5283. $target = '';
  5284. $class = 'btn btn-default ajax';
  5285. if ($arrLP[$i]['item_type'] == TOOL_LINK) {
  5286. $class = 'btn btn-default';
  5287. $target = '_blank';
  5288. }
  5289. $link = self::rl_get_resource_link_for_learnpath(
  5290. $this->course_int_id,
  5291. $this->lp_id,
  5292. $arrLP[$i]['id'],
  5293. 0
  5294. );
  5295. $previewIcon = Display::url(
  5296. $previewImage,
  5297. $link,
  5298. [
  5299. 'class' => $class,
  5300. 'data-title' => $arrLP[$i]['title'],
  5301. 'target' => $target
  5302. ]
  5303. );
  5304. break;
  5305. default:
  5306. $previewIcon = Display::url(
  5307. $previewImage,
  5308. $url.'&action=view_item', ['class' => 'btn btn-default']
  5309. );
  5310. break;
  5311. }
  5312. if ($arrLP[$i]['item_type'] != 'dir') {
  5313. $prerequisities_icon = Display::url(
  5314. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5315. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5316. );
  5317. if ($arrLP[$i]['item_type'] != 'final_item') {
  5318. $move_item_icon = Display::url(
  5319. Display::return_icon(
  5320. 'move.png',
  5321. get_lang('Move'),
  5322. array(),
  5323. ICON_SIZE_TINY
  5324. ),
  5325. $url.'&action=move_item',
  5326. ['class' => 'btn btn-default']
  5327. );
  5328. }
  5329. $audio_icon = Display::url(
  5330. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5331. $url.'&action=add_audio',
  5332. ['class' => 'btn btn-default']
  5333. );
  5334. }
  5335. }
  5336. if ($update_audio != 'true') {
  5337. $row = $move_icon . ' ' . $icon .
  5338. Display::span($title_cut) .
  5339. Display::tag(
  5340. 'div',
  5341. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5342. array('class'=>'btn-toolbar button_actions')
  5343. );
  5344. } else {
  5345. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5346. }
  5347. $parent_id = $arrLP[$i]['parent_item_id'];
  5348. $default_data[$arrLP[$i]['id']] = $row;
  5349. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5350. if (empty($parent_id)) {
  5351. $elements[$arrLP[$i]['id']]['data'] = $row;
  5352. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5353. } else {
  5354. $parent_arrays = array();
  5355. if ($arrLP[$i]['depth'] > 1) {
  5356. //Getting list of parents
  5357. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5358. foreach($arrLP as $item) {
  5359. if ($item['id'] == $parent_id) {
  5360. if ($item['parent_item_id'] == 0) {
  5361. $parent_id = $item['id'];
  5362. break;
  5363. } else {
  5364. $parent_id = $item['parent_item_id'];
  5365. if (empty($parent_arrays)) {
  5366. $parent_arrays[] = intval($item['id']);
  5367. }
  5368. $parent_arrays[] = $parent_id;
  5369. break;
  5370. }
  5371. }
  5372. }
  5373. }
  5374. }
  5375. if (!empty($parent_arrays)) {
  5376. $parent_arrays = array_reverse($parent_arrays);
  5377. $val = '$elements';
  5378. $x = 0;
  5379. foreach($parent_arrays as $item) {
  5380. if ($x != count($parent_arrays) -1) {
  5381. $val .= '["'.$item.'"]["children"]';
  5382. } else {
  5383. $val .= '["'.$item.'"]["children"]';
  5384. }
  5385. $x++;
  5386. }
  5387. $val .= "";
  5388. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5389. eval($code_str);
  5390. } else {
  5391. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5392. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5393. }
  5394. }
  5395. }
  5396. $list = '<ul id="lp_item_list">';
  5397. $tree = self::print_recursive($elements, $default_data, $default_content);
  5398. if (!empty($tree)) {
  5399. $list .= $tree;
  5400. } else {
  5401. if ($drop_element_here) {
  5402. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5403. }
  5404. }
  5405. $list .= '</ul>';
  5406. $return .= Display::panelCollapse(
  5407. $this->name,
  5408. $list,
  5409. 'scorm-list',
  5410. null,
  5411. 'scorm-list-accordion',
  5412. 'scorm-list-collapse'
  5413. );
  5414. if ($update_audio == 'true') {
  5415. $return = $return_audio;
  5416. }
  5417. return $return;
  5418. }
  5419. /**
  5420. * @param array $elements
  5421. * @param array $default_data
  5422. * @param array $default_content
  5423. * @return string
  5424. */
  5425. public function print_recursive($elements, $default_data, $default_content)
  5426. {
  5427. $return = '';
  5428. foreach ($elements as $key => $item) {
  5429. if (isset($item['load_data']) || empty($item['data'])) {
  5430. $item['data'] = $default_data[$item['load_data']];
  5431. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5432. }
  5433. $sub_list = '';
  5434. if (isset($item['type']) && $item['type'] == 'dir') {
  5435. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5436. }
  5437. if (empty($item['children'])) {
  5438. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5439. $active = null;
  5440. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5441. $active = 'active';
  5442. }
  5443. $return .= Display::tag(
  5444. 'li',
  5445. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5446. array('id'=>$key, 'class'=>'record li_container')
  5447. );
  5448. } else {
  5449. // Sections
  5450. if (isset($item['children'])) {
  5451. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5452. }
  5453. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5454. $return .= Display::tag(
  5455. 'li',
  5456. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5457. array('id'=>$key, 'class'=>'record li_container')
  5458. );
  5459. }
  5460. }
  5461. return $return;
  5462. }
  5463. /**
  5464. * This function builds the action menu
  5465. * @param bool $returnContent Optional
  5466. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5467. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5468. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5469. * @return string
  5470. */
  5471. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5472. {
  5473. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5474. $actionsLeft = '';
  5475. $actionsRight = '';
  5476. $actionsLeft .= Display::url(
  5477. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5478. 'lp_controller.php?'.api_get_cidreq().'&' . http_build_query([
  5479. 'gradebook' => $gradebook,
  5480. 'action' => 'view',
  5481. 'lp_id' => $_SESSION['oLP']->lp_id,
  5482. 'isStudentView' => 'true'
  5483. ])
  5484. );
  5485. $actionsLeft .= Display::url(
  5486. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5487. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5488. 'action' => 'admin_view',
  5489. 'lp_id' => $_SESSION['oLP']->lp_id,
  5490. 'updateaudio' => 'true'
  5491. ])
  5492. );
  5493. if (!$isConfigPage) {
  5494. $actionsLeft .= Display::url(
  5495. Display::return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM),
  5496. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5497. 'action' => 'edit',
  5498. 'lp_id' => $_SESSION['oLP']->lp_id
  5499. ])
  5500. );
  5501. } else {
  5502. $actionsLeft .= Display::url(
  5503. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5504. 'lp_controller.php?' . http_build_query([
  5505. 'action' => 'build',
  5506. 'lp_id' => $_SESSION['oLP']->lp_id
  5507. ]) . '&' . api_get_cidreq()
  5508. );
  5509. }
  5510. if ($allowExpand) {
  5511. $actionsLeft .= Display::url(
  5512. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM) .
  5513. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5514. '#',
  5515. ['role' => 'button', 'id' => 'hide_bar_template']
  5516. );
  5517. }
  5518. if ($showRequirementButtons) {
  5519. $buttons = array(
  5520. array(
  5521. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5522. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5523. 'action' => 'set_previous_step_as_prerequisite',
  5524. 'lp_id' => $_SESSION['oLP']->lp_id
  5525. ])
  5526. ),
  5527. array(
  5528. 'title' => get_lang('ClearAllPrerequisites'),
  5529. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5530. 'action' => 'clear_prerequisites',
  5531. 'lp_id' => $_SESSION['oLP']->lp_id
  5532. ])
  5533. ),
  5534. );
  5535. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons, true);
  5536. }
  5537. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5538. if ($returnContent) {
  5539. return $toolbar;
  5540. }
  5541. echo $toolbar;
  5542. }
  5543. /**
  5544. * Creates the default learning path folder
  5545. * @param array $course
  5546. * @param int $creatorId
  5547. *
  5548. * @return bool
  5549. */
  5550. public static function generate_learning_path_folder($course, $creatorId = 0)
  5551. {
  5552. // Creating learning_path folder
  5553. $dir = '/learning_path';
  5554. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5555. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5556. $folder = false;
  5557. if (!is_dir($filepath.'/'.$dir)) {
  5558. $folderData = create_unexisting_directory(
  5559. $course,
  5560. $creatorId,
  5561. 0,
  5562. 0,
  5563. 0,
  5564. $filepath,
  5565. $dir,
  5566. get_lang('LearningPaths'),
  5567. 0
  5568. );
  5569. if (!empty($folderData)) {
  5570. $folder = true;
  5571. }
  5572. } else {
  5573. $folder = true;
  5574. }
  5575. return $folder;
  5576. }
  5577. /**
  5578. * @param array $course
  5579. * @param string $lp_name
  5580. * @param int $creatorId
  5581. *
  5582. * @return array
  5583. */
  5584. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5585. {
  5586. $filepath = '';
  5587. $dir = '/learning_path/';
  5588. if (empty($lp_name)) {
  5589. $lp_name = $this->name;
  5590. }
  5591. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5592. $folder = self::generate_learning_path_folder($course, $creatorId);
  5593. // Limits title size
  5594. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5595. $dir = $dir.$title;
  5596. // Creating LP folder
  5597. $documentId = null;
  5598. if ($folder) {
  5599. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5600. if (!is_dir($filepath.'/'.$dir)) {
  5601. $folderData = create_unexisting_directory(
  5602. $course,
  5603. $creatorId,
  5604. 0,
  5605. 0,
  5606. 0,
  5607. $filepath,
  5608. $dir,
  5609. $lp_name
  5610. );
  5611. if (!empty($folderData)) {
  5612. $folder = true;
  5613. }
  5614. $documentId = $folderData['id'];
  5615. } else {
  5616. $folder = true;
  5617. }
  5618. $dir = $dir.'/';
  5619. if ($folder) {
  5620. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5621. }
  5622. }
  5623. if (empty($documentId)) {
  5624. $dir = api_remove_trailing_slash($dir);
  5625. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5626. }
  5627. $array = array(
  5628. 'dir' => $dir,
  5629. 'filepath' => $filepath,
  5630. 'folder' => $folder,
  5631. 'id' => $documentId
  5632. );
  5633. return $array;
  5634. }
  5635. /**
  5636. * Create a new document //still needs some finetuning
  5637. * @param array $courseInfo
  5638. * @param string $content
  5639. * @param string $title
  5640. * @param string $extension
  5641. * @param int $parentId
  5642. * @param int $creatorId creator id
  5643. *
  5644. * @return string
  5645. */
  5646. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5647. {
  5648. if (!empty($courseInfo)) {
  5649. $course_id = $courseInfo['real_id'];
  5650. } else {
  5651. $course_id = api_get_course_int_id();
  5652. }
  5653. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5654. $sessionId = api_get_session_id();
  5655. // Generates folder
  5656. $result = $this->generate_lp_folder($courseInfo);
  5657. $dir = $result['dir'];
  5658. if (empty($parentId)) {
  5659. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  5660. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5661. // Please, do not modify this dirname formatting.
  5662. if (strstr($dir, '..')) {
  5663. $dir = '/';
  5664. }
  5665. if (!empty($dir[0]) && $dir[0] == '.') {
  5666. $dir = substr($dir, 1);
  5667. }
  5668. if (!empty($dir[0]) && $dir[0] != '/') {
  5669. $dir = '/'.$dir;
  5670. }
  5671. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5672. $dir .= '/';
  5673. }
  5674. } else {
  5675. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5676. if (!empty($parentInfo)) {
  5677. $dir = $parentInfo['path'].'/';
  5678. }
  5679. }
  5680. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5681. if (!is_dir($filepath)) {
  5682. $dir = '/';
  5683. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5684. }
  5685. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5686. // is already escaped twice when it gets here.
  5687. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5688. if (!empty($title)) {
  5689. $title = api_replace_dangerous_char(stripslashes($title));
  5690. } else {
  5691. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5692. }
  5693. $title = disable_dangerous_file($title);
  5694. $filename = $title;
  5695. $content = !empty($content) ? $content : $_POST['content_lp'];
  5696. $tmp_filename = $filename;
  5697. $i = 0;
  5698. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5699. $tmp_filename = $filename . '_' . ++ $i;
  5700. }
  5701. $filename = $tmp_filename . '.'.$extension;
  5702. if ($extension == 'html') {
  5703. $content = stripslashes($content);
  5704. $content = str_replace(
  5705. api_get_path(WEB_COURSE_PATH),
  5706. api_get_path(REL_PATH).'courses/',
  5707. $content
  5708. );
  5709. // Change the path of mp3 to absolute.
  5710. // The first regexp deals with :// urls.
  5711. $content = preg_replace(
  5712. "|(flashvars=\"file=)([^:/]+)/|",
  5713. "$1".api_get_path(
  5714. REL_COURSE_PATH
  5715. ).$courseInfo['path'].'/document/',
  5716. $content
  5717. );
  5718. // The second regexp deals with audio/ urls.
  5719. $content = preg_replace(
  5720. "|(flashvars=\"file=)([^/]+)/|",
  5721. "$1".api_get_path(
  5722. REL_COURSE_PATH
  5723. ).$courseInfo['path'].'/document/$2/',
  5724. $content
  5725. );
  5726. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5727. $content = str_replace(
  5728. '</body>',
  5729. '<style type="text/css">body{}</style></body>',
  5730. $content
  5731. );
  5732. }
  5733. if (!file_exists($filepath . $filename)) {
  5734. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5735. fputs($fp, $content);
  5736. fclose($fp);
  5737. $file_size = filesize($filepath . $filename);
  5738. $save_file_path = $dir.$filename;
  5739. $document_id = add_document(
  5740. $courseInfo,
  5741. $save_file_path,
  5742. 'file',
  5743. $file_size,
  5744. $tmp_filename,
  5745. '',
  5746. 0, //readonly
  5747. true,
  5748. null,
  5749. $sessionId,
  5750. $creatorId
  5751. );
  5752. if ($document_id) {
  5753. api_item_property_update(
  5754. $courseInfo,
  5755. TOOL_DOCUMENT,
  5756. $document_id,
  5757. 'DocumentAdded',
  5758. $creatorId,
  5759. null,
  5760. null,
  5761. null,
  5762. null,
  5763. $sessionId
  5764. );
  5765. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5766. $new_title = $originalTitle;
  5767. if ($new_comment || $new_title) {
  5768. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5769. $ct = '';
  5770. if ($new_comment)
  5771. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5772. if ($new_title)
  5773. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5774. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5775. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5776. Database::query($sql);
  5777. }
  5778. }
  5779. return $document_id;
  5780. }
  5781. }
  5782. }
  5783. /**
  5784. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5785. * @param array $_course array
  5786. * @return void
  5787. */
  5788. public function edit_document($_course)
  5789. {
  5790. $course_id = api_get_course_int_id();
  5791. $urlAppend = api_get_configuration_value('url_append');
  5792. // Please, do not modify this dirname formatting.
  5793. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5794. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  5795. if (strstr($dir, '..')) {
  5796. $dir = '/';
  5797. }
  5798. if (isset($dir[0]) && $dir[0] == '.') {
  5799. $dir = substr($dir, 1);
  5800. }
  5801. if (isset($dir[0]) && $dir[0] != '/') {
  5802. $dir = '/'.$dir;
  5803. }
  5804. if (isset($dir[strlen($dir) - 1] ) && $dir[strlen($dir) - 1] != '/') {
  5805. $dir .= '/';
  5806. }
  5807. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5808. if (!is_dir($filepath)) {
  5809. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5810. }
  5811. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  5812. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5813. $document_id = intval($_POST['path']);
  5814. $sql = "SELECT path FROM " . $table_doc . "
  5815. WHERE c_id = $course_id AND id = " . $document_id;
  5816. $res = Database::query($sql);
  5817. $row = Database::fetch_array($res);
  5818. $content = stripslashes($_POST['content_lp']);
  5819. $file = $filepath . $row['path'];
  5820. if ($fp = @ fopen($file, 'w')) {
  5821. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  5822. // Change the path of mp3 to absolute.
  5823. // The first regexp deals with :// urls.
  5824. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5825. // The second regexp deals with audio/ urls.
  5826. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5827. fputs($fp, $content);
  5828. fclose($fp);
  5829. $sql = "UPDATE " . $table_doc ." SET
  5830. title='".Database::escape_string($_POST['title'])."'
  5831. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5832. Database::query($sql);
  5833. }
  5834. }
  5835. }
  5836. /**
  5837. * Displays the selected item, with a panel for manipulating the item
  5838. * @param int $item_id
  5839. * @param string $msg
  5840. * @return string
  5841. */
  5842. public function display_item($item_id, $msg = null, $show_actions = true)
  5843. {
  5844. $course_id = api_get_course_int_id();
  5845. $return = '';
  5846. if (is_numeric($item_id)) {
  5847. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5848. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  5849. WHERE
  5850. c_id = $course_id AND
  5851. lp.id = " . intval($item_id);
  5852. $result = Database::query($sql);
  5853. while ($row = Database::fetch_array($result,'ASSOC')) {
  5854. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  5855. // Prevents wrong parent selection for document, see Bug#1251.
  5856. if ($row['item_type'] != 'dir') {
  5857. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5858. }
  5859. if ($show_actions) {
  5860. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5861. }
  5862. $return .= '<div style="padding:10px;">';
  5863. if ($msg != '') {
  5864. $return .= $msg;
  5865. }
  5866. $return .= '<h3>'.$row['title'].'</h3>';
  5867. switch ($row['item_type']) {
  5868. case TOOL_THREAD:
  5869. $link = $this->rl_get_resource_link_for_learnpath(
  5870. $course_id,
  5871. $row['lp_id'],
  5872. $item_id,
  5873. 0
  5874. );
  5875. $return .= Display::url(
  5876. get_lang('GoToThread'),
  5877. $link,
  5878. ['class' => 'btn btn-primary']
  5879. );
  5880. break;
  5881. case TOOL_FORUM:
  5882. $return .= Display::url(
  5883. get_lang('GoToForum'),
  5884. api_get_path(WEB_CODE_PATH) . 'forum/viewforum.php?' . api_get_cidreq() . '&forum=' . $row['path'],
  5885. ['class' => 'btn btn-primary']
  5886. );
  5887. break;
  5888. case TOOL_QUIZ:
  5889. if (!empty($row['path'])) {
  5890. $exercise = new Exercise();
  5891. $exercise->read($row['path']);
  5892. $return .= $exercise->description.'<br />';
  5893. $return .= Display::url(
  5894. get_lang('GoToExercise'),
  5895. api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . api_get_cidreq() . '&exerciseId=' . $exercise->id,
  5896. ['class' => 'btn btn-primary']
  5897. );
  5898. }
  5899. break;
  5900. case TOOL_LP_FINAL_ITEM:
  5901. $return .= $this->getSavedFinalItem();
  5902. break;
  5903. case TOOL_DOCUMENT:
  5904. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5905. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5906. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5907. $result = Database::query($sql_doc);
  5908. $path_file = Database::result($result, 0, 0);
  5909. $path_parts = pathinfo($path_file);
  5910. // TODO: Correct the following naive comparisons.
  5911. if (in_array($path_parts['extension'], array(
  5912. 'html',
  5913. 'txt',
  5914. 'png',
  5915. 'jpg',
  5916. 'JPG',
  5917. 'jpeg',
  5918. 'JPEG',
  5919. 'gif',
  5920. 'swf',
  5921. 'pdf',
  5922. 'htm'
  5923. ))) {
  5924. $return .= $this->display_document($row['path'], true, true);
  5925. }
  5926. break;
  5927. case TOOL_HOTPOTATOES:
  5928. $return .= $this->display_document($row['path'], false, true);
  5929. break;
  5930. }
  5931. $return .= '</div>';
  5932. }
  5933. }
  5934. return $return;
  5935. }
  5936. /**
  5937. * Shows the needed forms for editing a specific item
  5938. * @param int $item_id
  5939. * @return string
  5940. */
  5941. public function display_edit_item($item_id)
  5942. {
  5943. $course_id = api_get_course_int_id();
  5944. $return = '';
  5945. if (is_numeric($item_id)) {
  5946. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5947. $sql = "SELECT * FROM $tbl_lp_item
  5948. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5949. $res = Database::query($sql);
  5950. $row = Database::fetch_array($res);
  5951. switch ($row['item_type']) {
  5952. case 'dir':
  5953. case 'asset':
  5954. case 'sco':
  5955. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5956. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5957. $return .= $this->display_item_form(
  5958. $row['item_type'],
  5959. get_lang('EditCurrentChapter').' :',
  5960. 'edit',
  5961. $item_id,
  5962. $row
  5963. );
  5964. } else {
  5965. $return .= $this->display_item_small_form(
  5966. $row['item_type'],
  5967. get_lang('EditCurrentChapter').' :',
  5968. $row
  5969. );
  5970. }
  5971. break;
  5972. case TOOL_DOCUMENT:
  5973. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5974. $sql = "SELECT lp.*, doc.path as dir
  5975. FROM $tbl_lp_item as lp
  5976. LEFT JOIN $tbl_doc as doc
  5977. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  5978. WHERE
  5979. lp.c_id = $course_id AND
  5980. doc.c_id = $course_id AND
  5981. lp.id = " . intval($item_id);
  5982. $res_step = Database::query($sql);
  5983. $row_step = Database::fetch_array($res_step, 'ASSOC');
  5984. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5985. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5986. break;
  5987. case TOOL_LINK:
  5988. $link_id = (string) $row['path'];
  5989. if (ctype_digit($link_id)) {
  5990. $tbl_link = Database::get_course_table(TABLE_LINK);
  5991. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5992. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5993. $res_link = Database::query($sql_select);
  5994. $row_link = Database::fetch_array($res_link);
  5995. if (is_array($row_link)) {
  5996. $row['url'] = $row_link['url'];
  5997. }
  5998. }
  5999. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6000. $return .= $this->display_link_form('edit', $item_id, $row);
  6001. break;
  6002. case TOOL_LP_FINAL_ITEM:
  6003. Session::write('finalItem', true);
  6004. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6005. $sql = "SELECT lp.*, doc.path as dir
  6006. FROM $tbl_lp_item as lp
  6007. LEFT JOIN $tbl_doc as doc
  6008. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6009. WHERE
  6010. lp.c_id = $course_id AND
  6011. doc.c_id = $course_id AND
  6012. lp.id = " . intval($item_id);
  6013. $res_step = Database::query($sql);
  6014. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6015. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6016. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6017. break;
  6018. case TOOL_QUIZ:
  6019. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6020. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6021. break;
  6022. case TOOL_HOTPOTATOES:
  6023. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6024. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6025. break;
  6026. case TOOL_STUDENTPUBLICATION:
  6027. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6028. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6029. break;
  6030. case TOOL_FORUM:
  6031. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6032. $return .= $this->display_forum_form('edit', $item_id, $row);
  6033. break;
  6034. case TOOL_THREAD:
  6035. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6036. $return .= $this->display_thread_form('edit', $item_id, $row);
  6037. break;
  6038. }
  6039. }
  6040. return $return;
  6041. }
  6042. /**
  6043. * Function that displays a list with al the resources that
  6044. * could be added to the learning path
  6045. * @return string
  6046. */
  6047. public function display_resources()
  6048. {
  6049. $course_code = api_get_course_id();
  6050. // Get all the docs.
  6051. $documents = $this->get_documents(true);
  6052. // Get all the exercises.
  6053. $exercises = $this->get_exercises();
  6054. // Get all the links.
  6055. $links = $this->get_links();
  6056. // Get all the student publications.
  6057. $works = $this->get_student_publications();
  6058. // Get all the forums.
  6059. $forums = $this->get_forums(null, $course_code);
  6060. // Get the final item form (see BT#11048) .
  6061. $finish = $this->getFinalItemForm();
  6062. $headers = array(
  6063. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  6064. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  6065. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  6066. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  6067. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  6068. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  6069. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6070. );
  6071. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6072. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6073. echo Display::tabs(
  6074. $headers,
  6075. array(
  6076. $documents,
  6077. $exercises,
  6078. $links,
  6079. $works,
  6080. $forums,
  6081. $dir,
  6082. $finish,
  6083. ),
  6084. 'resource_tab'
  6085. );
  6086. return true;
  6087. }
  6088. /**
  6089. * Returns the extension of a document
  6090. * @param string $filename
  6091. * @return string Extension (part after the last dot)
  6092. */
  6093. public function get_extension($filename)
  6094. {
  6095. $explode = explode('.', $filename);
  6096. return $explode[count($explode) - 1];
  6097. }
  6098. /**
  6099. * Displays a document by id
  6100. *
  6101. * @param int $id
  6102. * @return string
  6103. */
  6104. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6105. {
  6106. $_course = api_get_course_info();
  6107. $course_id = api_get_course_int_id();
  6108. $return = '';
  6109. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6110. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  6111. WHERE c_id = ".$course_id." AND id = " . $id;
  6112. $res_doc = Database::query($sql_doc);
  6113. $row_doc = Database::fetch_array($res_doc);
  6114. // TODO: Add a path filter.
  6115. if ($iframe) {
  6116. $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>';
  6117. } else {
  6118. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/' . $row_doc['path']);
  6119. }
  6120. return $return;
  6121. }
  6122. /**
  6123. * Return HTML form to add/edit a quiz
  6124. * @param string $action Action (add/edit)
  6125. * @param integer $id Item ID if already exists
  6126. * @param mixed $extra_info Extra information (quiz ID if integer)
  6127. * @return string HTML form
  6128. */
  6129. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6130. {
  6131. $course_id = api_get_course_int_id();
  6132. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6133. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6134. if ($id != 0 && is_array($extra_info)) {
  6135. $item_title = $extra_info['title'];
  6136. $item_description = $extra_info['description'];
  6137. } elseif (is_numeric($extra_info)) {
  6138. $sql = "SELECT title, description
  6139. FROM $tbl_quiz
  6140. WHERE c_id = $course_id AND id = " . $extra_info;
  6141. $result = Database::query($sql);
  6142. $row = Database::fetch_array($result);
  6143. $item_title = $row['title'];
  6144. $item_description = $row['description'];
  6145. } else {
  6146. $item_title = '';
  6147. $item_description = '';
  6148. }
  6149. $item_title = Security::remove_XSS($item_title);
  6150. $item_description = Security::remove_XSS($item_description);
  6151. if ($id != 0 && is_array($extra_info)) {
  6152. $parent = $extra_info['parent_item_id'];
  6153. } else {
  6154. $parent = 0;
  6155. }
  6156. $sql = "SELECT * FROM $tbl_lp_item
  6157. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  6158. $result = Database::query($sql);
  6159. $arrLP = array ();
  6160. while ($row = Database::fetch_array($result)) {
  6161. $arrLP[] = array(
  6162. 'id' => $row['id'],
  6163. 'item_type' => $row['item_type'],
  6164. 'title' => $row['title'],
  6165. 'path' => $row['path'],
  6166. 'description' => $row['description'],
  6167. 'parent_item_id' => $row['parent_item_id'],
  6168. 'previous_item_id' => $row['previous_item_id'],
  6169. 'next_item_id' => $row['next_item_id'],
  6170. 'display_order' => $row['display_order'],
  6171. 'max_score' => $row['max_score'],
  6172. 'min_score' => $row['min_score'],
  6173. 'mastery_score' => $row['mastery_score'],
  6174. 'prerequisite' => $row['prerequisite'],
  6175. 'max_time_allowed' => $row['max_time_allowed']
  6176. );
  6177. }
  6178. $this->tree_array($arrLP);
  6179. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6180. unset ($this->arrMenu);
  6181. $form = new FormValidator(
  6182. 'quiz_form',
  6183. 'POST',
  6184. $this->getCurrentBuildingModeURL()
  6185. );
  6186. $defaults = [];
  6187. if ($action == 'add') {
  6188. $legend = get_lang('CreateTheExercise');
  6189. } elseif ($action == 'move') {
  6190. $legend = get_lang('MoveTheCurrentExercise');
  6191. } else {
  6192. $legend = get_lang('EditCurrentExecice');
  6193. }
  6194. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6195. $legend .= Display::return_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6196. }
  6197. $form->addHeader($legend);
  6198. if ($action != 'move') {
  6199. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6200. $defaults['title'] = $item_title;
  6201. }
  6202. // Select for Parent item, root or chapter
  6203. $selectParent = $form->addSelect(
  6204. 'parent',
  6205. get_lang('Parent'),
  6206. [],
  6207. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6208. );
  6209. $selectParent->addOption($this->name, 0);
  6210. $arrHide = array (
  6211. $id
  6212. );
  6213. for ($i = 0; $i < count($arrLP); $i++) {
  6214. if ($action != 'add') {
  6215. if (
  6216. ($arrLP[$i]['item_type'] == 'dir') &&
  6217. !in_array($arrLP[$i]['id'], $arrHide) &&
  6218. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6219. ) {
  6220. $selectParent->addOption(
  6221. $arrLP[$i]['title'],
  6222. $arrLP[$i]['id'],
  6223. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6224. );
  6225. if ($parent == $arrLP[$i]['id']) {
  6226. $selectParent->setSelected($arrLP[$i]['id']);
  6227. }
  6228. } else {
  6229. $arrHide[] = $arrLP[$i]['id'];
  6230. }
  6231. } else {
  6232. if ($arrLP[$i]['item_type'] == 'dir') {
  6233. $selectParent->addOption(
  6234. $arrLP[$i]['title'],
  6235. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6236. );
  6237. if ($parent == $arrLP[$i]['id']) {
  6238. $selectParent->setSelected($arrLP[$i]['id']);
  6239. }
  6240. }
  6241. }
  6242. }
  6243. if (is_array($arrLP)) {
  6244. reset($arrLP);
  6245. }
  6246. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6247. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6248. for ($i = 0; $i < count($arrLP); $i++) {
  6249. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6250. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6251. if (is_array($extra_info)) {
  6252. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6253. $selectPrevious->setSelected($arrLP[$i]['id']);
  6254. }
  6255. } elseif ($action == 'add') {
  6256. $selectPrevious->setSelected($arrLP[$i]['id']);
  6257. }
  6258. }
  6259. }
  6260. if ($action != 'move') {
  6261. $id_prerequisite = 0;
  6262. if (is_array($arrLP)) {
  6263. foreach ($arrLP as $key => $value) {
  6264. if ($value['id'] == $id) {
  6265. $id_prerequisite = $value['prerequisite'];
  6266. break;
  6267. }
  6268. }
  6269. }
  6270. $arrHide = array ();
  6271. for ($i = 0; $i < count($arrLP); $i++) {
  6272. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6273. if (is_array($extra_info)) {
  6274. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6275. $s_selected_position = $arrLP[$i]['id'];
  6276. }
  6277. } elseif ($action == 'add') {
  6278. $s_selected_position = 0;
  6279. }
  6280. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6281. }
  6282. }
  6283. /*// Commented the prerequisites, only visible in edit (exercise).
  6284. $return .= '<tr>';
  6285. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6286. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6287. foreach($arrHide as $key => $value){
  6288. if($key==$s_selected_position && $action == 'add'){
  6289. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6290. }
  6291. elseif($key==$id_prerequisite && $action == 'edit'){
  6292. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6293. }
  6294. else{
  6295. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6296. }
  6297. }
  6298. $return .= "</select></td>";
  6299. */
  6300. /*$return .= '<tr>';
  6301. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6302. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6303. // Remove temporarily the test description.
  6304. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6305. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6306. $return .= '</tr>'; */
  6307. }
  6308. if ($action == 'add') {
  6309. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6310. } else {
  6311. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6312. }
  6313. if ($action == 'move') {
  6314. $form->addHidden('title', $item_title);
  6315. $form->addHidden('description', $item_description);
  6316. }
  6317. if (is_numeric($extra_info)) {
  6318. $form->addHidden('path', $extra_info);
  6319. } elseif (is_array($extra_info)) {
  6320. $form->addHidden('path', $extra_info['path']);
  6321. }
  6322. $form->addHidden('type', TOOL_QUIZ);
  6323. $form->addHidden('post_time', time());
  6324. $form->setDefaults($defaults);
  6325. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6326. }
  6327. /**
  6328. * Addition of Hotpotatoes tests
  6329. * @param string Action
  6330. * @param integer Internal ID of the item
  6331. * @param mixed Extra information - can be an array with title and description indexes
  6332. * @return string HTML structure to display the hotpotatoes addition formular
  6333. */
  6334. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6335. {
  6336. $course_id = api_get_course_int_id();
  6337. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6338. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6339. if ($id != 0 && is_array($extra_info)) {
  6340. $item_title = stripslashes($extra_info['title']);
  6341. $item_description = stripslashes($extra_info['description']);
  6342. } elseif (is_numeric($extra_info)) {
  6343. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6344. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6345. WHERE
  6346. c_id = ".$course_id." AND
  6347. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6348. id = " . (int) $extra_info . "
  6349. ORDER BY id ASC";
  6350. $res_hot = Database::query($sql);
  6351. $row = Database::fetch_array($res_hot);
  6352. $item_title = $row['title'];
  6353. $item_description = $row['description'];
  6354. if (!empty ($row['comment'])) {
  6355. $item_title = $row['comment'];
  6356. }
  6357. } else {
  6358. $item_title = '';
  6359. $item_description = '';
  6360. }
  6361. if ($id != 0 && is_array($extra_info)) {
  6362. $parent = $extra_info['parent_item_id'];
  6363. } else {
  6364. $parent = 0;
  6365. }
  6366. $sql = "SELECT * FROM $tbl_lp_item
  6367. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6368. $result = Database::query($sql);
  6369. $arrLP = array ();
  6370. while ($row = Database::fetch_array($result)) {
  6371. $arrLP[] = array(
  6372. 'id' => $row['id'],
  6373. 'item_type' => $row['item_type'],
  6374. 'title' => $row['title'],
  6375. 'path' => $row['path'],
  6376. 'description' => $row['description'],
  6377. 'parent_item_id' => $row['parent_item_id'],
  6378. 'previous_item_id' => $row['previous_item_id'],
  6379. 'next_item_id' => $row['next_item_id'],
  6380. 'display_order' => $row['display_order'],
  6381. 'max_score' => $row['max_score'],
  6382. 'min_score' => $row['min_score'],
  6383. 'mastery_score' => $row['mastery_score'],
  6384. 'prerequisite' => $row['prerequisite'],
  6385. 'max_time_allowed' => $row['max_time_allowed']
  6386. );
  6387. }
  6388. $legend = '<legend>';
  6389. if ($action == 'add') {
  6390. $legend .= get_lang('CreateTheExercise');
  6391. } elseif ($action == 'move') {
  6392. $legend .= get_lang('MoveTheCurrentExercise');
  6393. } else {
  6394. $legend .= get_lang('EditCurrentExecice');
  6395. }
  6396. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6397. $legend .= Display:: return_message(
  6398. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6399. );
  6400. }
  6401. $legend .= '</legend>';
  6402. $return = '<form method="POST">';
  6403. $return .= $legend;
  6404. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6405. $return .= '<tr>';
  6406. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6407. $return .= '<td class="input">';
  6408. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6409. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6410. $arrHide = array(
  6411. $id
  6412. );
  6413. if (count($arrLP) > 0) {
  6414. for ($i = 0; $i < count($arrLP); $i++) {
  6415. if ($action != 'add') {
  6416. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6417. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6418. } else {
  6419. $arrHide[] = $arrLP[$i]['id'];
  6420. }
  6421. } else {
  6422. if ($arrLP[$i]['item_type'] == 'dir')
  6423. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6424. }
  6425. }
  6426. reset($arrLP);
  6427. }
  6428. $return .= '</select>';
  6429. $return .= '</td>';
  6430. $return .= '</tr>';
  6431. $return .= '<tr>';
  6432. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6433. $return .= '<td class="input">';
  6434. $return .= '<select id="previous" name="previous" size="1">';
  6435. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6436. for ($i = 0; $i < count($arrLP); $i++) {
  6437. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6438. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6439. $selected = 'selected="selected" ';
  6440. elseif ($action == 'add') $selected = 'selected="selected" ';
  6441. else
  6442. $selected = '';
  6443. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6444. }
  6445. }
  6446. $return .= '</select>';
  6447. $return .= '</td>';
  6448. $return .= '</tr>';
  6449. if ($action != 'move') {
  6450. $return .= '<tr>';
  6451. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6452. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6453. $return .= '</tr>';
  6454. $id_prerequisite = 0;
  6455. if (is_array($arrLP) && count($arrLP) > 0) {
  6456. foreach ($arrLP as $key => $value) {
  6457. if ($value['id'] == $id) {
  6458. $id_prerequisite = $value['prerequisite'];
  6459. break;
  6460. }
  6461. }
  6462. $arrHide = array ();
  6463. for ($i = 0; $i < count($arrLP); $i++) {
  6464. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6465. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6466. $s_selected_position = $arrLP[$i]['id'];
  6467. elseif ($action == 'add') $s_selected_position = 0;
  6468. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6469. }
  6470. }
  6471. }
  6472. }
  6473. $return .= '<tr>';
  6474. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6475. $return .= '</tr>';
  6476. $return .= '</table>';
  6477. if ($action == 'move') {
  6478. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6479. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6480. }
  6481. if (is_numeric($extra_info)) {
  6482. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6483. } elseif (is_array($extra_info)) {
  6484. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6485. }
  6486. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6487. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6488. $return .= '</form>';
  6489. return $return;
  6490. }
  6491. /**
  6492. * Return the form to display the forum edit/add option
  6493. * @param string Action (add/edit)
  6494. * @param integer ID of the lp_item if already exists
  6495. * @param mixed Forum ID or title
  6496. * @return string HTML form
  6497. */
  6498. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6499. {
  6500. $course_id = api_get_course_int_id();
  6501. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6502. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6503. if ($id != 0 && is_array($extra_info)) {
  6504. $item_title = stripslashes($extra_info['title']);
  6505. } elseif (is_numeric($extra_info)) {
  6506. $sql = "SELECT forum_title as title, forum_comment as comment
  6507. FROM " . $tbl_forum . "
  6508. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6509. $result = Database::query($sql);
  6510. $row = Database::fetch_array($result);
  6511. $item_title = $row['title'];
  6512. $item_description = $row['comment'];
  6513. } else {
  6514. $item_title = '';
  6515. $item_description = '';
  6516. }
  6517. if ($id != 0 && is_array($extra_info)) {
  6518. $parent = $extra_info['parent_item_id'];
  6519. } else {
  6520. $parent = 0;
  6521. }
  6522. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6523. WHERE
  6524. c_id = ".$course_id." AND
  6525. lp_id = " . $this->lp_id;
  6526. $result = Database::query($sql);
  6527. $arrLP = array();
  6528. while ($row = Database::fetch_array($result)) {
  6529. $arrLP[] = array(
  6530. 'id' => $row['id'],
  6531. 'item_type' => $row['item_type'],
  6532. 'title' => $row['title'],
  6533. 'path' => $row['path'],
  6534. 'description' => $row['description'],
  6535. 'parent_item_id' => $row['parent_item_id'],
  6536. 'previous_item_id' => $row['previous_item_id'],
  6537. 'next_item_id' => $row['next_item_id'],
  6538. 'display_order' => $row['display_order'],
  6539. 'max_score' => $row['max_score'],
  6540. 'min_score' => $row['min_score'],
  6541. 'mastery_score' => $row['mastery_score'],
  6542. 'prerequisite' => $row['prerequisite']
  6543. );
  6544. }
  6545. $this->tree_array($arrLP);
  6546. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6547. unset($this->arrMenu);
  6548. if ($action == 'add') {
  6549. $legend = get_lang('CreateTheForum');
  6550. } elseif ($action == 'move') {
  6551. $legend = get_lang('MoveTheCurrentForum');
  6552. } else {
  6553. $legend = get_lang('EditCurrentForum');
  6554. }
  6555. $form = new FormValidator(
  6556. 'forum_form',
  6557. 'POST',
  6558. $this->getCurrentBuildingModeURL()
  6559. );
  6560. $defaults = [];
  6561. $form->addHeader($legend);
  6562. if ($action != 'move') {
  6563. $form->addText(
  6564. 'title',
  6565. get_lang('Title'),
  6566. true,
  6567. ['id' => 'idTitle', 'class' => 'learnpath_item_form']
  6568. );
  6569. $defaults['title'] = $item_title;
  6570. }
  6571. $selectParent = $form->addSelect(
  6572. 'parent',
  6573. get_lang('Parent'),
  6574. [],
  6575. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6576. );
  6577. $selectParent->addOption($this->name, 0);
  6578. $arrHide = array(
  6579. $id
  6580. );
  6581. //$parent_item_id = $_SESSION['parent_item_id'];
  6582. for ($i = 0; $i < count($arrLP); $i++) {
  6583. if ($action != 'add') {
  6584. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6585. $selectParent->addOption(
  6586. $arrLP[$i]['title'],
  6587. $arrLP[$i]['id'],
  6588. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6589. );
  6590. if ($parent == $arrLP[$i]['id']) {
  6591. $selectParent->setSelected($arrLP[$i]['id']);
  6592. }
  6593. } else {
  6594. $arrHide[] = $arrLP[$i]['id'];
  6595. }
  6596. } else {
  6597. if ($arrLP[$i]['item_type'] == 'dir') {
  6598. $selectParent->addOption(
  6599. $arrLP[$i]['title'],
  6600. $arrLP[$i]['id'],
  6601. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6602. );
  6603. if ($parent == $arrLP[$i]['id']) {
  6604. $selectParent->setSelected($arrLP[$i]['id']);
  6605. }
  6606. }
  6607. }
  6608. }
  6609. if (is_array($arrLP)) {
  6610. reset($arrLP);
  6611. }
  6612. $selectPrevious = $form->addSelect(
  6613. 'previous',
  6614. get_lang('Position'),
  6615. [],
  6616. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6617. );
  6618. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6619. for ($i = 0; $i < count($arrLP); $i++) {
  6620. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6621. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6622. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6623. $selectPrevious->setSelected($arrLP[$i]['id']);
  6624. } elseif ($action == 'add') {
  6625. $selectPrevious->setSelected($arrLP[$i]['id']);
  6626. }
  6627. }
  6628. }
  6629. if ($action != 'move') {
  6630. $id_prerequisite = 0;
  6631. if (is_array($arrLP)) {
  6632. foreach ($arrLP as $key => $value) {
  6633. if ($value['id'] == $id) {
  6634. $id_prerequisite = $value['prerequisite'];
  6635. break;
  6636. }
  6637. }
  6638. }
  6639. $arrHide = array();
  6640. for ($i = 0; $i < count($arrLP); $i++) {
  6641. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6642. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6643. $s_selected_position = $arrLP[$i]['id'];
  6644. elseif ($action == 'add') $s_selected_position = 0;
  6645. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6646. }
  6647. }
  6648. }
  6649. if ($action == 'add') {
  6650. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6651. } else {
  6652. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6653. }
  6654. if ($action == 'move') {
  6655. $form->addHidden('title', $item_title);
  6656. $form->addHidden('description', $item_description);
  6657. }
  6658. if (is_numeric($extra_info)) {
  6659. $form->addHidden('path', $extra_info);
  6660. } elseif (is_array($extra_info)) {
  6661. $form->addHidden('path', $extra_info['path']);
  6662. }
  6663. $form->addHidden('type', TOOL_FORUM);
  6664. $form->addHidden('post_time', time());
  6665. $form->setDefaults($defaults);
  6666. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6667. }
  6668. /**
  6669. * Return HTML form to add/edit forum threads
  6670. * @param string Action (add/edit)
  6671. * @param integer Item ID if already exists in learning path
  6672. * @param mixed Extra information (thread ID if integer)
  6673. * @return string HTML form
  6674. */
  6675. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6676. {
  6677. $course_id = api_get_course_int_id();
  6678. if (empty($course_id)) {
  6679. return null;
  6680. }
  6681. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6682. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  6683. if ($id != 0 && is_array($extra_info)) {
  6684. $item_title = stripslashes($extra_info['title']);
  6685. } elseif (is_numeric($extra_info)) {
  6686. $sql = "SELECT thread_title as title FROM $tbl_forum
  6687. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6688. $result = Database::query($sql);
  6689. $row = Database::fetch_array($result);
  6690. $item_title = $row['title'];
  6691. $item_description = '';
  6692. } else {
  6693. $item_title = '';
  6694. $item_description = '';
  6695. }
  6696. if ($id != 0 && is_array($extra_info)) {
  6697. $parent = $extra_info['parent_item_id'];
  6698. } else {
  6699. $parent = 0;
  6700. }
  6701. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6702. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6703. $result = Database::query($sql);
  6704. $arrLP = array();
  6705. while ($row = Database::fetch_array($result)) {
  6706. $arrLP[] = array (
  6707. 'id' => $row['id'],
  6708. 'item_type' => $row['item_type'],
  6709. 'title' => $row['title'],
  6710. 'path' => $row['path'],
  6711. 'description' => $row['description'],
  6712. 'parent_item_id' => $row['parent_item_id'],
  6713. 'previous_item_id' => $row['previous_item_id'],
  6714. 'next_item_id' => $row['next_item_id'],
  6715. 'display_order' => $row['display_order'],
  6716. 'max_score' => $row['max_score'],
  6717. 'min_score' => $row['min_score'],
  6718. 'mastery_score' => $row['mastery_score'],
  6719. 'prerequisite' => $row['prerequisite']
  6720. );
  6721. }
  6722. $this->tree_array($arrLP);
  6723. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6724. unset ($this->arrMenu);
  6725. $form = new FormValidator(
  6726. 'thread_form',
  6727. 'POST',
  6728. $this->getCurrentBuildingModeURL()
  6729. );
  6730. $defaults = [];
  6731. if ($action == 'add') {
  6732. $legend = get_lang('CreateTheForum');
  6733. } elseif ($action == 'move') {
  6734. $legend = get_lang('MoveTheCurrentForum');
  6735. } else {
  6736. $legend = get_lang('EditCurrentForum');
  6737. }
  6738. $form->addHeader($legend);
  6739. $selectParent = $form->addSelect(
  6740. 'parent',
  6741. get_lang('Parent'),
  6742. [],
  6743. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6744. );
  6745. $selectParent->addOption($this->name, 0);
  6746. $arrHide = array (
  6747. $id
  6748. );
  6749. for ($i = 0; $i < count($arrLP); $i++) {
  6750. if ($action != 'add') {
  6751. if (
  6752. ($arrLP[$i]['item_type'] == 'dir') &&
  6753. !in_array($arrLP[$i]['id'], $arrHide) &&
  6754. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6755. ) {
  6756. $selectParent->addOption(
  6757. $arrLP[$i]['title'],
  6758. $arrLP[$i]['id'],
  6759. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6760. );
  6761. if ($parent == $arrLP[$i]['id']) {
  6762. $selectParent->setSelected($arrLP[$i]['id']);
  6763. }
  6764. } else {
  6765. $arrHide[] = $arrLP[$i]['id'];
  6766. }
  6767. } else {
  6768. if ($arrLP[$i]['item_type'] == 'dir') {
  6769. $selectParent->addOption(
  6770. $arrLP[$i]['title'],
  6771. $arrLP[$i]['id'],
  6772. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6773. );
  6774. if ($parent == $arrLP[$i]['id']) {
  6775. $selectParent->setSelected($arrLP[$i]['id']);
  6776. }
  6777. }
  6778. }
  6779. }
  6780. if ($arrLP != null) {
  6781. reset($arrLP);
  6782. }
  6783. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6784. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6785. for ($i = 0; $i < count($arrLP); $i++) {
  6786. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6787. $selectPrevious->addOption(
  6788. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6789. $arrLP[$i]['id']
  6790. );
  6791. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6792. $selectPrevious->setSelected($arrLP[$i]['id']);
  6793. } elseif ($action == 'add') {
  6794. $selectPrevious->setSelected($arrLP[$i]['id']);
  6795. }
  6796. }
  6797. }
  6798. if ($action != 'move') {
  6799. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6800. $defaults['title'] = $item_title;
  6801. $id_prerequisite = 0;
  6802. if ($arrLP != null) {
  6803. foreach ($arrLP as $key => $value) {
  6804. if ($value['id'] == $id) {
  6805. $id_prerequisite = $value['prerequisite'];
  6806. break;
  6807. }
  6808. }
  6809. }
  6810. $arrHide = array();
  6811. $s_selected_position = 0;
  6812. for ($i = 0; $i < count($arrLP); $i++) {
  6813. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6814. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6815. $s_selected_position = $arrLP[$i]['id'];
  6816. elseif ($action == 'add') $s_selected_position = 0;
  6817. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6818. }
  6819. }
  6820. $selectPrerequisites = $form->addSelect(
  6821. 'prerequisites',
  6822. get_lang('LearnpathPrerequisites'),
  6823. [],
  6824. ['id' => 'prerequisites']
  6825. );
  6826. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6827. foreach ($arrHide as $key => $value) {
  6828. $selectPrerequisites->addOption($value['value'], $key);
  6829. if ($key == $s_selected_position && $action == 'add') {
  6830. $selectPrerequisites->setSelected($key);
  6831. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6832. $selectPrerequisites->setSelected($key);
  6833. }
  6834. }
  6835. }
  6836. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6837. if ($action == 'move') {
  6838. $form->addHidden('title', $item_title);
  6839. $form->addHidden('description', $item_description);
  6840. }
  6841. if (is_numeric($extra_info)) {
  6842. $form->addHidden('path', $extra_info);
  6843. }
  6844. elseif (is_array($extra_info)) {
  6845. $form->addHidden('path', $extra_info['path']);
  6846. }
  6847. $form->addHidden('type', TOOL_THREAD);
  6848. $form->addHidden('post_time', time());
  6849. $form->setDefaults($defaults);
  6850. return $form->returnForm();
  6851. }
  6852. /**
  6853. * Return the HTML form to display an item (generally a dir item)
  6854. * @param string Item type (dir)
  6855. * @param string Title (optional, only when creating)
  6856. * @param string Action ('add'/'edit')
  6857. * @param integer lp_item ID
  6858. * @param mixed Extra info
  6859. * @return string HTML form
  6860. */
  6861. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6862. {
  6863. $course_id = api_get_course_int_id();
  6864. $_course = api_get_course_info();
  6865. global $charset;
  6866. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6867. if ($id != 0 && is_array($extra_info)) {
  6868. $item_title = $extra_info['title'];
  6869. $item_description = $extra_info['description'];
  6870. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6871. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6872. } else {
  6873. $item_title = '';
  6874. $item_description = '';
  6875. $item_path_fck = '';
  6876. }
  6877. if ($id != 0 && is_array($extra_info)) {
  6878. $parent = $extra_info['parent_item_id'];
  6879. } else {
  6880. $parent = 0;
  6881. }
  6882. $id = intval($id);
  6883. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6884. WHERE
  6885. c_id = ".$course_id." AND
  6886. lp_id = " . $this->lp_id . " AND
  6887. id != $id";
  6888. if ($item_type == 'dir') {
  6889. $sql .= " AND parent_item_id = 0";
  6890. }
  6891. $result = Database::query($sql);
  6892. $arrLP = [];
  6893. while ($row = Database::fetch_array($result)) {
  6894. $arrLP[] = array(
  6895. 'id' => $row['id'],
  6896. 'item_type' => $row['item_type'],
  6897. 'title' => $row['title'],
  6898. 'path' => $row['path'],
  6899. 'description' => $row['description'],
  6900. 'parent_item_id' => $row['parent_item_id'],
  6901. 'previous_item_id' => $row['previous_item_id'],
  6902. 'next_item_id' => $row['next_item_id'],
  6903. 'max_score' => $row['max_score'],
  6904. 'min_score' => $row['min_score'],
  6905. 'mastery_score' => $row['mastery_score'],
  6906. 'prerequisite' => $row['prerequisite'],
  6907. 'display_order' => $row['display_order']
  6908. );
  6909. }
  6910. $this->tree_array($arrLP);
  6911. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6912. unset($this->arrMenu);
  6913. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  6914. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6915. $form = new FormValidator('form', 'POST', $url);
  6916. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6917. $defaults['description'] = $item_description;
  6918. $form->addElement('header', $title);
  6919. //$arrHide = array($id);
  6920. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  6921. $arrHide[0]['padding'] = 20;
  6922. $charset = api_get_system_encoding();
  6923. for ($i = 0; $i < count($arrLP); $i++) {
  6924. if ($action != 'add') {
  6925. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6926. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6927. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6928. if ($parent == $arrLP[$i]['id']) {
  6929. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6930. }
  6931. }
  6932. } else {
  6933. if ($arrLP[$i]['item_type'] == 'dir') {
  6934. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6935. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6936. if ($parent == $arrLP[$i]['id']) {
  6937. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6938. }
  6939. }
  6940. }
  6941. }
  6942. if ($action != 'move') {
  6943. $form->addElement('text', 'title', get_lang('Title'));
  6944. $form->applyFilter('title', 'html_filter');
  6945. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6946. } else {
  6947. $form->addElement('hidden', 'title');
  6948. }
  6949. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6950. foreach ($arrHide as $key => $value) {
  6951. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6952. }
  6953. if (!empty($s_selected_parent)) {
  6954. $parent_select->setSelected($s_selected_parent);
  6955. }
  6956. if (is_array($arrLP)) {
  6957. reset($arrLP);
  6958. }
  6959. $arrHide = array();
  6960. // POSITION
  6961. for ($i = 0; $i < count($arrLP); $i++) {
  6962. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6963. //this is the same!
  6964. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6965. $s_selected_position = $arrLP[$i]['id'];
  6966. } elseif ($action == 'add') {
  6967. $s_selected_position = $arrLP[$i]['id'];
  6968. }
  6969. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6970. }
  6971. }
  6972. $position = $form->addElement(
  6973. 'select',
  6974. 'previous',
  6975. get_lang('Position'),
  6976. '',
  6977. array('id' => 'previous')
  6978. );
  6979. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6980. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6981. foreach ($arrHide as $key => $value) {
  6982. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6983. }
  6984. if (!empty ($s_selected_position)) {
  6985. $position->setSelected($s_selected_position);
  6986. }
  6987. if (is_array($arrLP)) {
  6988. reset($arrLP);
  6989. }
  6990. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6991. //fix in order to use the tab
  6992. if ($item_type == 'dir') {
  6993. $form->addElement('hidden', 'type', 'dir');
  6994. }
  6995. $extension = null;
  6996. if (!empty($item_path)) {
  6997. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6998. }
  6999. //assets can't be modified
  7000. //$item_type == 'asset' ||
  7001. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7002. if ($item_type == 'sco') {
  7003. $form->addElement('html', '<script>alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  7004. }
  7005. $renderer = $form->defaultRenderer();
  7006. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7007. $relative_prefix = '';
  7008. $editor_config = array(
  7009. 'ToolbarSet' => 'LearningPathDocuments',
  7010. 'Width' => '100%',
  7011. 'Height' => '500',
  7012. 'FullPage' => true,
  7013. 'CreateDocumentDir' => $relative_prefix,
  7014. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7015. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7016. );
  7017. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7018. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7019. $defaults['content_lp'] = file_get_contents($content_path);
  7020. }
  7021. $form->addElement('hidden', 'type', $item_type);
  7022. $form->addElement('hidden', 'post_time', time());
  7023. $form->setDefaults($defaults);
  7024. return $form->return_form();
  7025. }
  7026. /**
  7027. * @return string
  7028. */
  7029. public function getCurrentBuildingModeURL()
  7030. {
  7031. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7032. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7033. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7034. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7035. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7036. return $currentUrl;
  7037. }
  7038. /**
  7039. * Returns the form to update or create a document
  7040. * @param string $action (add/edit)
  7041. * @param integer $id ID of the lp_item (if already exists)
  7042. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7043. * @return string HTML form
  7044. */
  7045. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7046. {
  7047. $course_id = api_get_course_int_id();
  7048. $_course = api_get_course_info();
  7049. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7050. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7051. $no_display_edit_textarea = false;
  7052. $item_description = '';
  7053. //If action==edit document
  7054. //We don't display the document form if it's not an editable document (html or txt file)
  7055. if ($action == 'edit') {
  7056. if (is_array($extra_info)) {
  7057. $path_parts = pathinfo($extra_info['dir']);
  7058. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7059. $no_display_edit_textarea = true;
  7060. }
  7061. }
  7062. }
  7063. $no_display_add = false;
  7064. // If action==add an existing document
  7065. // We don't display the document form if it's not an editable document (html or txt file).
  7066. if ($action == 'add') {
  7067. if (is_numeric($extra_info)) {
  7068. $sql_doc = "SELECT path FROM $tbl_doc
  7069. WHERE c_id = $course_id AND id = " . intval($extra_info);
  7070. $result = Database::query($sql_doc);
  7071. $path_file = Database::result($result, 0, 0);
  7072. $path_parts = pathinfo($path_file);
  7073. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7074. $no_display_add = true;
  7075. }
  7076. }
  7077. }
  7078. if ($id != 0 && is_array($extra_info)) {
  7079. $item_title = stripslashes($extra_info['title']);
  7080. $item_description = stripslashes($extra_info['description']);
  7081. $item_terms = stripslashes($extra_info['terms']);
  7082. if (empty ($item_title)) {
  7083. $path_parts = pathinfo($extra_info['path']);
  7084. $item_title = stripslashes($path_parts['filename']);
  7085. }
  7086. } elseif (is_numeric($extra_info)) {
  7087. $sql = "SELECT path, title FROM $tbl_doc
  7088. WHERE
  7089. c_id = ".$course_id." AND
  7090. id = " . intval($extra_info);
  7091. $result = Database::query($sql);
  7092. $row = Database::fetch_array($result);
  7093. $item_title = $row['title'];
  7094. $item_title = str_replace('_', ' ', $item_title);
  7095. if (empty ($item_title)) {
  7096. $path_parts = pathinfo($row['path']);
  7097. $item_title = stripslashes($path_parts['filename']);
  7098. }
  7099. } else {
  7100. $item_title = '';
  7101. $item_description = '';
  7102. }
  7103. $return = '<legend>';
  7104. if ($id != 0 && is_array($extra_info)) {
  7105. $parent = $extra_info['parent_item_id'];
  7106. } else {
  7107. $parent = 0;
  7108. }
  7109. $sql = "SELECT * FROM $tbl_lp_item
  7110. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7111. $result = Database::query($sql);
  7112. $arrLP = array();
  7113. while ($row = Database::fetch_array($result)) {
  7114. $arrLP[] = array(
  7115. 'id' => $row['id'],
  7116. 'item_type' => $row['item_type'],
  7117. 'title' => $row['title'],
  7118. 'path' => $row['path'],
  7119. 'description' => $row['description'],
  7120. 'parent_item_id' => $row['parent_item_id'],
  7121. 'previous_item_id' => $row['previous_item_id'],
  7122. 'next_item_id' => $row['next_item_id'],
  7123. 'display_order' => $row['display_order'],
  7124. 'max_score' => $row['max_score'],
  7125. 'min_score' => $row['min_score'],
  7126. 'mastery_score' => $row['mastery_score'],
  7127. 'prerequisite' => $row['prerequisite']
  7128. );
  7129. }
  7130. $this->tree_array($arrLP);
  7131. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7132. unset ($this->arrMenu);
  7133. if ($action == 'add') {
  7134. $return .= get_lang('CreateTheDocument');
  7135. } elseif ($action == 'move') {
  7136. $return .= get_lang('MoveTheCurrentDocument');
  7137. } else {
  7138. $return .= get_lang('EditTheCurrentDocument');
  7139. }
  7140. $return .= '</legend>';
  7141. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7142. $return .= Display::return_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7143. }
  7144. $form = new FormValidator(
  7145. 'form',
  7146. 'POST',
  7147. $this->getCurrentBuildingModeURL(),
  7148. '',
  7149. array('enctype' => 'multipart/form-data')
  7150. );
  7151. $defaults['title'] = Security::remove_XSS($item_title);
  7152. if (empty($item_title)) {
  7153. $defaults['title'] = Security::remove_XSS($item_title);
  7154. }
  7155. $defaults['description'] = $item_description;
  7156. $form->addElement('html', $return);
  7157. if ($action != 'move') {
  7158. $data = $this->generate_lp_folder($_course);
  7159. if ($action != 'edit') {
  7160. $folders = DocumentManager::get_all_document_folders(
  7161. $_course,
  7162. 0,
  7163. true
  7164. );
  7165. DocumentManager::build_directory_selector(
  7166. $folders,
  7167. '',
  7168. array(),
  7169. true,
  7170. $form,
  7171. 'directory_parent_id'
  7172. );
  7173. }
  7174. if (isset($data['id'])) {
  7175. $defaults['directory_parent_id'] = $data['id'];
  7176. }
  7177. $form->addElement(
  7178. 'text',
  7179. 'title',
  7180. get_lang('Title'),
  7181. array('id' => 'idTitle', 'class' => 'col-md-4')
  7182. );
  7183. $form->applyFilter('title', 'html_filter');
  7184. }
  7185. $arrHide[0]['value'] = $this->name;
  7186. $arrHide[0]['padding'] = 20;
  7187. for ($i = 0; $i < count($arrLP); $i++) {
  7188. if ($action != 'add') {
  7189. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7190. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7191. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7192. if ($parent == $arrLP[$i]['id']) {
  7193. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7194. }
  7195. }
  7196. } else {
  7197. if ($arrLP[$i]['item_type'] == 'dir') {
  7198. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7199. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7200. if ($parent == $arrLP[$i]['id']) {
  7201. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7202. }
  7203. }
  7204. }
  7205. }
  7206. $parent_select = $form->addSelect(
  7207. 'parent',
  7208. get_lang('Parent'),
  7209. [],
  7210. [
  7211. 'id' => 'idParent',
  7212. 'onchange' => 'javascript: load_cbo(this.value);',
  7213. ]
  7214. );
  7215. $my_count = 0;
  7216. foreach ($arrHide as $key => $value) {
  7217. if ($my_count != 0) {
  7218. // The LP name is also the first section and is not in the same charset like the other sections.
  7219. $value['value'] = Security::remove_XSS($value['value']);
  7220. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7221. } else {
  7222. $value['value'] = Security::remove_XSS($value['value']);
  7223. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7224. }
  7225. $my_count++;
  7226. }
  7227. if (!empty($id)) {
  7228. $parent_select->setSelected($parent);
  7229. } else {
  7230. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7231. $parent_select->setSelected($parent_item_id);
  7232. }
  7233. if (is_array($arrLP)) {
  7234. reset($arrLP);
  7235. }
  7236. $arrHide = array();
  7237. $s_selected_position = null;
  7238. // POSITION
  7239. $lastPosition = null;
  7240. for ($i = 0; $i < count($arrLP); $i++) {
  7241. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7242. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7243. ) {
  7244. if ((isset($extra_info['previous_item_id']) &&
  7245. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  7246. ) {
  7247. $s_selected_position = $arrLP[$i]['id'];
  7248. }
  7249. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7250. }
  7251. $lastPosition = $arrLP[$i]['id'];
  7252. }
  7253. if (empty($s_selected_position)) {
  7254. $s_selected_position = $lastPosition;
  7255. }
  7256. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7257. $position->addOption(get_lang('FirstPosition'), 0);
  7258. foreach ($arrHide as $key => $value) {
  7259. $padding = isset($value['padding']) ? $value['padding']: 20;
  7260. $position->addOption(
  7261. $value['value'],
  7262. $key,
  7263. 'style="padding-left:'.$padding.'px;"'
  7264. );
  7265. }
  7266. $position->setSelected($s_selected_position);
  7267. if (is_array($arrLP)) {
  7268. reset($arrLP);
  7269. }
  7270. if ($action != 'move') {
  7271. $id_prerequisite = 0;
  7272. if (is_array($arrLP)) {
  7273. foreach ($arrLP as $key => $value) {
  7274. if ($value['id'] == $id) {
  7275. $id_prerequisite = $value['prerequisite'];
  7276. break;
  7277. }
  7278. }
  7279. }
  7280. $arrHide = array();
  7281. for ($i = 0; $i < count($arrLP); $i++) {
  7282. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7283. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7284. $s_selected_position = $arrLP[$i]['id'];
  7285. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7286. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7287. }
  7288. }
  7289. if (!$no_display_add) {
  7290. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7291. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7292. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true') {
  7293. if (isset ($_POST['content'])) {
  7294. $content = stripslashes($_POST['content']);
  7295. } elseif (is_array($extra_info)) {
  7296. //If it's an html document or a text file
  7297. if (!$no_display_edit_textarea) {
  7298. $content = $this->display_document($extra_info['path'], false, false);
  7299. }
  7300. } elseif (is_numeric($extra_info)) {
  7301. $content = $this->display_document(
  7302. $extra_info,
  7303. false,
  7304. false
  7305. );
  7306. } else {
  7307. $content = '';
  7308. }
  7309. if (!$no_display_edit_textarea) {
  7310. // We need to calculate here some specific settings for the online editor.
  7311. // The calculated settings work for documents in the Documents tool
  7312. // (on the root or in subfolders).
  7313. // For documents in native scorm packages it is unclear whether the
  7314. // online editor should be activated or not.
  7315. // A new document, it is in the root of the repository.
  7316. $relative_path = '';
  7317. $relative_prefix = '';
  7318. if (is_array($extra_info) && $extra_info != 'new') {
  7319. // The document already exists. Whe have to determine its relative path towards the repository root.
  7320. $relative_path = explode('/', $extra_info['dir']);
  7321. $cnt = count($relative_path) - 2;
  7322. if ($cnt < 0) {
  7323. $cnt = 0;
  7324. }
  7325. $relative_prefix = str_repeat('../', $cnt);
  7326. $relative_path = array_slice($relative_path, 1, $cnt);
  7327. $relative_path = implode('/', $relative_path);
  7328. if (strlen($relative_path) > 0) {
  7329. $relative_path = $relative_path . '/';
  7330. }
  7331. } else {
  7332. $result = $this->generate_lp_folder($_course);
  7333. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7334. $relative_prefix = '../../';
  7335. }
  7336. $editor_config = array(
  7337. 'ToolbarSet' => 'LearningPathDocuments',
  7338. 'Width' => '100%',
  7339. 'Height' => '500',
  7340. 'FullPage' => true,
  7341. 'CreateDocumentDir' => $relative_prefix,
  7342. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7343. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7344. );
  7345. if ($_GET['action'] == 'add_item') {
  7346. $class = 'add';
  7347. $text = get_lang('LPCreateDocument');
  7348. } else {
  7349. if ($_GET['action'] == 'edit_item') {
  7350. $class = 'save';
  7351. $text = get_lang('SaveDocument');
  7352. }
  7353. }
  7354. $form->addButtonSave($text, 'submit_button');
  7355. $renderer = $form->defaultRenderer();
  7356. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7357. $form->addElement('html', '<div class="editor-lp">');
  7358. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7359. $form->addElement('html', '</div>');
  7360. $defaults['content_lp'] = $content;
  7361. }
  7362. } elseif (is_numeric($extra_info)) {
  7363. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7364. $return = $this->display_document($extra_info, true, true, true);
  7365. $form->addElement('html', $return);
  7366. }
  7367. }
  7368. }
  7369. if (isset($extra_info['item_type'] ) &&
  7370. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7371. ) {
  7372. $parent_select->freeze();
  7373. $position->freeze();
  7374. }
  7375. if ($action == 'move') {
  7376. $form->addElement('hidden', 'title', $item_title);
  7377. $form->addElement('hidden', 'description', $item_description);
  7378. }
  7379. if (is_numeric($extra_info)) {
  7380. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7381. $form->addElement('hidden', 'path', $extra_info);
  7382. } elseif (is_array($extra_info)) {
  7383. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7384. $form->addElement('hidden', 'path', $extra_info['path']);
  7385. }
  7386. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7387. $form->addElement('hidden', 'post_time', time());
  7388. $form->setDefaults($defaults);
  7389. return $form->returnForm();
  7390. }
  7391. /**
  7392. * Return HTML form to add/edit a link item
  7393. * @param string $action (add/edit)
  7394. * @param integer $id Item ID if exists
  7395. * @param mixed $extra_info
  7396. * @return string HTML form
  7397. */
  7398. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7399. {
  7400. $course_id = api_get_course_int_id();
  7401. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7402. $tbl_link = Database::get_course_table(TABLE_LINK);
  7403. if ($id != 0 && is_array($extra_info)) {
  7404. $item_title = stripslashes($extra_info['title']);
  7405. $item_description = stripslashes($extra_info['description']);
  7406. $item_url = stripslashes($extra_info['url']);
  7407. } elseif (is_numeric($extra_info)) {
  7408. $extra_info = intval($extra_info);
  7409. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7410. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7411. $result = Database::query($sql);
  7412. $row = Database::fetch_array($result);
  7413. $item_title = $row['title'];
  7414. $item_description = $row['description'];
  7415. $item_url = $row['url'];
  7416. } else {
  7417. $item_title = '';
  7418. $item_description = '';
  7419. $item_url = '';
  7420. }
  7421. $form = new FormValidator(
  7422. 'edit_link',
  7423. 'POST',
  7424. $this->getCurrentBuildingModeURL()
  7425. );
  7426. $defaults = [];
  7427. if ($id != 0 && is_array($extra_info)) {
  7428. $parent = $extra_info['parent_item_id'];
  7429. } else {
  7430. $parent = 0;
  7431. }
  7432. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7433. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7434. $result = Database::query($sql);
  7435. $arrLP = array();
  7436. while ($row = Database::fetch_array($result)) {
  7437. $arrLP[] = array(
  7438. 'id' => $row['id'],
  7439. 'item_type' => $row['item_type'],
  7440. 'title' => $row['title'],
  7441. 'path' => $row['path'],
  7442. 'description' => $row['description'],
  7443. 'parent_item_id' => $row['parent_item_id'],
  7444. 'previous_item_id' => $row['previous_item_id'],
  7445. 'next_item_id' => $row['next_item_id'],
  7446. 'display_order' => $row['display_order'],
  7447. 'max_score' => $row['max_score'],
  7448. 'min_score' => $row['min_score'],
  7449. 'mastery_score' => $row['mastery_score'],
  7450. 'prerequisite' => $row['prerequisite']
  7451. );
  7452. }
  7453. $this->tree_array($arrLP);
  7454. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7455. unset ($this->arrMenu);
  7456. if ($action == 'add') {
  7457. $legend = get_lang('CreateTheLink');
  7458. } elseif ($action == 'move') {
  7459. $legend = get_lang('MoveCurrentLink');
  7460. } else {
  7461. $legend = get_lang('EditCurrentLink');
  7462. }
  7463. $form->addHeader($legend);
  7464. if ($action != 'move') {
  7465. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7466. $defaults['title'] = $item_title;
  7467. }
  7468. $selectParent = $form->addSelect(
  7469. 'parent',
  7470. get_lang('Parent'),
  7471. [],
  7472. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7473. );
  7474. $selectParent->addOption($this->name, 0);
  7475. $arrHide = array(
  7476. $id
  7477. );
  7478. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7479. for ($i = 0; $i < count($arrLP); $i++) {
  7480. if ($action != 'add') {
  7481. if (
  7482. ($arrLP[$i]['item_type'] == 'dir') &&
  7483. !in_array($arrLP[$i]['id'], $arrHide) &&
  7484. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7485. ) {
  7486. $selectParent->addOption(
  7487. $arrLP[$i]['title'],
  7488. $arrLP[$i]['id'],
  7489. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7490. );
  7491. if ($parent == $arrLP[$i]['id']) {
  7492. $selectParent->setSelected($arrLP[$i]['id']);
  7493. }
  7494. } else {
  7495. $arrHide[] = $arrLP[$i]['id'];
  7496. }
  7497. } else {
  7498. if ($arrLP[$i]['item_type'] == 'dir') {
  7499. $selectParent->addOption(
  7500. $arrLP[$i]['title'],
  7501. $arrLP[$i]['id'],
  7502. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7503. );
  7504. if ($parent_item_id == $arrLP[$i]['id']) {
  7505. $selectParent->setSelected($arrLP[$i]['id']);
  7506. }
  7507. }
  7508. }
  7509. }
  7510. if (is_array($arrLP)) {
  7511. reset($arrLP);
  7512. }
  7513. $selectPrevious = $form->addSelect(
  7514. 'previous',
  7515. get_lang('Position'),
  7516. [],
  7517. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7518. );
  7519. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7520. for ($i = 0; $i < count($arrLP); $i++) {
  7521. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7522. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7523. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7524. $selectPrevious->setSelected($arrLP[$i]['id']);
  7525. } elseif ($action == 'add') {
  7526. $selectPrevious->setSelected($arrLP[$i]['id']);
  7527. }
  7528. }
  7529. }
  7530. if ($action != 'move') {
  7531. $urlAttributes = ['class' => 'learnpath_item_form'];
  7532. if (is_numeric($extra_info)) {
  7533. $urlAttributes['disabled'] = 'disabled';
  7534. }
  7535. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7536. $defaults['url'] = $item_url;
  7537. $id_prerequisite = 0;
  7538. if (is_array($arrLP)) {
  7539. foreach ($arrLP as $key => $value) {
  7540. if ($value['id'] == $id) {
  7541. $id_prerequisite = $value['prerequisite'];
  7542. break;
  7543. }
  7544. }
  7545. }
  7546. $arrHide = array();
  7547. for ($i = 0; $i < count($arrLP); $i++) {
  7548. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7549. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7550. $s_selected_position = $arrLP[$i]['id'];
  7551. elseif ($action == 'add') $s_selected_position = 0;
  7552. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7553. }
  7554. }
  7555. }
  7556. if ($action == 'add') {
  7557. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7558. } else {
  7559. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7560. }
  7561. if ($action == 'move') {
  7562. $form->addHidden('title', $item_title);
  7563. $form->addHidden('description', $item_description);
  7564. }
  7565. if (is_numeric($extra_info)) {
  7566. $form->addHidden('path', $extra_info);
  7567. } elseif (is_array($extra_info)) {
  7568. $form->addHidden('path', $extra_info['path']);
  7569. }
  7570. $form->addHidden('type', TOOL_LINK);
  7571. $form->addHidden('post_time', time());
  7572. $form->setDefaults($defaults);
  7573. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7574. }
  7575. /**
  7576. * Return HTML form to add/edit a student publication (work)
  7577. * @param string Action (add/edit)
  7578. * @param integer Item ID if already exists
  7579. * @param mixed Extra info (work ID if integer)
  7580. * @return string HTML form
  7581. */
  7582. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7583. {
  7584. $course_id = api_get_course_int_id();
  7585. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7586. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  7587. if ($id != 0 && is_array($extra_info)) {
  7588. $item_title = stripslashes($extra_info['title']);
  7589. $item_description = stripslashes($extra_info['description']);
  7590. } elseif (is_numeric($extra_info)) {
  7591. $extra_info = intval($extra_info);
  7592. $sql = "SELECT title, description
  7593. FROM $tbl_publication
  7594. WHERE c_id = $course_id AND id = " . $extra_info;
  7595. $result = Database::query($sql);
  7596. $row = Database::fetch_array($result);
  7597. $item_title = $row['title'];
  7598. } else {
  7599. $item_title = get_lang('Student_publication');
  7600. }
  7601. if ($id != 0 && is_array($extra_info)) {
  7602. $parent = $extra_info['parent_item_id'];
  7603. } else {
  7604. $parent = 0;
  7605. }
  7606. $sql = "SELECT * FROM $tbl_lp_item
  7607. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7608. $result = Database::query($sql);
  7609. $arrLP = array();
  7610. while ($row = Database::fetch_array($result)) {
  7611. $arrLP[] = array(
  7612. 'id' => $row['id'],
  7613. 'item_type' => $row['item_type'],
  7614. 'title' => $row['title'],
  7615. 'path' => $row['path'],
  7616. 'description' => $row['description'],
  7617. 'parent_item_id' => $row['parent_item_id'],
  7618. 'previous_item_id' => $row['previous_item_id'],
  7619. 'next_item_id' => $row['next_item_id'],
  7620. 'display_order' => $row['display_order'],
  7621. 'max_score' => $row['max_score'],
  7622. 'min_score' => $row['min_score'],
  7623. 'mastery_score' => $row['mastery_score'],
  7624. 'prerequisite' => $row['prerequisite']
  7625. );
  7626. }
  7627. $this->tree_array($arrLP);
  7628. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7629. unset($this->arrMenu);
  7630. $form = new FormValidator('frm_student_publication', 'post', '#');
  7631. if ($action == 'add') {
  7632. $form->addHeader(get_lang('Student_publication'));
  7633. } elseif ($action == 'move') {
  7634. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7635. } else {
  7636. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7637. }
  7638. if ($action != 'move') {
  7639. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7640. }
  7641. $parentSelect = $form->addSelect(
  7642. 'parent',
  7643. get_lang('Parent'),
  7644. ['0' => $this->name],
  7645. [
  7646. 'onchange' => 'javascript: load_cbo(this.value);',
  7647. 'class' => 'learnpath_item_form',
  7648. 'id' => 'idParent'
  7649. ]
  7650. );
  7651. $arrHide = array($id);
  7652. for ($i = 0; $i < count($arrLP); $i++) {
  7653. if ($action != 'add') {
  7654. if (
  7655. ($arrLP[$i]['item_type'] == 'dir') &&
  7656. !in_array($arrLP[$i]['id'], $arrHide) &&
  7657. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7658. ) {
  7659. $parentSelect->addOption(
  7660. $arrLP[$i]['title'],
  7661. $arrLP[$i]['id'],
  7662. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7663. );
  7664. if ($parent == $arrLP[$i]['id']) {
  7665. $parentSelect->setSelected($arrLP[$i]['id']);
  7666. }
  7667. } else {
  7668. $arrHide[] = $arrLP[$i]['id'];
  7669. }
  7670. } else {
  7671. if ($arrLP[$i]['item_type'] == 'dir') {
  7672. $parentSelect->addOption(
  7673. $arrLP[$i]['title'],
  7674. $arrLP[$i]['id'],
  7675. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7676. );
  7677. if ($parent == $arrLP[$i]['id']) {
  7678. $parentSelect->setSelected($arrLP[$i]['id']);
  7679. }
  7680. }
  7681. }
  7682. }
  7683. if (is_array($arrLP)) {
  7684. reset($arrLP);
  7685. }
  7686. $previousSelect = $form->addSelect(
  7687. 'previous',
  7688. get_lang('Position'),
  7689. ['0' => get_lang('FirstPosition')],
  7690. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7691. );
  7692. for ($i = 0; $i < count($arrLP); $i++) {
  7693. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7694. $previousSelect->addOption(
  7695. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7696. $arrLP[$i]['id']
  7697. );
  7698. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7699. $previousSelect->setSelected($arrLP[$i]['id']);
  7700. } elseif ($action == 'add') {
  7701. $previousSelect->setSelected($arrLP[$i]['id']);
  7702. }
  7703. }
  7704. }
  7705. if ($action != 'move') {
  7706. $id_prerequisite = 0;
  7707. if (is_array($arrLP)) {
  7708. foreach ($arrLP as $key => $value) {
  7709. if ($value['id'] == $id) {
  7710. $id_prerequisite = $value['prerequisite'];
  7711. break;
  7712. }
  7713. }
  7714. }
  7715. $arrHide = array ();
  7716. for ($i = 0; $i < count($arrLP); $i++) {
  7717. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7718. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7719. $s_selected_position = $arrLP[$i]['id'];
  7720. elseif ($action == 'add') $s_selected_position = 0;
  7721. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7722. }
  7723. }
  7724. }
  7725. if ($action == 'add') {
  7726. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7727. } else {
  7728. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7729. }
  7730. if ($action == 'move') {
  7731. $form->addHidden('title', $item_title);
  7732. $form->addHidden('description', $item_description);
  7733. }
  7734. if (is_numeric($extra_info)) {
  7735. $form->addHidden('path', $extra_info);
  7736. } elseif (is_array($extra_info)) {
  7737. $form->addHidden('path', $extra_info['path']);
  7738. }
  7739. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7740. $form->addHidden('post_time', time());
  7741. $form->setDefaults(['title' => $item_title]);
  7742. $return = '<div class="sectioncomment">';
  7743. $return .= $form->returnForm();
  7744. $return .= '</div>';
  7745. return $return;
  7746. }
  7747. /**
  7748. * Displays the menu for manipulating a step
  7749. *
  7750. * @param $item_id
  7751. * @param string $item_type
  7752. * @return string
  7753. */
  7754. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7755. {
  7756. $_course = api_get_course_info();
  7757. $course_id = api_get_course_int_id();
  7758. $course_code = api_get_course_id();
  7759. $return = '<div class="actions">';
  7760. switch ($item_type) {
  7761. case 'dir':
  7762. // Commented the message cause should not show it.
  7763. //$lang = get_lang('TitleManipulateChapter');
  7764. break;
  7765. case TOOL_LP_FINAL_ITEM:
  7766. case TOOL_DOCUMENT:
  7767. // Commented the message cause should not show it.
  7768. //$lang = get_lang('TitleManipulateDocument');
  7769. break;
  7770. case TOOL_LINK:
  7771. case 'link' :
  7772. // Commented the message cause should not show it.
  7773. //$lang = get_lang('TitleManipulateLink');
  7774. break;
  7775. case TOOL_QUIZ:
  7776. // Commented the message cause should not show it.
  7777. //$lang = get_lang('TitleManipulateQuiz');
  7778. break;
  7779. case TOOL_STUDENTPUBLICATION:
  7780. // Commented the message cause should not show it.
  7781. //$lang = get_lang('TitleManipulateStudentPublication');
  7782. break;
  7783. }
  7784. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7785. $item_id = intval($item_id);
  7786. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7787. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7788. $result = Database::query($sql);
  7789. $row = Database::fetch_assoc($result);
  7790. $audio_player = null;
  7791. // We display an audio player if needed.
  7792. if (!empty($row['audio'])) {
  7793. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7794. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7795. </div>';
  7796. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7797. $audio_player .= '<script>
  7798. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7799. s1.addParam("allowscriptaccess","always");
  7800. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7801. s1.write("container");
  7802. </script>';
  7803. }
  7804. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7805. if ($item_type != TOOL_LP_FINAL_ITEM) {
  7806. $return .= Display::url(
  7807. Display::return_icon(
  7808. 'edit.png',
  7809. get_lang('Edit'),
  7810. array(),
  7811. ICON_SIZE_SMALL
  7812. ),
  7813. $url.'&action=edit_item&path_item='.$row['path']
  7814. );
  7815. $return .= Display::url(
  7816. Display::return_icon(
  7817. 'move.png',
  7818. get_lang('Move'),
  7819. array(),
  7820. ICON_SIZE_SMALL
  7821. ),
  7822. $url.'&action=move_item'
  7823. );
  7824. }
  7825. // Commented for now as prerequisites cannot be added to chapters.
  7826. if ($item_type != 'dir') {
  7827. $return .= Display::url(
  7828. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7829. $url.'&action=edit_item_prereq'
  7830. );
  7831. }
  7832. $return .= Display::url(
  7833. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7834. $url.'&action=delete_item'
  7835. );
  7836. if ($item_type == TOOL_HOTPOTATOES) {
  7837. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7838. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7839. }
  7840. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7841. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7842. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7843. }
  7844. $return .= '</div>';
  7845. if (!empty($audio_player)) {
  7846. $return .= '<br />'.$audio_player;
  7847. }
  7848. return $return;
  7849. }
  7850. /**
  7851. * Creates the javascript needed for filling up the checkboxes without page reload
  7852. * @return string
  7853. */
  7854. public function get_js_dropdown_array()
  7855. {
  7856. $course_id = api_get_course_int_id();
  7857. $return = 'var child_name = new Array();' . "\n";
  7858. $return .= 'var child_value = new Array();' . "\n\n";
  7859. $return .= 'child_name[0] = new Array();' . "\n";
  7860. $return .= 'child_value[0] = new Array();' . "\n\n";
  7861. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7862. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7863. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7864. ORDER BY display_order ASC";
  7865. $res_zero = Database::query($sql);
  7866. $i = 0;
  7867. while ($row_zero = Database::fetch_array($res_zero)) {
  7868. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7869. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7870. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7871. }
  7872. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7873. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7874. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7875. }
  7876. }
  7877. $return .= "\n";
  7878. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7879. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7880. $res = Database::query($sql);
  7881. while ($row = Database::fetch_array($res)) {
  7882. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7883. WHERE
  7884. c_id = ".$course_id." AND
  7885. parent_item_id = " . $row['id'] . "
  7886. ORDER BY display_order ASC";
  7887. $res_parent = Database::query($sql_parent);
  7888. $i = 0;
  7889. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7890. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7891. while ($row_parent = Database::fetch_array($res_parent)) {
  7892. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7893. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7894. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7895. }
  7896. $return .= "\n";
  7897. }
  7898. return $return;
  7899. }
  7900. /**
  7901. * Display the form to allow moving an item
  7902. * @param integer Item ID
  7903. * @return string HTML form
  7904. */
  7905. public function display_move_item($item_id)
  7906. {
  7907. $course_id = api_get_course_int_id();
  7908. $return = '';
  7909. if (is_numeric($item_id)) {
  7910. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7911. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7912. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7913. $res = Database::query($sql);
  7914. $row = Database::fetch_array($res);
  7915. switch ($row['item_type']) {
  7916. case 'dir':
  7917. case 'asset':
  7918. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7919. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7920. break;
  7921. case TOOL_DOCUMENT:
  7922. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7923. $return .= $this->display_document_form('move', $item_id, $row);
  7924. break;
  7925. case TOOL_LINK:
  7926. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7927. $return .= $this->display_link_form('move', $item_id, $row);
  7928. break;
  7929. case TOOL_HOTPOTATOES:
  7930. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7931. $return .= $this->display_link_form('move', $item_id, $row);
  7932. break;
  7933. case TOOL_QUIZ:
  7934. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7935. $return .= $this->display_quiz_form('move', $item_id, $row);
  7936. break;
  7937. case TOOL_STUDENTPUBLICATION:
  7938. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7939. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7940. break;
  7941. case TOOL_FORUM:
  7942. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7943. $return .= $this->display_forum_form('move', $item_id, $row);
  7944. break;
  7945. case TOOL_THREAD:
  7946. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7947. $return .= $this->display_forum_form('move', $item_id, $row);
  7948. break;
  7949. }
  7950. }
  7951. return $return;
  7952. }
  7953. /**
  7954. * Displays a basic form on the overview page for changing the item title and the item description.
  7955. * @param string $item_type
  7956. * @param string $title
  7957. * @param array $data
  7958. * @return string
  7959. */
  7960. public function display_item_small_form($item_type, $title = '', $data = array())
  7961. {
  7962. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7963. $form = new FormValidator('small_form', 'post', $url);
  7964. $form->addElement('header', $title);
  7965. $form->addElement('text', 'title', get_lang('Title'));
  7966. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7967. $form->addElement('hidden', 'id', $data['id']);
  7968. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7969. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7970. $form->setDefaults(array('title' => $data['title']));
  7971. return $form->toHtml();
  7972. }
  7973. /**
  7974. * Return HTML form to allow prerequisites selection
  7975. * @todo use FormValidator
  7976. * @param integer Item ID
  7977. * @return string HTML form
  7978. */
  7979. public function display_item_prerequisites_form($item_id)
  7980. {
  7981. $course_id = api_get_course_int_id();
  7982. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7983. $item_id = intval($item_id);
  7984. /* Current prerequisite */
  7985. $sql = "SELECT * FROM $tbl_lp_item
  7986. WHERE c_id = $course_id AND id = " . $item_id;
  7987. $result = Database::query($sql);
  7988. $row = Database::fetch_array($result);
  7989. $prerequisiteId = $row['prerequisite'];
  7990. $return = '<legend>';
  7991. $return .= get_lang('AddEditPrerequisites');
  7992. $return .= '</legend>';
  7993. $return .= '<form method="POST">';
  7994. $return .= '<table class="data_table">';
  7995. $return .= '<tr>';
  7996. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7997. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7998. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7999. $return .= '</tr>';
  8000. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8001. $return .= '<tr >';
  8002. $return .= '<td colspan="3" class="radio">';
  8003. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  8004. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  8005. $return .= '</tr>';
  8006. $sql = "SELECT * FROM $tbl_lp_item
  8007. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  8008. $result = Database::query($sql);
  8009. $arrLP = array();
  8010. $selectedMinScore = array();
  8011. $selectedMaxScore = array();
  8012. while ($row = Database::fetch_array($result)) {
  8013. if ($row['id'] == $item_id) {
  8014. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8015. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8016. }
  8017. $arrLP[] = array(
  8018. 'id' => $row['id'],
  8019. 'item_type' => $row['item_type'],
  8020. 'title' => $row['title'],
  8021. 'ref' => $row['ref'],
  8022. 'description' => $row['description'],
  8023. 'parent_item_id' => $row['parent_item_id'],
  8024. 'previous_item_id' => $row['previous_item_id'],
  8025. 'next_item_id' => $row['next_item_id'],
  8026. 'max_score' => $row['max_score'],
  8027. 'min_score' => $row['min_score'],
  8028. 'mastery_score' => $row['mastery_score'],
  8029. 'prerequisite' => $row['prerequisite'],
  8030. 'next_item_id' => $row['next_item_id'],
  8031. 'display_order' => $row['display_order'],
  8032. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  8033. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  8034. );
  8035. }
  8036. $this->tree_array($arrLP);
  8037. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  8038. unset($this->arrMenu);
  8039. for ($i = 0; $i < count($arrLP); $i++) {
  8040. $item = $arrLP[$i];
  8041. if ($item['id'] == $item_id) {
  8042. break;
  8043. }
  8044. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8045. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  8046. $return .= '<tr>';
  8047. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  8048. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . ($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  8049. $return .= '<label for="id' . $item['id'] . '">';
  8050. $icon_name = str_replace(' ', '', $item['item_type']);
  8051. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  8052. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  8053. } else {
  8054. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  8055. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  8056. } else {
  8057. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  8058. }
  8059. }
  8060. $return .= $item['title'] . '</label>';
  8061. $return .= '</td>';
  8062. if ($item['item_type'] == TOOL_QUIZ) {
  8063. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8064. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  8065. $tmp_obj_exercice = new Exercise();
  8066. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  8067. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  8068. $tmp_obj_lp_item->update_in_bdd();
  8069. $item['max_score'] = $tmp_obj_lp_item->max_score;
  8070. $return .= '<td class="exercise">';
  8071. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  8072. $return .= '</td>';
  8073. $return .= '<td class="exercise">';
  8074. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  8075. $return .= '</td>';
  8076. }
  8077. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8078. $return .= '<td class="exercise">';
  8079. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  8080. $return .= '</td>';
  8081. $return .= '<td class="exercise"">';
  8082. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  8083. $return .= '</td>';
  8084. }
  8085. $return .= '</tr>';
  8086. }
  8087. $return .= '<tr>';
  8088. $return .= '</tr>';
  8089. $return .= '</table>';
  8090. $return .= '<div style="padding-top:3px;">';
  8091. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  8092. $return .= '</form>';
  8093. return $return;
  8094. }
  8095. /**
  8096. * Return HTML list to allow prerequisites selection for lp
  8097. * @param integer Item ID
  8098. * @return string HTML form
  8099. */
  8100. public function display_lp_prerequisites_list()
  8101. {
  8102. $course_id = api_get_course_int_id();
  8103. $lp_id = $this->lp_id;
  8104. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8105. // get current prerequisite
  8106. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8107. $result = Database::query($sql);
  8108. $row = Database::fetch_array($result);
  8109. $prerequisiteId = $row['prerequisite'];
  8110. $session_id = api_get_session_id();
  8111. $session_condition = api_get_session_condition($session_id, true, true);
  8112. $sql = "SELECT * FROM $tbl_lp
  8113. WHERE c_id = $course_id $session_condition
  8114. ORDER BY display_order ";
  8115. $rs = Database::query($sql);
  8116. $return = '';
  8117. $return .= '<select name="prerequisites" class="form-control">';
  8118. $return .= '<option value="0">'.get_lang('None').'</option>';
  8119. if (Database::num_rows($rs) > 0) {
  8120. while ($row = Database::fetch_array($rs)) {
  8121. if ($row['id'] == $lp_id) {
  8122. continue;
  8123. }
  8124. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  8125. }
  8126. }
  8127. $return .= '</select>';
  8128. return $return;
  8129. }
  8130. /**
  8131. * Creates a list with all the documents in it
  8132. * @param bool $showInvisibleFiles
  8133. * @return string
  8134. */
  8135. public function get_documents($showInvisibleFiles = false)
  8136. {
  8137. $course_info = api_get_course_info();
  8138. $sessionId = api_get_session_id();
  8139. $documentTree = DocumentManager::get_document_preview(
  8140. $course_info,
  8141. $this->lp_id,
  8142. null,
  8143. $sessionId,
  8144. true,
  8145. null,
  8146. null,
  8147. $showInvisibleFiles,
  8148. true
  8149. );
  8150. $headers = array(
  8151. get_lang('Files'),
  8152. get_lang('CreateTheDocument'),
  8153. get_lang('Upload')
  8154. );
  8155. $form = new FormValidator(
  8156. 'form_upload',
  8157. 'POST',
  8158. $this->getCurrentBuildingModeURL(),
  8159. '',
  8160. array('enctype' => 'multipart/form-data'),
  8161. FormValidator::LAYOUT_INLINE
  8162. );
  8163. $folders = DocumentManager::get_all_document_folders(
  8164. api_get_course_info(),
  8165. 0,
  8166. true
  8167. );
  8168. DocumentManager::build_directory_selector(
  8169. $folders,
  8170. '',
  8171. array(),
  8172. true,
  8173. $form,
  8174. 'directory_parent_id'
  8175. );
  8176. $form->addHtml('<hr>');
  8177. $group = array(
  8178. $form->createElement(
  8179. 'radio',
  8180. 'if_exists',
  8181. get_lang("UplWhatIfFileExists"),
  8182. get_lang('Yes'),
  8183. 'nothing'
  8184. ),
  8185. $form->createElement(
  8186. 'radio',
  8187. 'if_exists',
  8188. null,
  8189. get_lang('UplOverwriteLong'),
  8190. 'overwrite'
  8191. ),
  8192. $form->createElement(
  8193. 'radio',
  8194. 'if_exists',
  8195. null,
  8196. get_lang('UplRenameLong'),
  8197. 'rename'
  8198. )
  8199. );
  8200. $form->addGroup($group, null, get_lang('UplWhatIfFileExists'));
  8201. $form->addHtml('<br />');
  8202. /*$form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8203. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8204. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');*/
  8205. $form->setDefaults(['if_exists' => 'rename']);
  8206. // Check box options
  8207. $form->addElement(
  8208. 'checkbox',
  8209. 'unzip',
  8210. get_lang('Options'),
  8211. get_lang('Uncompress')
  8212. );
  8213. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8214. $form->addMultipleUpload($url);
  8215. $new = $this->display_document_form('add', 0);
  8216. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8217. return $tabs;
  8218. }
  8219. /**
  8220. * Creates a list with all the exercises (quiz) in it
  8221. * @return string
  8222. */
  8223. public function get_exercises()
  8224. {
  8225. $course_id = api_get_course_int_id();
  8226. $session_id = api_get_session_id();
  8227. $userInfo = api_get_user_info();
  8228. // New for hotpotatoes.
  8229. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8230. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  8231. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  8232. $condition_session = api_get_session_condition($session_id, true, true);
  8233. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8234. $activeCondition = ' active <> -1 ';
  8235. if ($setting) {
  8236. $activeCondition = ' active = 1 ';
  8237. }
  8238. $sql_quiz = "SELECT * FROM $tbl_quiz
  8239. WHERE c_id = $course_id AND $activeCondition $condition_session
  8240. ORDER BY title ASC";
  8241. $sql_hot = "SELECT * FROM $tbl_doc
  8242. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8243. ORDER BY id ASC";
  8244. $res_quiz = Database::query($sql_quiz);
  8245. $res_hot = Database::query($sql_hot);
  8246. $return = '<ul class="lp_resource">';
  8247. $return .= '<li class="lp_resource_element">';
  8248. $return .= Display::return_icon('new_exercice.png');
  8249. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8250. get_lang('NewExercise') . '</a>';
  8251. $return .= '</li>';
  8252. $previewIcon = Display::return_icon('preview_view.png', get_lang('Preview'));
  8253. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  8254. // Display hotpotatoes
  8255. while ($row_hot = Database::fetch_array($res_hot)) {
  8256. $link = Display::url(
  8257. $previewIcon,
  8258. $exerciseUrl.'&file='.$row_hot['path'],
  8259. ['target' => '_blank']
  8260. );
  8261. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8262. $return .= '<a class="moved" href="#">';
  8263. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8264. $return .= '</a> ';
  8265. $return .= Display::return_icon('hotpotatoes_s.png');
  8266. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8267. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])) .$link. '</a>';
  8268. $return .= '</li>';
  8269. }
  8270. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  8271. while ($row_quiz = Database::fetch_array($res_quiz)) {
  8272. $link = Display::url(
  8273. $previewIcon,
  8274. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  8275. ['target' => '_blank']
  8276. );
  8277. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8278. $return .= '<a class="moved" href="#">';
  8279. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8280. $return .= '</a> ';
  8281. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8282. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  8283. $return .= '<a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8284. Security::remove_XSS(cut($row_quiz['title'], 80)).$link.$sessionStar.
  8285. '</a>';
  8286. $return .= '</li>';
  8287. }
  8288. $return .= '</ul>';
  8289. return $return;
  8290. }
  8291. /**
  8292. * Creates a list with all the links in it
  8293. * @return string
  8294. */
  8295. public function get_links()
  8296. {
  8297. $selfUrl = api_get_self();
  8298. $courseIdReq = api_get_cidreq();
  8299. $course = api_get_course_info();
  8300. $userInfo = api_get_user_info();
  8301. $course_id = $course['real_id'];
  8302. $tbl_link = Database::get_course_table(TABLE_LINK);
  8303. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8304. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8305. $session_id = api_get_session_id();
  8306. $condition_session = api_get_session_condition(
  8307. $session_id,
  8308. true,
  8309. true,
  8310. "link.session_id"
  8311. );
  8312. $sql = "SELECT
  8313. link.id as link_id,
  8314. link.title as link_title,
  8315. link.session_id as link_session_id,
  8316. link.category_id as category_id,
  8317. link_category.category_title as category_title
  8318. FROM $tbl_link as link
  8319. LEFT JOIN $linkCategoryTable as link_category
  8320. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8321. WHERE link.c_id = ".$course_id." $condition_session
  8322. ORDER BY link_category.category_title ASC, link.title ASC";
  8323. $result = Database::query($sql);
  8324. $categorizedLinks = array();
  8325. $categories = array();
  8326. while ($link = Database::fetch_array($result)) {
  8327. if (!$link['category_id']) {
  8328. $link['category_title'] = get_lang('Uncategorized');
  8329. }
  8330. $categories[$link['category_id']] = $link['category_title'];
  8331. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  8332. }
  8333. $linksHtmlCode =
  8334. '<script>
  8335. function toggle_tool(tool, id){
  8336. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8337. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8338. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8339. } else {
  8340. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8341. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8342. }
  8343. }
  8344. </script>
  8345. <ul class="lp_resource">
  8346. <li class="lp_resource_element">
  8347. '.Display::return_icon('linksnew.gif').'
  8348. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8349. get_lang('LinkAdd').'
  8350. </a>
  8351. </li>';
  8352. foreach ($categorizedLinks as $categoryId => $links) {
  8353. $linkNodes = null;
  8354. foreach ($links as $key => $linkInfo) {
  8355. $title = $linkInfo['link_title'];
  8356. $linkSessionId = $linkInfo['link_session_id'];
  8357. $link = Display::url(
  8358. Display::return_icon('preview_view.png', get_lang('Preview')),
  8359. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8360. ['target' => '_blank']
  8361. );
  8362. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8363. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8364. $linkNodes .=
  8365. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8366. <a class="moved" href="#">'.
  8367. $moveEverywhereIcon.
  8368. '</a>
  8369. '.Display::return_icon('lp_link.png').'
  8370. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8371. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8372. Security::remove_XSS($title).$sessionStar.$link.
  8373. '</a>
  8374. </li>';
  8375. }
  8376. }
  8377. $linksHtmlCode .=
  8378. '<li>
  8379. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8380. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8381. align="absbottom" />
  8382. </a>
  8383. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8384. </li>
  8385. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8386. }
  8387. $linksHtmlCode .= '</ul>';
  8388. return $linksHtmlCode;
  8389. }
  8390. /**
  8391. * Creates a list with all the student publications in it
  8392. * @return string
  8393. */
  8394. public function get_student_publications()
  8395. {
  8396. $return = '<ul class="lp_resource">';
  8397. $return .= '<li class="lp_resource_element">';
  8398. $return .= Display::return_icon('works_new.gif');
  8399. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8400. get_lang('AddAssignmentPage') . '</a>';
  8401. $return .= '</li>';
  8402. $sessionId = api_get_session_id();
  8403. if (empty($sessionId)) {
  8404. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8405. $works = getWorkListTeacher(0, 100, null, null, null);
  8406. if (!empty($works)) {
  8407. foreach ($works as $work) {
  8408. $link = Display::url(
  8409. Display::return_icon('preview_view.png', get_lang('Preview')),
  8410. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8411. ['target' => '_blank']
  8412. );
  8413. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8414. $return .= '<a class="moved" href="#">';
  8415. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8416. $return .= '</a> ';
  8417. $return .= Display::return_icon('works.gif');
  8418. $return .= ' <a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&file=' . $work['iid'] . '&lp_id=' . $this->lp_id . '">' .
  8419. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8420. </a>';
  8421. $return .= '</li>';
  8422. }
  8423. }
  8424. }
  8425. $return .= '</ul>';
  8426. return $return;
  8427. }
  8428. /**
  8429. * Creates a list with all the forums in it
  8430. * @return string
  8431. */
  8432. public function get_forums()
  8433. {
  8434. require_once '../forum/forumfunction.inc.php';
  8435. require_once '../forum/forumconfig.inc.php';
  8436. $forumCategories = get_forum_categories();
  8437. $forumsInNoCategory = get_forums_in_category(0);
  8438. if (!empty($forumsInNoCategory)) {
  8439. $forumCategories = array_merge(
  8440. $forumCategories,
  8441. array(
  8442. array(
  8443. 'cat_id' => 0,
  8444. 'session_id' => 0,
  8445. 'visibility' => 1,
  8446. 'cat_comment' => null,
  8447. ),
  8448. )
  8449. );
  8450. }
  8451. $forumList = get_forums();
  8452. $a_forums = [];
  8453. foreach ($forumCategories as $forumCategory) {
  8454. // The forums in this category.
  8455. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8456. if (!empty($forumsInCategory)) {
  8457. foreach ($forumList as $forum) {
  8458. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8459. $a_forums[] = $forum;
  8460. }
  8461. }
  8462. }
  8463. }
  8464. $return = '<ul class="lp_resource">';
  8465. // First add link
  8466. $return .= '<li class="lp_resource_element">';
  8467. $return .= Display::return_icon('new_forum.png');
  8468. $return .= Display::url(
  8469. get_lang('CreateANewForum'),
  8470. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8471. 'action' => 'add',
  8472. 'content' => 'forum',
  8473. 'lp_id' => $this->lp_id
  8474. ]),
  8475. ['title' => get_lang('CreateANewForum')]
  8476. );
  8477. $return .= '</li>';
  8478. $return .= '<script>
  8479. function toggle_forum(forum_id) {
  8480. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8481. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8482. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8483. } else {
  8484. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8485. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8486. }
  8487. }
  8488. </script>';
  8489. foreach ($a_forums as $forum) {
  8490. if (!empty($forum['forum_id'])) {
  8491. $link = Display::url(
  8492. Display::return_icon('preview_view.png', get_lang('Preview')),
  8493. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8494. ['target' => '_blank']
  8495. );
  8496. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8497. $return .= '<a class="moved" href="#">';
  8498. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8499. $return .= ' </a>';
  8500. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8501. $return .= '<a onclick="javascript:toggle_forum(' . $forum['forum_id'] . ');" style="cursor:hand; vertical-align:middle">
  8502. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8503. </a>
  8504. <a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  8505. Security::remove_XSS($forum['forum_title']) . ' '.$link.'</a>';
  8506. $return .= '</li>';
  8507. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8508. $a_threads = get_threads($forum['forum_id']);
  8509. if (is_array($a_threads)) {
  8510. foreach ($a_threads as $thread) {
  8511. $link = Display::url(
  8512. Display::return_icon('preview_view.png', get_lang('Preview')),
  8513. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8514. ['target' => '_blank']
  8515. );
  8516. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8517. $return .= '&nbsp;<a class="moved" href="#">';
  8518. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8519. $return .= ' </a>';
  8520. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8521. $return .= '<a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  8522. Security::remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8523. $return .= '</li>';
  8524. }
  8525. }
  8526. $return .= '</div>';
  8527. }
  8528. }
  8529. $return .= '</ul>';
  8530. return $return;
  8531. }
  8532. /**
  8533. * // TODO: The output encoding should be equal to the system encoding.
  8534. *
  8535. * Exports the learning path as a SCORM package. This is the main function that
  8536. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8537. * whole thing and returns the zip.
  8538. *
  8539. * This method needs to be called in PHP5, as it will fail with non-adequate
  8540. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8541. * you need to call it on a learnpath object.
  8542. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8543. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8544. * path has been modified, it should use the generic method here below.
  8545. * @TODO link this function with the export_lp() function in the same class
  8546. * @param string Optional name of zip file. If none, title of learnpath is
  8547. * domesticated and trailed with ".zip"
  8548. * @return string Returns the zip package string, or null if error
  8549. */
  8550. public function scorm_export()
  8551. {
  8552. $_course = api_get_course_info();
  8553. $course_id = $_course['real_id'];
  8554. // Remove memory and time limits as much as possible as this might be a long process...
  8555. if (function_exists('ini_set')) {
  8556. api_set_memory_limit('256M');
  8557. ini_set('max_execution_time', 600);
  8558. }
  8559. // Create the zip handler (this will remain available throughout the method).
  8560. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8561. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8562. $temp_dir_short = uniqid();
  8563. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8564. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8565. $zip_folder = new PclZip($temp_zip_file);
  8566. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8567. $root_path = $main_path = api_get_path(SYS_PATH);
  8568. $files_cleanup = array();
  8569. // Place to temporarily stash the zip file.
  8570. // create the temp dir if it doesn't exist
  8571. // or do a cleanup before creating the zip file.
  8572. if (!is_dir($temp_zip_dir)) {
  8573. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8574. } else {
  8575. // Cleanup: Check the temp dir for old files and delete them.
  8576. $handle = opendir($temp_zip_dir);
  8577. while (false !== ($file = readdir($handle))) {
  8578. if ($file != '.' && $file != '..') {
  8579. unlink("$temp_zip_dir/$file");
  8580. }
  8581. }
  8582. closedir($handle);
  8583. }
  8584. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8585. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8586. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8587. ) {
  8588. // Remove the possible . at the end of the path.
  8589. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8590. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8591. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8592. copyr(
  8593. $current_course_path.'/scorm/'.$this->path,
  8594. $dest_path_to_scorm_folder,
  8595. array('imsmanifest'),
  8596. $zip_files
  8597. );
  8598. }
  8599. // Build a dummy imsmanifest structure.
  8600. // Do not add to the zip yet (we still need it).
  8601. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8602. // Aggregation Model official document, section "2.3 Content Packaging".
  8603. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8604. $xmldoc = new DOMDocument('1.0');
  8605. $root = $xmldoc->createElement('manifest');
  8606. $root->setAttribute('identifier', 'SingleCourseManifest');
  8607. $root->setAttribute('version', '1.1');
  8608. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8609. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8610. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8611. $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');
  8612. // Build mandatory sub-root container elements.
  8613. $metadata = $xmldoc->createElement('metadata');
  8614. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8615. $metadata->appendChild($md_schema);
  8616. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8617. $metadata->appendChild($md_schemaversion);
  8618. $root->appendChild($metadata);
  8619. $organizations = $xmldoc->createElement('organizations');
  8620. $resources = $xmldoc->createElement('resources');
  8621. // Build the only organization we will use in building our learnpaths.
  8622. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8623. $organization = $xmldoc->createElement('organization');
  8624. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8625. // To set the title of the SCORM entity (=organization), we take the name given
  8626. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8627. // learning path charset) as it is the encoding that defines how it is stored
  8628. // in the database. Then we convert it to HTML entities again as the "&" character
  8629. // alone is not authorized in XML (must be &amp;).
  8630. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8631. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8632. $organization->appendChild($org_title);
  8633. $folder_name = 'document';
  8634. // Removes the learning_path/scorm_folder path when exporting see #4841
  8635. $path_to_remove = null;
  8636. $result = $this->generate_lp_folder($_course);
  8637. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8638. $path_to_remove = 'document'.$result['dir'];
  8639. $path_to_replace = $folder_name.'/';
  8640. }
  8641. // Fixes chamilo scorm exports
  8642. if ($this->ref === 'chamilo_scorm_export') {
  8643. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8644. }
  8645. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8646. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8647. $link_updates = array();
  8648. $links_to_create = array();
  8649. //foreach ($this->items as $index => $item) {
  8650. foreach ($this->ordered_items as $index => $itemId) {
  8651. $item = $this->items[$itemId];
  8652. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8653. // Get included documents from this item.
  8654. if ($item->type === 'sco') {
  8655. $inc_docs = $item->get_resources_from_source(
  8656. null,
  8657. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8658. );
  8659. } else {
  8660. $inc_docs = $item->get_resources_from_source();
  8661. }
  8662. // Give a child element <item> to the <organization> element.
  8663. $my_item_id = $item->get_id();
  8664. $my_item = $xmldoc->createElement('item');
  8665. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8666. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8667. $my_item->setAttribute('isvisible', 'true');
  8668. // Give a child element <title> to the <item> element.
  8669. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8670. $my_item->appendChild($my_title);
  8671. // Give a child element <adlcp:prerequisites> to the <item> element.
  8672. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8673. $my_prereqs->setAttribute('type', 'aicc_script');
  8674. $my_item->appendChild($my_prereqs);
  8675. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8676. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8677. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8678. //$xmldoc->createElement('adlcp:timelimitaction','');
  8679. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8680. //$xmldoc->createElement('adlcp:datafromlms','');
  8681. // Give a child element <adlcp:masteryscore> to the <item> element.
  8682. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8683. $my_item->appendChild($my_masteryscore);
  8684. // Attach this item to the organization element or hits parent if there is one.
  8685. if (!empty($item->parent) && $item->parent != 0) {
  8686. $children = $organization->childNodes;
  8687. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8688. if (is_object($possible_parent)) {
  8689. $possible_parent->appendChild($my_item);
  8690. } else {
  8691. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8692. }
  8693. } else {
  8694. if ($this->debug > 0) { error_log('No parent'); }
  8695. $organization->appendChild($my_item);
  8696. }
  8697. // Get the path of the file(s) from the course directory root.
  8698. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8699. if (!empty($path_to_remove)) {
  8700. //From docs
  8701. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8702. //From quiz
  8703. if ($this->ref === 'chamilo_scorm_export') {
  8704. $path_to_remove = 'scorm/'.$this->path.'/';
  8705. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8706. }
  8707. } else {
  8708. $my_xml_file_path = $my_file_path;
  8709. }
  8710. $my_sub_dir = dirname($my_file_path);
  8711. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8712. $my_xml_sub_dir = $my_sub_dir;
  8713. // Give a <resource> child to the <resources> element
  8714. $my_resource = $xmldoc->createElement('resource');
  8715. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8716. $my_resource->setAttribute('type', 'webcontent');
  8717. $my_resource->setAttribute('href', $my_xml_file_path);
  8718. // adlcp:scormtype can be either 'sco' or 'asset'.
  8719. if ($item->type === 'sco') {
  8720. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8721. } else {
  8722. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8723. }
  8724. // xml:base is the base directory to find the files declared in this resource.
  8725. $my_resource->setAttribute('xml:base', '');
  8726. // Give a <file> child to the <resource> element.
  8727. $my_file = $xmldoc->createElement('file');
  8728. $my_file->setAttribute('href', $my_xml_file_path);
  8729. $my_resource->appendChild($my_file);
  8730. // Dependency to other files - not yet supported.
  8731. $i = 1;
  8732. if ($inc_docs)
  8733. foreach ($inc_docs as $doc_info) {
  8734. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8735. continue;
  8736. }
  8737. $my_dep = $xmldoc->createElement('resource');
  8738. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8739. $my_dep->setAttribute('identifier', $res_id);
  8740. $my_dep->setAttribute('type', 'webcontent');
  8741. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8742. $my_dep_file = $xmldoc->createElement('file');
  8743. // Check type of URL.
  8744. if ($doc_info[1] == 'remote') {
  8745. // Remote file. Save url as is.
  8746. $my_dep_file->setAttribute('href', $doc_info[0]);
  8747. $my_dep->setAttribute('xml:base', '');
  8748. } elseif ($doc_info[1] === 'local') {
  8749. switch ($doc_info[2]) {
  8750. case 'url': // Local URL - save path as url for now, don't zip file.
  8751. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8752. $current_dir = dirname($abs_path);
  8753. $current_dir = str_replace('\\', '/', $current_dir);
  8754. $file_path = realpath($abs_path);
  8755. $file_path = str_replace('\\', '/', $file_path);
  8756. $my_dep_file->setAttribute('href', $file_path);
  8757. $my_dep->setAttribute('xml:base', '');
  8758. if (strstr($file_path, $main_path) !== false) {
  8759. // The calculated real path is really inside Chamilo's root path.
  8760. // Reduce file path to what's under the DocumentRoot.
  8761. $file_path = substr($file_path, strlen($root_path) - 1);
  8762. //echo $file_path;echo '<br /><br />';
  8763. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8764. $zip_files_abs[] = $file_path;
  8765. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8766. $my_dep_file->setAttribute('href', $file_path);
  8767. $my_dep->setAttribute('xml:base', '');
  8768. } elseif (empty($file_path)) {
  8769. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8770. if (strpos($document_root, -1) == '/') {
  8771. $document_root = substr(0, -1, $document_root);
  8772. }*/
  8773. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8774. $file_path = str_replace('//', '/', $file_path);
  8775. if (file_exists($file_path)) {
  8776. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8777. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8778. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8779. $my_dep_file->setAttribute('href', $file_path);
  8780. $my_dep->setAttribute('xml:base', '');
  8781. }
  8782. }
  8783. break;
  8784. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8785. $my_dep_file->setAttribute('href', $doc_info[0]);
  8786. $my_dep->setAttribute('xml:base', '');
  8787. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8788. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8789. $abs_img_path_without_subdir = $doc_info[0];
  8790. $relp = api_get_path(REL_PATH); // The url-append config param.
  8791. $pos = strpos($abs_img_path_without_subdir, $relp);
  8792. if ($pos === 0) {
  8793. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8794. }
  8795. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8796. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8797. $file_path = str_replace('\\', '/', $file_path);
  8798. $file_path = str_replace('//', '/', $file_path);
  8799. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8800. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8801. // Check if the current document is in that path.
  8802. if (strstr($file_path, $cur_path) !== false) {
  8803. // The document is in that path, now get the relative path
  8804. // to the containing document.
  8805. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8806. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8807. $relative_path = '';
  8808. if (strstr($file_path, $cur_path) !== false) {
  8809. //$relative_path = substr($file_path, strlen($orig_file_path));
  8810. $relative_path = str_replace($cur_path, '', $file_path);
  8811. $file_path = substr($file_path, strlen($cur_path));
  8812. } else {
  8813. // This case is still a problem as it's difficult to calculate a relative path easily
  8814. // might still generate wrong links.
  8815. //$file_path = substr($file_path,strlen($cur_path));
  8816. // Calculate the directory path to the current file (without trailing slash).
  8817. $my_relative_path = dirname($file_path);
  8818. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8819. $my_relative_file = basename($file_path);
  8820. // Calculate the directory path to the containing file (without trailing slash).
  8821. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8822. $dotdot = '';
  8823. $subdir = '';
  8824. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8825. $my_relative_path2 = dirname($my_relative_path);
  8826. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8827. $my_orig_file_path = dirname($my_orig_file_path);
  8828. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8829. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8830. $dotdot += '../';
  8831. $my_relative_path = $my_relative_path2;
  8832. }
  8833. $relative_path = $dotdot.$subdir.$my_relative_file;
  8834. }
  8835. // Put the current document in the zip (this array is the array
  8836. // that will manage documents already in the course folder - relative).
  8837. $zip_files[] = $file_path;
  8838. // Update the links to the current document in the containing document (make them relative).
  8839. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8840. $my_dep_file->setAttribute('href', $file_path);
  8841. $my_dep->setAttribute('xml:base', '');
  8842. } elseif (strstr($file_path, $main_path) !== false) {
  8843. // The calculated real path is really inside Chamilo's root path.
  8844. // Reduce file path to what's under the DocumentRoot.
  8845. $file_path = substr($file_path, strlen($root_path));
  8846. //echo $file_path;echo '<br /><br />';
  8847. //error_log('Reduced path: '.$file_path, 0);
  8848. $zip_files_abs[] = $file_path;
  8849. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8850. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8851. $my_dep->setAttribute('xml:base', '');
  8852. } elseif (empty($file_path)) {
  8853. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8854. if(strpos($document_root,-1) == '/') {
  8855. $document_root = substr(0, -1, $document_root);
  8856. }*/
  8857. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8858. $file_path = str_replace('//', '/', $file_path);
  8859. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8860. $current_dir = dirname($abs_path);
  8861. $current_dir = str_replace('\\', '/', $current_dir);
  8862. if (file_exists($file_path)) {
  8863. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8864. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8865. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8866. $my_dep_file->setAttribute('href','document/'.$file_path);
  8867. $my_dep->setAttribute('xml:base', '');
  8868. }
  8869. }
  8870. break;
  8871. case 'rel':
  8872. // Path relative to the current document.
  8873. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8874. if (substr($doc_info[0], 0, 2) == '..') {
  8875. // Relative path going up.
  8876. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8877. $current_dir = str_replace('\\', '/', $current_dir);
  8878. $file_path = realpath($current_dir.$doc_info[0]);
  8879. $file_path = str_replace('\\', '/', $file_path);
  8880. //error_log($file_path.' <-> '.$main_path,0);
  8881. if (strstr($file_path, $main_path) !== false) {
  8882. // The calculated real path is really inside Chamilo's root path.
  8883. // Reduce file path to what's under the DocumentRoot.
  8884. $file_path = substr($file_path, strlen($root_path));
  8885. //error_log('Reduced path: '.$file_path, 0);
  8886. $zip_files_abs[] = $file_path;
  8887. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8888. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8889. $my_dep->setAttribute('xml:base', '');
  8890. }
  8891. } else {
  8892. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8893. $my_dep_file->setAttribute('href', $doc_info[0]);
  8894. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8895. }
  8896. break;
  8897. default:
  8898. $my_dep_file->setAttribute('href', $doc_info[0]);
  8899. $my_dep->setAttribute('xml:base', '');
  8900. break;
  8901. }
  8902. }
  8903. $my_dep->appendChild($my_dep_file);
  8904. $resources->appendChild($my_dep);
  8905. $dependency = $xmldoc->createElement('dependency');
  8906. $dependency->setAttribute('identifierref', $res_id);
  8907. $my_resource->appendChild($dependency);
  8908. $i++;
  8909. }
  8910. $resources->appendChild($my_resource);
  8911. $zip_files[] = $my_file_path;
  8912. } else {
  8913. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8914. switch ($item->type) {
  8915. case TOOL_LINK:
  8916. $my_item = $xmldoc->createElement('item');
  8917. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8918. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8919. $my_item->setAttribute('isvisible', 'true');
  8920. // Give a child element <title> to the <item> element.
  8921. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8922. $my_item->appendChild($my_title);
  8923. // Give a child element <adlcp:prerequisites> to the <item> element.
  8924. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8925. $my_prereqs->setAttribute('type', 'aicc_script');
  8926. $my_item->appendChild($my_prereqs);
  8927. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8928. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8929. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8930. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8931. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8932. //$xmldoc->createElement('adlcp:datafromlms', '');
  8933. // Give a child element <adlcp:masteryscore> to the <item> element.
  8934. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8935. $my_item->appendChild($my_masteryscore);
  8936. // Attach this item to the organization element or its parent if there is one.
  8937. if (!empty($item->parent) && $item->parent != 0) {
  8938. $children = $organization->childNodes;
  8939. for ($i = 0; $i < $children->length; $i++) {
  8940. $item_temp = $children->item($i);
  8941. if ($item_temp -> nodeName == 'item') {
  8942. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8943. $item_temp -> appendChild($my_item);
  8944. }
  8945. }
  8946. }
  8947. } else {
  8948. $organization->appendChild($my_item);
  8949. }
  8950. $my_file_path = 'link_'.$item->get_id().'.html';
  8951. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  8952. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8953. $rs = Database::query($sql);
  8954. if ($link = Database::fetch_array($rs)) {
  8955. $url = $link['url'];
  8956. $title = stripslashes($link['title']);
  8957. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8958. $my_xml_file_path = $my_file_path;
  8959. $my_sub_dir = dirname($my_file_path);
  8960. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8961. $my_xml_sub_dir = $my_sub_dir;
  8962. // Give a <resource> child to the <resources> element.
  8963. $my_resource = $xmldoc->createElement('resource');
  8964. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8965. $my_resource->setAttribute('type', 'webcontent');
  8966. $my_resource->setAttribute('href', $my_xml_file_path);
  8967. // adlcp:scormtype can be either 'sco' or 'asset'.
  8968. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8969. // xml:base is the base directory to find the files declared in this resource.
  8970. $my_resource->setAttribute('xml:base', '');
  8971. // give a <file> child to the <resource> element.
  8972. $my_file = $xmldoc->createElement('file');
  8973. $my_file->setAttribute('href', $my_xml_file_path);
  8974. $my_resource->appendChild($my_file);
  8975. $resources->appendChild($my_resource);
  8976. }
  8977. break;
  8978. case TOOL_QUIZ:
  8979. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8980. $exe = new Exercise();
  8981. $exe->read($exe_id);
  8982. $my_item = $xmldoc->createElement('item');
  8983. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8984. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8985. $my_item->setAttribute('isvisible', 'true');
  8986. // Give a child element <title> to the <item> element.
  8987. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8988. $my_item->appendChild($my_title);
  8989. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8990. //$my_item->appendChild($my_max_score);
  8991. // Give a child element <adlcp:prerequisites> to the <item> element.
  8992. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8993. $my_prereqs->setAttribute('type','aicc_script');
  8994. $my_item->appendChild($my_prereqs);
  8995. // Give a child element <adlcp:masteryscore> to the <item> element.
  8996. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8997. $my_item->appendChild($my_masteryscore);
  8998. // Attach this item to the organization element or hits parent if there is one.
  8999. if (!empty($item->parent) && $item->parent != 0) {
  9000. $children = $organization->childNodes;
  9001. /*for ($i = 0; $i < $children->length; $i++) {
  9002. $item_temp = $children->item($i);
  9003. if ($exe_id == 81) {
  9004. error_log($item_temp->nodeName );
  9005. error_log($item_temp->getAttribute('identifier'));
  9006. }
  9007. if ($item_temp->nodeName == 'item') {
  9008. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9009. $item_temp->appendChild($my_item);
  9010. }
  9011. }
  9012. }*/
  9013. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9014. if ($possible_parent) {
  9015. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9016. $possible_parent->appendChild($my_item);
  9017. }
  9018. }
  9019. } else {
  9020. $organization->appendChild($my_item);
  9021. }
  9022. // Get the path of the file(s) from the course directory root
  9023. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9024. $my_file_path = 'quiz_'.$item->get_id().'.html';
  9025. // Write the contents of the exported exercise into a (big) html file
  9026. // to later pack it into the exported SCORM. The file will be removed afterwards.
  9027. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  9028. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  9029. $res = file_put_contents($tmp_file_path, $contents);
  9030. if ($res === false) {
  9031. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  9032. }
  9033. $files_cleanup[] = $tmp_file_path;
  9034. $my_xml_file_path = $my_file_path;
  9035. $my_sub_dir = dirname($my_file_path);
  9036. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9037. $my_xml_sub_dir = $my_sub_dir;
  9038. // Give a <resource> child to the <resources> element.
  9039. $my_resource = $xmldoc->createElement('resource');
  9040. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9041. $my_resource->setAttribute('type', 'webcontent');
  9042. $my_resource->setAttribute('href', $my_xml_file_path);
  9043. // adlcp:scormtype can be either 'sco' or 'asset'.
  9044. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9045. // xml:base is the base directory to find the files declared in this resource.
  9046. $my_resource->setAttribute('xml:base', '');
  9047. // Give a <file> child to the <resource> element.
  9048. $my_file = $xmldoc->createElement('file');
  9049. $my_file->setAttribute('href', $my_xml_file_path);
  9050. $my_resource->appendChild($my_file);
  9051. // Get included docs.
  9052. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  9053. // Dependency to other files - not yet supported.
  9054. $i = 1;
  9055. foreach ($inc_docs as $doc_info) {
  9056. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  9057. $my_dep = $xmldoc->createElement('resource');
  9058. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9059. $my_dep->setAttribute('identifier', $res_id);
  9060. $my_dep->setAttribute('type', 'webcontent');
  9061. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9062. $my_dep_file = $xmldoc->createElement('file');
  9063. // Check type of URL.
  9064. if ($doc_info[1] == 'remote') {
  9065. // Remote file. Save url as is.
  9066. $my_dep_file->setAttribute('href', $doc_info[0]);
  9067. $my_dep->setAttribute('xml:base', '');
  9068. } elseif ($doc_info[1] == 'local') {
  9069. switch ($doc_info[2]) {
  9070. case 'url': // Local URL - save path as url for now, don't zip file.
  9071. // Save file but as local file (retrieve from URL).
  9072. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9073. $current_dir = dirname($abs_path);
  9074. $current_dir = str_replace('\\', '/', $current_dir);
  9075. $file_path = realpath($abs_path);
  9076. $file_path = str_replace('\\', '/', $file_path);
  9077. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9078. $my_dep->setAttribute('xml:base', '');
  9079. if (strstr($file_path, $main_path) !== false) {
  9080. // The calculated real path is really inside the chamilo root path.
  9081. // Reduce file path to what's under the DocumentRoot.
  9082. $file_path = substr($file_path, strlen($root_path));
  9083. $zip_files_abs[] = $file_path;
  9084. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9085. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9086. $my_dep->setAttribute('xml:base', '');
  9087. } elseif (empty($file_path)) {
  9088. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  9089. if (strpos($document_root,-1) == '/') {
  9090. $document_root = substr(0, -1, $document_root);
  9091. }*/
  9092. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9093. $file_path = str_replace('//', '/', $file_path);
  9094. if (file_exists($file_path)) {
  9095. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9096. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9097. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9098. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9099. $my_dep->setAttribute('xml:base', '');
  9100. }
  9101. }
  9102. break;
  9103. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9104. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9105. $current_dir = str_replace('\\', '/', $current_dir);
  9106. $file_path = realpath($doc_info[0]);
  9107. $file_path = str_replace('\\', '/', $file_path);
  9108. $my_dep_file->setAttribute('href', $file_path);
  9109. $my_dep->setAttribute('xml:base', '');
  9110. if (strstr($file_path,$main_path) !== false) {
  9111. // The calculated real path is really inside the chamilo root path.
  9112. // Reduce file path to what's under the DocumentRoot.
  9113. $file_path = substr($file_path, strlen($root_path));
  9114. //echo $file_path;echo '<br /><br />';
  9115. //error_log('Reduced path: '.$file_path, 0);
  9116. $zip_files_abs[] = $file_path;
  9117. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9118. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9119. $my_dep->setAttribute('xml:base', '');
  9120. } elseif (empty($file_path)) {
  9121. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9122. if (strpos($document_root,-1) == '/') {
  9123. $document_root = substr(0, -1, $document_root);
  9124. }*/
  9125. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9126. $file_path = str_replace('//', '/', $file_path);
  9127. if (file_exists($file_path)) {
  9128. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  9129. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9130. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9131. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9132. $my_dep->setAttribute('xml:base', '');
  9133. }
  9134. }
  9135. break;
  9136. 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
  9137. if (substr($doc_info[0], 0, 2) == '..') {
  9138. // Relative path going up.
  9139. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9140. $current_dir = str_replace('\\', '/', $current_dir);
  9141. $file_path = realpath($current_dir.$doc_info[0]);
  9142. $file_path = str_replace('\\', '/', $file_path);
  9143. //error_log($file_path.' <-> '.$main_path, 0);
  9144. if (strstr($file_path, $main_path) !== false) {
  9145. // The calculated real path is really inside Chamilo's root path.
  9146. // Reduce file path to what's under the DocumentRoot.
  9147. $file_path = substr($file_path, strlen($root_path));
  9148. $file_path_dest = $file_path;
  9149. // File path is courses/CHAMILO/document/....
  9150. $info_file_path = explode('/', $file_path);
  9151. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9152. $file_path_dest = 'document/'.$file_path;
  9153. }
  9154. //error_log('Reduced path: '.$file_path, 0);
  9155. $zip_files_abs[] = $file_path;
  9156. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  9157. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9158. $my_dep->setAttribute('xml:base', '');
  9159. }
  9160. } else {
  9161. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9162. $my_dep_file->setAttribute('href', $doc_info[0]);
  9163. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9164. }
  9165. break;
  9166. default:
  9167. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9168. $my_dep->setAttribute('xml:base', '');
  9169. break;
  9170. }
  9171. }
  9172. $my_dep->appendChild($my_dep_file);
  9173. $resources->appendChild($my_dep);
  9174. $dependency = $xmldoc->createElement('dependency');
  9175. $dependency->setAttribute('identifierref', $res_id);
  9176. $my_resource->appendChild($dependency);
  9177. $i++;
  9178. }
  9179. $resources->appendChild($my_resource);
  9180. $zip_files[] = $my_file_path;
  9181. break;
  9182. default:
  9183. // Get the path of the file(s) from the course directory root
  9184. $my_file_path = 'non_exportable.html';
  9185. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9186. $my_xml_file_path = $my_file_path;
  9187. $my_sub_dir = dirname($my_file_path);
  9188. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9189. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9190. $my_xml_sub_dir = $my_sub_dir;
  9191. // Give a <resource> child to the <resources> element.
  9192. $my_resource = $xmldoc->createElement('resource');
  9193. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9194. $my_resource->setAttribute('type', 'webcontent');
  9195. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9196. // adlcp:scormtype can be either 'sco' or 'asset'.
  9197. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9198. // xml:base is the base directory to find the files declared in this resource.
  9199. $my_resource->setAttribute('xml:base', '');
  9200. // Give a <file> child to the <resource> element.
  9201. $my_file = $xmldoc->createElement('file');
  9202. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9203. $my_resource->appendChild($my_file);
  9204. $resources->appendChild($my_resource);
  9205. break;
  9206. }
  9207. }
  9208. }
  9209. $organizations->appendChild($organization);
  9210. $root->appendChild($organizations);
  9211. $root->appendChild($resources);
  9212. $xmldoc->appendChild($root);
  9213. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  9214. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9215. // then add the file to the zip, then destroy the file (this is done automatically).
  9216. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9217. foreach ($zip_files as $file_path) {
  9218. if (empty($file_path)) {
  9219. continue;
  9220. }
  9221. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  9222. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9223. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9224. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9225. }
  9226. $this->create_path($dest_file);
  9227. @copy($filePath, $dest_file);
  9228. // Check if the file needs a link update.
  9229. if (in_array($file_path, array_keys($link_updates))) {
  9230. $string = file_get_contents($dest_file);
  9231. unlink($dest_file);
  9232. foreach ($link_updates[$file_path] as $old_new) {
  9233. // This is an ugly hack that allows .flv files to be found by the flv player that
  9234. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9235. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9236. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9237. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9238. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9239. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9240. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9241. }
  9242. //Fix to avoid problems with default_course_document
  9243. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9244. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9245. $newDestination = $old_new['dest'];
  9246. } else {
  9247. $newDestination = str_replace('document/', '', $old_new['dest']);
  9248. }
  9249. $string = str_replace($old_new['orig'], $newDestination, $string);
  9250. // Add files inside the HTMLs
  9251. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9252. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9253. if (file_exists($sys_course_path.$new_path)) {
  9254. copy($sys_course_path.$new_path, $destinationFile);
  9255. }
  9256. }
  9257. file_put_contents($dest_file, $string);
  9258. }
  9259. if (file_exists($filePath) && $copyAll) {
  9260. $extension = $this->get_extension($filePath);
  9261. if (in_array($extension, ['html', 'html'])) {
  9262. $containerOrigin = dirname($filePath);
  9263. $containerDestination = dirname($dest_file);
  9264. $finder = new Finder();
  9265. $finder->files()->in($containerOrigin)
  9266. ->notName('*_DELETED_*')
  9267. ->exclude('share_folder')
  9268. ->exclude('chat_files')
  9269. ->exclude('certificates')
  9270. ;
  9271. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9272. $fs = new Filesystem();
  9273. $fs->mirror(
  9274. $containerOrigin,
  9275. $containerDestination,
  9276. $finder
  9277. );
  9278. }
  9279. }
  9280. }
  9281. }
  9282. foreach ($zip_files_abs as $file_path) {
  9283. if (empty($file_path)) {
  9284. continue;
  9285. }
  9286. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9287. continue;
  9288. }
  9289. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9290. $this->create_path($dest_file);
  9291. copy($main_path.$file_path, $dest_file);
  9292. // Check if the file needs a link update.
  9293. if (in_array($file_path, array_keys($link_updates))) {
  9294. $string = file_get_contents($dest_file);
  9295. unlink($dest_file);
  9296. foreach ($link_updates[$file_path] as $old_new) {
  9297. // This is an ugly hack that allows .flv files to be found by the flv player that
  9298. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9299. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9300. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9301. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9302. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9303. }
  9304. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9305. }
  9306. file_put_contents($dest_file, $string);
  9307. }
  9308. }
  9309. if (is_array($links_to_create)) {
  9310. foreach ($links_to_create as $file => $link) {
  9311. $file_content = '<!DOCTYPE html><head>
  9312. <meta charset="'.api_get_language_isocode().'" />
  9313. <title>'.$link['title'].'</title>
  9314. </head>
  9315. <body dir="'.api_get_text_direction().'">
  9316. <div style="text-align:center">
  9317. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9318. </body>
  9319. </html>';
  9320. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9321. }
  9322. }
  9323. // Add non exportable message explanation.
  9324. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9325. $file_content = '<!DOCTYPE html><head>
  9326. <meta charset="'.api_get_language_isocode().'" />
  9327. <title>'.$lang_not_exportable.'</title>
  9328. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9329. </head>
  9330. <body dir="'.api_get_text_direction().'">';
  9331. $file_content .=
  9332. <<<EOD
  9333. <style>
  9334. .error-message {
  9335. font-family: arial, verdana, helvetica, sans-serif;
  9336. border-width: 1px;
  9337. border-style: solid;
  9338. left: 50%;
  9339. margin: 10px auto;
  9340. min-height: 30px;
  9341. padding: 5px;
  9342. right: 50%;
  9343. width: 500px;
  9344. background-color: #FFD1D1;
  9345. border-color: #FF0000;
  9346. color: #000;
  9347. }
  9348. </style>
  9349. <body>
  9350. <div class="error-message">
  9351. $lang_not_exportable
  9352. </div>
  9353. </body>
  9354. </html>
  9355. EOD;
  9356. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9357. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9358. }
  9359. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9360. // Add the extra files that go along with a SCORM package.
  9361. $main_code_path = api_get_path(SYS_CODE_PATH) . 'lp/packaging/';
  9362. $fs = new Filesystem();
  9363. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9364. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9365. $manifest = @$xmldoc->saveXML();
  9366. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9367. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9368. $zip_folder->add(
  9369. $archive_path.'/'.$temp_dir_short,
  9370. PCLZIP_OPT_REMOVE_PATH,
  9371. $archive_path.'/'.$temp_dir_short.'/'
  9372. );
  9373. // Clean possible temporary files.
  9374. foreach ($files_cleanup as $file) {
  9375. $res = unlink($file);
  9376. if ($res === false) {
  9377. error_log(
  9378. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9379. 0
  9380. );
  9381. }
  9382. }
  9383. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9384. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9385. }
  9386. /**
  9387. * @param int $lp_id
  9388. * @return bool
  9389. */
  9390. public function scorm_export_to_pdf($lp_id)
  9391. {
  9392. $lp_id = intval($lp_id);
  9393. $files_to_export = array();
  9394. $course_data = api_get_course_info($this->cc);
  9395. if (!empty($course_data)) {
  9396. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9397. $list = self::get_flat_ordered_items_list($lp_id);
  9398. if (!empty($list)) {
  9399. foreach ($list as $item_id) {
  9400. $item = $this->items[$item_id];
  9401. switch ($item->type) {
  9402. case 'document':
  9403. //Getting documents from a LP with chamilo documents
  9404. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9405. // Try loading document from the base course.
  9406. if (empty($file_data) && !empty($sessionId)) {
  9407. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9408. }
  9409. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9410. if (file_exists($file_path)) {
  9411. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9412. }
  9413. break;
  9414. case 'asset': //commes from a scorm package generated by chamilo
  9415. case 'sco':
  9416. $file_path = $scorm_path.'/'.$item->path;
  9417. if (file_exists($file_path)) {
  9418. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9419. }
  9420. break;
  9421. case 'dir':
  9422. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9423. break;
  9424. }
  9425. }
  9426. }
  9427. $pdf = new PDF();
  9428. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9429. return $result;
  9430. }
  9431. return false;
  9432. }
  9433. /**
  9434. * Temp function to be moved in main_api or the best place around for this.
  9435. * Creates a file path if it doesn't exist
  9436. * @param string $path
  9437. */
  9438. public function create_path($path)
  9439. {
  9440. $path_bits = explode('/', dirname($path));
  9441. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9442. $path_built = IS_WINDOWS_OS ? '' : '/';
  9443. foreach ($path_bits as $bit) {
  9444. if (!empty ($bit)) {
  9445. $new_path = $path_built . $bit;
  9446. if (is_dir($new_path)) {
  9447. $path_built = $new_path . '/';
  9448. } else {
  9449. mkdir($new_path, api_get_permissions_for_new_directories());
  9450. $path_built = $new_path . '/';
  9451. }
  9452. }
  9453. }
  9454. }
  9455. /**
  9456. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9457. * @return boolean The results of the unlink function, or false if there was no image to start with
  9458. */
  9459. public function delete_lp_image()
  9460. {
  9461. $img = $this->get_preview_image();
  9462. if ($img != '') {
  9463. $del_file = $this->get_preview_image_path(null, 'sys');
  9464. if (isset($del_file) && file_exists($del_file)) {
  9465. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9466. if (file_exists($del_file_2)) {
  9467. unlink($del_file_2);
  9468. }
  9469. $this->set_preview_image('');
  9470. return @unlink($del_file);
  9471. }
  9472. }
  9473. return false;
  9474. }
  9475. /**
  9476. * Uploads an author image to the upload/learning_path/images directory
  9477. * @param array The image array, coming from the $_FILES superglobal
  9478. * @return boolean True on success, false on error
  9479. */
  9480. public function upload_image($image_array)
  9481. {
  9482. if (!empty ($image_array['name'])) {
  9483. $upload_ok = process_uploaded_file($image_array);
  9484. $has_attachment = true;
  9485. }
  9486. if ($upload_ok) {
  9487. if ($has_attachment) {
  9488. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9489. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9490. $updir = $sys_course_path . $courseDir;
  9491. // Try to add an extension to the file if it hasn't one.
  9492. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9493. if (!filter_extension($new_file_name)) {
  9494. } else {
  9495. $file_extension = explode('.', $image_array['name']);
  9496. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9497. $filename = uniqid('');
  9498. $new_file_name = $filename.'.'.$file_extension;
  9499. $new_path = $updir.'/'.$new_file_name;
  9500. // Resize the image.
  9501. $temp = new Image($image_array['tmp_name']);
  9502. $temp->resize(104);
  9503. $result = $temp->send_image($new_path);
  9504. // Storing the image filename.
  9505. if ($result) {
  9506. $this->set_preview_image($new_file_name);
  9507. //Resize to 64px to use on course homepage
  9508. $temp->resize(64);
  9509. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9510. return true;
  9511. }
  9512. }
  9513. }
  9514. }
  9515. return false;
  9516. }
  9517. /**
  9518. * @param int $lp_id
  9519. * @param string $status
  9520. */
  9521. public function set_autolaunch($lp_id, $status)
  9522. {
  9523. $course_id = api_get_course_int_id();
  9524. $lp_id = intval($lp_id);
  9525. $status = intval($status);
  9526. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9527. // Setting everything to autolaunch = 0
  9528. $attributes['autolaunch'] = 0;
  9529. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9530. Database::update($lp_table, $attributes, $where);
  9531. if ($status == 1) {
  9532. //Setting my lp_id to autolaunch = 1
  9533. $attributes['autolaunch'] = 1;
  9534. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9535. Database::update($lp_table, $attributes, $where );
  9536. }
  9537. }
  9538. /**
  9539. * Gets previous_item_id for the next element of the lp_item table
  9540. * @author Isaac flores paz
  9541. * @return integer Previous item ID
  9542. */
  9543. public function select_previous_item_id()
  9544. {
  9545. $course_id = api_get_course_int_id();
  9546. if ($this->debug > 0) {
  9547. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9548. }
  9549. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9550. // Get the max order of the items
  9551. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9552. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9553. $rs_max_order = Database::query($sql);
  9554. $row_max_order = Database::fetch_object($rs_max_order);
  9555. $max_order = $row_max_order->display_order;
  9556. // Get the previous item ID
  9557. $sql = "SELECT id as previous FROM $table_lp_item
  9558. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9559. $rs_max = Database::query($sql);
  9560. $row_max = Database::fetch_object($rs_max);
  9561. // Return the previous item ID
  9562. return $row_max->previous;
  9563. }
  9564. /**
  9565. * Copies an LP
  9566. */
  9567. public function copy()
  9568. {
  9569. // Course builder
  9570. $cb = new CourseBuilder();
  9571. //Setting tools that will be copied
  9572. $cb->set_tools_to_build(array('learnpaths'));
  9573. //Setting elements that will be copied
  9574. $cb->set_tools_specific_id_list(
  9575. array('learnpaths' => array($this->lp_id))
  9576. );
  9577. $course = $cb->build();
  9578. //Course restorer
  9579. $course_restorer = new CourseRestorer($course);
  9580. $course_restorer->set_add_text_in_items(true);
  9581. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9582. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9583. }
  9584. public function verify_document_size($s)
  9585. {
  9586. $post_max = ini_get('post_max_size');
  9587. if (substr($post_max, -1, 1) == 'M') {
  9588. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9589. } elseif (substr($post_max, -1, 1) == 'G') {
  9590. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9591. }
  9592. $upl_max = ini_get('upload_max_filesize');
  9593. if (substr($upl_max, -1, 1) == 'M') {
  9594. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9595. } elseif (substr($upl_max, -1, 1) == 'G') {
  9596. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9597. }
  9598. $documents_total_space = DocumentManager::documents_total_space();
  9599. $course_max_space = DocumentManager::get_course_quota();
  9600. $total_size = filesize($s) + $documents_total_space;
  9601. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9602. return true;
  9603. } else{
  9604. return false;
  9605. }
  9606. }
  9607. /**
  9608. * Clear LP prerequisites
  9609. */
  9610. public function clear_prerequisites()
  9611. {
  9612. $course_id = $this->get_course_int_id();
  9613. if ($this->debug > 0) {
  9614. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9615. }
  9616. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9617. $lp_id = $this->get_id();
  9618. //Cleaning prerequisites
  9619. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9620. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9621. Database::query($sql);
  9622. //Cleaning mastery score for exercises
  9623. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9624. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9625. Database::query($sql);
  9626. }
  9627. public function set_previous_step_as_prerequisite_for_all_items()
  9628. {
  9629. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9630. $course_id = $this->get_course_int_id();
  9631. $lp_id = $this->get_id();
  9632. if (!empty($this->items)) {
  9633. $previous_item_id = null;
  9634. $previous_item_max = 0;
  9635. $previous_item_type = null;
  9636. $last_item_not_dir = null;
  9637. $last_item_not_dir_type = null;
  9638. $last_item_not_dir_max = null;
  9639. foreach ($this->items as $item) {
  9640. // if there was a previous item... (otherwise jump to set it)
  9641. if (!empty($previous_item_id)) {
  9642. $current_item_id = $item->get_id(); //save current id
  9643. if ($item->get_type() != 'dir') {
  9644. // Current item is not a folder, so it qualifies to get a prerequisites
  9645. if ($last_item_not_dir_type == 'quiz') {
  9646. // if previous is quiz, mark its max score as default score to be achieved
  9647. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9648. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9649. Database::query($sql);
  9650. }
  9651. // now simply update the prerequisite to set it to the last non-chapter item
  9652. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9653. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9654. Database::query($sql);
  9655. // record item as 'non-chapter' reference
  9656. $last_item_not_dir = $item->get_id();
  9657. $last_item_not_dir_type = $item->get_type();
  9658. $last_item_not_dir_max = $item->get_max();
  9659. }
  9660. } else {
  9661. if ($item->get_type() != 'dir') {
  9662. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9663. $last_item_not_dir = $item->get_id();
  9664. $last_item_not_dir_type = $item->get_type();
  9665. $last_item_not_dir_max = $item->get_max();
  9666. }
  9667. }
  9668. // Saving the item as "previous item" for the next loop
  9669. $previous_item_id = $item->get_id();
  9670. $previous_item_max = $item->get_max();
  9671. $previous_item_type = $item->get_type();
  9672. }
  9673. }
  9674. }
  9675. /**
  9676. * @param array $params
  9677. */
  9678. public static function createCategory($params)
  9679. {
  9680. $em = Database::getManager();
  9681. $item = new CLpCategory();
  9682. $item->setName($params['name']);
  9683. $item->setCId($params['c_id']);
  9684. $em->persist($item);
  9685. $em->flush();
  9686. }
  9687. /**
  9688. * @param array $params
  9689. */
  9690. public static function updateCategory($params)
  9691. {
  9692. $em = Database::getManager();
  9693. /** @var CLpCategory $item */
  9694. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9695. if ($item) {
  9696. $item->setName($params['name']);
  9697. $em->merge($item);
  9698. $em->flush();
  9699. }
  9700. }
  9701. /**
  9702. * @param int $id
  9703. */
  9704. public static function moveUpCategory($id)
  9705. {
  9706. $em = Database::getManager();
  9707. /** @var CLpCategory $item */
  9708. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9709. if ($item) {
  9710. $position = $item->getPosition() - 1;
  9711. $item->setPosition($position);
  9712. $em->persist($item);
  9713. $em->flush();
  9714. }
  9715. }
  9716. /**
  9717. * @param int $id
  9718. */
  9719. public static function moveDownCategory($id)
  9720. {
  9721. $em = Database::getManager();
  9722. /** @var CLpCategory $item */
  9723. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9724. if ($item) {
  9725. $position = $item->getPosition() + 1;
  9726. $item->setPosition($position);
  9727. $em->persist($item);
  9728. $em->flush();
  9729. }
  9730. }
  9731. /**
  9732. * @param int $courseId
  9733. * @return int|mixed
  9734. */
  9735. public static function getCountCategories($courseId)
  9736. {
  9737. if (empty($course_id)) {
  9738. return 0;
  9739. }
  9740. $em = Database::getManager();
  9741. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9742. $query->setParameter('id', $courseId);
  9743. return $query->getSingleScalarResult();
  9744. }
  9745. /**
  9746. * @param int $courseId
  9747. *
  9748. * @return mixed
  9749. */
  9750. public static function getCategories($courseId)
  9751. {
  9752. $em = Database::getManager();
  9753. //Default behaviour
  9754. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9755. array('cId' => $course_id),
  9756. array('name' => 'ASC')
  9757. );*/
  9758. // Using doctrine extensions
  9759. /** @var SortableRepository $repo */
  9760. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9761. $items = $repo
  9762. ->getBySortableGroupsQuery(['cId' => $courseId])
  9763. ->getResult();
  9764. return $items;
  9765. }
  9766. /**
  9767. * @param int $id
  9768. *
  9769. * @return CLpCategory
  9770. */
  9771. public static function getCategory($id)
  9772. {
  9773. $em = Database::getManager();
  9774. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9775. return $item;
  9776. }
  9777. /**
  9778. * @param int $courseId
  9779. * @return array
  9780. */
  9781. public static function getCategoryByCourse($courseId)
  9782. {
  9783. $em = Database::getManager();
  9784. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9785. array('cId' => $courseId)
  9786. );
  9787. return $items;
  9788. }
  9789. /**
  9790. * @param int $id
  9791. *
  9792. * @return mixed
  9793. */
  9794. public static function deleteCategory($id)
  9795. {
  9796. $em = Database::getManager();
  9797. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9798. if ($item) {
  9799. $courseId = $item->getCId();
  9800. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9801. $query->setParameter('id', $courseId);
  9802. $query->setParameter('catId', $item->getId());
  9803. $lps = $query->getResult();
  9804. // Setting category = 0.
  9805. if ($lps) {
  9806. foreach ($lps as $lpItem) {
  9807. $lpItem->setCategoryId(0);
  9808. }
  9809. }
  9810. // Removing category.
  9811. $em->remove($item);
  9812. $em->flush();
  9813. }
  9814. }
  9815. /**
  9816. * @param int $courseId
  9817. * @param bool $addSelectOption
  9818. *
  9819. * @return mixed
  9820. */
  9821. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9822. {
  9823. $items = self::getCategoryByCourse($courseId);
  9824. $cats = array();
  9825. if ($addSelectOption) {
  9826. $cats = array(get_lang('SelectACategory'));
  9827. }
  9828. if (!empty($items)) {
  9829. foreach ($items as $cat) {
  9830. $cats[$cat->getId()] = $cat->getName();
  9831. }
  9832. }
  9833. return $cats;
  9834. }
  9835. /**
  9836. * Return the scorm item type object with spaces replaced with _
  9837. * The return result is use to build a css classname like scorm_type_$return
  9838. * @param $in_type
  9839. * @return mixed
  9840. */
  9841. private static function format_scorm_type_item($in_type)
  9842. {
  9843. return str_replace(' ', '_', $in_type);
  9844. }
  9845. /**
  9846. * @param string $courseCode
  9847. * @param int $lp_id
  9848. * @param int $user_id
  9849. *
  9850. * @return learnpath
  9851. */
  9852. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9853. {
  9854. $learnPath = null;
  9855. $lpObject = Session::read('lpobject');
  9856. if ($lpObject !== null) {
  9857. $learnPath = unserialize($lpObject);
  9858. }
  9859. if (!is_object($learnPath)) {
  9860. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9861. }
  9862. return $learnPath;
  9863. }
  9864. /**
  9865. * @param int $itemId
  9866. * @return learnpathItem|false
  9867. */
  9868. public function getItem($itemId)
  9869. {
  9870. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9871. return $this->items[$itemId];
  9872. }
  9873. return false;
  9874. }
  9875. /**
  9876. * @return int
  9877. */
  9878. public function getCategoryId()
  9879. {
  9880. return $this->categoryId;
  9881. }
  9882. /**
  9883. * @param int $categoryId
  9884. * @return bool
  9885. */
  9886. public function setCategoryId($categoryId)
  9887. {
  9888. $this->categoryId = intval($categoryId);
  9889. $courseId = api_get_course_int_id();
  9890. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9891. $lp_id = $this->get_id();
  9892. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9893. WHERE c_id = $courseId AND id = $lp_id";
  9894. Database::query($sql);
  9895. return true;
  9896. }
  9897. /**
  9898. * Get whether this is a learning path with the possibility to subscribe
  9899. * users or not
  9900. * @return int
  9901. */
  9902. public function getSubscribeUsers()
  9903. {
  9904. return $this->subscribeUsers;
  9905. }
  9906. /**
  9907. * Set whether this is a learning path with the possibility to subscribe
  9908. * users or not
  9909. * @param int $subscribeUsers (0 = false, 1 = true)
  9910. */
  9911. public function setSubscribeUsers($value)
  9912. {
  9913. if ($this->debug > 0) {
  9914. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9915. }
  9916. $this->subscribeUsers = intval($value);;
  9917. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9918. $lp_id = $this->get_id();
  9919. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9920. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9921. Database::query($sql);
  9922. return true;
  9923. }
  9924. /**
  9925. * Calculate the count of stars for a user in this LP
  9926. * This calculation is based on the following rules:
  9927. * - the student gets one star when he gets to 50% of the learning path
  9928. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9929. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9930. * - the student gets the final star when the score for the *last* test is >= 80%
  9931. * @param int $sessionId Optional. The session ID
  9932. * @return int The count of stars
  9933. */
  9934. public function getCalculateStars($sessionId = 0)
  9935. {
  9936. $stars = 0;
  9937. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9938. if ($progress >= 50) {
  9939. $stars++;
  9940. }
  9941. // Calculate stars chapters evaluation
  9942. $exercisesItems = $this->getExercisesItems();
  9943. if (!empty($exercisesItems)) {
  9944. $totalResult = 0;
  9945. foreach ($exercisesItems as $exerciseItem) {
  9946. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9947. $this->user_id,
  9948. $exerciseItem->path,
  9949. $this->course_int_id,
  9950. $sessionId,
  9951. $this->lp_id,
  9952. $exerciseItem->db_id
  9953. );
  9954. $exerciseResultInfo = end($exerciseResultInfo);
  9955. if (!$exerciseResultInfo) {
  9956. continue;
  9957. }
  9958. if (!empty($exerciseResultInfo['exe_weighting'])) {
  9959. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9960. } else {
  9961. $exerciseResult = 0;
  9962. }
  9963. $totalResult += $exerciseResult;
  9964. }
  9965. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9966. if ($totalExerciseAverage >= 50) {
  9967. $stars++;
  9968. }
  9969. if ($totalExerciseAverage >= 80) {
  9970. $stars++;
  9971. }
  9972. }
  9973. // Calculate star for final evaluation
  9974. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9975. if (!empty($finalEvaluationItem)) {
  9976. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9977. $this->user_id,
  9978. $finalEvaluationItem->path,
  9979. $this->course_int_id,
  9980. $sessionId,
  9981. $this->lp_id,
  9982. $finalEvaluationItem->db_id
  9983. );
  9984. $evaluationResultInfo = end($evaluationResultInfo);
  9985. if ($evaluationResultInfo) {
  9986. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9987. if ($evaluationResult >= 80) {
  9988. $stars++;
  9989. }
  9990. }
  9991. }
  9992. return $stars;
  9993. }
  9994. /**
  9995. * Get the items of exercise type
  9996. * @return array The items. Otherwise return false
  9997. */
  9998. public function getExercisesItems()
  9999. {
  10000. $exercises = [];
  10001. foreach ($this->items as $item) {
  10002. if ($item->type != 'quiz') {
  10003. continue;
  10004. }
  10005. $exercises[] = $item;
  10006. }
  10007. array_pop($exercises);
  10008. return $exercises;
  10009. }
  10010. /**
  10011. * Get the item of exercise type (evaluation type)
  10012. * @return array The final evaluation. Otherwise return false
  10013. */
  10014. public function getFinalEvaluationItem()
  10015. {
  10016. $exercises = [];
  10017. foreach ($this->items as $item) {
  10018. if ($item->type != 'quiz') {
  10019. continue;
  10020. }
  10021. $exercises[] = $item;
  10022. }
  10023. return array_pop($exercises);
  10024. }
  10025. /**
  10026. * Calculate the total points achieved for the current user in this learning path
  10027. * @param int $sessionId Optional. The session Id
  10028. * @return int
  10029. */
  10030. public function getCalculateScore($sessionId = 0)
  10031. {
  10032. // Calculate stars chapters evaluation
  10033. $exercisesItems = $this->getExercisesItems();
  10034. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10035. $totalExercisesResult = 0;
  10036. $totalEvaluationResult = 0;
  10037. if ($exercisesItems !== false) {
  10038. foreach ($exercisesItems as $exerciseItem) {
  10039. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10040. $this->user_id,
  10041. $exerciseItem->path,
  10042. $this->course_int_id,
  10043. $sessionId,
  10044. $this->lp_id,
  10045. $exerciseItem->db_id
  10046. );
  10047. $exerciseResultInfo = end($exerciseResultInfo);
  10048. if (!$exerciseResultInfo) {
  10049. continue;
  10050. }
  10051. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  10052. }
  10053. }
  10054. if (!empty($finalEvaluationItem)) {
  10055. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10056. $this->user_id,
  10057. $finalEvaluationItem->path,
  10058. $this->course_int_id,
  10059. $sessionId,
  10060. $this->lp_id,
  10061. $finalEvaluationItem->db_id
  10062. );
  10063. $evaluationResultInfo = end($evaluationResultInfo);
  10064. if ($evaluationResultInfo) {
  10065. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  10066. }
  10067. }
  10068. return $totalExercisesResult + $totalEvaluationResult;
  10069. }
  10070. /**
  10071. * Check if URL is not allowed to be show in a iframe
  10072. * @param string $src
  10073. *
  10074. * @return string
  10075. */
  10076. public function fixBlockedLinks($src)
  10077. {
  10078. $urlInfo = parse_url($src);
  10079. $platformProtocol = 'https';
  10080. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  10081. $platformProtocol = 'http';
  10082. }
  10083. $protocolFixApplied = false;
  10084. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  10085. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  10086. if ($platformProtocol != $scheme) {
  10087. Session::write('x_frame_source', $src);
  10088. $src = 'blank.php?error=x_frames_options';
  10089. $protocolFixApplied = true;
  10090. }
  10091. if ($protocolFixApplied == false) {
  10092. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  10093. // Check X-Frame-Options
  10094. $ch = curl_init();
  10095. $options = array(
  10096. CURLOPT_URL => $src,
  10097. CURLOPT_RETURNTRANSFER => true,
  10098. CURLOPT_HEADER => true,
  10099. CURLOPT_FOLLOWLOCATION => true,
  10100. CURLOPT_ENCODING => "",
  10101. CURLOPT_AUTOREFERER => true,
  10102. CURLOPT_CONNECTTIMEOUT => 120,
  10103. CURLOPT_TIMEOUT => 120,
  10104. CURLOPT_MAXREDIRS => 10,
  10105. );
  10106. curl_setopt_array($ch, $options);
  10107. $response = curl_exec($ch);
  10108. $httpCode = curl_getinfo($ch);
  10109. $headers = substr($response, 0, $httpCode['header_size']);
  10110. $error = false;
  10111. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  10112. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  10113. ) {
  10114. $error = true;
  10115. }
  10116. if ($error) {
  10117. Session::write('x_frame_source', $src);
  10118. $src = 'blank.php?error=x_frames_options';
  10119. }
  10120. }
  10121. }
  10122. return $src;
  10123. }
  10124. /**
  10125. * Check if this LP has a created forum in the basis course
  10126. * @return boolean
  10127. */
  10128. public function lpHasForum()
  10129. {
  10130. $forumTable = Database::get_course_table(TABLE_FORUM);
  10131. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10132. $fakeFrom = "
  10133. $forumTable f
  10134. INNER JOIN $itemProperty ip
  10135. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  10136. ";
  10137. $resultData = Database::select(
  10138. 'COUNT(f.iid) AS qty',
  10139. $fakeFrom,
  10140. [
  10141. 'where' => [
  10142. 'ip.visibility != ? AND ' => 2,
  10143. 'ip.tool = ? AND ' => TOOL_FORUM,
  10144. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10145. 'f.lp_id = ?' => intval($this->lp_id)
  10146. ]
  10147. ],
  10148. 'first'
  10149. );
  10150. return $resultData['qty'] > 0;
  10151. }
  10152. /**
  10153. * Get the forum for this learning path
  10154. * @return boolean
  10155. */
  10156. public function getForum($sessionId = 0)
  10157. {
  10158. $forumTable = Database::get_course_table(TABLE_FORUM);
  10159. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10160. $fakeFrom = "$forumTable f
  10161. INNER JOIN $itemProperty ip ";
  10162. if ($this->lp_session_id == 0) {
  10163. $fakeFrom .= "
  10164. ON (
  10165. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  10166. f.session_id = ip.session_id OR ip.session_id IS NULL
  10167. )
  10168. )
  10169. ";
  10170. } else {
  10171. $fakeFrom .= "
  10172. ON (
  10173. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  10174. )
  10175. ";
  10176. }
  10177. $resultData = Database::select(
  10178. 'f.*',
  10179. $fakeFrom,
  10180. [
  10181. 'where' => [
  10182. 'ip.visibility != ? AND ' => 2,
  10183. 'ip.tool = ? AND ' => TOOL_FORUM,
  10184. 'f.session_id = ? AND ' => $sessionId,
  10185. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10186. 'f.lp_id = ?' => intval($this->lp_id)
  10187. ]
  10188. ],
  10189. 'first'
  10190. );
  10191. if (empty($resultData)) {
  10192. return false;
  10193. }
  10194. return $resultData;
  10195. }
  10196. /**
  10197. * Create a forum for this learning path
  10198. * @param type $forumCategoryId
  10199. * @return int The forum ID if was created. Otherwise return false
  10200. */
  10201. public function createForum($forumCategoryId)
  10202. {
  10203. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  10204. $forumId = store_forum(
  10205. [
  10206. 'lp_id' => $this->lp_id,
  10207. 'forum_title' => $this->name,
  10208. 'forum_comment' => null,
  10209. 'forum_category' => intval($forumCategoryId),
  10210. 'students_can_edit_group' => ['students_can_edit' => 0],
  10211. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  10212. 'default_view_type_group' => ['default_view_type' => 'flat'],
  10213. 'group_forum' => 0,
  10214. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  10215. ],
  10216. [],
  10217. true
  10218. );
  10219. return $forumId;
  10220. }
  10221. /**
  10222. * Check and obtain the lp final item if exist
  10223. *
  10224. * @return learnpathItem
  10225. */
  10226. private function getFinalItem()
  10227. {
  10228. if (empty($this->items)) {
  10229. return null;
  10230. }
  10231. foreach ($this->items as $item) {
  10232. if ($item->type !== 'final_item') {
  10233. continue;
  10234. }
  10235. return $item;
  10236. }
  10237. }
  10238. /**
  10239. * Get the LP Final Item Template
  10240. *
  10241. * @return string
  10242. */
  10243. private function getFinalItemTemplate()
  10244. {
  10245. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'lp/final_item_template/template.html');
  10246. }
  10247. /**
  10248. * Get the LP Final Item Url
  10249. *
  10250. * @return string
  10251. */
  10252. private function getSavedFinalItem()
  10253. {
  10254. $finalItem = $this->getFinalItem();
  10255. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10256. if ($doc && file_exists($doc['absolute_path'])) {
  10257. return file_get_contents($doc['absolute_path']);
  10258. }
  10259. return '';
  10260. }
  10261. /**
  10262. * Get the LP Final Item form
  10263. *
  10264. * @return string
  10265. */
  10266. public function getFinalItemForm()
  10267. {
  10268. $finalItem = $this->getFinalItem();
  10269. $title = '';
  10270. if ($finalItem) {
  10271. $title = $finalItem->get_title();
  10272. $buttonText = get_lang('Save');
  10273. $content = $this->getSavedFinalItem();
  10274. } else {
  10275. $buttonText = get_lang('LPCreateDocument');
  10276. $content = $this->getFinalItemTemplate();
  10277. }
  10278. $courseInfo = api_get_course_info();
  10279. $result = $this->generate_lp_folder($courseInfo);
  10280. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10281. $relative_prefix = '../../';
  10282. $editorConfig = [
  10283. 'ToolbarSet' => 'LearningPathDocuments',
  10284. 'Width' => '100%',
  10285. 'Height' => '500',
  10286. 'FullPage' => true,
  10287. 'CreateDocumentDir' => $relative_prefix,
  10288. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  10289. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  10290. ];
  10291. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  10292. 'type' => 'document',
  10293. 'lp_id' => $this->lp_id
  10294. ]);
  10295. $form = new FormValidator('final_item', 'POST', $url);
  10296. $form->addText('title', get_lang('Title'));
  10297. $form->addButtonSave($buttonText);
  10298. $form->addHtml(
  10299. Display::return_message(
  10300. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10301. 'normal',
  10302. false
  10303. )
  10304. );
  10305. $renderer = $form->defaultRenderer();
  10306. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10307. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10308. $form->addHidden('action', 'add_final_item');
  10309. $form->addHidden('path', Session::read('pathItem'));
  10310. $form->addHidden('previous', $this->get_last());
  10311. $form->setDefaults(
  10312. ['title' => $title, 'content_lp_certificate' => $content]
  10313. );
  10314. if ($form->validate()) {
  10315. $values = $form->exportValues();
  10316. $lastItemId = $this->get_last();
  10317. if (!$finalItem) {
  10318. $documentId = $this->create_document(
  10319. $this->course_info,
  10320. $values['content_lp_certificate'],
  10321. $values['title']
  10322. );
  10323. $this->add_item(
  10324. 0,
  10325. $lastItemId,
  10326. 'final_item',
  10327. $documentId,
  10328. $values['title'],
  10329. ''
  10330. );
  10331. Display::addFlash(
  10332. Display::return_message(get_lang('Added'))
  10333. );
  10334. } else {
  10335. $this->edit_document($this->course_info);
  10336. }
  10337. }
  10338. return $form->returnForm();
  10339. }
  10340. /**
  10341. * Check if the current lp item is first, both, last or none from lp list
  10342. *
  10343. * @param int $currentItemId
  10344. * @return string
  10345. */
  10346. public function isFirstOrLastItem($currentItemId)
  10347. {
  10348. if ($this->debug > 0) {
  10349. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10350. }
  10351. $lpItemId = [];
  10352. $typeListNotToVerify = self::getChapterTypes();
  10353. // Using get_toc() function instead $this->items because returns the correct order of the items
  10354. foreach ($this->get_toc() as $item) {
  10355. if (!in_array($item['type'], $typeListNotToVerify)) {
  10356. $lpItemId[] = $item['id'];
  10357. }
  10358. }
  10359. $lastLpItemIndex = count($lpItemId) - 1;
  10360. $position = array_search($currentItemId, $lpItemId);
  10361. switch ($position) {
  10362. case 0:
  10363. if (!$lastLpItemIndex) {
  10364. $answer = 'both';
  10365. break;
  10366. }
  10367. $answer = 'first';
  10368. break;
  10369. case $lastLpItemIndex:
  10370. $answer = 'last';
  10371. break;
  10372. default:
  10373. $answer = 'none';
  10374. }
  10375. return $answer;
  10376. }
  10377. /**
  10378. * Get whether this is a learning path with the accumulated SCORM time or not
  10379. * @return int
  10380. */
  10381. public function getAccumulateScormTime()
  10382. {
  10383. return $this->accumulateScormTime;
  10384. }
  10385. /**
  10386. * Set whether this is a learning path with the accumulated SCORM time or not
  10387. * @param int $value (0 = false, 1 = true)
  10388. * @return bool Always returns true
  10389. */
  10390. public function setAccumulateScormTime($value)
  10391. {
  10392. if ($this->debug > 0) {
  10393. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10394. }
  10395. $this->accumulateScormTime = intval($value);
  10396. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10397. $lp_id = $this->get_id();
  10398. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10399. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10400. Database::query($sql);
  10401. return true;
  10402. }
  10403. /**
  10404. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10405. * the new learning path tool.
  10406. *
  10407. * The function is a big switch on tool type.
  10408. * In each case, we query the corresponding table for information and build the link
  10409. * with that information.
  10410. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on previous work (display_addedresource_link_in_learnpath())
  10411. * @param int $course_id Course code
  10412. * @param int $learningPathId The learning path ID (in lp table)
  10413. * @param int $id_in_path the unique index in the items table
  10414. * @param int $lpViewId
  10415. * @return string
  10416. */
  10417. public static function rl_get_resource_link_for_learnpath(
  10418. $course_id,
  10419. $learningPathId,
  10420. $id_in_path,
  10421. $lpViewId
  10422. ) {
  10423. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10424. $course_info = api_get_course_info_by_id($course_id);
  10425. $course_id = $course_info['real_id'];
  10426. $course_code = $course_info['code'];
  10427. $session_id = api_get_session_id();
  10428. $learningPathId = intval($learningPathId);
  10429. $id_in_path = intval($id_in_path);
  10430. $lpViewId = intval($lpViewId);
  10431. $em = Database::getManager();
  10432. $sql = "SELECT * FROM $tbl_lp_item
  10433. WHERE
  10434. c_id = $course_id AND
  10435. lp_id = $learningPathId AND
  10436. id = $id_in_path
  10437. ";
  10438. $res_item = Database::query($sql);
  10439. if (Database::num_rows($res_item) < 1) {
  10440. return -1;
  10441. }
  10442. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10443. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10444. // Get the lp_item_view with the highest view_count.
  10445. $sql = "SELECT * FROM $item_view_table
  10446. WHERE
  10447. c_id = $course_id AND
  10448. lp_item_id = " . $row_item['id'] . " AND
  10449. lp_view_id = $lpViewId
  10450. ORDER BY view_count DESC";
  10451. $learnpathItemViewResult = Database::query($sql);
  10452. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10453. $learnpathItemViewId = 0;
  10454. if (isset($learnpathItemViewData)) {
  10455. $learnpathItemViewId = $learnpathItemViewData['id'];
  10456. }
  10457. $type = strtolower($row_item['item_type']);
  10458. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10459. $origin = 'learnpath';
  10460. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10461. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10462. $link = '';
  10463. switch ($type) {
  10464. case 'dir':
  10465. $link .= $main_dir_path . 'lp/blank.php';
  10466. case TOOL_CALENDAR_EVENT:
  10467. $link .= $main_dir_path.'calendar/agenda.php?origin='.$origin.'&agenda_id='.$id;
  10468. break;
  10469. case TOOL_ANNOUNCEMENT:
  10470. $link .= $main_dir_path.'announcements/announcements.php?origin='.$origin.'&ann_id='.$id;
  10471. break;
  10472. case TOOL_LINK:
  10473. $link .= $main_dir_path.'link/link_goto.php?'.api_get_cidreq().'&link_id='.$id;
  10474. break;
  10475. case TOOL_QUIZ:
  10476. if (!empty($id)) {
  10477. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10478. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10479. $result= Database::query($sql);
  10480. $myrow = Database::fetch_array($result);
  10481. if ($row_item['title'] != '') {
  10482. $myrow['title'] = $row_item['title'];
  10483. }
  10484. $link .= $main_dir_path . 'exercise/overview.php?cidReq='.$course_code.'&session_id='.$session_id.'&lp_init=1&origin='.$origin.'&learnpath_item_view_id='.$learnpathItemViewId.'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id;
  10485. }
  10486. break;
  10487. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  10488. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10489. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10490. $myrow = Database::fetch_array($result);
  10491. $path = $myrow['path'];
  10492. $link .= $main_dir_path . 'exercise/showinframes.php?file='.$path.'&origin='.$origin.'&cid='.$course_code.'&uid='.api_get_user_id().'' .
  10493. '&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId;
  10494. break;
  10495. case TOOL_FORUM:
  10496. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&origin=learnpath';
  10497. break;
  10498. case TOOL_THREAD: //forum post
  10499. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10500. if (!empty($id)) {
  10501. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10502. $result = Database::query($sql);
  10503. $myrow = Database::fetch_array($result);
  10504. $link .= $main_dir_path.'forum/viewthread.php?origin=learnpath&thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true';
  10505. }
  10506. break;
  10507. case TOOL_POST:
  10508. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10509. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10510. $myrow = Database::fetch_array($result);
  10511. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'&lp=true';
  10512. break;
  10513. case TOOL_DOCUMENT:
  10514. $document = $em
  10515. ->getRepository('ChamiloCourseBundle:CDocument')
  10516. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10517. if (!$document) {
  10518. break;
  10519. }
  10520. $documentPathInfo = pathinfo($document->getPath());
  10521. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10522. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10523. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10524. if ($showDirectUrl) {
  10525. $link = $main_course_path . 'document' . $document->getPath() . '?' . api_get_cidreq();
  10526. } else {
  10527. $link = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?' . http_build_query([
  10528. 'cidReq' => $course_code,
  10529. 'id' => $id,
  10530. 'origin' => 'learnpathitem'
  10531. ]);
  10532. }
  10533. $openmethod = 2;
  10534. $officedoc = false;
  10535. Session::write('openmethod',$openmethod);
  10536. Session::write('officedoc',$officedoc);
  10537. break;
  10538. case TOOL_LP_FINAL_ITEM:
  10539. $link .= api_get_path(WEB_CODE_PATH) . 'lp/lp_final_item.php?'.api_get_cidreq().'&id='.$id.'&lp_id='.$learningPathId;
  10540. break;
  10541. case 'assignments':
  10542. $link .= $main_dir_path.'work/work.php?origin='.$origin;
  10543. break;
  10544. case TOOL_DROPBOX:
  10545. $link .= $main_dir_path.'dropbox/index.php?origin=learnpath';
  10546. break;
  10547. case 'introduction_text': //DEPRECATED
  10548. $link .= '';
  10549. break;
  10550. case TOOL_COURSE_DESCRIPTION:
  10551. $link .= $main_dir_path.'course_description?origin=learnpath';
  10552. break;
  10553. case TOOL_GROUP:
  10554. $link .= $main_dir_path.'group/group.php?origin='.$origin;
  10555. break;
  10556. case TOOL_USER:
  10557. $link .= $main_dir_path.'user/user.php?origin='.$origin;
  10558. break;
  10559. case TOOL_STUDENTPUBLICATION:
  10560. if (!empty($row_item['path'])) {
  10561. $link .= $main_dir_path.'work/work_list.php?'.api_get_cidreq().'&id='.$row_item['path'].'&origin=learnpath';
  10562. break;
  10563. }
  10564. $link .= $main_dir_path . 'work/work.php?' . api_get_cidreq() . '&' . http_build_query([
  10565. 'id' => $row_item['path'],
  10566. 'origin' => 'learnpath'
  10567. ]);
  10568. break;
  10569. } //end switch
  10570. return $link;
  10571. }
  10572. /**
  10573. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10574. *
  10575. * @author Yannick Warnier <ywarnier@beeznest.org>
  10576. * @param string Course code
  10577. * @param string The tool type (using constants declared in main_api.lib.php)
  10578. * @param integer The resource ID
  10579. */
  10580. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10581. {
  10582. $_course = api_get_course_info($course_code);
  10583. $course_id = $_course['real_id'];
  10584. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10585. $learningPathId = intval($learningPathId);
  10586. $id_in_path = intval($id_in_path);
  10587. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10588. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10589. $res_item = Database::query($sql_item);
  10590. if (Database::num_rows($res_item) < 1) {
  10591. return '';
  10592. }
  10593. $row_item = Database::fetch_array($res_item);
  10594. $type = strtolower($row_item['item_type']);
  10595. $id = $row_item['ref'];
  10596. $output = '';
  10597. switch ($type) {
  10598. case TOOL_CALENDAR_EVENT:
  10599. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10600. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10601. $myrow = Database::fetch_array($result);
  10602. $output = $myrow['title'];
  10603. break;
  10604. case TOOL_ANNOUNCEMENT:
  10605. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10606. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10607. $myrow = Database::fetch_array($result);
  10608. $output = $myrow['title'];
  10609. break;
  10610. case TOOL_LINK:
  10611. // Doesn't take $target into account.
  10612. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10613. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10614. $myrow = Database::fetch_array($result);
  10615. $output = $myrow['title'];
  10616. break;
  10617. case TOOL_QUIZ:
  10618. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10619. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10620. $myrow = Database::fetch_array($result);
  10621. $output = $myrow['title'];
  10622. break;
  10623. case TOOL_FORUM:
  10624. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10625. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10626. $myrow = Database::fetch_array($result);
  10627. $output = $myrow['forum_name'];
  10628. break;
  10629. case TOOL_THREAD: //=topics
  10630. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10631. // Grabbing the title of the post.
  10632. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10633. $result_title = Database::query($sql_title);
  10634. $myrow_title = Database::fetch_array($result_title);
  10635. $output = $myrow_title['post_title'];
  10636. break;
  10637. case TOOL_POST:
  10638. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10639. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10640. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10641. $result = Database::query($sql);
  10642. $post = Database::fetch_array($result);
  10643. $output = $post['post_title'];
  10644. break;
  10645. case 'dir':
  10646. $title = $row_item['title'];
  10647. if (!empty($title)) {
  10648. $output = $title;
  10649. } else {
  10650. $output = '-';
  10651. }
  10652. break;
  10653. case TOOL_DOCUMENT:
  10654. $title = $row_item['title'];
  10655. if (!empty($title)) {
  10656. $output = $title;
  10657. } else {
  10658. $output = '-';
  10659. }
  10660. break;
  10661. case 'hotpotatoes':
  10662. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10663. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10664. $myrow = Database::fetch_array($result);
  10665. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10666. $last = count($pathname) - 1; // Making a correct name for the link.
  10667. $filename = $pathname[$last]; // Making a correct name for the link.
  10668. $ext = explode('.', $filename);
  10669. $ext = strtolower($ext[sizeof($ext) - 1]);
  10670. $myrow['path'] = rawurlencode($myrow['path']);
  10671. $output = $filename;
  10672. break;
  10673. }
  10674. return stripslashes($output);
  10675. }
  10676. }
  10677. if (!function_exists('trim_value')) {
  10678. function trim_value(& $value) {
  10679. $value = trim($value);
  10680. }
  10681. }