learnpath.class.php 479 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784
  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 'chamilo', '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 = self::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 self::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. // TODO: Update the item object (can be ignored for now because refreshed).
  1420. return true;
  1421. }
  1422. /**
  1423. * Static admin function exporting a learnpath into a zip file
  1424. * @param string Export type (scorm, zip, cd)
  1425. * @param string Course code
  1426. * @param integer Learnpath ID
  1427. * @param string Zip file name
  1428. * @return string Zip file path (or false on error)
  1429. */
  1430. public function export_lp($type, $course, $id, $zipname)
  1431. {
  1432. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1433. return false;
  1434. }
  1435. $url = '';
  1436. switch ($type) {
  1437. case 'scorm':
  1438. break;
  1439. case 'zip':
  1440. break;
  1441. case 'cdrom':
  1442. break;
  1443. }
  1444. return $url;
  1445. }
  1446. /**
  1447. * Gets all the chapters belonging to the same parent as the item/chapter given
  1448. * Can also be called as abstract method
  1449. * @param integer $id Item ID
  1450. * @return array A list of all the "brother items" (or an empty array on failure)
  1451. */
  1452. public function getSiblingDirectories($id)
  1453. {
  1454. $course_id = api_get_course_int_id();
  1455. if ($this->debug > 0) {
  1456. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1457. }
  1458. if (empty($id) || $id != strval(intval($id))) {
  1459. return array();
  1460. }
  1461. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1462. $sql_parent = "SELECT * FROM $lp_item
  1463. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1464. $res_parent = Database::query($sql_parent);
  1465. if (Database::num_rows($res_parent) > 0) {
  1466. $row_parent = Database::fetch_array($res_parent);
  1467. $parent = $row_parent['parent_item_id'];
  1468. $sql = "SELECT * FROM $lp_item
  1469. WHERE
  1470. c_id = ".$course_id." AND
  1471. parent_item_id = $parent AND
  1472. id = $id AND
  1473. item_type='dir'
  1474. ORDER BY display_order";
  1475. $res_bros = Database::query($sql);
  1476. $list = array();
  1477. while ($row_bro = Database::fetch_array($res_bros)) {
  1478. $list[] = $row_bro;
  1479. }
  1480. return $list;
  1481. }
  1482. return array();
  1483. }
  1484. /**
  1485. * Gets all the items belonging to the same parent as the item given
  1486. * Can also be called as abstract method
  1487. * @param integer $id Item ID
  1488. * @return array A list of all the "brother items" (or an empty array on failure)
  1489. */
  1490. public function get_brother_items($id)
  1491. {
  1492. $course_id = api_get_course_int_id();
  1493. if ($this->debug > 0) {
  1494. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1495. }
  1496. if (empty ($id) || $id != strval(intval($id))) {
  1497. return array();
  1498. }
  1499. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1500. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1501. $res_parent = Database::query($sql_parent);
  1502. if (Database::num_rows($res_parent) > 0) {
  1503. $row_parent = Database::fetch_array($res_parent);
  1504. $parent = $row_parent['parent_item_id'];
  1505. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1506. ORDER BY display_order";
  1507. $res_bros = Database::query($sql_bros);
  1508. $list = [];
  1509. while ($row_bro = Database::fetch_array($res_bros)) {
  1510. $list[] = $row_bro;
  1511. }
  1512. return $list;
  1513. }
  1514. return [];
  1515. }
  1516. /**
  1517. * Get the specific prefix index terms of this learning path
  1518. * @param string $prefix
  1519. * @return array Array of terms
  1520. */
  1521. public function get_common_index_terms_by_prefix($prefix)
  1522. {
  1523. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1524. $terms = get_specific_field_values_list_by_prefix(
  1525. $prefix,
  1526. $this->cc,
  1527. TOOL_LEARNPATH,
  1528. $this->lp_id
  1529. );
  1530. $prefix_terms = array();
  1531. if (!empty($terms)) {
  1532. foreach ($terms as $term) {
  1533. $prefix_terms[] = $term['value'];
  1534. }
  1535. }
  1536. return $prefix_terms;
  1537. }
  1538. /**
  1539. * Gets the number of items currently completed
  1540. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1541. * @return integer The number of items currently completed
  1542. */
  1543. public function get_complete_items_count($failedStatusException = false)
  1544. {
  1545. if ($this->debug > 0) {
  1546. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1547. }
  1548. $i = 0;
  1549. $completedStatusList = array(
  1550. 'completed',
  1551. 'passed',
  1552. 'succeeded',
  1553. 'browsed'
  1554. );
  1555. if (!$failedStatusException) {
  1556. $completedStatusList[] = 'failed';
  1557. }
  1558. foreach ($this->items as $id => $dummy) {
  1559. // Trying failed and browsed considered "progressed" as well.
  1560. if ($this->items[$id]->status_is($completedStatusList) &&
  1561. $this->items[$id]->get_type() != 'dir'
  1562. ) {
  1563. $i++;
  1564. }
  1565. }
  1566. return $i;
  1567. }
  1568. /**
  1569. * Gets the current item ID
  1570. * @return integer The current learnpath item id
  1571. */
  1572. public function get_current_item_id()
  1573. {
  1574. $current = 0;
  1575. if ($this->debug > 0) {
  1576. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1577. }
  1578. if (!empty($this->current)) {
  1579. $current = $this->current;
  1580. }
  1581. if ($this->debug > 2) {
  1582. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1583. }
  1584. return $current;
  1585. }
  1586. /**
  1587. * Force to get the first learnpath item id
  1588. * @return integer The current learnpath item id
  1589. */
  1590. public function get_first_item_id()
  1591. {
  1592. $current = 0;
  1593. if (is_array($this->ordered_items)) {
  1594. $current = $this->ordered_items[0];
  1595. }
  1596. return $current;
  1597. }
  1598. /**
  1599. * Gets the total number of items available for viewing in this SCORM
  1600. * @return integer The total number of items
  1601. */
  1602. public function get_total_items_count()
  1603. {
  1604. if ($this->debug > 0) {
  1605. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1606. }
  1607. return count($this->items);
  1608. }
  1609. /**
  1610. * Gets the total number of items available for viewing in this SCORM but without chapters
  1611. * @return integer The total no-chapters number of items
  1612. */
  1613. public function getTotalItemsCountWithoutDirs()
  1614. {
  1615. if ($this->debug > 0) {
  1616. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1617. }
  1618. $total = 0;
  1619. $typeListNotToCount = self::getChapterTypes();
  1620. foreach ($this->items as $temp2) {
  1621. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1622. $total++;
  1623. }
  1624. }
  1625. return $total;
  1626. }
  1627. /**
  1628. * Gets the first element URL.
  1629. * @return string URL to load into the viewer
  1630. */
  1631. public function first()
  1632. {
  1633. if ($this->debug > 0) {
  1634. error_log('New LP - In learnpath::first()', 0);
  1635. error_log('$this->last_item_seen '.$this->last_item_seen);
  1636. }
  1637. // Test if the last_item_seen exists and is not a dir.
  1638. if (count($this->ordered_items) == 0) {
  1639. $this->index = 0;
  1640. }
  1641. if ($this->debug > 0) {
  1642. if (isset($this->items[$this->last_item_seen])) {
  1643. $status = $this->items[$this->last_item_seen]->get_status();
  1644. }
  1645. error_log('status '.$status);
  1646. }
  1647. if (!empty($this->last_item_seen) &&
  1648. !empty($this->items[$this->last_item_seen]) &&
  1649. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1650. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1651. //&& !$this->items[$this->last_item_seen]->is_done()
  1652. ) {
  1653. if ($this->debug > 2) {
  1654. error_log('New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1655. }
  1656. $index = -1;
  1657. foreach ($this->ordered_items as $myindex => $item_id) {
  1658. if ($item_id == $this->last_item_seen) {
  1659. $index = $myindex;
  1660. break;
  1661. }
  1662. }
  1663. if ($index == -1) {
  1664. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1665. if ($this->debug > 2) {
  1666. error_log('New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!', 0);
  1667. }
  1668. return false;
  1669. } else {
  1670. $this->last = $this->last_item_seen;
  1671. $this->current = $this->last_item_seen;
  1672. $this->index = $index;
  1673. }
  1674. } else {
  1675. if ($this->debug > 2) {
  1676. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1677. }
  1678. $index = 0;
  1679. // Loop through all ordered items and stop at the first item that is
  1680. // not a directory *and* that has not been completed yet.
  1681. while (!empty($this->ordered_items[$index]) AND
  1682. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1683. (
  1684. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1685. $this->items[$this->ordered_items[$index]]->is_done() === true
  1686. ) AND $index < $this->max_ordered_items) {
  1687. $index++;
  1688. }
  1689. $this->last = $this->current;
  1690. // current is
  1691. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1692. $this->index = $index;
  1693. if ($this->debug > 2) {
  1694. error_log('$index '.$index);
  1695. }
  1696. if ($this->debug > 2) {
  1697. error_log('New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')', 0);
  1698. }
  1699. }
  1700. if ($this->debug > 2) {
  1701. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1702. }
  1703. }
  1704. /**
  1705. * Gets the information about an item in a format usable as JavaScript to update
  1706. * the JS API by just printing this content into the <head> section of the message frame
  1707. * @param int $item_id
  1708. * @return string
  1709. */
  1710. public function get_js_info($item_id = 0)
  1711. {
  1712. if ($this->debug > 0) {
  1713. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1714. }
  1715. $info = '';
  1716. $item_id = intval($item_id);
  1717. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1718. //if item is defined, return values from DB
  1719. $oItem = $this->items[$item_id];
  1720. $info .= '<script language="javascript">';
  1721. $info .= "top.set_score(".$oItem->get_score().");\n";
  1722. $info .= "top.set_max(".$oItem->get_max().");\n";
  1723. $info .= "top.set_min(".$oItem->get_min().");\n";
  1724. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1725. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1726. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1727. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1728. $info .= "top.set_flag_synchronized();";
  1729. $info .= '</script>';
  1730. if ($this->debug > 2) {
  1731. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1732. }
  1733. return $info;
  1734. } else {
  1735. // If item_id is empty, just update to default SCORM data.
  1736. $info .= '<script language="javascript">';
  1737. $info .= "top.set_score(".learnpathItem::get_score().");\n";
  1738. $info .= "top.set_max(".learnpathItem::get_max().");\n";
  1739. $info .= "top.set_min(".learnpathItem::get_min().");\n";
  1740. $info .= "top.set_lesson_status('".learnpathItem::get_status()."');";
  1741. $info .= "top.set_session_time('".learnpathItem::getScormTimeFromParameter('js')."');";
  1742. $info .= "top.set_suspend_data('".learnpathItem::get_suspend_data()."');";
  1743. $info .= "top.set_saved_lesson_status('".learnpathItem::get_status()."');";
  1744. $info .= "top.set_flag_synchronized();";
  1745. $info .= '</script>';
  1746. if ($this->debug > 2) {
  1747. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1748. }
  1749. return $info;
  1750. }
  1751. }
  1752. /**
  1753. * Gets the js library from the database
  1754. * @return string The name of the javascript library to be used
  1755. */
  1756. public function get_js_lib()
  1757. {
  1758. $lib = '';
  1759. if (!empty ($this->js_lib)) {
  1760. $lib = $this->js_lib;
  1761. }
  1762. return $lib;
  1763. }
  1764. /**
  1765. * Gets the learnpath database ID
  1766. * @return integer Learnpath ID in the lp table
  1767. */
  1768. public function get_id()
  1769. {
  1770. if (!empty ($this->lp_id)) {
  1771. return $this->lp_id;
  1772. } else {
  1773. return 0;
  1774. }
  1775. }
  1776. /**
  1777. * Gets the last element URL.
  1778. * @return string URL to load into the viewer
  1779. */
  1780. public function get_last()
  1781. {
  1782. if ($this->debug > 0) {
  1783. error_log('New LP - In learnpath::get_last()', 0);
  1784. }
  1785. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1786. if (count($this->ordered_items) > 0) {
  1787. $this->index = count($this->ordered_items) - 1;
  1788. return $this->ordered_items[$this->index];
  1789. }
  1790. return false;
  1791. }
  1792. /**
  1793. * Gets the navigation bar for the learnpath display screen
  1794. * @return string The HTML string to use as a navigation bar
  1795. */
  1796. public function get_navigation_bar($idBar = null, $display = null)
  1797. {
  1798. if ($this->debug > 0) {
  1799. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1800. }
  1801. if (empty($idBar)) {
  1802. $idBar = 'control-top';
  1803. }
  1804. $navbar = null;
  1805. $lp_id = $this->lp_id;
  1806. $mycurrentitemid = $this->get_current_item_id();
  1807. $reportingText = get_lang('Reporting');
  1808. $previousText = get_lang('ScormPrevious');
  1809. $nextText = get_lang('ScormNext');
  1810. $fullScreenText = get_lang('ScormExitFullScreen');
  1811. if ($this->mode == 'fullscreen') {
  1812. $navbar = '
  1813. <span id="'.$idBar.'" class="buttons">
  1814. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1815. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText.'</span>
  1816. </a>
  1817. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid.',\'previous\');return false;" title="'.$previousText.'">
  1818. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText.'</span>
  1819. </a>
  1820. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid.',\'next\');return false;" title="'.$nextText.'">
  1821. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText.'</span>
  1822. </a>
  1823. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1824. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText.'</span>
  1825. </a>
  1826. </span>';
  1827. } else {
  1828. $navbar = '
  1829. <span id="'.$idBar.'" class="buttons text-right">
  1830. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="'.$reportingText.'" id="stats_link">
  1831. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText.'</span>
  1832. </a>
  1833. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid.',\'previous\');return false;" title="'.$previousText.'">
  1834. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText.'</span>
  1835. </a>
  1836. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid.',\'next\');return false;" title="'.$nextText.'">
  1837. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText.'</span>
  1838. </a>
  1839. </span>';
  1840. }
  1841. return $navbar;
  1842. }
  1843. /**
  1844. * Gets the next resource in queue (url).
  1845. * @return string URL to load into the viewer
  1846. */
  1847. public function get_next_index()
  1848. {
  1849. if ($this->debug > 0) {
  1850. error_log('New LP - In learnpath::get_next_index()', 0);
  1851. }
  1852. // TODO
  1853. $index = $this->index;
  1854. $index++;
  1855. if ($this->debug > 2) {
  1856. error_log('New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type, 0);
  1857. }
  1858. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1859. $index++;
  1860. if ($index == $this->max_ordered_items) {
  1861. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1862. return $this->index;
  1863. } else {
  1864. return $index;
  1865. }
  1866. }
  1867. }
  1868. if (empty ($this->ordered_items[$index])) {
  1869. return $this->index;
  1870. }
  1871. if ($this->debug > 2) {
  1872. error_log('New LP - index is now '.$index, 0);
  1873. }
  1874. return $index;
  1875. }
  1876. /**
  1877. * Gets item_id for the next element
  1878. * @return integer Next item (DB) ID
  1879. */
  1880. public function get_next_item_id()
  1881. {
  1882. if ($this->debug > 0) {
  1883. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1884. }
  1885. $new_index = $this->get_next_index();
  1886. if (!empty ($new_index)) {
  1887. if (isset ($this->ordered_items[$new_index])) {
  1888. if ($this->debug > 2) {
  1889. error_log('New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index], 0);
  1890. }
  1891. return $this->ordered_items[$new_index];
  1892. }
  1893. }
  1894. if ($this->debug > 2) {
  1895. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1896. }
  1897. return 0;
  1898. }
  1899. /**
  1900. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1901. *
  1902. * Generally, the package provided is in the form of a zip file, so the function
  1903. * has been written to test a zip file. If not a zip, the function will return the
  1904. * default return value: ''
  1905. * @param string the path to the file
  1906. * @param string the original name of the file
  1907. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1908. */
  1909. public static function get_package_type($file_path, $file_name)
  1910. {
  1911. // Get name of the zip file without the extension.
  1912. $file_info = pathinfo($file_name);
  1913. $filename = $file_info['basename']; // Name including extension.
  1914. $extension = $file_info['extension']; // Extension only.
  1915. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1916. 'dll',
  1917. 'exe'
  1918. ))) {
  1919. return 'oogie';
  1920. }
  1921. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1922. 'dll',
  1923. 'exe'
  1924. ))) {
  1925. return 'woogie';
  1926. }
  1927. $zipFile = new PclZip($file_path);
  1928. // Check the zip content (real size and file extension).
  1929. $zipContentArray = $zipFile->listContent();
  1930. $package_type = '';
  1931. $at_root = false;
  1932. $manifest = '';
  1933. $aicc_match_crs = 0;
  1934. $aicc_match_au = 0;
  1935. $aicc_match_des = 0;
  1936. $aicc_match_cst = 0;
  1937. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1938. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1939. foreach ($zipContentArray as $thisContent) {
  1940. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1941. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1942. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1943. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1944. $package_type = 'scorm';
  1945. break; // Exit the foreach loop.
  1946. } elseif (
  1947. preg_match('/aicc\//i', $thisContent['filename']) ||
  1948. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array('crs', 'au', 'des', 'cst'))
  1949. ) {
  1950. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1951. switch ($ext) {
  1952. case 'crs':
  1953. $aicc_match_crs = 1;
  1954. break;
  1955. case 'au':
  1956. $aicc_match_au = 1;
  1957. break;
  1958. case 'des':
  1959. $aicc_match_des = 1;
  1960. break;
  1961. case 'cst':
  1962. $aicc_match_cst = 1;
  1963. break;
  1964. default:
  1965. break;
  1966. }
  1967. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1968. } else {
  1969. $package_type = '';
  1970. }
  1971. }
  1972. }
  1973. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1974. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1975. $package_type = 'aicc';
  1976. }
  1977. return $package_type;
  1978. }
  1979. /**
  1980. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1981. * @return string URL to load into the viewer
  1982. */
  1983. public function get_previous_index()
  1984. {
  1985. if ($this->debug > 0) {
  1986. error_log('New LP - In learnpath::get_previous_index()', 0);
  1987. }
  1988. $index = $this->index;
  1989. if (isset ($this->ordered_items[$index - 1])) {
  1990. $index--;
  1991. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1992. $index--;
  1993. if ($index < 0) {
  1994. return $this->index;
  1995. }
  1996. }
  1997. } else {
  1998. if ($this->debug > 2) {
  1999. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  2000. }
  2001. // There is no previous item.
  2002. }
  2003. return $index;
  2004. }
  2005. /**
  2006. * Gets item_id for the next element
  2007. * @return integer Previous item (DB) ID
  2008. */
  2009. public function get_previous_item_id()
  2010. {
  2011. if ($this->debug > 0) {
  2012. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2013. }
  2014. $new_index = $this->get_previous_index();
  2015. return $this->ordered_items[$new_index];
  2016. }
  2017. /**
  2018. * Gets the progress value from the progress_db attribute
  2019. * @return integer Current progress value
  2020. * @deprecated This method does not seem to be used as of 20170514
  2021. */
  2022. public function get_progress()
  2023. {
  2024. if ($this->debug > 0) {
  2025. error_log('New LP - In learnpath::get_progress()', 0);
  2026. }
  2027. if (!empty ($this->progress_db)) {
  2028. return $this->progress_db;
  2029. }
  2030. return 0;
  2031. }
  2032. /**
  2033. * Returns the HTML necessary to print a mediaplayer block inside a page
  2034. * @return string The mediaplayer HTML
  2035. */
  2036. public function get_mediaplayer($lpItemId, $autostart = 'true')
  2037. {
  2038. $course_id = api_get_course_int_id();
  2039. $_course = api_get_course_info();
  2040. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2041. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2042. // Getting all the information about the item.
  2043. $sql = "SELECT * FROM $tbl_lp_item as lp
  2044. INNER JOIN $tbl_lp_item_view as lp_view
  2045. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2046. WHERE
  2047. lp.id = '$lpItemId' AND
  2048. lp.c_id = $course_id AND
  2049. lp_view.c_id = $course_id";
  2050. $result = Database::query($sql);
  2051. $row = Database::fetch_assoc($result);
  2052. $output = '';
  2053. if (!empty ($row['audio'])) {
  2054. $list = $_SESSION['oLP']->get_toc();
  2055. $type_quiz = false;
  2056. foreach ($list as $toc) {
  2057. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2058. $type_quiz = true;
  2059. }
  2060. }
  2061. if ($type_quiz) {
  2062. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2063. $autostart_audio = $row['status'] === 'completed' ? 'false' : 'true';
  2064. } else {
  2065. $autostart_audio = $autostart;
  2066. }
  2067. } else {
  2068. $autostart_audio = 'true';
  2069. }
  2070. $courseInfo = api_get_course_info();
  2071. $audio = $row['audio'];
  2072. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2073. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2074. if (!file_exists($file)) {
  2075. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2076. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2077. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2078. }
  2079. $player = Display::getMediaPlayer(
  2080. $file,
  2081. array(
  2082. 'id' => 'lp_audio_media_player',
  2083. 'url' => $url,
  2084. 'autoplay' => $autostart_audio,
  2085. 'width' => '100%'
  2086. )
  2087. );
  2088. // The mp3 player.
  2089. $output = '<div id="container">';
  2090. $output .= $player;
  2091. $output .= '</div>';
  2092. }
  2093. return $output;
  2094. }
  2095. /**
  2096. * @param int $studentId
  2097. * @param int $prerequisite
  2098. * @param array $courseInfo
  2099. * @param int $sessionId
  2100. *
  2101. * @return bool
  2102. *
  2103. */
  2104. public static function isBlockedByPrerequisite(
  2105. $studentId,
  2106. $prerequisite,
  2107. $courseInfo,
  2108. $sessionId
  2109. ) {
  2110. $isBlocked = false;
  2111. if (!empty($prerequisite)) {
  2112. $progress = self::getProgress(
  2113. $prerequisite,
  2114. $studentId,
  2115. $courseInfo['real_id'],
  2116. $sessionId
  2117. );
  2118. if ($progress < 100) {
  2119. $isBlocked = true;
  2120. }
  2121. }
  2122. return $isBlocked;
  2123. }
  2124. /**
  2125. * Checks if the learning path is visible for student after the progress
  2126. * of its prerequisite is completed, considering the time availability and
  2127. * the LP visibility.
  2128. * @param int $lp_id
  2129. * @param int $student_id
  2130. * @param string Course code (optional)
  2131. * @param int $sessionId
  2132. * @return bool
  2133. */
  2134. public static function is_lp_visible_for_student(
  2135. $lp_id,
  2136. $student_id,
  2137. $courseCode = null,
  2138. $sessionId = 0
  2139. ) {
  2140. $courseInfo = api_get_course_info($courseCode);
  2141. $lp_id = (int) $lp_id;
  2142. $sessionId = (int) $sessionId;
  2143. if (empty($sessionId)) {
  2144. $sessionId = api_get_session_id();
  2145. }
  2146. if (empty($courseInfo)) {
  2147. return false;
  2148. }
  2149. $itemInfo = api_get_item_property_info(
  2150. $courseInfo['real_id'],
  2151. TOOL_LEARNPATH,
  2152. $lp_id,
  2153. $sessionId
  2154. );
  2155. // If the item was deleted.
  2156. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2157. return false;
  2158. }
  2159. // @todo remove this query and load the row info as a parameter
  2160. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2161. // Get current prerequisite
  2162. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2163. FROM $tbl_learnpath
  2164. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2165. $rs = Database::query($sql);
  2166. $now = time();
  2167. if (Database::num_rows($rs) > 0) {
  2168. $row = Database::fetch_array($rs, 'ASSOC');
  2169. $prerequisite = $row['prerequisite'];
  2170. $is_visible = true;
  2171. $isBlocked = self::isBlockedByPrerequisite(
  2172. $student_id,
  2173. $prerequisite,
  2174. $courseInfo,
  2175. $sessionId
  2176. );
  2177. if ($isBlocked) {
  2178. $is_visible = false;
  2179. }
  2180. // Also check the time availability of the LP
  2181. if ($is_visible) {
  2182. // Adding visibility restrictions
  2183. if (!empty($row['publicated_on'])) {
  2184. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2185. $is_visible = false;
  2186. }
  2187. }
  2188. // Blocking empty start times see BT#2800
  2189. global $_custom;
  2190. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2191. $_custom['lps_hidden_when_no_start_date']
  2192. ) {
  2193. if (empty($row['publicated_on'])) {
  2194. $is_visible = false;
  2195. }
  2196. }
  2197. if (!empty($row['expired_on'])) {
  2198. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2199. $is_visible = false;
  2200. }
  2201. }
  2202. }
  2203. // Check if the subscription users/group to a LP is ON
  2204. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2205. // Try group
  2206. $is_visible = false;
  2207. // Checking only the user visibility
  2208. $userVisibility = api_get_item_visibility(
  2209. $courseInfo,
  2210. 'learnpath',
  2211. $row['id'],
  2212. $sessionId,
  2213. $student_id,
  2214. 'LearnpathSubscription'
  2215. );
  2216. if ($userVisibility == 1) {
  2217. $is_visible = true;
  2218. } else {
  2219. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2220. if (!empty($userGroups)) {
  2221. foreach ($userGroups as $groupInfo) {
  2222. $groupId = $groupInfo['iid'];
  2223. $userVisibility = api_get_item_visibility(
  2224. $courseInfo,
  2225. 'learnpath',
  2226. $row['id'],
  2227. $sessionId,
  2228. null,
  2229. 'LearnpathSubscription',
  2230. $groupId
  2231. );
  2232. if ($userVisibility == 1) {
  2233. $is_visible = true;
  2234. break;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. }
  2240. return $is_visible;
  2241. }
  2242. return false;
  2243. }
  2244. /**
  2245. * @param int $lpId
  2246. * @param int $userId
  2247. * @param int $courseId
  2248. * @param int $sessionId
  2249. * @return int
  2250. */
  2251. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2252. {
  2253. $lpId = intval($lpId);
  2254. $userId = intval($userId);
  2255. $courseId = intval($courseId);
  2256. $sessionId = intval($sessionId);
  2257. $progress = 0;
  2258. $sessionCondition = api_get_session_condition($sessionId);
  2259. $table = Database::get_course_table(TABLE_LP_VIEW);
  2260. $sql = "SELECT * FROM $table
  2261. WHERE
  2262. c_id = ".$courseId." AND
  2263. lp_id = $lpId AND
  2264. user_id = $userId $sessionCondition";
  2265. $res = Database::query($sql);
  2266. if (Database::num_rows($res) > 0) {
  2267. $row = Database:: fetch_array($res);
  2268. $progress = $row['progress'];
  2269. }
  2270. return (int) $progress;
  2271. }
  2272. /**
  2273. * Displays a progress bar
  2274. * completed so far.
  2275. * @param integer $percentage Progress value to display
  2276. * @param string $text_add Text to display near the progress value
  2277. * @return string HTML string containing the progress bar
  2278. */
  2279. public static function get_progress_bar($percentage = -1, $text_add = '')
  2280. {
  2281. $text = $percentage.$text_add;
  2282. $output = '<div class="progress">
  2283. <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.';">
  2284. '. $text.'
  2285. </div>
  2286. </div>';
  2287. return $output;
  2288. }
  2289. /**
  2290. * @param string $mode can be '%' or 'abs'
  2291. * otherwise this value will be used $this->progress_bar_mode
  2292. * @return string
  2293. */
  2294. public function getProgressBar($mode = null)
  2295. {
  2296. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2297. return self::get_progress_bar($percentage, $text_add);
  2298. }
  2299. /**
  2300. * Gets the progress bar info to display inside the progress bar.
  2301. * Also used by scorm_api.php
  2302. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2303. * we display a number of completed elements per total elements
  2304. * @param integer $add Additional steps to fake as completed
  2305. * @return list array Percentage or number and symbol (% or /xx)
  2306. */
  2307. public function get_progress_bar_text($mode = '', $add = 0)
  2308. {
  2309. if ($this->debug > 0) {
  2310. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2311. }
  2312. if (empty($mode)) {
  2313. $mode = $this->progress_bar_mode;
  2314. }
  2315. $total_items = $this->getTotalItemsCountWithoutDirs();
  2316. if ($this->debug > 2) {
  2317. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2318. }
  2319. $completeItems = $this->get_complete_items_count();
  2320. if ($this->debug > 2) {
  2321. error_log('New LP - Items completed so far: '.$completeItems, 0);
  2322. }
  2323. if ($add != 0) {
  2324. $completeItems += $add;
  2325. if ($this->debug > 2) {
  2326. error_log('New LP - Items completed so far (+modifier): '.$completeItems, 0);
  2327. }
  2328. }
  2329. $text = '';
  2330. if ($completeItems > $total_items) {
  2331. $completeItems = $total_items;
  2332. }
  2333. $percentage = 0;
  2334. if ($mode == '%') {
  2335. if ($total_items > 0) {
  2336. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2337. } else {
  2338. $percentage = 0;
  2339. }
  2340. $percentage = number_format($percentage, 0);
  2341. $text = '%';
  2342. } elseif ($mode == 'abs') {
  2343. $percentage = $completeItems;
  2344. $text = '/'.$total_items;
  2345. }
  2346. return array(
  2347. $percentage,
  2348. $text
  2349. );
  2350. }
  2351. /**
  2352. * Gets the progress bar mode
  2353. * @return string The progress bar mode attribute
  2354. */
  2355. public function get_progress_bar_mode()
  2356. {
  2357. if ($this->debug > 0) {
  2358. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2359. }
  2360. if (!empty ($this->progress_bar_mode)) {
  2361. return $this->progress_bar_mode;
  2362. } else {
  2363. return '%';
  2364. }
  2365. }
  2366. /**
  2367. * Gets the learnpath proximity (remote or local)
  2368. * @return string Learnpath proximity
  2369. */
  2370. public function get_proximity()
  2371. {
  2372. if ($this->debug > 0) {
  2373. error_log('New LP - In learnpath::get_proximity()', 0);
  2374. }
  2375. if (!empty ($this->proximity)) {
  2376. return $this->proximity;
  2377. } else {
  2378. return '';
  2379. }
  2380. }
  2381. /**
  2382. * Gets the learnpath theme (remote or local)
  2383. * @return string Learnpath theme
  2384. */
  2385. public function get_theme()
  2386. {
  2387. if ($this->debug > 0) {
  2388. error_log('New LP - In learnpath::get_theme()', 0);
  2389. }
  2390. if (!empty ($this->theme)) {
  2391. return $this->theme;
  2392. } else {
  2393. return '';
  2394. }
  2395. }
  2396. /**
  2397. * Gets the learnpath session id
  2398. * @return int
  2399. */
  2400. public function get_lp_session_id()
  2401. {
  2402. if ($this->debug > 0) {
  2403. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2404. }
  2405. if (!empty ($this->lp_session_id)) {
  2406. return (int) $this->lp_session_id;
  2407. } else {
  2408. return 0;
  2409. }
  2410. }
  2411. /**
  2412. * Gets the learnpath image
  2413. * @return string Web URL of the LP image
  2414. */
  2415. public function get_preview_image()
  2416. {
  2417. if ($this->debug > 0) {
  2418. error_log('New LP - In learnpath::get_preview_image()', 0);
  2419. }
  2420. if (!empty($this->preview_image)) {
  2421. return $this->preview_image;
  2422. } else {
  2423. return '';
  2424. }
  2425. }
  2426. /**
  2427. * @param string $size
  2428. * @param string $path_type
  2429. * @return bool|string
  2430. */
  2431. public function get_preview_image_path($size = null, $path_type = 'web')
  2432. {
  2433. $preview_image = $this->get_preview_image();
  2434. if (isset($preview_image) && !empty($preview_image)) {
  2435. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2436. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2437. if (isset($size)) {
  2438. $info = pathinfo($preview_image);
  2439. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2440. if (file_exists($image_sys_path.$image_custom_size)) {
  2441. if ($path_type == 'web') {
  2442. return $image_path.$image_custom_size;
  2443. } else {
  2444. return $image_sys_path.$image_custom_size;
  2445. }
  2446. }
  2447. } else {
  2448. if ($path_type == 'web') {
  2449. return $image_path.$preview_image;
  2450. } else {
  2451. return $image_sys_path.$preview_image;
  2452. }
  2453. }
  2454. }
  2455. return false;
  2456. }
  2457. /**
  2458. * Gets the learnpath author
  2459. * @return string LP's author
  2460. */
  2461. public function get_author()
  2462. {
  2463. if ($this->debug > 0) {
  2464. error_log('New LP - In learnpath::get_author()', 0);
  2465. }
  2466. if (!empty ($this->author)) {
  2467. return $this->author;
  2468. } else {
  2469. return '';
  2470. }
  2471. }
  2472. /**
  2473. * Gets the learnpath author
  2474. * @return string LP's author
  2475. */
  2476. public function get_hide_toc_frame()
  2477. {
  2478. if ($this->debug > 0) {
  2479. error_log('New LP - In learnpath::get_author()', 0);
  2480. }
  2481. if (!empty ($this->hide_toc_frame)) {
  2482. return $this->hide_toc_frame;
  2483. } else {
  2484. return '';
  2485. }
  2486. }
  2487. /**
  2488. * Generate a new prerequisites string for a given item. If this item was a sco and
  2489. * its prerequisites were strings (instead of IDs), then transform those strings into
  2490. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2491. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2492. * same rule as the scorm_export() method
  2493. * @param integer Item ID
  2494. * @return string Prerequisites string ready for the export as SCORM
  2495. */
  2496. public function get_scorm_prereq_string($item_id)
  2497. {
  2498. if ($this->debug > 0) {
  2499. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2500. }
  2501. if (!is_object($this->items[$item_id])) {
  2502. return false;
  2503. }
  2504. /** @var learnpathItem $oItem */
  2505. $oItem = $this->items[$item_id];
  2506. $prereq = $oItem->get_prereq_string();
  2507. if (empty($prereq)) {
  2508. return '';
  2509. }
  2510. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2511. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2512. // then simply return it (with the ITEM_ prefix).
  2513. //return 'ITEM_' . $prereq;
  2514. return $this->items[$prereq]->ref;
  2515. } else {
  2516. if (isset($this->refs_list[$prereq])) {
  2517. // It's a simple string item from which the ID can be found in the refs list,
  2518. // so we can transform it directly to an ID for export.
  2519. return $this->items[$this->refs_list[$prereq]]->ref;
  2520. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2521. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2522. } else {
  2523. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2524. // and replace them, one by one, by the internal IDs (chamilo db)
  2525. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2526. // by a space as well.
  2527. $find = array(
  2528. '&',
  2529. '|',
  2530. '~',
  2531. '=',
  2532. '<>',
  2533. '{',
  2534. '}',
  2535. '*',
  2536. '(',
  2537. ')'
  2538. );
  2539. $replace = array(
  2540. ' ',
  2541. ' ',
  2542. ' ',
  2543. ' ',
  2544. ' ',
  2545. ' ',
  2546. ' ',
  2547. ' ',
  2548. ' ',
  2549. ' '
  2550. );
  2551. $prereq_mod = str_replace($find, $replace, $prereq);
  2552. $ids = explode(' ', $prereq_mod);
  2553. foreach ($ids as $id) {
  2554. $id = trim($id);
  2555. if (isset ($this->refs_list[$id])) {
  2556. $prereq = preg_replace('/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/', 'ITEM_'.$this->refs_list[$id], $prereq);
  2557. }
  2558. }
  2559. return $prereq;
  2560. }
  2561. }
  2562. }
  2563. /**
  2564. * Returns the XML DOM document's node
  2565. * @param resource Reference to a list of objects to search for the given ITEM_*
  2566. * @param string The identifier to look for
  2567. * @return mixed The reference to the element found with that identifier. False if not found
  2568. */
  2569. public function get_scorm_xml_node(& $children, $id)
  2570. {
  2571. for ($i = 0; $i < $children->length; $i++) {
  2572. $item_temp = $children->item($i);
  2573. if ($item_temp->nodeName == 'item') {
  2574. if ($item_temp->getAttribute('identifier') == $id) {
  2575. return $item_temp;
  2576. }
  2577. }
  2578. $subchildren = $item_temp->childNodes;
  2579. if ($subchildren && $subchildren->length > 0) {
  2580. $val = $this->get_scorm_xml_node($subchildren, $id);
  2581. if (is_object($val)) {
  2582. return $val;
  2583. }
  2584. }
  2585. }
  2586. return false;
  2587. }
  2588. /**
  2589. * Gets the status list for all LP's items
  2590. * @return array Array of [index] => [item ID => current status]
  2591. */
  2592. public function get_items_status_list()
  2593. {
  2594. if ($this->debug > 0) {
  2595. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2596. }
  2597. $list = array();
  2598. foreach ($this->ordered_items as $item_id) {
  2599. $list[] = array(
  2600. $item_id => $this->items[$item_id]->get_status()
  2601. );
  2602. }
  2603. return $list;
  2604. }
  2605. /**
  2606. * Return the number of interactions for the given learnpath Item View ID.
  2607. * This method can be used as static.
  2608. * @param integer Item View ID
  2609. * @param integer course id
  2610. * @return integer Number of interactions
  2611. */
  2612. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2613. {
  2614. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2615. $lp_iv_id = intval($lp_iv_id);
  2616. $course_id = intval($course_id);
  2617. $sql = "SELECT count(*) FROM $table
  2618. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2619. $res = Database::query($sql);
  2620. $num = 0;
  2621. if (Database::num_rows($res)) {
  2622. $row = Database::fetch_array($res);
  2623. $num = $row[0];
  2624. }
  2625. return $num;
  2626. }
  2627. /**
  2628. * Return the interactions as an array for the given lp_iv_id.
  2629. * This method can be used as static.
  2630. * @param integer Learnpath Item View ID
  2631. * @return array
  2632. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2633. */
  2634. public static function get_iv_interactions_array($lp_iv_id)
  2635. {
  2636. $course_id = api_get_course_int_id();
  2637. $list = array();
  2638. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2639. if (empty($lp_iv_id)) {
  2640. return array();
  2641. }
  2642. $sql = "SELECT * FROM $table
  2643. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2644. ORDER BY order_id ASC";
  2645. $res = Database::query($sql);
  2646. $num = Database::num_rows($res);
  2647. if ($num > 0) {
  2648. $list[] = array(
  2649. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2650. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2651. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2652. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2653. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2654. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2655. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2656. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2657. );
  2658. while ($row = Database::fetch_array($res)) {
  2659. $list[] = array(
  2660. 'order_id' => ($row['order_id'] + 1),
  2661. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2662. 'type' => $row['interaction_type'],
  2663. 'time' => $row['completion_time'],
  2664. //'correct_responses' => $row['correct_responses'],
  2665. 'correct_responses' => '', // Hide correct responses from students.
  2666. 'student_response' => $row['student_response'],
  2667. 'result' => $row['result'],
  2668. 'latency' => $row['latency']
  2669. );
  2670. }
  2671. }
  2672. return $list;
  2673. }
  2674. /**
  2675. * Return the number of objectives for the given learnpath Item View ID.
  2676. * This method can be used as static.
  2677. * @param integer Item View ID
  2678. * @return integer Number of objectives
  2679. */
  2680. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2681. {
  2682. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2683. $course_id = intval($course_id);
  2684. $lp_iv_id = intval($lp_iv_id);
  2685. $sql = "SELECT count(*) FROM $table
  2686. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2687. //@todo seems that this always returns 0
  2688. $res = Database::query($sql);
  2689. $num = 0;
  2690. if (Database::num_rows($res)) {
  2691. $row = Database::fetch_array($res);
  2692. $num = $row[0];
  2693. }
  2694. return $num;
  2695. }
  2696. /**
  2697. * Return the objectives as an array for the given lp_iv_id.
  2698. * This method can be used as static.
  2699. * @param integer Learnpath Item View ID
  2700. * @return array
  2701. * @todo Translate labels
  2702. */
  2703. public static function get_iv_objectives_array($lp_iv_id = 0)
  2704. {
  2705. $course_id = api_get_course_int_id();
  2706. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2707. $sql = "SELECT * FROM $table
  2708. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2709. ORDER BY order_id ASC";
  2710. $res = Database::query($sql);
  2711. $num = Database::num_rows($res);
  2712. $list = array();
  2713. if ($num > 0) {
  2714. $list[] = array(
  2715. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2716. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2717. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2718. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2719. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2720. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2721. );
  2722. while ($row = Database::fetch_array($res)) {
  2723. $list[] = array(
  2724. 'order_id' => ($row['order_id'] + 1),
  2725. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2726. 'score_raw' => $row['score_raw'],
  2727. 'score_max' => $row['score_max'],
  2728. 'score_min' => $row['score_min'],
  2729. 'status' => $row['status']
  2730. );
  2731. }
  2732. }
  2733. return $list;
  2734. }
  2735. /**
  2736. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2737. * used by get_html_toc() to be ready to display
  2738. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2739. */
  2740. public function get_toc()
  2741. {
  2742. if ($this->debug > 0) {
  2743. error_log('learnpath::get_toc()', 0);
  2744. }
  2745. $toc = array();
  2746. foreach ($this->ordered_items as $item_id) {
  2747. if ($this->debug > 2) {
  2748. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2749. }
  2750. // TODO: Change this link generation and use new function instead.
  2751. $toc[] = array(
  2752. 'id' => $item_id,
  2753. 'title' => $this->items[$item_id]->get_title(),
  2754. 'status' => $this->items[$item_id]->get_status(),
  2755. 'level' => $this->items[$item_id]->get_level(),
  2756. 'type' => $this->items[$item_id]->get_type(),
  2757. 'description' => $this->items[$item_id]->get_description(),
  2758. 'path' => $this->items[$item_id]->get_path(),
  2759. );
  2760. }
  2761. if ($this->debug > 2) {
  2762. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2763. }
  2764. return $toc;
  2765. }
  2766. /**
  2767. * Generate and return the table of contents for this learnpath. The JS
  2768. * table returned is used inside of scorm_api.php
  2769. * @return string A JS array vairiable construction
  2770. */
  2771. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2772. {
  2773. if ($this->debug > 0) {
  2774. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2775. }
  2776. $toc = $varname.' = new Array();';
  2777. foreach ($this->ordered_items as $item_id) {
  2778. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2779. }
  2780. if ($this->debug > 2) {
  2781. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2782. }
  2783. return $toc;
  2784. }
  2785. /**
  2786. * Gets the learning path type
  2787. * @param boolean Return the name? If false, return the ID. Default is false.
  2788. * @return mixed Type ID or name, depending on the parameter
  2789. */
  2790. public function get_type($get_name = false)
  2791. {
  2792. $res = false;
  2793. if ($this->debug > 0) {
  2794. error_log('New LP - In learnpath::get_type()', 0);
  2795. }
  2796. if (!empty ($this->type) && (!$get_name)) {
  2797. $res = $this->type;
  2798. }
  2799. if ($this->debug > 2) {
  2800. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2801. }
  2802. return $res;
  2803. }
  2804. /**
  2805. * Gets the learning path type as static method
  2806. * @param boolean Return the name? If false, return the ID. Default is false.
  2807. * @return mixed Type ID or name, depending on the parameter
  2808. */
  2809. public static function get_type_static($lp_id = 0)
  2810. {
  2811. $course_id = api_get_course_int_id();
  2812. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2813. $lp_id = intval($lp_id);
  2814. $sql = "SELECT lp_type FROM $tbl_lp
  2815. WHERE c_id = $course_id AND id = '".$lp_id."'";
  2816. $res = Database::query($sql);
  2817. if ($res === false) {
  2818. return null;
  2819. }
  2820. if (Database::num_rows($res) <= 0) {
  2821. return null;
  2822. }
  2823. $row = Database::fetch_array($res);
  2824. return $row['lp_type'];
  2825. }
  2826. /**
  2827. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2828. * This method can be used as abstract and is recursive
  2829. * @param integer Learnpath ID
  2830. * @param integer Parent ID of the items to look for
  2831. * @return array Ordered list of item IDs (empty array on error)
  2832. */
  2833. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2834. {
  2835. if (empty($course_id)) {
  2836. $course_id = api_get_course_int_id();
  2837. } else {
  2838. $course_id = intval($course_id);
  2839. }
  2840. $list = array();
  2841. if (empty($lp)) {
  2842. return $list;
  2843. }
  2844. $lp = intval($lp);
  2845. $parent = intval($parent);
  2846. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2847. $sql = "SELECT id FROM $tbl_lp_item
  2848. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2849. ORDER BY display_order";
  2850. $res = Database::query($sql);
  2851. while ($row = Database::fetch_array($res)) {
  2852. $sublist = self::get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2853. $list[] = $row['id'];
  2854. foreach ($sublist as $item) {
  2855. $list[] = $item;
  2856. }
  2857. }
  2858. return $list;
  2859. }
  2860. /**
  2861. * @return array
  2862. */
  2863. public static function getChapterTypes()
  2864. {
  2865. return array(
  2866. 'dir'
  2867. );
  2868. }
  2869. /**
  2870. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2871. * @return string HTML TOC ready to display
  2872. */
  2873. public function getListArrayToc($toc_list = null)
  2874. {
  2875. if ($this->debug > 0) {
  2876. error_log('In learnpath::get_html_toc()', 0);
  2877. }
  2878. if (empty($toc_list)) {
  2879. $toc_list = $this->get_toc();
  2880. }
  2881. // Temporary variables.
  2882. $mycurrentitemid = $this->get_current_item_id();
  2883. $list = [];
  2884. $arrayList = [];
  2885. $classStatus = [
  2886. 'not attempted' => 'scorm_not_attempted',
  2887. 'incomplete' => 'scorm_not_attempted',
  2888. 'failed' => 'scorm_failed',
  2889. 'completed' => 'scorm_completed',
  2890. 'passed' => 'scorm_completed',
  2891. 'succeeded' => 'scorm_completed',
  2892. 'browsed' => 'scorm_completed',
  2893. ];
  2894. foreach ($toc_list as $item) {
  2895. $list['id'] = $item['id'];
  2896. $list['status'] = $item['status'];
  2897. $cssStatus = null;
  2898. if (isset($classStatus[$item['status']])) {
  2899. $cssStatus = $classStatus[$item['status']];
  2900. }
  2901. $classStyle = ' ';
  2902. $dirTypes = self::getChapterTypes();
  2903. if (in_array($item['type'], $dirTypes)) {
  2904. $classStyle = 'scorm_item_section ';
  2905. }
  2906. if ($item['id'] == $this->current) {
  2907. $classStyle = 'scorm_item_normal '.$classStyle.'scorm_highlight';
  2908. } elseif (!in_array($item['type'], $dirTypes)) {
  2909. $classStyle = 'scorm_item_normal '.$classStyle.' ';
  2910. }
  2911. $title = $item['title'];
  2912. if (empty($title)) {
  2913. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2914. }
  2915. $title = Security::remove_XSS($item['title']);
  2916. if (empty($item['description'])) {
  2917. $list['description'] = $title;
  2918. } else {
  2919. $list['description'] = $item['description'];
  2920. }
  2921. $list['class'] = $classStyle.' '.$cssStatus;
  2922. $list['level'] = $item['level'];
  2923. $list['type'] = $item['type'];
  2924. if (in_array($item['type'], $dirTypes)) {
  2925. $list['css_level'] = 'level_'.$item['level'];
  2926. } else {
  2927. $list['css_level'] = 'level_'.$item['level']
  2928. .' scorm_type_'
  2929. .learnpath::format_scorm_type_item($item['type']);
  2930. }
  2931. if (in_array($item['type'], $dirTypes)) {
  2932. $list['title'] = stripslashes($title);
  2933. } else {
  2934. $list['title'] = stripslashes($title);
  2935. $list['url'] = $this->get_link('http', $item['id'], $toc_list);
  2936. $list['current_id'] = $mycurrentitemid;
  2937. }
  2938. $arrayList[] = $list;
  2939. }
  2940. return $arrayList;
  2941. }
  2942. /**
  2943. * Returns an HTML-formatted string ready to display with teacher buttons
  2944. * in LP view menu
  2945. * @return string HTML TOC ready to display
  2946. */
  2947. public function get_teacher_toc_buttons()
  2948. {
  2949. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2950. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2951. $html = '';
  2952. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2953. $gradebook = '';
  2954. if (!empty($_GET['gradebook'])) {
  2955. $gradebook = Security::remove_XSS($_GET['gradebook']);
  2956. }
  2957. if ($this->get_lp_session_id() == api_get_session_id()) {
  2958. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2959. $html .= '<div class="btn-group">';
  2960. $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'>".
  2961. Display::returnFontAwesomeIcon('street-view').get_lang('Overview')."</a>";
  2962. $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'>".
  2963. Display::returnFontAwesomeIcon('pencil').get_lang('Edit')."</a>";
  2964. $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">'.
  2965. Display::returnFontAwesomeIcon('cog').get_lang('Settings').'</a>';
  2966. $html .= '</div>';
  2967. $html .= '</div>';
  2968. }
  2969. }
  2970. return $html;
  2971. }
  2972. /**
  2973. * Gets the learnpath maker name - generally the editor's name
  2974. * @return string Learnpath maker name
  2975. */
  2976. public function get_maker()
  2977. {
  2978. if ($this->debug > 0) {
  2979. error_log('New LP - In learnpath::get_maker()', 0);
  2980. }
  2981. if (!empty ($this->maker)) {
  2982. return $this->maker;
  2983. } else {
  2984. return '';
  2985. }
  2986. }
  2987. /**
  2988. * Gets the learnpath name/title
  2989. * @return string Learnpath name/title
  2990. */
  2991. public function get_name()
  2992. {
  2993. if ($this->debug > 0) {
  2994. error_log('New LP - In learnpath::get_name()', 0);
  2995. }
  2996. if (!empty ($this->name)) {
  2997. return $this->name;
  2998. } else {
  2999. return 'N/A';
  3000. }
  3001. }
  3002. /**
  3003. * Gets a link to the resource from the present location, depending on item ID.
  3004. * @param string $type Type of link expected
  3005. * @param integer $item_id Learnpath item ID
  3006. * @return string $provided_toc Link to the lp_item resource
  3007. */
  3008. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3009. {
  3010. $course_id = $this->get_course_int_id();
  3011. if ($this->debug > 0) {
  3012. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3013. }
  3014. if (empty($item_id)) {
  3015. if ($this->debug > 2) {
  3016. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(), 0);
  3017. }
  3018. $item_id = $this->get_current_item_id();
  3019. }
  3020. if (empty($item_id)) {
  3021. if ($this->debug > 2) {
  3022. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3023. }
  3024. //still empty, this means there was no item_id given and we are not in an object context or
  3025. //the object property is empty, return empty link
  3026. $item_id = $this->first();
  3027. return '';
  3028. }
  3029. $file = '';
  3030. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3031. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3032. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3033. $item_id = intval($item_id);
  3034. $sql = "SELECT
  3035. l.lp_type as ltype,
  3036. l.path as lpath,
  3037. li.item_type as litype,
  3038. li.path as lipath,
  3039. li.parameters as liparams
  3040. FROM $lp_table l
  3041. INNER JOIN $lp_item_table li
  3042. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3043. WHERE li.id = $item_id ";
  3044. if ($this->debug > 2) {
  3045. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3046. }
  3047. $res = Database::query($sql);
  3048. if (Database::num_rows($res) > 0) {
  3049. $row = Database::fetch_array($res);
  3050. $lp_type = $row['ltype'];
  3051. $lp_path = $row['lpath'];
  3052. $lp_item_type = $row['litype'];
  3053. $lp_item_path = $row['lipath'];
  3054. $lp_item_params = $row['liparams'];
  3055. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3056. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3057. }
  3058. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3059. if ($type == 'http') {
  3060. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3061. } else {
  3062. $course_path = $sys_course_path; //system path
  3063. }
  3064. // 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.
  3065. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3066. $lp_type = 1;
  3067. }
  3068. if ($this->debug > 2) {
  3069. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3070. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3071. }
  3072. // Now go through the specific cases to get the end of the path
  3073. // @todo Use constants instead of int values.
  3074. switch ($lp_type) {
  3075. case 1 :
  3076. $file = self::rl_get_resource_link_for_learnpath(
  3077. $course_id,
  3078. $this->get_id(),
  3079. $item_id,
  3080. $this->get_view_id()
  3081. );
  3082. if ($this->debug > 0) {
  3083. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3084. }
  3085. switch ($lp_item_type) {
  3086. case 'dir':
  3087. $file = 'lp_content.php?type=dir';
  3088. break;
  3089. case 'link':
  3090. if (Link::is_youtube_link($file)) {
  3091. $src = Link::get_youtube_video_id($file);
  3092. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=youtube&source='.$src;
  3093. } elseif (Link::isVimeoLink($file)) {
  3094. $src = Link::getVimeoLinkId($file);
  3095. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=vimeo&source='.$src;
  3096. } else {
  3097. // If the current site is HTTPS and the link is
  3098. // HTTP, browsers will refuse opening the link
  3099. $urlId = api_get_current_access_url_id();
  3100. $url = api_get_access_url($urlId, false);
  3101. $protocol = substr($url['url'], 0, 5);
  3102. if ($protocol === 'https') {
  3103. $linkProtocol = substr($file, 0, 5);
  3104. if ($linkProtocol === 'http:') {
  3105. //this is the special intervention case
  3106. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3107. }
  3108. }
  3109. }
  3110. break;
  3111. case 'quiz':
  3112. // Check how much attempts of a exercise exits in lp
  3113. $lp_item_id = $this->get_current_item_id();
  3114. $lp_view_id = $this->get_view_id();
  3115. $prevent_reinit = null;
  3116. if (isset($this->items[$this->current])) {
  3117. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3118. }
  3119. if (empty($provided_toc)) {
  3120. if ($this->debug > 0) {
  3121. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3122. }
  3123. $list = $this->get_toc();
  3124. } else {
  3125. if ($this->debug > 0) {
  3126. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3127. }
  3128. $list = $provided_toc;
  3129. }
  3130. $type_quiz = false;
  3131. foreach ($list as $toc) {
  3132. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3133. $type_quiz = true;
  3134. }
  3135. }
  3136. if ($type_quiz) {
  3137. $lp_item_id = intval($lp_item_id);
  3138. $lp_view_id = intval($lp_view_id);
  3139. $sql = "SELECT count(*) FROM $lp_item_view_table
  3140. WHERE
  3141. c_id = $course_id AND
  3142. lp_item_id='".$lp_item_id."' AND
  3143. lp_view_id ='".$lp_view_id."' AND
  3144. status='completed'";
  3145. $result = Database::query($sql);
  3146. $row_count = Database:: fetch_row($result);
  3147. $count_item_view = (int) $row_count[0];
  3148. $not_multiple_attempt = 0;
  3149. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3150. $not_multiple_attempt = 1;
  3151. }
  3152. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3153. }
  3154. break;
  3155. }
  3156. $tmp_array = explode('/', $file);
  3157. $document_name = $tmp_array[count($tmp_array) - 1];
  3158. if (strpos($document_name, '_DELETED_')) {
  3159. $file = 'blank.php?error=document_deleted';
  3160. }
  3161. break;
  3162. case 2 :
  3163. if ($this->debug > 2) {
  3164. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3165. }
  3166. if ($lp_item_type != 'dir') {
  3167. // Quite complex here:
  3168. // We want to make sure 'http://' (and similar) links can
  3169. // be loaded as is (withouth the Chamilo path in front) but
  3170. // some contents use this form: resource.htm?resource=http://blablabla
  3171. // which means we have to find a protocol at the path's start, otherwise
  3172. // it should not be considered as an external URL.
  3173. //if ($this->prerequisites_match($item_id)) {
  3174. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3175. if ($this->debug > 2) {
  3176. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3177. }
  3178. // Distant url, return as is.
  3179. $file = $lp_item_path;
  3180. } else {
  3181. if ($this->debug > 2) {
  3182. error_log('New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3183. }
  3184. // Prevent getting untranslatable urls.
  3185. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3186. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3187. // Prepare the path.
  3188. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3189. // TODO: Fix this for urls with protocol header.
  3190. $file = str_replace('//', '/', $file);
  3191. $file = str_replace(':/', '://', $file);
  3192. if (substr($lp_path, -1) == '/') {
  3193. $lp_path = substr($lp_path, 0, -1);
  3194. }
  3195. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3196. // if file not found.
  3197. $decoded = html_entity_decode($lp_item_path);
  3198. list ($decoded) = explode('?', $decoded);
  3199. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3200. $file = self::rl_get_resource_link_for_learnpath(
  3201. $course_id,
  3202. $this->get_id(),
  3203. $item_id,
  3204. $this->get_view_id()
  3205. );
  3206. if (empty($file)) {
  3207. $file = 'blank.php?error=document_not_found';
  3208. } else {
  3209. $tmp_array = explode('/', $file);
  3210. $document_name = $tmp_array[count($tmp_array) - 1];
  3211. if (strpos($document_name, '_DELETED_')) {
  3212. $file = 'blank.php?error=document_deleted';
  3213. } else {
  3214. $file = 'blank.php?error=document_not_found';
  3215. }
  3216. }
  3217. } else {
  3218. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3219. }
  3220. }
  3221. }
  3222. // We want to use parameters if they were defined in the imsmanifest
  3223. if (strpos($file, 'blank.php') === false) {
  3224. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3225. }
  3226. } else {
  3227. $file = 'lp_content.php?type=dir';
  3228. }
  3229. break;
  3230. case 3 :
  3231. if ($this->debug > 2) {
  3232. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3233. }
  3234. // Formatting AICC HACP append URL.
  3235. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(api_get_path(WEB_CODE_PATH).'lp/aicc_hacp.php').'&';
  3236. if (!empty($lp_item_params)) {
  3237. $aicc_append .= $lp_item_params.'&';
  3238. }
  3239. if ($lp_item_type != 'dir') {
  3240. // Quite complex here:
  3241. // We want to make sure 'http://' (and similar) links can
  3242. // be loaded as is (withouth the Chamilo path in front) but
  3243. // some contents use this form: resource.htm?resource=http://blablabla
  3244. // which means we have to find a protocol at the path's start, otherwise
  3245. // it should not be considered as an external URL.
  3246. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3247. if ($this->debug > 2) {
  3248. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path, 0);
  3249. }
  3250. // Distant url, return as is.
  3251. $file = $lp_item_path;
  3252. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3253. /*
  3254. if (stristr($file,'<servername>') !== false) {
  3255. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3256. }
  3257. */
  3258. if (stripos($file, '<servername>') !== false) {
  3259. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3260. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3261. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3262. }
  3263. //
  3264. $file .= $aicc_append;
  3265. } else {
  3266. if ($this->debug > 2) {
  3267. error_log('New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path, 0);
  3268. }
  3269. // Prevent getting untranslatable urls.
  3270. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3271. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3272. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3273. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3274. // TODO: Fix this for urls with protocol header.
  3275. $file = str_replace('//', '/', $file);
  3276. $file = str_replace(':/', '://', $file);
  3277. $file .= $aicc_append;
  3278. }
  3279. } else {
  3280. $file = 'lp_content.php?type=dir';
  3281. }
  3282. break;
  3283. case 4 :
  3284. break;
  3285. default:
  3286. break;
  3287. }
  3288. // Replace &amp; by & because &amp; will break URL with params
  3289. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3290. }
  3291. if ($this->debug > 2) {
  3292. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3293. }
  3294. return $file;
  3295. }
  3296. /**
  3297. * Gets the latest usable view or generate a new one
  3298. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3299. * @return integer DB lp_view id
  3300. */
  3301. public function get_view($attempt_num = 0)
  3302. {
  3303. if ($this->debug > 0) {
  3304. error_log('New LP - In learnpath::get_view()', 0);
  3305. }
  3306. $search = '';
  3307. // Use $attempt_num to enable multi-views management (disabled so far).
  3308. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3309. $search = 'AND view_count = '.$attempt_num;
  3310. }
  3311. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3312. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3313. $course_id = api_get_course_int_id();
  3314. $sessionId = api_get_session_id();
  3315. $sql = "SELECT id, view_count FROM $lp_view_table
  3316. WHERE
  3317. c_id = ".$course_id." AND
  3318. lp_id = " . $this->get_id()." AND
  3319. user_id = " . $this->get_user_id()." AND
  3320. session_id = $sessionId
  3321. $search
  3322. ORDER BY view_count DESC";
  3323. $res = Database::query($sql);
  3324. if (Database::num_rows($res) > 0) {
  3325. $row = Database::fetch_array($res);
  3326. $this->lp_view_id = $row['id'];
  3327. } else if (!api_is_invitee()) {
  3328. // There is no database record, create one.
  3329. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3330. ($course_id, ".$this->get_id().",".$this->get_user_id().", 1, $sessionId)";
  3331. Database::query($sql);
  3332. $id = Database::insert_id();
  3333. $this->lp_view_id = $id;
  3334. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3335. Database::query($sql);
  3336. }
  3337. return $this->lp_view_id;
  3338. }
  3339. /**
  3340. * Gets the current view id
  3341. * @return integer View ID (from lp_view)
  3342. */
  3343. public function get_view_id()
  3344. {
  3345. if ($this->debug > 0) {
  3346. error_log('New LP - In learnpath::get_view_id()', 0);
  3347. }
  3348. if (!empty ($this->lp_view_id)) {
  3349. return $this->lp_view_id;
  3350. } else {
  3351. return 0;
  3352. }
  3353. }
  3354. /**
  3355. * Gets the update queue
  3356. * @return array Array containing IDs of items to be updated by JavaScript
  3357. */
  3358. public function get_update_queue()
  3359. {
  3360. if ($this->debug > 0) {
  3361. error_log('New LP - In learnpath::get_update_queue()', 0);
  3362. }
  3363. return $this->update_queue;
  3364. }
  3365. /**
  3366. * Gets the user ID
  3367. * @return integer User ID
  3368. */
  3369. public function get_user_id()
  3370. {
  3371. if ($this->debug > 0) {
  3372. error_log('New LP - In learnpath::get_user_id()', 0);
  3373. }
  3374. if (!empty ($this->user_id)) {
  3375. return $this->user_id;
  3376. } else {
  3377. return false;
  3378. }
  3379. }
  3380. /**
  3381. * Checks if any of the items has an audio element attached
  3382. * @return bool True or false
  3383. */
  3384. public function has_audio()
  3385. {
  3386. if ($this->debug > 1) {
  3387. error_log('New LP - In learnpath::has_audio()', 0);
  3388. }
  3389. $has = false;
  3390. foreach ($this->items as $i => $item) {
  3391. if (!empty ($this->items[$i]->audio)) {
  3392. $has = true;
  3393. break;
  3394. }
  3395. }
  3396. return $has;
  3397. }
  3398. /**
  3399. * Logs a message into a file
  3400. * @param string Message to log
  3401. * @return boolean True on success, false on error or if msg empty
  3402. */
  3403. public function log($msg)
  3404. {
  3405. if ($this->debug > 0) {
  3406. error_log('New LP - In learnpath::log()', 0);
  3407. }
  3408. // TODO
  3409. $this->error .= $msg;
  3410. return true;
  3411. }
  3412. /**
  3413. * Moves an item up and down at its level
  3414. * @param integer Item to move up and down
  3415. * @param string Direction 'up' or 'down'
  3416. * @return integer New display order, or false on error
  3417. */
  3418. public function move_item($id, $direction)
  3419. {
  3420. $course_id = api_get_course_int_id();
  3421. if ($this->debug > 0) {
  3422. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3423. }
  3424. if (empty($id) || empty($direction)) {
  3425. return false;
  3426. }
  3427. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3428. $sql_sel = "SELECT *
  3429. FROM " . $tbl_lp_item."
  3430. WHERE c_id = ".$course_id." AND id = ".$id;
  3431. $res_sel = Database::query($sql_sel);
  3432. // Check if elem exists.
  3433. if (Database::num_rows($res_sel) < 1) {
  3434. return false;
  3435. }
  3436. // Gather data.
  3437. $row = Database::fetch_array($res_sel);
  3438. $previous = $row['previous_item_id'];
  3439. $next = $row['next_item_id'];
  3440. $display = $row['display_order'];
  3441. $parent = $row['parent_item_id'];
  3442. $lp = $row['lp_id'];
  3443. // Update the item (switch with previous/next one).
  3444. switch ($direction) {
  3445. case 'up':
  3446. if ($this->debug > 2) {
  3447. error_log('Movement up detected', 0);
  3448. }
  3449. if ($display > 1) {
  3450. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3451. WHERE c_id = ".$course_id." AND id = $previous";
  3452. if ($this->debug > 2) {
  3453. error_log('Selecting previous: '.$sql_sel2, 0);
  3454. }
  3455. $res_sel2 = Database::query($sql_sel2);
  3456. if (Database::num_rows($res_sel2) < 1) {
  3457. $previous_previous = 0;
  3458. }
  3459. // Gather data.
  3460. $row2 = Database::fetch_array($res_sel2);
  3461. $previous_previous = $row2['previous_item_id'];
  3462. // Update previous_previous item (switch "next" with current).
  3463. if ($previous_previous != 0) {
  3464. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3465. next_item_id = $id
  3466. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3467. if ($this->debug > 2) {
  3468. error_log($sql_upd2, 0);
  3469. }
  3470. Database::query($sql_upd2);
  3471. }
  3472. // Update previous item (switch with current).
  3473. if ($previous != 0) {
  3474. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3475. next_item_id = $next,
  3476. previous_item_id = $id,
  3477. display_order = display_order +1
  3478. WHERE c_id = ".$course_id." AND id = $previous";
  3479. if ($this->debug > 2) {
  3480. error_log($sql_upd2, 0);
  3481. }
  3482. Database::query($sql_upd2);
  3483. }
  3484. // Update current item (switch with previous).
  3485. if ($id != 0) {
  3486. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3487. next_item_id = $previous,
  3488. previous_item_id = $previous_previous,
  3489. display_order = display_order-1
  3490. WHERE c_id = ".$course_id." AND id = $id";
  3491. if ($this->debug > 2) {
  3492. error_log($sql_upd2, 0);
  3493. }
  3494. Database::query($sql_upd2);
  3495. }
  3496. // Update next item (new previous item).
  3497. if ($next != 0) {
  3498. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3499. WHERE c_id = ".$course_id." AND id = $next";
  3500. if ($this->debug > 2) {
  3501. error_log($sql_upd2, 0);
  3502. }
  3503. Database::query($sql_upd2);
  3504. }
  3505. $display = $display - 1;
  3506. }
  3507. break;
  3508. case 'down':
  3509. if ($this->debug > 2) {
  3510. error_log('Movement down detected', 0);
  3511. }
  3512. if ($next != 0) {
  3513. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3514. if ($this->debug > 2) {
  3515. error_log('Selecting next: '.$sql_sel2, 0);
  3516. }
  3517. $res_sel2 = Database::query($sql_sel2);
  3518. if (Database::num_rows($res_sel2) < 1) {
  3519. $next_next = 0;
  3520. }
  3521. // Gather data.
  3522. $row2 = Database::fetch_array($res_sel2);
  3523. $next_next = $row2['next_item_id'];
  3524. // Update previous item (switch with current).
  3525. if ($previous != 0) {
  3526. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3527. WHERE c_id = ".$course_id." AND id = $previous";
  3528. Database::query($sql_upd2);
  3529. }
  3530. // Update current item (switch with previous).
  3531. if ($id != 0) {
  3532. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3533. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3534. WHERE c_id = ".$course_id." AND id = $id";
  3535. Database::query($sql_upd2);
  3536. }
  3537. // Update next item (new previous item).
  3538. if ($next != 0) {
  3539. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3540. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3541. WHERE c_id = ".$course_id." AND id = $next";
  3542. Database::query($sql_upd2);
  3543. }
  3544. // Update next_next item (switch "previous" with current).
  3545. if ($next_next != 0) {
  3546. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3547. previous_item_id = $id
  3548. WHERE c_id = ".$course_id." AND id = $next_next";
  3549. Database::query($sql_upd2);
  3550. }
  3551. $display = $display + 1;
  3552. }
  3553. break;
  3554. default:
  3555. return false;
  3556. }
  3557. return $display;
  3558. }
  3559. /**
  3560. * Move a LP up (display_order)
  3561. * @param int $lp_id Learnpath ID
  3562. * @param int $categoryId
  3563. * @return bool
  3564. */
  3565. public static function move_up($lp_id, $categoryId = 0)
  3566. {
  3567. $courseId = api_get_course_int_id();
  3568. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3569. $categoryCondition = '';
  3570. if (!empty($categoryId)) {
  3571. $categoryId = (int) $categoryId;
  3572. $categoryCondition = " AND category_id = $categoryId";
  3573. }
  3574. $sql = "SELECT * FROM $lp_table
  3575. WHERE c_id = $courseId
  3576. $categoryCondition
  3577. ORDER BY display_order";
  3578. $res = Database::query($sql);
  3579. if ($res === false) {
  3580. return false;
  3581. }
  3582. $lps = [];
  3583. $lp_order = [];
  3584. $num = Database::num_rows($res);
  3585. // First check the order is correct, globally (might be wrong because
  3586. // of versions < 1.8.4)
  3587. if ($num > 0) {
  3588. $i = 1;
  3589. while ($row = Database::fetch_array($res)) {
  3590. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3591. $sql = "UPDATE $lp_table SET display_order = $i
  3592. WHERE c_id = $courseId AND id = ".$row['id'];
  3593. Database::query($sql);
  3594. }
  3595. $row['display_order'] = $i;
  3596. $lps[$row['id']] = $row;
  3597. $lp_order[$i] = $row['id'];
  3598. $i++;
  3599. }
  3600. }
  3601. if ($num > 1) { // If there's only one element, no need to sort.
  3602. $order = $lps[$lp_id]['display_order'];
  3603. if ($order > 1) { // If it's the first element, no need to move up.
  3604. $sql = "UPDATE $lp_table SET display_order = $order
  3605. WHERE c_id = $courseId AND id = ".$lp_order[$order - 1];
  3606. Database::query($sql);
  3607. $sql = "UPDATE $lp_table SET display_order = ".($order - 1)."
  3608. WHERE c_id = $courseId AND id = ".$lp_id;
  3609. Database::query($sql);
  3610. }
  3611. }
  3612. return true;
  3613. }
  3614. /**
  3615. * Move a learnpath down (display_order)
  3616. * @param int $lp_id Learnpath ID
  3617. * @param int $categoryId
  3618. * @return bool
  3619. */
  3620. public static function move_down($lp_id, $categoryId = 0)
  3621. {
  3622. $courseId = api_get_course_int_id();
  3623. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3624. $categoryCondition = '';
  3625. if (!empty($categoryId)) {
  3626. $categoryId = (int) $categoryId;
  3627. $categoryCondition = " AND category_id = $categoryId";
  3628. }
  3629. $sql = "SELECT * FROM $lp_table
  3630. WHERE c_id = $courseId
  3631. $categoryCondition
  3632. ORDER BY display_order";
  3633. $res = Database::query($sql);
  3634. if ($res === false) {
  3635. return false;
  3636. }
  3637. $lps = [];
  3638. $lp_order = [];
  3639. $num = Database::num_rows($res);
  3640. $max = 0;
  3641. // First check the order is correct, globally (might be wrong because
  3642. // of versions < 1.8.4).
  3643. if ($num > 0) {
  3644. $i = 1;
  3645. while ($row = Database::fetch_array($res)) {
  3646. $max = $i;
  3647. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3648. $need_fix = true;
  3649. $sql_u = "UPDATE $lp_table SET display_order = $i
  3650. WHERE c_id = ".$courseId." AND id = ".$row['id'];
  3651. Database::query($sql_u);
  3652. }
  3653. $row['display_order'] = $i;
  3654. $lps[$row['id']] = $row;
  3655. $lp_order[$i] = $row['id'];
  3656. $i++;
  3657. }
  3658. }
  3659. if ($num > 1) { // If there's only one element, no need to sort.
  3660. $order = $lps[$lp_id]['display_order'];
  3661. if ($order < $max) { // If it's the first element, no need to move up.
  3662. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3663. WHERE c_id = ".$courseId." AND id = ".$lp_order[$order + 1];
  3664. Database::query($sql_u1);
  3665. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3666. WHERE c_id = ".$courseId." AND id = ".$lp_id;
  3667. Database::query($sql_u2);
  3668. }
  3669. }
  3670. return true;
  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,) = $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. }
  4569. // If sco, then it is supposed to have been updated by some other call.
  4570. if ($item_type == 'sco') {
  4571. $this->items[$this->current]->restart();
  4572. }
  4573. }
  4574. if ($this->debug > 0) {
  4575. error_log('New LP - End of learnpath::start_current_item()', 0);
  4576. }
  4577. return true;
  4578. }
  4579. /**
  4580. * Stops the processing and counters for the old item (as held in $this->last)
  4581. * @return boolean True/False
  4582. */
  4583. public function stop_previous_item()
  4584. {
  4585. if ($this->debug > 0) {
  4586. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4587. }
  4588. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4589. if ($this->debug > 2) {
  4590. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4591. }
  4592. switch ($this->get_type()) {
  4593. case '3':
  4594. if ($this->items[$this->last]->get_type() != 'au') {
  4595. if ($this->debug > 2) {
  4596. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au', 0);
  4597. }
  4598. $this->items[$this->last]->close();
  4599. //$this->autocomplete_parents($this->last);
  4600. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4601. } else {
  4602. if ($this->debug > 2) {
  4603. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4604. }
  4605. }
  4606. break;
  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::return_message(get_lang('YouShouldAddItemsBeforeAttachAudio'), 'normal');
  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. $audio .= '';
  5174. }
  5175. } else {
  5176. $types = self::getChapterTypes();
  5177. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5178. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5179. if (!empty ($arrLP[$i]['audio'])) {
  5180. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5181. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang('RemoveAudio');
  5182. }
  5183. }
  5184. }
  5185. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5186. $return_audio .= '</td>';
  5187. $move_icon = '';
  5188. $move_item_icon = '';
  5189. $edit_icon = '';
  5190. $delete_icon = '';
  5191. $audio_icon = '';
  5192. $prerequisities_icon = '';
  5193. $forumIcon = '';
  5194. $previewIcon = '';
  5195. if ($is_allowed_to_edit) {
  5196. if (!$update_audio || $update_audio <> 'true') {
  5197. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5198. $move_icon .= '<a class="moved" href="#">';
  5199. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5200. $move_icon .= '</a>';
  5201. }
  5202. }
  5203. // No edit for this item types
  5204. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5205. if ($arrLP[$i]['item_type'] != 'dir') {
  5206. $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">';
  5207. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5208. $edit_icon .= '</a>';
  5209. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5210. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5211. $this->course_int_id,
  5212. $this->lp_session_id
  5213. );
  5214. if ($forumThread) {
  5215. $forumIconUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  5216. 'action' => 'dissociate_forum',
  5217. 'id' => $arrLP[$i]['id'],
  5218. 'lp_id' => $this->lp_id
  5219. ]);
  5220. $forumIcon = Display::url(
  5221. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5222. $forumIconUrl,
  5223. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5224. );
  5225. } else {
  5226. $forumIconUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  5227. 'action' => 'create_forum',
  5228. 'id' => $arrLP[$i]['id'],
  5229. 'lp_id' => $this->lp_id
  5230. ]);
  5231. $forumIcon = Display::url(
  5232. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5233. $forumIconUrl,
  5234. ['class' => "btn btn-default lp-btn-associate-forum"]
  5235. );
  5236. }
  5237. }
  5238. } else {
  5239. $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">';
  5240. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5241. $edit_icon .= '</a>';
  5242. }
  5243. } else {
  5244. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5245. $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">';
  5246. $edit_icon .= Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_TINY);
  5247. $edit_icon .= '</a>';
  5248. }
  5249. }
  5250. $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">';
  5251. $delete_icon .= Display::return_icon(
  5252. 'delete.png',
  5253. get_lang('LearnpathDeleteModule'),
  5254. [],
  5255. ICON_SIZE_TINY
  5256. );
  5257. $delete_icon .= '</a>';
  5258. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5259. $previewImage = Display::return_icon(
  5260. 'preview_view.png',
  5261. get_lang('Preview'),
  5262. [],
  5263. ICON_SIZE_TINY
  5264. );
  5265. switch ($arrLP[$i]['item_type']) {
  5266. case TOOL_DOCUMENT:
  5267. case TOOL_LP_FINAL_ITEM:
  5268. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5269. $previewIcon = Display::url(
  5270. $previewImage,
  5271. $urlPreviewLink,
  5272. array(
  5273. 'target' => '_blank',
  5274. 'class' => 'btn btn-default',
  5275. 'data-title' => $arrLP[$i]['title']
  5276. )
  5277. );
  5278. break;
  5279. case TOOL_THREAD:
  5280. case TOOL_FORUM:
  5281. case TOOL_QUIZ:
  5282. case TOOL_STUDENTPUBLICATION:
  5283. case TOOL_LP_FINAL_ITEM:
  5284. case TOOL_LINK:
  5285. //$target = '';
  5286. //$class = 'btn btn-default ajax';
  5287. //if ($arrLP[$i]['item_type'] == TOOL_LINK) {
  5288. $class = 'btn btn-default';
  5289. $target = '_blank';
  5290. //}
  5291. $link = self::rl_get_resource_link_for_learnpath(
  5292. $this->course_int_id,
  5293. $this->lp_id,
  5294. $arrLP[$i]['id'],
  5295. 0,
  5296. ''
  5297. );
  5298. $previewIcon = Display::url(
  5299. $previewImage,
  5300. $link,
  5301. [
  5302. 'class' => $class,
  5303. 'data-title' => $arrLP[$i]['title'],
  5304. 'target' => $target
  5305. ]
  5306. );
  5307. break;
  5308. default:
  5309. $previewIcon = Display::url(
  5310. $previewImage,
  5311. $url.'&action=view_item',
  5312. ['class' => 'btn btn-default', 'target' => '_blank']
  5313. );
  5314. break;
  5315. }
  5316. if ($arrLP[$i]['item_type'] != 'dir') {
  5317. $prerequisities_icon = Display::url(
  5318. Display::return_icon(
  5319. 'accept.png',
  5320. get_lang('LearnpathPrerequisites'),
  5321. array(),
  5322. ICON_SIZE_TINY
  5323. ),
  5324. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5325. );
  5326. if ($arrLP[$i]['item_type'] != 'final_item') {
  5327. $move_item_icon = Display::url(
  5328. Display::return_icon(
  5329. 'move.png',
  5330. get_lang('Move'),
  5331. array(),
  5332. ICON_SIZE_TINY
  5333. ),
  5334. $url.'&action=move_item',
  5335. ['class' => 'btn btn-default']
  5336. );
  5337. }
  5338. $audio_icon = Display::url(
  5339. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5340. $url.'&action=add_audio',
  5341. ['class' => 'btn btn-default']
  5342. );
  5343. }
  5344. }
  5345. if ($update_audio != 'true') {
  5346. $row = $move_icon.' '.$icon.
  5347. Display::span($title_cut).
  5348. Display::tag(
  5349. 'div',
  5350. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5351. array('class'=>'btn-toolbar button_actions')
  5352. );
  5353. } else {
  5354. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5355. }
  5356. $parent_id = $arrLP[$i]['parent_item_id'];
  5357. $default_data[$arrLP[$i]['id']] = $row;
  5358. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5359. if (empty($parent_id)) {
  5360. $elements[$arrLP[$i]['id']]['data'] = $row;
  5361. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5362. } else {
  5363. $parent_arrays = array();
  5364. if ($arrLP[$i]['depth'] > 1) {
  5365. //Getting list of parents
  5366. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5367. foreach ($arrLP as $item) {
  5368. if ($item['id'] == $parent_id) {
  5369. if ($item['parent_item_id'] == 0) {
  5370. $parent_id = $item['id'];
  5371. break;
  5372. } else {
  5373. $parent_id = $item['parent_item_id'];
  5374. if (empty($parent_arrays)) {
  5375. $parent_arrays[] = intval($item['id']);
  5376. }
  5377. $parent_arrays[] = $parent_id;
  5378. break;
  5379. }
  5380. }
  5381. }
  5382. }
  5383. }
  5384. if (!empty($parent_arrays)) {
  5385. $parent_arrays = array_reverse($parent_arrays);
  5386. $val = '$elements';
  5387. $x = 0;
  5388. foreach ($parent_arrays as $item) {
  5389. if ($x != count($parent_arrays) - 1) {
  5390. $val .= '["'.$item.'"]["children"]';
  5391. } else {
  5392. $val .= '["'.$item.'"]["children"]';
  5393. }
  5394. $x++;
  5395. }
  5396. $val .= "";
  5397. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5398. eval($code_str);
  5399. } else {
  5400. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5401. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5402. }
  5403. }
  5404. }
  5405. $list = '<ul id="lp_item_list">';
  5406. $tree = self::print_recursive($elements, $default_data, $default_content);
  5407. if (!empty($tree)) {
  5408. $list .= $tree;
  5409. } else {
  5410. if ($drop_element_here) {
  5411. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5412. }
  5413. }
  5414. $list .= '</ul>';
  5415. $return .= Display::panelCollapse(
  5416. $this->name,
  5417. $list,
  5418. 'scorm-list',
  5419. null,
  5420. 'scorm-list-accordion',
  5421. 'scorm-list-collapse'
  5422. );
  5423. if ($update_audio == 'true') {
  5424. $return = $return_audio;
  5425. }
  5426. return $return;
  5427. }
  5428. /**
  5429. * @param array $elements
  5430. * @param array $default_data
  5431. * @param array $default_content
  5432. * @return string
  5433. */
  5434. public function print_recursive($elements, $default_data, $default_content)
  5435. {
  5436. $return = '';
  5437. foreach ($elements as $key => $item) {
  5438. if (isset($item['load_data']) || empty($item['data'])) {
  5439. $item['data'] = $default_data[$item['load_data']];
  5440. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5441. }
  5442. $sub_list = '';
  5443. if (isset($item['type']) && $item['type'] == 'dir') {
  5444. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5445. }
  5446. if (empty($item['children'])) {
  5447. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5448. $active = null;
  5449. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5450. $active = 'active';
  5451. }
  5452. $return .= Display::tag(
  5453. 'li',
  5454. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5455. array('id'=>$key, 'class'=>'record li_container')
  5456. );
  5457. } else {
  5458. // Sections
  5459. if (isset($item['children'])) {
  5460. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5461. }
  5462. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5463. $return .= Display::tag(
  5464. 'li',
  5465. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5466. array('id'=>$key, 'class'=>'record li_container')
  5467. );
  5468. }
  5469. }
  5470. return $return;
  5471. }
  5472. /**
  5473. * This function builds the action menu
  5474. * @param bool $returnContent Optional
  5475. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5476. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5477. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5478. * @return string
  5479. */
  5480. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5481. {
  5482. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5483. $actionsLeft = '';
  5484. $actionsRight = '';
  5485. $actionsLeft .= Display::url(
  5486. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5487. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5488. 'gradebook' => $gradebook,
  5489. 'action' => 'view',
  5490. 'lp_id' => $_SESSION['oLP']->lp_id,
  5491. 'isStudentView' => 'true'
  5492. ])
  5493. );
  5494. $actionsLeft .= Display::url(
  5495. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5496. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5497. 'action' => 'admin_view',
  5498. 'lp_id' => $_SESSION['oLP']->lp_id,
  5499. 'updateaudio' => 'true'
  5500. ])
  5501. );
  5502. if (!$isConfigPage) {
  5503. $actionsLeft .= Display::url(
  5504. Display::return_icon('settings.png', get_lang('CourseSettings'), '', ICON_SIZE_MEDIUM),
  5505. 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5506. 'action' => 'edit',
  5507. 'lp_id' => $_SESSION['oLP']->lp_id
  5508. ])
  5509. );
  5510. } else {
  5511. $actionsLeft .= Display::url(
  5512. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5513. 'lp_controller.php?'.http_build_query([
  5514. 'action' => 'build',
  5515. 'lp_id' => $_SESSION['oLP']->lp_id
  5516. ]).'&'.api_get_cidreq()
  5517. );
  5518. }
  5519. if ($allowExpand) {
  5520. $actionsLeft .= Display::url(
  5521. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM).
  5522. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5523. '#',
  5524. ['role' => 'button', 'id' => 'hide_bar_template']
  5525. );
  5526. }
  5527. if ($showRequirementButtons) {
  5528. $buttons = array(
  5529. array(
  5530. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5531. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5532. 'action' => 'set_previous_step_as_prerequisite',
  5533. 'lp_id' => $_SESSION['oLP']->lp_id
  5534. ])
  5535. ),
  5536. array(
  5537. 'title' => get_lang('ClearAllPrerequisites'),
  5538. 'href' => 'lp_controller.php?'.api_get_cidreq().'&'.http_build_query([
  5539. 'action' => 'clear_prerequisites',
  5540. 'lp_id' => $_SESSION['oLP']->lp_id
  5541. ])
  5542. ),
  5543. );
  5544. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons, true);
  5545. }
  5546. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5547. if ($returnContent) {
  5548. return $toolbar;
  5549. }
  5550. echo $toolbar;
  5551. }
  5552. /**
  5553. * Creates the default learning path folder
  5554. * @param array $course
  5555. * @param int $creatorId
  5556. *
  5557. * @return bool
  5558. */
  5559. public static function generate_learning_path_folder($course, $creatorId = 0)
  5560. {
  5561. // Creating learning_path folder
  5562. $dir = '/learning_path';
  5563. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5564. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5565. $folder = false;
  5566. if (!is_dir($filepath.'/'.$dir)) {
  5567. $folderData = create_unexisting_directory(
  5568. $course,
  5569. $creatorId,
  5570. 0,
  5571. 0,
  5572. 0,
  5573. $filepath,
  5574. $dir,
  5575. get_lang('LearningPaths'),
  5576. 0
  5577. );
  5578. if (!empty($folderData)) {
  5579. $folder = true;
  5580. }
  5581. } else {
  5582. $folder = true;
  5583. }
  5584. return $folder;
  5585. }
  5586. /**
  5587. * @param array $course
  5588. * @param string $lp_name
  5589. * @param int $creatorId
  5590. *
  5591. * @return array
  5592. */
  5593. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5594. {
  5595. $filepath = '';
  5596. $dir = '/learning_path/';
  5597. if (empty($lp_name)) {
  5598. $lp_name = $this->name;
  5599. }
  5600. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5601. $folder = self::generate_learning_path_folder($course, $creatorId);
  5602. // Limits title size
  5603. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5604. $dir = $dir.$title;
  5605. // Creating LP folder
  5606. $documentId = null;
  5607. if ($folder) {
  5608. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5609. if (!is_dir($filepath.'/'.$dir)) {
  5610. $folderData = create_unexisting_directory(
  5611. $course,
  5612. $creatorId,
  5613. 0,
  5614. 0,
  5615. 0,
  5616. $filepath,
  5617. $dir,
  5618. $lp_name
  5619. );
  5620. if (!empty($folderData)) {
  5621. $folder = true;
  5622. }
  5623. $documentId = $folderData['id'];
  5624. } else {
  5625. $folder = true;
  5626. }
  5627. $dir = $dir.'/';
  5628. if ($folder) {
  5629. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5630. }
  5631. }
  5632. if (empty($documentId)) {
  5633. $dir = api_remove_trailing_slash($dir);
  5634. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5635. }
  5636. $array = array(
  5637. 'dir' => $dir,
  5638. 'filepath' => $filepath,
  5639. 'folder' => $folder,
  5640. 'id' => $documentId
  5641. );
  5642. return $array;
  5643. }
  5644. /**
  5645. * Create a new document //still needs some finetuning
  5646. * @param array $courseInfo
  5647. * @param string $content
  5648. * @param string $title
  5649. * @param string $extension
  5650. * @param int $parentId
  5651. * @param int $creatorId creator id
  5652. *
  5653. * @return string
  5654. */
  5655. public function create_document(
  5656. $courseInfo,
  5657. $content = '',
  5658. $title = '',
  5659. $extension = 'html',
  5660. $parentId = 0,
  5661. $creatorId = 0
  5662. ) {
  5663. if (!empty($courseInfo)) {
  5664. $course_id = $courseInfo['real_id'];
  5665. } else {
  5666. $course_id = api_get_course_int_id();
  5667. }
  5668. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5669. $sessionId = api_get_session_id();
  5670. // Generates folder
  5671. $result = $this->generate_lp_folder($courseInfo);
  5672. $dir = $result['dir'];
  5673. if (empty($parentId) || $parentId == '/') {
  5674. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  5675. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5676. if ($parentId === '/') {
  5677. $dir = '/';
  5678. }
  5679. // Please, do not modify this dirname formatting.
  5680. if (strstr($dir, '..')) {
  5681. $dir = '/';
  5682. }
  5683. if (!empty($dir[0]) && $dir[0] == '.') {
  5684. $dir = substr($dir, 1);
  5685. }
  5686. if (!empty($dir[0]) && $dir[0] != '/') {
  5687. $dir = '/'.$dir;
  5688. }
  5689. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5690. $dir .= '/';
  5691. }
  5692. } else {
  5693. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5694. if (!empty($parentInfo)) {
  5695. $dir = $parentInfo['path'].'/';
  5696. }
  5697. }
  5698. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  5699. if (!is_dir($filepath)) {
  5700. $dir = '/';
  5701. $filepath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/'.$dir;
  5702. }
  5703. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5704. // is already escaped twice when it gets here.
  5705. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5706. if (!empty($title)) {
  5707. $title = api_replace_dangerous_char(stripslashes($title));
  5708. } else {
  5709. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5710. }
  5711. $title = disable_dangerous_file($title);
  5712. $filename = $title;
  5713. $content = !empty($content) ? $content : $_POST['content_lp'];
  5714. $tmp_filename = $filename;
  5715. $i = 0;
  5716. while (file_exists($filepath.$tmp_filename.'.'.$extension)) {
  5717. $tmp_filename = $filename.'_'.++ $i;
  5718. }
  5719. $filename = $tmp_filename.'.'.$extension;
  5720. if ($extension == 'html') {
  5721. $content = stripslashes($content);
  5722. $content = str_replace(
  5723. api_get_path(WEB_COURSE_PATH),
  5724. api_get_path(REL_PATH).'courses/',
  5725. $content
  5726. );
  5727. // Change the path of mp3 to absolute.
  5728. // The first regexp deals with :// urls.
  5729. $content = preg_replace(
  5730. "|(flashvars=\"file=)([^:/]+)/|",
  5731. "$1".api_get_path(
  5732. REL_COURSE_PATH
  5733. ).$courseInfo['path'].'/document/',
  5734. $content
  5735. );
  5736. // The second regexp deals with audio/ urls.
  5737. $content = preg_replace(
  5738. "|(flashvars=\"file=)([^/]+)/|",
  5739. "$1".api_get_path(
  5740. REL_COURSE_PATH
  5741. ).$courseInfo['path'].'/document/$2/',
  5742. $content
  5743. );
  5744. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5745. $content = str_replace(
  5746. '</body>',
  5747. '<style type="text/css">body{}</style></body>',
  5748. $content
  5749. );
  5750. }
  5751. if (!file_exists($filepath.$filename)) {
  5752. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5753. fputs($fp, $content);
  5754. fclose($fp);
  5755. $file_size = filesize($filepath.$filename);
  5756. $save_file_path = $dir.$filename;
  5757. $document_id = add_document(
  5758. $courseInfo,
  5759. $save_file_path,
  5760. 'file',
  5761. $file_size,
  5762. $tmp_filename,
  5763. '',
  5764. 0, //readonly
  5765. true,
  5766. null,
  5767. $sessionId,
  5768. $creatorId
  5769. );
  5770. if ($document_id) {
  5771. api_item_property_update(
  5772. $courseInfo,
  5773. TOOL_DOCUMENT,
  5774. $document_id,
  5775. 'DocumentAdded',
  5776. $creatorId,
  5777. null,
  5778. null,
  5779. null,
  5780. null,
  5781. $sessionId
  5782. );
  5783. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5784. $new_title = $originalTitle;
  5785. if ($new_comment || $new_title) {
  5786. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5787. $ct = '';
  5788. if ($new_comment)
  5789. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5790. if ($new_title)
  5791. $ct .= ", title='".Database::escape_string($new_title)."' ";
  5792. $sql = "UPDATE ".$tbl_doc." SET ".substr($ct, 1)."
  5793. WHERE c_id = ".$course_id." AND id = ".$document_id;
  5794. Database::query($sql);
  5795. }
  5796. }
  5797. return $document_id;
  5798. }
  5799. }
  5800. }
  5801. /**
  5802. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5803. * @param array $_course array
  5804. * @return void
  5805. */
  5806. public function edit_document($_course)
  5807. {
  5808. $course_id = api_get_course_int_id();
  5809. $urlAppend = api_get_configuration_value('url_append');
  5810. // Please, do not modify this dirname formatting.
  5811. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5812. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  5813. if (strstr($dir, '..')) {
  5814. $dir = '/';
  5815. }
  5816. if (isset($dir[0]) && $dir[0] == '.') {
  5817. $dir = substr($dir, 1);
  5818. }
  5819. if (isset($dir[0]) && $dir[0] != '/') {
  5820. $dir = '/'.$dir;
  5821. }
  5822. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5823. $dir .= '/';
  5824. }
  5825. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5826. if (!is_dir($filepath)) {
  5827. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5828. }
  5829. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  5830. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5831. $document_id = intval($_POST['path']);
  5832. $sql = "SELECT path FROM ".$table_doc."
  5833. WHERE c_id = $course_id AND id = ".$document_id;
  5834. $res = Database::query($sql);
  5835. $row = Database::fetch_array($res);
  5836. $content = stripslashes($_POST['content_lp']);
  5837. $file = $filepath.$row['path'];
  5838. if ($fp = @ fopen($file, 'w')) {
  5839. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  5840. // Change the path of mp3 to absolute.
  5841. // The first regexp deals with :// urls.
  5842. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/', $content);
  5843. // The second regexp deals with audio/ urls.
  5844. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/', $content);
  5845. fputs($fp, $content);
  5846. fclose($fp);
  5847. $sql = "UPDATE ".$table_doc." SET
  5848. title='".Database::escape_string($_POST['title'])."'
  5849. WHERE c_id = ".$course_id." AND id = ".$document_id;
  5850. Database::query($sql);
  5851. }
  5852. }
  5853. }
  5854. /**
  5855. * Displays the selected item, with a panel for manipulating the item
  5856. * @param int $item_id
  5857. * @param string $msg
  5858. * @return string
  5859. */
  5860. public function display_item($item_id, $msg = null, $show_actions = true)
  5861. {
  5862. $course_id = api_get_course_int_id();
  5863. $return = '';
  5864. if (is_numeric($item_id)) {
  5865. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5866. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  5867. WHERE
  5868. c_id = $course_id AND
  5869. lp.id = ".intval($item_id);
  5870. $result = Database::query($sql);
  5871. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5872. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  5873. // Prevents wrong parent selection for document, see Bug#1251.
  5874. if ($row['item_type'] != 'dir') {
  5875. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5876. }
  5877. if ($show_actions) {
  5878. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5879. }
  5880. $return .= '<div style="padding:10px;">';
  5881. if ($msg != '') {
  5882. $return .= $msg;
  5883. }
  5884. $return .= '<h3>'.$row['title'].'</h3>';
  5885. switch ($row['item_type']) {
  5886. case TOOL_THREAD:
  5887. $link = $this->rl_get_resource_link_for_learnpath(
  5888. $course_id,
  5889. $row['lp_id'],
  5890. $item_id,
  5891. 0
  5892. );
  5893. $return .= Display::url(
  5894. get_lang('GoToThread'),
  5895. $link,
  5896. ['class' => 'btn btn-primary']
  5897. );
  5898. break;
  5899. case TOOL_FORUM:
  5900. $return .= Display::url(
  5901. get_lang('GoToForum'),
  5902. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$row['path'],
  5903. ['class' => 'btn btn-primary']
  5904. );
  5905. break;
  5906. case TOOL_QUIZ:
  5907. if (!empty($row['path'])) {
  5908. $exercise = new Exercise();
  5909. $exercise->read($row['path']);
  5910. $return .= $exercise->description.'<br />';
  5911. $return .= Display::url(
  5912. get_lang('GoToExercise'),
  5913. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$exercise->id,
  5914. ['class' => 'btn btn-primary']
  5915. );
  5916. }
  5917. break;
  5918. case TOOL_LP_FINAL_ITEM:
  5919. $return .= $this->getSavedFinalItem();
  5920. break;
  5921. case TOOL_DOCUMENT:
  5922. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5923. $sql_doc = "SELECT path FROM ".$tbl_doc."
  5924. WHERE c_id = ".$course_id." AND id = ".intval($row['path']);
  5925. $result = Database::query($sql_doc);
  5926. $path_file = Database::result($result, 0, 0);
  5927. $path_parts = pathinfo($path_file);
  5928. // TODO: Correct the following naive comparisons.
  5929. if (in_array($path_parts['extension'], array(
  5930. 'html',
  5931. 'txt',
  5932. 'png',
  5933. 'jpg',
  5934. 'JPG',
  5935. 'jpeg',
  5936. 'JPEG',
  5937. 'gif',
  5938. 'swf',
  5939. 'pdf',
  5940. 'htm'
  5941. ))) {
  5942. $return .= $this->display_document($row['path'], true, true);
  5943. }
  5944. break;
  5945. case TOOL_HOTPOTATOES:
  5946. $return .= $this->display_document($row['path'], false, true);
  5947. break;
  5948. }
  5949. $return .= '</div>';
  5950. }
  5951. }
  5952. return $return;
  5953. }
  5954. /**
  5955. * Shows the needed forms for editing a specific item
  5956. * @param int $item_id
  5957. * @return string
  5958. */
  5959. public function display_edit_item($item_id)
  5960. {
  5961. $course_id = api_get_course_int_id();
  5962. $return = '';
  5963. if (is_numeric($item_id)) {
  5964. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5965. $sql = "SELECT * FROM $tbl_lp_item
  5966. WHERE c_id = ".$course_id." AND id = ".intval($item_id);
  5967. $res = Database::query($sql);
  5968. $row = Database::fetch_array($res);
  5969. switch ($row['item_type']) {
  5970. case 'dir':
  5971. case 'asset':
  5972. case 'sco':
  5973. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5974. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5975. $return .= $this->display_item_form(
  5976. $row['item_type'],
  5977. get_lang('EditCurrentChapter').' :',
  5978. 'edit',
  5979. $item_id,
  5980. $row
  5981. );
  5982. } else {
  5983. $return .= $this->display_item_small_form(
  5984. $row['item_type'],
  5985. get_lang('EditCurrentChapter').' :',
  5986. $row
  5987. );
  5988. }
  5989. break;
  5990. case TOOL_DOCUMENT:
  5991. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5992. $sql = "SELECT lp.*, doc.path as dir
  5993. FROM $tbl_lp_item as lp
  5994. LEFT JOIN $tbl_doc as doc
  5995. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  5996. WHERE
  5997. lp.c_id = $course_id AND
  5998. doc.c_id = $course_id AND
  5999. lp.id = ".intval($item_id);
  6000. $res_step = Database::query($sql);
  6001. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6002. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6003. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6004. break;
  6005. case TOOL_LINK:
  6006. $link_id = (string) $row['path'];
  6007. if (ctype_digit($link_id)) {
  6008. $tbl_link = Database::get_course_table(TABLE_LINK);
  6009. $sql_select = 'SELECT url FROM '.$tbl_link.'
  6010. WHERE c_id = '.$course_id.' AND id = '.intval($link_id);
  6011. $res_link = Database::query($sql_select);
  6012. $row_link = Database::fetch_array($res_link);
  6013. if (is_array($row_link)) {
  6014. $row['url'] = $row_link['url'];
  6015. }
  6016. }
  6017. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6018. $return .= $this->display_link_form('edit', $item_id, $row);
  6019. break;
  6020. case TOOL_LP_FINAL_ITEM:
  6021. Session::write('finalItem', true);
  6022. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6023. $sql = "SELECT lp.*, doc.path as dir
  6024. FROM $tbl_lp_item as lp
  6025. LEFT JOIN $tbl_doc as doc
  6026. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6027. WHERE
  6028. lp.c_id = $course_id AND
  6029. doc.c_id = $course_id AND
  6030. lp.id = ".intval($item_id);
  6031. $res_step = Database::query($sql);
  6032. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6033. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6034. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6035. break;
  6036. case TOOL_QUIZ:
  6037. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6038. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6039. break;
  6040. case TOOL_HOTPOTATOES:
  6041. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6042. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6043. break;
  6044. case TOOL_STUDENTPUBLICATION:
  6045. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6046. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6047. break;
  6048. case TOOL_FORUM:
  6049. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6050. $return .= $this->display_forum_form('edit', $item_id, $row);
  6051. break;
  6052. case TOOL_THREAD:
  6053. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6054. $return .= $this->display_thread_form('edit', $item_id, $row);
  6055. break;
  6056. }
  6057. }
  6058. return $return;
  6059. }
  6060. /**
  6061. * Function that displays a list with al the resources that
  6062. * could be added to the learning path
  6063. * @return string
  6064. */
  6065. public function display_resources()
  6066. {
  6067. $course_code = api_get_course_id();
  6068. // Get all the docs.
  6069. $documents = $this->get_documents(true);
  6070. // Get all the exercises.
  6071. $exercises = $this->get_exercises();
  6072. // Get all the links.
  6073. $links = $this->get_links();
  6074. // Get all the student publications.
  6075. $works = $this->get_student_publications();
  6076. // Get all the forums.
  6077. $forums = $this->get_forums(null, $course_code);
  6078. // Get the final item form (see BT#11048) .
  6079. $finish = $this->getFinalItemForm();
  6080. $headers = array(
  6081. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  6082. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  6083. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  6084. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  6085. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  6086. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  6087. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6088. );
  6089. echo Display::return_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'), 'normal');
  6090. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6091. echo Display::tabs(
  6092. $headers,
  6093. array(
  6094. $documents,
  6095. $exercises,
  6096. $links,
  6097. $works,
  6098. $forums,
  6099. $dir,
  6100. $finish,
  6101. ),
  6102. 'resource_tab'
  6103. );
  6104. return true;
  6105. }
  6106. /**
  6107. * Returns the extension of a document
  6108. * @param string $filename
  6109. * @return string Extension (part after the last dot)
  6110. */
  6111. public function get_extension($filename)
  6112. {
  6113. $explode = explode('.', $filename);
  6114. return $explode[count($explode) - 1];
  6115. }
  6116. /**
  6117. * Displays a document by id
  6118. *
  6119. * @param int $id
  6120. * @return string
  6121. */
  6122. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6123. {
  6124. $_course = api_get_course_info();
  6125. $course_id = api_get_course_int_id();
  6126. $return = '';
  6127. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6128. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6129. WHERE c_id = ".$course_id." AND id = ".$id;
  6130. $res_doc = Database::query($sql_doc);
  6131. $row_doc = Database::fetch_array($res_doc);
  6132. // TODO: Add a path filter.
  6133. if ($iframe) {
  6134. $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>';
  6135. } else {
  6136. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/'.$row_doc['path']);
  6137. }
  6138. return $return;
  6139. }
  6140. /**
  6141. * Return HTML form to add/edit a quiz
  6142. * @param string $action Action (add/edit)
  6143. * @param integer $id Item ID if already exists
  6144. * @param mixed $extra_info Extra information (quiz ID if integer)
  6145. * @return string HTML form
  6146. */
  6147. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6148. {
  6149. $course_id = api_get_course_int_id();
  6150. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6151. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6152. if ($id != 0 && is_array($extra_info)) {
  6153. $item_title = $extra_info['title'];
  6154. $item_description = $extra_info['description'];
  6155. } elseif (is_numeric($extra_info)) {
  6156. $sql = "SELECT title, description
  6157. FROM $tbl_quiz
  6158. WHERE c_id = $course_id AND id = ".$extra_info;
  6159. $result = Database::query($sql);
  6160. $row = Database::fetch_array($result);
  6161. $item_title = $row['title'];
  6162. $item_description = $row['description'];
  6163. } else {
  6164. $item_title = '';
  6165. $item_description = '';
  6166. }
  6167. $item_title = Security::remove_XSS($item_title);
  6168. $item_description = Security::remove_XSS($item_description);
  6169. if ($id != 0 && is_array($extra_info)) {
  6170. $parent = $extra_info['parent_item_id'];
  6171. } else {
  6172. $parent = 0;
  6173. }
  6174. $sql = "SELECT * FROM $tbl_lp_item
  6175. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  6176. $result = Database::query($sql);
  6177. $arrLP = array();
  6178. while ($row = Database::fetch_array($result)) {
  6179. $arrLP[] = array(
  6180. 'id' => $row['id'],
  6181. 'item_type' => $row['item_type'],
  6182. 'title' => $row['title'],
  6183. 'path' => $row['path'],
  6184. 'description' => $row['description'],
  6185. 'parent_item_id' => $row['parent_item_id'],
  6186. 'previous_item_id' => $row['previous_item_id'],
  6187. 'next_item_id' => $row['next_item_id'],
  6188. 'display_order' => $row['display_order'],
  6189. 'max_score' => $row['max_score'],
  6190. 'min_score' => $row['min_score'],
  6191. 'mastery_score' => $row['mastery_score'],
  6192. 'prerequisite' => $row['prerequisite'],
  6193. 'max_time_allowed' => $row['max_time_allowed']
  6194. );
  6195. }
  6196. $this->tree_array($arrLP);
  6197. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6198. unset($this->arrMenu);
  6199. $form = new FormValidator(
  6200. 'quiz_form',
  6201. 'POST',
  6202. $this->getCurrentBuildingModeURL()
  6203. );
  6204. $defaults = [];
  6205. if ($action == 'add') {
  6206. $legend = get_lang('CreateTheExercise');
  6207. } elseif ($action == 'move') {
  6208. $legend = get_lang('MoveTheCurrentExercise');
  6209. } else {
  6210. $legend = get_lang('EditCurrentExecice');
  6211. }
  6212. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6213. $legend .= Display::return_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6214. }
  6215. $form->addHeader($legend);
  6216. if ($action != 'move') {
  6217. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6218. $defaults['title'] = $item_title;
  6219. }
  6220. // Select for Parent item, root or chapter
  6221. $selectParent = $form->addSelect(
  6222. 'parent',
  6223. get_lang('Parent'),
  6224. [],
  6225. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6226. );
  6227. $selectParent->addOption($this->name, 0);
  6228. $arrHide = array(
  6229. $id
  6230. );
  6231. for ($i = 0; $i < count($arrLP); $i++) {
  6232. if ($action != 'add') {
  6233. if (
  6234. ($arrLP[$i]['item_type'] == 'dir') &&
  6235. !in_array($arrLP[$i]['id'], $arrHide) &&
  6236. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6237. ) {
  6238. $selectParent->addOption(
  6239. $arrLP[$i]['title'],
  6240. $arrLP[$i]['id'],
  6241. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6242. );
  6243. if ($parent == $arrLP[$i]['id']) {
  6244. $selectParent->setSelected($arrLP[$i]['id']);
  6245. }
  6246. } else {
  6247. $arrHide[] = $arrLP[$i]['id'];
  6248. }
  6249. } else {
  6250. if ($arrLP[$i]['item_type'] == 'dir') {
  6251. $selectParent->addOption(
  6252. $arrLP[$i]['title'],
  6253. $arrLP[$i]['id'], ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6254. );
  6255. if ($parent == $arrLP[$i]['id']) {
  6256. $selectParent->setSelected($arrLP[$i]['id']);
  6257. }
  6258. }
  6259. }
  6260. }
  6261. if (is_array($arrLP)) {
  6262. reset($arrLP);
  6263. }
  6264. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6265. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6266. for ($i = 0; $i < count($arrLP); $i++) {
  6267. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6268. $selectPrevious->addOption(get_lang('After').' "'.$arrLP[$i]['title'].'"', $arrLP[$i]['id']);
  6269. if (is_array($extra_info)) {
  6270. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6271. $selectPrevious->setSelected($arrLP[$i]['id']);
  6272. }
  6273. } elseif ($action == 'add') {
  6274. $selectPrevious->setSelected($arrLP[$i]['id']);
  6275. }
  6276. }
  6277. }
  6278. if ($action != 'move') {
  6279. $id_prerequisite = 0;
  6280. if (is_array($arrLP)) {
  6281. foreach ($arrLP as $key => $value) {
  6282. if ($value['id'] == $id) {
  6283. $id_prerequisite = $value['prerequisite'];
  6284. break;
  6285. }
  6286. }
  6287. }
  6288. $arrHide = array();
  6289. for ($i = 0; $i < count($arrLP); $i++) {
  6290. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6291. if (is_array($extra_info)) {
  6292. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6293. $s_selected_position = $arrLP[$i]['id'];
  6294. }
  6295. } elseif ($action == 'add') {
  6296. $s_selected_position = 0;
  6297. }
  6298. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6299. }
  6300. }
  6301. /*// Commented the prerequisites, only visible in edit (exercise).
  6302. $return .= '<tr>';
  6303. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6304. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6305. foreach($arrHide as $key => $value){
  6306. if($key==$s_selected_position && $action == 'add'){
  6307. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6308. }
  6309. elseif($key==$id_prerequisite && $action == 'edit'){
  6310. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6311. }
  6312. else{
  6313. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6314. }
  6315. }
  6316. $return .= "</select></td>";
  6317. */
  6318. /*$return .= '<tr>';
  6319. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6320. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6321. // Remove temporarily the test description.
  6322. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6323. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6324. $return .= '</tr>'; */
  6325. }
  6326. if ($action == 'add') {
  6327. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6328. } else {
  6329. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6330. }
  6331. if ($action == 'move') {
  6332. $form->addHidden('title', $item_title);
  6333. $form->addHidden('description', $item_description);
  6334. }
  6335. if (is_numeric($extra_info)) {
  6336. $form->addHidden('path', $extra_info);
  6337. } elseif (is_array($extra_info)) {
  6338. $form->addHidden('path', $extra_info['path']);
  6339. }
  6340. $form->addHidden('type', TOOL_QUIZ);
  6341. $form->addHidden('post_time', time());
  6342. $form->setDefaults($defaults);
  6343. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  6344. }
  6345. /**
  6346. * Addition of Hotpotatoes tests
  6347. * @param string Action
  6348. * @param integer Internal ID of the item
  6349. * @param mixed Extra information - can be an array with title and description indexes
  6350. * @return string HTML structure to display the hotpotatoes addition formular
  6351. */
  6352. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6353. {
  6354. $course_id = api_get_course_int_id();
  6355. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6356. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6357. if ($id != 0 && is_array($extra_info)) {
  6358. $item_title = stripslashes($extra_info['title']);
  6359. $item_description = stripslashes($extra_info['description']);
  6360. } elseif (is_numeric($extra_info)) {
  6361. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6362. $sql = "SELECT * FROM ".$TBL_DOCUMENT."
  6363. WHERE
  6364. c_id = ".$course_id." AND
  6365. path LIKE '" . $uploadPath."/%/%htm%' AND
  6366. id = " . (int) $extra_info."
  6367. ORDER BY id ASC";
  6368. $res_hot = Database::query($sql);
  6369. $row = Database::fetch_array($res_hot);
  6370. $item_title = $row['title'];
  6371. $item_description = $row['description'];
  6372. if (!empty ($row['comment'])) {
  6373. $item_title = $row['comment'];
  6374. }
  6375. } else {
  6376. $item_title = '';
  6377. $item_description = '';
  6378. }
  6379. if ($id != 0 && is_array($extra_info)) {
  6380. $parent = $extra_info['parent_item_id'];
  6381. } else {
  6382. $parent = 0;
  6383. }
  6384. $sql = "SELECT * FROM $tbl_lp_item
  6385. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6386. $result = Database::query($sql);
  6387. $arrLP = array();
  6388. while ($row = Database::fetch_array($result)) {
  6389. $arrLP[] = array(
  6390. 'id' => $row['id'],
  6391. 'item_type' => $row['item_type'],
  6392. 'title' => $row['title'],
  6393. 'path' => $row['path'],
  6394. 'description' => $row['description'],
  6395. 'parent_item_id' => $row['parent_item_id'],
  6396. 'previous_item_id' => $row['previous_item_id'],
  6397. 'next_item_id' => $row['next_item_id'],
  6398. 'display_order' => $row['display_order'],
  6399. 'max_score' => $row['max_score'],
  6400. 'min_score' => $row['min_score'],
  6401. 'mastery_score' => $row['mastery_score'],
  6402. 'prerequisite' => $row['prerequisite'],
  6403. 'max_time_allowed' => $row['max_time_allowed']
  6404. );
  6405. }
  6406. $legend = '<legend>';
  6407. if ($action == 'add') {
  6408. $legend .= get_lang('CreateTheExercise');
  6409. } elseif ($action == 'move') {
  6410. $legend .= get_lang('MoveTheCurrentExercise');
  6411. } else {
  6412. $legend .= get_lang('EditCurrentExecice');
  6413. }
  6414. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6415. $legend .= Display:: return_message(
  6416. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6417. );
  6418. }
  6419. $legend .= '</legend>';
  6420. $return = '<form method="POST">';
  6421. $return .= $legend;
  6422. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6423. $return .= '<tr>';
  6424. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6425. $return .= '<td class="input">';
  6426. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6427. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6428. $arrHide = array(
  6429. $id
  6430. );
  6431. if (count($arrLP) > 0) {
  6432. for ($i = 0; $i < count($arrLP); $i++) {
  6433. if ($action != 'add') {
  6434. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6435. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6436. } else {
  6437. $arrHide[] = $arrLP[$i]['id'];
  6438. }
  6439. } else {
  6440. if ($arrLP[$i]['item_type'] == 'dir')
  6441. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6442. }
  6443. }
  6444. reset($arrLP);
  6445. }
  6446. $return .= '</select>';
  6447. $return .= '</td>';
  6448. $return .= '</tr>';
  6449. $return .= '<tr>';
  6450. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6451. $return .= '<td class="input">';
  6452. $return .= '<select id="previous" name="previous" size="1">';
  6453. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6454. for ($i = 0; $i < count($arrLP); $i++) {
  6455. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6456. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6457. $selected = 'selected="selected" ';
  6458. elseif ($action == 'add') $selected = 'selected="selected" ';
  6459. else
  6460. $selected = '';
  6461. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang('After').' "'.$arrLP[$i]['title'].'"</option>';
  6462. }
  6463. }
  6464. $return .= '</select>';
  6465. $return .= '</td>';
  6466. $return .= '</tr>';
  6467. if ($action != 'move') {
  6468. $return .= '<tr>';
  6469. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6470. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6471. $return .= '</tr>';
  6472. $id_prerequisite = 0;
  6473. if (is_array($arrLP) && count($arrLP) > 0) {
  6474. foreach ($arrLP as $key => $value) {
  6475. if ($value['id'] == $id) {
  6476. $id_prerequisite = $value['prerequisite'];
  6477. break;
  6478. }
  6479. }
  6480. $arrHide = array();
  6481. for ($i = 0; $i < count($arrLP); $i++) {
  6482. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6483. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6484. $s_selected_position = $arrLP[$i]['id'];
  6485. elseif ($action == 'add') $s_selected_position = 0;
  6486. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6487. }
  6488. }
  6489. }
  6490. }
  6491. $return .= '<tr>';
  6492. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang('SaveHotpotatoes').'</button></td>';
  6493. $return .= '</tr>';
  6494. $return .= '</table>';
  6495. if ($action == 'move') {
  6496. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6497. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6498. }
  6499. if (is_numeric($extra_info)) {
  6500. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6501. } elseif (is_array($extra_info)) {
  6502. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6503. }
  6504. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6505. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6506. $return .= '</form>';
  6507. return $return;
  6508. }
  6509. /**
  6510. * Return the form to display the forum edit/add option
  6511. * @param string Action (add/edit)
  6512. * @param integer ID of the lp_item if already exists
  6513. * @param mixed Forum ID or title
  6514. * @return string HTML form
  6515. */
  6516. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6517. {
  6518. $course_id = api_get_course_int_id();
  6519. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6520. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6521. if ($id != 0 && is_array($extra_info)) {
  6522. $item_title = stripslashes($extra_info['title']);
  6523. } elseif (is_numeric($extra_info)) {
  6524. $sql = "SELECT forum_title as title, forum_comment as comment
  6525. FROM " . $tbl_forum."
  6526. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6527. $result = Database::query($sql);
  6528. $row = Database::fetch_array($result);
  6529. $item_title = $row['title'];
  6530. $item_description = $row['comment'];
  6531. } else {
  6532. $item_title = '';
  6533. $item_description = '';
  6534. }
  6535. if ($id != 0 && is_array($extra_info)) {
  6536. $parent = $extra_info['parent_item_id'];
  6537. } else {
  6538. $parent = 0;
  6539. }
  6540. $sql = "SELECT * FROM ".$tbl_lp_item."
  6541. WHERE
  6542. c_id = ".$course_id." AND
  6543. lp_id = " . $this->lp_id;
  6544. $result = Database::query($sql);
  6545. $arrLP = array();
  6546. while ($row = Database::fetch_array($result)) {
  6547. $arrLP[] = array(
  6548. 'id' => $row['id'],
  6549. 'item_type' => $row['item_type'],
  6550. 'title' => $row['title'],
  6551. 'path' => $row['path'],
  6552. 'description' => $row['description'],
  6553. 'parent_item_id' => $row['parent_item_id'],
  6554. 'previous_item_id' => $row['previous_item_id'],
  6555. 'next_item_id' => $row['next_item_id'],
  6556. 'display_order' => $row['display_order'],
  6557. 'max_score' => $row['max_score'],
  6558. 'min_score' => $row['min_score'],
  6559. 'mastery_score' => $row['mastery_score'],
  6560. 'prerequisite' => $row['prerequisite']
  6561. );
  6562. }
  6563. $this->tree_array($arrLP);
  6564. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6565. unset($this->arrMenu);
  6566. if ($action == 'add') {
  6567. $legend = get_lang('CreateTheForum');
  6568. } elseif ($action == 'move') {
  6569. $legend = get_lang('MoveTheCurrentForum');
  6570. } else {
  6571. $legend = get_lang('EditCurrentForum');
  6572. }
  6573. $form = new FormValidator(
  6574. 'forum_form',
  6575. 'POST',
  6576. $this->getCurrentBuildingModeURL()
  6577. );
  6578. $defaults = [];
  6579. $form->addHeader($legend);
  6580. if ($action != 'move') {
  6581. $form->addText(
  6582. 'title',
  6583. get_lang('Title'),
  6584. true,
  6585. ['id' => 'idTitle', 'class' => 'learnpath_item_form']
  6586. );
  6587. $defaults['title'] = $item_title;
  6588. }
  6589. $selectParent = $form->addSelect(
  6590. 'parent',
  6591. get_lang('Parent'),
  6592. [],
  6593. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6594. );
  6595. $selectParent->addOption($this->name, 0);
  6596. $arrHide = array(
  6597. $id
  6598. );
  6599. //$parent_item_id = $_SESSION['parent_item_id'];
  6600. for ($i = 0; $i < count($arrLP); $i++) {
  6601. if ($action != 'add') {
  6602. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6603. $selectParent->addOption(
  6604. $arrLP[$i]['title'],
  6605. $arrLP[$i]['id'],
  6606. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6607. );
  6608. if ($parent == $arrLP[$i]['id']) {
  6609. $selectParent->setSelected($arrLP[$i]['id']);
  6610. }
  6611. } else {
  6612. $arrHide[] = $arrLP[$i]['id'];
  6613. }
  6614. } else {
  6615. if ($arrLP[$i]['item_type'] == 'dir') {
  6616. $selectParent->addOption(
  6617. $arrLP[$i]['title'],
  6618. $arrLP[$i]['id'],
  6619. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6620. );
  6621. if ($parent == $arrLP[$i]['id']) {
  6622. $selectParent->setSelected($arrLP[$i]['id']);
  6623. }
  6624. }
  6625. }
  6626. }
  6627. if (is_array($arrLP)) {
  6628. reset($arrLP);
  6629. }
  6630. $selectPrevious = $form->addSelect(
  6631. 'previous',
  6632. get_lang('Position'),
  6633. [],
  6634. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6635. );
  6636. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6637. for ($i = 0; $i < count($arrLP); $i++) {
  6638. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6639. $selectPrevious->addOption(get_lang('After').' "'.$arrLP[$i]['title'].'"', $arrLP[$i]['id']);
  6640. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6641. $selectPrevious->setSelected($arrLP[$i]['id']);
  6642. } elseif ($action == 'add') {
  6643. $selectPrevious->setSelected($arrLP[$i]['id']);
  6644. }
  6645. }
  6646. }
  6647. if ($action != 'move') {
  6648. $id_prerequisite = 0;
  6649. if (is_array($arrLP)) {
  6650. foreach ($arrLP as $key => $value) {
  6651. if ($value['id'] == $id) {
  6652. $id_prerequisite = $value['prerequisite'];
  6653. break;
  6654. }
  6655. }
  6656. }
  6657. $arrHide = array();
  6658. for ($i = 0; $i < count($arrLP); $i++) {
  6659. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6660. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6661. $s_selected_position = $arrLP[$i]['id'];
  6662. elseif ($action == 'add') $s_selected_position = 0;
  6663. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6664. }
  6665. }
  6666. }
  6667. if ($action == 'add') {
  6668. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6669. } else {
  6670. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6671. }
  6672. if ($action == 'move') {
  6673. $form->addHidden('title', $item_title);
  6674. $form->addHidden('description', $item_description);
  6675. }
  6676. if (is_numeric($extra_info)) {
  6677. $form->addHidden('path', $extra_info);
  6678. } elseif (is_array($extra_info)) {
  6679. $form->addHidden('path', $extra_info['path']);
  6680. }
  6681. $form->addHidden('type', TOOL_FORUM);
  6682. $form->addHidden('post_time', time());
  6683. $form->setDefaults($defaults);
  6684. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  6685. }
  6686. /**
  6687. * Return HTML form to add/edit forum threads
  6688. * @param string Action (add/edit)
  6689. * @param integer Item ID if already exists in learning path
  6690. * @param mixed Extra information (thread ID if integer)
  6691. * @return string HTML form
  6692. */
  6693. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6694. {
  6695. $course_id = api_get_course_int_id();
  6696. if (empty($course_id)) {
  6697. return null;
  6698. }
  6699. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6700. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  6701. if ($id != 0 && is_array($extra_info)) {
  6702. $item_title = stripslashes($extra_info['title']);
  6703. } elseif (is_numeric($extra_info)) {
  6704. $sql = "SELECT thread_title as title FROM $tbl_forum
  6705. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6706. $result = Database::query($sql);
  6707. $row = Database::fetch_array($result);
  6708. $item_title = $row['title'];
  6709. $item_description = '';
  6710. } else {
  6711. $item_title = '';
  6712. $item_description = '';
  6713. }
  6714. if ($id != 0 && is_array($extra_info)) {
  6715. $parent = $extra_info['parent_item_id'];
  6716. } else {
  6717. $parent = 0;
  6718. }
  6719. $sql = "SELECT * FROM ".$tbl_lp_item."
  6720. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6721. $result = Database::query($sql);
  6722. $arrLP = array();
  6723. while ($row = Database::fetch_array($result)) {
  6724. $arrLP[] = array(
  6725. 'id' => $row['id'],
  6726. 'item_type' => $row['item_type'],
  6727. 'title' => $row['title'],
  6728. 'path' => $row['path'],
  6729. 'description' => $row['description'],
  6730. 'parent_item_id' => $row['parent_item_id'],
  6731. 'previous_item_id' => $row['previous_item_id'],
  6732. 'next_item_id' => $row['next_item_id'],
  6733. 'display_order' => $row['display_order'],
  6734. 'max_score' => $row['max_score'],
  6735. 'min_score' => $row['min_score'],
  6736. 'mastery_score' => $row['mastery_score'],
  6737. 'prerequisite' => $row['prerequisite']
  6738. );
  6739. }
  6740. $this->tree_array($arrLP);
  6741. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6742. unset($this->arrMenu);
  6743. $form = new FormValidator(
  6744. 'thread_form',
  6745. 'POST',
  6746. $this->getCurrentBuildingModeURL()
  6747. );
  6748. $defaults = [];
  6749. if ($action == 'add') {
  6750. $legend = get_lang('CreateTheForum');
  6751. } elseif ($action == 'move') {
  6752. $legend = get_lang('MoveTheCurrentForum');
  6753. } else {
  6754. $legend = get_lang('EditCurrentForum');
  6755. }
  6756. $form->addHeader($legend);
  6757. $selectParent = $form->addSelect(
  6758. 'parent',
  6759. get_lang('Parent'),
  6760. [],
  6761. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6762. );
  6763. $selectParent->addOption($this->name, 0);
  6764. $arrHide = array(
  6765. $id
  6766. );
  6767. for ($i = 0; $i < count($arrLP); $i++) {
  6768. if ($action != 'add') {
  6769. if (
  6770. ($arrLP[$i]['item_type'] == 'dir') &&
  6771. !in_array($arrLP[$i]['id'], $arrHide) &&
  6772. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6773. ) {
  6774. $selectParent->addOption(
  6775. $arrLP[$i]['title'],
  6776. $arrLP[$i]['id'],
  6777. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6778. );
  6779. if ($parent == $arrLP[$i]['id']) {
  6780. $selectParent->setSelected($arrLP[$i]['id']);
  6781. }
  6782. } else {
  6783. $arrHide[] = $arrLP[$i]['id'];
  6784. }
  6785. } else {
  6786. if ($arrLP[$i]['item_type'] == 'dir') {
  6787. $selectParent->addOption(
  6788. $arrLP[$i]['title'],
  6789. $arrLP[$i]['id'],
  6790. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  6791. );
  6792. if ($parent == $arrLP[$i]['id']) {
  6793. $selectParent->setSelected($arrLP[$i]['id']);
  6794. }
  6795. }
  6796. }
  6797. }
  6798. if ($arrLP != null) {
  6799. reset($arrLP);
  6800. }
  6801. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6802. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6803. for ($i = 0; $i < count($arrLP); $i++) {
  6804. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6805. $selectPrevious->addOption(
  6806. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  6807. $arrLP[$i]['id']
  6808. );
  6809. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6810. $selectPrevious->setSelected($arrLP[$i]['id']);
  6811. } elseif ($action == 'add') {
  6812. $selectPrevious->setSelected($arrLP[$i]['id']);
  6813. }
  6814. }
  6815. }
  6816. if ($action != 'move') {
  6817. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6818. $defaults['title'] = $item_title;
  6819. $id_prerequisite = 0;
  6820. if ($arrLP != null) {
  6821. foreach ($arrLP as $key => $value) {
  6822. if ($value['id'] == $id) {
  6823. $id_prerequisite = $value['prerequisite'];
  6824. break;
  6825. }
  6826. }
  6827. }
  6828. $arrHide = array();
  6829. $s_selected_position = 0;
  6830. for ($i = 0; $i < count($arrLP); $i++) {
  6831. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6832. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6833. $s_selected_position = $arrLP[$i]['id'];
  6834. elseif ($action == 'add') $s_selected_position = 0;
  6835. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6836. }
  6837. }
  6838. $selectPrerequisites = $form->addSelect(
  6839. 'prerequisites',
  6840. get_lang('LearnpathPrerequisites'),
  6841. [],
  6842. ['id' => 'prerequisites']
  6843. );
  6844. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6845. foreach ($arrHide as $key => $value) {
  6846. $selectPrerequisites->addOption($value['value'], $key);
  6847. if ($key == $s_selected_position && $action == 'add') {
  6848. $selectPrerequisites->setSelected($key);
  6849. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6850. $selectPrerequisites->setSelected($key);
  6851. }
  6852. }
  6853. }
  6854. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6855. if ($action == 'move') {
  6856. $form->addHidden('title', $item_title);
  6857. $form->addHidden('description', $item_description);
  6858. }
  6859. if (is_numeric($extra_info)) {
  6860. $form->addHidden('path', $extra_info);
  6861. }
  6862. elseif (is_array($extra_info)) {
  6863. $form->addHidden('path', $extra_info['path']);
  6864. }
  6865. $form->addHidden('type', TOOL_THREAD);
  6866. $form->addHidden('post_time', time());
  6867. $form->setDefaults($defaults);
  6868. return $form->returnForm();
  6869. }
  6870. /**
  6871. * Return the HTML form to display an item (generally a dir item)
  6872. * @param string Item type (dir)
  6873. * @param string Title (optional, only when creating)
  6874. * @param string Action ('add'/'edit')
  6875. * @param integer lp_item ID
  6876. * @param mixed Extra info
  6877. * @return string HTML form
  6878. */
  6879. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6880. {
  6881. $course_id = api_get_course_int_id();
  6882. $_course = api_get_course_info();
  6883. global $charset;
  6884. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6885. if ($id != 0 && is_array($extra_info)) {
  6886. $item_title = $extra_info['title'];
  6887. $item_description = $extra_info['description'];
  6888. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6889. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6890. } else {
  6891. $item_title = '';
  6892. $item_description = '';
  6893. $item_path_fck = '';
  6894. }
  6895. if ($id != 0 && is_array($extra_info)) {
  6896. $parent = $extra_info['parent_item_id'];
  6897. } else {
  6898. $parent = 0;
  6899. }
  6900. $id = intval($id);
  6901. $sql = "SELECT * FROM ".$tbl_lp_item."
  6902. WHERE
  6903. c_id = ".$course_id." AND
  6904. lp_id = " . $this->lp_id." AND
  6905. id != $id";
  6906. if ($item_type == 'dir') {
  6907. $sql .= " AND parent_item_id = 0";
  6908. }
  6909. $result = Database::query($sql);
  6910. $arrLP = [];
  6911. while ($row = Database::fetch_array($result)) {
  6912. $arrLP[] = array(
  6913. 'id' => $row['id'],
  6914. 'item_type' => $row['item_type'],
  6915. 'title' => $row['title'],
  6916. 'path' => $row['path'],
  6917. 'description' => $row['description'],
  6918. 'parent_item_id' => $row['parent_item_id'],
  6919. 'previous_item_id' => $row['previous_item_id'],
  6920. 'next_item_id' => $row['next_item_id'],
  6921. 'max_score' => $row['max_score'],
  6922. 'min_score' => $row['min_score'],
  6923. 'mastery_score' => $row['mastery_score'],
  6924. 'prerequisite' => $row['prerequisite'],
  6925. 'display_order' => $row['display_order']
  6926. );
  6927. }
  6928. $this->tree_array($arrLP);
  6929. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6930. unset($this->arrMenu);
  6931. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  6932. $url = api_get_self().'?'.api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6933. $form = new FormValidator('form', 'POST', $url);
  6934. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6935. $defaults['description'] = $item_description;
  6936. $form->addElement('header', $title);
  6937. //$arrHide = array($id);
  6938. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  6939. $arrHide[0]['padding'] = 20;
  6940. $charset = api_get_system_encoding();
  6941. for ($i = 0; $i < count($arrLP); $i++) {
  6942. if ($action != 'add') {
  6943. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6944. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6945. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6946. if ($parent == $arrLP[$i]['id']) {
  6947. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6948. }
  6949. }
  6950. } else {
  6951. if ($arrLP[$i]['item_type'] == 'dir') {
  6952. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6953. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6954. if ($parent == $arrLP[$i]['id']) {
  6955. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6956. }
  6957. }
  6958. }
  6959. }
  6960. if ($action != 'move') {
  6961. $form->addElement('text', 'title', get_lang('Title'));
  6962. $form->applyFilter('title', 'html_filter');
  6963. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6964. } else {
  6965. $form->addElement('hidden', 'title');
  6966. }
  6967. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6968. foreach ($arrHide as $key => $value) {
  6969. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6970. }
  6971. if (!empty($s_selected_parent)) {
  6972. $parent_select->setSelected($s_selected_parent);
  6973. }
  6974. if (is_array($arrLP)) {
  6975. reset($arrLP);
  6976. }
  6977. $arrHide = array();
  6978. // POSITION
  6979. for ($i = 0; $i < count($arrLP); $i++) {
  6980. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6981. //this is the same!
  6982. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6983. $s_selected_position = $arrLP[$i]['id'];
  6984. } elseif ($action == 'add') {
  6985. $s_selected_position = $arrLP[$i]['id'];
  6986. }
  6987. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6988. }
  6989. }
  6990. $position = $form->addElement(
  6991. 'select',
  6992. 'previous',
  6993. get_lang('Position'),
  6994. '',
  6995. array('id' => 'previous')
  6996. );
  6997. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6998. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6999. foreach ($arrHide as $key => $value) {
  7000. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  7001. }
  7002. if (!empty ($s_selected_position)) {
  7003. $position->setSelected($s_selected_position);
  7004. }
  7005. if (is_array($arrLP)) {
  7006. reset($arrLP);
  7007. }
  7008. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  7009. //fix in order to use the tab
  7010. if ($item_type == 'dir') {
  7011. $form->addElement('hidden', 'type', 'dir');
  7012. }
  7013. $extension = null;
  7014. if (!empty($item_path)) {
  7015. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7016. }
  7017. //assets can't be modified
  7018. //$item_type == 'asset' ||
  7019. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7020. if ($item_type == 'sco') {
  7021. $form->addElement('html', '<script>alert("'.get_lang('WarningWhenEditingScorm').'")</script>');
  7022. }
  7023. $renderer = $form->defaultRenderer();
  7024. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7025. $relative_prefix = '';
  7026. $editor_config = array(
  7027. 'ToolbarSet' => 'LearningPathDocuments',
  7028. 'Width' => '100%',
  7029. 'Height' => '500',
  7030. 'FullPage' => true,
  7031. 'CreateDocumentDir' => $relative_prefix,
  7032. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7033. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7034. );
  7035. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7036. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7037. $defaults['content_lp'] = file_get_contents($content_path);
  7038. }
  7039. $form->addElement('hidden', 'type', $item_type);
  7040. $form->addElement('hidden', 'post_time', time());
  7041. $form->setDefaults($defaults);
  7042. return $form->return_form();
  7043. }
  7044. /**
  7045. * @return string
  7046. */
  7047. public function getCurrentBuildingModeURL()
  7048. {
  7049. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7050. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7051. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7052. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7053. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7054. return $currentUrl;
  7055. }
  7056. /**
  7057. * Returns the form to update or create a document
  7058. * @param string $action (add/edit)
  7059. * @param integer $id ID of the lp_item (if already exists)
  7060. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7061. * @return string HTML form
  7062. */
  7063. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7064. {
  7065. $course_id = api_get_course_int_id();
  7066. $_course = api_get_course_info();
  7067. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7068. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7069. $no_display_edit_textarea = false;
  7070. $item_description = '';
  7071. //If action==edit document
  7072. //We don't display the document form if it's not an editable document (html or txt file)
  7073. if ($action == 'edit') {
  7074. if (is_array($extra_info)) {
  7075. $path_parts = pathinfo($extra_info['dir']);
  7076. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7077. $no_display_edit_textarea = true;
  7078. }
  7079. }
  7080. }
  7081. $no_display_add = false;
  7082. // If action==add an existing document
  7083. // We don't display the document form if it's not an editable document (html or txt file).
  7084. if ($action == 'add') {
  7085. if (is_numeric($extra_info)) {
  7086. $sql_doc = "SELECT path FROM $tbl_doc
  7087. WHERE c_id = $course_id AND id = ".intval($extra_info);
  7088. $result = Database::query($sql_doc);
  7089. $path_file = Database::result($result, 0, 0);
  7090. $path_parts = pathinfo($path_file);
  7091. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7092. $no_display_add = true;
  7093. }
  7094. }
  7095. }
  7096. if ($id != 0 && is_array($extra_info)) {
  7097. $item_title = stripslashes($extra_info['title']);
  7098. $item_description = stripslashes($extra_info['description']);
  7099. $item_terms = stripslashes($extra_info['terms']);
  7100. if (empty ($item_title)) {
  7101. $path_parts = pathinfo($extra_info['path']);
  7102. $item_title = stripslashes($path_parts['filename']);
  7103. }
  7104. } elseif (is_numeric($extra_info)) {
  7105. $sql = "SELECT path, title FROM $tbl_doc
  7106. WHERE
  7107. c_id = ".$course_id." AND
  7108. id = " . intval($extra_info);
  7109. $result = Database::query($sql);
  7110. $row = Database::fetch_array($result);
  7111. $item_title = $row['title'];
  7112. $item_title = str_replace('_', ' ', $item_title);
  7113. if (empty ($item_title)) {
  7114. $path_parts = pathinfo($row['path']);
  7115. $item_title = stripslashes($path_parts['filename']);
  7116. }
  7117. } else {
  7118. $item_title = '';
  7119. $item_description = '';
  7120. }
  7121. $return = '<legend>';
  7122. if ($id != 0 && is_array($extra_info)) {
  7123. $parent = $extra_info['parent_item_id'];
  7124. } else {
  7125. $parent = 0;
  7126. }
  7127. $sql = "SELECT * FROM $tbl_lp_item
  7128. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7129. $result = Database::query($sql);
  7130. $arrLP = array();
  7131. while ($row = Database::fetch_array($result)) {
  7132. $arrLP[] = array(
  7133. 'id' => $row['id'],
  7134. 'item_type' => $row['item_type'],
  7135. 'title' => $row['title'],
  7136. 'path' => $row['path'],
  7137. 'description' => $row['description'],
  7138. 'parent_item_id' => $row['parent_item_id'],
  7139. 'previous_item_id' => $row['previous_item_id'],
  7140. 'next_item_id' => $row['next_item_id'],
  7141. 'display_order' => $row['display_order'],
  7142. 'max_score' => $row['max_score'],
  7143. 'min_score' => $row['min_score'],
  7144. 'mastery_score' => $row['mastery_score'],
  7145. 'prerequisite' => $row['prerequisite']
  7146. );
  7147. }
  7148. $this->tree_array($arrLP);
  7149. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7150. unset($this->arrMenu);
  7151. if ($action == 'add') {
  7152. $return .= get_lang('CreateTheDocument');
  7153. } elseif ($action == 'move') {
  7154. $return .= get_lang('MoveTheCurrentDocument');
  7155. } else {
  7156. $return .= get_lang('EditTheCurrentDocument');
  7157. }
  7158. $return .= '</legend>';
  7159. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7160. $return .= Display::return_message('<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'), false);
  7161. }
  7162. $form = new FormValidator(
  7163. 'form',
  7164. 'POST',
  7165. $this->getCurrentBuildingModeURL(),
  7166. '',
  7167. array('enctype' => 'multipart/form-data')
  7168. );
  7169. $defaults['title'] = Security::remove_XSS($item_title);
  7170. if (empty($item_title)) {
  7171. $defaults['title'] = Security::remove_XSS($item_title);
  7172. }
  7173. $defaults['description'] = $item_description;
  7174. $form->addElement('html', $return);
  7175. if ($action != 'move') {
  7176. $data = $this->generate_lp_folder($_course);
  7177. if ($action != 'edit') {
  7178. $folders = DocumentManager::get_all_document_folders(
  7179. $_course,
  7180. 0,
  7181. true
  7182. );
  7183. DocumentManager::build_directory_selector(
  7184. $folders,
  7185. '',
  7186. array(),
  7187. true,
  7188. $form,
  7189. 'directory_parent_id'
  7190. );
  7191. }
  7192. if (isset($data['id'])) {
  7193. $defaults['directory_parent_id'] = $data['id'];
  7194. }
  7195. $form->addElement(
  7196. 'text',
  7197. 'title',
  7198. get_lang('Title'),
  7199. array('id' => 'idTitle', 'class' => 'col-md-4')
  7200. );
  7201. $form->applyFilter('title', 'html_filter');
  7202. }
  7203. $arrHide[0]['value'] = $this->name;
  7204. $arrHide[0]['padding'] = 20;
  7205. for ($i = 0; $i < count($arrLP); $i++) {
  7206. if ($action != 'add') {
  7207. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7208. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7209. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7210. if ($parent == $arrLP[$i]['id']) {
  7211. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7212. }
  7213. }
  7214. } else {
  7215. if ($arrLP[$i]['item_type'] == 'dir') {
  7216. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7217. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7218. if ($parent == $arrLP[$i]['id']) {
  7219. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7220. }
  7221. }
  7222. }
  7223. }
  7224. $parent_select = $form->addSelect(
  7225. 'parent',
  7226. get_lang('Parent'),
  7227. [],
  7228. [
  7229. 'id' => 'idParent',
  7230. 'onchange' => 'javascript: load_cbo(this.value);',
  7231. ]
  7232. );
  7233. $my_count = 0;
  7234. foreach ($arrHide as $key => $value) {
  7235. if ($my_count != 0) {
  7236. // The LP name is also the first section and is not in the same charset like the other sections.
  7237. $value['value'] = Security::remove_XSS($value['value']);
  7238. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7239. } else {
  7240. $value['value'] = Security::remove_XSS($value['value']);
  7241. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7242. }
  7243. $my_count++;
  7244. }
  7245. if (!empty($id)) {
  7246. $parent_select->setSelected($parent);
  7247. } else {
  7248. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7249. $parent_select->setSelected($parent_item_id);
  7250. }
  7251. if (is_array($arrLP)) {
  7252. reset($arrLP);
  7253. }
  7254. $arrHide = array();
  7255. $s_selected_position = null;
  7256. // POSITION
  7257. $lastPosition = null;
  7258. for ($i = 0; $i < count($arrLP); $i++) {
  7259. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7260. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7261. ) {
  7262. if ((isset($extra_info['previous_item_id']) &&
  7263. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  7264. ) {
  7265. $s_selected_position = $arrLP[$i]['id'];
  7266. }
  7267. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7268. }
  7269. $lastPosition = $arrLP[$i]['id'];
  7270. }
  7271. if (empty($s_selected_position)) {
  7272. $s_selected_position = $lastPosition;
  7273. }
  7274. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7275. $position->addOption(get_lang('FirstPosition'), 0);
  7276. foreach ($arrHide as $key => $value) {
  7277. $padding = isset($value['padding']) ? $value['padding'] : 20;
  7278. $position->addOption(
  7279. $value['value'],
  7280. $key,
  7281. 'style="padding-left:'.$padding.'px;"'
  7282. );
  7283. }
  7284. $position->setSelected($s_selected_position);
  7285. if (is_array($arrLP)) {
  7286. reset($arrLP);
  7287. }
  7288. if ($action != 'move') {
  7289. $id_prerequisite = 0;
  7290. if (is_array($arrLP)) {
  7291. foreach ($arrLP as $key => $value) {
  7292. if ($value['id'] == $id) {
  7293. $id_prerequisite = $value['prerequisite'];
  7294. break;
  7295. }
  7296. }
  7297. }
  7298. $arrHide = array();
  7299. for ($i = 0; $i < count($arrLP); $i++) {
  7300. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7301. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7302. $s_selected_position = $arrLP[$i]['id'];
  7303. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7304. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7305. }
  7306. }
  7307. if (!$no_display_add) {
  7308. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7309. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7310. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true') {
  7311. if (isset ($_POST['content'])) {
  7312. $content = stripslashes($_POST['content']);
  7313. } elseif (is_array($extra_info)) {
  7314. //If it's an html document or a text file
  7315. if (!$no_display_edit_textarea) {
  7316. $content = $this->display_document($extra_info['path'], false, false);
  7317. }
  7318. } elseif (is_numeric($extra_info)) {
  7319. $content = $this->display_document(
  7320. $extra_info,
  7321. false,
  7322. false
  7323. );
  7324. } else {
  7325. $content = '';
  7326. }
  7327. if (!$no_display_edit_textarea) {
  7328. // We need to calculate here some specific settings for the online editor.
  7329. // The calculated settings work for documents in the Documents tool
  7330. // (on the root or in subfolders).
  7331. // For documents in native scorm packages it is unclear whether the
  7332. // online editor should be activated or not.
  7333. // A new document, it is in the root of the repository.
  7334. $relative_path = '';
  7335. $relative_prefix = '';
  7336. if (is_array($extra_info) && $extra_info != 'new') {
  7337. // The document already exists. Whe have to determine its relative path towards the repository root.
  7338. $relative_path = explode('/', $extra_info['dir']);
  7339. $cnt = count($relative_path) - 2;
  7340. if ($cnt < 0) {
  7341. $cnt = 0;
  7342. }
  7343. $relative_prefix = str_repeat('../', $cnt);
  7344. $relative_path = array_slice($relative_path, 1, $cnt);
  7345. $relative_path = implode('/', $relative_path);
  7346. if (strlen($relative_path) > 0) {
  7347. $relative_path = $relative_path.'/';
  7348. }
  7349. } else {
  7350. $result = $this->generate_lp_folder($_course);
  7351. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7352. $relative_prefix = '../../';
  7353. }
  7354. $editor_config = array(
  7355. 'ToolbarSet' => 'LearningPathDocuments',
  7356. 'Width' => '100%',
  7357. 'Height' => '500',
  7358. 'FullPage' => true,
  7359. 'CreateDocumentDir' => $relative_prefix,
  7360. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7361. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7362. );
  7363. if ($_GET['action'] == 'add_item') {
  7364. $class = 'add';
  7365. $text = get_lang('LPCreateDocument');
  7366. } else {
  7367. if ($_GET['action'] == 'edit_item') {
  7368. $class = 'save';
  7369. $text = get_lang('SaveDocument');
  7370. }
  7371. }
  7372. $form->addButtonSave($text, 'submit_button');
  7373. $renderer = $form->defaultRenderer();
  7374. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7375. $form->addElement('html', '<div class="editor-lp">');
  7376. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7377. $form->addElement('html', '</div>');
  7378. $defaults['content_lp'] = $content;
  7379. }
  7380. } elseif (is_numeric($extra_info)) {
  7381. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7382. $return = $this->display_document($extra_info, true, true, true);
  7383. $form->addElement('html', $return);
  7384. }
  7385. }
  7386. }
  7387. if (isset($extra_info['item_type']) &&
  7388. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7389. ) {
  7390. $parent_select->freeze();
  7391. $position->freeze();
  7392. }
  7393. if ($action == 'move') {
  7394. $form->addElement('hidden', 'title', $item_title);
  7395. $form->addElement('hidden', 'description', $item_description);
  7396. }
  7397. if (is_numeric($extra_info)) {
  7398. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7399. $form->addElement('hidden', 'path', $extra_info);
  7400. } elseif (is_array($extra_info)) {
  7401. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7402. $form->addElement('hidden', 'path', $extra_info['path']);
  7403. }
  7404. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7405. $form->addElement('hidden', 'post_time', time());
  7406. $form->setDefaults($defaults);
  7407. return $form->returnForm();
  7408. }
  7409. /**
  7410. * Return HTML form to add/edit a link item
  7411. * @param string $action (add/edit)
  7412. * @param integer $id Item ID if exists
  7413. * @param mixed $extra_info
  7414. * @return string HTML form
  7415. */
  7416. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7417. {
  7418. $course_id = api_get_course_int_id();
  7419. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7420. $tbl_link = Database::get_course_table(TABLE_LINK);
  7421. if ($id != 0 && is_array($extra_info)) {
  7422. $item_title = stripslashes($extra_info['title']);
  7423. $item_description = stripslashes($extra_info['description']);
  7424. $item_url = stripslashes($extra_info['url']);
  7425. } elseif (is_numeric($extra_info)) {
  7426. $extra_info = intval($extra_info);
  7427. $sql = "SELECT title, description, url FROM ".$tbl_link."
  7428. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7429. $result = Database::query($sql);
  7430. $row = Database::fetch_array($result);
  7431. $item_title = $row['title'];
  7432. $item_description = $row['description'];
  7433. $item_url = $row['url'];
  7434. } else {
  7435. $item_title = '';
  7436. $item_description = '';
  7437. $item_url = '';
  7438. }
  7439. $form = new FormValidator(
  7440. 'edit_link',
  7441. 'POST',
  7442. $this->getCurrentBuildingModeURL()
  7443. );
  7444. $defaults = [];
  7445. if ($id != 0 && is_array($extra_info)) {
  7446. $parent = $extra_info['parent_item_id'];
  7447. } else {
  7448. $parent = 0;
  7449. }
  7450. $sql = "SELECT * FROM ".$tbl_lp_item."
  7451. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7452. $result = Database::query($sql);
  7453. $arrLP = array();
  7454. while ($row = Database::fetch_array($result)) {
  7455. $arrLP[] = array(
  7456. 'id' => $row['id'],
  7457. 'item_type' => $row['item_type'],
  7458. 'title' => $row['title'],
  7459. 'path' => $row['path'],
  7460. 'description' => $row['description'],
  7461. 'parent_item_id' => $row['parent_item_id'],
  7462. 'previous_item_id' => $row['previous_item_id'],
  7463. 'next_item_id' => $row['next_item_id'],
  7464. 'display_order' => $row['display_order'],
  7465. 'max_score' => $row['max_score'],
  7466. 'min_score' => $row['min_score'],
  7467. 'mastery_score' => $row['mastery_score'],
  7468. 'prerequisite' => $row['prerequisite']
  7469. );
  7470. }
  7471. $this->tree_array($arrLP);
  7472. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7473. unset($this->arrMenu);
  7474. if ($action == 'add') {
  7475. $legend = get_lang('CreateTheLink');
  7476. } elseif ($action == 'move') {
  7477. $legend = get_lang('MoveCurrentLink');
  7478. } else {
  7479. $legend = get_lang('EditCurrentLink');
  7480. }
  7481. $form->addHeader($legend);
  7482. if ($action != 'move') {
  7483. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7484. $defaults['title'] = $item_title;
  7485. }
  7486. $selectParent = $form->addSelect(
  7487. 'parent',
  7488. get_lang('Parent'),
  7489. [],
  7490. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7491. );
  7492. $selectParent->addOption($this->name, 0);
  7493. $arrHide = array(
  7494. $id
  7495. );
  7496. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7497. for ($i = 0; $i < count($arrLP); $i++) {
  7498. if ($action != 'add') {
  7499. if (
  7500. ($arrLP[$i]['item_type'] == 'dir') &&
  7501. !in_array($arrLP[$i]['id'], $arrHide) &&
  7502. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7503. ) {
  7504. $selectParent->addOption(
  7505. $arrLP[$i]['title'],
  7506. $arrLP[$i]['id'],
  7507. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px;']
  7508. );
  7509. if ($parent == $arrLP[$i]['id']) {
  7510. $selectParent->setSelected($arrLP[$i]['id']);
  7511. }
  7512. } else {
  7513. $arrHide[] = $arrLP[$i]['id'];
  7514. }
  7515. } else {
  7516. if ($arrLP[$i]['item_type'] == 'dir') {
  7517. $selectParent->addOption(
  7518. $arrLP[$i]['title'],
  7519. $arrLP[$i]['id'],
  7520. ['style' => 'padding-left: '.(20 + $arrLP[$i]['depth'] * 20).'px']
  7521. );
  7522. if ($parent_item_id == $arrLP[$i]['id']) {
  7523. $selectParent->setSelected($arrLP[$i]['id']);
  7524. }
  7525. }
  7526. }
  7527. }
  7528. if (is_array($arrLP)) {
  7529. reset($arrLP);
  7530. }
  7531. $selectPrevious = $form->addSelect(
  7532. 'previous',
  7533. get_lang('Position'),
  7534. [],
  7535. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7536. );
  7537. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7538. for ($i = 0; $i < count($arrLP); $i++) {
  7539. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7540. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7541. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7542. $selectPrevious->setSelected($arrLP[$i]['id']);
  7543. } elseif ($action == 'add') {
  7544. $selectPrevious->setSelected($arrLP[$i]['id']);
  7545. }
  7546. }
  7547. }
  7548. if ($action != 'move') {
  7549. $urlAttributes = ['class' => 'learnpath_item_form'];
  7550. if (is_numeric($extra_info)) {
  7551. $urlAttributes['disabled'] = 'disabled';
  7552. }
  7553. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7554. $defaults['url'] = $item_url;
  7555. $id_prerequisite = 0;
  7556. if (is_array($arrLP)) {
  7557. foreach ($arrLP as $key => $value) {
  7558. if ($value['id'] == $id) {
  7559. $id_prerequisite = $value['prerequisite'];
  7560. break;
  7561. }
  7562. }
  7563. }
  7564. $arrHide = array();
  7565. for ($i = 0; $i < count($arrLP); $i++) {
  7566. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7567. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7568. $s_selected_position = $arrLP[$i]['id'];
  7569. elseif ($action == 'add') $s_selected_position = 0;
  7570. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7571. }
  7572. }
  7573. }
  7574. if ($action == 'add') {
  7575. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7576. } else {
  7577. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7578. }
  7579. if ($action == 'move') {
  7580. $form->addHidden('title', $item_title);
  7581. $form->addHidden('description', $item_description);
  7582. }
  7583. if (is_numeric($extra_info)) {
  7584. $form->addHidden('path', $extra_info);
  7585. } elseif (is_array($extra_info)) {
  7586. $form->addHidden('path', $extra_info['path']);
  7587. }
  7588. $form->addHidden('type', TOOL_LINK);
  7589. $form->addHidden('post_time', time());
  7590. $form->setDefaults($defaults);
  7591. return '<div class="sectioncomment">'.$form->returnForm().'</div>';
  7592. }
  7593. /**
  7594. * Return HTML form to add/edit a student publication (work)
  7595. * @param string Action (add/edit)
  7596. * @param integer Item ID if already exists
  7597. * @param mixed Extra info (work ID if integer)
  7598. * @return string HTML form
  7599. */
  7600. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7601. {
  7602. $course_id = api_get_course_int_id();
  7603. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7604. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  7605. if ($id != 0 && is_array($extra_info)) {
  7606. $item_title = stripslashes($extra_info['title']);
  7607. $item_description = stripslashes($extra_info['description']);
  7608. } elseif (is_numeric($extra_info)) {
  7609. $extra_info = intval($extra_info);
  7610. $sql = "SELECT title, description
  7611. FROM $tbl_publication
  7612. WHERE c_id = $course_id AND id = ".$extra_info;
  7613. $result = Database::query($sql);
  7614. $row = Database::fetch_array($result);
  7615. $item_title = $row['title'];
  7616. } else {
  7617. $item_title = get_lang('Student_publication');
  7618. }
  7619. if ($id != 0 && is_array($extra_info)) {
  7620. $parent = $extra_info['parent_item_id'];
  7621. } else {
  7622. $parent = 0;
  7623. }
  7624. $sql = "SELECT * FROM $tbl_lp_item
  7625. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7626. $result = Database::query($sql);
  7627. $arrLP = array();
  7628. while ($row = Database::fetch_array($result)) {
  7629. $arrLP[] = array(
  7630. 'id' => $row['id'],
  7631. 'item_type' => $row['item_type'],
  7632. 'title' => $row['title'],
  7633. 'path' => $row['path'],
  7634. 'description' => $row['description'],
  7635. 'parent_item_id' => $row['parent_item_id'],
  7636. 'previous_item_id' => $row['previous_item_id'],
  7637. 'next_item_id' => $row['next_item_id'],
  7638. 'display_order' => $row['display_order'],
  7639. 'max_score' => $row['max_score'],
  7640. 'min_score' => $row['min_score'],
  7641. 'mastery_score' => $row['mastery_score'],
  7642. 'prerequisite' => $row['prerequisite']
  7643. );
  7644. }
  7645. $this->tree_array($arrLP);
  7646. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7647. unset($this->arrMenu);
  7648. $form = new FormValidator('frm_student_publication', 'post', '#');
  7649. if ($action == 'add') {
  7650. $form->addHeader(get_lang('Student_publication'));
  7651. } elseif ($action == 'move') {
  7652. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7653. } else {
  7654. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7655. }
  7656. if ($action != 'move') {
  7657. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7658. }
  7659. $parentSelect = $form->addSelect(
  7660. 'parent',
  7661. get_lang('Parent'),
  7662. ['0' => $this->name],
  7663. [
  7664. 'onchange' => 'javascript: load_cbo(this.value);',
  7665. 'class' => 'learnpath_item_form',
  7666. 'id' => 'idParent'
  7667. ]
  7668. );
  7669. $arrHide = array($id);
  7670. for ($i = 0; $i < count($arrLP); $i++) {
  7671. if ($action != 'add') {
  7672. if (
  7673. ($arrLP[$i]['item_type'] == 'dir') &&
  7674. !in_array($arrLP[$i]['id'], $arrHide) &&
  7675. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7676. ) {
  7677. $parentSelect->addOption(
  7678. $arrLP[$i]['title'],
  7679. $arrLP[$i]['id'],
  7680. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  7681. );
  7682. if ($parent == $arrLP[$i]['id']) {
  7683. $parentSelect->setSelected($arrLP[$i]['id']);
  7684. }
  7685. } else {
  7686. $arrHide[] = $arrLP[$i]['id'];
  7687. }
  7688. } else {
  7689. if ($arrLP[$i]['item_type'] == 'dir') {
  7690. $parentSelect->addOption(
  7691. $arrLP[$i]['title'],
  7692. $arrLP[$i]['id'],
  7693. ['style' => 'padding-left: '.(($arrLP[$i]['depth'] * 10) + 20).'px;']
  7694. );
  7695. if ($parent == $arrLP[$i]['id']) {
  7696. $parentSelect->setSelected($arrLP[$i]['id']);
  7697. }
  7698. }
  7699. }
  7700. }
  7701. if (is_array($arrLP)) {
  7702. reset($arrLP);
  7703. }
  7704. $previousSelect = $form->addSelect(
  7705. 'previous',
  7706. get_lang('Position'),
  7707. ['0' => get_lang('FirstPosition')],
  7708. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7709. );
  7710. for ($i = 0; $i < count($arrLP); $i++) {
  7711. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7712. $previousSelect->addOption(
  7713. get_lang('After').' "'.$arrLP[$i]['title'].'"',
  7714. $arrLP[$i]['id']
  7715. );
  7716. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7717. $previousSelect->setSelected($arrLP[$i]['id']);
  7718. } elseif ($action == 'add') {
  7719. $previousSelect->setSelected($arrLP[$i]['id']);
  7720. }
  7721. }
  7722. }
  7723. if ($action != 'move') {
  7724. $id_prerequisite = 0;
  7725. if (is_array($arrLP)) {
  7726. foreach ($arrLP as $key => $value) {
  7727. if ($value['id'] == $id) {
  7728. $id_prerequisite = $value['prerequisite'];
  7729. break;
  7730. }
  7731. }
  7732. }
  7733. $arrHide = array();
  7734. for ($i = 0; $i < count($arrLP); $i++) {
  7735. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7736. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7737. $s_selected_position = $arrLP[$i]['id'];
  7738. elseif ($action == 'add') $s_selected_position = 0;
  7739. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7740. }
  7741. }
  7742. }
  7743. if ($action == 'add') {
  7744. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7745. } else {
  7746. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7747. }
  7748. if ($action == 'move') {
  7749. $form->addHidden('title', $item_title);
  7750. $form->addHidden('description', $item_description);
  7751. }
  7752. if (is_numeric($extra_info)) {
  7753. $form->addHidden('path', $extra_info);
  7754. } elseif (is_array($extra_info)) {
  7755. $form->addHidden('path', $extra_info['path']);
  7756. }
  7757. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7758. $form->addHidden('post_time', time());
  7759. $form->setDefaults(['title' => $item_title]);
  7760. $return = '<div class="sectioncomment">';
  7761. $return .= $form->returnForm();
  7762. $return .= '</div>';
  7763. return $return;
  7764. }
  7765. /**
  7766. * Displays the menu for manipulating a step
  7767. *
  7768. * @param $item_id
  7769. * @param string $item_type
  7770. * @return string
  7771. */
  7772. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7773. {
  7774. $_course = api_get_course_info();
  7775. $course_id = api_get_course_int_id();
  7776. $course_code = api_get_course_id();
  7777. $return = '<div class="actions">';
  7778. switch ($item_type) {
  7779. case 'dir':
  7780. // Commented the message cause should not show it.
  7781. //$lang = get_lang('TitleManipulateChapter');
  7782. break;
  7783. case TOOL_LP_FINAL_ITEM:
  7784. case TOOL_DOCUMENT:
  7785. // Commented the message cause should not show it.
  7786. //$lang = get_lang('TitleManipulateDocument');
  7787. break;
  7788. case TOOL_LINK:
  7789. case 'link' :
  7790. // Commented the message cause should not show it.
  7791. //$lang = get_lang('TitleManipulateLink');
  7792. break;
  7793. case TOOL_QUIZ:
  7794. // Commented the message cause should not show it.
  7795. //$lang = get_lang('TitleManipulateQuiz');
  7796. break;
  7797. case TOOL_STUDENTPUBLICATION:
  7798. // Commented the message cause should not show it.
  7799. //$lang = get_lang('TitleManipulateStudentPublication');
  7800. break;
  7801. }
  7802. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7803. $item_id = intval($item_id);
  7804. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  7805. WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7806. $result = Database::query($sql);
  7807. $row = Database::fetch_assoc($result);
  7808. $audio_player = null;
  7809. // We display an audio player if needed.
  7810. if (!empty($row['audio'])) {
  7811. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7812. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7813. </div>';
  7814. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7815. $audio_player .= '<script>
  7816. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7817. s1.addParam("allowscriptaccess","always");
  7818. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7819. s1.write("container");
  7820. </script>';
  7821. }
  7822. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7823. if ($item_type != TOOL_LP_FINAL_ITEM) {
  7824. $return .= Display::url(
  7825. Display::return_icon(
  7826. 'edit.png',
  7827. get_lang('Edit'),
  7828. array(),
  7829. ICON_SIZE_SMALL
  7830. ),
  7831. $url.'&action=edit_item&path_item='.$row['path']
  7832. );
  7833. $return .= Display::url(
  7834. Display::return_icon(
  7835. 'move.png',
  7836. get_lang('Move'),
  7837. array(),
  7838. ICON_SIZE_SMALL
  7839. ),
  7840. $url.'&action=move_item'
  7841. );
  7842. }
  7843. // Commented for now as prerequisites cannot be added to chapters.
  7844. if ($item_type != 'dir') {
  7845. $return .= Display::url(
  7846. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7847. $url.'&action=edit_item_prereq'
  7848. );
  7849. }
  7850. $return .= Display::url(
  7851. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7852. $url.'&action=delete_item'
  7853. );
  7854. if ($item_type == TOOL_HOTPOTATOES) {
  7855. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7856. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7857. }
  7858. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7859. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7860. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7861. }
  7862. $return .= '</div>';
  7863. if (!empty($audio_player)) {
  7864. $return .= '<br />'.$audio_player;
  7865. }
  7866. return $return;
  7867. }
  7868. /**
  7869. * Creates the javascript needed for filling up the checkboxes without page reload
  7870. * @return string
  7871. */
  7872. public function get_js_dropdown_array()
  7873. {
  7874. $course_id = api_get_course_int_id();
  7875. $return = 'var child_name = new Array();'."\n";
  7876. $return .= 'var child_value = new Array();'."\n\n";
  7877. $return .= 'child_name[0] = new Array();'."\n";
  7878. $return .= 'child_value[0] = new Array();'."\n\n";
  7879. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7880. $sql = "SELECT * FROM ".$tbl_lp_item."
  7881. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7882. ORDER BY display_order ASC";
  7883. $res_zero = Database::query($sql);
  7884. $i = 0;
  7885. while ($row_zero = Database::fetch_array($res_zero)) {
  7886. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7887. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7888. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7889. }
  7890. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7891. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7892. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7893. }
  7894. }
  7895. $return .= "\n";
  7896. $sql = "SELECT * FROM ".$tbl_lp_item."
  7897. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7898. $res = Database::query($sql);
  7899. while ($row = Database::fetch_array($res)) {
  7900. $sql_parent = "SELECT * FROM ".$tbl_lp_item."
  7901. WHERE
  7902. c_id = ".$course_id." AND
  7903. parent_item_id = " . $row['id']."
  7904. ORDER BY display_order ASC";
  7905. $res_parent = Database::query($sql_parent);
  7906. $i = 0;
  7907. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7908. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7909. while ($row_parent = Database::fetch_array($res_parent)) {
  7910. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7911. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7912. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7913. }
  7914. $return .= "\n";
  7915. }
  7916. return $return;
  7917. }
  7918. /**
  7919. * Display the form to allow moving an item
  7920. * @param integer Item ID
  7921. * @return string HTML form
  7922. */
  7923. public function display_move_item($item_id)
  7924. {
  7925. $course_id = api_get_course_int_id();
  7926. $return = '';
  7927. if (is_numeric($item_id)) {
  7928. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7929. $sql = "SELECT * FROM ".$tbl_lp_item."
  7930. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7931. $res = Database::query($sql);
  7932. $row = Database::fetch_array($res);
  7933. switch ($row['item_type']) {
  7934. case 'dir':
  7935. case 'asset':
  7936. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7937. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7938. break;
  7939. case TOOL_DOCUMENT:
  7940. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7941. $return .= $this->display_document_form('move', $item_id, $row);
  7942. break;
  7943. case TOOL_LINK:
  7944. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7945. $return .= $this->display_link_form('move', $item_id, $row);
  7946. break;
  7947. case TOOL_HOTPOTATOES:
  7948. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7949. $return .= $this->display_link_form('move', $item_id, $row);
  7950. break;
  7951. case TOOL_QUIZ:
  7952. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7953. $return .= $this->display_quiz_form('move', $item_id, $row);
  7954. break;
  7955. case TOOL_STUDENTPUBLICATION:
  7956. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7957. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7958. break;
  7959. case TOOL_FORUM:
  7960. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7961. $return .= $this->display_forum_form('move', $item_id, $row);
  7962. break;
  7963. case TOOL_THREAD:
  7964. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7965. $return .= $this->display_forum_form('move', $item_id, $row);
  7966. break;
  7967. }
  7968. }
  7969. return $return;
  7970. }
  7971. /**
  7972. * Displays a basic form on the overview page for changing the item title and the item description.
  7973. * @param string $item_type
  7974. * @param string $title
  7975. * @param array $data
  7976. * @return string
  7977. */
  7978. public function display_item_small_form($item_type, $title = '', $data = array())
  7979. {
  7980. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7981. $form = new FormValidator('small_form', 'post', $url);
  7982. $form->addElement('header', $title);
  7983. $form->addElement('text', 'title', get_lang('Title'));
  7984. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7985. $form->addElement('hidden', 'id', $data['id']);
  7986. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7987. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7988. $form->setDefaults(array('title' => $data['title']));
  7989. return $form->toHtml();
  7990. }
  7991. /**
  7992. * Return HTML form to allow prerequisites selection
  7993. * @todo use FormValidator
  7994. * @param integer Item ID
  7995. * @return string HTML form
  7996. */
  7997. public function display_item_prerequisites_form($item_id)
  7998. {
  7999. $course_id = api_get_course_int_id();
  8000. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8001. $item_id = intval($item_id);
  8002. /* Current prerequisite */
  8003. $sql = "SELECT * FROM $tbl_lp_item
  8004. WHERE c_id = $course_id AND id = ".$item_id;
  8005. $result = Database::query($sql);
  8006. $row = Database::fetch_array($result);
  8007. $prerequisiteId = $row['prerequisite'];
  8008. $return = '<legend>';
  8009. $return .= get_lang('AddEditPrerequisites');
  8010. $return .= '</legend>';
  8011. $return .= '<form method="POST">';
  8012. $return .= '<div class="table-responsive">';
  8013. $return .= '<table class="table table-hover">';
  8014. $return .= '<tr>';
  8015. $return .= '<th>'.get_lang('LearnpathPrerequisites').'</th>';
  8016. $return .= '<th width="140" >'.get_lang('Minimum').'</th>';
  8017. $return .= '<th width="140">'.get_lang('Maximum').'</th>';
  8018. $return .= '</tr>';
  8019. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8020. $return .= '<tr>';
  8021. $return .= '<td colspan="3">';
  8022. $return .= '<div class="radio learnpath"><label for="idNone">';
  8023. $return .= '<input checked="checked" id="idNone" name="prerequisites" type="radio" />';
  8024. $return .= get_lang('None').'</label>';
  8025. $return .= '</div>';
  8026. $return .= '</tr>';
  8027. $sql = "SELECT * FROM $tbl_lp_item
  8028. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8029. $result = Database::query($sql);
  8030. $arrLP = array();
  8031. $selectedMinScore = array();
  8032. $selectedMaxScore = array();
  8033. while ($row = Database::fetch_array($result)) {
  8034. if ($row['id'] == $item_id) {
  8035. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8036. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8037. }
  8038. $arrLP[] = array(
  8039. 'id' => $row['id'],
  8040. 'item_type' => $row['item_type'],
  8041. 'title' => $row['title'],
  8042. 'ref' => $row['ref'],
  8043. 'description' => $row['description'],
  8044. 'parent_item_id' => $row['parent_item_id'],
  8045. 'previous_item_id' => $row['previous_item_id'],
  8046. 'next_item_id' => $row['next_item_id'],
  8047. 'max_score' => $row['max_score'],
  8048. 'min_score' => $row['min_score'],
  8049. 'mastery_score' => $row['mastery_score'],
  8050. 'prerequisite' => $row['prerequisite'],
  8051. 'next_item_id' => $row['next_item_id'],
  8052. 'display_order' => $row['display_order'],
  8053. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  8054. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  8055. );
  8056. }
  8057. $this->tree_array($arrLP);
  8058. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  8059. unset($this->arrMenu);
  8060. for ($i = 0; $i < count($arrLP); $i++) {
  8061. $item = $arrLP[$i];
  8062. if ($item['id'] == $item_id) {
  8063. break;
  8064. }
  8065. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8066. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']] : 0;
  8067. $return .= '<tr>';
  8068. $return .= '<td '.(($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8069. $return .= '<div style="margin-left:'.($item['depth'] * 20).'px;" class="radio learnpath">';
  8070. $return .= '<label for="id'.$item['id'].'">';
  8071. $return .= '<input'.(in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '').($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ').'id="id'.$item['id'].'" name="prerequisites" type="radio" value="'.$item['id'].'" />';
  8072. $icon_name = str_replace(' ', '', $item['item_type']);
  8073. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8074. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8075. } else {
  8076. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8077. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8078. } else {
  8079. $return .= Display::return_icon('folder_document.png');
  8080. }
  8081. }
  8082. $return .= $item['title'].'</label>';
  8083. $return .= '</div>';
  8084. $return .= '</td>';
  8085. if ($item['item_type'] == TOOL_QUIZ) {
  8086. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8087. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  8088. $tmp_obj_exercice = new Exercise();
  8089. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  8090. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  8091. $tmp_obj_lp_item->update_in_bdd();
  8092. $item['max_score'] = $tmp_obj_lp_item->max_score;
  8093. $return .= '<td>';
  8094. $return .= '<input class="form-control" size="4" maxlength="3" name="min_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMinScoreValue.'" />';
  8095. $return .= '</td>';
  8096. $return .= '<td>';
  8097. $return .= '<input class="form-control" size="4" maxlength="3" name="max_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue.'" />';
  8098. $return .= '</td>';
  8099. }
  8100. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8101. $return .= '<td>';
  8102. $return .= '<center><input size="4" maxlength="3" name="min_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMinScoreValue.'" /></center>';
  8103. $return .= '</td>';
  8104. $return .= '<td>';
  8105. $return .= '<center><input size="4" maxlength="3" name="max_'.$item['id'].'" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue.'" /></center>';
  8106. $return .= '</td>';
  8107. }
  8108. $return .= '</tr>';
  8109. }
  8110. $return .= '<tr>';
  8111. $return .= '</tr>';
  8112. $return .= '</table>';
  8113. $return .= '</div>';
  8114. $return .= '<div class="form-group">';
  8115. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">'.get_lang('ModifyPrerequisites').'</button>';
  8116. $return .= '</form>';
  8117. return $return;
  8118. }
  8119. /**
  8120. * Return HTML list to allow prerequisites selection for lp
  8121. * @param integer Item ID
  8122. * @return string HTML form
  8123. */
  8124. public function display_lp_prerequisites_list()
  8125. {
  8126. $course_id = api_get_course_int_id();
  8127. $lp_id = $this->lp_id;
  8128. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8129. // get current prerequisite
  8130. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8131. $result = Database::query($sql);
  8132. $row = Database::fetch_array($result);
  8133. $prerequisiteId = $row['prerequisite'];
  8134. $session_id = api_get_session_id();
  8135. $session_condition = api_get_session_condition($session_id, true, true);
  8136. $sql = "SELECT * FROM $tbl_lp
  8137. WHERE c_id = $course_id $session_condition
  8138. ORDER BY display_order ";
  8139. $rs = Database::query($sql);
  8140. $return = '';
  8141. $return .= '<select name="prerequisites" class="form-control">';
  8142. $return .= '<option value="0">'.get_lang('None').'</option>';
  8143. if (Database::num_rows($rs) > 0) {
  8144. while ($row = Database::fetch_array($rs)) {
  8145. if ($row['id'] == $lp_id) {
  8146. continue;
  8147. }
  8148. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $prerequisiteId) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8149. }
  8150. }
  8151. $return .= '</select>';
  8152. return $return;
  8153. }
  8154. /**
  8155. * Creates a list with all the documents in it
  8156. * @param bool $showInvisibleFiles
  8157. * @return string
  8158. */
  8159. public function get_documents($showInvisibleFiles = false)
  8160. {
  8161. $course_info = api_get_course_info();
  8162. $sessionId = api_get_session_id();
  8163. $documentTree = DocumentManager::get_document_preview(
  8164. $course_info,
  8165. $this->lp_id,
  8166. null,
  8167. $sessionId,
  8168. true,
  8169. null,
  8170. null,
  8171. $showInvisibleFiles,
  8172. true
  8173. );
  8174. $headers = array(
  8175. get_lang('Files'),
  8176. get_lang('CreateTheDocument'),
  8177. get_lang('Upload')
  8178. );
  8179. $form = new FormValidator(
  8180. 'form_upload',
  8181. 'POST',
  8182. $this->getCurrentBuildingModeURL(),
  8183. '',
  8184. array('enctype' => 'multipart/form-data'),
  8185. FormValidator::LAYOUT_INLINE
  8186. );
  8187. $folders = DocumentManager::get_all_document_folders(
  8188. api_get_course_info(),
  8189. 0,
  8190. true
  8191. );
  8192. DocumentManager::build_directory_selector(
  8193. $folders,
  8194. '',
  8195. array(),
  8196. true,
  8197. $form,
  8198. 'directory_parent_id'
  8199. );
  8200. $form->addHtml('<hr>');
  8201. $group = array(
  8202. $form->createElement(
  8203. 'radio',
  8204. 'if_exists',
  8205. get_lang("UplWhatIfFileExists"),
  8206. get_lang('Yes'),
  8207. 'nothing'
  8208. ),
  8209. $form->createElement(
  8210. 'radio',
  8211. 'if_exists',
  8212. null,
  8213. get_lang('UplOverwriteLong'),
  8214. 'overwrite'
  8215. ),
  8216. $form->createElement(
  8217. 'radio',
  8218. 'if_exists',
  8219. null,
  8220. get_lang('UplRenameLong'),
  8221. 'rename'
  8222. )
  8223. );
  8224. $form->addGroup($group, null, get_lang('UplWhatIfFileExists'));
  8225. $form->addHtml('<br />');
  8226. /*$form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8227. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8228. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');*/
  8229. $form->setDefaults(['if_exists' => 'rename']);
  8230. // Check box options
  8231. $form->addElement(
  8232. 'checkbox',
  8233. 'unzip',
  8234. get_lang('Options'),
  8235. get_lang('Uncompress')
  8236. );
  8237. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8238. $form->addMultipleUpload($url);
  8239. $new = $this->display_document_form('add', 0);
  8240. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8241. return $tabs;
  8242. }
  8243. /**
  8244. * Creates a list with all the exercises (quiz) in it
  8245. * @return string
  8246. */
  8247. public function get_exercises()
  8248. {
  8249. $course_id = api_get_course_int_id();
  8250. $session_id = api_get_session_id();
  8251. $userInfo = api_get_user_info();
  8252. // New for hotpotatoes.
  8253. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8254. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  8255. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  8256. $condition_session = api_get_session_condition($session_id, true, true);
  8257. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8258. $activeCondition = ' active <> -1 ';
  8259. if ($setting) {
  8260. $activeCondition = ' active = 1 ';
  8261. }
  8262. $sql_quiz = "SELECT * FROM $tbl_quiz
  8263. WHERE c_id = $course_id AND $activeCondition $condition_session
  8264. ORDER BY title ASC";
  8265. $sql_hot = "SELECT * FROM $tbl_doc
  8266. WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session
  8267. ORDER BY id ASC";
  8268. $res_quiz = Database::query($sql_quiz);
  8269. $res_hot = Database::query($sql_hot);
  8270. $return = '<ul class="lp_resource">';
  8271. $return .= '<li class="lp_resource_element">';
  8272. $return .= Display::return_icon('new_exercice.png');
  8273. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id='.$this->lp_id.'">'.
  8274. get_lang('NewExercise').'</a>';
  8275. $return .= '</li>';
  8276. $previewIcon = Display::return_icon('preview_view.png', get_lang('Preview'));
  8277. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  8278. // Display hotpotatoes
  8279. while ($row_hot = Database::fetch_array($res_hot)) {
  8280. $link = Display::url(
  8281. $previewIcon,
  8282. $exerciseUrl.'&file='.$row_hot['path'],
  8283. ['target' => '_blank']
  8284. );
  8285. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8286. $return .= '<a class="moved" href="#">';
  8287. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8288. $return .= '</a> ';
  8289. $return .= Display::return_icon('hotpotatoes_s.png');
  8290. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_HOTPOTATOES.'&file='.$row_hot['id'].'&lp_id='.$this->lp_id.'">'.
  8291. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])).$link.'</a>';
  8292. $return .= '</li>';
  8293. }
  8294. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  8295. while ($row_quiz = Database::fetch_array($res_quiz)) {
  8296. $title = strip_tags(
  8297. api_html_entity_decode($row_quiz['title'])
  8298. );
  8299. $link = Display::url(
  8300. $previewIcon,
  8301. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  8302. ['target' => '_blank']
  8303. );
  8304. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$title.'" >';
  8305. $return .= '<a class="moved" href="#">';
  8306. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8307. $return .= '</a> ';
  8308. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8309. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  8310. $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.'">'.
  8311. Security::remove_XSS(cut($title, 80)).$link.$sessionStar.
  8312. '</a>';
  8313. $return .= '</li>';
  8314. }
  8315. $return .= '</ul>';
  8316. return $return;
  8317. }
  8318. /**
  8319. * Creates a list with all the links in it
  8320. * @return string
  8321. */
  8322. public function get_links()
  8323. {
  8324. $selfUrl = api_get_self();
  8325. $courseIdReq = api_get_cidreq();
  8326. $course = api_get_course_info();
  8327. $userInfo = api_get_user_info();
  8328. $course_id = $course['real_id'];
  8329. $tbl_link = Database::get_course_table(TABLE_LINK);
  8330. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8331. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8332. $session_id = api_get_session_id();
  8333. $condition_session = api_get_session_condition(
  8334. $session_id,
  8335. true,
  8336. true,
  8337. "link.session_id"
  8338. );
  8339. $sql = "SELECT
  8340. link.id as link_id,
  8341. link.title as link_title,
  8342. link.session_id as link_session_id,
  8343. link.category_id as category_id,
  8344. link_category.category_title as category_title
  8345. FROM $tbl_link as link
  8346. LEFT JOIN $linkCategoryTable as link_category
  8347. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8348. WHERE link.c_id = ".$course_id." $condition_session
  8349. ORDER BY link_category.category_title ASC, link.title ASC";
  8350. $result = Database::query($sql);
  8351. $categorizedLinks = array();
  8352. $categories = array();
  8353. while ($link = Database::fetch_array($result)) {
  8354. if (!$link['category_id']) {
  8355. $link['category_title'] = get_lang('Uncategorized');
  8356. }
  8357. $categories[$link['category_id']] = $link['category_title'];
  8358. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  8359. }
  8360. $linksHtmlCode =
  8361. '<script>
  8362. function toggle_tool(tool, id){
  8363. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8364. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8365. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8366. } else {
  8367. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8368. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8369. }
  8370. }
  8371. </script>
  8372. <ul class="lp_resource">
  8373. <li class="lp_resource_element">
  8374. '.Display::return_icon('linksnew.gif').'
  8375. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8376. get_lang('LinkAdd').'
  8377. </a>
  8378. </li>';
  8379. foreach ($categorizedLinks as $categoryId => $links) {
  8380. $linkNodes = null;
  8381. foreach ($links as $key => $linkInfo) {
  8382. $title = $linkInfo['link_title'];
  8383. $linkSessionId = $linkInfo['link_session_id'];
  8384. $link = Display::url(
  8385. Display::return_icon('preview_view.png', get_lang('Preview')),
  8386. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8387. ['target' => '_blank']
  8388. );
  8389. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8390. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8391. $linkNodes .=
  8392. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8393. <a class="moved" href="#">'.
  8394. $moveEverywhereIcon.
  8395. '</a>
  8396. '.Display::return_icon('lp_link.png').'
  8397. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8398. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8399. Security::remove_XSS($title).$sessionStar.$link.
  8400. '</a>
  8401. </li>';
  8402. }
  8403. }
  8404. $linksHtmlCode .=
  8405. '<li>
  8406. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8407. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8408. align="absbottom" />
  8409. </a>
  8410. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8411. </li>
  8412. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8413. }
  8414. $linksHtmlCode .= '</ul>';
  8415. return $linksHtmlCode;
  8416. }
  8417. /**
  8418. * Creates a list with all the student publications in it
  8419. * @return string
  8420. */
  8421. public function get_student_publications()
  8422. {
  8423. $return = '<ul class="lp_resource">';
  8424. $return .= '<li class="lp_resource_element">';
  8425. $return .= Display::return_icon('works_new.gif');
  8426. $return .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_STUDENTPUBLICATION.'&lp_id='.$this->lp_id.'">'.
  8427. get_lang('AddAssignmentPage').'</a>';
  8428. $return .= '</li>';
  8429. $sessionId = api_get_session_id();
  8430. if (empty($sessionId)) {
  8431. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8432. $works = getWorkListTeacher(0, 100, null, null, null);
  8433. if (!empty($works)) {
  8434. foreach ($works as $work) {
  8435. $link = Display::url(
  8436. Display::return_icon('preview_view.png', get_lang('Preview')),
  8437. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8438. ['target' => '_blank']
  8439. );
  8440. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8441. $return .= '<a class="moved" href="#">';
  8442. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8443. $return .= '</a> ';
  8444. $return .= Display::return_icon('works.gif');
  8445. $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.'">'.
  8446. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8447. </a>';
  8448. $return .= '</li>';
  8449. }
  8450. }
  8451. }
  8452. $return .= '</ul>';
  8453. return $return;
  8454. }
  8455. /**
  8456. * Creates a list with all the forums in it
  8457. * @return string
  8458. */
  8459. public function get_forums()
  8460. {
  8461. require_once '../forum/forumfunction.inc.php';
  8462. require_once '../forum/forumconfig.inc.php';
  8463. $forumCategories = get_forum_categories();
  8464. $forumsInNoCategory = get_forums_in_category(0);
  8465. if (!empty($forumsInNoCategory)) {
  8466. $forumCategories = array_merge(
  8467. $forumCategories,
  8468. array(
  8469. array(
  8470. 'cat_id' => 0,
  8471. 'session_id' => 0,
  8472. 'visibility' => 1,
  8473. 'cat_comment' => null,
  8474. ),
  8475. )
  8476. );
  8477. }
  8478. $forumList = get_forums();
  8479. $a_forums = [];
  8480. foreach ($forumCategories as $forumCategory) {
  8481. // The forums in this category.
  8482. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8483. if (!empty($forumsInCategory)) {
  8484. foreach ($forumList as $forum) {
  8485. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8486. $a_forums[] = $forum;
  8487. }
  8488. }
  8489. }
  8490. }
  8491. $return = '<ul class="lp_resource">';
  8492. // First add link
  8493. $return .= '<li class="lp_resource_element">';
  8494. $return .= Display::return_icon('new_forum.png');
  8495. $return .= Display::url(
  8496. get_lang('CreateANewForum'),
  8497. api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&'.http_build_query([
  8498. 'action' => 'add',
  8499. 'content' => 'forum',
  8500. 'lp_id' => $this->lp_id
  8501. ]),
  8502. ['title' => get_lang('CreateANewForum')]
  8503. );
  8504. $return .= '</li>';
  8505. $return .= '<script>
  8506. function toggle_forum(forum_id) {
  8507. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8508. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8509. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8510. } else {
  8511. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8512. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8513. }
  8514. }
  8515. </script>';
  8516. foreach ($a_forums as $forum) {
  8517. if (!empty($forum['forum_id'])) {
  8518. $link = Display::url(
  8519. Display::return_icon('preview_view.png', get_lang('Preview')),
  8520. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8521. ['target' => '_blank']
  8522. );
  8523. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8524. $return .= '<a class="moved" href="#">';
  8525. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8526. $return .= ' </a>';
  8527. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8528. $return .= '<a onclick="javascript:toggle_forum('.$forum['forum_id'].');" style="cursor:hand; vertical-align:middle">
  8529. <img src="' . Display::returnIconPath('add.gif').'" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8530. </a>
  8531. <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">'.
  8532. Security::remove_XSS($forum['forum_title']).' '.$link.'</a>';
  8533. $return .= '</li>';
  8534. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8535. $a_threads = get_threads($forum['forum_id']);
  8536. if (is_array($a_threads)) {
  8537. foreach ($a_threads as $thread) {
  8538. $link = Display::url(
  8539. Display::return_icon('preview_view.png', get_lang('Preview')),
  8540. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8541. ['target' => '_blank']
  8542. );
  8543. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8544. $return .= '&nbsp;<a class="moved" href="#">';
  8545. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8546. $return .= ' </a>';
  8547. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8548. $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.'">'.
  8549. Security::remove_XSS($thread['thread_title']).' '.$link.'</a>';
  8550. $return .= '</li>';
  8551. }
  8552. }
  8553. $return .= '</div>';
  8554. }
  8555. }
  8556. $return .= '</ul>';
  8557. return $return;
  8558. }
  8559. /**
  8560. * // TODO: The output encoding should be equal to the system encoding.
  8561. *
  8562. * Exports the learning path as a SCORM package. This is the main function that
  8563. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8564. * whole thing and returns the zip.
  8565. *
  8566. * This method needs to be called in PHP5, as it will fail with non-adequate
  8567. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8568. * you need to call it on a learnpath object.
  8569. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8570. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8571. * path has been modified, it should use the generic method here below.
  8572. * @TODO link this function with the export_lp() function in the same class
  8573. * @param string Optional name of zip file. If none, title of learnpath is
  8574. * domesticated and trailed with ".zip"
  8575. * @return string Returns the zip package string, or null if error
  8576. */
  8577. public function scorm_export()
  8578. {
  8579. $_course = api_get_course_info();
  8580. $course_id = $_course['real_id'];
  8581. // Remove memory and time limits as much as possible as this might be a long process...
  8582. if (function_exists('ini_set')) {
  8583. api_set_memory_limit('256M');
  8584. ini_set('max_execution_time', 600);
  8585. }
  8586. // Create the zip handler (this will remain available throughout the method).
  8587. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8588. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8589. $temp_dir_short = uniqid();
  8590. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8591. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8592. $zip_folder = new PclZip($temp_zip_file);
  8593. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8594. $root_path = $main_path = api_get_path(SYS_PATH);
  8595. $files_cleanup = array();
  8596. // Place to temporarily stash the zip file.
  8597. // create the temp dir if it doesn't exist
  8598. // or do a cleanup before creating the zip file.
  8599. if (!is_dir($temp_zip_dir)) {
  8600. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8601. } else {
  8602. // Cleanup: Check the temp dir for old files and delete them.
  8603. $handle = opendir($temp_zip_dir);
  8604. while (false !== ($file = readdir($handle))) {
  8605. if ($file != '.' && $file != '..') {
  8606. unlink("$temp_zip_dir/$file");
  8607. }
  8608. }
  8609. closedir($handle);
  8610. }
  8611. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8612. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8613. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8614. ) {
  8615. // Remove the possible . at the end of the path.
  8616. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8617. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8618. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8619. copyr(
  8620. $current_course_path.'/scorm/'.$this->path,
  8621. $dest_path_to_scorm_folder,
  8622. array('imsmanifest'),
  8623. $zip_files
  8624. );
  8625. }
  8626. // Build a dummy imsmanifest structure.
  8627. // Do not add to the zip yet (we still need it).
  8628. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8629. // Aggregation Model official document, section "2.3 Content Packaging".
  8630. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8631. $xmldoc = new DOMDocument('1.0');
  8632. $root = $xmldoc->createElement('manifest');
  8633. $root->setAttribute('identifier', 'SingleCourseManifest');
  8634. $root->setAttribute('version', '1.1');
  8635. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8636. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8637. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8638. $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');
  8639. // Build mandatory sub-root container elements.
  8640. $metadata = $xmldoc->createElement('metadata');
  8641. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8642. $metadata->appendChild($md_schema);
  8643. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8644. $metadata->appendChild($md_schemaversion);
  8645. $root->appendChild($metadata);
  8646. $organizations = $xmldoc->createElement('organizations');
  8647. $resources = $xmldoc->createElement('resources');
  8648. // Build the only organization we will use in building our learnpaths.
  8649. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8650. $organization = $xmldoc->createElement('organization');
  8651. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8652. // To set the title of the SCORM entity (=organization), we take the name given
  8653. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8654. // learning path charset) as it is the encoding that defines how it is stored
  8655. // in the database. Then we convert it to HTML entities again as the "&" character
  8656. // alone is not authorized in XML (must be &amp;).
  8657. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8658. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8659. $organization->appendChild($org_title);
  8660. $folder_name = 'document';
  8661. // Removes the learning_path/scorm_folder path when exporting see #4841
  8662. $path_to_remove = null;
  8663. $result = $this->generate_lp_folder($_course);
  8664. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8665. $path_to_remove = 'document'.$result['dir'];
  8666. $path_to_replace = $folder_name.'/';
  8667. }
  8668. // Fixes chamilo scorm exports
  8669. if ($this->ref === 'chamilo_scorm_export') {
  8670. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8671. }
  8672. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8673. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8674. $link_updates = array();
  8675. $links_to_create = array();
  8676. //foreach ($this->items as $index => $item) {
  8677. foreach ($this->ordered_items as $index => $itemId) {
  8678. $item = $this->items[$itemId];
  8679. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8680. // Get included documents from this item.
  8681. if ($item->type === 'sco') {
  8682. $inc_docs = $item->get_resources_from_source(
  8683. null,
  8684. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8685. );
  8686. } else {
  8687. $inc_docs = $item->get_resources_from_source();
  8688. }
  8689. // Give a child element <item> to the <organization> element.
  8690. $my_item_id = $item->get_id();
  8691. $my_item = $xmldoc->createElement('item');
  8692. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8693. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8694. $my_item->setAttribute('isvisible', 'true');
  8695. // Give a child element <title> to the <item> element.
  8696. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8697. $my_item->appendChild($my_title);
  8698. // Give a child element <adlcp:prerequisites> to the <item> element.
  8699. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8700. $my_prereqs->setAttribute('type', 'aicc_script');
  8701. $my_item->appendChild($my_prereqs);
  8702. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8703. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8704. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8705. //$xmldoc->createElement('adlcp:timelimitaction','');
  8706. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8707. //$xmldoc->createElement('adlcp:datafromlms','');
  8708. // Give a child element <adlcp:masteryscore> to the <item> element.
  8709. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8710. $my_item->appendChild($my_masteryscore);
  8711. // Attach this item to the organization element or hits parent if there is one.
  8712. if (!empty($item->parent) && $item->parent != 0) {
  8713. $children = $organization->childNodes;
  8714. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8715. if (is_object($possible_parent)) {
  8716. $possible_parent->appendChild($my_item);
  8717. } else {
  8718. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8719. }
  8720. } else {
  8721. if ($this->debug > 0) { error_log('No parent'); }
  8722. $organization->appendChild($my_item);
  8723. }
  8724. // Get the path of the file(s) from the course directory root.
  8725. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8726. if (!empty($path_to_remove)) {
  8727. //From docs
  8728. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8729. //From quiz
  8730. if ($this->ref === 'chamilo_scorm_export') {
  8731. $path_to_remove = 'scorm/'.$this->path.'/';
  8732. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8733. }
  8734. } else {
  8735. $my_xml_file_path = $my_file_path;
  8736. }
  8737. $my_sub_dir = dirname($my_file_path);
  8738. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8739. $my_xml_sub_dir = $my_sub_dir;
  8740. // Give a <resource> child to the <resources> element
  8741. $my_resource = $xmldoc->createElement('resource');
  8742. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8743. $my_resource->setAttribute('type', 'webcontent');
  8744. $my_resource->setAttribute('href', $my_xml_file_path);
  8745. // adlcp:scormtype can be either 'sco' or 'asset'.
  8746. if ($item->type === 'sco') {
  8747. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8748. } else {
  8749. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8750. }
  8751. // xml:base is the base directory to find the files declared in this resource.
  8752. $my_resource->setAttribute('xml:base', '');
  8753. // Give a <file> child to the <resource> element.
  8754. $my_file = $xmldoc->createElement('file');
  8755. $my_file->setAttribute('href', $my_xml_file_path);
  8756. $my_resource->appendChild($my_file);
  8757. // Dependency to other files - not yet supported.
  8758. $i = 1;
  8759. if ($inc_docs)
  8760. foreach ($inc_docs as $doc_info) {
  8761. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8762. continue;
  8763. }
  8764. $my_dep = $xmldoc->createElement('resource');
  8765. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8766. $my_dep->setAttribute('identifier', $res_id);
  8767. $my_dep->setAttribute('type', 'webcontent');
  8768. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8769. $my_dep_file = $xmldoc->createElement('file');
  8770. // Check type of URL.
  8771. if ($doc_info[1] == 'remote') {
  8772. // Remote file. Save url as is.
  8773. $my_dep_file->setAttribute('href', $doc_info[0]);
  8774. $my_dep->setAttribute('xml:base', '');
  8775. } elseif ($doc_info[1] === 'local') {
  8776. switch ($doc_info[2]) {
  8777. case 'url': // Local URL - save path as url for now, don't zip file.
  8778. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8779. $current_dir = dirname($abs_path);
  8780. $current_dir = str_replace('\\', '/', $current_dir);
  8781. $file_path = realpath($abs_path);
  8782. $file_path = str_replace('\\', '/', $file_path);
  8783. $my_dep_file->setAttribute('href', $file_path);
  8784. $my_dep->setAttribute('xml:base', '');
  8785. if (strstr($file_path, $main_path) !== false) {
  8786. // The calculated real path is really inside Chamilo's root path.
  8787. // Reduce file path to what's under the DocumentRoot.
  8788. $file_path = substr($file_path, strlen($root_path) - 1);
  8789. //echo $file_path;echo '<br /><br />';
  8790. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8791. $zip_files_abs[] = $file_path;
  8792. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8793. $my_dep_file->setAttribute('href', $file_path);
  8794. $my_dep->setAttribute('xml:base', '');
  8795. } elseif (empty($file_path)) {
  8796. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8797. if (strpos($document_root, -1) == '/') {
  8798. $document_root = substr(0, -1, $document_root);
  8799. }*/
  8800. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8801. $file_path = str_replace('//', '/', $file_path);
  8802. if (file_exists($file_path)) {
  8803. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8804. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8805. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8806. $my_dep_file->setAttribute('href', $file_path);
  8807. $my_dep->setAttribute('xml:base', '');
  8808. }
  8809. }
  8810. break;
  8811. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8812. $my_dep_file->setAttribute('href', $doc_info[0]);
  8813. $my_dep->setAttribute('xml:base', '');
  8814. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8815. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8816. $abs_img_path_without_subdir = $doc_info[0];
  8817. $relp = api_get_path(REL_PATH); // The url-append config param.
  8818. $pos = strpos($abs_img_path_without_subdir, $relp);
  8819. if ($pos === 0) {
  8820. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8821. }
  8822. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8823. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8824. $file_path = str_replace('\\', '/', $file_path);
  8825. $file_path = str_replace('//', '/', $file_path);
  8826. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8827. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8828. // Check if the current document is in that path.
  8829. if (strstr($file_path, $cur_path) !== false) {
  8830. // The document is in that path, now get the relative path
  8831. // to the containing document.
  8832. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8833. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8834. $relative_path = '';
  8835. if (strstr($file_path, $cur_path) !== false) {
  8836. //$relative_path = substr($file_path, strlen($orig_file_path));
  8837. $relative_path = str_replace($cur_path, '', $file_path);
  8838. $file_path = substr($file_path, strlen($cur_path));
  8839. } else {
  8840. // This case is still a problem as it's difficult to calculate a relative path easily
  8841. // might still generate wrong links.
  8842. //$file_path = substr($file_path,strlen($cur_path));
  8843. // Calculate the directory path to the current file (without trailing slash).
  8844. $my_relative_path = dirname($file_path);
  8845. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8846. $my_relative_file = basename($file_path);
  8847. // Calculate the directory path to the containing file (without trailing slash).
  8848. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8849. $dotdot = '';
  8850. $subdir = '';
  8851. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8852. $my_relative_path2 = dirname($my_relative_path);
  8853. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8854. $my_orig_file_path = dirname($my_orig_file_path);
  8855. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8856. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8857. $dotdot += '../';
  8858. $my_relative_path = $my_relative_path2;
  8859. }
  8860. $relative_path = $dotdot.$subdir.$my_relative_file;
  8861. }
  8862. // Put the current document in the zip (this array is the array
  8863. // that will manage documents already in the course folder - relative).
  8864. $zip_files[] = $file_path;
  8865. // Update the links to the current document in the containing document (make them relative).
  8866. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8867. $my_dep_file->setAttribute('href', $file_path);
  8868. $my_dep->setAttribute('xml:base', '');
  8869. } elseif (strstr($file_path, $main_path) !== false) {
  8870. // The calculated real path is really inside Chamilo's root path.
  8871. // Reduce file path to what's under the DocumentRoot.
  8872. $file_path = substr($file_path, strlen($root_path));
  8873. //echo $file_path;echo '<br /><br />';
  8874. //error_log('Reduced path: '.$file_path, 0);
  8875. $zip_files_abs[] = $file_path;
  8876. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8877. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8878. $my_dep->setAttribute('xml:base', '');
  8879. } elseif (empty($file_path)) {
  8880. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8881. if(strpos($document_root,-1) == '/') {
  8882. $document_root = substr(0, -1, $document_root);
  8883. }*/
  8884. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8885. $file_path = str_replace('//', '/', $file_path);
  8886. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8887. $current_dir = dirname($abs_path);
  8888. $current_dir = str_replace('\\', '/', $current_dir);
  8889. if (file_exists($file_path)) {
  8890. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8891. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8892. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8893. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8894. $my_dep->setAttribute('xml:base', '');
  8895. }
  8896. }
  8897. break;
  8898. case 'rel':
  8899. // Path relative to the current document.
  8900. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8901. if (substr($doc_info[0], 0, 2) == '..') {
  8902. // Relative path going up.
  8903. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8904. $current_dir = str_replace('\\', '/', $current_dir);
  8905. $file_path = realpath($current_dir.$doc_info[0]);
  8906. $file_path = str_replace('\\', '/', $file_path);
  8907. //error_log($file_path.' <-> '.$main_path,0);
  8908. if (strstr($file_path, $main_path) !== false) {
  8909. // The calculated real path is really inside Chamilo's root path.
  8910. // Reduce file path to what's under the DocumentRoot.
  8911. $file_path = substr($file_path, strlen($root_path));
  8912. //error_log('Reduced path: '.$file_path, 0);
  8913. $zip_files_abs[] = $file_path;
  8914. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8915. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8916. $my_dep->setAttribute('xml:base', '');
  8917. }
  8918. } else {
  8919. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8920. $my_dep_file->setAttribute('href', $doc_info[0]);
  8921. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8922. }
  8923. break;
  8924. default:
  8925. $my_dep_file->setAttribute('href', $doc_info[0]);
  8926. $my_dep->setAttribute('xml:base', '');
  8927. break;
  8928. }
  8929. }
  8930. $my_dep->appendChild($my_dep_file);
  8931. $resources->appendChild($my_dep);
  8932. $dependency = $xmldoc->createElement('dependency');
  8933. $dependency->setAttribute('identifierref', $res_id);
  8934. $my_resource->appendChild($dependency);
  8935. $i++;
  8936. }
  8937. $resources->appendChild($my_resource);
  8938. $zip_files[] = $my_file_path;
  8939. } else {
  8940. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8941. switch ($item->type) {
  8942. case TOOL_LINK:
  8943. $my_item = $xmldoc->createElement('item');
  8944. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8945. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8946. $my_item->setAttribute('isvisible', 'true');
  8947. // Give a child element <title> to the <item> element.
  8948. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8949. $my_item->appendChild($my_title);
  8950. // Give a child element <adlcp:prerequisites> to the <item> element.
  8951. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8952. $my_prereqs->setAttribute('type', 'aicc_script');
  8953. $my_item->appendChild($my_prereqs);
  8954. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8955. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8956. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8957. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8958. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8959. //$xmldoc->createElement('adlcp:datafromlms', '');
  8960. // Give a child element <adlcp:masteryscore> to the <item> element.
  8961. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8962. $my_item->appendChild($my_masteryscore);
  8963. // Attach this item to the organization element or its parent if there is one.
  8964. if (!empty($item->parent) && $item->parent != 0) {
  8965. $children = $organization->childNodes;
  8966. for ($i = 0; $i < $children->length; $i++) {
  8967. $item_temp = $children->item($i);
  8968. if ($item_temp -> nodeName == 'item') {
  8969. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8970. $item_temp -> appendChild($my_item);
  8971. }
  8972. }
  8973. }
  8974. } else {
  8975. $organization->appendChild($my_item);
  8976. }
  8977. $my_file_path = 'link_'.$item->get_id().'.html';
  8978. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  8979. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8980. $rs = Database::query($sql);
  8981. if ($link = Database::fetch_array($rs)) {
  8982. $url = $link['url'];
  8983. $title = stripslashes($link['title']);
  8984. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8985. $my_xml_file_path = $my_file_path;
  8986. $my_sub_dir = dirname($my_file_path);
  8987. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8988. $my_xml_sub_dir = $my_sub_dir;
  8989. // Give a <resource> child to the <resources> element.
  8990. $my_resource = $xmldoc->createElement('resource');
  8991. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8992. $my_resource->setAttribute('type', 'webcontent');
  8993. $my_resource->setAttribute('href', $my_xml_file_path);
  8994. // adlcp:scormtype can be either 'sco' or 'asset'.
  8995. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8996. // xml:base is the base directory to find the files declared in this resource.
  8997. $my_resource->setAttribute('xml:base', '');
  8998. // give a <file> child to the <resource> element.
  8999. $my_file = $xmldoc->createElement('file');
  9000. $my_file->setAttribute('href', $my_xml_file_path);
  9001. $my_resource->appendChild($my_file);
  9002. $resources->appendChild($my_resource);
  9003. }
  9004. break;
  9005. case TOOL_QUIZ:
  9006. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  9007. $exe = new Exercise();
  9008. $exe->read($exe_id);
  9009. $my_item = $xmldoc->createElement('item');
  9010. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  9011. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  9012. $my_item->setAttribute('isvisible', 'true');
  9013. // Give a child element <title> to the <item> element.
  9014. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  9015. $my_item->appendChild($my_title);
  9016. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  9017. //$my_item->appendChild($my_max_score);
  9018. // Give a child element <adlcp:prerequisites> to the <item> element.
  9019. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  9020. $my_prereqs->setAttribute('type', 'aicc_script');
  9021. $my_item->appendChild($my_prereqs);
  9022. // Give a child element <adlcp:masteryscore> to the <item> element.
  9023. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  9024. $my_item->appendChild($my_masteryscore);
  9025. // Attach this item to the organization element or hits parent if there is one.
  9026. if (!empty($item->parent) && $item->parent != 0) {
  9027. $children = $organization->childNodes;
  9028. /*for ($i = 0; $i < $children->length; $i++) {
  9029. $item_temp = $children->item($i);
  9030. if ($exe_id == 81) {
  9031. error_log($item_temp->nodeName );
  9032. error_log($item_temp->getAttribute('identifier'));
  9033. }
  9034. if ($item_temp->nodeName == 'item') {
  9035. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9036. $item_temp->appendChild($my_item);
  9037. }
  9038. }
  9039. }*/
  9040. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  9041. if ($possible_parent) {
  9042. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  9043. $possible_parent->appendChild($my_item);
  9044. }
  9045. }
  9046. } else {
  9047. $organization->appendChild($my_item);
  9048. }
  9049. // Get the path of the file(s) from the course directory root
  9050. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  9051. $my_file_path = 'quiz_'.$item->get_id().'.html';
  9052. // Write the contents of the exported exercise into a (big) html file
  9053. // to later pack it into the exported SCORM. The file will be removed afterwards.
  9054. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  9055. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  9056. $res = file_put_contents($tmp_file_path, $contents);
  9057. if ($res === false) {
  9058. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  9059. }
  9060. $files_cleanup[] = $tmp_file_path;
  9061. $my_xml_file_path = $my_file_path;
  9062. $my_sub_dir = dirname($my_file_path);
  9063. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9064. $my_xml_sub_dir = $my_sub_dir;
  9065. // Give a <resource> child to the <resources> element.
  9066. $my_resource = $xmldoc->createElement('resource');
  9067. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9068. $my_resource->setAttribute('type', 'webcontent');
  9069. $my_resource->setAttribute('href', $my_xml_file_path);
  9070. // adlcp:scormtype can be either 'sco' or 'asset'.
  9071. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9072. // xml:base is the base directory to find the files declared in this resource.
  9073. $my_resource->setAttribute('xml:base', '');
  9074. // Give a <file> child to the <resource> element.
  9075. $my_file = $xmldoc->createElement('file');
  9076. $my_file->setAttribute('href', $my_xml_file_path);
  9077. $my_resource->appendChild($my_file);
  9078. // Get included docs.
  9079. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  9080. // Dependency to other files - not yet supported.
  9081. $i = 1;
  9082. foreach ($inc_docs as $doc_info) {
  9083. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  9084. $my_dep = $xmldoc->createElement('resource');
  9085. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9086. $my_dep->setAttribute('identifier', $res_id);
  9087. $my_dep->setAttribute('type', 'webcontent');
  9088. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9089. $my_dep_file = $xmldoc->createElement('file');
  9090. // Check type of URL.
  9091. if ($doc_info[1] == 'remote') {
  9092. // Remote file. Save url as is.
  9093. $my_dep_file->setAttribute('href', $doc_info[0]);
  9094. $my_dep->setAttribute('xml:base', '');
  9095. } elseif ($doc_info[1] == 'local') {
  9096. switch ($doc_info[2]) {
  9097. case 'url': // Local URL - save path as url for now, don't zip file.
  9098. // Save file but as local file (retrieve from URL).
  9099. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9100. $current_dir = dirname($abs_path);
  9101. $current_dir = str_replace('\\', '/', $current_dir);
  9102. $file_path = realpath($abs_path);
  9103. $file_path = str_replace('\\', '/', $file_path);
  9104. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9105. $my_dep->setAttribute('xml:base', '');
  9106. if (strstr($file_path, $main_path) !== false) {
  9107. // The calculated real path is really inside the chamilo root path.
  9108. // Reduce file path to what's under the DocumentRoot.
  9109. $file_path = substr($file_path, strlen($root_path));
  9110. $zip_files_abs[] = $file_path;
  9111. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9112. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9113. $my_dep->setAttribute('xml:base', '');
  9114. } elseif (empty($file_path)) {
  9115. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  9116. if (strpos($document_root,-1) == '/') {
  9117. $document_root = substr(0, -1, $document_root);
  9118. }*/
  9119. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9120. $file_path = str_replace('//', '/', $file_path);
  9121. if (file_exists($file_path)) {
  9122. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9123. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9124. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9125. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9126. $my_dep->setAttribute('xml:base', '');
  9127. }
  9128. }
  9129. break;
  9130. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9131. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9132. $current_dir = str_replace('\\', '/', $current_dir);
  9133. $file_path = realpath($doc_info[0]);
  9134. $file_path = str_replace('\\', '/', $file_path);
  9135. $my_dep_file->setAttribute('href', $file_path);
  9136. $my_dep->setAttribute('xml:base', '');
  9137. if (strstr($file_path, $main_path) !== false) {
  9138. // The calculated real path is really inside the chamilo root path.
  9139. // Reduce file path to what's under the DocumentRoot.
  9140. $file_path = substr($file_path, strlen($root_path));
  9141. //echo $file_path;echo '<br /><br />';
  9142. //error_log('Reduced path: '.$file_path, 0);
  9143. $zip_files_abs[] = $file_path;
  9144. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9145. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9146. $my_dep->setAttribute('xml:base', '');
  9147. } elseif (empty($file_path)) {
  9148. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9149. if (strpos($document_root,-1) == '/') {
  9150. $document_root = substr(0, -1, $document_root);
  9151. }*/
  9152. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9153. $file_path = str_replace('//', '/', $file_path);
  9154. if (file_exists($file_path)) {
  9155. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9156. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9157. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9158. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9159. $my_dep->setAttribute('xml:base', '');
  9160. }
  9161. }
  9162. break;
  9163. 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
  9164. if (substr($doc_info[0], 0, 2) == '..') {
  9165. // Relative path going up.
  9166. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9167. $current_dir = str_replace('\\', '/', $current_dir);
  9168. $file_path = realpath($current_dir.$doc_info[0]);
  9169. $file_path = str_replace('\\', '/', $file_path);
  9170. //error_log($file_path.' <-> '.$main_path, 0);
  9171. if (strstr($file_path, $main_path) !== false) {
  9172. // The calculated real path is really inside Chamilo's root path.
  9173. // Reduce file path to what's under the DocumentRoot.
  9174. $file_path = substr($file_path, strlen($root_path));
  9175. $file_path_dest = $file_path;
  9176. // File path is courses/CHAMILO/document/....
  9177. $info_file_path = explode('/', $file_path);
  9178. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9179. $file_path_dest = 'document/'.$file_path;
  9180. }
  9181. //error_log('Reduced path: '.$file_path, 0);
  9182. $zip_files_abs[] = $file_path;
  9183. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  9184. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9185. $my_dep->setAttribute('xml:base', '');
  9186. }
  9187. } else {
  9188. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9189. $my_dep_file->setAttribute('href', $doc_info[0]);
  9190. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9191. }
  9192. break;
  9193. default:
  9194. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9195. $my_dep->setAttribute('xml:base', '');
  9196. break;
  9197. }
  9198. }
  9199. $my_dep->appendChild($my_dep_file);
  9200. $resources->appendChild($my_dep);
  9201. $dependency = $xmldoc->createElement('dependency');
  9202. $dependency->setAttribute('identifierref', $res_id);
  9203. $my_resource->appendChild($dependency);
  9204. $i++;
  9205. }
  9206. $resources->appendChild($my_resource);
  9207. $zip_files[] = $my_file_path;
  9208. break;
  9209. default:
  9210. // Get the path of the file(s) from the course directory root
  9211. $my_file_path = 'non_exportable.html';
  9212. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9213. $my_xml_file_path = $my_file_path;
  9214. $my_sub_dir = dirname($my_file_path);
  9215. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9216. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9217. $my_xml_sub_dir = $my_sub_dir;
  9218. // Give a <resource> child to the <resources> element.
  9219. $my_resource = $xmldoc->createElement('resource');
  9220. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9221. $my_resource->setAttribute('type', 'webcontent');
  9222. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9223. // adlcp:scormtype can be either 'sco' or 'asset'.
  9224. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9225. // xml:base is the base directory to find the files declared in this resource.
  9226. $my_resource->setAttribute('xml:base', '');
  9227. // Give a <file> child to the <resource> element.
  9228. $my_file = $xmldoc->createElement('file');
  9229. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9230. $my_resource->appendChild($my_file);
  9231. $resources->appendChild($my_resource);
  9232. break;
  9233. }
  9234. }
  9235. }
  9236. $organizations->appendChild($organization);
  9237. $root->appendChild($organizations);
  9238. $root->appendChild($resources);
  9239. $xmldoc->appendChild($root);
  9240. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  9241. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9242. // then add the file to the zip, then destroy the file (this is done automatically).
  9243. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9244. foreach ($zip_files as $file_path) {
  9245. if (empty($file_path)) {
  9246. continue;
  9247. }
  9248. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  9249. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9250. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9251. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9252. }
  9253. $this->create_path($dest_file);
  9254. @copy($filePath, $dest_file);
  9255. // Check if the file needs a link update.
  9256. if (in_array($file_path, array_keys($link_updates))) {
  9257. $string = file_get_contents($dest_file);
  9258. unlink($dest_file);
  9259. foreach ($link_updates[$file_path] as $old_new) {
  9260. // This is an ugly hack that allows .flv files to be found by the flv player that
  9261. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9262. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9263. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9264. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9265. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9266. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9267. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9268. }
  9269. //Fix to avoid problems with default_course_document
  9270. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9271. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9272. $newDestination = $old_new['dest'];
  9273. } else {
  9274. $newDestination = str_replace('document/', '', $old_new['dest']);
  9275. }
  9276. $string = str_replace($old_new['orig'], $newDestination, $string);
  9277. // Add files inside the HTMLs
  9278. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9279. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9280. if (file_exists($sys_course_path.$new_path)) {
  9281. copy($sys_course_path.$new_path, $destinationFile);
  9282. }
  9283. }
  9284. file_put_contents($dest_file, $string);
  9285. }
  9286. if (file_exists($filePath) && $copyAll) {
  9287. $extension = $this->get_extension($filePath);
  9288. if (in_array($extension, ['html', 'html'])) {
  9289. $containerOrigin = dirname($filePath);
  9290. $containerDestination = dirname($dest_file);
  9291. $finder = new Finder();
  9292. $finder->files()->in($containerOrigin)
  9293. ->notName('*_DELETED_*')
  9294. ->exclude('share_folder')
  9295. ->exclude('chat_files')
  9296. ->exclude('certificates')
  9297. ;
  9298. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9299. $fs = new Filesystem();
  9300. $fs->mirror(
  9301. $containerOrigin,
  9302. $containerDestination,
  9303. $finder
  9304. );
  9305. }
  9306. }
  9307. }
  9308. }
  9309. foreach ($zip_files_abs as $file_path) {
  9310. if (empty($file_path)) {
  9311. continue;
  9312. }
  9313. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9314. continue;
  9315. }
  9316. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9317. $this->create_path($dest_file);
  9318. copy($main_path.$file_path, $dest_file);
  9319. // Check if the file needs a link update.
  9320. if (in_array($file_path, array_keys($link_updates))) {
  9321. $string = file_get_contents($dest_file);
  9322. unlink($dest_file);
  9323. foreach ($link_updates[$file_path] as $old_new) {
  9324. // This is an ugly hack that allows .flv files to be found by the flv player that
  9325. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9326. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9327. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9328. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9329. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9330. }
  9331. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9332. }
  9333. file_put_contents($dest_file, $string);
  9334. }
  9335. }
  9336. if (is_array($links_to_create)) {
  9337. foreach ($links_to_create as $file => $link) {
  9338. $file_content = '<!DOCTYPE html><head>
  9339. <meta charset="'.api_get_language_isocode().'" />
  9340. <title>'.$link['title'].'</title>
  9341. </head>
  9342. <body dir="'.api_get_text_direction().'">
  9343. <div style="text-align:center">
  9344. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9345. </body>
  9346. </html>';
  9347. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9348. }
  9349. }
  9350. // Add non exportable message explanation.
  9351. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9352. $file_content = '<!DOCTYPE html><head>
  9353. <meta charset="'.api_get_language_isocode().'" />
  9354. <title>'.$lang_not_exportable.'</title>
  9355. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9356. </head>
  9357. <body dir="'.api_get_text_direction().'">';
  9358. $file_content .=
  9359. <<<EOD
  9360. <style>
  9361. .error-message {
  9362. font-family: arial, verdana, helvetica, sans-serif;
  9363. border-width: 1px;
  9364. border-style: solid;
  9365. left: 50%;
  9366. margin: 10px auto;
  9367. min-height: 30px;
  9368. padding: 5px;
  9369. right: 50%;
  9370. width: 500px;
  9371. background-color: #FFD1D1;
  9372. border-color: #FF0000;
  9373. color: #000;
  9374. }
  9375. </style>
  9376. <body>
  9377. <div class="error-message">
  9378. $lang_not_exportable
  9379. </div>
  9380. </body>
  9381. </html>
  9382. EOD;
  9383. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9384. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9385. }
  9386. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9387. // Add the extra files that go along with a SCORM package.
  9388. $main_code_path = api_get_path(SYS_CODE_PATH).'lp/packaging/';
  9389. $fs = new Filesystem();
  9390. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9391. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9392. $manifest = @$xmldoc->saveXML();
  9393. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9394. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9395. $zip_folder->add(
  9396. $archive_path.'/'.$temp_dir_short,
  9397. PCLZIP_OPT_REMOVE_PATH,
  9398. $archive_path.'/'.$temp_dir_short.'/'
  9399. );
  9400. // Clean possible temporary files.
  9401. foreach ($files_cleanup as $file) {
  9402. $res = unlink($file);
  9403. if ($res === false) {
  9404. error_log(
  9405. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9406. 0
  9407. );
  9408. }
  9409. }
  9410. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9411. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9412. }
  9413. /**
  9414. * @param int $lp_id
  9415. * @return bool
  9416. */
  9417. public function scorm_export_to_pdf($lp_id)
  9418. {
  9419. $lp_id = intval($lp_id);
  9420. $files_to_export = array();
  9421. $course_data = api_get_course_info($this->cc);
  9422. if (!empty($course_data)) {
  9423. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9424. $list = self::get_flat_ordered_items_list($lp_id);
  9425. if (!empty($list)) {
  9426. foreach ($list as $item_id) {
  9427. $item = $this->items[$item_id];
  9428. switch ($item->type) {
  9429. case 'document':
  9430. //Getting documents from a LP with chamilo documents
  9431. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9432. // Try loading document from the base course.
  9433. if (empty($file_data) && !empty($sessionId)) {
  9434. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9435. }
  9436. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9437. if (file_exists($file_path)) {
  9438. $files_to_export[] = array('title'=>$item->get_title(), 'path'=>$file_path);
  9439. }
  9440. break;
  9441. case 'asset': //commes from a scorm package generated by chamilo
  9442. case 'sco':
  9443. $file_path = $scorm_path.'/'.$item->path;
  9444. if (file_exists($file_path)) {
  9445. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9446. }
  9447. break;
  9448. case 'dir':
  9449. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9450. break;
  9451. }
  9452. }
  9453. }
  9454. $pdf = new PDF();
  9455. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9456. return $result;
  9457. }
  9458. return false;
  9459. }
  9460. /**
  9461. * Temp function to be moved in main_api or the best place around for this.
  9462. * Creates a file path if it doesn't exist
  9463. * @param string $path
  9464. */
  9465. public function create_path($path)
  9466. {
  9467. $path_bits = explode('/', dirname($path));
  9468. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9469. $path_built = IS_WINDOWS_OS ? '' : '/';
  9470. foreach ($path_bits as $bit) {
  9471. if (!empty ($bit)) {
  9472. $new_path = $path_built.$bit;
  9473. if (is_dir($new_path)) {
  9474. $path_built = $new_path.'/';
  9475. } else {
  9476. mkdir($new_path, api_get_permissions_for_new_directories());
  9477. $path_built = $new_path.'/';
  9478. }
  9479. }
  9480. }
  9481. }
  9482. /**
  9483. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9484. * @return boolean The results of the unlink function, or false if there was no image to start with
  9485. */
  9486. public function delete_lp_image()
  9487. {
  9488. $img = $this->get_preview_image();
  9489. if ($img != '') {
  9490. $del_file = $this->get_preview_image_path(null, 'sys');
  9491. if (isset($del_file) && file_exists($del_file)) {
  9492. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9493. if (file_exists($del_file_2)) {
  9494. unlink($del_file_2);
  9495. }
  9496. $this->set_preview_image('');
  9497. return @unlink($del_file);
  9498. }
  9499. }
  9500. return false;
  9501. }
  9502. /**
  9503. * Uploads an author image to the upload/learning_path/images directory
  9504. * @param array The image array, coming from the $_FILES superglobal
  9505. * @return boolean True on success, false on error
  9506. */
  9507. public function upload_image($image_array)
  9508. {
  9509. if (!empty ($image_array['name'])) {
  9510. $upload_ok = process_uploaded_file($image_array);
  9511. $has_attachment = true;
  9512. }
  9513. if ($upload_ok) {
  9514. if ($has_attachment) {
  9515. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9516. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9517. $updir = $sys_course_path.$courseDir;
  9518. // Try to add an extension to the file if it hasn't one.
  9519. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9520. if (filter_extension($new_file_name)) {
  9521. $file_extension = explode('.', $image_array['name']);
  9522. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9523. $filename = uniqid('');
  9524. $new_file_name = $filename.'.'.$file_extension;
  9525. $new_path = $updir.'/'.$new_file_name;
  9526. // Resize the image.
  9527. $temp = new Image($image_array['tmp_name']);
  9528. $temp->resize(104);
  9529. $result = $temp->send_image($new_path);
  9530. // Storing the image filename.
  9531. if ($result) {
  9532. $this->set_preview_image($new_file_name);
  9533. //Resize to 64px to use on course homepage
  9534. $temp->resize(64);
  9535. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9536. return true;
  9537. }
  9538. }
  9539. }
  9540. }
  9541. return false;
  9542. }
  9543. /**
  9544. * @param int $lp_id
  9545. * @param string $status
  9546. */
  9547. public function set_autolaunch($lp_id, $status)
  9548. {
  9549. $course_id = api_get_course_int_id();
  9550. $lp_id = intval($lp_id);
  9551. $status = intval($status);
  9552. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9553. // Setting everything to autolaunch = 0
  9554. $attributes['autolaunch'] = 0;
  9555. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9556. Database::update($lp_table, $attributes, $where);
  9557. if ($status == 1) {
  9558. //Setting my lp_id to autolaunch = 1
  9559. $attributes['autolaunch'] = 1;
  9560. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9561. Database::update($lp_table, $attributes, $where);
  9562. }
  9563. }
  9564. /**
  9565. * Gets previous_item_id for the next element of the lp_item table
  9566. * @author Isaac flores paz
  9567. * @return integer Previous item ID
  9568. */
  9569. public function select_previous_item_id()
  9570. {
  9571. $course_id = api_get_course_int_id();
  9572. if ($this->debug > 0) {
  9573. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9574. }
  9575. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9576. // Get the max order of the items
  9577. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9578. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'";
  9579. $rs_max_order = Database::query($sql);
  9580. $row_max_order = Database::fetch_object($rs_max_order);
  9581. $max_order = $row_max_order->display_order;
  9582. // Get the previous item ID
  9583. $sql = "SELECT id as previous FROM $table_lp_item
  9584. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' AND display_order = '".$max_order."' ";
  9585. $rs_max = Database::query($sql);
  9586. $row_max = Database::fetch_object($rs_max);
  9587. // Return the previous item ID
  9588. return $row_max->previous;
  9589. }
  9590. /**
  9591. * Copies an LP
  9592. */
  9593. public function copy()
  9594. {
  9595. // Course builder
  9596. $cb = new CourseBuilder();
  9597. //Setting tools that will be copied
  9598. $cb->set_tools_to_build(array('learnpaths'));
  9599. //Setting elements that will be copied
  9600. $cb->set_tools_specific_id_list(
  9601. array('learnpaths' => array($this->lp_id))
  9602. );
  9603. $course = $cb->build();
  9604. //Course restorer
  9605. $course_restorer = new CourseRestorer($course);
  9606. $course_restorer->set_add_text_in_items(true);
  9607. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9608. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9609. }
  9610. public function verify_document_size($s)
  9611. {
  9612. $post_max = ini_get('post_max_size');
  9613. if (substr($post_max, -1, 1) == 'M') {
  9614. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9615. } elseif (substr($post_max, -1, 1) == 'G') {
  9616. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9617. }
  9618. $upl_max = ini_get('upload_max_filesize');
  9619. if (substr($upl_max, -1, 1) == 'M') {
  9620. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9621. } elseif (substr($upl_max, -1, 1) == 'G') {
  9622. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9623. }
  9624. $documents_total_space = DocumentManager::documents_total_space();
  9625. $course_max_space = DocumentManager::get_course_quota();
  9626. $total_size = filesize($s) + $documents_total_space;
  9627. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9628. return true;
  9629. } else {
  9630. return false;
  9631. }
  9632. }
  9633. /**
  9634. * Clear LP prerequisites
  9635. */
  9636. public function clear_prerequisites()
  9637. {
  9638. $course_id = $this->get_course_int_id();
  9639. if ($this->debug > 0) {
  9640. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9641. }
  9642. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9643. $lp_id = $this->get_id();
  9644. //Cleaning prerequisites
  9645. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9646. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9647. Database::query($sql);
  9648. //Cleaning mastery score for exercises
  9649. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9650. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9651. Database::query($sql);
  9652. }
  9653. public function set_previous_step_as_prerequisite_for_all_items()
  9654. {
  9655. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9656. $course_id = $this->get_course_int_id();
  9657. $lp_id = $this->get_id();
  9658. if (!empty($this->items)) {
  9659. $previous_item_id = null;
  9660. $previous_item_max = 0;
  9661. $previous_item_type = null;
  9662. $last_item_not_dir = null;
  9663. $last_item_not_dir_type = null;
  9664. $last_item_not_dir_max = null;
  9665. foreach ($this->items as $item) {
  9666. // if there was a previous item... (otherwise jump to set it)
  9667. if (!empty($previous_item_id)) {
  9668. $current_item_id = $item->get_id(); //save current id
  9669. if ($item->get_type() != 'dir') {
  9670. // Current item is not a folder, so it qualifies to get a prerequisites
  9671. if ($last_item_not_dir_type == 'quiz') {
  9672. // if previous is quiz, mark its max score as default score to be achieved
  9673. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9674. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9675. Database::query($sql);
  9676. }
  9677. // now simply update the prerequisite to set it to the last non-chapter item
  9678. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9679. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9680. Database::query($sql);
  9681. // record item as 'non-chapter' reference
  9682. $last_item_not_dir = $item->get_id();
  9683. $last_item_not_dir_type = $item->get_type();
  9684. $last_item_not_dir_max = $item->get_max();
  9685. }
  9686. } else {
  9687. if ($item->get_type() != 'dir') {
  9688. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9689. $last_item_not_dir = $item->get_id();
  9690. $last_item_not_dir_type = $item->get_type();
  9691. $last_item_not_dir_max = $item->get_max();
  9692. }
  9693. }
  9694. // Saving the item as "previous item" for the next loop
  9695. $previous_item_id = $item->get_id();
  9696. $previous_item_max = $item->get_max();
  9697. $previous_item_type = $item->get_type();
  9698. }
  9699. }
  9700. }
  9701. /**
  9702. * @param array $params
  9703. */
  9704. public static function createCategory($params)
  9705. {
  9706. $em = Database::getManager();
  9707. $item = new CLpCategory();
  9708. $item->setName($params['name']);
  9709. $item->setCId($params['c_id']);
  9710. $em->persist($item);
  9711. $em->flush();
  9712. }
  9713. /**
  9714. * @param array $params
  9715. */
  9716. public static function updateCategory($params)
  9717. {
  9718. $em = Database::getManager();
  9719. /** @var CLpCategory $item */
  9720. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9721. if ($item) {
  9722. $item->setName($params['name']);
  9723. $em->merge($item);
  9724. $em->flush();
  9725. }
  9726. }
  9727. /**
  9728. * @param int $id
  9729. */
  9730. public static function moveUpCategory($id)
  9731. {
  9732. $em = Database::getManager();
  9733. /** @var CLpCategory $item */
  9734. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9735. if ($item) {
  9736. $position = $item->getPosition() - 1;
  9737. $item->setPosition($position);
  9738. $em->persist($item);
  9739. $em->flush();
  9740. }
  9741. }
  9742. /**
  9743. * @param int $id
  9744. */
  9745. public static function moveDownCategory($id)
  9746. {
  9747. $em = Database::getManager();
  9748. /** @var CLpCategory $item */
  9749. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9750. if ($item) {
  9751. $position = $item->getPosition() + 1;
  9752. $item->setPosition($position);
  9753. $em->persist($item);
  9754. $em->flush();
  9755. }
  9756. }
  9757. /**
  9758. * @param int $courseId
  9759. * @return int|mixed
  9760. */
  9761. public static function getCountCategories($courseId)
  9762. {
  9763. if (empty($course_id)) {
  9764. return 0;
  9765. }
  9766. $em = Database::getManager();
  9767. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9768. $query->setParameter('id', $courseId);
  9769. return $query->getSingleScalarResult();
  9770. }
  9771. /**
  9772. * @param int $courseId
  9773. *
  9774. * @return mixed
  9775. */
  9776. public static function getCategories($courseId)
  9777. {
  9778. $em = Database::getManager();
  9779. //Default behaviour
  9780. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9781. array('cId' => $course_id),
  9782. array('name' => 'ASC')
  9783. );*/
  9784. // Using doctrine extensions
  9785. /** @var SortableRepository $repo */
  9786. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9787. $items = $repo
  9788. ->getBySortableGroupsQuery(['cId' => $courseId])
  9789. ->getResult();
  9790. return $items;
  9791. }
  9792. /**
  9793. * @param int $id
  9794. *
  9795. * @return CLpCategory
  9796. */
  9797. public static function getCategory($id)
  9798. {
  9799. $em = Database::getManager();
  9800. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9801. return $item;
  9802. }
  9803. /**
  9804. * @param int $courseId
  9805. * @return array
  9806. */
  9807. public static function getCategoryByCourse($courseId)
  9808. {
  9809. $em = Database::getManager();
  9810. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9811. array('cId' => $courseId)
  9812. );
  9813. return $items;
  9814. }
  9815. /**
  9816. * @param int $id
  9817. *
  9818. * @return mixed
  9819. */
  9820. public static function deleteCategory($id)
  9821. {
  9822. $em = Database::getManager();
  9823. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9824. if ($item) {
  9825. $courseId = $item->getCId();
  9826. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9827. $query->setParameter('id', $courseId);
  9828. $query->setParameter('catId', $item->getId());
  9829. $lps = $query->getResult();
  9830. // Setting category = 0.
  9831. if ($lps) {
  9832. foreach ($lps as $lpItem) {
  9833. $lpItem->setCategoryId(0);
  9834. }
  9835. }
  9836. // Removing category.
  9837. $em->remove($item);
  9838. $em->flush();
  9839. }
  9840. }
  9841. /**
  9842. * @param int $courseId
  9843. * @param bool $addSelectOption
  9844. *
  9845. * @return mixed
  9846. */
  9847. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9848. {
  9849. $items = self::getCategoryByCourse($courseId);
  9850. $cats = array();
  9851. if ($addSelectOption) {
  9852. $cats = array(get_lang('SelectACategory'));
  9853. }
  9854. if (!empty($items)) {
  9855. foreach ($items as $cat) {
  9856. $cats[$cat->getId()] = $cat->getName();
  9857. }
  9858. }
  9859. return $cats;
  9860. }
  9861. /**
  9862. * Return the scorm item type object with spaces replaced with _
  9863. * The return result is use to build a css classname like scorm_type_$return
  9864. * @param $in_type
  9865. * @return mixed
  9866. */
  9867. private static function format_scorm_type_item($in_type)
  9868. {
  9869. return str_replace(' ', '_', $in_type);
  9870. }
  9871. /**
  9872. * @param string $courseCode
  9873. * @param int $lp_id
  9874. * @param int $user_id
  9875. *
  9876. * @return learnpath
  9877. */
  9878. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9879. {
  9880. $learnPath = null;
  9881. $lpObject = Session::read('lpobject');
  9882. if ($lpObject !== null) {
  9883. $learnPath = unserialize($lpObject);
  9884. }
  9885. if (!is_object($learnPath)) {
  9886. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9887. }
  9888. return $learnPath;
  9889. }
  9890. /**
  9891. * @param int $itemId
  9892. * @return learnpathItem|false
  9893. */
  9894. public function getItem($itemId)
  9895. {
  9896. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9897. return $this->items[$itemId];
  9898. }
  9899. return false;
  9900. }
  9901. /**
  9902. * @return int
  9903. */
  9904. public function getCategoryId()
  9905. {
  9906. return $this->categoryId;
  9907. }
  9908. /**
  9909. * @param int $categoryId
  9910. * @return bool
  9911. */
  9912. public function setCategoryId($categoryId)
  9913. {
  9914. $this->categoryId = intval($categoryId);
  9915. $courseId = api_get_course_int_id();
  9916. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9917. $lp_id = $this->get_id();
  9918. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9919. WHERE c_id = $courseId AND id = $lp_id";
  9920. Database::query($sql);
  9921. return true;
  9922. }
  9923. /**
  9924. * Get whether this is a learning path with the possibility to subscribe
  9925. * users or not
  9926. * @return int
  9927. */
  9928. public function getSubscribeUsers()
  9929. {
  9930. return $this->subscribeUsers;
  9931. }
  9932. /**
  9933. * Set whether this is a learning path with the possibility to subscribe
  9934. * users or not
  9935. * @param int $subscribeUsers (0 = false, 1 = true)
  9936. */
  9937. public function setSubscribeUsers($value)
  9938. {
  9939. if ($this->debug > 0) {
  9940. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9941. }
  9942. $this->subscribeUsers = intval($value); ;
  9943. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9944. $lp_id = $this->get_id();
  9945. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9946. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9947. Database::query($sql);
  9948. return true;
  9949. }
  9950. /**
  9951. * Calculate the count of stars for a user in this LP
  9952. * This calculation is based on the following rules:
  9953. * - the student gets one star when he gets to 50% of the learning path
  9954. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9955. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9956. * - the student gets the final star when the score for the *last* test is >= 80%
  9957. * @param int $sessionId Optional. The session ID
  9958. * @return int The count of stars
  9959. */
  9960. public function getCalculateStars($sessionId = 0)
  9961. {
  9962. $stars = 0;
  9963. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9964. if ($progress >= 50) {
  9965. $stars++;
  9966. }
  9967. // Calculate stars chapters evaluation
  9968. $exercisesItems = $this->getExercisesItems();
  9969. if (!empty($exercisesItems)) {
  9970. $totalResult = 0;
  9971. foreach ($exercisesItems as $exerciseItem) {
  9972. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9973. $this->user_id,
  9974. $exerciseItem->path,
  9975. $this->course_int_id,
  9976. $sessionId,
  9977. $this->lp_id,
  9978. $exerciseItem->db_id
  9979. );
  9980. $exerciseResultInfo = end($exerciseResultInfo);
  9981. if (!$exerciseResultInfo) {
  9982. continue;
  9983. }
  9984. if (!empty($exerciseResultInfo['exe_weighting'])) {
  9985. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9986. } else {
  9987. $exerciseResult = 0;
  9988. }
  9989. $totalResult += $exerciseResult;
  9990. }
  9991. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9992. if ($totalExerciseAverage >= 50) {
  9993. $stars++;
  9994. }
  9995. if ($totalExerciseAverage >= 80) {
  9996. $stars++;
  9997. }
  9998. }
  9999. // Calculate star for final evaluation
  10000. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10001. if (!empty($finalEvaluationItem)) {
  10002. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10003. $this->user_id,
  10004. $finalEvaluationItem->path,
  10005. $this->course_int_id,
  10006. $sessionId,
  10007. $this->lp_id,
  10008. $finalEvaluationItem->db_id
  10009. );
  10010. $evaluationResultInfo = end($evaluationResultInfo);
  10011. if ($evaluationResultInfo) {
  10012. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  10013. if ($evaluationResult >= 80) {
  10014. $stars++;
  10015. }
  10016. }
  10017. }
  10018. return $stars;
  10019. }
  10020. /**
  10021. * Get the items of exercise type
  10022. * @return array The items. Otherwise return false
  10023. */
  10024. public function getExercisesItems()
  10025. {
  10026. $exercises = [];
  10027. foreach ($this->items as $item) {
  10028. if ($item->type != 'quiz') {
  10029. continue;
  10030. }
  10031. $exercises[] = $item;
  10032. }
  10033. array_pop($exercises);
  10034. return $exercises;
  10035. }
  10036. /**
  10037. * Get the item of exercise type (evaluation type)
  10038. * @return array The final evaluation. Otherwise return false
  10039. */
  10040. public function getFinalEvaluationItem()
  10041. {
  10042. $exercises = [];
  10043. foreach ($this->items as $item) {
  10044. if ($item->type != 'quiz') {
  10045. continue;
  10046. }
  10047. $exercises[] = $item;
  10048. }
  10049. return array_pop($exercises);
  10050. }
  10051. /**
  10052. * Calculate the total points achieved for the current user in this learning path
  10053. * @param int $sessionId Optional. The session Id
  10054. * @return int
  10055. */
  10056. public function getCalculateScore($sessionId = 0)
  10057. {
  10058. // Calculate stars chapters evaluation
  10059. $exercisesItems = $this->getExercisesItems();
  10060. $finalEvaluationItem = $this->getFinalEvaluationItem();
  10061. $totalExercisesResult = 0;
  10062. $totalEvaluationResult = 0;
  10063. if ($exercisesItems !== false) {
  10064. foreach ($exercisesItems as $exerciseItem) {
  10065. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10066. $this->user_id,
  10067. $exerciseItem->path,
  10068. $this->course_int_id,
  10069. $sessionId,
  10070. $this->lp_id,
  10071. $exerciseItem->db_id
  10072. );
  10073. $exerciseResultInfo = end($exerciseResultInfo);
  10074. if (!$exerciseResultInfo) {
  10075. continue;
  10076. }
  10077. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  10078. }
  10079. }
  10080. if (!empty($finalEvaluationItem)) {
  10081. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10082. $this->user_id,
  10083. $finalEvaluationItem->path,
  10084. $this->course_int_id,
  10085. $sessionId,
  10086. $this->lp_id,
  10087. $finalEvaluationItem->db_id
  10088. );
  10089. $evaluationResultInfo = end($evaluationResultInfo);
  10090. if ($evaluationResultInfo) {
  10091. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  10092. }
  10093. }
  10094. return $totalExercisesResult + $totalEvaluationResult;
  10095. }
  10096. /**
  10097. * Check if URL is not allowed to be show in a iframe
  10098. * @param string $src
  10099. *
  10100. * @return string
  10101. */
  10102. public function fixBlockedLinks($src)
  10103. {
  10104. $urlInfo = parse_url($src);
  10105. $platformProtocol = 'https';
  10106. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  10107. $platformProtocol = 'http';
  10108. }
  10109. $protocolFixApplied = false;
  10110. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  10111. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  10112. if ($platformProtocol != $scheme) {
  10113. Session::write('x_frame_source', $src);
  10114. $src = 'blank.php?error=x_frames_options';
  10115. $protocolFixApplied = true;
  10116. }
  10117. if ($protocolFixApplied == false) {
  10118. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  10119. // Check X-Frame-Options
  10120. $ch = curl_init();
  10121. $options = array(
  10122. CURLOPT_URL => $src,
  10123. CURLOPT_RETURNTRANSFER => true,
  10124. CURLOPT_HEADER => true,
  10125. CURLOPT_FOLLOWLOCATION => true,
  10126. CURLOPT_ENCODING => "",
  10127. CURLOPT_AUTOREFERER => true,
  10128. CURLOPT_CONNECTTIMEOUT => 120,
  10129. CURLOPT_TIMEOUT => 120,
  10130. CURLOPT_MAXREDIRS => 10,
  10131. );
  10132. curl_setopt_array($ch, $options);
  10133. $response = curl_exec($ch);
  10134. $httpCode = curl_getinfo($ch);
  10135. $headers = substr($response, 0, $httpCode['header_size']);
  10136. $error = false;
  10137. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  10138. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  10139. ) {
  10140. $error = true;
  10141. }
  10142. if ($error) {
  10143. Session::write('x_frame_source', $src);
  10144. $src = 'blank.php?error=x_frames_options';
  10145. }
  10146. }
  10147. }
  10148. return $src;
  10149. }
  10150. /**
  10151. * Check if this LP has a created forum in the basis course
  10152. * @return boolean
  10153. */
  10154. public function lpHasForum()
  10155. {
  10156. $forumTable = Database::get_course_table(TABLE_FORUM);
  10157. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10158. $fakeFrom = "
  10159. $forumTable f
  10160. INNER JOIN $itemProperty ip
  10161. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  10162. ";
  10163. $resultData = Database::select(
  10164. 'COUNT(f.iid) AS qty',
  10165. $fakeFrom,
  10166. [
  10167. 'where' => [
  10168. 'ip.visibility != ? AND ' => 2,
  10169. 'ip.tool = ? AND ' => TOOL_FORUM,
  10170. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10171. 'f.lp_id = ?' => intval($this->lp_id)
  10172. ]
  10173. ],
  10174. 'first'
  10175. );
  10176. return $resultData['qty'] > 0;
  10177. }
  10178. /**
  10179. * Get the forum for this learning path
  10180. * @return boolean
  10181. */
  10182. public function getForum($sessionId = 0)
  10183. {
  10184. $forumTable = Database::get_course_table(TABLE_FORUM);
  10185. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10186. $fakeFrom = "$forumTable f
  10187. INNER JOIN $itemProperty ip ";
  10188. if ($this->lp_session_id == 0) {
  10189. $fakeFrom .= "
  10190. ON (
  10191. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  10192. f.session_id = ip.session_id OR ip.session_id IS NULL
  10193. )
  10194. )
  10195. ";
  10196. } else {
  10197. $fakeFrom .= "
  10198. ON (
  10199. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  10200. )
  10201. ";
  10202. }
  10203. $resultData = Database::select(
  10204. 'f.*',
  10205. $fakeFrom,
  10206. [
  10207. 'where' => [
  10208. 'ip.visibility != ? AND ' => 2,
  10209. 'ip.tool = ? AND ' => TOOL_FORUM,
  10210. 'f.session_id = ? AND ' => $sessionId,
  10211. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10212. 'f.lp_id = ?' => intval($this->lp_id)
  10213. ]
  10214. ],
  10215. 'first'
  10216. );
  10217. if (empty($resultData)) {
  10218. return false;
  10219. }
  10220. return $resultData;
  10221. }
  10222. /**
  10223. * Create a forum for this learning path
  10224. * @param type $forumCategoryId
  10225. * @return int The forum ID if was created. Otherwise return false
  10226. */
  10227. public function createForum($forumCategoryId)
  10228. {
  10229. require_once api_get_path(SYS_CODE_PATH).'/forum/forumfunction.inc.php';
  10230. $forumId = store_forum(
  10231. [
  10232. 'lp_id' => $this->lp_id,
  10233. 'forum_title' => $this->name,
  10234. 'forum_comment' => null,
  10235. 'forum_category' => intval($forumCategoryId),
  10236. 'students_can_edit_group' => ['students_can_edit' => 0],
  10237. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  10238. 'default_view_type_group' => ['default_view_type' => 'flat'],
  10239. 'group_forum' => 0,
  10240. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  10241. ],
  10242. [],
  10243. true
  10244. );
  10245. return $forumId;
  10246. }
  10247. /**
  10248. * Check and obtain the lp final item if exist
  10249. *
  10250. * @return learnpathItem
  10251. */
  10252. private function getFinalItem()
  10253. {
  10254. if (empty($this->items)) {
  10255. return null;
  10256. }
  10257. foreach ($this->items as $item) {
  10258. if ($item->type !== 'final_item') {
  10259. continue;
  10260. }
  10261. return $item;
  10262. }
  10263. }
  10264. /**
  10265. * Get the LP Final Item Template
  10266. *
  10267. * @return string
  10268. */
  10269. private function getFinalItemTemplate()
  10270. {
  10271. return file_get_contents(api_get_path(SYS_CODE_PATH).'lp/final_item_template/template.html');
  10272. }
  10273. /**
  10274. * Get the LP Final Item Url
  10275. *
  10276. * @return string
  10277. */
  10278. private function getSavedFinalItem()
  10279. {
  10280. $finalItem = $this->getFinalItem();
  10281. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10282. if ($doc && file_exists($doc['absolute_path'])) {
  10283. return file_get_contents($doc['absolute_path']);
  10284. }
  10285. return '';
  10286. }
  10287. /**
  10288. * Get the LP Final Item form
  10289. *
  10290. * @return string
  10291. */
  10292. public function getFinalItemForm()
  10293. {
  10294. $finalItem = $this->getFinalItem();
  10295. $title = '';
  10296. if ($finalItem) {
  10297. $title = $finalItem->get_title();
  10298. $buttonText = get_lang('Save');
  10299. $content = $this->getSavedFinalItem();
  10300. } else {
  10301. $buttonText = get_lang('LPCreateDocument');
  10302. $content = $this->getFinalItemTemplate();
  10303. }
  10304. $courseInfo = api_get_course_info();
  10305. $result = $this->generate_lp_folder($courseInfo);
  10306. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10307. $relative_prefix = '../../';
  10308. $editorConfig = [
  10309. 'ToolbarSet' => 'LearningPathDocuments',
  10310. 'Width' => '100%',
  10311. 'Height' => '500',
  10312. 'FullPage' => true,
  10313. 'CreateDocumentDir' => $relative_prefix,
  10314. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  10315. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  10316. ];
  10317. $url = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  10318. 'type' => 'document',
  10319. 'lp_id' => $this->lp_id
  10320. ]);
  10321. $form = new FormValidator('final_item', 'POST', $url);
  10322. $form->addText('title', get_lang('Title'));
  10323. $form->addButtonSave($buttonText);
  10324. $form->addHtml(
  10325. Display::return_message(
  10326. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10327. 'normal',
  10328. false
  10329. )
  10330. );
  10331. $renderer = $form->defaultRenderer();
  10332. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10333. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10334. $form->addHidden('action', 'add_final_item');
  10335. $form->addHidden('path', Session::read('pathItem'));
  10336. $form->addHidden('previous', $this->get_last());
  10337. $form->setDefaults(
  10338. ['title' => $title, 'content_lp_certificate' => $content]
  10339. );
  10340. if ($form->validate()) {
  10341. $values = $form->exportValues();
  10342. $lastItemId = $this->get_last();
  10343. if (!$finalItem) {
  10344. $documentId = $this->create_document(
  10345. $this->course_info,
  10346. $values['content_lp_certificate'],
  10347. $values['title']
  10348. );
  10349. $this->add_item(
  10350. 0,
  10351. $lastItemId,
  10352. 'final_item',
  10353. $documentId,
  10354. $values['title'],
  10355. ''
  10356. );
  10357. Display::addFlash(
  10358. Display::return_message(get_lang('Added'))
  10359. );
  10360. } else {
  10361. $this->edit_document($this->course_info);
  10362. }
  10363. }
  10364. return $form->returnForm();
  10365. }
  10366. /**
  10367. * Check if the current lp item is first, both, last or none from lp list
  10368. *
  10369. * @param int $currentItemId
  10370. * @return string
  10371. */
  10372. public function isFirstOrLastItem($currentItemId)
  10373. {
  10374. if ($this->debug > 0) {
  10375. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10376. }
  10377. $lpItemId = [];
  10378. $typeListNotToVerify = self::getChapterTypes();
  10379. // Using get_toc() function instead $this->items because returns the correct order of the items
  10380. foreach ($this->get_toc() as $item) {
  10381. if (!in_array($item['type'], $typeListNotToVerify)) {
  10382. $lpItemId[] = $item['id'];
  10383. }
  10384. }
  10385. $lastLpItemIndex = count($lpItemId) - 1;
  10386. $position = array_search($currentItemId, $lpItemId);
  10387. switch ($position) {
  10388. case 0:
  10389. if (!$lastLpItemIndex) {
  10390. $answer = 'both';
  10391. break;
  10392. }
  10393. $answer = 'first';
  10394. break;
  10395. case $lastLpItemIndex:
  10396. $answer = 'last';
  10397. break;
  10398. default:
  10399. $answer = 'none';
  10400. }
  10401. return $answer;
  10402. }
  10403. /**
  10404. * Get whether this is a learning path with the accumulated SCORM time or not
  10405. * @return int
  10406. */
  10407. public function getAccumulateScormTime()
  10408. {
  10409. return $this->accumulateScormTime;
  10410. }
  10411. /**
  10412. * Set whether this is a learning path with the accumulated SCORM time or not
  10413. * @param int $value (0 = false, 1 = true)
  10414. * @return bool Always returns true
  10415. */
  10416. public function setAccumulateScormTime($value)
  10417. {
  10418. if ($this->debug > 0) {
  10419. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10420. }
  10421. $this->accumulateScormTime = intval($value);
  10422. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10423. $lp_id = $this->get_id();
  10424. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10425. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10426. Database::query($sql);
  10427. return true;
  10428. }
  10429. /**
  10430. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10431. * the new learning path tool.
  10432. *
  10433. * The function is a big switch on tool type.
  10434. * In each case, we query the corresponding table for information and build the link
  10435. * with that information.
  10436. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on
  10437. * previous work (display_addedresource_link_in_learnpath())
  10438. * @param int $course_id Course code
  10439. * @param int $learningPathId The learning path ID (in lp table)
  10440. * @param int $id_in_path the unique index in the items table
  10441. * @param int $lpViewId
  10442. * @param string $origin
  10443. * @return string
  10444. */
  10445. public static function rl_get_resource_link_for_learnpath(
  10446. $course_id,
  10447. $learningPathId,
  10448. $id_in_path,
  10449. $lpViewId,
  10450. $origin = 'learnpath'
  10451. ) {
  10452. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10453. $course_info = api_get_course_info_by_id($course_id);
  10454. $course_id = $course_info['real_id'];
  10455. $course_code = $course_info['code'];
  10456. $session_id = api_get_session_id();
  10457. $learningPathId = intval($learningPathId);
  10458. $id_in_path = intval($id_in_path);
  10459. $lpViewId = intval($lpViewId);
  10460. $em = Database::getManager();
  10461. $sql = "SELECT * FROM $tbl_lp_item
  10462. WHERE
  10463. c_id = $course_id AND
  10464. lp_id = $learningPathId AND
  10465. id = $id_in_path
  10466. ";
  10467. $res_item = Database::query($sql);
  10468. if (Database::num_rows($res_item) < 1) {
  10469. return -1;
  10470. }
  10471. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10472. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10473. // Get the lp_item_view with the highest view_count.
  10474. $sql = "SELECT * FROM $item_view_table
  10475. WHERE
  10476. c_id = $course_id AND
  10477. lp_item_id = ".$row_item['id']." AND
  10478. lp_view_id = $lpViewId
  10479. ORDER BY view_count DESC";
  10480. $learnpathItemViewResult = Database::query($sql);
  10481. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10482. $learnpathItemViewId = 0;
  10483. if (isset($learnpathItemViewData)) {
  10484. $learnpathItemViewId = $learnpathItemViewData['id'];
  10485. }
  10486. $type = strtolower($row_item['item_type']);
  10487. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10488. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10489. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10490. $link = '';
  10491. $extraParams = "origin=$origin&cidReq=$course_code&session_id=$session_id&id_session=$session_id";
  10492. switch ($type) {
  10493. case 'dir':
  10494. $link .= $main_dir_path.'lp/blank.php';
  10495. break;
  10496. case TOOL_CALENDAR_EVENT:
  10497. $link .= $main_dir_path.'calendar/agenda.php?agenda_id='.$id.'&'.$extraParams;
  10498. break;
  10499. case TOOL_ANNOUNCEMENT:
  10500. $link .= $main_dir_path.'announcements/announcements.php?ann_id='.$id.'&'.$extraParams;
  10501. break;
  10502. case TOOL_LINK:
  10503. $link .= $main_dir_path.'link/link_goto.php?link_id='.$id.'&'.$extraParams;
  10504. break;
  10505. case TOOL_QUIZ:
  10506. if (!empty($id)) {
  10507. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10508. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10509. $result = Database::query($sql);
  10510. $myrow = Database::fetch_array($result);
  10511. if ($row_item['title'] != '') {
  10512. $myrow['title'] = $row_item['title'];
  10513. }
  10514. $link .= $main_dir_path.'exercise/overview.php?lp_init=1&learnpath_item_view_id='.$learnpathItemViewId.'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id.'&'.$extraParams;
  10515. }
  10516. break;
  10517. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  10518. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10519. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10520. $myrow = Database::fetch_array($result);
  10521. $path = $myrow['path'];
  10522. $link .= $main_dir_path.'exercise/showinframes.php?file='.$path.'&cid='.$course_code.'&uid='.api_get_user_id().'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId.'&'.$extraParams;
  10523. break;
  10524. case TOOL_FORUM:
  10525. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&'.$extraParams;
  10526. break;
  10527. case TOOL_THREAD: //forum post
  10528. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10529. if (!empty($id)) {
  10530. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10531. $result = Database::query($sql);
  10532. $myrow = Database::fetch_array($result);
  10533. $link .= $main_dir_path.'forum/viewthread.php?thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10534. }
  10535. break;
  10536. case TOOL_POST:
  10537. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10538. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10539. $myrow = Database::fetch_array($result);
  10540. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'&lp=true&'.$extraParams;
  10541. break;
  10542. case TOOL_DOCUMENT:
  10543. $document = $em
  10544. ->getRepository('ChamiloCourseBundle:CDocument')
  10545. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10546. if (!$document) {
  10547. break;
  10548. }
  10549. $documentPathInfo = pathinfo($document->getPath());
  10550. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10551. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10552. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10553. if ($showDirectUrl) {
  10554. $link = $main_course_path.'document'.$document->getPath().'?'.$extraParams;
  10555. } else {
  10556. $link = api_get_path(WEB_CODE_PATH).'document/showinframes.php?id='.$id.'&'.$extraParams;
  10557. }
  10558. $openmethod = 2;
  10559. $officedoc = false;
  10560. Session::write('openmethod', $openmethod);
  10561. Session::write('officedoc', $officedoc);
  10562. break;
  10563. case TOOL_LP_FINAL_ITEM:
  10564. $link .= api_get_path(WEB_CODE_PATH).'lp/lp_final_item.php?&id='.$id.'&lp_id='.$learningPathId.'&'.$extraParams;
  10565. break;
  10566. case 'assignments':
  10567. $link .= $main_dir_path.'work/work.php?'.$extraParams;
  10568. break;
  10569. case TOOL_DROPBOX:
  10570. $link .= $main_dir_path.'dropbox/index.php?'.$extraParams;
  10571. break;
  10572. case 'introduction_text': //DEPRECATED
  10573. $link .= '';
  10574. break;
  10575. case TOOL_COURSE_DESCRIPTION:
  10576. $link .= $main_dir_path.'course_description?'.$extraParams;
  10577. break;
  10578. case TOOL_GROUP:
  10579. $link .= $main_dir_path.'group/group.php?'.$extraParams;
  10580. break;
  10581. case TOOL_USER:
  10582. $link .= $main_dir_path.'user/user.php?'.$extraParams;
  10583. break;
  10584. case TOOL_STUDENTPUBLICATION:
  10585. if (!empty($row_item['path'])) {
  10586. $link .= $main_dir_path.'work/work_list.php?id='.$row_item['path'].'&'.$extraParams;
  10587. break;
  10588. }
  10589. $link .= $main_dir_path.'work/work.php?'.api_get_cidreq().'&id='.$row_item['path'].'&'.$extraParams;
  10590. break;
  10591. } //end switch
  10592. return $link;
  10593. }
  10594. /**
  10595. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10596. *
  10597. * @author Yannick Warnier <ywarnier@beeznest.org>
  10598. * @param string Course code
  10599. * @param string The tool type (using constants declared in main_api.lib.php)
  10600. * @param integer The resource ID
  10601. */
  10602. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10603. {
  10604. $_course = api_get_course_info($course_code);
  10605. $course_id = $_course['real_id'];
  10606. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10607. $learningPathId = intval($learningPathId);
  10608. $id_in_path = intval($id_in_path);
  10609. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10610. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10611. $res_item = Database::query($sql_item);
  10612. if (Database::num_rows($res_item) < 1) {
  10613. return '';
  10614. }
  10615. $row_item = Database::fetch_array($res_item);
  10616. $type = strtolower($row_item['item_type']);
  10617. $id = $row_item['ref'];
  10618. $output = '';
  10619. switch ($type) {
  10620. case TOOL_CALENDAR_EVENT:
  10621. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10622. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10623. $myrow = Database::fetch_array($result);
  10624. $output = $myrow['title'];
  10625. break;
  10626. case TOOL_ANNOUNCEMENT:
  10627. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10628. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10629. $myrow = Database::fetch_array($result);
  10630. $output = $myrow['title'];
  10631. break;
  10632. case TOOL_LINK:
  10633. // Doesn't take $target into account.
  10634. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10635. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10636. $myrow = Database::fetch_array($result);
  10637. $output = $myrow['title'];
  10638. break;
  10639. case TOOL_QUIZ:
  10640. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10641. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10642. $myrow = Database::fetch_array($result);
  10643. $output = $myrow['title'];
  10644. break;
  10645. case TOOL_FORUM:
  10646. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10647. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10648. $myrow = Database::fetch_array($result);
  10649. $output = $myrow['forum_name'];
  10650. break;
  10651. case TOOL_THREAD: //=topics
  10652. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10653. // Grabbing the title of the post.
  10654. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10655. $result_title = Database::query($sql_title);
  10656. $myrow_title = Database::fetch_array($result_title);
  10657. $output = $myrow_title['post_title'];
  10658. break;
  10659. case TOOL_POST:
  10660. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10661. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10662. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10663. $result = Database::query($sql);
  10664. $post = Database::fetch_array($result);
  10665. $output = $post['post_title'];
  10666. break;
  10667. case 'dir':
  10668. $title = $row_item['title'];
  10669. if (!empty($title)) {
  10670. $output = $title;
  10671. } else {
  10672. $output = '-';
  10673. }
  10674. break;
  10675. case TOOL_DOCUMENT:
  10676. $title = $row_item['title'];
  10677. if (!empty($title)) {
  10678. $output = $title;
  10679. } else {
  10680. $output = '-';
  10681. }
  10682. break;
  10683. case 'hotpotatoes':
  10684. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10685. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10686. $myrow = Database::fetch_array($result);
  10687. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10688. $last = count($pathname) - 1; // Making a correct name for the link.
  10689. $filename = $pathname[$last]; // Making a correct name for the link.
  10690. $ext = explode('.', $filename);
  10691. $ext = strtolower($ext[sizeof($ext) - 1]);
  10692. $myrow['path'] = rawurlencode($myrow['path']);
  10693. $output = $filename;
  10694. break;
  10695. }
  10696. return stripslashes($output);
  10697. }
  10698. }
  10699. if (!function_exists('trim_value')) {
  10700. function trim_value(& $value) {
  10701. $value = trim($value);
  10702. }
  10703. }