learnpath.class.php 457 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. use Symfony\Component\Filesystem\Filesystem;
  6. use Symfony\Component\Finder\Finder;
  7. /**
  8. * Class learnpath
  9. * This class defines the parent attributes and methods for Chamilo learnpaths
  10. * and SCORM learnpaths. It is used by the scorm class.
  11. *
  12. * @package chamilo.learnpath
  13. * @author Yannick Warnier <ywarnier@beeznest.org>
  14. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  15. */
  16. class learnpath
  17. {
  18. public $attempt = 0; // The number for the current ID view.
  19. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  20. public $current; // Id of the current item the user is viewing.
  21. public $current_score; // The score of the current item.
  22. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  23. public $current_time_stop; // The time the user closed this resource.
  24. public $default_status = 'not attempted';
  25. public $encoding = 'UTF-8';
  26. public $error = '';
  27. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  28. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  29. public $index; // The index of the active learnpath_item in $ordered_items array.
  30. public $items = array();
  31. public $last; // item_id of last item viewed in the learning path.
  32. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  33. public $license; // Which license this course has been given - not used yet on 20060522.
  34. public $lp_id; // DB ID for this learnpath.
  35. public $lp_view_id; // DB ID for lp_view
  36. public $log_file; // File where to log learnpath API msg.
  37. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  38. public $message = '';
  39. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  40. public $name; // Learnpath name (they generally have one).
  41. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  42. public $path = ''; // Path inside the scorm directory (if scorm).
  43. public $theme; // The current theme of the learning path.
  44. public $preview_image; // The current image of the learning path.
  45. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  46. public $prevent_reinit = 1;
  47. // Describes the mode of progress bar display.
  48. public $seriousgame_mode = 0;
  49. public $progress_bar_mode = '%';
  50. // Percentage progress as saved in the db.
  51. public $progress_db = '0';
  52. public $proximity; // Wether the content is distant or local or unknown.
  53. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  54. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  55. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  56. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  57. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  58. public $user_id; //ID of the user that is viewing/using the course
  59. public $update_queue = array();
  60. public $scorm_debug = 0;
  61. public $arrMenu = array(); // Array for the menu items.
  62. public $debug = 0; // Logging level.
  63. public $lp_session_id = 0;
  64. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  65. public $prerequisite = 0;
  66. public $use_max_score = 1; // 1 or 0
  67. public $subscribeUsers = 0; // Subscribe users or not
  68. public $created_on = '';
  69. public $modified_on = '';
  70. public $publicated_on = '';
  71. public $expired_on = '';
  72. public $ref = null;
  73. public $course_int_id;
  74. public $course_info = array();
  75. public $categoryId;
  76. /**
  77. * Constructor.
  78. * Needs a database handler, a course code and a learnpath id from the database.
  79. * Also builds the list of items into $this->items.
  80. * @param string $course Course code
  81. * @param integer $lp_id
  82. * @param integer $user_id
  83. * @return mixed True on success, false on error
  84. */
  85. public function __construct($course, $lp_id, $user_id)
  86. {
  87. $this->encoding = api_get_system_encoding();
  88. if ($this->debug > 0) {
  89. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  90. }
  91. if (empty($course)) {
  92. $this->error = 'Course code is empty';
  93. return false;
  94. } else {
  95. $course_info = api_get_course_info($course);
  96. if (!empty($course_info)) {
  97. $this->cc = $course_info['code'];
  98. $this->course_info = $course_info;
  99. $course_id = $course_info['real_id'];
  100. } else {
  101. $this->error = 'Course code does not exist in database.';
  102. return false;
  103. }
  104. }
  105. $this->set_course_int_id($course_id);
  106. // Check learnpath ID.
  107. if (empty($lp_id)) {
  108. $this->error = 'Learnpath ID is empty';
  109. return false;
  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. $lp_id = intval($lp_id);
  114. $sql = "SELECT * FROM $lp_table
  115. WHERE id = '$lp_id' AND c_id = $course_id";
  116. if ($this->debug > 2) {
  117. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  118. }
  119. $res = Database::query($sql);
  120. if (Database::num_rows($res) > 0) {
  121. $this->lp_id = $lp_id;
  122. $row = Database::fetch_array($res);
  123. $this->type = $row['lp_type'];
  124. $this->name = stripslashes($row['name']);
  125. $this->proximity = $row['content_local'];
  126. $this->theme = $row['theme'];
  127. $this->maker = $row['content_maker'];
  128. $this->prevent_reinit = $row['prevent_reinit'];
  129. $this->seriousgame_mode = $row['seriousgame_mode'];
  130. $this->license = $row['content_license'];
  131. $this->scorm_debug = $row['debug'];
  132. $this->js_lib = $row['js_lib'];
  133. $this->path = $row['path'];
  134. $this->preview_image = $row['preview_image'];
  135. $this->author = $row['author'];
  136. $this->hide_toc_frame = $row['hide_toc_frame'];
  137. $this->lp_session_id = $row['session_id'];
  138. $this->use_max_score = $row['use_max_score'];
  139. $this->subscribeUsers = $row['subscribe_users'];
  140. $this->created_on = $row['created_on'];
  141. $this->modified_on = $row['modified_on'];
  142. $this->ref = $row['ref'];
  143. $this->categoryId = $row['category_id'];
  144. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  145. $this->publicated_on = $row['publicated_on'];
  146. }
  147. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  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. } else {
  157. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  158. return false;
  159. }
  160. }
  161. // Check user ID.
  162. if (empty($user_id)) {
  163. $this->error = 'User ID is empty';
  164. return false;
  165. } else {
  166. $user_info = api_get_user_info($user_id);
  167. if (!empty($user_info)) {
  168. $this->user_id = $user_info['user_id'];
  169. } else {
  170. $this->error = 'User ID does not exist in database ('.$sql.')';
  171. return false;
  172. }
  173. }
  174. // End of variables checking.
  175. $session_id = api_get_session_id();
  176. // Get the session condition for learning paths of the base + session.
  177. $session = api_get_session_condition($session_id);
  178. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  179. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  180. // Selecting by view_count descending allows to get the highest view_count first.
  181. $sql = "SELECT * FROM $lp_table
  182. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  183. ORDER BY view_count DESC";
  184. $res = Database::query($sql);
  185. if ($this->debug > 2) {
  186. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  187. }
  188. if (Database :: num_rows($res) > 0) {
  189. if ($this->debug > 2) {
  190. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  191. }
  192. $row = Database :: fetch_array($res);
  193. $this->attempt = $row['view_count'];
  194. $this->lp_view_id = $row['id'];
  195. $this->last_item_seen = $row['last_item'];
  196. $this->progress_db = $row['progress'];
  197. $this->lp_view_session_id = $row['session_id'];
  198. } else if (!api_is_invitee()) {
  199. if ($this->debug > 2) {
  200. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  201. }
  202. $this->attempt = 1;
  203. $params = [
  204. 'c_id' => $course_id,
  205. 'lp_id' => $lp_id,
  206. 'user_id' => $user_id,
  207. 'view_count' => 1,
  208. 'session_id' => $session_id,
  209. 'last_item' => 0
  210. ];
  211. Database::insert($lp_table, $params);
  212. $this->lp_view_id = Database::insert_id();
  213. if ($this->debug > 2) {
  214. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  215. }
  216. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  217. Database::query($sql);
  218. }
  219. // Initialise items.
  220. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  221. $sql = "SELECT * FROM $lp_item_table
  222. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  223. ORDER BY parent_item_id, display_order";
  224. $res = Database::query($sql);
  225. if ($this->debug > 2) {
  226. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  227. error_log('-- Start while--', 0);
  228. }
  229. $lp_item_id_list = array();
  230. while ($row = Database::fetch_array($res)) {
  231. $lp_item_id_list[] = $row['id'];
  232. switch ($this->type) {
  233. case 3: //aicc
  234. $oItem = new aiccItem('db', $row['id'], $course_id);
  235. if (is_object($oItem)) {
  236. $my_item_id = $oItem->get_id();
  237. $oItem->set_lp_view($this->lp_view_id, $course_id);
  238. $oItem->set_prevent_reinit($this->prevent_reinit);
  239. // Don't use reference here as the next loop will make the pointed object change.
  240. $this->items[$my_item_id] = $oItem;
  241. $this->refs_list[$oItem->ref] = $my_item_id;
  242. if ($this->debug > 2) {
  243. error_log(
  244. 'New LP - learnpath::__construct() - ' .
  245. 'aicc object with id ' . $my_item_id .
  246. ' set in items[]',
  247. 0
  248. );
  249. }
  250. }
  251. break;
  252. case 2:
  253. $oItem = new scormItem('db', $row['id'], $course_id);
  254. if (is_object($oItem)) {
  255. $my_item_id = $oItem->get_id();
  256. $oItem->set_lp_view($this->lp_view_id, $course_id);
  257. $oItem->set_prevent_reinit($this->prevent_reinit);
  258. // Don't use reference here as the next loop will make the pointed object change.
  259. $this->items[$my_item_id] = $oItem;
  260. $this->refs_list[$oItem->ref] = $my_item_id;
  261. if ($this->debug > 2) {
  262. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  263. }
  264. }
  265. break;
  266. case 1:
  267. default:
  268. if ($this->debug > 2) {
  269. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  270. }
  271. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  272. if ($this->debug > 2) {
  273. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  274. }
  275. if (is_object($oItem)) {
  276. $my_item_id = $oItem->get_id();
  277. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  278. $oItem->set_prevent_reinit($this->prevent_reinit);
  279. // Don't use reference here as the next loop will make the pointed object change.
  280. $this->items[$my_item_id] = $oItem;
  281. $this->refs_list[$my_item_id] = $my_item_id;
  282. if ($this->debug > 2) {
  283. error_log(
  284. 'New LP - learnpath::__construct() ' . __LINE__ .
  285. ' - object with id ' . $my_item_id . ' set in items[]',
  286. 0);
  287. }
  288. }
  289. break;
  290. }
  291. // Setting the object level with variable $this->items[$i][parent]
  292. foreach ($this->items as $itemLPObject) {
  293. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  294. $itemLPObject->level = $level;
  295. }
  296. // Setting the view in the item object.
  297. if (is_object($this->items[$row['id']])) {
  298. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  299. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  300. $this->items[$row['id']]->current_start_time = 0;
  301. $this->items[$row['id']]->current_stop_time = 0;
  302. }
  303. }
  304. }
  305. if ($this->debug > 2) {
  306. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  307. }
  308. if (!empty($lp_item_id_list)) {
  309. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  310. if (!empty($lp_item_id_list_to_string)) {
  311. // Get last viewing vars.
  312. $lp_item_view_table = Database:: get_course_table(
  313. TABLE_LP_ITEM_VIEW
  314. );
  315. // This query should only return one or zero result.
  316. $sql = "SELECT lp_item_id, status
  317. FROM $lp_item_view_table
  318. WHERE
  319. c_id = $course_id AND
  320. lp_view_id = ".$this->lp_view_id." AND
  321. lp_item_id IN ('".$lp_item_id_list_to_string."')
  322. ORDER BY view_count DESC ";
  323. if ($this->debug > 2) {
  324. error_log(
  325. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  326. 0
  327. );
  328. }
  329. $status_list = array();
  330. $res = Database::query($sql);
  331. while ($row = Database:: fetch_array($res)) {
  332. $status_list[$row['lp_item_id']] = $row['status'];
  333. }
  334. foreach ($lp_item_id_list as $item_id) {
  335. if (isset($status_list[$item_id])) {
  336. $status = $status_list[$item_id];
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status($status);
  339. if (empty($status)) {
  340. $this->items[$item_id]->set_status(
  341. $this->default_status
  342. );
  343. }
  344. }
  345. } else {
  346. if (!api_is_invitee()) {
  347. if (is_object($this->items[$item_id])) {
  348. $this->items[$item_id]->set_status(
  349. $this->default_status
  350. );
  351. }
  352. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  353. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  354. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  355. if ($this->debug > 2) {
  356. error_log(
  357. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  358. 0
  359. );
  360. }
  361. $this->items[$item_id]->set_lp_view(
  362. $this->lp_view_id,
  363. $course_id
  364. );
  365. Database::query($sql);
  366. $insertId = Database::insert_id();
  367. $sql = "UPDATE $lp_item_view_table SET id = iid
  368. WHERE iid = $insertId";
  369. Database::query($sql);
  370. }
  371. }
  372. }
  373. }
  374. }
  375. $this->ordered_items = self::get_flat_ordered_items_list(
  376. $this->get_id(),
  377. 0,
  378. $course_id
  379. );
  380. $this->max_ordered_items = 0;
  381. foreach ($this->ordered_items as $index => $dummy) {
  382. if ($index > $this->max_ordered_items && !empty($dummy)) {
  383. $this->max_ordered_items = $index;
  384. }
  385. }
  386. // TODO: Define the current item better.
  387. $this->first();
  388. if ($this->debug > 2) {
  389. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  390. }
  391. return true;
  392. }
  393. /**
  394. * @return string
  395. */
  396. public function getCourseCode()
  397. {
  398. return $this->cc;
  399. }
  400. /**
  401. * @return int
  402. */
  403. public function get_course_int_id()
  404. {
  405. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  406. }
  407. /**
  408. * @param $course_id
  409. * @return int
  410. */
  411. public function set_course_int_id($course_id)
  412. {
  413. return $this->course_int_id = intval($course_id);
  414. }
  415. /**
  416. * Get the depth level of LP item
  417. * @param $in_tab_items
  418. * @param $in_current_item_id
  419. * @return int
  420. */
  421. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  422. {
  423. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  424. if ($parent_item_id == 0) {
  425. return 0;
  426. } else {
  427. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  428. }
  429. }
  430. /**
  431. * Function rewritten based on old_add_item() from Yannick Warnier.
  432. * Due the fact that users can decide where the item should come, I had to overlook this function and
  433. * I found it better to rewrite it. Old function is still available.
  434. * Added also the possibility to add a description.
  435. *
  436. * @param int $parent
  437. * @param int $previous
  438. * @param string $type
  439. * @param int resource ID (ref)
  440. * @param string $title
  441. * @param string $description
  442. * @param int $prerequisites
  443. * @param int $max_time_allowed
  444. * @param int $userId
  445. *
  446. * @return int
  447. */
  448. public function add_item(
  449. $parent,
  450. $previous,
  451. $type = 'dokeos_chapter',
  452. $id,
  453. $title,
  454. $description,
  455. $prerequisites = 0,
  456. $max_time_allowed = 0,
  457. $userId = 0
  458. ) {
  459. $course_id = $this->course_info['real_id'];
  460. if ($this->debug > 0) {
  461. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  462. }
  463. if (empty($course_id)) {
  464. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  465. $this->course_info = api_get_course_info($this->cc);
  466. $course_id = $this->course_info['real_id'];
  467. }
  468. $userId = empty($userId) ? api_get_user_id() : $userId;
  469. $sessionId = api_get_session_id();
  470. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  471. $_course = $this->course_info;
  472. $parent = intval($parent);
  473. $previous = intval($previous);
  474. $id = intval($id);
  475. $max_time_allowed = htmlentities($max_time_allowed);
  476. if (empty($max_time_allowed)) {
  477. $max_time_allowed = 0;
  478. }
  479. $sql = "SELECT COUNT(id) AS num
  480. FROM $tbl_lp_item
  481. WHERE
  482. c_id = $course_id AND
  483. lp_id = " . $this->get_id() . " AND
  484. parent_item_id = " . $parent;
  485. $res_count = Database::query($sql);
  486. $row = Database :: fetch_array($res_count);
  487. $num = $row['num'];
  488. if ($num > 0) {
  489. if ($previous == 0) {
  490. $sql = "SELECT id, next_item_id, display_order
  491. FROM " . $tbl_lp_item . "
  492. WHERE
  493. c_id = $course_id AND
  494. lp_id = " . $this->get_id() . " AND
  495. parent_item_id = " . $parent . " AND
  496. previous_item_id = 0 OR
  497. previous_item_id=" . $parent;
  498. $result = Database::query($sql);
  499. $row = Database :: fetch_array($result);
  500. $tmp_previous = 0;
  501. $next = $row['id'];
  502. $display_order = 0;
  503. } else {
  504. $previous = (int) $previous;
  505. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  506. FROM $tbl_lp_item
  507. WHERE
  508. c_id = $course_id AND
  509. lp_id = " . $this->get_id() . " AND
  510. id = " . $previous;
  511. $result = Database::query($sql);
  512. $row = Database:: fetch_array($result);
  513. $tmp_previous = $row['id'];
  514. $next = $row['next_item_id'];
  515. $display_order = $row['display_order'];
  516. }
  517. } else {
  518. $tmp_previous = 0;
  519. $next = 0;
  520. $display_order = 0;
  521. }
  522. $id = intval($id);
  523. $typeCleaned = Database::escape_string($type);
  524. if ($type == 'quiz') {
  525. $sql = 'SELECT SUM(ponderation)
  526. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  527. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  528. ON
  529. quiz_question.id = quiz_rel_question.question_id AND
  530. quiz_question.c_id = quiz_rel_question.c_id
  531. WHERE
  532. quiz_rel_question.exercice_id = '.$id." AND
  533. quiz_question.c_id = $course_id AND
  534. quiz_rel_question.c_id = $course_id ";
  535. $rsQuiz = Database::query($sql);
  536. $max_score = Database :: result($rsQuiz, 0, 0);
  537. // Disabling the exercise if we add it inside a LP
  538. $exercise = new Exercise($course_id);
  539. $exercise->read($id);
  540. $exercise->disable();
  541. $exercise->save();
  542. } else {
  543. $max_score = 100;
  544. }
  545. $params = array(
  546. "c_id" => $course_id,
  547. "lp_id" => $this->get_id(),
  548. "item_type" => $typeCleaned,
  549. "ref" => '',
  550. "title" => $title,
  551. "description" => $description,
  552. "path" => $id,
  553. "max_score" => $max_score,
  554. "parent_item_id" => $parent,
  555. "previous_item_id" => $previous,
  556. "next_item_id" => intval($next),
  557. "display_order" => $display_order + 1,
  558. "prerequisite" => $prerequisites,
  559. "max_time_allowed" => $max_time_allowed,
  560. 'min_score' => 0,
  561. 'launch_data' => '',
  562. );
  563. if ($prerequisites != 0) {
  564. $params['prerequisite'] = $prerequisites;
  565. }
  566. $new_item_id = Database::insert($tbl_lp_item, $params);
  567. if ($this->debug > 2) {
  568. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  569. }
  570. if ($new_item_id) {
  571. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  572. Database::query($sql);
  573. // Update the item that should come after the new item.
  574. $sql = " UPDATE $tbl_lp_item SET
  575. previous_item_id = $new_item_id,
  576. next_item_id = $new_item_id,
  577. id = $new_item_id
  578. WHERE iid = $new_item_id";
  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 ($publicated_on == '0000-00-00 00:00:00' || 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 = '';
  711. } else {
  712. $publicated_on = api_get_utc_datetime();
  713. }
  714. } else {
  715. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  716. }
  717. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  718. $expired_on = '';
  719. } else {
  720. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  721. }
  722. while (Database :: num_rows($res_name)) {
  723. // There is already one such name, update the current one a bit.
  724. $i++;
  725. $name = $name . ' - ' . $i;
  726. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  727. $res_name = Database::query($check_name);
  728. }
  729. // New name does not exist yet; keep it.
  730. // Escape description.
  731. // Kevin: added htmlentities().
  732. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  733. $type = 1;
  734. switch ($learnpath) {
  735. case 'guess':
  736. break;
  737. case 'dokeos':
  738. case 'chamilo':
  739. $type = 1;
  740. break;
  741. case 'aicc':
  742. break;
  743. }
  744. switch ($origin) {
  745. case 'zip':
  746. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  747. break;
  748. case 'manual':
  749. default:
  750. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  751. $res_max = Database::query($get_max);
  752. if (Database :: num_rows($res_max) < 1) {
  753. $dsp = 1;
  754. } else {
  755. $row = Database :: fetch_array($res_max);
  756. $dsp = $row[0] + 1;
  757. }
  758. $params = [
  759. 'c_id' => $course_id,
  760. 'lp_type' => $type,
  761. 'name' => $name,
  762. 'description' => $description,
  763. 'path' => '',
  764. 'default_view_mod' => 'embedded',
  765. 'default_encoding' => 'UTF-8',
  766. 'display_order' => $dsp,
  767. 'content_maker' => 'Chamilo',
  768. 'content_local' => 'local',
  769. 'js_lib' => '',
  770. 'session_id' => $session_id,
  771. 'created_on' => api_get_utc_datetime(),
  772. 'modified_on' => api_get_utc_datetime(),
  773. 'publicated_on' => $publicated_on,
  774. 'expired_on' => $expired_on,
  775. 'category_id' => $categoryId,
  776. 'force_commit' => 0,
  777. 'content_license' => '',
  778. 'debug' => 0,
  779. 'theme' => '',
  780. 'preview_image' => '',
  781. 'author' => '',
  782. 'prerequisite' => 0,
  783. 'hide_toc_frame' => 0,
  784. 'seriousgame_mode' => 0,
  785. 'autolaunch' => 0,
  786. 'max_attempts' => 0,
  787. 'subscribe_users' => 0
  788. ];
  789. $id = Database::insert($tbl_lp, $params);
  790. if ($id > 0) {
  791. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  792. Database::query($sql);
  793. // Insert into item_property.
  794. api_item_property_update(
  795. $courseInfo,
  796. TOOL_LEARNPATH,
  797. $id,
  798. 'LearnpathAdded',
  799. $userId
  800. );
  801. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  802. return $id;
  803. }
  804. break;
  805. }
  806. }
  807. /**
  808. * Auto completes the parents of an item in case it's been completed or passed
  809. * @param integer $item Optional ID of the item from which to look for parents
  810. */
  811. public function autocomplete_parents($item)
  812. {
  813. $debug = $this->debug;
  814. if ($debug) {
  815. error_log('Learnpath::autocomplete_parents()', 0);
  816. }
  817. if (empty($item)) {
  818. $item = $this->current;
  819. }
  820. $currentItem = $this->getItem($item);
  821. if ($currentItem) {
  822. $parent_id = $currentItem->get_parent();
  823. $parent = $this->getItem($parent_id);
  824. if ($parent) {
  825. // if $item points to an object and there is a parent.
  826. if ($debug) {
  827. error_log(
  828. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  829. 0
  830. );
  831. }
  832. // New experiment including failed and browsed in completed status.
  833. //$current_status = $currentItem->get_status();
  834. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  835. // Fixes chapter auto complete
  836. if (true) {
  837. // If the current item is completed or passes or succeeded.
  838. $updateParentStatus = true;
  839. if ($debug) {
  840. error_log('Status of current item is alright', 0);
  841. }
  842. foreach ($parent->get_children() as $childItemId) {
  843. $childItem = $this->getItem($childItemId);
  844. // If children was not set try to get the info
  845. if (empty($childItem->db_item_view_id)) {
  846. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  847. }
  848. // Check all his brothers (parent's children) for completion status.
  849. if ($childItemId != $item) {
  850. if ($debug) {
  851. error_log(
  852. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  853. 0
  854. );
  855. }
  856. // Trying completing parents of failed and browsed items as well.
  857. if ($childItem->status_is(
  858. array(
  859. 'completed',
  860. 'passed',
  861. 'succeeded',
  862. 'browsed',
  863. 'failed'
  864. )
  865. )
  866. ) {
  867. // Keep completion status to true.
  868. continue;
  869. } else {
  870. if ($debug > 2) {
  871. error_log(
  872. '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,
  873. 0
  874. );
  875. }
  876. $updateParentStatus = false;
  877. break;
  878. }
  879. }
  880. }
  881. if ($updateParentStatus) {
  882. // If all the children were completed:
  883. $parent->set_status('completed');
  884. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  885. // Force the status to "completed"
  886. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  887. $this->update_queue[$parent->get_id()] = 'completed';
  888. if ($debug) {
  889. error_log(
  890. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  891. print_r($this->update_queue, 1),
  892. 0
  893. );
  894. }
  895. // Recursive call.
  896. $this->autocomplete_parents($parent->get_id());
  897. }
  898. }
  899. } else {
  900. if ($debug) {
  901. error_log("Parent #$parent_id does not exists");
  902. }
  903. }
  904. } else {
  905. if ($debug) {
  906. error_log("#$item is an item that doesn't have parents");
  907. }
  908. }
  909. }
  910. /**
  911. * Auto saves the current results into the database for the whole learnpath
  912. */
  913. public function autosave()
  914. {
  915. if ($this->debug > 0) {
  916. error_log('New LP - In learnpath::autosave()', 0);
  917. }
  918. // TODO: Add save operations for the learnpath itself.
  919. }
  920. /**
  921. * Closes the current resource
  922. *
  923. * Stops the timer
  924. * Saves into the database if required
  925. * Clears the current resource data from this object
  926. * @return boolean True on success, false on failure
  927. */
  928. public function close()
  929. {
  930. if ($this->debug > 0) {
  931. error_log('New LP - In learnpath::close()', 0);
  932. }
  933. if (empty ($this->lp_id)) {
  934. $this->error = 'Trying to close this learnpath but no ID is set';
  935. return false;
  936. }
  937. $this->current_time_stop = time();
  938. $this->ordered_items = array();
  939. $this->index = 0;
  940. unset ($this->lp_id);
  941. //unset other stuff
  942. return true;
  943. }
  944. /**
  945. * Static admin function allowing removal of a learnpath
  946. * @param array $courseInfo
  947. * @param integer Learnpath ID
  948. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  949. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  950. */
  951. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  952. {
  953. $course_id = api_get_course_int_id();
  954. if (!empty($courseInfo)) {
  955. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  956. }
  957. // TODO: Implement a way of getting this to work when the current object is not set.
  958. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  959. // If an ID is specifically given and the current LP is not the same, prevent delete.
  960. if (!empty ($id) && ($id != $this->lp_id)) {
  961. return false;
  962. }
  963. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  964. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  965. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  966. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  967. // Delete lp item id.
  968. foreach ($this->items as $id => $dummy) {
  969. $sql = "DELETE FROM $lp_item_view
  970. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  971. Database::query($sql);
  972. }
  973. // Proposed by Christophe (nickname: clefevre)
  974. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  975. Database::query($sql);
  976. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  977. Database::query($sql);
  978. self::toggle_publish($this->lp_id, 'i');
  979. if ($this->type == 2 || $this->type == 3) {
  980. // This is a scorm learning path, delete the files as well.
  981. $sql = "SELECT path FROM $lp
  982. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  983. $res = Database::query($sql);
  984. if (Database :: num_rows($res) > 0) {
  985. $row = Database :: fetch_array($res);
  986. $path = $row['path'];
  987. $sql = "SELECT id FROM $lp
  988. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  989. $res = Database::query($sql);
  990. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  991. if ($this->debug > 2) {
  992. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  993. }
  994. } else {
  995. // No other LP uses that directory, delete it.
  996. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  997. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  998. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  999. if ($this->debug > 2) {
  1000. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1001. }
  1002. // Proposed by Christophe (clefevre).
  1003. if (strcmp(substr($path, -2), "/.") == 0) {
  1004. $path = substr($path, 0, -1); // Remove "." at the end.
  1005. }
  1006. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1007. rmdirr($course_scorm_dir . $path);
  1008. }
  1009. }
  1010. }
  1011. }
  1012. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  1013. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1014. // Delete tools
  1015. $sql = "DELETE FROM $tbl_tool
  1016. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1017. Database::query($sql);
  1018. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1019. Database::query($sql);
  1020. // Updates the display order of all lps.
  1021. $this->update_display_order();
  1022. api_item_property_update(
  1023. api_get_course_info(),
  1024. TOOL_LEARNPATH,
  1025. $this->lp_id,
  1026. 'delete',
  1027. api_get_user_id()
  1028. );
  1029. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1030. if ($link_info !== false) {
  1031. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1032. }
  1033. if (api_get_setting('search_enabled') == 'true') {
  1034. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1035. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1036. }
  1037. }
  1038. /**
  1039. * Removes all the children of one item - dangerous!
  1040. * @param integer $id Element ID of which children have to be removed
  1041. * @return integer Total number of children removed
  1042. */
  1043. public function delete_children_items($id)
  1044. {
  1045. $course_id = $this->course_info['real_id'];
  1046. if ($this->debug > 0) {
  1047. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1048. }
  1049. $num = 0;
  1050. if (empty ($id) || $id != strval(intval($id))) {
  1051. return false;
  1052. }
  1053. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1054. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1055. $res = Database::query($sql);
  1056. while ($row = Database :: fetch_array($res)) {
  1057. $num += $this->delete_children_items($row['id']);
  1058. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1059. Database::query($sql_del);
  1060. $num++;
  1061. }
  1062. return $num;
  1063. }
  1064. /**
  1065. * Removes an item from the current learnpath
  1066. * @param integer $id Elem ID (0 if first)
  1067. * @param integer $remove Whether to remove the resource/data from the
  1068. * system or leave it (default: 'keep', others 'remove')
  1069. * @return integer Number of elements moved
  1070. * @todo implement resource removal
  1071. */
  1072. public function delete_item($id, $remove = 'keep')
  1073. {
  1074. $course_id = api_get_course_int_id();
  1075. if ($this->debug > 0) {
  1076. error_log('New LP - In learnpath::delete_item()', 0);
  1077. }
  1078. // TODO: Implement the resource removal.
  1079. if (empty ($id) || $id != strval(intval($id))) {
  1080. return false;
  1081. }
  1082. // First select item to get previous, next, and display order.
  1083. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1084. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1085. $res_sel = Database::query($sql_sel);
  1086. if (Database :: num_rows($res_sel) < 1) {
  1087. return false;
  1088. }
  1089. $row = Database :: fetch_array($res_sel);
  1090. $previous = $row['previous_item_id'];
  1091. $next = $row['next_item_id'];
  1092. $display = $row['display_order'];
  1093. $parent = $row['parent_item_id'];
  1094. $lp = $row['lp_id'];
  1095. // Delete children items.
  1096. $num = $this->delete_children_items($id);
  1097. if ($this->debug > 2) {
  1098. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1099. }
  1100. // Now delete the item.
  1101. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1102. if ($this->debug > 2) {
  1103. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1104. }
  1105. Database::query($sql_del);
  1106. // Now update surrounding items.
  1107. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1108. WHERE c_id = ".$course_id." AND id = $previous";
  1109. Database::query($sql_upd);
  1110. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1111. WHERE c_id = ".$course_id." AND id = $next";
  1112. Database::query($sql_upd);
  1113. // Now update all following items with new display order.
  1114. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1115. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1116. Database::query($sql_all);
  1117. //Removing prerequisites since the item will not longer exist
  1118. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1119. Database::query($sql_all);
  1120. // Remove from search engine if enabled.
  1121. if (api_get_setting('search_enabled') == 'true') {
  1122. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1123. $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';
  1124. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1125. $res = Database::query($sql);
  1126. if (Database :: num_rows($res) > 0) {
  1127. $row2 = Database :: fetch_array($res);
  1128. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1129. $di = new ChamiloIndexer();
  1130. $di->remove_document((int) $row2['search_did']);
  1131. }
  1132. $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';
  1133. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1134. Database::query($sql);
  1135. }
  1136. }
  1137. /**
  1138. * Updates an item's content in place
  1139. * @param integer $id Element ID
  1140. * @param integer $parent Parent item ID
  1141. * @param integer $previous Previous item ID
  1142. * @param string $title Item title
  1143. * @param string $description Item description
  1144. * @param string $prerequisites Prerequisites (optional)
  1145. * @param array $audio The array resulting of the $_FILES[mp3] element
  1146. * @param int $max_time_allowed
  1147. * @param string $url
  1148. * @return boolean True on success, false on error
  1149. */
  1150. public function edit_item(
  1151. $id,
  1152. $parent,
  1153. $previous,
  1154. $title,
  1155. $description,
  1156. $prerequisites = '0',
  1157. $audio = array(),
  1158. $max_time_allowed = 0,
  1159. $url = ''
  1160. ) {
  1161. $course_id = api_get_course_int_id();
  1162. $_course = api_get_course_info();
  1163. if ($this->debug > 0) {
  1164. error_log('New LP - In learnpath::edit_item()', 0);
  1165. }
  1166. if (empty ($max_time_allowed)) {
  1167. $max_time_allowed = 0;
  1168. }
  1169. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1170. return false;
  1171. }
  1172. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1173. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1174. $res_select = Database::query($sql_select);
  1175. $row_select = Database :: fetch_array($res_select);
  1176. $audio_update_sql = '';
  1177. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1178. // Create the audio folder if it does not exist yet.
  1179. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1180. if (!is_dir($filepath . 'audio')) {
  1181. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1182. $audio_id = add_document(
  1183. $_course,
  1184. '/audio',
  1185. 'folder',
  1186. 0,
  1187. 'audio'
  1188. );
  1189. api_item_property_update(
  1190. $_course,
  1191. TOOL_DOCUMENT,
  1192. $audio_id,
  1193. 'FolderCreated',
  1194. api_get_user_id(),
  1195. null,
  1196. null,
  1197. null,
  1198. null,
  1199. api_get_session_id()
  1200. );
  1201. api_item_property_update(
  1202. $_course,
  1203. TOOL_DOCUMENT,
  1204. $audio_id,
  1205. 'invisible',
  1206. api_get_user_id(),
  1207. null,
  1208. null,
  1209. null,
  1210. null,
  1211. api_get_session_id()
  1212. );
  1213. }
  1214. // Upload file in documents.
  1215. $pi = pathinfo($audio['name']);
  1216. if ($pi['extension'] == 'mp3') {
  1217. $c_det = api_get_course_info($this->cc);
  1218. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1219. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1220. $path = substr($path, 7);
  1221. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1222. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1223. }
  1224. }
  1225. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1226. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1227. // TODO: htmlspecialchars to be checked for encoding related problems.
  1228. if ($same_parent && $same_previous) {
  1229. // Only update title and description.
  1230. $sql = "UPDATE " . $tbl_lp_item . "
  1231. SET title = '" . Database::escape_string($title) . "',
  1232. prerequisite = '" . $prerequisites . "',
  1233. description = '" . Database::escape_string($description) . "'
  1234. " . $audio_update_sql . ",
  1235. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1236. WHERE c_id = ".$course_id." AND id = " . $id;
  1237. Database::query($sql);
  1238. } else {
  1239. $old_parent = $row_select['parent_item_id'];
  1240. $old_previous = $row_select['previous_item_id'];
  1241. $old_next = $row_select['next_item_id'];
  1242. $old_order = $row_select['display_order'];
  1243. $old_prerequisite = $row_select['prerequisite'];
  1244. $old_max_time_allowed = $row_select['max_time_allowed'];
  1245. /* BEGIN -- virtually remove the current item id */
  1246. /* for the next and previous item it is like the current item doesn't exist anymore */
  1247. if ($old_previous != 0) {
  1248. // Next
  1249. $sql = "UPDATE " . $tbl_lp_item . "
  1250. SET next_item_id = " . $old_next . "
  1251. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1252. Database::query($sql);
  1253. }
  1254. if ($old_next != 0) {
  1255. // Previous
  1256. $sql = "UPDATE " . $tbl_lp_item . "
  1257. SET previous_item_id = " . $old_previous . "
  1258. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1259. Database::query($sql);
  1260. }
  1261. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1262. $sql = "UPDATE " . $tbl_lp_item . "
  1263. SET display_order = display_order - 1
  1264. WHERE
  1265. c_id = ".$course_id." AND
  1266. display_order > " . $old_order . " AND
  1267. lp_id = " . $this->lp_id . " AND
  1268. parent_item_id = " . $old_parent;
  1269. Database::query($sql);
  1270. /* END -- virtually remove the current item id */
  1271. /* BEGIN -- update the current item id to his new location */
  1272. if ($previous == 0) {
  1273. // Select the data of the item that should come after the current item.
  1274. $sql = "SELECT id, display_order
  1275. FROM " . $tbl_lp_item . "
  1276. WHERE
  1277. c_id = ".$course_id." AND
  1278. lp_id = " . $this->lp_id . " AND
  1279. parent_item_id = " . $parent . " AND
  1280. previous_item_id = " . $previous;
  1281. $res_select_old = Database::query($sql);
  1282. $row_select_old = Database::fetch_array($res_select_old);
  1283. // If the new parent didn't have children before.
  1284. if (Database :: num_rows($res_select_old) == 0) {
  1285. $new_next = 0;
  1286. $new_order = 1;
  1287. } else {
  1288. $new_next = $row_select_old['id'];
  1289. $new_order = $row_select_old['display_order'];
  1290. }
  1291. } else {
  1292. // Select the data of the item that should come before the current item.
  1293. $sql = "SELECT next_item_id, display_order
  1294. FROM " . $tbl_lp_item . "
  1295. WHERE c_id = ".$course_id." AND id = " . $previous;
  1296. $res_select_old = Database::query($sql);
  1297. $row_select_old = Database :: fetch_array($res_select_old);
  1298. $new_next = $row_select_old['next_item_id'];
  1299. $new_order = $row_select_old['display_order'] + 1;
  1300. }
  1301. // TODO: htmlspecialchars to be checked for encoding related problems.
  1302. // Update the current item with the new data.
  1303. $sql = "UPDATE " . $tbl_lp_item . "
  1304. SET
  1305. title = '" . Database::escape_string($title) . "',
  1306. description = '" . Database::escape_string($description) . "',
  1307. parent_item_id = " . $parent . ",
  1308. previous_item_id = " . $previous . ",
  1309. next_item_id = " . $new_next . ",
  1310. display_order = " . $new_order . "
  1311. " . $audio_update_sql . "
  1312. WHERE c_id = ".$course_id." AND id = " . $id;
  1313. Database::query($sql);
  1314. if ($previous != 0) {
  1315. // Update the previous item's next_item_id.
  1316. $sql = "UPDATE " . $tbl_lp_item . "
  1317. SET next_item_id = " . $id . "
  1318. WHERE c_id = ".$course_id." AND id = " . $previous;
  1319. Database::query($sql);
  1320. }
  1321. if ($new_next != 0) {
  1322. // Update the next item's previous_item_id.
  1323. $sql = "UPDATE " . $tbl_lp_item . "
  1324. SET previous_item_id = " . $id . "
  1325. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1326. Database::query($sql);
  1327. }
  1328. if ($old_prerequisite != $prerequisites) {
  1329. $sql = "UPDATE " . $tbl_lp_item . "
  1330. SET prerequisite = '" . $prerequisites . "'
  1331. WHERE c_id = ".$course_id." AND id = " . $id;
  1332. Database::query($sql);
  1333. }
  1334. if ($old_max_time_allowed != $max_time_allowed) {
  1335. // update max time allowed
  1336. $sql = "UPDATE " . $tbl_lp_item . "
  1337. SET max_time_allowed = " . $max_time_allowed . "
  1338. WHERE c_id = ".$course_id." AND id = " . $id;
  1339. Database::query($sql);
  1340. }
  1341. // Update all the items with the same or a bigger display_order than the current item.
  1342. $sql = "UPDATE " . $tbl_lp_item . "
  1343. SET display_order = display_order + 1
  1344. WHERE
  1345. c_id = ".$course_id." AND
  1346. lp_id = " . $this->get_id() . " AND
  1347. id <> " . $id . " AND
  1348. parent_item_id = " . $parent . " AND
  1349. display_order >= " . $new_order;
  1350. Database::query($sql);
  1351. }
  1352. if ($row_select['item_type'] == 'link') {
  1353. $link = new Link();
  1354. $linkId = $row_select['path'];
  1355. $link->updateLink($linkId, $url);
  1356. }
  1357. }
  1358. /**
  1359. * Updates an item's prereq in place
  1360. * @param integer $id Element ID
  1361. * @param string $prerequisite_id Prerequisite Element ID
  1362. * @param int $mastery_score Prerequisite min score
  1363. * @param int $max_score Prerequisite max score
  1364. *
  1365. * @return boolean True on success, false on error
  1366. */
  1367. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1368. {
  1369. $course_id = api_get_course_int_id();
  1370. if ($this->debug > 0) {
  1371. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1372. }
  1373. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1374. return false;
  1375. }
  1376. $prerequisite_id = intval($prerequisite_id);
  1377. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1378. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1379. $mastery_score = 0;
  1380. }
  1381. if (!is_numeric($max_score) || $max_score < 0) {
  1382. $max_score = 100;
  1383. }
  1384. /*if ($mastery_score > $max_score) {
  1385. $max_score = $mastery_score;
  1386. }*/
  1387. if (!is_numeric($prerequisite_id)) {
  1388. $prerequisite_id = 'NULL';
  1389. }
  1390. $mastery_score = floatval($mastery_score);
  1391. $max_score = floatval($max_score);
  1392. $sql = " UPDATE $tbl_lp_item
  1393. SET
  1394. prerequisite = $prerequisite_id ,
  1395. prerequisite_min_score = $mastery_score ,
  1396. prerequisite_max_score = $max_score
  1397. WHERE c_id = $course_id AND id = $id";
  1398. Database::query($sql);
  1399. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1400. // Will this be enough to ensure unicity?
  1401. /*$sql = " UPDATE $tbl_lp_item
  1402. SET mastery_score = $mastery_score
  1403. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1404. Database::query($sql);*/
  1405. }
  1406. // TODO: Update the item object (can be ignored for now because refreshed).
  1407. return true;
  1408. }
  1409. /**
  1410. * Escapes a string with the available database escape function
  1411. * @param string String to escape
  1412. * @return string String escaped
  1413. * @deprecated use Database::escape_string
  1414. */
  1415. public function escape_string($string)
  1416. {
  1417. return Database::escape_string($string);
  1418. }
  1419. /**
  1420. * Static admin function exporting a learnpath into a zip file
  1421. * @param string Export type (scorm, zip, cd)
  1422. * @param string Course code
  1423. * @param integer Learnpath ID
  1424. * @param string Zip file name
  1425. * @return string Zip file path (or false on error)
  1426. */
  1427. public function export_lp($type, $course, $id, $zipname)
  1428. {
  1429. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1430. return false;
  1431. }
  1432. $url = '';
  1433. switch ($type) {
  1434. case 'scorm':
  1435. break;
  1436. case 'zip':
  1437. break;
  1438. case 'cdrom':
  1439. break;
  1440. }
  1441. return $url;
  1442. }
  1443. /**
  1444. * Gets all the chapters belonging to the same parent as the item/chapter given
  1445. * Can also be called as abstract method
  1446. * @param integer Item ID
  1447. * @return array A list of all the "brother items" (or an empty array on failure)
  1448. */
  1449. public function get_brother_chapters($id)
  1450. {
  1451. $course_id = api_get_course_int_id();
  1452. if ($this->debug > 0) {
  1453. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1454. }
  1455. if (empty($id) || $id != strval(intval($id))) {
  1456. return array ();
  1457. }
  1458. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1459. $sql_parent = "SELECT * FROM $lp_item
  1460. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1461. $res_parent = Database::query($sql_parent);
  1462. if (Database :: num_rows($res_parent) > 0) {
  1463. $row_parent = Database :: fetch_array($res_parent);
  1464. $parent = $row_parent['parent_item_id'];
  1465. $sql = "SELECT * FROM $lp_item
  1466. WHERE
  1467. c_id = ".$course_id." AND
  1468. parent_item_id = $parent AND
  1469. id = $id AND
  1470. item_type='dokeos_chapter'
  1471. ORDER BY display_order";
  1472. $res_bros = Database::query($sql);
  1473. $list = array();
  1474. while ($row_bro = Database :: fetch_array($res_bros)) {
  1475. $list[] = $row_bro;
  1476. }
  1477. return $list;
  1478. }
  1479. return array();
  1480. }
  1481. /**
  1482. * Gets all the items belonging to the same parent as the item given
  1483. * Can also be called as abstract method
  1484. * @param integer Item ID
  1485. * @return array A list of all the "brother items" (or an empty array on failure)
  1486. */
  1487. public function get_brother_items($id)
  1488. {
  1489. $course_id = api_get_course_int_id();
  1490. if ($this->debug > 0) {
  1491. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1492. }
  1493. if (empty ($id) || $id != strval(intval($id))) {
  1494. return array ();
  1495. }
  1496. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1497. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1498. $res_parent = Database::query($sql_parent);
  1499. if (Database :: num_rows($res_parent) > 0) {
  1500. $row_parent = Database :: fetch_array($res_parent);
  1501. $parent = $row_parent['parent_item_id'];
  1502. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1503. ORDER BY display_order";
  1504. $res_bros = Database::query($sql_bros);
  1505. $list = array ();
  1506. while ($row_bro = Database :: fetch_array($res_bros)) {
  1507. $list[] = $row_bro;
  1508. }
  1509. return $list;
  1510. }
  1511. return array ();
  1512. }
  1513. /**
  1514. * Get the specific prefix index terms of this learning path
  1515. * @param string $prefix
  1516. * @return array Array of terms
  1517. */
  1518. public function get_common_index_terms_by_prefix($prefix)
  1519. {
  1520. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1521. $terms = get_specific_field_values_list_by_prefix(
  1522. $prefix,
  1523. $this->cc,
  1524. TOOL_LEARNPATH,
  1525. $this->lp_id
  1526. );
  1527. $prefix_terms = array();
  1528. if (!empty($terms)) {
  1529. foreach ($terms as $term) {
  1530. $prefix_terms[] = $term['value'];
  1531. }
  1532. }
  1533. return $prefix_terms;
  1534. }
  1535. /**
  1536. * Gets the number of items currently completed
  1537. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1538. * @return integer The number of items currently completed
  1539. */
  1540. public function get_complete_items_count($failedStatusException = false)
  1541. {
  1542. if ($this->debug > 0) {
  1543. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1544. }
  1545. $i = 0;
  1546. $completedStatusList = array(
  1547. 'completed',
  1548. 'passed',
  1549. 'succeeded',
  1550. 'browsed'
  1551. );
  1552. if (!$failedStatusException) {
  1553. $completedStatusList[] = 'failed';
  1554. }
  1555. foreach ($this->items as $id => $dummy) {
  1556. // Trying failed and browsed considered "progressed" as well.
  1557. if ($this->items[$id]->status_is($completedStatusList) &&
  1558. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1559. $this->items[$id]->get_type() != 'dir'
  1560. ) {
  1561. $i++;
  1562. }
  1563. }
  1564. return $i;
  1565. }
  1566. /**
  1567. * Gets the current item ID
  1568. * @return integer The current learnpath item id
  1569. */
  1570. public function get_current_item_id()
  1571. {
  1572. $current = 0;
  1573. if ($this->debug > 0) {
  1574. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1575. }
  1576. if (!empty($this->current)) {
  1577. $current = $this->current;
  1578. }
  1579. if ($this->debug > 2) {
  1580. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1581. }
  1582. return $current;
  1583. }
  1584. /**
  1585. * Force to get the first learnpath item id
  1586. * @return integer The current learnpath item id
  1587. */
  1588. public function get_first_item_id()
  1589. {
  1590. $current = 0;
  1591. if (is_array($this->ordered_items)) {
  1592. $current = $this->ordered_items[0];
  1593. }
  1594. return $current;
  1595. }
  1596. /**
  1597. * Gets the total number of items available for viewing in this SCORM
  1598. * @return integer The total number of items
  1599. */
  1600. public function get_total_items_count()
  1601. {
  1602. if ($this->debug > 0) {
  1603. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1604. }
  1605. return count($this->items);
  1606. }
  1607. /**
  1608. * Gets the total number of items available for viewing in this SCORM but without chapters
  1609. * @return integer The total no-chapters number of items
  1610. */
  1611. public function get_total_items_count_without_chapters()
  1612. {
  1613. if ($this->debug > 0) {
  1614. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1615. }
  1616. $total = 0;
  1617. $typeListNotToCount = self::getChapterTypes();
  1618. foreach ($this->items as $temp2) {
  1619. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1620. $total++;
  1621. }
  1622. }
  1623. return $total;
  1624. }
  1625. /**
  1626. * Gets the first element URL.
  1627. * @return string URL to load into the viewer
  1628. */
  1629. public function first()
  1630. {
  1631. if ($this->debug > 0) {
  1632. error_log('New LP - In learnpath::first()', 0);
  1633. error_log('$this->last_item_seen '.$this->last_item_seen);
  1634. }
  1635. // Test if the last_item_seen exists and is not a dir.
  1636. if (count($this->ordered_items) == 0) {
  1637. $this->index = 0;
  1638. }
  1639. if ($this->debug > 0) {
  1640. if (isset($this->items[$this->last_item_seen])) {
  1641. $status = $this->items[$this->last_item_seen]->get_status();
  1642. }
  1643. error_log('status '.$status);
  1644. }
  1645. if (!empty($this->last_item_seen) &&
  1646. !empty($this->items[$this->last_item_seen]) &&
  1647. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1648. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1649. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1650. //&& !$this->items[$this->last_item_seen]->is_done()
  1651. ) {
  1652. if ($this->debug > 2) {
  1653. 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);
  1654. }
  1655. $index = -1;
  1656. foreach ($this->ordered_items as $myindex => $item_id) {
  1657. if ($item_id == $this->last_item_seen) {
  1658. $index = $myindex;
  1659. break;
  1660. }
  1661. }
  1662. if ($index == -1) {
  1663. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1664. if ($this->debug > 2) {
  1665. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1666. }
  1667. return false;
  1668. } else {
  1669. $this->last = $this->last_item_seen;
  1670. $this->current = $this->last_item_seen;
  1671. $this->index = $index;
  1672. }
  1673. } else {
  1674. if ($this->debug > 2) {
  1675. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1676. }
  1677. $index = 0;
  1678. // Loop through all ordered items and stop at the first item that is
  1679. // not a directory *and* that has not been completed yet.
  1680. while ( !empty($this->ordered_items[$index]) AND
  1681. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1682. (
  1683. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1684. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' 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 = '')
  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. if ($this->debug > 0) {
  1798. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1799. }
  1800. if(empty($idBar)){
  1801. $idBar='control-top';
  1802. }
  1803. /* if(empty($display)){
  1804. $display='display:block';
  1805. } */
  1806. $navbar = null;
  1807. $lp_id = $this->lp_id;
  1808. $mycurrentitemid = $this->get_current_item_id();
  1809. if ($this->mode == 'fullscreen') {
  1810. $navbar = '
  1811. <span id="'.$idBar.'" class="buttons">
  1812. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link">
  1813. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1814. </a>
  1815. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1816. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1817. </a>
  1818. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1819. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1820. </a>
  1821. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1822. <span class="fa fa-columns"></span><span class="sr-only">' . get_lang('ScormExitFullScreen') . '</span>
  1823. </a>
  1824. </span>';
  1825. } else {
  1826. $navbar = '
  1827. <span id="'.$idBar.'" class="buttons text-right">
  1828. <a class="icon-toolbar" href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link">
  1829. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1830. </a>
  1831. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1832. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1833. </a>
  1834. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1835. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1836. </a>
  1837. </span>';
  1838. }
  1839. return $navbar;
  1840. }
  1841. /**
  1842. * Gets the next resource in queue (url).
  1843. * @return string URL to load into the viewer
  1844. */
  1845. public function get_next_index()
  1846. {
  1847. if ($this->debug > 0) {
  1848. error_log('New LP - In learnpath::get_next_index()', 0);
  1849. }
  1850. // TODO
  1851. $index = $this->index;
  1852. $index++;
  1853. if ($this->debug > 2) {
  1854. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1855. }
  1856. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1857. $index++;
  1858. if ($index == $this->max_ordered_items){
  1859. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1860. return $this->index;
  1861. } else {
  1862. return $index;
  1863. }
  1864. }
  1865. }
  1866. if (empty ($this->ordered_items[$index])) {
  1867. return $this->index;
  1868. }
  1869. if ($this->debug > 2) {
  1870. error_log('New LP - index is now ' . $index, 0);
  1871. }
  1872. return $index;
  1873. }
  1874. /**
  1875. * Gets item_id for the next element
  1876. * @return integer Next item (DB) ID
  1877. */
  1878. public function get_next_item_id()
  1879. {
  1880. if ($this->debug > 0) {
  1881. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1882. }
  1883. $new_index = $this->get_next_index();
  1884. if (!empty ($new_index)) {
  1885. if (isset ($this->ordered_items[$new_index])) {
  1886. if ($this->debug > 2) {
  1887. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1888. }
  1889. return $this->ordered_items[$new_index];
  1890. }
  1891. }
  1892. if ($this->debug > 2) {
  1893. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1894. }
  1895. return 0;
  1896. }
  1897. /**
  1898. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1899. *
  1900. * Generally, the package provided is in the form of a zip file, so the function
  1901. * has been written to test a zip file. If not a zip, the function will return the
  1902. * default return value: ''
  1903. * @param string the path to the file
  1904. * @param string the original name of the file
  1905. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1906. */
  1907. public static function get_package_type($file_path, $file_name)
  1908. {
  1909. // Get name of the zip file without the extension.
  1910. $file_info = pathinfo($file_name);
  1911. $filename = $file_info['basename']; // Name including extension.
  1912. $extension = $file_info['extension']; // Extension only.
  1913. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1914. 'dll',
  1915. 'exe'
  1916. ))) {
  1917. return 'oogie';
  1918. }
  1919. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1920. 'dll',
  1921. 'exe'
  1922. ))) {
  1923. return 'woogie';
  1924. }
  1925. // Filename without its extension.
  1926. $file_base_name = str_replace('.' . $extension, '', $filename);
  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' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1992. $index--;
  1993. if ($index < 0) {
  1994. return $this->index;
  1995. }
  1996. }
  1997. } else {
  1998. if ($this->debug > 2) {
  1999. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2000. }
  2001. // There is no previous item.
  2002. }
  2003. return $index;
  2004. }
  2005. /**
  2006. * Gets item_id for the next element
  2007. * @return integer Previous item (DB) ID
  2008. */
  2009. public function get_previous_item_id()
  2010. {
  2011. if ($this->debug > 0) {
  2012. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2013. }
  2014. $new_index = $this->get_previous_index();
  2015. return $this->ordered_items[$new_index];
  2016. }
  2017. /**
  2018. * Gets the progress value from the progress_db attribute
  2019. * @return integer Current progress value
  2020. */
  2021. public function get_progress()
  2022. {
  2023. if ($this->debug > 0) {
  2024. error_log('New LP - In learnpath::get_progress()', 0);
  2025. }
  2026. if (!empty ($this->progress_db)) {
  2027. return $this->progress_db;
  2028. }
  2029. return 0;
  2030. }
  2031. /**
  2032. * Returns the HTML necessary to print a mediaplayer block inside a page
  2033. * @return string The mediaplayer HTML
  2034. */
  2035. public function get_mediaplayer($autostart = 'true')
  2036. {
  2037. $course_id = api_get_course_int_id();
  2038. $_course = api_get_course_info();
  2039. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2040. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2041. // Getting all the information about the item.
  2042. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2043. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2044. ON lp.id = lp_view.lp_item_id
  2045. WHERE
  2046. lp.id = '".$_SESSION['oLP']->current."' AND
  2047. lp.c_id = $course_id AND
  2048. lp_view.c_id = $course_id";
  2049. $result = Database::query($sql);
  2050. $row = Database::fetch_assoc($result);
  2051. $output = '';
  2052. if (!empty ($row['audio'])) {
  2053. $list = $_SESSION['oLP']->get_toc();
  2054. $type_quiz = false;
  2055. foreach($list as $toc) {
  2056. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2057. $type_quiz = true;
  2058. }
  2059. }
  2060. if ($type_quiz) {
  2061. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2062. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2063. } else {
  2064. $autostart_audio = $autostart;
  2065. }
  2066. } else {
  2067. $autostart_audio = 'true';
  2068. }
  2069. $courseInfo = api_get_course_info();
  2070. $audio = $row['audio'];
  2071. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2072. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2073. if (!file_exists($file)) {
  2074. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2075. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2076. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2077. }
  2078. $player = Display::getMediaPlayer(
  2079. $file,
  2080. array(
  2081. 'id' => 'lp_audio_media_player',
  2082. 'url' => $url,
  2083. 'autoplay' => $autostart_audio,
  2084. 'width' => '100%'
  2085. )
  2086. );
  2087. // The mp3 player.
  2088. $output = '<div id="container">';
  2089. $output .= $player;
  2090. $output .= '</div>';
  2091. }
  2092. return $output;
  2093. }
  2094. /**
  2095. * Checks if the learning path is visible for student after the progress
  2096. * of its prerequisite is completed, considering the time availability and
  2097. * the LP visibility.
  2098. * @param int $lp_id
  2099. * @param int $student_id
  2100. * @param string Course code (optional)
  2101. * @param int $sessionId
  2102. * @return bool
  2103. */
  2104. public static function is_lp_visible_for_student(
  2105. $lp_id,
  2106. $student_id,
  2107. $courseCode = null,
  2108. $sessionId = null
  2109. ) {
  2110. $lp_id = (int)$lp_id;
  2111. $courseInfo = api_get_course_info($courseCode);
  2112. $sessionId = intval($sessionId);
  2113. if (empty($sessionId)) {
  2114. $sessionId = api_get_session_id();
  2115. }
  2116. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2117. // Get current prerequisite
  2118. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2119. FROM $tbl_learnpath
  2120. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2121. $itemInfo = api_get_item_property_info(
  2122. $courseInfo['real_id'],
  2123. TOOL_LEARNPATH,
  2124. $lp_id,
  2125. $sessionId
  2126. );
  2127. // If the item was deleted.
  2128. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2129. return false;
  2130. }
  2131. $rs = Database::query($sql);
  2132. $now = time();
  2133. if (Database::num_rows($rs) > 0) {
  2134. $row = Database::fetch_array($rs, 'ASSOC');
  2135. $prerequisite = $row['prerequisite'];
  2136. $is_visible = true;
  2137. if (!empty($prerequisite)) {
  2138. $progress = self::getProgress(
  2139. $prerequisite,
  2140. $student_id,
  2141. $courseInfo['real_id'],
  2142. $sessionId
  2143. );
  2144. $progress = intval($progress);
  2145. if ($progress < 100) {
  2146. $is_visible = false;
  2147. }
  2148. }
  2149. // Also check the time availability of the LP
  2150. if ($is_visible) {
  2151. // Adding visibility restrictions
  2152. if (!empty($row['publicated_on']) &&
  2153. $row['publicated_on'] != '0000-00-00 00:00:00'
  2154. ) {
  2155. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2156. //api_not_allowed();
  2157. $is_visible = false;
  2158. }
  2159. }
  2160. // Blocking empty start times see BT#2800
  2161. global $_custom;
  2162. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2163. $_custom['lps_hidden_when_no_start_date']
  2164. ) {
  2165. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2166. //api_not_allowed();
  2167. $is_visible = false;
  2168. }
  2169. }
  2170. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2171. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2172. //api_not_allowed();
  2173. $is_visible = false;
  2174. }
  2175. }
  2176. }
  2177. // Check if the subscription users/group to a LP is ON
  2178. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2179. // Try group
  2180. $is_visible = false;
  2181. // Checking only the user visibility
  2182. $userVisibility = api_get_item_visibility(
  2183. $courseInfo,
  2184. 'learnpath',
  2185. $row['id'],
  2186. $sessionId,
  2187. $student_id,
  2188. 'LearnpathSubscription'
  2189. );
  2190. if ($userVisibility == 1) {
  2191. $is_visible = true;
  2192. } else {
  2193. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2194. if (!empty($userGroups)) {
  2195. foreach ($userGroups as $groupInfo) {
  2196. $groupId = $groupInfo['iid'];
  2197. $userVisibility = api_get_item_visibility(
  2198. $courseInfo,
  2199. 'learnpath',
  2200. $row['id'],
  2201. $sessionId,
  2202. null,
  2203. 'LearnpathSubscription',
  2204. $groupId
  2205. );
  2206. if ($userVisibility == 1) {
  2207. $is_visible = true;
  2208. break;
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. return $is_visible;
  2215. }
  2216. return false;
  2217. }
  2218. /**
  2219. * @param int $lpId
  2220. * @param int $userId
  2221. * @param int $courseId
  2222. * @param int $sessionId
  2223. * @return int
  2224. */
  2225. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2226. {
  2227. $lpId = intval($lpId);
  2228. $userId = intval($userId);
  2229. $courseId = intval($courseId);
  2230. $sessionId = intval($sessionId);
  2231. $progress = 0;
  2232. $sessionCondition = api_get_session_condition($sessionId);
  2233. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2234. $sql = "SELECT * FROM $table
  2235. WHERE
  2236. c_id = ".$courseId." AND
  2237. lp_id = $lpId AND
  2238. user_id = $userId $sessionCondition";
  2239. $res = Database::query($sql);
  2240. if (Database :: num_rows($res) > 0) {
  2241. $row = Database:: fetch_array($res);
  2242. $progress = $row['progress'];
  2243. }
  2244. return $progress;
  2245. }
  2246. /**
  2247. * Displays a progress bar
  2248. * completed so far.
  2249. * @param integer $percentage Progress value to display
  2250. * @param string $text_add Text to display near the progress value
  2251. * @return string HTML string containing the progress bar
  2252. */
  2253. public static function get_progress_bar($percentage = -1, $text_add = '')
  2254. {
  2255. $text = $percentage . $text_add;
  2256. $output = '<div class="progress">
  2257. <div id="progress_bar_value" class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2258. '. $text .'
  2259. </div>
  2260. </div>';
  2261. return $output;
  2262. }
  2263. /**
  2264. * @param string $mode can be '%' or 'abs'
  2265. * otherwise this value will be used $this->progress_bar_mode
  2266. * @return string
  2267. */
  2268. public function getProgressBar($mode = null)
  2269. {
  2270. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2271. return self::get_progress_bar($percentage, $text_add);
  2272. }
  2273. /**
  2274. * Gets the progress bar info to display inside the progress bar.
  2275. * Also used by scorm_api.php
  2276. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2277. * we display a number of completed elements per total elements
  2278. * @param integer $add Additional steps to fake as completed
  2279. * @return list array Percentage or number and symbol (% or /xx)
  2280. */
  2281. public function get_progress_bar_text($mode = '', $add = 0)
  2282. {
  2283. if ($this->debug > 0) {
  2284. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2285. }
  2286. if (empty($mode)) {
  2287. $mode = $this->progress_bar_mode;
  2288. }
  2289. $total_items = $this->get_total_items_count_without_chapters();
  2290. if ($this->debug > 2) {
  2291. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2292. }
  2293. $completeItems = $this->get_complete_items_count();
  2294. if ($this->debug > 2) {
  2295. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2296. }
  2297. if ($add != 0) {
  2298. $completeItems += $add;
  2299. if ($this->debug > 2) {
  2300. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2301. }
  2302. }
  2303. $text = '';
  2304. if ($completeItems > $total_items) {
  2305. $completeItems = $total_items;
  2306. }
  2307. $percentage = 0;
  2308. if ($mode == '%') {
  2309. if ($total_items > 0) {
  2310. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2311. } else {
  2312. $percentage = 0;
  2313. }
  2314. $percentage = number_format($percentage, 0);
  2315. $text = '%';
  2316. } elseif ($mode == 'abs') {
  2317. $percentage = $completeItems;
  2318. $text = '/' . $total_items;
  2319. }
  2320. return array(
  2321. $percentage,
  2322. $text
  2323. );
  2324. }
  2325. /**
  2326. * Gets the progress bar mode
  2327. * @return string The progress bar mode attribute
  2328. */
  2329. public function get_progress_bar_mode()
  2330. {
  2331. if ($this->debug > 0) {
  2332. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2333. }
  2334. if (!empty ($this->progress_bar_mode)) {
  2335. return $this->progress_bar_mode;
  2336. } else {
  2337. return '%';
  2338. }
  2339. }
  2340. /**
  2341. * Gets the learnpath proximity (remote or local)
  2342. * @return string Learnpath proximity
  2343. */
  2344. public function get_proximity()
  2345. {
  2346. if ($this->debug > 0) {
  2347. error_log('New LP - In learnpath::get_proximity()', 0);
  2348. }
  2349. if (!empty ($this->proximity)) {
  2350. return $this->proximity;
  2351. } else {
  2352. return '';
  2353. }
  2354. }
  2355. /**
  2356. * Gets the learnpath theme (remote or local)
  2357. * @return string Learnpath theme
  2358. */
  2359. public function get_theme()
  2360. {
  2361. if ($this->debug > 0) {
  2362. error_log('New LP - In learnpath::get_theme()', 0);
  2363. }
  2364. if (!empty ($this->theme)) {
  2365. return $this->theme;
  2366. } else {
  2367. return '';
  2368. }
  2369. }
  2370. /**
  2371. * Gets the learnpath session id
  2372. * @return string Learnpath theme
  2373. */
  2374. public function get_lp_session_id()
  2375. {
  2376. if ($this->debug > 0) {
  2377. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2378. }
  2379. if (!empty ($this->lp_session_id)) {
  2380. return $this->lp_session_id;
  2381. } else {
  2382. return 0;
  2383. }
  2384. }
  2385. /**
  2386. * Gets the learnpath image
  2387. * @return string Web URL of the LP image
  2388. */
  2389. public function get_preview_image()
  2390. {
  2391. if ($this->debug > 0) {
  2392. error_log('New LP - In learnpath::get_preview_image()', 0);
  2393. }
  2394. if (!empty($this->preview_image)) {
  2395. return $this->preview_image;
  2396. } else {
  2397. return '';
  2398. }
  2399. }
  2400. /**
  2401. * @param string $size
  2402. * @param string $path_type
  2403. * @return bool|string
  2404. */
  2405. public function get_preview_image_path($size = null, $path_type = 'web')
  2406. {
  2407. $preview_image = $this->get_preview_image();
  2408. if (isset($preview_image) && !empty($preview_image)) {
  2409. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2410. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2411. if (isset($size)) {
  2412. $info = pathinfo($preview_image);
  2413. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2414. if (file_exists($image_sys_path.$image_custom_size)) {
  2415. if ($path_type == 'web') {
  2416. return $image_path.$image_custom_size;
  2417. } else {
  2418. return $image_sys_path.$image_custom_size;
  2419. }
  2420. }
  2421. } else {
  2422. if ($path_type == 'web') {
  2423. return $image_path.$preview_image;
  2424. } else {
  2425. return $image_sys_path.$preview_image;
  2426. }
  2427. }
  2428. }
  2429. return false;
  2430. }
  2431. /**
  2432. * Gets the learnpath author
  2433. * @return string LP's author
  2434. */
  2435. public function get_author()
  2436. {
  2437. if ($this->debug > 0) {
  2438. error_log('New LP - In learnpath::get_author()', 0);
  2439. }
  2440. if (!empty ($this->author)) {
  2441. return $this->author;
  2442. } else {
  2443. return '';
  2444. }
  2445. }
  2446. /**
  2447. * Gets the learnpath author
  2448. * @return string LP's author
  2449. */
  2450. public function get_hide_toc_frame()
  2451. {
  2452. if ($this->debug > 0) {
  2453. error_log('New LP - In learnpath::get_author()', 0);
  2454. }
  2455. if (!empty ($this->hide_toc_frame)) {
  2456. return $this->hide_toc_frame;
  2457. } else {
  2458. return '';
  2459. }
  2460. }
  2461. /**
  2462. * Generate a new prerequisites string for a given item. If this item was a sco and
  2463. * its prerequisites were strings (instead of IDs), then transform those strings into
  2464. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2465. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2466. * same rule as the scorm_export() method
  2467. * @param integer Item ID
  2468. * @return string Prerequisites string ready for the export as SCORM
  2469. */
  2470. public function get_scorm_prereq_string($item_id)
  2471. {
  2472. if ($this->debug > 0) {
  2473. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2474. }
  2475. if (!is_object($this->items[$item_id])) {
  2476. return false;
  2477. }
  2478. /** @var learnpathItem $oItem */
  2479. $oItem = $this->items[$item_id];
  2480. $prereq = $oItem->get_prereq_string();
  2481. if (empty($prereq)) {
  2482. return '';
  2483. }
  2484. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2485. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2486. // then simply return it (with the ITEM_ prefix).
  2487. //return 'ITEM_' . $prereq;
  2488. return $this->items[$prereq]->ref;
  2489. } else {
  2490. if (isset($this->refs_list[$prereq])) {
  2491. // It's a simple string item from which the ID can be found in the refs list,
  2492. // so we can transform it directly to an ID for export.
  2493. return $this->items[$this->refs_list[$prereq]]->ref;
  2494. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2495. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2496. } else {
  2497. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2498. // and replace them, one by one, by the internal IDs (chamilo db)
  2499. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2500. // by a space as well.
  2501. $find = array (
  2502. '&',
  2503. '|',
  2504. '~',
  2505. '=',
  2506. '<>',
  2507. '{',
  2508. '}',
  2509. '*',
  2510. '(',
  2511. ')'
  2512. );
  2513. $replace = array (
  2514. ' ',
  2515. ' ',
  2516. ' ',
  2517. ' ',
  2518. ' ',
  2519. ' ',
  2520. ' ',
  2521. ' ',
  2522. ' ',
  2523. ' '
  2524. );
  2525. $prereq_mod = str_replace($find, $replace, $prereq);
  2526. $ids = explode(' ', $prereq_mod);
  2527. foreach ($ids as $id) {
  2528. $id = trim($id);
  2529. if (isset ($this->refs_list[$id])) {
  2530. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2531. }
  2532. }
  2533. return $prereq;
  2534. }
  2535. }
  2536. }
  2537. /**
  2538. * Returns the XML DOM document's node
  2539. * @param resource Reference to a list of objects to search for the given ITEM_*
  2540. * @param string The identifier to look for
  2541. * @return mixed The reference to the element found with that identifier. False if not found
  2542. */
  2543. public function get_scorm_xml_node(& $children, $id)
  2544. {
  2545. for ($i = 0; $i < $children->length; $i++) {
  2546. $item_temp = $children->item($i);
  2547. if ($item_temp->nodeName == 'item') {
  2548. if ($item_temp->getAttribute('identifier') == $id) {
  2549. return $item_temp;
  2550. }
  2551. }
  2552. $subchildren = $item_temp->childNodes;
  2553. if ($subchildren && $subchildren->length > 0) {
  2554. $val = $this->get_scorm_xml_node($subchildren, $id);
  2555. if (is_object($val)) {
  2556. return $val;
  2557. }
  2558. }
  2559. }
  2560. return false;
  2561. }
  2562. /**
  2563. * Returns a usable array of stats related to the current learnpath and user
  2564. * @return array Well-formatted array containing status for the current learnpath
  2565. */
  2566. public function get_stats()
  2567. {
  2568. if ($this->debug > 0) {
  2569. error_log('New LP - In learnpath::get_stats()', 0);
  2570. }
  2571. }
  2572. /**
  2573. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2574. * the given course (for all learnpaths and all users)
  2575. * @param string Course code
  2576. * @return array Well-formatted array containing status for the course's learnpaths
  2577. */
  2578. public function get_stats_course($course)
  2579. {
  2580. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2581. // TODO
  2582. }
  2583. /**
  2584. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2585. * the given course and learnpath (for all users)
  2586. * @param string Course code
  2587. * @param integer Learnpath ID
  2588. * @return array Well-formatted array containing status for the specified learnpath
  2589. */
  2590. public function get_stats_lp($course, $lp)
  2591. {
  2592. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2593. // TODO
  2594. }
  2595. /**
  2596. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2597. * the given course, learnpath and user.
  2598. * @param string Course code
  2599. * @param integer Learnpath ID
  2600. * @param integer User ID
  2601. * @return array Well-formatted array containing status for the specified learnpath and user
  2602. */
  2603. public function get_stats_lp_user($course, $lp, $user)
  2604. {
  2605. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2606. // TODO
  2607. }
  2608. /**
  2609. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2610. * the given course and learnpath (for all users)
  2611. * @param string Course code
  2612. * @param integer User ID
  2613. * @return array Well-formatted array containing status for the user's learnpaths
  2614. */
  2615. public function get_stats_user($course, $user) {
  2616. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2617. // TODO
  2618. }
  2619. /**
  2620. * Gets the status list for all LP's items
  2621. * @return array Array of [index] => [item ID => current status]
  2622. */
  2623. public function get_items_status_list()
  2624. {
  2625. if ($this->debug > 0) {
  2626. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2627. }
  2628. $list = array ();
  2629. foreach ($this->ordered_items as $item_id) {
  2630. $list[] = array (
  2631. $item_id => $this->items[$item_id]->get_status()
  2632. );
  2633. }
  2634. return $list;
  2635. }
  2636. /**
  2637. * Return the number of interactions for the given learnpath Item View ID.
  2638. * This method can be used as static.
  2639. * @param integer Item View ID
  2640. * @param integer course id
  2641. * @return integer Number of interactions
  2642. */
  2643. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2644. {
  2645. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2646. $lp_iv_id = intval($lp_iv_id);
  2647. $course_id = intval($course_id);
  2648. $sql = "SELECT count(*) FROM $table
  2649. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2650. $res = Database::query($sql);
  2651. $num = 0;
  2652. if (Database::num_rows($res)) {
  2653. $row = Database::fetch_array($res);
  2654. $num = $row[0];
  2655. }
  2656. return $num;
  2657. }
  2658. /**
  2659. * Return the interactions as an array for the given lp_iv_id.
  2660. * This method can be used as static.
  2661. * @param integer Learnpath Item View ID
  2662. * @return array
  2663. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2664. */
  2665. public static function get_iv_interactions_array($lp_iv_id)
  2666. {
  2667. $course_id = api_get_course_int_id();
  2668. $list = array();
  2669. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2670. if (empty($lp_iv_id)) {
  2671. return array();
  2672. }
  2673. $sql = "SELECT * FROM $table
  2674. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2675. ORDER BY order_id ASC";
  2676. $res = Database::query($sql);
  2677. $num = Database :: num_rows($res);
  2678. if ($num > 0) {
  2679. $list[] = array (
  2680. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2681. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2682. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2683. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2684. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2685. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2686. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2687. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2688. );
  2689. while ($row = Database :: fetch_array($res)) {
  2690. $list[] = array (
  2691. 'order_id' => ($row['order_id'] + 1),
  2692. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2693. 'type' => $row['interaction_type'],
  2694. 'time' => $row['completion_time'],
  2695. //'correct_responses' => $row['correct_responses'],
  2696. 'correct_responses' => '', // Hide correct responses from students.
  2697. 'student_response' => $row['student_response'],
  2698. 'result' => $row['result'],
  2699. 'latency' => $row['latency']
  2700. );
  2701. }
  2702. }
  2703. return $list;
  2704. }
  2705. /**
  2706. * Return the number of objectives for the given learnpath Item View ID.
  2707. * This method can be used as static.
  2708. * @param integer Item View ID
  2709. * @return integer Number of objectives
  2710. */
  2711. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2712. {
  2713. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2714. $course_id = intval($course_id);
  2715. $lp_iv_id = intval($lp_iv_id);
  2716. $sql = "SELECT count(*) FROM $table
  2717. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2718. //@todo seems that this always returns 0
  2719. $res = Database::query($sql);
  2720. $num = 0;
  2721. if (Database::num_rows($res)) {
  2722. $row = Database :: fetch_array($res);
  2723. $num = $row[0];
  2724. }
  2725. return $num;
  2726. }
  2727. /**
  2728. * Return the objectives as an array for the given lp_iv_id.
  2729. * This method can be used as static.
  2730. * @param integer Learnpath Item View ID
  2731. * @return array
  2732. * @todo Translate labels
  2733. */
  2734. public static function get_iv_objectives_array($lp_iv_id = 0)
  2735. {
  2736. $course_id = api_get_course_int_id();
  2737. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2738. $sql = "SELECT * FROM $table
  2739. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2740. ORDER BY order_id ASC";
  2741. $res = Database::query($sql);
  2742. $num = Database :: num_rows($res);
  2743. $list = array();
  2744. if ($num > 0) {
  2745. $list[] = array(
  2746. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2747. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2748. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2749. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2750. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2751. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2752. );
  2753. while ($row = Database :: fetch_array($res)) {
  2754. $list[] = array (
  2755. 'order_id' => ($row['order_id'] + 1),
  2756. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2757. 'score_raw' => $row['score_raw'],
  2758. 'score_max' => $row['score_max'],
  2759. 'score_min' => $row['score_min'],
  2760. 'status' => $row['status']
  2761. );
  2762. }
  2763. }
  2764. return $list;
  2765. }
  2766. /**
  2767. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2768. * used by get_html_toc() to be ready to display
  2769. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2770. */
  2771. public function get_toc()
  2772. {
  2773. if ($this->debug > 0) {
  2774. error_log('learnpath::get_toc()', 0);
  2775. }
  2776. $toc = array();
  2777. foreach ($this->ordered_items as $item_id) {
  2778. if ($this->debug > 2) {
  2779. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2780. }
  2781. // TODO: Change this link generation and use new function instead.
  2782. $toc[] = array (
  2783. 'id' => $item_id,
  2784. 'title' => $this->items[$item_id]->get_title(),
  2785. 'status' => $this->items[$item_id]->get_status(),
  2786. 'level' => $this->items[$item_id]->get_level(),
  2787. 'type' => $this->items[$item_id]->get_type(),
  2788. 'description' => $this->items[$item_id]->get_description(),
  2789. 'path' => $this->items[$item_id]->get_path(),
  2790. );
  2791. }
  2792. if ($this->debug > 2) {
  2793. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2794. }
  2795. return $toc;
  2796. }
  2797. /**
  2798. * Generate and return the table of contents for this learnpath. The JS
  2799. * table returned is used inside of scorm_api.php
  2800. * @return string A JS array vairiable construction
  2801. */
  2802. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2803. {
  2804. if ($this->debug > 0) {
  2805. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2806. }
  2807. $toc = $varname.' = new Array();';
  2808. foreach ($this->ordered_items as $item_id) {
  2809. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2810. }
  2811. if ($this->debug > 2) {
  2812. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2813. }
  2814. return $toc;
  2815. }
  2816. /**
  2817. * Gets the learning path type
  2818. * @param boolean Return the name? If false, return the ID. Default is false.
  2819. * @return mixed Type ID or name, depending on the parameter
  2820. */
  2821. public function get_type($get_name = false)
  2822. {
  2823. $res = false;
  2824. if ($this->debug > 0) {
  2825. error_log('New LP - In learnpath::get_type()', 0);
  2826. }
  2827. if (!empty ($this->type)) {
  2828. if ($get_name) {
  2829. // Get it from the lp_type table in main db.
  2830. } else {
  2831. $res = $this->type;
  2832. }
  2833. }
  2834. if ($this->debug > 2) {
  2835. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2836. }
  2837. return $res;
  2838. }
  2839. /**
  2840. * Gets the learning path type as static method
  2841. * @param boolean Return the name? If false, return the ID. Default is false.
  2842. * @return mixed Type ID or name, depending on the parameter
  2843. */
  2844. public static function get_type_static($lp_id = 0)
  2845. {
  2846. $course_id = api_get_course_int_id();
  2847. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2848. $lp_id = intval($lp_id);
  2849. $sql = "SELECT lp_type FROM $tbl_lp
  2850. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2851. $res = Database::query($sql);
  2852. if ($res === false) {
  2853. return null;
  2854. }
  2855. if (Database :: num_rows($res) <= 0) {
  2856. return null;
  2857. }
  2858. $row = Database :: fetch_array($res);
  2859. return $row['lp_type'];
  2860. }
  2861. /**
  2862. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2863. * This method can be used as abstract and is recursive
  2864. * @param integer Learnpath ID
  2865. * @param integer Parent ID of the items to look for
  2866. * @return mixed Ordered list of item IDs or false on error
  2867. */
  2868. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2869. {
  2870. if (empty($course_id)) {
  2871. $course_id = api_get_course_int_id();
  2872. } else {
  2873. $course_id = intval($course_id);
  2874. }
  2875. $list = array();
  2876. if (empty($lp)) {
  2877. return false;
  2878. }
  2879. $lp = intval($lp);
  2880. $parent = intval($parent);
  2881. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2882. $sql = "SELECT id FROM $tbl_lp_item
  2883. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2884. ORDER BY display_order";
  2885. $res = Database::query($sql);
  2886. while ($row = Database :: fetch_array($res)) {
  2887. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2888. $list[] = $row['id'];
  2889. foreach ($sublist as $item) {
  2890. $list[] = $item;
  2891. }
  2892. }
  2893. return $list;
  2894. }
  2895. /**
  2896. * @return array
  2897. */
  2898. public static function getChapterTypes()
  2899. {
  2900. return array(
  2901. 'dokeos_chapter',
  2902. 'dokeos_module',
  2903. 'chapter',
  2904. 'dir'
  2905. );
  2906. }
  2907. /**
  2908. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2909. * @return string HTML TOC ready to display
  2910. */
  2911. public function get_html_toc($toc_list = null)
  2912. {
  2913. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2914. if ($this->debug > 0) {
  2915. error_log('In learnpath::get_html_toc()', 0);
  2916. }
  2917. if (empty($toc_list)) {
  2918. $toc_list = $this->get_toc();
  2919. }
  2920. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2921. $html = '<div class="scorm-body">';
  2922. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2923. require_once 'resourcelinker.inc.php';
  2924. // Temporary variables.
  2925. $mycurrentitemid = $this->get_current_item_id();
  2926. $color_counter = 0;
  2927. $i = 0;
  2928. foreach ($toc_list as $item) {
  2929. // Style Status
  2930. $class_name = [
  2931. 'not attempted' => 'scorm_not_attempted',
  2932. 'incomplete' => 'scorm_not_attempted',
  2933. 'failed' => 'scorm_failed',
  2934. 'completed' => 'scorm_completed',
  2935. 'passed' => 'scorm_completed',
  2936. 'succeeded' => 'scorm_completed',
  2937. 'browsed' => 'scorm_completed',
  2938. ];
  2939. $scorm_color_background = 'row_odd';
  2940. $style_item = '';
  2941. if ($color_counter % 2 == 0) {
  2942. $scorm_color_background = 'row_even';
  2943. }
  2944. $dirTypes = self::getChapterTypes();
  2945. if (in_array($item['type'], $dirTypes)) {
  2946. $scorm_color_background ='scorm_item_section ';
  2947. $style_item = '';
  2948. }
  2949. if ($item['id'] == $this->current) {
  2950. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2951. } elseif (!in_array($item['type'], $dirTypes)) {
  2952. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2953. }
  2954. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2955. // Learning path title
  2956. $title = $item['title'];
  2957. if (empty ($title)) {
  2958. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2959. }
  2960. $title = Security::remove_XSS($title);
  2961. // Learning path personalization
  2962. // build the LP tree
  2963. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2964. $description = $item['description'];
  2965. if (empty($description)) {
  2966. $description = $title;
  2967. }
  2968. if (in_array($item['type'], $dirTypes)) {
  2969. // Chapters
  2970. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2971. } else {
  2972. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2973. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2974. }
  2975. if (in_array($item['type'], $dirTypes)) {
  2976. // Chapter
  2977. // if you want to put an image before, you should use css
  2978. $html .= stripslashes($title);
  2979. } else {
  2980. $this->get_link('http', $item['id'], $toc_list);
  2981. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2982. }
  2983. $html .= "</div>";
  2984. if ($scorm_color_background != '') {
  2985. $html .= '</div>';
  2986. }
  2987. $color_counter++;
  2988. }
  2989. $html .= "</div>";
  2990. $html .= "</div>";
  2991. return $html;
  2992. }
  2993. /**
  2994. * Returns an HTML-formatted string ready to display with teacher buttons
  2995. * in LP view menu
  2996. * @return string HTML TOC ready to display
  2997. */
  2998. public function get_teacher_toc_buttons()
  2999. {
  3000. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  3001. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  3002. $html = '';
  3003. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  3004. $gradebook = '';
  3005. if (!empty($_GET['gradebook'])) {
  3006. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  3007. }
  3008. if ($this->get_lp_session_id() == api_get_session_id()) {
  3009. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3010. $html .= '<div class="btn-group">';
  3011. $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'>" .
  3012. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  3013. $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'>" .
  3014. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  3015. $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">' .
  3016. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  3017. $html .= '</div>';
  3018. $html .= '</div>';
  3019. }
  3020. }
  3021. return $html;
  3022. }
  3023. /**
  3024. * Gets the learnpath maker name - generally the editor's name
  3025. * @return string Learnpath maker name
  3026. */
  3027. public function get_maker()
  3028. {
  3029. if ($this->debug > 0) {
  3030. error_log('New LP - In learnpath::get_maker()', 0);
  3031. }
  3032. if (!empty ($this->maker)) {
  3033. return $this->maker;
  3034. } else {
  3035. return '';
  3036. }
  3037. }
  3038. /**
  3039. * Gets the learnpath name/title
  3040. * @return string Learnpath name/title
  3041. */
  3042. public function get_name()
  3043. {
  3044. if ($this->debug > 0) {
  3045. error_log('New LP - In learnpath::get_name()', 0);
  3046. }
  3047. if (!empty ($this->name)) {
  3048. return $this->name;
  3049. } else {
  3050. return 'N/A';
  3051. }
  3052. }
  3053. /**
  3054. * Gets a link to the resource from the present location, depending on item ID.
  3055. * @param string $type Type of link expected
  3056. * @param integer $item_id Learnpath item ID
  3057. * @return string $provided_toc Link to the lp_item resource
  3058. */
  3059. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3060. {
  3061. $course_id = $this->get_course_int_id();
  3062. if ($this->debug > 0) {
  3063. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3064. }
  3065. if (empty($item_id)) {
  3066. if ($this->debug > 2) {
  3067. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3068. }
  3069. $item_id = $this->get_current_item_id();
  3070. }
  3071. if (empty($item_id)) {
  3072. if ($this->debug > 2) {
  3073. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3074. }
  3075. //still empty, this means there was no item_id given and we are not in an object context or
  3076. //the object property is empty, return empty link
  3077. $item_id = $this->first();
  3078. return '';
  3079. }
  3080. $file = '';
  3081. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3082. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3083. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3084. $item_id = intval($item_id);
  3085. $sql = "SELECT
  3086. l.lp_type as ltype,
  3087. l.path as lpath,
  3088. li.item_type as litype,
  3089. li.path as lipath,
  3090. li.parameters as liparams
  3091. FROM $lp_table l
  3092. INNER JOIN $lp_item_table li
  3093. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3094. WHERE li.id = $item_id ";
  3095. if ($this->debug > 2) {
  3096. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3097. }
  3098. $res = Database::query($sql);
  3099. if (Database :: num_rows($res) > 0) {
  3100. $row = Database :: fetch_array($res);
  3101. $lp_type = $row['ltype'];
  3102. $lp_path = $row['lpath'];
  3103. $lp_item_type = $row['litype'];
  3104. $lp_item_path = $row['lipath'];
  3105. $lp_item_params = $row['liparams'];
  3106. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3107. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3108. }
  3109. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3110. if ($type == 'http') {
  3111. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3112. } else {
  3113. $course_path = $sys_course_path; //system path
  3114. }
  3115. // 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.
  3116. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3117. $lp_type = 1;
  3118. }
  3119. if ($this->debug > 2) {
  3120. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3121. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3122. }
  3123. // Now go through the specific cases to get the end of the path
  3124. // @todo Use constants instead of int values.
  3125. switch ($lp_type) {
  3126. case 1 :
  3127. if ($lp_item_type == 'dokeos_chapter') {
  3128. $file = 'lp_content.php?type=dir';
  3129. } else {
  3130. require_once 'resourcelinker.inc.php';
  3131. $file = rl_get_resource_link_for_learnpath(
  3132. $course_id,
  3133. $this->get_id(),
  3134. $item_id,
  3135. $this->get_view_id()
  3136. );
  3137. if ($this->debug > 0) {
  3138. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3139. }
  3140. if ($lp_item_type == 'link') {
  3141. if (Link::is_youtube_link($file)) {
  3142. $src = Link::get_youtube_video_id($file);
  3143. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3144. } elseif (Link::isVimeoLink($file)) {
  3145. $src = Link::getVimeoLinkId($file);
  3146. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3147. } else {
  3148. // If the current site is HTTPS and the link is
  3149. // HTTP, browsers will refuse opening the link
  3150. $urlId = api_get_current_access_url_id();
  3151. $url = api_get_access_url($urlId, false);
  3152. $protocol = substr($url['url'], 0, 5);
  3153. if ($protocol === 'https') {
  3154. $linkProtocol = substr($file, 0, 5);
  3155. if ($linkProtocol === 'http:') {
  3156. //this is the special intervention case
  3157. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&source=' . urlencode($file);
  3158. }
  3159. }
  3160. }
  3161. } else {
  3162. // Check how much attempts of a exercise exits in lp
  3163. $lp_item_id = $this->get_current_item_id();
  3164. $lp_view_id = $this->get_view_id();
  3165. $prevent_reinit = null;
  3166. if (isset($this->items[$this->current])) {
  3167. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3168. }
  3169. if (empty($provided_toc)) {
  3170. if ($this->debug > 0) {
  3171. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3172. }
  3173. $list = $this->get_toc();
  3174. } else {
  3175. if ($this->debug > 0) {
  3176. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3177. }
  3178. $list = $provided_toc;
  3179. }
  3180. $type_quiz = false;
  3181. foreach ($list as $toc) {
  3182. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3183. $type_quiz = true;
  3184. }
  3185. }
  3186. if ($type_quiz) {
  3187. $lp_item_id = intval($lp_item_id);
  3188. $lp_view_id = intval($lp_view_id);
  3189. $sql = "SELECT count(*) FROM $lp_item_view_table
  3190. WHERE
  3191. c_id = $course_id AND
  3192. lp_item_id='" . $lp_item_id . "' AND
  3193. lp_view_id ='" . $lp_view_id . "' AND
  3194. status='completed'";
  3195. $result = Database::query($sql);
  3196. $row_count = Database :: fetch_row($result);
  3197. $count_item_view = (int) $row_count[0];
  3198. $not_multiple_attempt = 0;
  3199. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3200. $not_multiple_attempt = 1;
  3201. }
  3202. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3203. }
  3204. $tmp_array = explode('/', $file);
  3205. $document_name = $tmp_array[count($tmp_array) - 1];
  3206. if (strpos($document_name, '_DELETED_')) {
  3207. $file = 'blank.php?error=document_deleted';
  3208. }
  3209. }
  3210. }
  3211. break;
  3212. case 2 :
  3213. if ($this->debug > 2) {
  3214. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3215. }
  3216. if ($lp_item_type != 'dir') {
  3217. // Quite complex here:
  3218. // We want to make sure 'http://' (and similar) links can
  3219. // be loaded as is (withouth the Chamilo path in front) but
  3220. // some contents use this form: resource.htm?resource=http://blablabla
  3221. // which means we have to find a protocol at the path's start, otherwise
  3222. // it should not be considered as an external URL.
  3223. //if ($this->prerequisites_match($item_id)) {
  3224. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3225. if ($this->debug > 2) {
  3226. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3227. }
  3228. // Distant url, return as is.
  3229. $file = $lp_item_path;
  3230. } else {
  3231. if ($this->debug > 2) {
  3232. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3233. }
  3234. // Prevent getting untranslatable urls.
  3235. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3236. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3237. // Prepare the path.
  3238. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3239. // TODO: Fix this for urls with protocol header.
  3240. $file = str_replace('//', '/', $file);
  3241. $file = str_replace(':/', '://', $file);
  3242. if (substr($lp_path, -1) == '/') {
  3243. $lp_path = substr($lp_path, 0, -1);
  3244. }
  3245. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3246. // if file not found.
  3247. $decoded = html_entity_decode($lp_item_path);
  3248. list ($decoded) = explode('?', $decoded);
  3249. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3250. require_once 'resourcelinker.inc.php';
  3251. $file = rl_get_resource_link_for_learnpath(
  3252. $course_id,
  3253. $this->get_id(),
  3254. $item_id,
  3255. $this->get_view_id()
  3256. );
  3257. if (empty($file)) {
  3258. $file = 'blank.php?error=document_not_found';
  3259. } else {
  3260. $tmp_array = explode('/', $file);
  3261. $document_name = $tmp_array[count($tmp_array) - 1];
  3262. if (strpos($document_name, '_DELETED_')) {
  3263. $file = 'blank.php?error=document_deleted';
  3264. } else {
  3265. $file = 'blank.php?error=document_not_found';
  3266. }
  3267. }
  3268. } else {
  3269. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3270. }
  3271. }
  3272. }
  3273. // We want to use parameters if they were defined in the imsmanifest
  3274. if (strpos($file, 'blank.php') === false) {
  3275. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3276. }
  3277. } else {
  3278. $file = 'lp_content.php?type=dir';
  3279. }
  3280. break;
  3281. case 3 :
  3282. if ($this->debug > 2) {
  3283. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3284. }
  3285. // Formatting AICC HACP append URL.
  3286. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3287. if (!empty($lp_item_params)) {
  3288. $aicc_append .= $lp_item_params . '&';
  3289. }
  3290. if ($lp_item_type != 'dir') {
  3291. // Quite complex here:
  3292. // We want to make sure 'http://' (and similar) links can
  3293. // be loaded as is (withouth the Chamilo path in front) but
  3294. // some contents use this form: resource.htm?resource=http://blablabla
  3295. // which means we have to find a protocol at the path's start, otherwise
  3296. // it should not be considered as an external URL.
  3297. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3298. if ($this->debug > 2) {
  3299. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3300. }
  3301. // Distant url, return as is.
  3302. $file = $lp_item_path;
  3303. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3304. /*
  3305. if (stristr($file,'<servername>') !== false) {
  3306. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3307. }
  3308. */
  3309. if (stripos($file, '<servername>') !== false) {
  3310. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3311. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3312. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3313. }
  3314. //
  3315. $file .= $aicc_append;
  3316. } else {
  3317. if ($this->debug > 2) {
  3318. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3319. }
  3320. // Prevent getting untranslatable urls.
  3321. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3322. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3323. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3324. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3325. // TODO: Fix this for urls with protocol header.
  3326. $file = str_replace('//', '/', $file);
  3327. $file = str_replace(':/', '://', $file);
  3328. $file .= $aicc_append;
  3329. }
  3330. } else {
  3331. $file = 'lp_content.php?type=dir';
  3332. }
  3333. break;
  3334. case 4 :
  3335. break;
  3336. default :
  3337. break;
  3338. }
  3339. // Replace &amp; by & because &amp; will break URL with params
  3340. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3341. }
  3342. if ($this->debug > 2) {
  3343. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3344. }
  3345. return $file;
  3346. }
  3347. /**
  3348. * Gets the latest usable view or generate a new one
  3349. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3350. * @return integer DB lp_view id
  3351. */
  3352. public function get_view($attempt_num = 0)
  3353. {
  3354. if ($this->debug > 0) {
  3355. error_log('New LP - In learnpath::get_view()', 0);
  3356. }
  3357. $search = '';
  3358. // Use $attempt_num to enable multi-views management (disabled so far).
  3359. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3360. $search = 'AND view_count = ' . $attempt_num;
  3361. }
  3362. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3363. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3364. $course_id = api_get_course_int_id();
  3365. $sessionId = api_get_session_id();
  3366. $sql = "SELECT id, view_count FROM $lp_view_table
  3367. WHERE
  3368. c_id = " . $course_id . " AND
  3369. lp_id = " . $this->get_id() . " AND
  3370. user_id = " . $this->get_user_id() . " AND
  3371. session_id = $sessionId
  3372. $search
  3373. ORDER BY view_count DESC";
  3374. $res = Database::query($sql);
  3375. if (Database :: num_rows($res) > 0) {
  3376. $row = Database :: fetch_array($res);
  3377. $this->lp_view_id = $row['id'];
  3378. } else if (!api_is_invitee()) {
  3379. // There is no database record, create one.
  3380. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3381. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3382. Database::query($sql);
  3383. $id = Database :: insert_id();
  3384. $this->lp_view_id = $id;
  3385. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3386. Database::query($sql);
  3387. }
  3388. return $this->lp_view_id;
  3389. }
  3390. /**
  3391. * Gets the current view id
  3392. * @return integer View ID (from lp_view)
  3393. */
  3394. public function get_view_id()
  3395. {
  3396. if ($this->debug > 0) {
  3397. error_log('New LP - In learnpath::get_view_id()', 0);
  3398. }
  3399. if (!empty ($this->lp_view_id)) {
  3400. return $this->lp_view_id;
  3401. } else {
  3402. return 0;
  3403. }
  3404. }
  3405. /**
  3406. * Gets the update queue
  3407. * @return array Array containing IDs of items to be updated by JavaScript
  3408. */
  3409. public function get_update_queue()
  3410. {
  3411. if ($this->debug > 0) {
  3412. error_log('New LP - In learnpath::get_update_queue()', 0);
  3413. }
  3414. return $this->update_queue;
  3415. }
  3416. /**
  3417. * Gets the user ID
  3418. * @return integer User ID
  3419. */
  3420. public function get_user_id()
  3421. {
  3422. if ($this->debug > 0) {
  3423. error_log('New LP - In learnpath::get_user_id()', 0);
  3424. }
  3425. if (!empty ($this->user_id)) {
  3426. return $this->user_id;
  3427. } else {
  3428. return false;
  3429. }
  3430. }
  3431. /**
  3432. * Checks if any of the items has an audio element attached
  3433. * @return bool True or false
  3434. */
  3435. public function has_audio()
  3436. {
  3437. if ($this->debug > 1) {
  3438. error_log('New LP - In learnpath::has_audio()', 0);
  3439. }
  3440. $has = false;
  3441. foreach ($this->items as $i => $item) {
  3442. if (!empty ($this->items[$i]->audio)) {
  3443. $has = true;
  3444. break;
  3445. }
  3446. }
  3447. return $has;
  3448. }
  3449. /**
  3450. * Logs a message into a file
  3451. * @param string Message to log
  3452. * @return boolean True on success, false on error or if msg empty
  3453. */
  3454. public function log($msg)
  3455. {
  3456. if ($this->debug > 0) {
  3457. error_log('New LP - In learnpath::log()', 0);
  3458. }
  3459. // TODO
  3460. $this->error .= $msg;
  3461. return true;
  3462. }
  3463. /**
  3464. * Moves an item up and down at its level
  3465. * @param integer Item to move up and down
  3466. * @param string Direction 'up' or 'down'
  3467. * @return integer New display order, or false on error
  3468. */
  3469. public function move_item($id, $direction)
  3470. {
  3471. $course_id = api_get_course_int_id();
  3472. if ($this->debug > 0) {
  3473. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3474. }
  3475. if (empty($id) || empty($direction)) {
  3476. return false;
  3477. }
  3478. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3479. $sql_sel = "SELECT *
  3480. FROM " . $tbl_lp_item . "
  3481. WHERE c_id = ".$course_id." AND id = " . $id;
  3482. $res_sel = Database::query($sql_sel);
  3483. // Check if elem exists.
  3484. if (Database :: num_rows($res_sel) < 1) {
  3485. return false;
  3486. }
  3487. // Gather data.
  3488. $row = Database :: fetch_array($res_sel);
  3489. $previous = $row['previous_item_id'];
  3490. $next = $row['next_item_id'];
  3491. $display = $row['display_order'];
  3492. $parent = $row['parent_item_id'];
  3493. $lp = $row['lp_id'];
  3494. // Update the item (switch with previous/next one).
  3495. switch ($direction) {
  3496. case 'up':
  3497. if ($this->debug > 2) {
  3498. error_log('Movement up detected', 0);
  3499. }
  3500. if ($display <= 1) { /*do nothing*/
  3501. } else {
  3502. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3503. WHERE c_id = ".$course_id." AND id = $previous";
  3504. if ($this->debug > 2) {
  3505. error_log('Selecting previous: ' . $sql_sel2, 0);
  3506. }
  3507. $res_sel2 = Database::query($sql_sel2);
  3508. if (Database :: num_rows($res_sel2) < 1) {
  3509. $previous_previous = 0;
  3510. }
  3511. // Gather data.
  3512. $row2 = Database :: fetch_array($res_sel2);
  3513. $previous_previous = $row2['previous_item_id'];
  3514. // Update previous_previous item (switch "next" with current).
  3515. if ($previous_previous != 0) {
  3516. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3517. next_item_id = $id
  3518. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3519. if ($this->debug > 2) {
  3520. error_log($sql_upd2, 0);
  3521. }
  3522. Database::query($sql_upd2);
  3523. }
  3524. // Update previous item (switch with current).
  3525. if ($previous != 0) {
  3526. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3527. next_item_id = $next,
  3528. previous_item_id = $id,
  3529. display_order = display_order +1
  3530. WHERE c_id = ".$course_id." AND id = $previous";
  3531. if ($this->debug > 2) {
  3532. error_log($sql_upd2, 0);
  3533. }
  3534. Database::query($sql_upd2);
  3535. }
  3536. // Update current item (switch with previous).
  3537. if ($id != 0) {
  3538. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3539. next_item_id = $previous,
  3540. previous_item_id = $previous_previous,
  3541. display_order = display_order-1
  3542. WHERE c_id = ".$course_id." AND id = $id";
  3543. if ($this->debug > 2) {
  3544. error_log($sql_upd2, 0);
  3545. }
  3546. Database::query($sql_upd2);
  3547. }
  3548. // Update next item (new previous item).
  3549. if ($next != 0) {
  3550. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3551. WHERE c_id = ".$course_id." AND id = $next";
  3552. if ($this->debug > 2) {
  3553. error_log($sql_upd2, 0);
  3554. }
  3555. Database::query($sql_upd2);
  3556. }
  3557. $display = $display -1;
  3558. }
  3559. break;
  3560. case 'down':
  3561. if ($this->debug > 2) {
  3562. error_log('Movement down detected', 0);
  3563. }
  3564. if ($next == 0) { /* Do nothing. */
  3565. } else {
  3566. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3567. if ($this->debug > 2) {
  3568. error_log('Selecting next: ' . $sql_sel2, 0);
  3569. }
  3570. $res_sel2 = Database::query($sql_sel2);
  3571. if (Database :: num_rows($res_sel2) < 1) {
  3572. $next_next = 0;
  3573. }
  3574. // Gather data.
  3575. $row2 = Database :: fetch_array($res_sel2);
  3576. $next_next = $row2['next_item_id'];
  3577. // Update previous item (switch with current).
  3578. if ($previous != 0) {
  3579. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3580. WHERE c_id = ".$course_id." AND id = $previous";
  3581. Database::query($sql_upd2);
  3582. }
  3583. // Update current item (switch with previous).
  3584. if ($id != 0) {
  3585. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3586. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3587. WHERE c_id = ".$course_id." AND id = $id";
  3588. Database::query($sql_upd2);
  3589. }
  3590. // Update next item (new previous item).
  3591. if ($next != 0) {
  3592. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3593. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3594. WHERE c_id = ".$course_id." AND id = $next";
  3595. Database::query($sql_upd2);
  3596. }
  3597. // Update next_next item (switch "previous" with current).
  3598. if ($next_next != 0) {
  3599. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3600. previous_item_id = $id
  3601. WHERE c_id = ".$course_id." AND id = $next_next";
  3602. Database::query($sql_upd2);
  3603. }
  3604. $display = $display +1;
  3605. }
  3606. break;
  3607. default :
  3608. return false;
  3609. }
  3610. return $display;
  3611. }
  3612. /**
  3613. * Move a learnpath up (display_order)
  3614. * @param integer $lp_id Learnpath ID
  3615. */
  3616. public static function move_up($lp_id)
  3617. {
  3618. $course_id = api_get_course_int_id();
  3619. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3620. $sql = "SELECT * FROM $lp_table
  3621. WHERE c_id = ".$course_id."
  3622. ORDER BY display_order";
  3623. $res = Database::query($sql);
  3624. if ($res === false)
  3625. return false;
  3626. $lps = array ();
  3627. $lp_order = array ();
  3628. $num = Database :: num_rows($res);
  3629. // First check the order is correct, globally (might be wrong because
  3630. // of versions < 1.8.4)
  3631. if ($num > 0) {
  3632. $i = 1;
  3633. while ($row = Database :: fetch_array($res)) {
  3634. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3635. $need_fix = true;
  3636. $sql_u = "UPDATE $lp_table SET display_order = $i
  3637. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3638. Database::query($sql_u);
  3639. }
  3640. $row['display_order'] = $i;
  3641. $lps[$row['id']] = $row;
  3642. $lp_order[$i] = $row['id'];
  3643. $i++;
  3644. }
  3645. }
  3646. if ($num > 1) { // If there's only one element, no need to sort.
  3647. $order = $lps[$lp_id]['display_order'];
  3648. if ($order > 1) { // If it's the first element, no need to move up.
  3649. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3650. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3651. Database::query($sql_u1);
  3652. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3653. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3654. Database::query($sql_u2);
  3655. }
  3656. }
  3657. }
  3658. /**
  3659. * Move a learnpath down (display_order)
  3660. * @param integer $lp_id Learnpath ID
  3661. */
  3662. public static function move_down($lp_id)
  3663. {
  3664. $course_id = api_get_course_int_id();
  3665. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3666. $sql = "SELECT * FROM $lp_table
  3667. WHERE c_id = ".$course_id."
  3668. ORDER BY display_order";
  3669. $res = Database::query($sql);
  3670. if ($res === false) {
  3671. return false;
  3672. }
  3673. $lps = array ();
  3674. $lp_order = array ();
  3675. $num = Database :: num_rows($res);
  3676. $max = 0;
  3677. // First check the order is correct, globally (might be wrong because
  3678. // of versions < 1.8.4).
  3679. if ($num > 0) {
  3680. $i = 1;
  3681. while ($row = Database :: fetch_array($res)) {
  3682. $max = $i;
  3683. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3684. $need_fix = true;
  3685. $sql_u = "UPDATE $lp_table SET display_order = $i
  3686. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3687. Database::query($sql_u);
  3688. }
  3689. $row['display_order'] = $i;
  3690. $lps[$row['id']] = $row;
  3691. $lp_order[$i] = $row['id'];
  3692. $i++;
  3693. }
  3694. }
  3695. if ($num > 1) { // If there's only one element, no need to sort.
  3696. $order = $lps[$lp_id]['display_order'];
  3697. if ($order < $max) { // If it's the first element, no need to move up.
  3698. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3699. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3700. Database::query($sql_u1);
  3701. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3702. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3703. Database::query($sql_u2);
  3704. }
  3705. }
  3706. }
  3707. /**
  3708. * Updates learnpath attributes to point to the next element
  3709. * The last part is similar to set_current_item but processing the other way around
  3710. */
  3711. public function next()
  3712. {
  3713. if ($this->debug > 0) {
  3714. error_log('New LP - In learnpath::next()', 0);
  3715. }
  3716. $this->last = $this->get_current_item_id();
  3717. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3718. $this->autocomplete_parents($this->last);
  3719. $new_index = $this->get_next_index();
  3720. if ($this->debug > 2) {
  3721. error_log('New LP - New index: ' . $new_index, 0);
  3722. }
  3723. $this->index = $new_index;
  3724. if ($this->debug > 2) {
  3725. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3726. }
  3727. $this->current = $this->ordered_items[$new_index];
  3728. if ($this->debug > 2) {
  3729. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3730. }
  3731. }
  3732. /**
  3733. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3734. * class, this might be redefined to allow several behaviours depending on the document type.
  3735. * @param integer Resource ID
  3736. * @return boolean True on success, false otherwise
  3737. */
  3738. public function open($id)
  3739. {
  3740. if ($this->debug > 0) {
  3741. error_log('New LP - In learnpath::open()', 0);
  3742. }
  3743. // TODO:
  3744. // set the current resource attribute to this resource
  3745. // switch on element type (redefine in child class?)
  3746. // set status for this item to "opened"
  3747. // start timer
  3748. // initialise score
  3749. $this->index = 0; //or = the last item seen (see $this->last)
  3750. }
  3751. /**
  3752. * Check that all prerequisites are fulfilled. Returns true and an
  3753. * empty string on success, returns false
  3754. * and the prerequisite string on error.
  3755. * This function is based on the rules for aicc_script language as
  3756. * described in the SCORM 1.2 CAM documentation page 108.
  3757. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3758. * @return boolean True if prerequisites are matched, false otherwise -
  3759. * Empty string if true returned, prerequisites string otherwise.
  3760. */
  3761. public function prerequisites_match($itemId = null)
  3762. {
  3763. $debug = $this->debug;
  3764. if ($debug > 0) {
  3765. error_log('In learnpath::prerequisites_match()', 0);
  3766. }
  3767. if (empty($itemId)) {
  3768. $itemId = $this->current;
  3769. }
  3770. $currentItem = $this->getItem($itemId);
  3771. if ($currentItem) {
  3772. if ($this->type == 2) {
  3773. // Getting prereq from scorm
  3774. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3775. } else {
  3776. $prereq_string = $currentItem->get_prereq_string();
  3777. }
  3778. if (empty($prereq_string)) {
  3779. if ($debug > 0) {
  3780. error_log('Found prereq_string is empty return true');
  3781. }
  3782. return true;
  3783. }
  3784. // Clean spaces.
  3785. $prereq_string = str_replace(' ', '', $prereq_string);
  3786. if ($debug > 0) {
  3787. error_log('Found prereq_string: ' . $prereq_string, 0);
  3788. }
  3789. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3790. $result = $currentItem->parse_prereq(
  3791. $prereq_string,
  3792. $this->items,
  3793. $this->refs_list,
  3794. $this->get_user_id()
  3795. );
  3796. if ($result === false) {
  3797. $this->set_error_msg($currentItem->prereq_alert);
  3798. }
  3799. } else {
  3800. $result = true;
  3801. if ($debug > 1) {
  3802. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3803. }
  3804. }
  3805. if ($debug > 1) {
  3806. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3807. }
  3808. return $result;
  3809. }
  3810. /**
  3811. * Updates learnpath attributes to point to the previous element
  3812. * The last part is similar to set_current_item but processing the other way around
  3813. */
  3814. public function previous()
  3815. {
  3816. if ($this->debug > 0) {
  3817. error_log('New LP - In learnpath::previous()', 0);
  3818. }
  3819. $this->last = $this->get_current_item_id();
  3820. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3821. $this->autocomplete_parents($this->last);
  3822. $new_index = $this->get_previous_index();
  3823. $this->index = $new_index;
  3824. $this->current = $this->ordered_items[$new_index];
  3825. }
  3826. /**
  3827. * Publishes a learnpath. This basically means show or hide the learnpath
  3828. * to normal users.
  3829. * Can be used as abstract
  3830. * @param integer Learnpath ID
  3831. * @param string New visibility
  3832. */
  3833. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3834. {
  3835. $action = 'visible';
  3836. if ($set_visibility != 1) {
  3837. $action = 'invisible';
  3838. self::toggle_publish($lp_id, 'i');
  3839. }
  3840. return api_item_property_update(
  3841. api_get_course_info(),
  3842. TOOL_LEARNPATH,
  3843. $lp_id,
  3844. $action,
  3845. api_get_user_id()
  3846. );
  3847. }
  3848. /**
  3849. * Publishes a learnpath. This basically means show or hide the learnpath
  3850. * on the course homepage
  3851. * Can be used as abstract
  3852. * @param integer $lp_id Learnpath id
  3853. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3854. * @return bool
  3855. */
  3856. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3857. {
  3858. $course_id = api_get_course_int_id();
  3859. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3860. $lp_id = intval($lp_id);
  3861. $sql = "SELECT * FROM $tbl_lp
  3862. WHERE c_id = ".$course_id." AND id = $lp_id";
  3863. $result = Database::query($sql);
  3864. if (Database::num_rows($result)) {
  3865. $row = Database :: fetch_array($result);
  3866. $name = domesticate($row['name']);
  3867. if ($set_visibility == 'i') {
  3868. $s = $name . " " . get_lang('LearnpathNotPublished');
  3869. $dialogBox = $s;
  3870. $v = 0;
  3871. }
  3872. if ($set_visibility == 'v') {
  3873. $s = $name . " " . get_lang('LearnpathPublished');
  3874. $dialogBox = $s;
  3875. $v = 1;
  3876. }
  3877. } else {
  3878. return false;
  3879. }
  3880. $session_id = api_get_session_id();
  3881. $session_condition = api_get_session_condition($session_id);
  3882. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3883. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3884. $sql = "SELECT * FROM $tbl_tool
  3885. WHERE
  3886. c_id = ".$course_id." AND
  3887. link='$link' and
  3888. image='scormbuilder.gif' and
  3889. link LIKE '$link%'
  3890. $session_condition
  3891. ";
  3892. $result = Database::query($sql);
  3893. $num = Database :: num_rows($result);
  3894. if ($set_visibility == 'i' && $num > 0) {
  3895. $sql = "DELETE FROM $tbl_tool
  3896. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3897. Database::query($sql);
  3898. } elseif ($set_visibility == 'v' && $num == 0) {
  3899. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3900. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3901. Database::query($sql);
  3902. $insertId = Database::insert_id();
  3903. if ($insertId) {
  3904. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3905. Database::query($sql);
  3906. }
  3907. } elseif ($set_visibility == 'v' && $num > 0) {
  3908. $sql = "UPDATE $tbl_tool SET
  3909. c_id = $course_id,
  3910. name = '$name',
  3911. link = '$link',
  3912. image = 'scormbuilder.gif',
  3913. visibility = '$v',
  3914. admin = '0',
  3915. address = 'pastillegris.gif',
  3916. added_tool = 0,
  3917. session_id = $session_id
  3918. WHERE
  3919. c_id = ".$course_id." AND
  3920. (link='$link' and image='scormbuilder.gif' $session_condition)
  3921. ";
  3922. Database::query($sql);
  3923. } else {
  3924. // Parameter and database incompatible, do nothing, exit.
  3925. return false;
  3926. }
  3927. }
  3928. /**
  3929. * Restart the whole learnpath. Return the URL of the first element.
  3930. * Make sure the results are saved with anoter method. This method should probably be
  3931. * redefined in children classes.
  3932. * To use a similar method statically, use the create_new_attempt() method
  3933. * @return string URL to load in the viewer
  3934. */
  3935. public function restart()
  3936. {
  3937. if ($this->debug > 0) {
  3938. error_log('New LP - In learnpath::restart()', 0);
  3939. }
  3940. // TODO
  3941. // Call autosave method to save the current progress.
  3942. //$this->index = 0;
  3943. if (api_is_invitee()) {
  3944. return false;
  3945. }
  3946. $session_id = api_get_session_id();
  3947. $course_id = api_get_course_int_id();
  3948. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3949. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3950. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3951. if ($this->debug > 2) {
  3952. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3953. }
  3954. $res = Database::query($sql);
  3955. $view_id = Database::insert_id();
  3956. if ($view_id) {
  3957. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3958. Database::query($sql);
  3959. $this->lp_view_id = $view_id;
  3960. $this->attempt = $this->attempt + 1;
  3961. } else {
  3962. $this->error = 'Could not insert into item_view table...';
  3963. return false;
  3964. }
  3965. $this->autocomplete_parents($this->current);
  3966. foreach ($this->items as $index => $dummy) {
  3967. $this->items[$index]->restart();
  3968. $this->items[$index]->set_lp_view($this->lp_view_id);
  3969. }
  3970. $this->first();
  3971. return true;
  3972. }
  3973. /**
  3974. * Saves the current item
  3975. * @return boolean
  3976. */
  3977. public function save_current()
  3978. {
  3979. if ($this->debug > 0) {
  3980. error_log('learnpath::save_current()', 0);
  3981. }
  3982. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3983. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3984. if ($this->debug > 2) {
  3985. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3986. }
  3987. if ($this->debug > 2) {
  3988. error_log('' . print_r($this->items, true), 0);
  3989. }
  3990. if (isset($this->items[$this->current]) &&
  3991. is_object($this->items[$this->current])
  3992. ) {
  3993. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3994. $this->autocomplete_parents($this->current);
  3995. $status = $this->items[$this->current]->get_status();
  3996. $this->update_queue[$this->current] = $status;
  3997. return $res;
  3998. }
  3999. return false;
  4000. }
  4001. /**
  4002. * Saves the given item
  4003. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  4004. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4005. * @return boolean
  4006. */
  4007. public function save_item($item_id = null, $from_outside = true)
  4008. {
  4009. $debug = $this->debug;
  4010. if ($debug) {
  4011. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4012. }
  4013. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4014. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4015. if (empty($item_id)) {
  4016. $item_id = intval($_REQUEST['id']);
  4017. }
  4018. if (empty($item_id)) {
  4019. $item_id = $this->get_current_item_id();
  4020. }
  4021. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4022. if ($debug) {
  4023. error_log('Object exists');
  4024. }
  4025. // Saving the item.
  4026. $res = $this->items[$item_id]->save(
  4027. $from_outside,
  4028. $this->prerequisites_match($item_id)
  4029. );
  4030. if ($debug) {
  4031. error_log('update_queue before:');
  4032. error_log(print_r($this->update_queue,1));
  4033. }
  4034. $this->autocomplete_parents($item_id);
  4035. $status = $this->items[$item_id]->get_status();
  4036. $this->update_queue[$item_id] = $status;
  4037. if ($debug) {
  4038. error_log('get_status(): ' . $status);
  4039. error_log('update_queue after:');
  4040. error_log(print_r($this->update_queue,1));
  4041. }
  4042. return $res;
  4043. }
  4044. return false;
  4045. }
  4046. /**
  4047. * Saves the last item seen's ID only in case
  4048. */
  4049. public function save_last()
  4050. {
  4051. $course_id = api_get_course_int_id();
  4052. if ($this->debug > 0) {
  4053. error_log('New LP - In learnpath::save_last()', 0);
  4054. }
  4055. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4056. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4057. if (isset($this->current) && !api_is_invitee()) {
  4058. if ($this->debug > 2) {
  4059. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4060. }
  4061. $sql = "UPDATE $table SET
  4062. last_item = " . intval($this->get_current_item_id()). "
  4063. WHERE
  4064. c_id = $course_id AND
  4065. lp_id = " . $this->get_id() . " AND
  4066. user_id = " . $this->get_user_id()." ".$session_condition;
  4067. if ($this->debug > 2) {
  4068. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4069. }
  4070. Database::query($sql);
  4071. }
  4072. if (!api_is_invitee()) {
  4073. // Save progress.
  4074. list($progress, $text) = $this->get_progress_bar_text('%');
  4075. if ($progress >= 0 && $progress <= 100) {
  4076. $progress = (int) $progress;
  4077. $sql = "UPDATE $table SET
  4078. progress = $progress
  4079. WHERE
  4080. c_id = ".$course_id." AND
  4081. lp_id = " . $this->get_id() . " AND
  4082. user_id = " . $this->get_user_id()." ".$session_condition;
  4083. // Ignore errors as some tables might not have the progress field just yet.
  4084. Database::query($sql);
  4085. $this->progress_db = $progress;
  4086. }
  4087. }
  4088. }
  4089. /**
  4090. * Sets the current item ID (checks if valid and authorized first)
  4091. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4092. */
  4093. public function set_current_item($item_id = null)
  4094. {
  4095. if ($this->debug > 0) {
  4096. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4097. }
  4098. if (empty ($item_id)) {
  4099. if ($this->debug > 2) {
  4100. error_log('New LP - No new current item given, ignore...', 0);
  4101. }
  4102. // Do nothing.
  4103. } else {
  4104. if ($this->debug > 2) {
  4105. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4106. }
  4107. if (is_numeric($item_id)) {
  4108. $item_id = intval($item_id);
  4109. // TODO: Check in database here.
  4110. $this->last = $this->current;
  4111. $this->current = $item_id;
  4112. // TODO: Update $this->index as well.
  4113. foreach ($this->ordered_items as $index => $item) {
  4114. if ($item == $this->current) {
  4115. $this->index = $index;
  4116. break;
  4117. }
  4118. }
  4119. if ($this->debug > 2) {
  4120. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4121. }
  4122. } else {
  4123. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4124. }
  4125. }
  4126. }
  4127. /**
  4128. * Sets the encoding
  4129. * @param string New encoding
  4130. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4131. */
  4132. public function set_encoding($enc = 'UTF-8')
  4133. {
  4134. if ($this->debug > 0) {
  4135. error_log('New LP - In learnpath::set_encoding()', 0);
  4136. }
  4137. $course_id = api_get_course_int_id();
  4138. $enc = api_refine_encoding_id($enc);
  4139. if (empty($enc)) {
  4140. $enc = api_get_system_encoding();
  4141. }
  4142. if (api_is_encoding_supported($enc)) {
  4143. $lp = $this->get_id();
  4144. if ($lp != 0) {
  4145. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4146. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4147. $res = Database::query($sql);
  4148. return $res;
  4149. }
  4150. }
  4151. return false;
  4152. }
  4153. /**
  4154. * Sets the JS lib setting in the database directly.
  4155. * This is the JavaScript library file this lp needs to load on startup
  4156. * @param string Proximity setting
  4157. * @return boolean True on update success. False otherwise.
  4158. */
  4159. public function set_jslib($lib = '')
  4160. {
  4161. if ($this->debug > 0) {
  4162. error_log('New LP - In learnpath::set_jslib()', 0);
  4163. }
  4164. $lp = $this->get_id();
  4165. $course_id = api_get_course_int_id();
  4166. if ($lp != 0) {
  4167. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4168. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4169. $res = Database::query($sql);
  4170. return $res;
  4171. } else {
  4172. return false;
  4173. }
  4174. }
  4175. /**
  4176. * Sets the name of the LP maker (publisher) (and save)
  4177. * @param string Optional string giving the new content_maker of this learnpath
  4178. * @return boolean True
  4179. */
  4180. public function set_maker($name = '')
  4181. {
  4182. if ($this->debug > 0) {
  4183. error_log('New LP - In learnpath::set_maker()', 0);
  4184. }
  4185. if (empty ($name))
  4186. return false;
  4187. $this->maker = $name;
  4188. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4189. $course_id = api_get_course_int_id();
  4190. $lp_id = $this->get_id();
  4191. $sql = "UPDATE $lp_table SET
  4192. content_maker = '" . Database::escape_string($this->maker) . "'
  4193. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4194. if ($this->debug > 2) {
  4195. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4196. }
  4197. Database::query($sql);
  4198. return true;
  4199. }
  4200. /**
  4201. * Sets the name of the current learnpath (and save)
  4202. * @param string $name Optional string giving the new name of this learnpath
  4203. * @return boolean True/False
  4204. */
  4205. public function set_name($name = null)
  4206. {
  4207. if ($this->debug > 0) {
  4208. error_log('New LP - In learnpath::set_name()', 0);
  4209. }
  4210. if (empty($name)) {
  4211. return false;
  4212. }
  4213. $this->name = Database::escape_string($name);
  4214. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4215. $lp_id = $this->get_id();
  4216. $course_id = $this->course_info['real_id'];
  4217. $sql = "UPDATE $lp_table SET
  4218. name = '" . Database::escape_string($this->name). "'
  4219. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4220. if ($this->debug > 2) {
  4221. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4222. }
  4223. $result = Database::query($sql);
  4224. // If the lp is visible on the homepage, change his name there.
  4225. if (Database::affected_rows($result)) {
  4226. $session_id = api_get_session_id();
  4227. $session_condition = api_get_session_condition($session_id);
  4228. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4229. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4230. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4231. WHERE
  4232. c_id = $course_id AND
  4233. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4234. Database::query($sql);
  4235. return true;
  4236. } else {
  4237. return false;
  4238. }
  4239. }
  4240. /**
  4241. * Set index specified prefix terms for all items in this path
  4242. * @param string Comma-separated list of terms
  4243. * @param char Xapian term prefix
  4244. * @return boolean False on error, true otherwise
  4245. */
  4246. public function set_terms_by_prefix($terms_string, $prefix)
  4247. {
  4248. $course_id = api_get_course_int_id();
  4249. if (api_get_setting('search_enabled') !== 'true')
  4250. return false;
  4251. if (!extension_loaded('xapian')) {
  4252. return false;
  4253. }
  4254. $terms_string = trim($terms_string);
  4255. $terms = explode(',', $terms_string);
  4256. array_walk($terms, 'trim_value');
  4257. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4258. // Don't do anything if no change, verify only at DB, not the search engine.
  4259. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4260. return false;
  4261. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4262. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4263. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4264. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4265. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4266. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4267. $lp_id = intval($_POST['lp_id']);
  4268. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4269. $result = Database::query($sql);
  4270. $di = new ChamiloIndexer();
  4271. while ($lp_item = Database :: fetch_array($result)) {
  4272. // Get search_did.
  4273. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4274. $sql = 'SELECT * FROM %s
  4275. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4276. LIMIT 1';
  4277. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4278. //echo $sql; echo '<br>';
  4279. $res = Database::query($sql);
  4280. if (Database::num_rows($res) > 0) {
  4281. $se_ref = Database :: fetch_array($res);
  4282. // Compare terms.
  4283. $doc = $di->get_document($se_ref['search_did']);
  4284. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4285. $xterms = array();
  4286. foreach ($xapian_terms as $xapian_term) {
  4287. $xterms[] = substr($xapian_term['name'], 1);
  4288. }
  4289. $dterms = $terms;
  4290. $missing_terms = array_diff($dterms, $xterms);
  4291. $deprecated_terms = array_diff($xterms, $dterms);
  4292. // Save it to search engine.
  4293. foreach ($missing_terms as $term) {
  4294. $doc->add_term($prefix . $term, 1);
  4295. }
  4296. foreach ($deprecated_terms as $term) {
  4297. $doc->remove_term($prefix . $term);
  4298. }
  4299. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4300. $di->getDb()->flush();
  4301. } else {
  4302. //@todo What we should do here?
  4303. }
  4304. }
  4305. return true;
  4306. }
  4307. /**
  4308. * Sets the theme of the LP (local/remote) (and save)
  4309. * @param string Optional string giving the new theme of this learnpath
  4310. * @return bool Returns true if theme name is not empty
  4311. */
  4312. public function set_theme($name = '')
  4313. {
  4314. $course_id = api_get_course_int_id();
  4315. if ($this->debug > 0) {
  4316. error_log('New LP - In learnpath::set_theme()', 0);
  4317. }
  4318. $this->theme = $name;
  4319. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4320. $lp_id = $this->get_id();
  4321. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4322. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4323. if ($this->debug > 2) {
  4324. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4325. }
  4326. Database::query($sql);
  4327. return true;
  4328. }
  4329. /**
  4330. * Sets the image of an LP (and save)
  4331. * @param string Optional string giving the new image of this learnpath
  4332. * @return bool Returns true if theme name is not empty
  4333. */
  4334. public function set_preview_image($name = '')
  4335. {
  4336. $course_id = api_get_course_int_id();
  4337. if ($this->debug > 0) {
  4338. error_log('New LP - In learnpath::set_preview_image()', 0);
  4339. }
  4340. $this->preview_image = $name;
  4341. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4342. $lp_id = $this->get_id();
  4343. $sql = "UPDATE $lp_table SET
  4344. preview_image = '" . Database::escape_string($this->preview_image). "'
  4345. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4346. if ($this->debug > 2) {
  4347. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4348. }
  4349. Database::query($sql);
  4350. return true;
  4351. }
  4352. /**
  4353. * Sets the author of a LP (and save)
  4354. * @param string Optional string giving the new author of this learnpath
  4355. * @return bool Returns true if author's name is not empty
  4356. */
  4357. public function set_author($name = '')
  4358. {
  4359. $course_id = api_get_course_int_id();
  4360. if ($this->debug > 0) {
  4361. error_log('New LP - In learnpath::set_author()', 0);
  4362. }
  4363. $this->author = $name;
  4364. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4365. $lp_id = $this->get_id();
  4366. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4367. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4368. if ($this->debug > 2) {
  4369. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4370. }
  4371. Database::query($sql);
  4372. return true;
  4373. }
  4374. /**
  4375. * Sets the hide_toc_frame parameter of a LP (and save)
  4376. * @param int 1 if frame is hidden 0 then else
  4377. * @return bool Returns true if author's name is not empty
  4378. */
  4379. public function set_hide_toc_frame($hide)
  4380. {
  4381. $course_id = api_get_course_int_id();
  4382. if ($this->debug > 0) {
  4383. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4384. }
  4385. if (intval($hide) == $hide){
  4386. $this->hide_toc_frame = $hide;
  4387. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4388. $lp_id = $this->get_id();
  4389. $sql = "UPDATE $lp_table SET
  4390. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4391. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4392. if ($this->debug > 2) {
  4393. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4394. }
  4395. Database::query($sql);
  4396. return true;
  4397. } else {
  4398. return false;
  4399. }
  4400. }
  4401. /**
  4402. * Sets the prerequisite of a LP (and save)
  4403. * @param int integer giving the new prerequisite of this learnpath
  4404. * @return bool returns true if prerequisite is not empty
  4405. */
  4406. public function set_prerequisite($prerequisite)
  4407. {
  4408. $course_id = api_get_course_int_id();
  4409. if ($this->debug > 0) {
  4410. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4411. }
  4412. $this->prerequisite = intval($prerequisite);
  4413. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4414. $lp_id = $this->get_id();
  4415. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4416. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4417. if ($this->debug > 2) {
  4418. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4419. }
  4420. Database::query($sql);
  4421. return true;
  4422. }
  4423. /**
  4424. * Sets the location/proximity of the LP (local/remote) (and save)
  4425. * @param string Optional string giving the new location of this learnpath
  4426. * @return boolean True on success / False on error
  4427. */
  4428. public function set_proximity($name = '')
  4429. {
  4430. $course_id = api_get_course_int_id();
  4431. if ($this->debug > 0) {
  4432. error_log('New LP - In learnpath::set_proximity()', 0);
  4433. }
  4434. if (empty ($name))
  4435. return false;
  4436. $this->proximity = $name;
  4437. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4438. $lp_id = $this->get_id();
  4439. $sql = "UPDATE $lp_table SET
  4440. content_local = '" . Database::escape_string($name) . "'
  4441. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4442. if ($this->debug > 2) {
  4443. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4444. }
  4445. Database::query($sql);
  4446. return true;
  4447. }
  4448. /**
  4449. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4450. * @param integer DB ID of the item
  4451. */
  4452. public function set_previous_item($id)
  4453. {
  4454. if ($this->debug > 0) {
  4455. error_log('New LP - In learnpath::set_previous_item()', 0);
  4456. }
  4457. $this->last = $id;
  4458. }
  4459. /**
  4460. * Sets use_max_score
  4461. * @param string $use_max_score Optional string giving the new location of this learnpath
  4462. * @return boolean True on success / False on error
  4463. */
  4464. public function set_use_max_score($use_max_score = 1)
  4465. {
  4466. $course_id = api_get_course_int_id();
  4467. if ($this->debug > 0) {
  4468. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4469. }
  4470. $use_max_score = intval($use_max_score);
  4471. $this->use_max_score = $use_max_score;
  4472. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4473. $lp_id = $this->get_id();
  4474. $sql = "UPDATE $lp_table SET
  4475. use_max_score = '" . $this->use_max_score . "'
  4476. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4477. if ($this->debug > 2) {
  4478. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4479. }
  4480. Database::query($sql);
  4481. return true;
  4482. }
  4483. /**
  4484. * Sets and saves the expired_on date
  4485. * @param string $expired_on Optional string giving the new author of this learnpath
  4486. * @return bool Returns true if author's name is not empty
  4487. */
  4488. public function set_expired_on($expired_on)
  4489. {
  4490. $course_id = api_get_course_int_id();
  4491. if ($this->debug > 0) {
  4492. error_log('New LP - In learnpath::set_expired_on()', 0);
  4493. }
  4494. if (!empty($expired_on)) {
  4495. $this->expired_on = api_get_utc_datetime($expired_on);
  4496. } else {
  4497. $this->expired_on = '';
  4498. }
  4499. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4500. $lp_id = $this->get_id();
  4501. $sql = "UPDATE $lp_table SET
  4502. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4503. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4504. if ($this->debug > 2) {
  4505. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4506. }
  4507. Database::query($sql);
  4508. return true;
  4509. }
  4510. /**
  4511. * Sets and saves the publicated_on date
  4512. * @param string $publicated_on Optional string giving the new author of this learnpath
  4513. * @return bool Returns true if author's name is not empty
  4514. */
  4515. public function set_publicated_on($publicated_on)
  4516. {
  4517. $course_id = api_get_course_int_id();
  4518. if ($this->debug > 0) {
  4519. error_log('New LP - In learnpath::set_expired_on()', 0);
  4520. }
  4521. if (!empty($publicated_on)) {
  4522. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4523. } else {
  4524. $this->publicated_on = '';
  4525. }
  4526. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4527. $lp_id = $this->get_id();
  4528. $sql = "UPDATE $lp_table SET
  4529. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4530. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4531. if ($this->debug > 2) {
  4532. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4533. }
  4534. Database::query($sql);
  4535. return true;
  4536. }
  4537. /**
  4538. * Sets and saves the expired_on date
  4539. * @return bool Returns true if author's name is not empty
  4540. */
  4541. public function set_modified_on()
  4542. {
  4543. $course_id = api_get_course_int_id();
  4544. if ($this->debug > 0) {
  4545. error_log('New LP - In learnpath::set_expired_on()', 0);
  4546. }
  4547. $this->modified_on = api_get_utc_datetime();
  4548. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4549. $lp_id = $this->get_id();
  4550. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4551. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4552. if ($this->debug > 2) {
  4553. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4554. }
  4555. Database::query($sql);
  4556. return true;
  4557. }
  4558. /**
  4559. * Sets the object's error message
  4560. * @param string Error message. If empty, reinits the error string
  4561. * @return void
  4562. */
  4563. public function set_error_msg($error = '')
  4564. {
  4565. if ($this->debug > 0) {
  4566. error_log('New LP - In learnpath::set_error_msg()', 0);
  4567. }
  4568. if (empty ($error)) {
  4569. $this->error = '';
  4570. } else {
  4571. $this->error .= $error;
  4572. }
  4573. }
  4574. /**
  4575. * Launches the current item if not 'sco'
  4576. * (starts timer and make sure there is a record ready in the DB)
  4577. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4578. * @return boolean
  4579. */
  4580. public function start_current_item($allow_new_attempt = false)
  4581. {
  4582. if ($this->debug > 0) {
  4583. error_log('New LP - In learnpath::start_current_item()', 0);
  4584. }
  4585. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4586. $type = $this->get_type();
  4587. $item_type = $this->items[$this->current]->get_type();
  4588. if (($type == 2 && $item_type != 'sco') ||
  4589. ($type == 3 && $item_type != 'au') ||
  4590. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4591. ) {
  4592. $this->items[$this->current]->open($allow_new_attempt);
  4593. $this->autocomplete_parents($this->current);
  4594. $prereq_check = $this->prerequisites_match($this->current);
  4595. $this->items[$this->current]->save(false, $prereq_check);
  4596. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4597. } else {
  4598. // If sco, then it is supposed to have been updated by some other call.
  4599. }
  4600. if ($item_type == 'sco') {
  4601. $this->items[$this->current]->restart();
  4602. }
  4603. }
  4604. if ($this->debug > 0) {
  4605. error_log('New LP - End of learnpath::start_current_item()', 0);
  4606. }
  4607. return true;
  4608. }
  4609. /**
  4610. * Stops the processing and counters for the old item (as held in $this->last)
  4611. * @return boolean True/False
  4612. */
  4613. public function stop_previous_item()
  4614. {
  4615. if ($this->debug > 0) {
  4616. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4617. }
  4618. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4619. if ($this->debug > 2) {
  4620. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4621. }
  4622. switch ($this->get_type()) {
  4623. case '3' :
  4624. if ($this->items[$this->last]->get_type() != 'au') {
  4625. if ($this->debug > 2) {
  4626. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4627. }
  4628. $this->items[$this->last]->close();
  4629. //$this->autocomplete_parents($this->last);
  4630. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4631. } else {
  4632. if ($this->debug > 2) {
  4633. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4634. }
  4635. }
  4636. case '2' :
  4637. if ($this->items[$this->last]->get_type() != 'sco') {
  4638. if ($this->debug > 2) {
  4639. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4640. }
  4641. $this->items[$this->last]->close();
  4642. //$this->autocomplete_parents($this->last);
  4643. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4644. } else {
  4645. if ($this->debug > 2) {
  4646. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4647. }
  4648. }
  4649. break;
  4650. case '1' :
  4651. default :
  4652. if ($this->debug > 2) {
  4653. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4654. }
  4655. $this->items[$this->last]->close();
  4656. break;
  4657. }
  4658. } else {
  4659. if ($this->debug > 2) {
  4660. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4661. }
  4662. return false;
  4663. }
  4664. return true;
  4665. }
  4666. /**
  4667. * Updates the default view mode from fullscreen to embedded and inversely
  4668. * @return string The current default view mode ('fullscreen' or 'embedded')
  4669. */
  4670. public function update_default_view_mode()
  4671. {
  4672. $course_id = api_get_course_int_id();
  4673. if ($this->debug > 0) {
  4674. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4675. }
  4676. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4677. $sql = "SELECT * FROM $lp_table
  4678. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4679. $res = Database::query($sql);
  4680. if (Database :: num_rows($res) > 0) {
  4681. $row = Database :: fetch_array($res);
  4682. $default_view_mode = $row['default_view_mod'];
  4683. $view_mode = $default_view_mode;
  4684. switch ($default_view_mode) {
  4685. case 'fullscreen': // default with popup
  4686. $view_mode = 'embedded';
  4687. break;
  4688. case 'embedded': // default view with left menu
  4689. $view_mode = 'embedframe';
  4690. break;
  4691. case 'embedframe': //folded menu
  4692. $view_mode = 'impress';
  4693. break;
  4694. case 'impress':
  4695. $view_mode = 'fullscreen';
  4696. break;
  4697. }
  4698. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4699. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4700. Database::query($sql);
  4701. $this->mode = $view_mode;
  4702. return $view_mode;
  4703. } else {
  4704. if ($this->debug > 2) {
  4705. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4706. }
  4707. }
  4708. return -1;
  4709. }
  4710. /**
  4711. * Updates the default behaviour about auto-commiting SCORM updates
  4712. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4713. */
  4714. public function update_default_scorm_commit()
  4715. {
  4716. $course_id = api_get_course_int_id();
  4717. if ($this->debug > 0) {
  4718. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4719. }
  4720. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4721. $sql = "SELECT * FROM $lp_table
  4722. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4723. $res = Database::query($sql);
  4724. if (Database :: num_rows($res) > 0) {
  4725. $row = Database :: fetch_array($res);
  4726. $force = $row['force_commit'];
  4727. if ($force == 1) {
  4728. $force = 0;
  4729. $force_return = false;
  4730. } elseif ($force == 0) {
  4731. $force = 1;
  4732. $force_return = true;
  4733. }
  4734. $sql = "UPDATE $lp_table SET force_commit = $force
  4735. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4736. Database::query($sql);
  4737. $this->force_commit = $force_return;
  4738. return $force_return;
  4739. } else {
  4740. if ($this->debug > 2) {
  4741. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4742. }
  4743. }
  4744. return -1;
  4745. }
  4746. /**
  4747. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4748. * @return bool True on success, false on failure
  4749. */
  4750. public function update_display_order()
  4751. {
  4752. $course_id = api_get_course_int_id();
  4753. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4754. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4755. $res = Database::query($sql);
  4756. if ($res === false)
  4757. return false;
  4758. $num = Database :: num_rows($res);
  4759. // First check the order is correct, globally (might be wrong because
  4760. // of versions < 1.8.4).
  4761. if ($num > 0) {
  4762. $i = 1;
  4763. while ($row = Database :: fetch_array($res)) {
  4764. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4765. $need_fix = true;
  4766. $sql = "UPDATE $lp_table SET display_order = $i
  4767. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4768. Database::query($sql);
  4769. }
  4770. $i++;
  4771. }
  4772. }
  4773. return true;
  4774. }
  4775. /**
  4776. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4777. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4778. */
  4779. public function update_reinit()
  4780. {
  4781. $course_id = api_get_course_int_id();
  4782. if ($this->debug > 0) {
  4783. error_log('New LP - In learnpath::update_reinit()', 0);
  4784. }
  4785. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4786. $sql = "SELECT * FROM $lp_table
  4787. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4788. $res = Database::query($sql);
  4789. if (Database :: num_rows($res) > 0) {
  4790. $row = Database :: fetch_array($res);
  4791. $force = $row['prevent_reinit'];
  4792. if ($force == 1) {
  4793. $force = 0;
  4794. } elseif ($force == 0) {
  4795. $force = 1;
  4796. }
  4797. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4798. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4799. Database::query($sql);
  4800. $this->prevent_reinit = $force;
  4801. return $force;
  4802. } else {
  4803. if ($this->debug > 2) {
  4804. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4805. }
  4806. }
  4807. return -1;
  4808. }
  4809. /**
  4810. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4811. *
  4812. * @return string 'single', 'multi' or 'seriousgame'
  4813. * @author ndiechburg <noel@cblue.be>
  4814. **/
  4815. public function get_attempt_mode()
  4816. {
  4817. //Set default value for seriousgame_mode
  4818. if (!isset($this->seriousgame_mode)) {
  4819. $this->seriousgame_mode=0;
  4820. }
  4821. // Set default value for prevent_reinit
  4822. if (!isset($this->prevent_reinit)) {
  4823. $this->prevent_reinit =1;
  4824. }
  4825. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4826. return 'seriousgame';
  4827. }
  4828. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4829. return 'single';
  4830. }
  4831. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4832. return 'multiple';
  4833. }
  4834. return 'single';
  4835. }
  4836. /**
  4837. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4838. *
  4839. * @param string 'seriousgame', 'single' or 'multiple'
  4840. * @return boolean
  4841. * @author ndiechburg <noel@cblue.be>
  4842. **/
  4843. public function set_attempt_mode($mode)
  4844. {
  4845. $course_id = api_get_course_int_id();
  4846. switch ($mode) {
  4847. case 'seriousgame' :
  4848. $sg_mode = 1;
  4849. $prevent_reinit = 1;
  4850. break;
  4851. case 'single' :
  4852. $sg_mode = 0;
  4853. $prevent_reinit = 1;
  4854. break;
  4855. case 'multiple' :
  4856. $sg_mode = 0;
  4857. $prevent_reinit = 0;
  4858. break;
  4859. default :
  4860. $sg_mode = 0;
  4861. $prevent_reinit = 0;
  4862. break;
  4863. }
  4864. $this->prevent_reinit = $prevent_reinit;
  4865. $this->seriousgame_mode = $sg_mode;
  4866. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4867. $sql = "UPDATE $lp_table SET
  4868. prevent_reinit = $prevent_reinit ,
  4869. seriousgame_mode = $sg_mode
  4870. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4871. $res = Database::query($sql);
  4872. if ($res) {
  4873. return true;
  4874. } else {
  4875. return false;
  4876. }
  4877. }
  4878. /**
  4879. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4880. *
  4881. * @return boolean
  4882. * @author ndiechburg <noel@cblue.be>
  4883. **/
  4884. public function switch_attempt_mode()
  4885. {
  4886. if ($this->debug > 0) {
  4887. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4888. }
  4889. $mode = $this->get_attempt_mode();
  4890. switch ($mode) {
  4891. case 'single' :
  4892. $next_mode = 'multiple';
  4893. break;
  4894. case 'multiple' :
  4895. $next_mode = 'seriousgame';
  4896. break;
  4897. case 'seriousgame' :
  4898. $next_mode = 'single';
  4899. break;
  4900. default :
  4901. $next_mode = 'single';
  4902. break;
  4903. }
  4904. $this->set_attempt_mode($next_mode);
  4905. }
  4906. /**
  4907. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4908. * but possibility to do again a completed item.
  4909. *
  4910. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4911. * @author ndiechburg <noel@cblue.be>
  4912. **/
  4913. public function set_seriousgame_mode()
  4914. {
  4915. $course_id = api_get_course_int_id();
  4916. if ($this->debug > 0) {
  4917. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4918. }
  4919. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4920. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4921. $res = Database::query($sql);
  4922. if (Database :: num_rows($res) > 0) {
  4923. $row = Database :: fetch_array($res);
  4924. $force = $row['seriousgame_mode'];
  4925. if ($force == 1) {
  4926. $force = 0;
  4927. } elseif ($force == 0) {
  4928. $force = 1;
  4929. }
  4930. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4931. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4932. Database::query($sql);
  4933. $this->seriousgame_mode = $force;
  4934. return $force;
  4935. } else {
  4936. if ($this->debug > 2) {
  4937. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4938. }
  4939. }
  4940. return -1;
  4941. }
  4942. /**
  4943. * Updates the "scorm_debug" value that shows or hide the debug window
  4944. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4945. */
  4946. public function update_scorm_debug()
  4947. {
  4948. $course_id = api_get_course_int_id();
  4949. if ($this->debug > 0) {
  4950. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4951. }
  4952. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4953. $sql = "SELECT * FROM $lp_table
  4954. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4955. $res = Database::query($sql);
  4956. if (Database :: num_rows($res) > 0) {
  4957. $row = Database :: fetch_array($res);
  4958. $force = $row['debug'];
  4959. if ($force == 1) {
  4960. $force = 0;
  4961. } elseif ($force == 0) {
  4962. $force = 1;
  4963. }
  4964. $sql = "UPDATE $lp_table SET debug = $force
  4965. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4966. $res = Database::query($sql);
  4967. $this->scorm_debug = $force;
  4968. return $force;
  4969. } else {
  4970. if ($this->debug > 2) {
  4971. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4972. }
  4973. }
  4974. return -1;
  4975. }
  4976. /**
  4977. * Function that makes a call to the function sort_tree_array and create_tree_array
  4978. * @author Kevin Van Den Haute
  4979. * @param array
  4980. */
  4981. public function tree_array($array)
  4982. {
  4983. if ($this->debug > 1) {
  4984. error_log('New LP - In learnpath::tree_array()', 0);
  4985. }
  4986. $array = $this->sort_tree_array($array);
  4987. $this->create_tree_array($array);
  4988. }
  4989. /**
  4990. * Creates an array with the elements of the learning path tree in it
  4991. *
  4992. * @author Kevin Van Den Haute
  4993. * @param array $array
  4994. * @param int $parent
  4995. * @param int $depth
  4996. * @param array $tmp
  4997. */
  4998. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4999. {
  5000. if ($this->debug > 1) {
  5001. error_log('New LP - In learnpath::create_tree_array())', 0);
  5002. }
  5003. if (is_array($array)) {
  5004. for ($i = 0; $i < count($array); $i++) {
  5005. if ($array[$i]['parent_item_id'] == $parent) {
  5006. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5007. $tmp[] = $array[$i]['parent_item_id'];
  5008. $depth++;
  5009. }
  5010. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5011. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5012. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5013. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5014. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5015. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5016. $this->arrMenu[] = array(
  5017. 'id' => $array[$i]['id'],
  5018. 'ref' => $ref,
  5019. 'item_type' => $array[$i]['item_type'],
  5020. 'title' => $array[$i]['title'],
  5021. 'path' => $path,
  5022. 'description' => $array[$i]['description'],
  5023. 'parent_item_id' => $array[$i]['parent_item_id'],
  5024. 'previous_item_id' => $array[$i]['previous_item_id'],
  5025. 'next_item_id' => $array[$i]['next_item_id'],
  5026. 'min_score' => $array[$i]['min_score'],
  5027. 'max_score' => $array[$i]['max_score'],
  5028. 'mastery_score' => $array[$i]['mastery_score'],
  5029. 'display_order' => $array[$i]['display_order'],
  5030. 'prerequisite' => $preq,
  5031. 'depth' => $depth,
  5032. 'audio' => $audio,
  5033. 'prerequisite_min_score' => $prerequisiteMinScore,
  5034. 'prerequisite_max_score' => $prerequisiteMaxScore
  5035. );
  5036. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5037. }
  5038. }
  5039. }
  5040. }
  5041. /**
  5042. * Sorts a multi dimensional array by parent id and display order
  5043. * @author Kevin Van Den Haute
  5044. *
  5045. * @param array $array (array with al the learning path items in it)
  5046. *
  5047. * @return array
  5048. */
  5049. public function sort_tree_array($array) {
  5050. foreach ($array as $key => $row) {
  5051. $parent[$key] = $row['parent_item_id'];
  5052. $position[$key] = $row['display_order'];
  5053. }
  5054. if (count($array) > 0)
  5055. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5056. return $array;
  5057. }
  5058. /**
  5059. * Function that creates a html list of learning path items so that we can add audio files to them
  5060. * @author Kevin Van Den Haute
  5061. * @param int $lp_id
  5062. * @return string
  5063. */
  5064. public function overview()
  5065. {
  5066. if ($this->debug > 0) {
  5067. error_log('New LP - In learnpath::overview()', 0);
  5068. }
  5069. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5070. $return = '';
  5071. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5072. // we need to start a form when we want to update all the mp3 files
  5073. if ($update_audio == 'true') {
  5074. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&action=' . Security :: remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5075. }
  5076. $return .= '<div id="message"></div>';
  5077. if (count($this->items) == 0) {
  5078. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5079. } else {
  5080. $return_audio = '<table class="data_table">';
  5081. $return_audio .= '<tr>';
  5082. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5083. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5084. $return_audio .= '</tr>';
  5085. if ($update_audio != 'true') {
  5086. $return .= '<div class="col-md-12">';
  5087. $return .= self::return_new_tree($update_audio);
  5088. $return .='</div>';
  5089. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5090. } else {
  5091. $return_audio .= self::return_new_tree($update_audio);
  5092. $return .= $return_audio.'</table>';
  5093. }
  5094. // We need to close the form when we are updating the mp3 files.
  5095. if ($update_audio == 'true') {
  5096. $return .= '<div class="footer-audio">';
  5097. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5098. $return .= '</div>';
  5099. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5100. }
  5101. }
  5102. // We need to close the form when we are updating the mp3 files.
  5103. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5104. $return .= '</form>';
  5105. }
  5106. return $return;
  5107. }
  5108. /**
  5109. * @param string string $update_audio
  5110. * @param bool $drop_element_here
  5111. * @return string
  5112. */
  5113. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5114. {
  5115. $return = '';
  5116. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5117. $course_id = api_get_course_int_id();
  5118. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5119. $sql = "SELECT * FROM $tbl_lp_item
  5120. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5121. $result = Database::query($sql);
  5122. $arrLP = array();
  5123. while ($row = Database :: fetch_array($result)) {
  5124. $arrLP[] = array(
  5125. 'id' => $row['id'],
  5126. 'item_type' => $row['item_type'],
  5127. 'title' => Security :: remove_XSS($row['title']),
  5128. 'path' => $row['path'],
  5129. 'description' => Security::remove_XSS($row['description']),
  5130. 'parent_item_id' => $row['parent_item_id'],
  5131. 'previous_item_id' => $row['previous_item_id'],
  5132. 'next_item_id' => $row['next_item_id'],
  5133. 'max_score' => $row['max_score'],
  5134. 'min_score' => $row['min_score'],
  5135. 'mastery_score' => $row['mastery_score'],
  5136. 'prerequisite' => $row['prerequisite'],
  5137. 'display_order' => $row['display_order'],
  5138. 'audio' => $row['audio'],
  5139. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5140. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5141. );
  5142. }
  5143. $this->tree_array($arrLP);
  5144. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5145. unset ($this->arrMenu);
  5146. $default_data = null;
  5147. $default_content = null;
  5148. $elements = array();
  5149. $return_audio = null;
  5150. for ($i = 0; $i < count($arrLP); $i++) {
  5151. $title = $arrLP[$i]['title'];
  5152. $title_cut = cut($arrLP[$i]['title'], 25);
  5153. // Link for the documents
  5154. if ($arrLP[$i]['item_type'] == 'document') {
  5155. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5156. $title_cut = Display::url(
  5157. $title_cut,
  5158. $url,
  5159. array(
  5160. 'class' => 'ajax moved',
  5161. 'data-title' => $title_cut
  5162. )
  5163. );
  5164. }
  5165. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5166. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5167. $_SESSION['pathItem'] = $arrLP[$i]['path'];
  5168. }
  5169. if (($i % 2) == 0) {
  5170. $oddClass = 'row_odd';
  5171. } else {
  5172. $oddClass = 'row_even';
  5173. }
  5174. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5175. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5176. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5177. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5178. } else {
  5179. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5180. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5181. } else {
  5182. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5183. $icon = Display::return_icon('certificate.png');
  5184. } else {
  5185. $icon = Display::return_icon('folder_document.gif');
  5186. }
  5187. }
  5188. }
  5189. // The audio column.
  5190. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5191. $audio = '';
  5192. if (!$update_audio || $update_audio <> 'true') {
  5193. if (!empty($arrLP[$i]['audio'])) {
  5194. } else {
  5195. $audio .= '';
  5196. }
  5197. } else {
  5198. $types = self::getChapterTypes();
  5199. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5200. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5201. if (!empty ($arrLP[$i]['audio'])) {
  5202. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5203. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5204. }
  5205. }
  5206. }
  5207. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5208. $return_audio .= '</td>';
  5209. $move_icon = '';
  5210. $move_item_icon = '';
  5211. $edit_icon = '';
  5212. $delete_icon = '';
  5213. $audio_icon = '';
  5214. $prerequisities_icon = '';
  5215. $forumIcon = '';
  5216. if ($is_allowed_to_edit) {
  5217. if (!$update_audio || $update_audio <> 'true') {
  5218. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5219. $move_icon .= '<a class="moved" href="#">';
  5220. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5221. $move_icon .= '</a>';
  5222. }
  5223. }
  5224. // No edit for this item types
  5225. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5226. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5227. $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">';
  5228. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5229. $edit_icon .= '</a>';
  5230. if (
  5231. !in_array($arrLP[$i]['item_type'], ['forum', 'thread'])
  5232. ) {
  5233. if (
  5234. $this->items[$arrLP[$i]['id']]->getForumThread(
  5235. $this->course_int_id,
  5236. $this->lp_session_id
  5237. )
  5238. ) {
  5239. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5240. 'action' => 'dissociate_forum',
  5241. 'id' => $arrLP[$i]['id'],
  5242. 'lp_id' => $this->lp_id
  5243. ]);
  5244. $forumIcon = Display::url(
  5245. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5246. $forumIconUrl,
  5247. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5248. );
  5249. } else {
  5250. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5251. 'action' => 'create_forum',
  5252. 'id' => $arrLP[$i]['id'],
  5253. 'lp_id' => $this->lp_id
  5254. ]);
  5255. $forumIcon = Display::url(
  5256. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5257. $forumIconUrl,
  5258. ['class' => "btn btn-default lp-btn-associate-forum"]
  5259. );
  5260. }
  5261. }
  5262. } else {
  5263. $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">';
  5264. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5265. $edit_icon .= '</a>';
  5266. }
  5267. }
  5268. $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">';
  5269. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5270. $delete_icon .= '</a>';
  5271. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5272. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5273. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']);
  5274. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item', ['class' => 'btn btn-default']);
  5275. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio', ['class' => 'btn btn-default']);
  5276. }
  5277. }
  5278. if ($update_audio != 'true') {
  5279. $row = $move_icon . ' ' . $icon .
  5280. Display::span($title_cut) .
  5281. Display::tag(
  5282. 'div',
  5283. "<div class=\"btn-group btn-group-xs\">$audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5284. array('class'=>'btn-toolbar button_actions')
  5285. );
  5286. } else {
  5287. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5288. }
  5289. $parent_id = $arrLP[$i]['parent_item_id'];
  5290. $default_data[$arrLP[$i]['id']] = $row;
  5291. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5292. if (empty($parent_id)) {
  5293. $elements[$arrLP[$i]['id']]['data'] = $row;
  5294. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5295. } else {
  5296. $parent_arrays = array();
  5297. if ($arrLP[$i]['depth'] > 1) {
  5298. //Getting list of parents
  5299. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5300. foreach($arrLP as $item) {
  5301. if ($item['id'] == $parent_id) {
  5302. if ($item['parent_item_id'] == 0) {
  5303. $parent_id = $item['id'];
  5304. break;
  5305. } else {
  5306. $parent_id = $item['parent_item_id'];
  5307. if (empty($parent_arrays)) {
  5308. $parent_arrays[] = intval($item['id']);
  5309. }
  5310. $parent_arrays[] = $parent_id;
  5311. break;
  5312. }
  5313. }
  5314. }
  5315. }
  5316. }
  5317. if (!empty($parent_arrays)) {
  5318. $parent_arrays = array_reverse($parent_arrays);
  5319. $val = '$elements';
  5320. $x = 0;
  5321. foreach($parent_arrays as $item) {
  5322. if ($x != count($parent_arrays) -1) {
  5323. $val .= '["'.$item.'"]["children"]';
  5324. } else {
  5325. $val .= '["'.$item.'"]["children"]';
  5326. }
  5327. $x++;
  5328. }
  5329. $val .= "";
  5330. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5331. eval($code_str);
  5332. } else {
  5333. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5334. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5335. }
  5336. }
  5337. }
  5338. $list = '<ul id="lp_item_list">';
  5339. $tree = self::print_recursive($elements, $default_data, $default_content);
  5340. if (!empty($tree)) {
  5341. $list .= $tree;
  5342. } else {
  5343. if ($drop_element_here) {
  5344. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5345. }
  5346. }
  5347. $list .= '</ul>';
  5348. //$return .= Display::panel($list, $this->name);
  5349. $return .= Display::panelCollapse($this->name, $list, 'scorm-list', null, 'scorm-list-accordion', 'scorm-list-collapse');
  5350. if ($update_audio == 'true') {
  5351. $return = $return_audio;
  5352. }
  5353. return $return;
  5354. }
  5355. /**
  5356. * @param array $elements
  5357. * @param array $default_data
  5358. * @param array $default_content
  5359. * @return string
  5360. */
  5361. public function print_recursive($elements, $default_data, $default_content)
  5362. {
  5363. $return = '';
  5364. foreach ($elements as $key => $item) {
  5365. if (isset($item['load_data']) || empty($item['data'])) {
  5366. $item['data'] = $default_data[$item['load_data']];
  5367. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5368. }
  5369. $sub_list = '';
  5370. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5371. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5372. }
  5373. if (empty($item['children'])) {
  5374. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5375. $active = null;
  5376. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5377. $active = 'active';
  5378. }
  5379. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5380. } else {
  5381. //sections
  5382. if (isset($item['children'])) {
  5383. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5384. }
  5385. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5386. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5387. }
  5388. }
  5389. return $return;
  5390. }
  5391. /**
  5392. * This function builds the action menu
  5393. * @param bool $returnContent
  5394. * @return void
  5395. */
  5396. public function build_action_menu($returnContent = false)
  5397. {
  5398. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5399. $return = '<div class="actions">';
  5400. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '&isStudentView=true">' . Display :: return_icon('preview_view.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  5401. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=admin_view&lp_id=' . $_SESSION['oLP']->lp_id . '&updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5402. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&action=edit&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5403. $buttons = array(
  5404. array(
  5405. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5406. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5407. ),
  5408. array(
  5409. 'title' => get_lang('ClearAllPrerequisites'),
  5410. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5411. ),
  5412. );
  5413. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5414. $return .= '</div>';
  5415. if ($returnContent) {
  5416. return $return;
  5417. }
  5418. echo $return;
  5419. }
  5420. /**
  5421. * Creates the default learning path folder
  5422. * @param array $course
  5423. * @param int $creatorId
  5424. *
  5425. * @return bool
  5426. */
  5427. public static function generate_learning_path_folder($course, $creatorId = 0)
  5428. {
  5429. // Creating learning_path folder
  5430. $dir = '/learning_path';
  5431. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5432. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5433. $folder = false;
  5434. if (!is_dir($filepath.'/'.$dir)) {
  5435. $folderData = create_unexisting_directory(
  5436. $course,
  5437. $creatorId,
  5438. api_get_session_id(),
  5439. 0,
  5440. 0,
  5441. $filepath,
  5442. $dir,
  5443. get_lang('LearningPaths'),
  5444. 0
  5445. );
  5446. if (!empty($folderData)) {
  5447. $folder = true;
  5448. }
  5449. } else {
  5450. $folder = true;
  5451. }
  5452. return $folder;
  5453. }
  5454. /**
  5455. * @param array $course
  5456. * @param string $lp_name
  5457. * @param int $creatorId
  5458. *
  5459. * @return array
  5460. */
  5461. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5462. {
  5463. $filepath = '';
  5464. $dir = '/learning_path/';
  5465. if (empty($lp_name)) {
  5466. $lp_name = $this->name;
  5467. }
  5468. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5469. $folder = self::generate_learning_path_folder($course, $creatorId);
  5470. // Creating LP folder
  5471. if ($folder) {
  5472. //Limits title size
  5473. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5474. $dir = $dir.$title;
  5475. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5476. if (!is_dir($filepath.'/'.$dir)) {
  5477. $folderData = create_unexisting_directory(
  5478. $course,
  5479. $creatorId,
  5480. 0,
  5481. 0,
  5482. 0,
  5483. $filepath,
  5484. $dir,
  5485. $lp_name
  5486. );
  5487. if (!empty($folderData)) {
  5488. $folder = true;
  5489. }
  5490. } else {
  5491. $folder = true;
  5492. }
  5493. $dir = $dir.'/';
  5494. if ($folder) {
  5495. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5496. }
  5497. }
  5498. $array = array(
  5499. 'dir' => $dir,
  5500. 'filepath' => $filepath,
  5501. 'folder' => $folder
  5502. );
  5503. return $array;
  5504. }
  5505. /**
  5506. * Create a new document //still needs some finetuning
  5507. * @param array $courseInfo
  5508. * @param string $content
  5509. * @param string $title
  5510. * @param string $extension
  5511. * @param int $creatorId creator id
  5512. *
  5513. * @return string
  5514. */
  5515. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $creatorId = 0)
  5516. {
  5517. if (!empty($courseInfo)) {
  5518. $course_id = $courseInfo['real_id'];
  5519. } else {
  5520. $course_id = api_get_course_int_id();
  5521. }
  5522. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5523. $sessionId = api_get_session_id();
  5524. global $charset;
  5525. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5526. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5527. // Please, do not modify this dirname formatting.
  5528. if (strstr($dir, '..')) {
  5529. $dir = '/';
  5530. }
  5531. if (!empty($dir[0]) && $dir[0] == '.') {
  5532. $dir = substr($dir, 1);
  5533. }
  5534. if (!empty($dir[0]) && $dir[0] != '/') {
  5535. $dir = '/' . $dir;
  5536. }
  5537. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5538. $dir .= '/';
  5539. }
  5540. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document' . $dir;
  5541. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5542. //Generates folder
  5543. $result = $this->generate_lp_folder($courseInfo, '', $creatorId);
  5544. $dir = $result['dir'];
  5545. $filepath = $result['filepath'];
  5546. }
  5547. if (!is_dir($filepath)) {
  5548. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/';
  5549. $dir = '/';
  5550. }
  5551. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5552. // is already escaped twice when it gets here.
  5553. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5554. if (!empty($title)) {
  5555. $title = api_replace_dangerous_char(stripslashes($title));
  5556. } else {
  5557. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5558. }
  5559. $title = disable_dangerous_file($title);
  5560. $filename = $title;
  5561. $content = !empty($content) ? $content : $_POST['content_lp'];
  5562. $tmp_filename = $filename;
  5563. $i = 0;
  5564. while (file_exists($filepath . $tmp_filename . '.'.$extension))
  5565. $tmp_filename = $filename . '_' . ++ $i;
  5566. $filename = $tmp_filename . '.'.$extension;
  5567. if ($extension == 'html') {
  5568. $content = stripslashes($content);
  5569. $content = str_replace(
  5570. api_get_path(WEB_COURSE_PATH),
  5571. api_get_path(REL_PATH).'courses/',
  5572. $content
  5573. );
  5574. // Change the path of mp3 to absolute.
  5575. // The first regexp deals with :// urls.
  5576. $content = preg_replace(
  5577. "|(flashvars=\"file=)([^:/]+)/|",
  5578. "$1".api_get_path(
  5579. REL_COURSE_PATH
  5580. ).$courseInfo['path'].'/document/',
  5581. $content
  5582. );
  5583. // The second regexp deals with audio/ urls.
  5584. $content = preg_replace(
  5585. "|(flashvars=\"file=)([^/]+)/|",
  5586. "$1".api_get_path(
  5587. REL_COURSE_PATH
  5588. ).$courseInfo['path'].'/document/$2/',
  5589. $content
  5590. );
  5591. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5592. $content = str_replace(
  5593. '</body>',
  5594. '<style type="text/css">body{}</style></body>',
  5595. $content
  5596. );
  5597. }
  5598. if (!file_exists($filepath . $filename)) {
  5599. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5600. fputs($fp, $content);
  5601. fclose($fp);
  5602. $file_size = filesize($filepath . $filename);
  5603. $save_file_path = $dir.$filename;
  5604. $document_id = add_document(
  5605. $courseInfo,
  5606. $save_file_path,
  5607. 'file',
  5608. $file_size,
  5609. $tmp_filename,
  5610. '',
  5611. 0, //readonly
  5612. true,
  5613. null,
  5614. $sessionId,
  5615. $creatorId
  5616. );
  5617. if ($document_id) {
  5618. api_item_property_update(
  5619. $courseInfo,
  5620. TOOL_DOCUMENT,
  5621. $document_id,
  5622. 'DocumentAdded',
  5623. $creatorId,
  5624. null,
  5625. null,
  5626. null,
  5627. null,
  5628. $sessionId
  5629. );
  5630. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5631. $new_title = $originalTitle;
  5632. if ($new_comment || $new_title) {
  5633. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5634. $ct = '';
  5635. if ($new_comment)
  5636. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5637. if ($new_title)
  5638. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5639. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5640. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5641. Database::query($sql);
  5642. }
  5643. }
  5644. return $document_id;
  5645. }
  5646. }
  5647. }
  5648. /**
  5649. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5650. * @param array $_course array
  5651. * @return void
  5652. */
  5653. public function edit_document($_course)
  5654. {
  5655. $course_id = api_get_course_int_id();
  5656. global $_configuration;
  5657. // Please, do not modify this dirname formatting.
  5658. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5659. if (strstr($dir, '..'))
  5660. $dir = '/';
  5661. if ($dir[0] == '.')
  5662. $dir = substr($dir, 1);
  5663. if ($dir[0] != '/')
  5664. $dir = '/' . $dir;
  5665. if ($dir[strlen($dir) - 1] != '/')
  5666. $dir .= '/';
  5667. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5668. if (!is_dir($filepath)) {
  5669. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5670. }
  5671. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5672. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5673. $document_id = intval($_POST['path']);
  5674. $sql = "SELECT path FROM " . $table_doc . "
  5675. WHERE c_id = $course_id AND id = " . $document_id;
  5676. $res = Database::query($sql);
  5677. $row = Database :: fetch_array($res);
  5678. $content = stripslashes($_POST['content_lp']);
  5679. $file = $filepath . $row['path'];
  5680. if ($fp = @ fopen($file, 'w')) {
  5681. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5682. // Change the path of mp3 to absolute.
  5683. // The first regexp deals with :// urls.
  5684. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5685. // The second regexp deals with audio/ urls.
  5686. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5687. fputs($fp, $content);
  5688. fclose($fp);
  5689. $sql = "UPDATE " . $table_doc ." SET
  5690. title='".Database::escape_string($_POST['title'])."'
  5691. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5692. Database::query($sql);
  5693. }
  5694. }
  5695. }
  5696. /**
  5697. * Displays the selected item, with a panel for manipulating the item
  5698. * @param int $item_id
  5699. * @param string $msg
  5700. * @return string
  5701. */
  5702. public function display_item($item_id, $msg = null, $show_actions = true)
  5703. {
  5704. $course_id = api_get_course_int_id();
  5705. $return = '';
  5706. if (is_numeric($item_id)) {
  5707. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5708. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5709. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5710. $result = Database::query($sql);
  5711. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5712. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5713. // Prevents wrong parent selection for document, see Bug#1251.
  5714. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5715. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5716. }
  5717. if ($show_actions) {
  5718. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5719. }
  5720. $return .= '<div style="padding:10px;">';
  5721. if ($msg != '')
  5722. $return .= $msg;
  5723. $return .= '<h3>'.$row['title'].'</h3>';
  5724. switch ($row['item_type']) {
  5725. case TOOL_QUIZ:
  5726. if (!empty($row['path'])) {
  5727. $exercise = new Exercise();
  5728. $exercise->read($row['path']);
  5729. $return .= $exercise->description.'<br />';
  5730. }
  5731. break;
  5732. case TOOL_DOCUMENT:
  5733. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5734. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5735. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5736. $result = Database::query($sql_doc);
  5737. $path_file = Database::result($result, 0, 0);
  5738. $path_parts = pathinfo($path_file);
  5739. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5740. if (in_array($path_parts['extension'], array(
  5741. 'html',
  5742. 'txt',
  5743. 'png',
  5744. 'jpg',
  5745. 'JPG',
  5746. 'jpeg',
  5747. 'JPEG',
  5748. 'gif',
  5749. 'swf'
  5750. ))) {
  5751. $return .= $this->display_document($row['path'], true, true);
  5752. }
  5753. break;
  5754. }
  5755. $return .= '</div>';
  5756. }
  5757. }
  5758. return $return;
  5759. }
  5760. /**
  5761. * Shows the needed forms for editing a specific item
  5762. * @param int $item_id
  5763. * @return string
  5764. */
  5765. public function display_edit_item($item_id)
  5766. {
  5767. $course_id = api_get_course_int_id();
  5768. $return = '';
  5769. if (is_numeric($item_id)) {
  5770. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5771. $sql = "SELECT * FROM $tbl_lp_item
  5772. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5773. $res = Database::query($sql);
  5774. $row = Database::fetch_array($res);
  5775. switch ($row['item_type']) {
  5776. case 'dokeos_chapter':
  5777. case 'dir':
  5778. case 'asset':
  5779. case 'sco':
  5780. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5781. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5782. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5783. } else {
  5784. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5785. }
  5786. break;
  5787. case TOOL_DOCUMENT:
  5788. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5789. $sql = "SELECT lp.*, doc.path as dir
  5790. FROM " . $tbl_lp_item . " as lp
  5791. LEFT JOIN " . $tbl_doc . " as doc
  5792. ON doc.id = lp.path
  5793. WHERE
  5794. lp.c_id = $course_id AND
  5795. doc.c_id = $course_id AND
  5796. lp.id = " . intval($item_id);
  5797. $res_step = Database::query($sql);
  5798. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5799. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5800. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5801. break;
  5802. case TOOL_LINK:
  5803. $link_id = (string) $row['path'];
  5804. if (ctype_digit($link_id)) {
  5805. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5806. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5807. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5808. $res_link = Database::query($sql_select);
  5809. $row_link = Database :: fetch_array($res_link);
  5810. if (is_array($row_link)) {
  5811. $row['url'] = $row_link['url'];
  5812. }
  5813. }
  5814. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5815. $return .= $this->display_link_form('edit', $item_id, $row);
  5816. break;
  5817. case TOOL_LP_FINAL_ITEM:
  5818. $_SESSION['finalItem'] = true;
  5819. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5820. $sql = "SELECT lp.*, doc.path as dir
  5821. FROM " . $tbl_lp_item . " as lp
  5822. LEFT JOIN " . $tbl_doc . " as doc
  5823. ON doc.id = lp.path
  5824. WHERE
  5825. lp.c_id = $course_id AND
  5826. doc.c_id = $course_id AND
  5827. lp.id = " . intval($item_id);
  5828. $res_step = Database::query($sql);
  5829. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5830. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5831. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5832. break;
  5833. case 'dokeos_module':
  5834. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5835. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5836. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5837. } else {
  5838. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5839. }
  5840. break;
  5841. case TOOL_QUIZ:
  5842. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5843. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5844. break;
  5845. case TOOL_HOTPOTATOES:
  5846. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5847. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5848. break;
  5849. case TOOL_STUDENTPUBLICATION:
  5850. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5851. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5852. break;
  5853. case TOOL_FORUM:
  5854. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5855. $return .= $this->display_forum_form('edit', $item_id, $row);
  5856. break;
  5857. case TOOL_THREAD:
  5858. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5859. $return .= $this->display_thread_form('edit', $item_id, $row);
  5860. break;
  5861. }
  5862. }
  5863. return $return;
  5864. }
  5865. /**
  5866. * Function that displays a list with al the resources that
  5867. * could be added to the learning path
  5868. * @return string
  5869. */
  5870. public function display_resources()
  5871. {
  5872. $course_code = api_get_course_id();
  5873. // Get all the docs.
  5874. $documents = $this->get_documents(true);
  5875. // Get all the exercises.
  5876. $exercises = $this->get_exercises();
  5877. // Get all the links.
  5878. $links = $this->get_links();
  5879. // Get all the student publications.
  5880. $works = $this->get_student_publications();
  5881. // Get all the forums.
  5882. $forums = $this->get_forums(null, $course_code);
  5883. // Get the final item form (see BT#11048) .
  5884. $finish = $this->getFinalItemForm();
  5885. $headers = array(
  5886. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5887. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5888. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5889. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5890. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5891. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  5892. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  5893. );
  5894. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5895. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5896. echo Display::tabs(
  5897. $headers,
  5898. array($documents, $exercises, $links, $works, $forums, $chapter, $finish), 'resource_tab'
  5899. );
  5900. return true;
  5901. }
  5902. /**
  5903. * Returns the extension of a document
  5904. * @param string filename
  5905. * @return string Extension (part after the last dot)
  5906. */
  5907. public function get_extension($filename)
  5908. {
  5909. $explode = explode('.', $filename);
  5910. return $explode[count($explode) - 1];
  5911. }
  5912. /**
  5913. * Displays a document by id
  5914. *
  5915. * @param int $id
  5916. * @return string
  5917. */
  5918. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5919. {
  5920. $_course = api_get_course_info();
  5921. $course_id = api_get_course_int_id();
  5922. $return = '';
  5923. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5924. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5925. WHERE c_id = ".$course_id." AND id = " . $id;
  5926. $res_doc = Database::query($sql_doc);
  5927. $row_doc = Database :: fetch_array($res_doc);
  5928. // TODO: Add a path filter.
  5929. if ($iframe) {
  5930. $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>';
  5931. } else {
  5932. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5933. }
  5934. return $return;
  5935. }
  5936. /**
  5937. * Return HTML form to add/edit a quiz
  5938. * @param string Action (add/edit)
  5939. * @param integer Item ID if already exists
  5940. * @param mixed Extra information (quiz ID if integer)
  5941. * @return string HTML form
  5942. */
  5943. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5944. {
  5945. $course_id = api_get_course_int_id();
  5946. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5947. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5948. if ($id != 0 && is_array($extra_info)) {
  5949. $item_title = $extra_info['title'];
  5950. $item_description = $extra_info['description'];
  5951. } elseif (is_numeric($extra_info)) {
  5952. $sql = "SELECT title, description
  5953. FROM " . $tbl_quiz . "
  5954. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5955. $result = Database::query($sql);
  5956. $row = Database::fetch_array($result);
  5957. $item_title = $row['title'];
  5958. $item_description = $row['description'];
  5959. } else {
  5960. $item_title = '';
  5961. $item_description = '';
  5962. }
  5963. $item_title = Security::remove_XSS($item_title);
  5964. $item_description = Security::remove_XSS($item_description);
  5965. if ($id != 0 && is_array($extra_info))
  5966. $parent = $extra_info['parent_item_id'];
  5967. else
  5968. $parent = 0;
  5969. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5970. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5971. $result = Database::query($sql);
  5972. $arrLP = array ();
  5973. while ($row = Database :: fetch_array($result)) {
  5974. $arrLP[] = array (
  5975. 'id' => $row['id'],
  5976. 'item_type' => $row['item_type'],
  5977. 'title' => $row['title'],
  5978. 'path' => $row['path'],
  5979. 'description' => $row['description'],
  5980. 'parent_item_id' => $row['parent_item_id'],
  5981. 'previous_item_id' => $row['previous_item_id'],
  5982. 'next_item_id' => $row['next_item_id'],
  5983. 'display_order' => $row['display_order'],
  5984. 'max_score' => $row['max_score'],
  5985. 'min_score' => $row['min_score'],
  5986. 'mastery_score' => $row['mastery_score'],
  5987. 'prerequisite' => $row['prerequisite'],
  5988. 'max_time_allowed' => $row['max_time_allowed']
  5989. );
  5990. }
  5991. $this->tree_array($arrLP);
  5992. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5993. unset ($this->arrMenu);
  5994. $form = new FormValidator('quiz_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  5995. $defaults = [];
  5996. if ($action == 'add') {
  5997. $legend = get_lang('CreateTheExercise');
  5998. } elseif ($action == 'move') {
  5999. $legend = get_lang('MoveTheCurrentExercise');
  6000. } else {
  6001. $legend = get_lang('EditCurrentExecice');
  6002. }
  6003. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6004. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6005. }
  6006. $form->addHeader($legend);
  6007. if ($action != 'move') {
  6008. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6009. $defaults['title'] = $item_title;
  6010. }
  6011. // Select for Parent item, root or chapter
  6012. $selectParent = $form->addSelect(
  6013. 'parent',
  6014. get_lang('Parent'),
  6015. [],
  6016. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6017. );
  6018. $selectParent->addOption($this->name, 0);
  6019. $arrHide = array (
  6020. $id
  6021. );
  6022. for ($i = 0; $i < count($arrLP); $i++) {
  6023. if ($action != 'add') {
  6024. if (
  6025. (
  6026. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6027. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6028. $arrLP[$i]['item_type'] == 'dir'
  6029. ) &&
  6030. !in_array($arrLP[$i]['id'], $arrHide) &&
  6031. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6032. ) {
  6033. $selectParent->addOption(
  6034. $arrLP[$i]['title'],
  6035. $arrLP[$i]['id'],
  6036. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6037. );
  6038. if ($parent == $arrLP[$i]['id']) {
  6039. $selectParent->setSelected($arrLP[$i]['id']);
  6040. }
  6041. } else {
  6042. $arrHide[] = $arrLP[$i]['id'];
  6043. }
  6044. } else {
  6045. if (
  6046. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6047. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6048. $arrLP[$i]['item_type'] == 'dir'
  6049. ) {
  6050. $selectParent->addOption(
  6051. $arrLP[$i]['title'],
  6052. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6053. );
  6054. if ($parent == $arrLP[$i]['id']) {
  6055. $selectParent->setSelected($arrLP[$i]['id']);
  6056. }
  6057. }
  6058. }
  6059. }
  6060. if (is_array($arrLP)) {
  6061. reset($arrLP);
  6062. }
  6063. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6064. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6065. for ($i = 0; $i < count($arrLP); $i++) {
  6066. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6067. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6068. if (is_array($extra_info)) {
  6069. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6070. $selectPrevious->setSelected($arrLP[$i]['id']);
  6071. }
  6072. } elseif ($action == 'add') {
  6073. $selectPrevious->setSelected($arrLP[$i]['id']);
  6074. }
  6075. }
  6076. }
  6077. if ($action != 'move') {
  6078. $id_prerequisite = 0;
  6079. if (is_array($arrLP)) {
  6080. foreach ($arrLP as $key => $value) {
  6081. if ($value['id'] == $id) {
  6082. $id_prerequisite = $value['prerequisite'];
  6083. break;
  6084. }
  6085. }
  6086. }
  6087. $arrHide = array ();
  6088. for ($i = 0; $i < count($arrLP); $i++) {
  6089. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6090. if (is_array($extra_info)) {
  6091. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6092. $s_selected_position = $arrLP[$i]['id'];
  6093. }
  6094. } elseif ($action == 'add') {
  6095. $s_selected_position = 0;
  6096. }
  6097. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6098. }
  6099. }
  6100. /*// Commented the prerequisites, only visible in edit (exercise).
  6101. $return .= '<tr>';
  6102. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6103. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6104. foreach($arrHide as $key => $value){
  6105. if($key==$s_selected_position && $action == 'add'){
  6106. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6107. }
  6108. elseif($key==$id_prerequisite && $action == 'edit'){
  6109. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6110. }
  6111. else{
  6112. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6113. }
  6114. }
  6115. $return .= "</select></td>";
  6116. */
  6117. /*$return .= '<tr>';
  6118. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6119. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6120. // Remove temporarily the test description.
  6121. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6122. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6123. $return .= '</tr>'; */
  6124. }
  6125. if ($action == 'add') {
  6126. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6127. } else {
  6128. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6129. }
  6130. if ($action == 'move') {
  6131. $form->addHidden('title', $item_title);
  6132. $form->addHidden('description', $item_description);
  6133. }
  6134. if (is_numeric($extra_info)) {
  6135. $form->addHidden('path', $extra_info);
  6136. } elseif (is_array($extra_info)) {
  6137. $form->addHidden('path', $extra_info['path']);
  6138. }
  6139. $form->addHidden('type', TOOL_QUIZ);
  6140. $form->addHidden('post_time', time());
  6141. $form->setDefaults($defaults);
  6142. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6143. }
  6144. /**
  6145. * Addition of Hotpotatoes tests
  6146. * @param string Action
  6147. * @param integer Internal ID of the item
  6148. * @param mixed Extra information - can be an array with title and description indexes
  6149. * @return string HTML structure to display the hotpotatoes addition formular
  6150. */
  6151. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6152. {
  6153. $course_id = api_get_course_int_id();
  6154. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6155. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6156. if ($id != 0 && is_array($extra_info)) {
  6157. $item_title = stripslashes($extra_info['title']);
  6158. $item_description = stripslashes($extra_info['description']);
  6159. } elseif (is_numeric($extra_info)) {
  6160. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6161. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6162. WHERE
  6163. c_id = ".$course_id." AND
  6164. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6165. id = " . (int) $extra_info . "
  6166. ORDER BY id ASC";
  6167. $res_hot = Database::query($sql);
  6168. $row = Database::fetch_array($res_hot);
  6169. $item_title = $row['title'];
  6170. $item_description = $row['description'];
  6171. if (!empty ($row['comment'])) {
  6172. $item_title = $row['comment'];
  6173. }
  6174. } else {
  6175. $item_title = '';
  6176. $item_description = '';
  6177. }
  6178. if ($id != 0 && is_array($extra_info)) {
  6179. $parent = $extra_info['parent_item_id'];
  6180. } else {
  6181. $parent = 0;
  6182. }
  6183. $sql = "SELECT * FROM $tbl_lp_item
  6184. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6185. $result = Database::query($sql);
  6186. $arrLP = array ();
  6187. while ($row = Database :: fetch_array($result)) {
  6188. $arrLP[] = array (
  6189. 'id' => $row['id'],
  6190. 'item_type' => $row['item_type'],
  6191. 'title' => $row['title'],
  6192. 'path' => $row['path'],
  6193. 'description' => $row['description'],
  6194. 'parent_item_id' => $row['parent_item_id'],
  6195. 'previous_item_id' => $row['previous_item_id'],
  6196. 'next_item_id' => $row['next_item_id'],
  6197. 'display_order' => $row['display_order'],
  6198. 'max_score' => $row['max_score'],
  6199. 'min_score' => $row['min_score'],
  6200. 'mastery_score' => $row['mastery_score'],
  6201. 'prerequisite' => $row['prerequisite'],
  6202. 'max_time_allowed' => $row['max_time_allowed']
  6203. );
  6204. }
  6205. $legend = '<legend>';
  6206. if ($action == 'add')
  6207. $legend .= get_lang('CreateTheExercise');
  6208. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6209. else
  6210. $legend .= get_lang('EditCurrentExecice');
  6211. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6212. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6213. }
  6214. $legend .= '</legend>';
  6215. $return = '<form method="POST">';
  6216. $return .= $legend;
  6217. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6218. $return .= '<tr>';
  6219. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6220. $return .= '<td class="input">';
  6221. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6222. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6223. $arrHide = array (
  6224. $id
  6225. );
  6226. if (count($arrLP) > 0) {
  6227. for ($i = 0; $i < count($arrLP); $i++) {
  6228. if ($action != 'add') {
  6229. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6230. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6231. } else {
  6232. $arrHide[] = $arrLP[$i]['id'];
  6233. }
  6234. } else {
  6235. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6236. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6237. }
  6238. }
  6239. reset($arrLP);
  6240. }
  6241. $return .= '</select>';
  6242. $return .= '</td>';
  6243. $return .= '</tr>';
  6244. $return .= '<tr>';
  6245. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6246. $return .= '<td class="input">';
  6247. $return .= '<select id="previous" name="previous" size="1">';
  6248. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6249. for ($i = 0; $i < count($arrLP); $i++) {
  6250. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6251. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6252. $selected = 'selected="selected" ';
  6253. elseif ($action == 'add') $selected = 'selected="selected" ';
  6254. else
  6255. $selected = '';
  6256. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6257. }
  6258. }
  6259. $return .= '</select>';
  6260. $return .= '</td>';
  6261. $return .= '</tr>';
  6262. if ($action != 'move') {
  6263. $return .= '<tr>';
  6264. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6265. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6266. $return .= '</tr>';
  6267. $id_prerequisite = 0;
  6268. if (is_array($arrLP) && count($arrLP) > 0) {
  6269. foreach ($arrLP as $key => $value) {
  6270. if ($value['id'] == $id) {
  6271. $id_prerequisite = $value['prerequisite'];
  6272. break;
  6273. }
  6274. }
  6275. $arrHide = array ();
  6276. for ($i = 0; $i < count($arrLP); $i++) {
  6277. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6278. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6279. $s_selected_position = $arrLP[$i]['id'];
  6280. elseif ($action == 'add') $s_selected_position = 0;
  6281. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6282. }
  6283. }
  6284. }
  6285. }
  6286. $return .= '<tr>';
  6287. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6288. $return .= '</tr>';
  6289. $return .= '</table>';
  6290. if ($action == 'move') {
  6291. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6292. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6293. }
  6294. if (is_numeric($extra_info)) {
  6295. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6296. } elseif (is_array($extra_info)) {
  6297. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6298. }
  6299. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6300. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6301. $return .= '</form>';
  6302. return $return;
  6303. }
  6304. /**
  6305. * Return the form to display the forum edit/add option
  6306. * @param string Action (add/edit)
  6307. * @param integer ID of the lp_item if already exists
  6308. * @param mixed Forum ID or title
  6309. * @return string HTML form
  6310. */
  6311. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6312. {
  6313. $course_id = api_get_course_int_id();
  6314. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6315. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6316. if ($id != 0 && is_array($extra_info)) {
  6317. $item_title = stripslashes($extra_info['title']);
  6318. } elseif (is_numeric($extra_info)) {
  6319. $sql = "SELECT forum_title as title, forum_comment as comment
  6320. FROM " . $tbl_forum . "
  6321. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6322. $result = Database::query($sql);
  6323. $row = Database :: fetch_array($result);
  6324. $item_title = $row['title'];
  6325. $item_description = $row['comment'];
  6326. } else {
  6327. $item_title = '';
  6328. $item_description = '';
  6329. }
  6330. if ($id != 0 && is_array($extra_info)) {
  6331. $parent = $extra_info['parent_item_id'];
  6332. } else {
  6333. $parent = 0;
  6334. }
  6335. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6336. WHERE
  6337. c_id = ".$course_id." AND
  6338. lp_id = " . $this->lp_id;
  6339. $result = Database::query($sql);
  6340. $arrLP = array();
  6341. while ($row = Database :: fetch_array($result)) {
  6342. $arrLP[] = array (
  6343. 'id' => $row['id'],
  6344. 'item_type' => $row['item_type'],
  6345. 'title' => $row['title'],
  6346. 'path' => $row['path'],
  6347. 'description' => $row['description'],
  6348. 'parent_item_id' => $row['parent_item_id'],
  6349. 'previous_item_id' => $row['previous_item_id'],
  6350. 'next_item_id' => $row['next_item_id'],
  6351. 'display_order' => $row['display_order'],
  6352. 'max_score' => $row['max_score'],
  6353. 'min_score' => $row['min_score'],
  6354. 'mastery_score' => $row['mastery_score'],
  6355. 'prerequisite' => $row['prerequisite']
  6356. );
  6357. }
  6358. $this->tree_array($arrLP);
  6359. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6360. unset($this->arrMenu);
  6361. if ($action == 'add') {
  6362. $legend = get_lang('CreateTheForum');
  6363. } elseif ($action == 'move') {
  6364. $legend = get_lang('MoveTheCurrentForum');
  6365. } else {
  6366. $legend = get_lang('EditCurrentForum');
  6367. }
  6368. $form = new FormValidator('forum_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6369. $defaults = [];
  6370. $form->addHeader($legend);
  6371. if ($action != 'move') {
  6372. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  6373. $defaults['title'] = $item_title;
  6374. }
  6375. $selectParent = $form->addSelect(
  6376. 'parent',
  6377. get_lang('Parent'),
  6378. [],
  6379. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6380. );
  6381. $selectParent->addOption($this->name, 0);
  6382. $arrHide = array(
  6383. $id
  6384. );
  6385. //$parent_item_id = $_SESSION['parent_item_id'];
  6386. for ($i = 0; $i < count($arrLP); $i++) {
  6387. if ($action != 'add') {
  6388. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6389. $selectParent->addOption(
  6390. $arrLP[$i]['title'],
  6391. $arrLP[$i]['id'],
  6392. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6393. );
  6394. if ($parent == $arrLP[$i]['id']) {
  6395. $selectParent->setSelected($arrLP[$i]['id']);
  6396. }
  6397. } else {
  6398. $arrHide[] = $arrLP[$i]['id'];
  6399. }
  6400. } else {
  6401. if (
  6402. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6403. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6404. $arrLP[$i]['item_type'] == 'dir'
  6405. ) {
  6406. $selectParent->addOption(
  6407. $arrLP[$i]['title'],
  6408. $arrLP[$i]['id'],
  6409. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6410. );
  6411. if ($parent == $arrLP[$i]['id']) {
  6412. $selectParent->setSelected($arrLP[$i]['id']);
  6413. }
  6414. }
  6415. }
  6416. }
  6417. if (is_array($arrLP)) {
  6418. reset($arrLP);
  6419. }
  6420. $selectPrevious = $form->addSelect(
  6421. 'previous',
  6422. get_lang('Position'),
  6423. [],
  6424. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6425. );
  6426. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6427. for ($i = 0; $i < count($arrLP); $i++) {
  6428. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6429. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6430. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6431. $selectPrevious->setSelected($arrLP[$i]['id']);
  6432. } elseif ($action == 'add') {
  6433. $selectPrevious->setSelected($arrLP[$i]['id']);
  6434. }
  6435. }
  6436. }
  6437. if ($action != 'move') {
  6438. $id_prerequisite = 0;
  6439. if (is_array($arrLP)) {
  6440. foreach ($arrLP as $key => $value) {
  6441. if ($value['id'] == $id) {
  6442. $id_prerequisite = $value['prerequisite'];
  6443. break;
  6444. }
  6445. }
  6446. }
  6447. $arrHide = array();
  6448. for ($i = 0; $i < count($arrLP); $i++) {
  6449. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6450. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6451. $s_selected_position = $arrLP[$i]['id'];
  6452. elseif ($action == 'add') $s_selected_position = 0;
  6453. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6454. }
  6455. }
  6456. }
  6457. if ($action == 'add') {
  6458. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6459. } else {
  6460. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6461. }
  6462. if ($action == 'move') {
  6463. $form->addHidden('title', $item_title);
  6464. $form->addHidden('description', $item_description);
  6465. }
  6466. if (is_numeric($extra_info)) {
  6467. $form->addHidden('path', $extra_info);
  6468. } elseif (is_array($extra_info)) {
  6469. $form->addHidden('path', $extra_info['path']);
  6470. }
  6471. $form->addHidden('type', TOOL_FORUM);
  6472. $form->addHidden('post_time', time());
  6473. $form->setDefaults($defaults);
  6474. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6475. }
  6476. /**
  6477. * Return HTML form to add/edit forum threads
  6478. * @param string Action (add/edit)
  6479. * @param integer Item ID if already exists in learning path
  6480. * @param mixed Extra information (thread ID if integer)
  6481. * @return string HTML form
  6482. */
  6483. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6484. {
  6485. $course_id = api_get_course_int_id();
  6486. if (empty($course_id)) {
  6487. return null;
  6488. }
  6489. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6490. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6491. if ($id != 0 && is_array($extra_info)) {
  6492. $item_title = stripslashes($extra_info['title']);
  6493. } elseif (is_numeric($extra_info)) {
  6494. $sql = "SELECT thread_title as title FROM $tbl_forum
  6495. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6496. $result = Database::query($sql);
  6497. $row = Database :: fetch_array($result);
  6498. $item_title = $row['title'];
  6499. $item_description = '';
  6500. } else {
  6501. $item_title = '';
  6502. $item_description = '';
  6503. }
  6504. if ($id != 0 && is_array($extra_info)) {
  6505. $parent = $extra_info['parent_item_id'];
  6506. } else {
  6507. $parent = 0;
  6508. }
  6509. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6510. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6511. $result = Database::query($sql);
  6512. $arrLP = array ();
  6513. while ($row = Database :: fetch_array($result)) {
  6514. $arrLP[] = array (
  6515. 'id' => $row['id'],
  6516. 'item_type' => $row['item_type'],
  6517. 'title' => $row['title'],
  6518. 'path' => $row['path'],
  6519. 'description' => $row['description'],
  6520. 'parent_item_id' => $row['parent_item_id'],
  6521. 'previous_item_id' => $row['previous_item_id'],
  6522. 'next_item_id' => $row['next_item_id'],
  6523. 'display_order' => $row['display_order'],
  6524. 'max_score' => $row['max_score'],
  6525. 'min_score' => $row['min_score'],
  6526. 'mastery_score' => $row['mastery_score'],
  6527. 'prerequisite' => $row['prerequisite']
  6528. );
  6529. }
  6530. $this->tree_array($arrLP);
  6531. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6532. unset ($this->arrMenu);
  6533. $form = new FormValidator('thread_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6534. $defaults = [];
  6535. if ($action == 'add') {
  6536. $legend = get_lang('CreateTheForum');
  6537. } elseif ($action == 'move') {
  6538. $legend = get_lang('MoveTheCurrentForum');
  6539. } else {
  6540. $legend = get_lang('EditCurrentForum');
  6541. }
  6542. $form->addHeader($legend);
  6543. $selectParent = $form->addSelect(
  6544. 'parent',
  6545. get_lang('Parent'),
  6546. [],
  6547. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6548. );
  6549. $selectParent->addOption($this->name, 0);
  6550. $arrHide = array (
  6551. $id
  6552. );
  6553. for ($i = 0; $i < count($arrLP); $i++) {
  6554. if ($action != 'add') {
  6555. if (
  6556. (
  6557. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6558. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6559. $arrLP[$i]['item_type'] == 'dir'
  6560. ) &&
  6561. !in_array($arrLP[$i]['id'], $arrHide) &&
  6562. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6563. ) {
  6564. $selectParent->addOption(
  6565. $arrLP[$i]['title'],
  6566. $arrLP[$i]['id'],
  6567. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6568. );
  6569. if ($parent == $arrLP[$i]['id']) {
  6570. $selectParent->setSelected($arrLP[$i]['id']);
  6571. }
  6572. } else {
  6573. $arrHide[] = $arrLP[$i]['id'];
  6574. }
  6575. } else {
  6576. if (
  6577. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6578. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6579. $arrLP[$i]['item_type'] == 'dir'
  6580. ) {
  6581. $selectParent->addOption(
  6582. $arrLP[$i]['title'],
  6583. $arrLP[$i]['id'],
  6584. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6585. );
  6586. if ($parent == $arrLP[$i]['id']) {
  6587. $selectParent->setSelected($arrLP[$i]['id']);
  6588. }
  6589. }
  6590. }
  6591. }
  6592. if ($arrLP != null) {
  6593. reset($arrLP);
  6594. }
  6595. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6596. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6597. for ($i = 0; $i < count($arrLP); $i++) {
  6598. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6599. $selectPrevious->addOption(
  6600. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6601. $arrLP[$i]['id']
  6602. );
  6603. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6604. $selectPrevious->setSelected($arrLP[$i]['id']);
  6605. } elseif ($action == 'add') {
  6606. $selectPrevious->setSelected($arrLP[$i]['id']);
  6607. }
  6608. }
  6609. }
  6610. if ($action != 'move') {
  6611. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6612. $defaults['title'] = $item_title;
  6613. $id_prerequisite = 0;
  6614. if ($arrLP != null) {
  6615. foreach ($arrLP as $key => $value) {
  6616. if ($value['id'] == $id) {
  6617. $id_prerequisite = $value['prerequisite'];
  6618. break;
  6619. }
  6620. }
  6621. }
  6622. $arrHide = array();
  6623. $s_selected_position = 0;
  6624. for ($i = 0; $i < count($arrLP); $i++) {
  6625. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6626. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6627. $s_selected_position = $arrLP[$i]['id'];
  6628. elseif ($action == 'add') $s_selected_position = 0;
  6629. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6630. }
  6631. }
  6632. $selectPrerequisites = $form->addSelect(
  6633. 'prerequisites',
  6634. get_lang('LearnpathPrerequisites'),
  6635. [],
  6636. ['id' => 'prerequisites']
  6637. );
  6638. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6639. foreach ($arrHide as $key => $value) {
  6640. $selectPrerequisites->addOption($value['value'], $key);
  6641. if ($key == $s_selected_position && $action == 'add') {
  6642. $selectPrerequisites->setSelected($key);
  6643. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6644. $selectPrerequisites->setSelected($key);
  6645. }
  6646. }
  6647. }
  6648. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6649. if ($action == 'move') {
  6650. $form->addHidden('title', $item_title);
  6651. $form->addHidden('description', $item_description);
  6652. }
  6653. if (is_numeric($extra_info)) {
  6654. $form->addHidden('path', $extra_info);
  6655. }
  6656. elseif (is_array($extra_info)) {
  6657. $form->addHidden('path', $extra_info['path']);
  6658. }
  6659. $form->addHidden('type', TOOL_THREAD);
  6660. $form->addHidden('post_time', time());
  6661. $form->setDefaults($defaults);
  6662. return $form->returnForm();
  6663. }
  6664. /**
  6665. * Return the HTML form to display an item (generally a section/module item)
  6666. * @param string Item type (module/dokeos_module)
  6667. * @param string Title (optional, only when creating)
  6668. * @param string Action ('add'/'edit')
  6669. * @param integer lp_item ID
  6670. * @param mixed Extra info
  6671. * @return string HTML form
  6672. */
  6673. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6674. {
  6675. $course_id = api_get_course_int_id();
  6676. $_course = api_get_course_info();
  6677. global $charset;
  6678. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6679. if ($id != 0 && is_array($extra_info)) {
  6680. $item_title = $extra_info['title'];
  6681. $item_description = $extra_info['description'];
  6682. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6683. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6684. } else {
  6685. $item_title = '';
  6686. $item_description = '';
  6687. $item_path_fck = '';
  6688. }
  6689. if ($id != 0 && is_array($extra_info)) {
  6690. $parent = $extra_info['parent_item_id'];
  6691. } else {
  6692. $parent = 0;
  6693. }
  6694. $id = intval($id);
  6695. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6696. WHERE
  6697. c_id = ".$course_id." AND
  6698. lp_id = " . $this->lp_id . " AND
  6699. id != $id";
  6700. if ($item_type == 'module')
  6701. $sql .= " AND parent_item_id = 0";
  6702. $result = Database::query($sql);
  6703. $arrLP = array ();
  6704. while ($row = Database :: fetch_array($result)) {
  6705. $arrLP[] = array(
  6706. 'id' => $row['id'],
  6707. 'item_type' => $row['item_type'],
  6708. 'title' => $row['title'],
  6709. 'path' => $row['path'],
  6710. 'description' => $row['description'],
  6711. 'parent_item_id' => $row['parent_item_id'],
  6712. 'previous_item_id' => $row['previous_item_id'],
  6713. 'next_item_id' => $row['next_item_id'],
  6714. 'max_score' => $row['max_score'],
  6715. 'min_score' => $row['min_score'],
  6716. 'mastery_score' => $row['mastery_score'],
  6717. 'prerequisite' => $row['prerequisite'],
  6718. 'display_order' => $row['display_order']
  6719. );
  6720. }
  6721. $this->tree_array($arrLP);
  6722. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6723. unset ($this->arrMenu);
  6724. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6725. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6726. $form = new FormValidator('form', 'POST', $url);
  6727. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6728. $defaults['description'] = $item_description;
  6729. $form->addElement('header', $title);
  6730. //$arrHide = array($id);
  6731. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6732. $arrHide[0]['padding'] = 20;
  6733. $charset = api_get_system_encoding();
  6734. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6735. for ($i = 0; $i < count($arrLP); $i++) {
  6736. if ($action != 'add') {
  6737. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6738. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6739. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6740. if ($parent == $arrLP[$i]['id']) {
  6741. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6742. }
  6743. }
  6744. } else {
  6745. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6746. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6747. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6748. if ($parent == $arrLP[$i]['id']) {
  6749. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6750. }
  6751. }
  6752. }
  6753. }
  6754. if ($action != 'move') {
  6755. $form->addElement('text', 'title', get_lang('Title'));
  6756. $form->applyFilter('title', 'html_filter');
  6757. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6758. } else {
  6759. $form->addElement('hidden', 'title');
  6760. }
  6761. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6762. foreach ($arrHide as $key => $value) {
  6763. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6764. }
  6765. if (!empty($s_selected_parent)) {
  6766. $parent_select->setSelected($s_selected_parent);
  6767. }
  6768. }
  6769. if (is_array($arrLP)) {
  6770. reset($arrLP);
  6771. }
  6772. $arrHide = array();
  6773. // POSITION
  6774. for ($i = 0; $i < count($arrLP); $i++) {
  6775. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6776. //this is the same!
  6777. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6778. $s_selected_position = $arrLP[$i]['id'];
  6779. } elseif ($action == 'add') {
  6780. $s_selected_position = $arrLP[$i]['id'];
  6781. }
  6782. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6783. }
  6784. }
  6785. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6786. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6787. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6788. foreach ($arrHide as $key => $value) {
  6789. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6790. }
  6791. if (!empty ($s_selected_position)) {
  6792. $position->setSelected($s_selected_position);
  6793. }
  6794. if (is_array($arrLP)) {
  6795. reset($arrLP);
  6796. }
  6797. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6798. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6799. $form->addElement('hidden', 'parent', '0');
  6800. }
  6801. //fix in order to use the tab
  6802. if ($item_type == 'chapter') {
  6803. $form->addElement('hidden', 'type', 'chapter');
  6804. }
  6805. $extension = null;
  6806. if (!empty($item_path)) {
  6807. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6808. }
  6809. //assets can't be modified
  6810. //$item_type == 'asset' ||
  6811. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6812. if ($item_type == 'sco') {
  6813. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6814. }
  6815. $renderer = $form->defaultRenderer();
  6816. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6817. $relative_prefix = '';
  6818. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6819. 'Width' => '100%',
  6820. 'Height' => '500',
  6821. 'FullPage' => true,
  6822. 'CreateDocumentDir' => $relative_prefix,
  6823. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6824. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6825. );
  6826. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6827. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6828. //$defaults['content_lp'] = file_get_contents($item_path);
  6829. $defaults['content_lp'] = file_get_contents($content_path);
  6830. }
  6831. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6832. $form->addElement('hidden', 'post_time', time());
  6833. $form->setDefaults($defaults);
  6834. return $form->return_form();
  6835. }
  6836. /**
  6837. * Returns the form to update or create a document
  6838. * @param string Action (add/edit)
  6839. * @param integer ID of the lp_item (if already exists)
  6840. * @param mixed Integer if document ID, string if info ('new')
  6841. * @return string HTML form
  6842. */
  6843. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6844. {
  6845. $course_id = api_get_course_int_id();
  6846. $_course = api_get_course_info();
  6847. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6848. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6849. $no_display_edit_textarea = false;
  6850. $item_description = '';
  6851. //If action==edit document
  6852. //We don't display the document form if it's not an editable document (html or txt file)
  6853. if ($action == "edit") {
  6854. if (is_array($extra_info)) {
  6855. $path_parts = pathinfo($extra_info['dir']);
  6856. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6857. $no_display_edit_textarea = true;
  6858. }
  6859. }
  6860. }
  6861. $no_display_add = false;
  6862. // If action==add an existing document
  6863. // We don't display the document form if it's not an editable document (html or txt file).
  6864. if ($action == "add") {
  6865. if (is_numeric($extra_info)) {
  6866. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6867. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6868. $result = Database::query($sql_doc);
  6869. $path_file = Database :: result($result, 0, 0);
  6870. $path_parts = pathinfo($path_file);
  6871. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6872. $no_display_add = true;
  6873. }
  6874. }
  6875. }
  6876. if ($id != 0 && is_array($extra_info)) {
  6877. $item_title = stripslashes($extra_info['title']);
  6878. $item_description = stripslashes($extra_info['description']);
  6879. $item_terms = stripslashes($extra_info['terms']);
  6880. if (empty ($item_title)) {
  6881. $path_parts = pathinfo($extra_info['path']);
  6882. $item_title = stripslashes($path_parts['filename']);
  6883. }
  6884. } elseif (is_numeric($extra_info)) {
  6885. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6886. WHERE
  6887. c_id = ".$course_id." AND
  6888. id = " . intval($extra_info);
  6889. $result = Database::query($sql_doc);
  6890. $row = Database::fetch_array($result);
  6891. $item_title = $row['title'];
  6892. $item_title = str_replace('_', ' ', $item_title);
  6893. if (empty ($item_title)) {
  6894. $path_parts = pathinfo($row['path']);
  6895. $item_title = stripslashes($path_parts['filename']);
  6896. }
  6897. } else {
  6898. $item_title = '';
  6899. $item_description = '';
  6900. }
  6901. $return = '<legend>';
  6902. if ($id != 0 && is_array($extra_info)) {
  6903. $parent = $extra_info['parent_item_id'];
  6904. } else {
  6905. $parent = 0;
  6906. }
  6907. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6908. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6909. $result = Database::query($sql);
  6910. $arrLP = array ();
  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. 'display_order' => $row['display_order'],
  6922. 'max_score' => $row['max_score'],
  6923. 'min_score' => $row['min_score'],
  6924. 'mastery_score' => $row['mastery_score'],
  6925. 'prerequisite' => $row['prerequisite']
  6926. );
  6927. }
  6928. $this->tree_array($arrLP);
  6929. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6930. unset ($this->arrMenu);
  6931. if ($action == 'add') {
  6932. $return .= get_lang('CreateTheDocument');
  6933. } elseif ($action == 'move') {
  6934. $return .= get_lang('MoveTheCurrentDocument');
  6935. } else {
  6936. $return .= get_lang('EditTheCurrentDocument');
  6937. }
  6938. $return .= '</legend>';
  6939. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6940. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6941. }
  6942. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6943. $defaults['title'] = Security :: remove_XSS($item_title);
  6944. if (empty($item_title)) {
  6945. $defaults['title'] = Security::remove_XSS($item_title);
  6946. }
  6947. $defaults['description'] = $item_description;
  6948. $form->addElement('html', $return);
  6949. if ($action != 'move') {
  6950. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6951. $form->applyFilter('title', 'html_filter');
  6952. }
  6953. $arrHide[0]['value'] = $this->name;
  6954. $arrHide[0]['padding'] = 20;
  6955. for ($i = 0; $i < count($arrLP); $i++) {
  6956. if ($action != 'add') {
  6957. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6958. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6959. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6960. if ($parent == $arrLP[$i]['id']) {
  6961. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6962. }
  6963. }
  6964. } else {
  6965. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6966. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6967. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6968. if ($parent == $arrLP[$i]['id']) {
  6969. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6970. }
  6971. }
  6972. }
  6973. }
  6974. $parent_select = $form->addSelect('parent', get_lang('Parent'), [], ['id' => 'idParent', 'onchange' => 'javascript: load_cbo(this.value);']);
  6975. $my_count=0;
  6976. foreach ($arrHide as $key => $value) {
  6977. if ($my_count!=0) {
  6978. // The LP name is also the first section and is not in the same charset like the other sections.
  6979. $value['value'] = Security :: remove_XSS($value['value']);
  6980. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6981. } else {
  6982. $value['value'] = Security :: remove_XSS($value['value']);
  6983. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6984. }
  6985. $my_count++;
  6986. }
  6987. if (!empty($id)) {
  6988. $parent_select->setSelected($parent);
  6989. } else {
  6990. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  6991. $parent_select->setSelected($parent_item_id);
  6992. }
  6993. if (is_array($arrLP)) {
  6994. reset($arrLP);
  6995. }
  6996. $arrHide = array();
  6997. $s_selected_position = null;
  6998. //POSITION
  6999. for ($i = 0; $i < count($arrLP); $i++) {
  7000. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id || $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  7001. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'] || $action == 'add') {
  7002. $s_selected_position = $arrLP[$i]['id'];
  7003. }
  7004. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7005. }
  7006. }
  7007. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7008. $position->addOption(get_lang('FirstPosition'), 0);
  7009. foreach ($arrHide as $key => $value) {
  7010. $padding = isset($value['padding']) ? $value['padding']: 20;
  7011. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  7012. }
  7013. $position->setSelected($s_selected_position);
  7014. if (is_array($arrLP)) {
  7015. reset($arrLP);
  7016. }
  7017. if ($action != 'move') {
  7018. $id_prerequisite = 0;
  7019. if (is_array($arrLP)) {
  7020. foreach ($arrLP as $key => $value) {
  7021. if ($value['id'] == $id) {
  7022. $id_prerequisite = $value['prerequisite'];
  7023. break;
  7024. }
  7025. }
  7026. }
  7027. $arrHide = array();
  7028. for ($i = 0; $i < count($arrLP); $i++) {
  7029. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7030. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7031. $s_selected_position = $arrLP[$i]['id'];
  7032. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7033. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7034. }
  7035. }
  7036. if (!$no_display_add) {
  7037. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7038. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7039. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true')) {
  7040. if (isset ($_POST['content']))
  7041. $content = stripslashes($_POST['content']);
  7042. elseif (is_array($extra_info)) {
  7043. //If it's an html document or a text file
  7044. if (!$no_display_edit_textarea) {
  7045. $content = $this->display_document($extra_info['path'], false, false);
  7046. }
  7047. } elseif (is_numeric($extra_info))
  7048. $content = $this->display_document($extra_info, false, false);
  7049. else
  7050. $content = '';
  7051. if (!$no_display_edit_textarea) {
  7052. // We need to calculate here some specific settings for the online editor.
  7053. // The calculated settings work for documents in the Documents tool
  7054. // (on the root or in subfolders).
  7055. // For documents in native scorm packages it is unclear whether the
  7056. // online editor should be activated or not.
  7057. // A new document, it is in the root of the repository.
  7058. $relative_path = '';
  7059. $relative_prefix = '';
  7060. if (is_array($extra_info) && $extra_info != 'new') {
  7061. // The document already exists. Whe have to determine its relative path towards the repository root.
  7062. $relative_path = explode('/', $extra_info['dir']);
  7063. $cnt = count($relative_path) - 2;
  7064. if ($cnt < 0) {
  7065. $cnt = 0;
  7066. }
  7067. $relative_prefix = str_repeat('../', $cnt);
  7068. $relative_path = array_slice($relative_path, 1, $cnt);
  7069. $relative_path = implode('/', $relative_path);
  7070. if (strlen($relative_path) > 0) {
  7071. $relative_path = $relative_path . '/';
  7072. }
  7073. } else {
  7074. $result = $this->generate_lp_folder($_course);
  7075. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7076. $relative_prefix = '../../';
  7077. }
  7078. $editor_config = array(
  7079. 'ToolbarSet'=> 'LearningPathDocuments',
  7080. 'Width' => '100%',
  7081. 'Height' => '500',
  7082. 'FullPage' => true,
  7083. 'CreateDocumentDir' => $relative_prefix,
  7084. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  7085. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  7086. );
  7087. if ($_GET['action'] == 'add_item') {
  7088. $class = 'add';
  7089. $text = get_lang('LPCreateDocument');
  7090. } else {
  7091. if ($_GET['action'] == 'edit_item') {
  7092. $class = 'save';
  7093. $text = get_lang('SaveDocument');
  7094. }
  7095. }
  7096. $form->addButtonSave($text, 'submit_button');
  7097. $renderer = $form->defaultRenderer();
  7098. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7099. $form->addElement('html', '<div class="editor-lp">');
  7100. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7101. $form->addElement('html', '</div>');
  7102. $defaults['content_lp'] = $content;
  7103. }
  7104. } elseif (is_numeric($extra_info)) {
  7105. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7106. $return = $this->display_document($extra_info, true, true, true);
  7107. $form->addElement('html', $return);
  7108. }
  7109. }
  7110. }
  7111. if ($action == 'move') {
  7112. $form->addElement('hidden', 'title', $item_title);
  7113. $form->addElement('hidden', 'description', $item_description);
  7114. }
  7115. if (is_numeric($extra_info)) {
  7116. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7117. $form->addElement('hidden', 'path', $extra_info);
  7118. } elseif (is_array($extra_info)) {
  7119. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7120. $form->addElement('hidden', 'path', $extra_info['path']);
  7121. }
  7122. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7123. $form->addElement('hidden', 'post_time', time());
  7124. $form->setDefaults($defaults);
  7125. return $form->return_form();
  7126. }
  7127. /**
  7128. * Return HTML form to add/edit a link item
  7129. * @param string $action (add/edit)
  7130. * @param integer $id Item ID if exists
  7131. * @param mixed $extra_info
  7132. * @return string HTML form
  7133. */
  7134. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7135. {
  7136. $course_id = api_get_course_int_id();
  7137. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7138. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7139. if ($id != 0 && is_array($extra_info)) {
  7140. $item_title = stripslashes($extra_info['title']);
  7141. $item_description = stripslashes($extra_info['description']);
  7142. $item_url = stripslashes($extra_info['url']);
  7143. } elseif (is_numeric($extra_info)) {
  7144. $extra_info = intval($extra_info);
  7145. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7146. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7147. $result = Database::query($sql);
  7148. $row = Database :: fetch_array($result);
  7149. $item_title = $row['title'];
  7150. $item_description = $row['description'];
  7151. $item_url = $row['url'];
  7152. } else {
  7153. $item_title = '';
  7154. $item_description = '';
  7155. $item_url = '';
  7156. }
  7157. $form = new FormValidator('edit_link', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  7158. $defaults = [];
  7159. if ($id != 0 && is_array($extra_info)) {
  7160. $parent = $extra_info['parent_item_id'];
  7161. } else {
  7162. $parent = 0;
  7163. }
  7164. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7165. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7166. $result = Database::query($sql);
  7167. $arrLP = array();
  7168. while ($row = Database :: fetch_array($result)) {
  7169. $arrLP[] = array(
  7170. 'id' => $row['id'],
  7171. 'item_type' => $row['item_type'],
  7172. 'title' => $row['title'],
  7173. 'path' => $row['path'],
  7174. 'description' => $row['description'],
  7175. 'parent_item_id' => $row['parent_item_id'],
  7176. 'previous_item_id' => $row['previous_item_id'],
  7177. 'next_item_id' => $row['next_item_id'],
  7178. 'display_order' => $row['display_order'],
  7179. 'max_score' => $row['max_score'],
  7180. 'min_score' => $row['min_score'],
  7181. 'mastery_score' => $row['mastery_score'],
  7182. 'prerequisite' => $row['prerequisite']
  7183. );
  7184. }
  7185. $this->tree_array($arrLP);
  7186. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7187. unset ($this->arrMenu);
  7188. if ($action == 'add') {
  7189. $legend = get_lang('CreateTheLink');
  7190. } elseif ($action == 'move') {
  7191. $legend = get_lang('MoveCurrentLink');
  7192. } else {
  7193. $legend = get_lang('EditCurrentLink');
  7194. }
  7195. $form->addHeader($legend);
  7196. if ($action != 'move') {
  7197. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7198. $defaults['title'] = $item_title;
  7199. }
  7200. $selectParent = $form->addSelect(
  7201. 'parent',
  7202. get_lang('Parent'),
  7203. [],
  7204. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7205. );
  7206. $selectParent->addOption($this->name, 0);
  7207. $arrHide = array(
  7208. $id
  7209. );
  7210. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7211. for ($i = 0; $i < count($arrLP); $i++) {
  7212. if ($action != 'add') {
  7213. if (
  7214. (
  7215. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7216. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7217. $arrLP[$i]['item_type'] == 'dir'
  7218. ) &&
  7219. !in_array($arrLP[$i]['id'], $arrHide) &&
  7220. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7221. ) {
  7222. $selectParent->addOption(
  7223. $arrLP[$i]['title'],
  7224. $arrLP[$i]['id'],
  7225. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7226. );
  7227. if ($parent == $arrLP[$i]['id']) {
  7228. $selectParent->setSelected($arrLP[$i]['id']);
  7229. }
  7230. } else {
  7231. $arrHide[] = $arrLP[$i]['id'];
  7232. }
  7233. } else {
  7234. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7235. $selectParent->addOption(
  7236. $arrLP[$i]['title'],
  7237. $arrLP[$i]['id'],
  7238. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7239. );
  7240. if ($parent_item_id == $arrLP[$i]['id']) {
  7241. $selectParent->setSelected($arrLP[$i]['id']);
  7242. }
  7243. }
  7244. }
  7245. }
  7246. if (is_array($arrLP)) {
  7247. reset($arrLP);
  7248. }
  7249. $selectPrevious = $form->addSelect(
  7250. 'previous',
  7251. get_lang('Position'),
  7252. [],
  7253. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7254. );
  7255. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7256. for ($i = 0; $i < count($arrLP); $i++) {
  7257. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7258. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7259. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7260. $selectPrevious->setSelected($arrLP[$i]['id']);
  7261. } elseif ($action == 'add') {
  7262. $selectPrevious->setSelected($arrLP[$i]['id']);
  7263. }
  7264. }
  7265. }
  7266. if ($action != 'move') {
  7267. $urlAttributes = ['class' => 'learnpath_item_form'];
  7268. if (is_numeric($extra_info)) {
  7269. $urlAttributes['disabled'] = 'disabled';
  7270. }
  7271. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7272. $defaults['url'] = $item_url;
  7273. $id_prerequisite = 0;
  7274. if (is_array($arrLP)) {
  7275. foreach ($arrLP as $key => $value) {
  7276. if ($value['id'] == $id) {
  7277. $id_prerequisite = $value['prerequisite'];
  7278. break;
  7279. }
  7280. }
  7281. }
  7282. $arrHide = array();
  7283. for ($i = 0; $i < count($arrLP); $i++) {
  7284. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7285. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7286. $s_selected_position = $arrLP[$i]['id'];
  7287. elseif ($action == 'add') $s_selected_position = 0;
  7288. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7289. }
  7290. }
  7291. }
  7292. if ($action == 'add') {
  7293. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7294. } else {
  7295. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7296. }
  7297. if ($action == 'move') {
  7298. $form->addHidden('title', $item_title);
  7299. $form->addHidden('description', $item_description);
  7300. }
  7301. if (is_numeric($extra_info)) {
  7302. $form->addHidden('path', $extra_info);
  7303. } elseif (is_array($extra_info)) {
  7304. $form->addHidden('path', $extra_info['path']);
  7305. }
  7306. $form->addHidden('type', TOOL_LINK);
  7307. $form->addHidden('post_time', time());
  7308. $form->setDefaults($defaults);
  7309. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7310. }
  7311. /**
  7312. * Return HTML form to add/edit a student publication (work)
  7313. * @param string Action (add/edit)
  7314. * @param integer Item ID if already exists
  7315. * @param mixed Extra info (work ID if integer)
  7316. * @return string HTML form
  7317. */
  7318. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7319. {
  7320. $course_id = api_get_course_int_id();
  7321. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7322. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7323. if ($id != 0 && is_array($extra_info)) {
  7324. $item_title = stripslashes($extra_info['title']);
  7325. $item_description = stripslashes($extra_info['description']);
  7326. } elseif (is_numeric($extra_info)) {
  7327. $extra_info = intval($extra_info);
  7328. $sql = "SELECT title, description
  7329. FROM " . $tbl_publication . "
  7330. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7331. $result = Database::query($sql);
  7332. $row = Database :: fetch_array($result);
  7333. $item_title = $row['title'];
  7334. } else {
  7335. $item_title = get_lang('Student_publication');
  7336. }
  7337. if ($id != 0 && is_array($extra_info)) {
  7338. $parent = $extra_info['parent_item_id'];
  7339. } else {
  7340. $parent = 0;
  7341. }
  7342. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7343. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7344. $result = Database::query($sql);
  7345. $arrLP = array();
  7346. while ($row = Database :: fetch_array($result)) {
  7347. $arrLP[] = array (
  7348. 'id' => $row['id'],
  7349. 'item_type' => $row['item_type'],
  7350. 'title' => $row['title'],
  7351. 'path' => $row['path'],
  7352. 'description' => $row['description'],
  7353. 'parent_item_id' => $row['parent_item_id'],
  7354. 'previous_item_id' => $row['previous_item_id'],
  7355. 'next_item_id' => $row['next_item_id'],
  7356. 'display_order' => $row['display_order'],
  7357. 'max_score' => $row['max_score'],
  7358. 'min_score' => $row['min_score'],
  7359. 'mastery_score' => $row['mastery_score'],
  7360. 'prerequisite' => $row['prerequisite']
  7361. );
  7362. }
  7363. $this->tree_array($arrLP);
  7364. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7365. unset ($this->arrMenu);
  7366. $form = new FormValidator('frm_student_publication', 'post', '#');
  7367. if ($action == 'add') {
  7368. $form->addHeader(get_lang('Student_publication'));
  7369. } elseif ($action == 'move') {
  7370. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7371. } else {
  7372. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7373. }
  7374. if ($action != 'move') {
  7375. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7376. }
  7377. $parentSelect = $form->addSelect(
  7378. 'parent',
  7379. get_lang('Parent'),
  7380. ['0' => $this->name],
  7381. [
  7382. 'onchange' => 'javascript: load_cbo(this.value);',
  7383. 'class' => 'learnpath_item_form',
  7384. 'id' => 'idParent'
  7385. ]
  7386. );
  7387. $arrHide = array (
  7388. $id
  7389. );
  7390. for ($i = 0; $i < count($arrLP); $i++) {
  7391. if ($action != 'add') {
  7392. if (
  7393. (
  7394. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7395. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7396. $arrLP[$i]['item_type'] == 'dir'
  7397. ) &&
  7398. !in_array($arrLP[$i]['id'], $arrHide) &&
  7399. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7400. ) {
  7401. $parentSelect->addOption(
  7402. $arrLP[$i]['title'],
  7403. $arrLP[$i]['id'],
  7404. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7405. );
  7406. if ($parent == $arrLP[$i]['id']) {
  7407. $parentSelect->setSelected($arrLP[$i]['id']);
  7408. }
  7409. } else {
  7410. $arrHide[] = $arrLP[$i]['id'];
  7411. }
  7412. } else {
  7413. if (
  7414. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7415. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7416. ) {
  7417. $parentSelect->addOption(
  7418. $arrLP[$i]['title'],
  7419. $arrLP[$i]['id'],
  7420. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7421. );
  7422. if ($parent == $arrLP[$i]['id']) {
  7423. $parentSelect->setSelected($arrLP[$i]['id']);
  7424. }
  7425. }
  7426. }
  7427. }
  7428. if (is_array($arrLP)) {
  7429. reset($arrLP);
  7430. }
  7431. $previousSelect = $form->addSelect(
  7432. 'previous',
  7433. get_lang('Position'),
  7434. ['0' => get_lang('FirstPosition')],
  7435. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7436. );
  7437. for ($i = 0; $i < count($arrLP); $i++) {
  7438. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7439. $previousSelect->addOption(
  7440. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7441. $arrLP[$i]['id']
  7442. );
  7443. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7444. $previousSelect->setSelected($arrLP[$i]['id']);
  7445. } elseif ($action == 'add') {
  7446. $previousSelect->setSelected($arrLP[$i]['id']);
  7447. }
  7448. }
  7449. }
  7450. if ($action != 'move') {
  7451. $id_prerequisite = 0;
  7452. if (is_array($arrLP)) {
  7453. foreach ($arrLP as $key => $value) {
  7454. if ($value['id'] == $id) {
  7455. $id_prerequisite = $value['prerequisite'];
  7456. break;
  7457. }
  7458. }
  7459. }
  7460. $arrHide = array ();
  7461. for ($i = 0; $i < count($arrLP); $i++) {
  7462. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7463. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7464. $s_selected_position = $arrLP[$i]['id'];
  7465. elseif ($action == 'add') $s_selected_position = 0;
  7466. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7467. }
  7468. }
  7469. }
  7470. if ($action == 'add') {
  7471. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7472. } else {
  7473. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7474. }
  7475. if ($action == 'move') {
  7476. $form->addHidden('title', $item_title);
  7477. $form->addHidden('description', $item_description);
  7478. }
  7479. if (is_numeric($extra_info)) {
  7480. $form->addHidden('path', $extra_info);
  7481. } elseif (is_array($extra_info)) {
  7482. $form->addHidden('path', $extra_info['path']);
  7483. }
  7484. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7485. $form->addHidden('post_time', time());
  7486. $form->setDefaults(['title' => $item_title]);
  7487. $return = '<div class="sectioncomment">';
  7488. $return .= $form->returnForm();
  7489. $return .= '</div>';
  7490. return $return;
  7491. }
  7492. /**
  7493. * Displays the menu for manipulating a step
  7494. *
  7495. * @param $item_id
  7496. * @param string $item_type
  7497. * @return string
  7498. */
  7499. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7500. {
  7501. $_course = api_get_course_info();
  7502. $course_id = api_get_course_int_id();
  7503. $course_code = api_get_course_id();
  7504. $return = '<div class="actions">';
  7505. switch ($item_type) {
  7506. case 'dokeos_chapter' :
  7507. case 'chapter' :
  7508. // Commented the message cause should not show it.
  7509. //$lang = get_lang('TitleManipulateChapter');
  7510. break;
  7511. case 'dokeos_module' :
  7512. case 'module' :
  7513. // Commented the message cause should not show it.
  7514. //$lang = get_lang('TitleManipulateModule');
  7515. break;
  7516. case TOOL_LP_FINAL_ITEM :
  7517. case TOOL_DOCUMENT :
  7518. // Commented the message cause should not show it.
  7519. //$lang = get_lang('TitleManipulateDocument');
  7520. break;
  7521. case TOOL_LINK :
  7522. case 'link' :
  7523. // Commented the message cause should not show it.
  7524. //$lang = get_lang('TitleManipulateLink');
  7525. break;
  7526. case TOOL_QUIZ :
  7527. // Commented the message cause should not show it.
  7528. //$lang = get_lang('TitleManipulateQuiz');
  7529. break;
  7530. case TOOL_STUDENTPUBLICATION :
  7531. // Commented the message cause should not show it.
  7532. //$lang = get_lang('TitleManipulateStudentPublication');
  7533. break;
  7534. }
  7535. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7536. $item_id = intval($item_id);
  7537. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7538. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7539. $result = Database::query($sql);
  7540. $row = Database::fetch_assoc($result);
  7541. $audio_player = null;
  7542. // We display an audio player if needed.
  7543. if (!empty($row['audio'])) {
  7544. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7545. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7546. </div>';
  7547. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7548. $audio_player .= '<script>
  7549. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7550. s1.addParam("allowscriptaccess","always");
  7551. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7552. s1.write("container");
  7553. </script>';
  7554. }
  7555. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7556. $return .= Display::url(
  7557. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7558. $url.'&action=edit_item&path_item=' . $row['path']
  7559. );
  7560. $return .= Display::url(
  7561. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7562. $url.'&action=move_item'
  7563. );
  7564. // Commented for now as prerequisites cannot be added to chapters.
  7565. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7566. $return .= Display::url(
  7567. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7568. $url.'&action=edit_item_prereq'
  7569. );
  7570. }
  7571. $return .= Display::url(
  7572. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7573. $url.'&action=delete_item'
  7574. );
  7575. if ($item_type == TOOL_HOTPOTATOES ) {
  7576. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7577. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7578. }
  7579. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7580. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7581. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7582. }
  7583. $return .= '</div>';
  7584. if (!empty($audio_player)) {
  7585. $return .= '<br />'.$audio_player;
  7586. }
  7587. return $return;
  7588. }
  7589. /**
  7590. * Creates the javascript needed for filling up the checkboxes without page reload
  7591. * @return string
  7592. */
  7593. public function get_js_dropdown_array()
  7594. {
  7595. $course_id = api_get_course_int_id();
  7596. $return = 'var child_name = new Array();' . "\n";
  7597. $return .= 'var child_value = new Array();' . "\n\n";
  7598. $return .= 'child_name[0] = new Array();' . "\n";
  7599. $return .= 'child_value[0] = new Array();' . "\n\n";
  7600. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7601. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7602. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7603. ORDER BY display_order ASC";
  7604. $res_zero = Database::query($sql_zero);
  7605. $i = 0;
  7606. while ($row_zero = Database :: fetch_array($res_zero)) {
  7607. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7608. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7609. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7610. }
  7611. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7612. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7613. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7614. }
  7615. }
  7616. $return .= "\n";
  7617. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7618. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7619. $res = Database::query($sql);
  7620. while ($row = Database :: fetch_array($res)) {
  7621. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7622. WHERE
  7623. c_id = ".$course_id." AND
  7624. parent_item_id = " . $row['id'] . "
  7625. ORDER BY display_order ASC";
  7626. $res_parent = Database::query($sql_parent);
  7627. $i = 0;
  7628. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7629. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7630. while ($row_parent = Database :: fetch_array($res_parent)) {
  7631. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7632. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7633. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7634. }
  7635. $return .= "\n";
  7636. }
  7637. return $return;
  7638. }
  7639. /**
  7640. * Display the form to allow moving an item
  7641. * @param integer Item ID
  7642. * @return string HTML form
  7643. */
  7644. public function display_move_item($item_id)
  7645. {
  7646. $course_id = api_get_course_int_id();
  7647. $return = '';
  7648. if (is_numeric($item_id)) {
  7649. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7650. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7651. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7652. $res = Database::query($sql);
  7653. $row = Database :: fetch_array($res);
  7654. switch ($row['item_type']) {
  7655. case 'dokeos_chapter' :
  7656. case 'dir' :
  7657. case 'asset' :
  7658. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7659. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7660. break;
  7661. case 'dokeos_module' :
  7662. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7663. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7664. break;
  7665. case TOOL_DOCUMENT :
  7666. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7667. $return .= $this->display_document_form('move', $item_id, $row);
  7668. break;
  7669. case TOOL_LINK :
  7670. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7671. $return .= $this->display_link_form('move', $item_id, $row);
  7672. break;
  7673. case TOOL_HOTPOTATOES :
  7674. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7675. $return .= $this->display_link_form('move', $item_id, $row);
  7676. break;
  7677. case TOOL_QUIZ :
  7678. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7679. $return .= $this->display_quiz_form('move', $item_id, $row);
  7680. break;
  7681. case TOOL_STUDENTPUBLICATION :
  7682. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7683. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7684. break;
  7685. case TOOL_FORUM :
  7686. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7687. $return .= $this->display_forum_form('move', $item_id, $row);
  7688. break;
  7689. case TOOL_THREAD :
  7690. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7691. $return .= $this->display_forum_form('move', $item_id, $row);
  7692. break;
  7693. }
  7694. }
  7695. return $return;
  7696. }
  7697. /**
  7698. * Displays a basic form on the overview page for changing the item title and the item description.
  7699. * @param string $item_type
  7700. * @param string $title
  7701. * @param array $data
  7702. * @return string
  7703. */
  7704. public function display_item_small_form($item_type, $title = '', $data = array())
  7705. {
  7706. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7707. $form = new FormValidator('small_form', 'post', $url);
  7708. $form->addElement('header', $title);
  7709. $form->addElement('text', 'title', get_lang('Title'));
  7710. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7711. $form->addElement('hidden', 'id', $data['id']);
  7712. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7713. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7714. $form->setDefaults(array('title' => $data['title']));
  7715. return $form->toHtml();
  7716. }
  7717. /**
  7718. * Return HTML form to allow prerequisites selection
  7719. * @todo use FormValidator
  7720. * @param integer Item ID
  7721. * @return string HTML form
  7722. */
  7723. public function display_item_prerequisites_form($item_id)
  7724. {
  7725. $course_id = api_get_course_int_id();
  7726. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7727. $item_id = intval($item_id);
  7728. /* Current prerequisite */
  7729. $sql = "SELECT * FROM $tbl_lp_item
  7730. WHERE c_id = $course_id AND id = " . $item_id;
  7731. $result = Database::query($sql);
  7732. $row = Database::fetch_array($result);
  7733. $prerequisiteId = $row['prerequisite'];
  7734. $return = '<legend>';
  7735. $return .= get_lang('AddEditPrerequisites');
  7736. $return .= '</legend>';
  7737. $return .= '<form method="POST">';
  7738. $return .= '<table class="data_table">';
  7739. $return .= '<tr>';
  7740. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7741. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7742. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7743. $return .= '</tr>';
  7744. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7745. $return .= '<tr >';
  7746. $return .= '<td colspan="3" class="radio">';
  7747. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7748. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7749. $return .= '</tr>';
  7750. $sql = "SELECT * FROM $tbl_lp_item
  7751. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7752. $result = Database::query($sql);
  7753. $arrLP = array();
  7754. $selectedMinScore = array();
  7755. $selectedMaxScore = array();
  7756. while ($row = Database :: fetch_array($result)) {
  7757. if ($row['id'] == $item_id) {
  7758. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7759. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7760. }
  7761. $arrLP[] = array(
  7762. 'id' => $row['id'],
  7763. 'item_type' => $row['item_type'],
  7764. 'title' => $row['title'],
  7765. 'ref' => $row['ref'],
  7766. 'description' => $row['description'],
  7767. 'parent_item_id' => $row['parent_item_id'],
  7768. 'previous_item_id' => $row['previous_item_id'],
  7769. 'next_item_id' => $row['next_item_id'],
  7770. 'max_score' => $row['max_score'],
  7771. 'min_score' => $row['min_score'],
  7772. 'mastery_score' => $row['mastery_score'],
  7773. 'prerequisite' => $row['prerequisite'],
  7774. 'next_item_id' => $row['next_item_id'],
  7775. 'display_order' => $row['display_order'],
  7776. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7777. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7778. );
  7779. }
  7780. $this->tree_array($arrLP);
  7781. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7782. unset($this->arrMenu);
  7783. for ($i = 0; $i < count($arrLP); $i++) {
  7784. $item = $arrLP[$i];
  7785. if ($item['id'] == $item_id) {
  7786. break;
  7787. }
  7788. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7789. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7790. $return .= '<tr>';
  7791. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7792. $return .= '<label for="id' . $item['id'] . '">';
  7793. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . (($item['item_type'] == 'dokeos_module' || $item['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7794. $icon_name = str_replace(' ', '', $item['item_type']);
  7795. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7796. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  7797. } else {
  7798. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7799. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  7800. } else {
  7801. $return .= Display::return_icon('folder_document.gif', '', array('style'=>'margin-right:5px;'));
  7802. }
  7803. }
  7804. $return .= $item['title'] . '</label>';
  7805. $return .= '</td>';
  7806. if ($item['item_type'] == TOOL_QUIZ) {
  7807. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7808. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7809. $tmp_obj_exercice = new Exercise();
  7810. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7811. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7812. $tmp_obj_lp_item->update_in_bdd();
  7813. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7814. $return .= '<td class="exercise">';
  7815. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7816. $return .= '</td>';
  7817. $return .= '<td class="exercise">';
  7818. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7819. $return .= '</td>';
  7820. }
  7821. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7822. $return .= '<td class="exercise">';
  7823. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7824. $return .= '</td>';
  7825. $return .= '<td class="exercise"">';
  7826. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7827. $return .= '</td>';
  7828. }
  7829. $return .= '</tr>';
  7830. }
  7831. $return .= '<tr>';
  7832. $return .= '</tr>';
  7833. $return .= '</table>';
  7834. $return .= '<div style="padding-top:3px;">';
  7835. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7836. $return .= '</form>';
  7837. return $return;
  7838. }
  7839. /**
  7840. * Return HTML list to allow prerequisites selection for lp
  7841. * @param integer Item ID
  7842. * @return string HTML form
  7843. */
  7844. public function display_lp_prerequisites_list()
  7845. {
  7846. $course_id = api_get_course_int_id();
  7847. $lp_id = $this->lp_id;
  7848. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7849. // get current prerequisite
  7850. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7851. $result = Database::query($sql);
  7852. $row = Database :: fetch_array($result);
  7853. $prerequisiteId = $row['prerequisite'];
  7854. $session_id = api_get_session_id();
  7855. $session_condition = api_get_session_condition($session_id, true, true);
  7856. $sql = "SELECT * FROM $tbl_lp
  7857. WHERE c_id = $course_id $session_condition
  7858. ORDER BY display_order ";
  7859. $rs = Database::query($sql);
  7860. $return = '';
  7861. $return .= '<select name="prerequisites" class="form-control">';
  7862. $return .= '<option value="0">'.get_lang('None').'</option>';
  7863. if (Database::num_rows($rs) > 0) {
  7864. while ($row = Database::fetch_array($rs)) {
  7865. if ($row['id'] == $lp_id) {
  7866. continue;
  7867. }
  7868. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7869. }
  7870. }
  7871. $return .= '</select>';
  7872. return $return;
  7873. }
  7874. /**
  7875. * Creates a list with all the documents in it
  7876. * @param bool $showInvisibleFiles
  7877. * @return string
  7878. */
  7879. public function get_documents($showInvisibleFiles = false)
  7880. {
  7881. $course_info = api_get_course_info();
  7882. $sessionId = api_get_session_id();
  7883. $document_tree = DocumentManager::get_document_preview(
  7884. $course_info,
  7885. $this->lp_id,
  7886. null,
  7887. $sessionId,
  7888. true,
  7889. null,
  7890. null,
  7891. $showInvisibleFiles,
  7892. true
  7893. );
  7894. return $document_tree;
  7895. }
  7896. /**
  7897. * Creates a list with all the exercises (quiz) in it
  7898. * @return string
  7899. */
  7900. public function get_exercises()
  7901. {
  7902. $course_id = api_get_course_int_id();
  7903. $session_id = api_get_session_id();
  7904. $userInfo = api_get_user_info();
  7905. // New for hotpotatoes.
  7906. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7907. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7908. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7909. $condition_session = api_get_session_condition($session_id, true, true);
  7910. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  7911. $activeCondition = " active <> -1 ";
  7912. if ($setting) {
  7913. $activeCondition = " active = 1 ";
  7914. }
  7915. $sql_quiz = "SELECT * FROM $tbl_quiz
  7916. WHERE c_id = $course_id AND $activeCondition $condition_session
  7917. ORDER BY title ASC";
  7918. $sql_hot = "SELECT * FROM $tbl_doc
  7919. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7920. ORDER BY id ASC";
  7921. $res_quiz = Database::query($sql_quiz);
  7922. $res_hot = Database::query($sql_hot);
  7923. $return = '<ul class="lp_resource">';
  7924. $return .= '<li class="lp_resource_element">';
  7925. $return .= Display::return_icon('new_test_small.gif');
  7926. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7927. get_lang('NewExercise') . '</a>';
  7928. $return .= '</li>';
  7929. // Display hotpotatoes
  7930. while ($row_hot = Database :: fetch_array($res_hot)) {
  7931. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7932. $return .= '<a class="moved" href="#">';
  7933. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7934. $return .= '</a> ';
  7935. $return .= Display::return_icon('hotpotatoes_s.png');
  7936. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7937. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7938. $return .= '</li>';
  7939. }
  7940. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7941. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7942. $return .= '<a class="moved" href="#">';
  7943. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7944. $return .= '</a> ';
  7945. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  7946. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  7947. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7948. Security :: remove_XSS(cut($row_quiz['title'], 80)).$sessionStar.
  7949. '</a>';
  7950. $return .= '</li>';
  7951. }
  7952. $return .= '</ul>';
  7953. return $return;
  7954. }
  7955. /**
  7956. * Creates a list with all the links in it
  7957. * @return string
  7958. */
  7959. public function get_links()
  7960. {
  7961. $selfUrl = api_get_self();
  7962. $courseIdReq = api_get_cidreq();
  7963. $course = api_get_course_info();
  7964. $userInfo = api_get_user_info();
  7965. $course_id = $course['real_id'];
  7966. $tbl_link = Database::get_course_table(TABLE_LINK);
  7967. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7968. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7969. $session_id = api_get_session_id();
  7970. $condition_session = api_get_session_condition(
  7971. $session_id,
  7972. true,
  7973. true,
  7974. "link.session_id"
  7975. );
  7976. $sql = "SELECT link.id as link_id,
  7977. link.title as link_title,
  7978. link.session_id as link_session_id,
  7979. link.category_id as category_id,
  7980. link_category.category_title as category_title
  7981. FROM $tbl_link as link
  7982. LEFT JOIN $linkCategoryTable as link_category
  7983. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  7984. WHERE link.c_id = ".$course_id." $condition_session
  7985. ORDER BY link_category.category_title ASC, link.title ASC";
  7986. $links = Database::query($sql);
  7987. $categorizedLinks = array();
  7988. $categories = array();
  7989. while ($link = Database :: fetch_array($links)) {
  7990. if (!$link['category_id']) {
  7991. $link['category_title'] = get_lang('Uncategorized');
  7992. }
  7993. $categories[$link['category_id']] = $link['category_title'];
  7994. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  7995. }
  7996. $linksHtmlCode =
  7997. '<script>
  7998. function toggle_tool(tool, id){
  7999. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8000. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8001. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8002. } else {
  8003. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8004. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8005. }
  8006. }
  8007. </script>
  8008. <ul class="lp_resource">
  8009. <li class="lp_resource_element">
  8010. '.Display::return_icon('linksnew.gif').'
  8011. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8012. get_lang('LinkAdd').'
  8013. </a>
  8014. </li>';
  8015. foreach ($categorizedLinks as $categoryId => $links) {
  8016. $linkNodes = null;
  8017. foreach ($links as $key => $linkInfo) {
  8018. $title = $linkInfo['link_title'];
  8019. $linkSessionId = $linkInfo['link_session_id'];
  8020. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8021. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8022. $linkNodes .=
  8023. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8024. <a class="moved" href="#">'.
  8025. $moveEverywhereIcon.
  8026. '</a>
  8027. '.Display::return_icon('lp_link.png').'
  8028. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8029. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8030. Security::remove_XSS($title).$sessionStar.
  8031. '</a>
  8032. </li>';
  8033. }
  8034. }
  8035. $linksHtmlCode .=
  8036. '<li>
  8037. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8038. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8039. align="absbottom" />
  8040. </a>
  8041. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8042. </li>
  8043. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8044. }
  8045. $linksHtmlCode .= '</ul>';
  8046. return $linksHtmlCode;
  8047. }
  8048. /**
  8049. * Creates a list with all the student publications in it
  8050. * @return unknown
  8051. */
  8052. public function get_student_publications()
  8053. {
  8054. $return = '<div class="lp_resource" >';
  8055. $return .= '<div class="lp_resource_element">';
  8056. $return .= Display::return_icon('works_small.gif', '', array(), ICON_SIZE_TINY);
  8057. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  8058. $return .= '</div>';
  8059. $return .= '</div>';
  8060. return $return;
  8061. }
  8062. /**
  8063. * Creates a list with all the forums in it
  8064. * @return string
  8065. */
  8066. public function get_forums()
  8067. {
  8068. require_once '../forum/forumfunction.inc.php';
  8069. require_once '../forum/forumconfig.inc.php';
  8070. $forumCategories = get_forum_categories();
  8071. $forumsInNoCategory = get_forums_in_category(0);
  8072. if (!empty($forumsInNoCategory)) {
  8073. $forumCategories = array_merge(
  8074. $forumCategories,
  8075. array(
  8076. array(
  8077. 'cat_id' => 0,
  8078. 'session_id' => 0,
  8079. 'visibility' => 1,
  8080. 'cat_comment' => null,
  8081. ),
  8082. )
  8083. );
  8084. }
  8085. $forumList = get_forums();
  8086. $a_forums = [];
  8087. foreach ($forumCategories as $forumCategory) {
  8088. // The forums in this category.
  8089. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8090. if (!empty($forumsInCategory)) {
  8091. foreach ($forumList as $forum) {
  8092. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8093. $a_forums[] = $forum;
  8094. }
  8095. }
  8096. }
  8097. }
  8098. $return = '<ul class="lp_resource">';
  8099. //First add link
  8100. $return .= '<li class="lp_resource_element">';
  8101. $return .= Display::return_icon('forum_new_small.gif');
  8102. $return .= Display::url(
  8103. get_lang('CreateANewForum'),
  8104. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8105. 'action' => 'add',
  8106. 'content' => 'forum',
  8107. 'lp_id' => $this->lp_id
  8108. ]),
  8109. ['title' => get_lang('CreateANewForum')]
  8110. );
  8111. $return .= '</li>';
  8112. $return .= '<script>
  8113. function toggle_forum(forum_id){
  8114. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8115. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8116. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8117. } else {
  8118. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8119. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8120. }
  8121. }
  8122. </script>';
  8123. foreach ($a_forums as $forum) {
  8124. if (!empty($forum['forum_id'])) {
  8125. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8126. $return .= '<a class="moved" href="#">';
  8127. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8128. $return .= ' </a>';
  8129. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8130. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  8131. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8132. </a>
  8133. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  8134. Security :: remove_XSS($forum['forum_title']) . '</a>';
  8135. $return .= '</li>';
  8136. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8137. $a_threads = get_threads($forum['forum_id']);
  8138. if (is_array($a_threads)) {
  8139. foreach ($a_threads as $thread) {
  8140. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8141. $return .= '&nbsp;<a class="moved" href="#">';
  8142. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8143. $return .= ' </a>';
  8144. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8145. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  8146. Security :: remove_XSS($thread['thread_title']) . '</a>';
  8147. $return .= '</li>';
  8148. }
  8149. }
  8150. $return .= '</div>';
  8151. }
  8152. }
  8153. $return .= '</ul>';
  8154. return $return;
  8155. }
  8156. /**
  8157. * // TODO: The output encoding should be equal to the system encoding.
  8158. *
  8159. * Exports the learning path as a SCORM package. This is the main function that
  8160. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8161. * whole thing and returns the zip.
  8162. *
  8163. * This method needs to be called in PHP5, as it will fail with non-adequate
  8164. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8165. * you need to call it on a learnpath object.
  8166. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8167. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8168. * path has been modified, it should use the generic method here below.
  8169. * @TODO link this function with the export_lp() function in the same class
  8170. * @param string Optional name of zip file. If none, title of learnpath is
  8171. * domesticated and trailed with ".zip"
  8172. * @return string Returns the zip package string, or null if error
  8173. */
  8174. public function scorm_export()
  8175. {
  8176. $_course = api_get_course_info();
  8177. $course_id = $_course['real_id'];
  8178. // Remove memory and time limits as much as possible as this might be a long process...
  8179. if (function_exists('ini_set')) {
  8180. api_set_memory_limit('128M');
  8181. ini_set('max_execution_time', 600);
  8182. }
  8183. // Create the zip handler (this will remain available throughout the method).
  8184. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8185. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8186. $temp_dir_short = uniqid();
  8187. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8188. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8189. $zip_folder = new PclZip($temp_zip_file);
  8190. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8191. $root_path = $main_path = api_get_path(SYS_PATH);
  8192. $files_cleanup = array();
  8193. // Place to temporarily stash the zip file.
  8194. // create the temp dir if it doesn't exist
  8195. // or do a cleanup before creating the zip file.
  8196. if (!is_dir($temp_zip_dir)) {
  8197. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8198. } else {
  8199. // Cleanup: Check the temp dir for old files and delete them.
  8200. $handle = opendir($temp_zip_dir);
  8201. while (false !== ($file = readdir($handle))) {
  8202. if ($file != '.' && $file != '..') {
  8203. unlink("$temp_zip_dir/$file");
  8204. }
  8205. }
  8206. closedir($handle);
  8207. }
  8208. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8209. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8210. // Remove the possible . at the end of the path.
  8211. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8212. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8213. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8214. copyr(
  8215. $current_course_path.'/scorm/'.$this->path,
  8216. $dest_path_to_scorm_folder,
  8217. array('imsmanifest'),
  8218. $zip_files
  8219. );
  8220. }
  8221. // Build a dummy imsmanifest structure.
  8222. // Do not add to the zip yet (we still need it).
  8223. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8224. // Aggregation Model official document, section "2.3 Content Packaging".
  8225. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8226. $xmldoc = new DOMDocument('1.0');
  8227. $root = $xmldoc->createElement('manifest');
  8228. $root->setAttribute('identifier', 'SingleCourseManifest');
  8229. $root->setAttribute('version', '1.1');
  8230. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8231. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8232. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8233. $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');
  8234. // Build mandatory sub-root container elements.
  8235. $metadata = $xmldoc->createElement('metadata');
  8236. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8237. $metadata->appendChild($md_schema);
  8238. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8239. $metadata->appendChild($md_schemaversion);
  8240. $root->appendChild($metadata);
  8241. $organizations = $xmldoc->createElement('organizations');
  8242. $resources = $xmldoc->createElement('resources');
  8243. // Build the only organization we will use in building our learnpaths.
  8244. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8245. $organization = $xmldoc->createElement('organization');
  8246. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8247. // To set the title of the SCORM entity (=organization), we take the name given
  8248. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8249. // learning path charset) as it is the encoding that defines how it is stored
  8250. // in the database. Then we convert it to HTML entities again as the "&" character
  8251. // alone is not authorized in XML (must be &amp;).
  8252. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8253. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8254. $organization->appendChild($org_title);
  8255. $folder_name = 'document';
  8256. // Removes the learning_path/scorm_folder path when exporting see #4841
  8257. $path_to_remove = null;
  8258. $result = $this->generate_lp_folder($_course);
  8259. $path_to_replace = '';
  8260. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8261. $path_to_remove = 'document'.$result['dir'];
  8262. $path_to_replace = $folder_name.'/';
  8263. }
  8264. //Fixes chamilo scorm exports
  8265. if ($this->ref == 'chamilo_scorm_export') {
  8266. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8267. }
  8268. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8269. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8270. $link_updates = array();
  8271. $links_to_create = array();
  8272. foreach ($this->items as $index => $item) {
  8273. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8274. // Get included documents from this item.
  8275. if ($item->type === 'sco') {
  8276. $inc_docs = $item->get_resources_from_source(
  8277. null,
  8278. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8279. );
  8280. } else {
  8281. $inc_docs = $item->get_resources_from_source();
  8282. }
  8283. // Give a child element <item> to the <organization> element.
  8284. $my_item_id = $item->get_id();
  8285. $my_item = $xmldoc->createElement('item');
  8286. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8287. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8288. $my_item->setAttribute('isvisible', 'true');
  8289. // Give a child element <title> to the <item> element.
  8290. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8291. $my_item->appendChild($my_title);
  8292. // Give a child element <adlcp:prerequisites> to the <item> element.
  8293. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8294. $my_prereqs->setAttribute('type', 'aicc_script');
  8295. $my_item->appendChild($my_prereqs);
  8296. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8297. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8298. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8299. //$xmldoc->createElement('adlcp:timelimitaction','');
  8300. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8301. //$xmldoc->createElement('adlcp:datafromlms','');
  8302. // Give a child element <adlcp:masteryscore> to the <item> element.
  8303. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8304. $my_item->appendChild($my_masteryscore);
  8305. // Attach this item to the organization element or hits parent if there is one.
  8306. if (!empty($item->parent) && $item->parent != 0) {
  8307. $children = $organization->childNodes;
  8308. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8309. if (is_object($possible_parent)) {
  8310. $possible_parent->appendChild($my_item);
  8311. } else {
  8312. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8313. }
  8314. } else {
  8315. if ($this->debug > 0) { error_log('No parent'); }
  8316. $organization->appendChild($my_item);
  8317. }
  8318. // Get the path of the file(s) from the course directory root.
  8319. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8320. if (!empty($path_to_remove)) {
  8321. //From docs
  8322. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8323. //From quiz
  8324. if ($this->ref == 'chamilo_scorm_export') {
  8325. $path_to_remove = 'scorm/'.$this->path.'/';
  8326. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8327. }
  8328. } else {
  8329. $my_xml_file_path = $my_file_path;
  8330. }
  8331. $my_sub_dir = dirname($my_file_path);
  8332. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8333. $my_xml_sub_dir = $my_sub_dir;
  8334. // Give a <resource> child to the <resources> element
  8335. $my_resource = $xmldoc->createElement('resource');
  8336. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8337. $my_resource->setAttribute('type', 'webcontent');
  8338. $my_resource->setAttribute('href', $my_xml_file_path);
  8339. // adlcp:scormtype can be either 'sco' or 'asset'.
  8340. if ($item->type === 'sco') {
  8341. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8342. } else {
  8343. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8344. }
  8345. // xml:base is the base directory to find the files declared in this resource.
  8346. $my_resource->setAttribute('xml:base', '');
  8347. // Give a <file> child to the <resource> element.
  8348. $my_file = $xmldoc->createElement('file');
  8349. $my_file->setAttribute('href', $my_xml_file_path);
  8350. $my_resource->appendChild($my_file);
  8351. // Dependency to other files - not yet supported.
  8352. $i = 1;
  8353. foreach ($inc_docs as $doc_info) {
  8354. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8355. continue;
  8356. }
  8357. $my_dep = $xmldoc->createElement('resource');
  8358. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8359. $my_dep->setAttribute('identifier', $res_id);
  8360. $my_dep->setAttribute('type', 'webcontent');
  8361. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8362. $my_dep_file = $xmldoc->createElement('file');
  8363. // Check type of URL.
  8364. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8365. if ($doc_info[1] == 'remote') {
  8366. // Remote file. Save url as is.
  8367. $my_dep_file->setAttribute('href', $doc_info[0]);
  8368. $my_dep->setAttribute('xml:base', '');
  8369. } elseif ($doc_info[1] === 'local') {
  8370. switch ($doc_info[2]) {
  8371. case 'url': // Local URL - save path as url for now, don't zip file.
  8372. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8373. $current_dir = dirname($abs_path);
  8374. $current_dir = str_replace('\\', '/', $current_dir);
  8375. $file_path = realpath($abs_path);
  8376. $file_path = str_replace('\\', '/', $file_path);
  8377. $my_dep_file->setAttribute('href', $file_path);
  8378. $my_dep->setAttribute('xml:base', '');
  8379. if (strstr($file_path, $main_path) !== false) {
  8380. // The calculated real path is really inside Chamilo's root path.
  8381. // Reduce file path to what's under the DocumentRoot.
  8382. $file_path = substr($file_path, strlen($root_path) - 1);
  8383. //echo $file_path;echo '<br /><br />';
  8384. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8385. $zip_files_abs[] = $file_path;
  8386. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8387. $my_dep_file->setAttribute('href', $file_path);
  8388. $my_dep->setAttribute('xml:base', '');
  8389. } elseif (empty($file_path)) {
  8390. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8391. if (strpos($document_root, -1) == '/') {
  8392. $document_root = substr(0, -1, $document_root);
  8393. }*/
  8394. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8395. $file_path = str_replace('//', '/', $file_path);
  8396. if (file_exists($file_path)) {
  8397. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8398. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8399. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8400. $my_dep_file->setAttribute('href', $file_path);
  8401. $my_dep->setAttribute('xml:base', '');
  8402. }
  8403. }
  8404. break;
  8405. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8406. $my_dep_file->setAttribute('href', $doc_info[0]);
  8407. $my_dep->setAttribute('xml:base', '');
  8408. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8409. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8410. $abs_img_path_without_subdir = $doc_info[0];
  8411. $relp = api_get_path(REL_PATH); // The url-append config param.
  8412. $pos = strpos($abs_img_path_without_subdir, $relp);
  8413. if ($pos === 0) {
  8414. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8415. }
  8416. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8417. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8418. $file_path = str_replace('\\', '/', $file_path);
  8419. $file_path = str_replace('//', '/', $file_path);
  8420. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8421. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8422. // Check if the current document is in that path.
  8423. if (strstr($file_path, $cur_path) !== false) {
  8424. // The document is in that path, now get the relative path
  8425. // to the containing document.
  8426. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8427. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8428. $relative_path = '';
  8429. // Image/src file is inside a course
  8430. $fixDirRelative = '';
  8431. // image file is inside a course
  8432. if (strstr($file_path, $cur_path) !== false) {
  8433. // html file that wraps the image is inside a course
  8434. if (strstr($orig_file_path, $cur_path) !== false) {
  8435. // Get count of directories from document until the last slash
  8436. // if $orig_file_path =
  8437. // /var/www/html/chamilo/app/courses/CLEAN123/document/learning_path/Le_parcours/
  8438. // $distance should be 2 (learning_path and Le_parcours)
  8439. $origFilePathFixed = str_replace($path_to_remove, $path_to_replace, $orig_file_path);
  8440. $distance = array_filter(explode('/', str_replace($cur_path.'document/', '', $origFilePathFixed)));
  8441. if ($distance) {
  8442. foreach ($distance as $relative) {
  8443. $fixDirRelative .= '../';
  8444. }
  8445. }
  8446. }
  8447. $relative_path = str_replace($cur_path, '', $file_path);
  8448. $file_path = substr($file_path, strlen($cur_path));
  8449. if (!empty($fixDirRelative)) {
  8450. $relative_path = $fixDirRelative.$relative_path;
  8451. }
  8452. } else {
  8453. // This case is still a problem as it's difficult to calculate a relative path easily
  8454. // might still generate wrong links.
  8455. //$file_path = substr($file_path,strlen($cur_path));
  8456. // Calculate the directory path to the current file (without trailing slash).
  8457. $my_relative_path = dirname($file_path);
  8458. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8459. $my_relative_file = basename($file_path);
  8460. // Calculate the directory path to the containing file (without trailing slash).
  8461. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8462. $dotdot = '';
  8463. $subdir = '';
  8464. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8465. $my_relative_path2 = dirname($my_relative_path);
  8466. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8467. $my_orig_file_path = dirname($my_orig_file_path);
  8468. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8469. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8470. $dotdot += '../';
  8471. $my_relative_path = $my_relative_path2;
  8472. }
  8473. $relative_path = $dotdot.$subdir.$my_relative_file;
  8474. }
  8475. // Put the current document in the zip (this array is the array
  8476. // that will manage documents already in the course folder - relative).
  8477. $zip_files[] = $file_path;
  8478. // Update the links to the current document in the containing document (make them relative).
  8479. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8480. $my_dep_file->setAttribute('href', $file_path);
  8481. $my_dep->setAttribute('xml:base', '');
  8482. } elseif (strstr($file_path, $main_path) !== false) {
  8483. // The calculated real path is really inside Chamilo's root path.
  8484. // Reduce file path to what's under the DocumentRoot.
  8485. $file_path = substr($file_path, strlen($root_path));
  8486. //echo $file_path;echo '<br /><br />';
  8487. //error_log('Reduced path: '.$file_path, 0);
  8488. $zip_files_abs[] = $file_path;
  8489. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8490. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8491. $my_dep->setAttribute('xml:base', '');
  8492. } elseif (empty($file_path)) {
  8493. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8494. if(strpos($document_root,-1) == '/') {
  8495. $document_root = substr(0, -1, $document_root);
  8496. }*/
  8497. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8498. $file_path = str_replace('//', '/', $file_path);
  8499. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8500. $current_dir = dirname($abs_path);
  8501. $current_dir = str_replace('\\', '/', $current_dir);
  8502. if (file_exists($file_path)) {
  8503. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8504. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8505. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8506. $my_dep_file->setAttribute('href','document/'.$file_path);
  8507. $my_dep->setAttribute('xml:base', '');
  8508. }
  8509. }
  8510. break;
  8511. case 'rel':
  8512. // Path relative to the current document.
  8513. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8514. if (substr($doc_info[0], 0, 2) == '..') {
  8515. // Relative path going up.
  8516. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8517. $current_dir = str_replace('\\', '/', $current_dir);
  8518. $file_path = realpath($current_dir.$doc_info[0]);
  8519. $file_path = str_replace('\\', '/', $file_path);
  8520. //error_log($file_path.' <-> '.$main_path,0);
  8521. if (strstr($file_path, $main_path) !== false) {
  8522. // The calculated real path is really inside Chamilo's root path.
  8523. // Reduce file path to what's under the DocumentRoot.
  8524. $file_path = substr($file_path, strlen($root_path));
  8525. //error_log('Reduced path: '.$file_path, 0);
  8526. $zip_files_abs[] = $file_path;
  8527. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8528. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8529. $my_dep->setAttribute('xml:base', '');
  8530. }
  8531. } else {
  8532. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8533. $my_dep_file->setAttribute('href', $doc_info[0]);
  8534. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8535. }
  8536. break;
  8537. default:
  8538. $my_dep_file->setAttribute('href', $doc_info[0]);
  8539. $my_dep->setAttribute('xml:base', '');
  8540. break;
  8541. }
  8542. }
  8543. $my_dep->appendChild($my_dep_file);
  8544. $resources->appendChild($my_dep);
  8545. $dependency = $xmldoc->createElement('dependency');
  8546. $dependency->setAttribute('identifierref', $res_id);
  8547. $my_resource->appendChild($dependency);
  8548. $i++;
  8549. }
  8550. $resources->appendChild($my_resource);
  8551. $zip_files[] = $my_file_path;
  8552. } else {
  8553. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8554. switch ($item->type) {
  8555. case TOOL_LINK:
  8556. $my_item = $xmldoc->createElement('item');
  8557. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8558. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8559. $my_item->setAttribute('isvisible', 'true');
  8560. // Give a child element <title> to the <item> element.
  8561. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8562. $my_item->appendChild($my_title);
  8563. // Give a child element <adlcp:prerequisites> to the <item> element.
  8564. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8565. $my_prereqs->setAttribute('type', 'aicc_script');
  8566. $my_item->appendChild($my_prereqs);
  8567. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8568. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8569. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8570. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8571. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8572. //$xmldoc->createElement('adlcp:datafromlms', '');
  8573. // Give a child element <adlcp:masteryscore> to the <item> element.
  8574. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8575. $my_item->appendChild($my_masteryscore);
  8576. // Attach this item to the organization element or its parent if there is one.
  8577. if (!empty($item->parent) && $item->parent != 0) {
  8578. $children = $organization->childNodes;
  8579. for ($i = 0; $i < $children->length; $i++) {
  8580. $item_temp = $children->item($i);
  8581. if ($item_temp -> nodeName == 'item') {
  8582. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8583. $item_temp -> appendChild($my_item);
  8584. }
  8585. }
  8586. }
  8587. } else {
  8588. $organization->appendChild($my_item);
  8589. }
  8590. $my_file_path = 'link_'.$item->get_id().'.html';
  8591. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8592. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8593. $rs = Database::query($sql);
  8594. if ($link = Database :: fetch_array($rs)) {
  8595. $url = $link['url'];
  8596. $title = stripslashes($link['title']);
  8597. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8598. $my_xml_file_path = $my_file_path;
  8599. $my_sub_dir = dirname($my_file_path);
  8600. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8601. $my_xml_sub_dir = $my_sub_dir;
  8602. // Give a <resource> child to the <resources> element.
  8603. $my_resource = $xmldoc->createElement('resource');
  8604. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8605. $my_resource->setAttribute('type', 'webcontent');
  8606. $my_resource->setAttribute('href', $my_xml_file_path);
  8607. // adlcp:scormtype can be either 'sco' or 'asset'.
  8608. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8609. // xml:base is the base directory to find the files declared in this resource.
  8610. $my_resource->setAttribute('xml:base', '');
  8611. // give a <file> child to the <resource> element.
  8612. $my_file = $xmldoc->createElement('file');
  8613. $my_file->setAttribute('href', $my_xml_file_path);
  8614. $my_resource->appendChild($my_file);
  8615. $resources->appendChild($my_resource);
  8616. }
  8617. break;
  8618. case TOOL_QUIZ:
  8619. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8620. $exe = new Exercise();
  8621. $exe->read($exe_id);
  8622. $my_item = $xmldoc->createElement('item');
  8623. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8624. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8625. $my_item->setAttribute('isvisible', 'true');
  8626. // Give a child element <title> to the <item> element.
  8627. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8628. $my_item->appendChild($my_title);
  8629. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8630. //$my_item->appendChild($my_max_score);
  8631. // Give a child element <adlcp:prerequisites> to the <item> element.
  8632. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8633. $my_prereqs->setAttribute('type','aicc_script');
  8634. $my_item->appendChild($my_prereqs);
  8635. // Give a child element <adlcp:masteryscore> to the <item> element.
  8636. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8637. $my_item->appendChild($my_masteryscore);
  8638. // Attach this item to the organization element or hits parent if there is one.
  8639. if (!empty($item->parent) && $item->parent != 0) {
  8640. $children = $organization->childNodes;
  8641. for ($i = 0; $i < $children->length; $i++) {
  8642. $item_temp = $children->item($i);
  8643. if ($item_temp -> nodeName == 'item') {
  8644. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8645. $item_temp -> appendChild($my_item);
  8646. }
  8647. }
  8648. }
  8649. } else {
  8650. $organization->appendChild($my_item);
  8651. }
  8652. // Get the path of the file(s) from the course directory root
  8653. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8654. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8655. // Write the contents of the exported exercise into a (big) html file
  8656. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8657. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8658. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8659. $res = file_put_contents($tmp_file_path, $contents);
  8660. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8661. $files_cleanup[] = $tmp_file_path;
  8662. //error_log($tmp_path); die();
  8663. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8664. $my_xml_file_path = $my_file_path;
  8665. $my_sub_dir = dirname($my_file_path);
  8666. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8667. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8668. $my_xml_sub_dir = $my_sub_dir;
  8669. // Give a <resource> child to the <resources> element.
  8670. $my_resource = $xmldoc->createElement('resource');
  8671. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8672. $my_resource->setAttribute('type', 'webcontent');
  8673. $my_resource->setAttribute('href', $my_xml_file_path);
  8674. // adlcp:scormtype can be either 'sco' or 'asset'.
  8675. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8676. // xml:base is the base directory to find the files declared in this resource.
  8677. $my_resource->setAttribute('xml:base', '');
  8678. // Give a <file> child to the <resource> element.
  8679. $my_file = $xmldoc->createElement('file');
  8680. $my_file->setAttribute('href', $my_xml_file_path);
  8681. $my_resource->appendChild($my_file);
  8682. // Get included docs.
  8683. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8684. // Dependency to other files - not yet supported.
  8685. $i = 1;
  8686. foreach ($inc_docs as $doc_info) {
  8687. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8688. $my_dep = $xmldoc->createElement('resource');
  8689. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8690. $my_dep->setAttribute('identifier', $res_id);
  8691. $my_dep->setAttribute('type', 'webcontent');
  8692. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8693. $my_dep_file = $xmldoc->createElement('file');
  8694. // Check type of URL.
  8695. if ($doc_info[1] == 'remote') {
  8696. // Remote file. Save url as is.
  8697. $my_dep_file->setAttribute('href', $doc_info[0]);
  8698. $my_dep->setAttribute('xml:base', '');
  8699. } elseif ($doc_info[1] == 'local') {
  8700. switch ($doc_info[2]) {
  8701. case 'url': // Local URL - save path as url for now, don't zip file.
  8702. // Save file but as local file (retrieve from URL).
  8703. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8704. $current_dir = dirname($abs_path);
  8705. $current_dir = str_replace('\\', '/', $current_dir);
  8706. $file_path = realpath($abs_path);
  8707. $file_path = str_replace('\\', '/', $file_path);
  8708. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8709. $my_dep->setAttribute('xml:base', '');
  8710. if (strstr($file_path, $main_path) !== false) {
  8711. // The calculated real path is really inside the chamilo root path.
  8712. // Reduce file path to what's under the DocumentRoot.
  8713. $file_path = substr($file_path, strlen($root_path));
  8714. //echo $file_path;echo '<br /><br />';
  8715. //error_log('Reduced path: '.$file_path, 0);
  8716. $zip_files_abs[] = $file_path;
  8717. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8718. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8719. $my_dep->setAttribute('xml:base', '');
  8720. } elseif (empty($file_path)) {
  8721. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8722. if (strpos($document_root,-1) == '/') {
  8723. $document_root = substr(0, -1, $document_root);
  8724. }*/
  8725. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8726. $file_path = str_replace('//', '/', $file_path);
  8727. if (file_exists($file_path)) {
  8728. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8729. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8730. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8731. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8732. $my_dep->setAttribute('xml:base', '');
  8733. }
  8734. }
  8735. break;
  8736. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8737. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8738. $current_dir = str_replace('\\', '/', $current_dir);
  8739. $file_path = realpath($doc_info[0]);
  8740. $file_path = str_replace('\\', '/', $file_path);
  8741. $my_dep_file->setAttribute('href', $file_path);
  8742. $my_dep->setAttribute('xml:base', '');
  8743. if (strstr($file_path,$main_path) !== false) {
  8744. // The calculated real path is really inside the chamilo root path.
  8745. // Reduce file path to what's under the DocumentRoot.
  8746. $file_path = substr($file_path, strlen($root_path));
  8747. //echo $file_path;echo '<br /><br />';
  8748. //error_log('Reduced path: '.$file_path, 0);
  8749. $zip_files_abs[] = $file_path;
  8750. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8751. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8752. $my_dep->setAttribute('xml:base', '');
  8753. } elseif (empty($file_path)) {
  8754. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8755. if (strpos($document_root,-1) == '/') {
  8756. $document_root = substr(0, -1, $document_root);
  8757. }*/
  8758. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8759. $file_path = str_replace('//', '/', $file_path);
  8760. if (file_exists($file_path)) {
  8761. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8762. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8763. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8764. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8765. $my_dep->setAttribute('xml:base', '');
  8766. }
  8767. }
  8768. break;
  8769. 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
  8770. if (substr($doc_info[0], 0, 2) == '..') {
  8771. // Relative path going up.
  8772. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8773. $current_dir = str_replace('\\', '/', $current_dir);
  8774. $file_path = realpath($current_dir.$doc_info[0]);
  8775. $file_path = str_replace('\\', '/', $file_path);
  8776. //error_log($file_path.' <-> '.$main_path, 0);
  8777. if (strstr($file_path, $main_path) !== false) {
  8778. // The calculated real path is really inside Chamilo's root path.
  8779. // Reduce file path to what's under the DocumentRoot.
  8780. $file_path = substr($file_path, strlen($root_path));
  8781. $file_path_dest = $file_path;
  8782. // File path is courses/CHAMILO/document/....
  8783. $info_file_path = explode('/', $file_path);
  8784. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8785. $file_path_dest = 'document/'.$file_path;
  8786. }
  8787. //error_log('Reduced path: '.$file_path, 0);
  8788. $zip_files_abs[] = $file_path;
  8789. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8790. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8791. $my_dep->setAttribute('xml:base', '');
  8792. }
  8793. } else {
  8794. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8795. $my_dep_file->setAttribute('href', $doc_info[0]);
  8796. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8797. }
  8798. break;
  8799. default:
  8800. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8801. $my_dep->setAttribute('xml:base', '');
  8802. break;
  8803. }
  8804. }
  8805. $my_dep->appendChild($my_dep_file);
  8806. $resources->appendChild($my_dep);
  8807. $dependency = $xmldoc->createElement('dependency');
  8808. $dependency->setAttribute('identifierref', $res_id);
  8809. $my_resource->appendChild($dependency);
  8810. $i++;
  8811. }
  8812. $resources->appendChild($my_resource);
  8813. $zip_files[] = $my_file_path;
  8814. break;
  8815. default:
  8816. // Get the path of the file(s) from the course directory root
  8817. $my_file_path = 'non_exportable.html';
  8818. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8819. $my_xml_file_path = $my_file_path;
  8820. $my_sub_dir = dirname($my_file_path);
  8821. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8822. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8823. $my_xml_sub_dir = $my_sub_dir;
  8824. // Give a <resource> child to the <resources> element.
  8825. $my_resource = $xmldoc->createElement('resource');
  8826. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8827. $my_resource->setAttribute('type', 'webcontent');
  8828. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8829. // adlcp:scormtype can be either 'sco' or 'asset'.
  8830. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8831. // xml:base is the base directory to find the files declared in this resource.
  8832. $my_resource->setAttribute('xml:base', '');
  8833. // Give a <file> child to the <resource> element.
  8834. $my_file = $xmldoc->createElement('file');
  8835. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8836. $my_resource->appendChild($my_file);
  8837. $resources->appendChild($my_resource);
  8838. break;
  8839. }
  8840. }
  8841. }
  8842. $organizations->appendChild($organization);
  8843. $root->appendChild($organizations);
  8844. $root->appendChild($resources);
  8845. $xmldoc->appendChild($root);
  8846. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  8847. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8848. // then add the file to the zip, then destroy the file (this is done automatically).
  8849. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8850. foreach ($zip_files as $file_path) {
  8851. if (empty($file_path)) {
  8852. continue;
  8853. }
  8854. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  8855. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8856. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8857. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8858. }
  8859. $this->create_path($dest_file);
  8860. @copy($filePath, $dest_file);
  8861. // Check if the file needs a link update.
  8862. if (in_array($file_path, array_keys($link_updates))) {
  8863. $string = file_get_contents($dest_file);
  8864. unlink($dest_file);
  8865. foreach ($link_updates[$file_path] as $old_new) {
  8866. // This is an ugly hack that allows .flv files to be found by the flv player that
  8867. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8868. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8869. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8870. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8871. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8872. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8873. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8874. }
  8875. //Fix to avoid problems with default_course_document
  8876. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8877. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8878. $newDestination = $old_new['dest'];
  8879. } else {
  8880. $newDestination = str_replace('document/', '', $old_new['dest']);
  8881. }
  8882. $string = str_replace($old_new['orig'], $newDestination, $string);
  8883. // Add files inside the HTMLs
  8884. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  8885. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8886. if (file_exists($sys_course_path.$new_path)) {
  8887. copy($sys_course_path.$new_path, $destinationFile);
  8888. }
  8889. }
  8890. file_put_contents($dest_file, $string);
  8891. }
  8892. if (file_exists($filePath) && $copyAll) {
  8893. $extension = $this->get_extension($filePath);
  8894. if (in_array($extension, ['html', 'html'])) {
  8895. $containerOrigin = dirname($filePath);
  8896. $containerDestination = dirname($dest_file);
  8897. $finder = new Finder();
  8898. $finder->files()->in($containerOrigin)
  8899. ->notName('*_DELETED_*')
  8900. ->exclude('share_folder')
  8901. ->exclude('chat_files')
  8902. ->exclude('certificates')
  8903. ;
  8904. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  8905. $fs = new Filesystem();
  8906. $fs->mirror(
  8907. $containerOrigin,
  8908. $containerDestination,
  8909. $finder
  8910. );
  8911. }
  8912. }
  8913. }
  8914. }
  8915. foreach ($zip_files_abs as $file_path) {
  8916. if (empty($file_path)) {
  8917. continue;
  8918. }
  8919. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8920. continue;
  8921. }
  8922. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8923. $this->create_path($dest_file);
  8924. copy($main_path.$file_path, $dest_file);
  8925. // Check if the file needs a link update.
  8926. if (in_array($file_path, array_keys($link_updates))) {
  8927. $string = file_get_contents($dest_file);
  8928. unlink($dest_file);
  8929. foreach ($link_updates[$file_path] as $old_new) {
  8930. // This is an ugly hack that allows .flv files to be found by the flv player that
  8931. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8932. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8933. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8934. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8935. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8936. }
  8937. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8938. }
  8939. file_put_contents($dest_file, $string);
  8940. }
  8941. }
  8942. if (is_array($links_to_create)) {
  8943. foreach ($links_to_create as $file => $link) {
  8944. $file_content = '<!DOCTYPE html><head>
  8945. <meta charset="'.api_get_language_isocode().'" />
  8946. <title>'.$link['title'].'</title>
  8947. </head>
  8948. <body dir="'.api_get_text_direction().'">
  8949. <div style="text-align:center">
  8950. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8951. </body>
  8952. </html>';
  8953. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8954. }
  8955. }
  8956. // Add non exportable message explanation.
  8957. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8958. $file_content = '<!DOCTYPE html><head>
  8959. <meta charset="'.api_get_language_isocode().'" />
  8960. <title>'.$lang_not_exportable.'</title>
  8961. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8962. </head>
  8963. <body dir="'.api_get_text_direction().'">';
  8964. $file_content .=
  8965. <<<EOD
  8966. <style>
  8967. .error-message {
  8968. font-family: arial, verdana, helvetica, sans-serif;
  8969. border-width: 1px;
  8970. border-style: solid;
  8971. left: 50%;
  8972. margin: 10px auto;
  8973. min-height: 30px;
  8974. padding: 5px;
  8975. right: 50%;
  8976. width: 500px;
  8977. background-color: #FFD1D1;
  8978. border-color: #FF0000;
  8979. color: #000;
  8980. }
  8981. </style>
  8982. <body>
  8983. <div class="error-message">
  8984. $lang_not_exportable
  8985. </div>
  8986. </body>
  8987. </html>
  8988. EOD;
  8989. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8990. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8991. }
  8992. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8993. // Add the extra files that go along with a SCORM package.
  8994. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8995. $extra_files = scandir($main_code_path);
  8996. foreach ($extra_files as $extra_file) {
  8997. if (strpos($extra_file, '.') === 0) {
  8998. continue;
  8999. } else {
  9000. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9001. $this->create_path($dest_file);
  9002. copy($main_code_path.$extra_file, $dest_file);
  9003. }
  9004. }
  9005. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9006. $manifest = @$xmldoc->saveXML();
  9007. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9008. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9009. $zip_folder->add(
  9010. $archive_path.'/'.$temp_dir_short,
  9011. PCLZIP_OPT_REMOVE_PATH,
  9012. $archive_path.'/'.$temp_dir_short.'/'
  9013. );
  9014. // Clean possible temporary files.
  9015. foreach ($files_cleanup as $file) {
  9016. $res = unlink($file);
  9017. if ($res === false) {
  9018. error_log(
  9019. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9020. 0
  9021. );
  9022. }
  9023. }
  9024. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9025. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9026. }
  9027. /**
  9028. * @param int $lp_id
  9029. * @return bool
  9030. */
  9031. public function scorm_export_to_pdf($lp_id)
  9032. {
  9033. $lp_id = intval($lp_id);
  9034. $files_to_export = array();
  9035. $course_data = api_get_course_info($this->cc);
  9036. if (!empty($course_data)) {
  9037. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9038. $list = self::get_flat_ordered_items_list($lp_id);
  9039. if (!empty($list)) {
  9040. foreach ($list as $item_id) {
  9041. $item = $this->items[$item_id];
  9042. switch ($item->type) {
  9043. case 'document':
  9044. //Getting documents from a LP with chamilo documents
  9045. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9046. // Try loading document from the base course.
  9047. if (empty($file_data) && !empty($sessionId)) {
  9048. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9049. }
  9050. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9051. if (file_exists($file_path)) {
  9052. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9053. }
  9054. break;
  9055. case 'asset': //commes from a scorm package generated by chamilo
  9056. case 'sco':
  9057. $file_path = $scorm_path.'/'.$item->path;
  9058. if (file_exists($file_path)) {
  9059. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9060. }
  9061. break;
  9062. case 'dokeos_chapter':
  9063. case 'dir':
  9064. case 'chapter':
  9065. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9066. break;
  9067. }
  9068. }
  9069. }
  9070. $pdf = new PDF();
  9071. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9072. return $result;
  9073. }
  9074. return false;
  9075. }
  9076. /**
  9077. * Temp function to be moved in main_api or the best place around for this.
  9078. * Creates a file path if it doesn't exist
  9079. * @param string $path
  9080. */
  9081. public function create_path($path)
  9082. {
  9083. $path_bits = explode('/', dirname($path));
  9084. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9085. $path_built = IS_WINDOWS_OS ? '' : '/';
  9086. foreach ($path_bits as $bit) {
  9087. if (!empty ($bit)) {
  9088. $new_path = $path_built . $bit;
  9089. if (is_dir($new_path)) {
  9090. $path_built = $new_path . '/';
  9091. } else {
  9092. mkdir($new_path, api_get_permissions_for_new_directories());
  9093. $path_built = $new_path . '/';
  9094. }
  9095. }
  9096. }
  9097. }
  9098. /**
  9099. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9100. * @return boolean The results of the unlink function, or false if there was no image to start with
  9101. */
  9102. public function delete_lp_image()
  9103. {
  9104. $img = $this->get_preview_image();
  9105. if ($img != '') {
  9106. $del_file = $this->get_preview_image_path(null, 'sys');
  9107. if (isset($del_file) && file_exists($del_file)) {
  9108. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9109. if (file_exists($del_file_2)) {
  9110. unlink($del_file_2);
  9111. }
  9112. $this->set_preview_image('');
  9113. return @unlink($del_file);
  9114. }
  9115. }
  9116. return false;
  9117. }
  9118. /**
  9119. * Uploads an author image to the upload/learning_path/images directory
  9120. * @param array The image array, coming from the $_FILES superglobal
  9121. * @return boolean True on success, false on error
  9122. */
  9123. public function upload_image($image_array)
  9124. {
  9125. $image_moved = false;
  9126. if (!empty ($image_array['name'])) {
  9127. $upload_ok = process_uploaded_file($image_array);
  9128. $has_attachment = true;
  9129. } else {
  9130. $image_moved = true;
  9131. }
  9132. if ($upload_ok) {
  9133. if ($has_attachment) {
  9134. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9135. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9136. $updir = $sys_course_path . $courseDir;
  9137. // Try to add an extension to the file if it hasn't one.
  9138. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9139. if (!filter_extension($new_file_name)) {
  9140. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9141. $image_moved = false;
  9142. } else {
  9143. $file_extension = explode('.', $image_array['name']);
  9144. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9145. $filename = uniqid('');
  9146. $new_file_name = $filename.'.'.$file_extension;
  9147. $new_path = $updir.'/'.$new_file_name;
  9148. // Resize the image.
  9149. $temp = new Image($image_array['tmp_name']);
  9150. $temp->resize(104);
  9151. $result = $temp->send_image($new_path);
  9152. // Storing the image filename.
  9153. if ($result) {
  9154. $image_moved = true;
  9155. $this->set_preview_image($new_file_name);
  9156. //Resize to 64px to use on course homepage
  9157. $temp->resize(64);
  9158. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9159. return true;
  9160. }
  9161. }
  9162. }
  9163. }
  9164. return false;
  9165. }
  9166. /**
  9167. * @param int $lp_id
  9168. * @param string $status
  9169. */
  9170. public function set_autolaunch($lp_id, $status)
  9171. {
  9172. $course_id = api_get_course_int_id();
  9173. $lp_id = intval($lp_id);
  9174. $status = intval($status);
  9175. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9176. // Setting everything to autolaunch = 0
  9177. $attributes['autolaunch'] = 0;
  9178. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9179. Database::update($lp_table, $attributes, $where);
  9180. if ($status == 1) {
  9181. //Setting my lp_id to autolaunch = 1
  9182. $attributes['autolaunch'] = 1;
  9183. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9184. Database::update($lp_table, $attributes, $where );
  9185. }
  9186. }
  9187. /**
  9188. * Gets previous_item_id for the next element of the lp_item table
  9189. * @author Isaac flores paz
  9190. * @return integer Previous item ID
  9191. */
  9192. public function select_previous_item_id()
  9193. {
  9194. $course_id = api_get_course_int_id();
  9195. if ($this->debug > 0) {
  9196. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9197. }
  9198. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9199. // Get the max order of the items
  9200. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9201. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9202. $rs_max_order = Database::query($sql_max_order);
  9203. $row_max_order = Database::fetch_object($rs_max_order);
  9204. $max_order = $row_max_order->display_order;
  9205. // Get the previous item ID
  9206. $sql = "SELECT id as previous FROM $table_lp_item
  9207. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9208. $rs_max = Database::query($sql);
  9209. $row_max = Database::fetch_object($rs_max);
  9210. // Return the previous item ID
  9211. return $row_max->previous;
  9212. }
  9213. /**
  9214. * Copies an LP
  9215. */
  9216. public function copy()
  9217. {
  9218. $main_path = api_get_path(SYS_CODE_PATH);
  9219. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9220. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9221. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9222. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9223. //Course builder
  9224. $cb = new CourseBuilder();
  9225. //Setting tools that will be copied
  9226. $cb->set_tools_to_build(array('learnpaths'));
  9227. //Setting elements that will be copied
  9228. $cb->set_tools_specific_id_list(
  9229. array('learnpaths' => array($this->lp_id))
  9230. );
  9231. $course = $cb->build();
  9232. //Course restorer
  9233. $course_restorer = new CourseRestorer($course);
  9234. $course_restorer->set_add_text_in_items(true);
  9235. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9236. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9237. }
  9238. public function verify_document_size($s)
  9239. {
  9240. $post_max = ini_get('post_max_size');
  9241. if (substr($post_max, -1, 1) == 'M') {
  9242. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9243. } elseif (substr($post_max, -1, 1) == 'G') {
  9244. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9245. }
  9246. $upl_max = ini_get('upload_max_filesize');
  9247. if (substr($upl_max, -1, 1) == 'M') {
  9248. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9249. } elseif (substr($upl_max, -1, 1) == 'G') {
  9250. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9251. }
  9252. $documents_total_space = DocumentManager::documents_total_space();
  9253. $course_max_space = DocumentManager::get_course_quota();
  9254. $total_size = filesize($s) + $documents_total_space;
  9255. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9256. return true;
  9257. } else{
  9258. return false;
  9259. }
  9260. }
  9261. /**
  9262. * Clear LP prerequisites
  9263. */
  9264. public function clear_prerequisites()
  9265. {
  9266. $course_id = $this->get_course_int_id();
  9267. if ($this->debug > 0) {
  9268. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9269. }
  9270. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9271. $lp_id = $this->get_id();
  9272. //Cleaning prerequisites
  9273. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9274. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9275. Database::query($sql);
  9276. //Cleaning mastery score for exercises
  9277. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9278. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9279. Database::query($sql);
  9280. }
  9281. public function set_previous_step_as_prerequisite_for_all_items()
  9282. {
  9283. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9284. $course_id = $this->get_course_int_id();
  9285. $lp_id = $this->get_id();
  9286. if (!empty($this->items)) {
  9287. $previous_item_id = null;
  9288. $previous_item_max = 0;
  9289. $previous_item_type = null;
  9290. $last_item_not_chapter = null;
  9291. $last_item_not_chapter_type = null;
  9292. $last_item_not_chapter_max = null;
  9293. foreach ($this->items as $item) {
  9294. // if there was a previous item... (otherwise jump to set it)
  9295. if (!empty($previous_item_id)) {
  9296. $current_item_id = $item->get_id(); //save current id
  9297. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9298. // Current item is not a folder, so it qualifies to get a prerequisites
  9299. if ($last_item_not_chapter_type == 'quiz') {
  9300. // if previous is quiz, mark its max score as default score to be achieved
  9301. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9302. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9303. Database::query($sql);
  9304. }
  9305. // now simply update the prerequisite to set it to the last non-chapter item
  9306. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9307. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9308. Database::query($sql);
  9309. // record item as 'non-chapter' reference
  9310. $last_item_not_chapter = $item->get_id();
  9311. $last_item_not_chapter_type = $item->get_type();
  9312. $last_item_not_chapter_max = $item->get_max();
  9313. }
  9314. } else {
  9315. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9316. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9317. $last_item_not_chapter = $item->get_id();
  9318. $last_item_not_chapter_type = $item->get_type();
  9319. $last_item_not_chapter_max = $item->get_max();
  9320. }
  9321. }
  9322. // Saving the item as "previous item" for the next loop
  9323. $previous_item_id = $item->get_id();
  9324. $previous_item_max = $item->get_max();
  9325. $previous_item_type = $item->get_type();
  9326. }
  9327. }
  9328. }
  9329. /**
  9330. * @param array $params
  9331. */
  9332. public static function createCategory($params)
  9333. {
  9334. $em = Database::getManager();
  9335. $item = new CLpCategory();
  9336. $item->setName($params['name']);
  9337. $item->setCId($params['c_id']);
  9338. $em->persist($item);
  9339. $em->flush();
  9340. }
  9341. /**
  9342. * @param array $params
  9343. */
  9344. public static function updateCategory($params)
  9345. {
  9346. $em = Database::getManager();
  9347. /** @var CLpCategory $item */
  9348. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9349. if ($item) {
  9350. $item->setName($params['name']);
  9351. $em->merge($item);
  9352. $em->flush();
  9353. }
  9354. }
  9355. /**
  9356. * @param int $id
  9357. */
  9358. public static function moveUpCategory($id)
  9359. {
  9360. $em = Database::getManager();
  9361. /** @var CLpCategory $item */
  9362. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9363. if ($item) {
  9364. $position = $item->getPosition() - 1;
  9365. $item->setPosition($position);
  9366. $em->persist($item);
  9367. $em->flush();
  9368. }
  9369. }
  9370. /**
  9371. * @param int $id
  9372. */
  9373. public static function moveDownCategory($id)
  9374. {
  9375. $em = Database::getManager();
  9376. /** @var CLpCategory $item */
  9377. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9378. if ($item) {
  9379. $position = $item->getPosition() + 1;
  9380. $item->setPosition($position);
  9381. $em->persist($item);
  9382. $em->flush();
  9383. }
  9384. }
  9385. /**
  9386. * @param int $courseId
  9387. * @return int|mixed
  9388. */
  9389. public static function getCountCategories($courseId)
  9390. {
  9391. if (empty($course_id)) {
  9392. return 0;
  9393. }
  9394. $em = Database::getManager();
  9395. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9396. $query->setParameter('id', $courseId);
  9397. return $query->getSingleScalarResult();
  9398. }
  9399. /**
  9400. * @param int $courseId
  9401. *
  9402. * @return mixed
  9403. */
  9404. public static function getCategories($courseId)
  9405. {
  9406. $em = Database::getManager();
  9407. //Default behaviour
  9408. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9409. array('cId' => $course_id),
  9410. array('name' => 'ASC')
  9411. );*/
  9412. // Using doctrine extensions
  9413. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9414. array('cId' => $courseId)
  9415. )->getResult();
  9416. return $items;
  9417. }
  9418. /**
  9419. * @param int $id
  9420. *
  9421. * @return mixed
  9422. */
  9423. public static function getCategory($id)
  9424. {
  9425. $em = Database::getManager();
  9426. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9427. return $item;
  9428. }
  9429. /**
  9430. * @param int $courseId
  9431. * @return array
  9432. */
  9433. public static function getCategoryByCourse($courseId)
  9434. {
  9435. $em = Database::getManager();
  9436. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9437. return $items;
  9438. }
  9439. /**
  9440. * @param int $id
  9441. *
  9442. * @return mixed
  9443. */
  9444. public static function deleteCategory($id)
  9445. {
  9446. $em = Database::getManager();
  9447. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9448. if ($item) {
  9449. $courseId = $item->getCId();
  9450. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9451. $query->setParameter('id', $courseId);
  9452. $query->setParameter('catId', $item->getId());
  9453. $lps = $query->getResult();
  9454. // Setting category = 0.
  9455. if ($lps) {
  9456. foreach ($lps as $lpItem) {
  9457. $lpItem->setCategoryId(0);
  9458. }
  9459. }
  9460. // Removing category.
  9461. $em->remove($item);
  9462. $em->flush();
  9463. }
  9464. }
  9465. /**
  9466. * @param int $courseId
  9467. * @param bool $addSelectOption
  9468. *
  9469. * @return mixed
  9470. */
  9471. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9472. {
  9473. $items = self::getCategoryByCourse($courseId);
  9474. $cats = array();
  9475. if ($addSelectOption) {
  9476. $cats = array(get_lang('SelectACategory'));
  9477. }
  9478. if (!empty($items)) {
  9479. foreach ($items as $cat) {
  9480. $cats[$cat->getId()] = $cat->getName();
  9481. }
  9482. }
  9483. return $cats;
  9484. }
  9485. /**
  9486. * Return the scorm item type object with spaces replaced with _
  9487. * The return result is use to build a css classname like scorm_type_$return
  9488. * @param $in_type
  9489. * @return mixed
  9490. */
  9491. private static function format_scorm_type_item($in_type)
  9492. {
  9493. return str_replace(' ', '_', $in_type);
  9494. }
  9495. /**
  9496. * @return \learnpath
  9497. */
  9498. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9499. {
  9500. $lpObject = Session::read('lpobject');
  9501. $learnPath = null;
  9502. if (isset($lpObject)) {
  9503. $learnPath = unserialize($lpObject);
  9504. }
  9505. if (!is_object($learnPath)) {
  9506. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9507. }
  9508. return $learnPath;
  9509. }
  9510. /**
  9511. * @param int $itemId
  9512. * @return learnpathItem|false
  9513. */
  9514. public function getItem($itemId)
  9515. {
  9516. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9517. return $this->items[$itemId];
  9518. }
  9519. return false;
  9520. }
  9521. /**
  9522. * @return int
  9523. */
  9524. public function getCategoryId()
  9525. {
  9526. return $this->categoryId;
  9527. }
  9528. /**
  9529. * @param int $categoryId
  9530. * @return bool
  9531. */
  9532. public function setCategoryId($categoryId)
  9533. {
  9534. $this->categoryId = intval($categoryId);
  9535. $courseId = api_get_course_int_id();
  9536. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9537. $lp_id = $this->get_id();
  9538. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9539. WHERE c_id = $courseId AND id = $lp_id";
  9540. Database::query($sql);
  9541. return true;
  9542. }
  9543. /**
  9544. * Get whether this is a learning path with the possibility to subscribe
  9545. * users or not
  9546. * @return int
  9547. */
  9548. public function getSubscribeUsers()
  9549. {
  9550. return $this->subscribeUsers;
  9551. }
  9552. /**
  9553. * Set whether this is a learning path with the possibility to subscribe
  9554. * users or not
  9555. * @param int $subscribeUsers (0 = false, 1 = true)
  9556. */
  9557. public function setSubscribeUsers($value)
  9558. {
  9559. if ($this->debug > 0) {
  9560. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9561. }
  9562. $this->subscribeUsers = intval($value);;
  9563. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9564. $lp_id = $this->get_id();
  9565. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9566. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9567. Database::query($sql);
  9568. return true;
  9569. }
  9570. /**
  9571. * Calculate the count of stars for a user in this LP
  9572. * This calculation is based on the following rules:
  9573. * - the student gets one star when he gets to 50% of the learning path
  9574. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9575. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9576. * - the student gets the final star when the score for the *last* test is >= 80%
  9577. * @param int $sessionId Optional. The session ID
  9578. * @return int The count of stars
  9579. */
  9580. public function getCalculateStars($sessionId = 0)
  9581. {
  9582. $stars = 0;
  9583. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9584. if ($progress >= 50) {
  9585. $stars++;
  9586. }
  9587. // Calculate stars chapters evaluation
  9588. $exercisesItems = $this->getExercisesItems();
  9589. if (!empty($exercisesItems)) {
  9590. $totalResult = 0;
  9591. foreach ($exercisesItems as $exerciseItem) {
  9592. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9593. $this->user_id,
  9594. $exerciseItem->path,
  9595. $this->course_int_id,
  9596. $sessionId,
  9597. $this->lp_id,
  9598. $exerciseItem->db_id
  9599. );
  9600. $exerciseResultInfo = end($exerciseResultInfo);
  9601. if (!$exerciseResultInfo) {
  9602. continue;
  9603. }
  9604. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9605. $totalResult += $exerciseResult;
  9606. }
  9607. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9608. if ($totalExerciseAverage >= 50) {
  9609. $stars++;
  9610. }
  9611. if ($totalExerciseAverage >= 80) {
  9612. $stars++;
  9613. }
  9614. }
  9615. // Calculate star for final evaluation
  9616. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9617. if (!empty($finalEvaluationItem)) {
  9618. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9619. $this->user_id,
  9620. $finalEvaluationItem->path,
  9621. $this->course_int_id,
  9622. $sessionId,
  9623. $this->lp_id,
  9624. $finalEvaluationItem->db_id
  9625. );
  9626. $evaluationResultInfo = end($evaluationResultInfo);
  9627. if ($evaluationResultInfo) {
  9628. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9629. if ($evaluationResult >= 80) {
  9630. $stars++;
  9631. }
  9632. }
  9633. }
  9634. return $stars;
  9635. }
  9636. /**
  9637. * Get the items of exercise type
  9638. * @return array The items. Otherwise return false
  9639. */
  9640. public function getExercisesItems()
  9641. {
  9642. $exercises = [];
  9643. foreach ($this->items as $item) {
  9644. if ($item->type != 'quiz') {
  9645. continue;
  9646. }
  9647. $exercises[] = $item;
  9648. }
  9649. array_pop($exercises);
  9650. return $exercises;
  9651. }
  9652. /**
  9653. * Get the item of exercise type (evaluation type)
  9654. * @return array The final evaluation. Otherwise return false
  9655. */
  9656. public function getFinalEvaluationItem()
  9657. {
  9658. $exercises = [];
  9659. foreach ($this->items as $item) {
  9660. if ($item->type != 'quiz') {
  9661. continue;
  9662. }
  9663. $exercises[] = $item;
  9664. }
  9665. return array_pop($exercises);
  9666. }
  9667. /**
  9668. * Calculate the total points achieved for the current user in this learning path
  9669. * @param int $sessionId Optional. The session Id
  9670. * @return int
  9671. */
  9672. public function getCalculateScore($sessionId = 0)
  9673. {
  9674. // Calculate stars chapters evaluation
  9675. $exercisesItems = $this->getExercisesItems();
  9676. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9677. $totalExercisesResult = 0;
  9678. $totalEvaluationResult = 0;
  9679. if ($exercisesItems !== false) {
  9680. foreach ($exercisesItems as $exerciseItem) {
  9681. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9682. $this->user_id,
  9683. $exerciseItem->path,
  9684. $this->course_int_id,
  9685. $sessionId,
  9686. $this->lp_id,
  9687. $exerciseItem->db_id
  9688. );
  9689. $exerciseResultInfo = end($exerciseResultInfo);
  9690. if (!$exerciseResultInfo) {
  9691. continue;
  9692. }
  9693. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  9694. }
  9695. }
  9696. if (!empty($finalEvaluationItem)) {
  9697. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9698. $this->user_id,
  9699. $finalEvaluationItem->path,
  9700. $this->course_int_id,
  9701. $sessionId,
  9702. $this->lp_id,
  9703. $finalEvaluationItem->db_id
  9704. );
  9705. $evaluationResultInfo = end($evaluationResultInfo);
  9706. if ($evaluationResultInfo) {
  9707. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  9708. }
  9709. }
  9710. return $totalExercisesResult + $totalEvaluationResult;
  9711. }
  9712. /**
  9713. * Check if URL is not allowed to be show in a iframe
  9714. * @param string $src
  9715. *
  9716. * @return string
  9717. */
  9718. public function fixBlockedLinks($src)
  9719. {
  9720. $urlInfo = parse_url($src);
  9721. //$platformProtocol = api_get_protocol();
  9722. $platformProtocol = 'https';
  9723. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9724. $platformProtocol = 'http';
  9725. }
  9726. $protocolFixApplied = false;
  9727. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9728. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9729. if ($platformProtocol != $scheme) {
  9730. $_SESSION['x_frame_source'] = $src;
  9731. $src = 'blank.php?error=x_frames_options';
  9732. $protocolFixApplied = true;
  9733. }
  9734. if ($protocolFixApplied == false) {
  9735. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9736. // Check X-Frame-Options
  9737. $ch = curl_init();
  9738. $options = array(
  9739. CURLOPT_URL => $src,
  9740. CURLOPT_RETURNTRANSFER => true,
  9741. CURLOPT_HEADER => true,
  9742. CURLOPT_FOLLOWLOCATION => true,
  9743. CURLOPT_ENCODING => "",
  9744. CURLOPT_AUTOREFERER => true,
  9745. CURLOPT_CONNECTTIMEOUT => 120,
  9746. CURLOPT_TIMEOUT => 120,
  9747. CURLOPT_MAXREDIRS => 10,
  9748. );
  9749. curl_setopt_array($ch, $options);
  9750. $response = curl_exec($ch);
  9751. $httpCode = curl_getinfo($ch);
  9752. $headers = substr($response, 0, $httpCode['header_size']);
  9753. $error = false;
  9754. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  9755. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9756. ) {
  9757. $error = true;
  9758. }
  9759. if ($error) {
  9760. $_SESSION['x_frame_source'] = $src;
  9761. $src = 'blank.php?error=x_frames_options';
  9762. }
  9763. }
  9764. }
  9765. return $src;
  9766. }
  9767. /**
  9768. * Check if this LP has a created forum in the basis course
  9769. * @return boolean
  9770. */
  9771. public function lpHasForum()
  9772. {
  9773. $forumTable = Database::get_course_table(TABLE_FORUM);
  9774. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9775. $fakeFrom = "
  9776. $forumTable f
  9777. INNER JOIN $itemProperty ip
  9778. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9779. ";
  9780. $resultData = Database::select(
  9781. 'COUNT(f.iid) AS qty',
  9782. $fakeFrom,
  9783. [
  9784. 'where' => [
  9785. 'ip.visibility != ? AND ' => 2,
  9786. 'ip.tool = ? AND ' => TOOL_FORUM,
  9787. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9788. 'f.lp_id = ?' => intval($this->lp_id)
  9789. ]
  9790. ],
  9791. 'first'
  9792. );
  9793. if ($resultData['qty'] > 0) {
  9794. return true;
  9795. }
  9796. return false;
  9797. }
  9798. /**
  9799. * Get the forum for this learning path
  9800. * @return boolean
  9801. */
  9802. public function getForum($sessionId = 0)
  9803. {
  9804. $forumTable = Database::get_course_table(TABLE_FORUM);
  9805. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9806. $fakeFrom = "$forumTable f
  9807. INNER JOIN $itemProperty ip ";
  9808. if ($this->lp_session_id == 0) {
  9809. $fakeFrom .= "
  9810. ON (
  9811. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9812. f.session_id = ip.session_id OR ip.session_id IS NULL
  9813. )
  9814. )
  9815. ";
  9816. } else {
  9817. $fakeFrom .= "
  9818. ON (
  9819. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9820. )
  9821. ";
  9822. }
  9823. $resultData = Database::select(
  9824. 'f.*',
  9825. $fakeFrom,
  9826. [
  9827. 'where' => [
  9828. 'ip.visibility != ? AND ' => 2,
  9829. 'ip.tool = ? AND ' => TOOL_FORUM,
  9830. 'f.session_id = ? AND ' => $sessionId,
  9831. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9832. 'f.lp_id = ?' => intval($this->lp_id)
  9833. ]
  9834. ],
  9835. 'first'
  9836. );
  9837. if (empty($resultData)) {
  9838. return false;
  9839. }
  9840. return $resultData;
  9841. }
  9842. /**
  9843. * Create a forum for this learning path
  9844. * @param type $forumCategoryId
  9845. * @return int The forum ID if was created. Otherwise return false
  9846. */
  9847. public function createForum($forumCategoryId)
  9848. {
  9849. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9850. $forumId = store_forum(
  9851. [
  9852. 'lp_id' => $this->lp_id,
  9853. 'forum_title' => $this->name,
  9854. 'forum_comment' => null,
  9855. 'forum_category' => intval($forumCategoryId),
  9856. 'students_can_edit_group' => ['students_can_edit' => 0],
  9857. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9858. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9859. 'group_forum' => 0,
  9860. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9861. ],
  9862. [],
  9863. true
  9864. );
  9865. return $forumId;
  9866. }
  9867. /**
  9868. * Check and obtain the lp final item if exist
  9869. *
  9870. * @return array lp items
  9871. */
  9872. private function getFinalItem()
  9873. {
  9874. if (empty($this->items)) {
  9875. return null;
  9876. }
  9877. foreach ($this->items as $item) {
  9878. if ($item->type !== 'final_item') {
  9879. continue;
  9880. }
  9881. return $item;
  9882. }
  9883. }
  9884. /**
  9885. * Get the LP Final Item Template
  9886. *
  9887. * @return html
  9888. */
  9889. private function getFinalItemTemplate()
  9890. {
  9891. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'newscorm/final_item_template/template.html');
  9892. }
  9893. /**
  9894. * Get the LP Final Item Url
  9895. *
  9896. * @return String
  9897. */
  9898. private function getSavedFinalItem()
  9899. {
  9900. $finalItem = $this->getFinalItem();
  9901. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  9902. return file_get_contents($doc['absolute_path']);
  9903. }
  9904. /**
  9905. * Get the LP Final Item form
  9906. *
  9907. * @return html
  9908. */
  9909. public function getFinalItemForm()
  9910. {
  9911. $finalItem = $this->getFinalItem();
  9912. $title = '';
  9913. $content = '';
  9914. if ($finalItem) {
  9915. $title = $finalItem->title;
  9916. $buttonText = get_lang('Save');
  9917. $content = $this->getSavedFinalItem();
  9918. } else {
  9919. $buttonText = get_lang('LPCreateDocument');
  9920. $content = $this->getFinalItemTemplate();
  9921. }
  9922. $courseInfo = api_get_course_info();
  9923. $result = $this->generate_lp_folder($courseInfo);
  9924. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  9925. $relative_prefix = '../../';
  9926. $editorConfig = [
  9927. 'ToolbarSet' => 'LearningPathDocuments',
  9928. 'Width' => '100%',
  9929. 'Height' => '500',
  9930. 'FullPage' => true,
  9931. 'CreateDocumentDir' => $relative_prefix,
  9932. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  9933. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  9934. ];
  9935. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  9936. 'type' => 'document',
  9937. 'lp_id' => $this->lp_id
  9938. ]);
  9939. $form = new FormValidator('final_item', 'POST', $url);
  9940. $form->addText('title', get_lang('Title'));
  9941. $form->addButtonSave($buttonText);
  9942. $form->addHtml('<div class="alert alert-info">Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b></div>');
  9943. $renderer = $form->defaultRenderer();
  9944. $renderer->setElementTemplate('<div class="editor-lp">&nbsp;{label}{element}</div>', 'content_lp');
  9945. $form->addHtmlEditor('content_lp', null, null, true, $editorConfig, true);
  9946. $form->addHidden('action', 'add_final_item');
  9947. $form->addHidden('path', isset($_SESSION['pathItem']) ? $_SESSION['pathItem'] : '');
  9948. $form->addHidden('previous', $this->get_last());
  9949. $form->setDefaults(['title' => $title, 'content_lp' => $content]);
  9950. if ($form->validate()) {
  9951. $values = $form->exportValues();
  9952. $lastItemId = $this->get_last();
  9953. if (!$finalItem) {
  9954. $documentId = $this->create_document($this->course_info, $values['content_lp'], $values['title']);
  9955. $this->add_item(
  9956. 0,
  9957. $lastItemId,
  9958. 'final_item',
  9959. $documentId,
  9960. $values['title'],
  9961. ''
  9962. );
  9963. } else {
  9964. $this->edit_document($this->course_info);
  9965. }
  9966. }
  9967. return $form->returnForm();
  9968. }
  9969. }
  9970. if (!function_exists('trim_value')) {
  9971. function trim_value(& $value) {
  9972. $value = trim($value);
  9973. }
  9974. }