learnpath.class.php 458 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if (!empty($row['publicated_on'])) {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if (!empty($row['expired_on'])) {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $params = [
  202. 'c_id' => $course_id,
  203. 'lp_id' => $lp_id,
  204. 'user_id' => $user_id,
  205. 'view_count' => 1,
  206. 'session_id' => $session_id,
  207. 'last_item' => 0
  208. ];
  209. Database::insert($lp_table, $params);
  210. $this->lp_view_id = Database::insert_id();
  211. if ($this->debug > 2) {
  212. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  213. }
  214. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  215. Database::query($sql);
  216. }
  217. // Initialise items.
  218. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  219. $sql = "SELECT * FROM $lp_item_table
  220. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  221. ORDER BY parent_item_id, display_order";
  222. $res = Database::query($sql);
  223. if ($this->debug > 2) {
  224. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  225. error_log('-- Start while--', 0);
  226. }
  227. $lp_item_id_list = array();
  228. while ($row = Database::fetch_array($res)) {
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - ' .
  243. 'aicc object with id ' . $my_item_id .
  244. ' set in items[]',
  245. 0
  246. );
  247. }
  248. }
  249. break;
  250. case 2:
  251. $oItem = new scormItem('db', $row['id'], $course_id);
  252. if (is_object($oItem)) {
  253. $my_item_id = $oItem->get_id();
  254. $oItem->set_lp_view($this->lp_view_id, $course_id);
  255. $oItem->set_prevent_reinit($this->prevent_reinit);
  256. // Don't use reference here as the next loop will make the pointed object change.
  257. $this->items[$my_item_id] = $oItem;
  258. $this->refs_list[$oItem->ref] = $my_item_id;
  259. if ($this->debug > 2) {
  260. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  261. }
  262. }
  263. break;
  264. case 1:
  265. default:
  266. if ($this->debug > 2) {
  267. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  268. }
  269. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  270. if ($this->debug > 2) {
  271. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  272. }
  273. if (is_object($oItem)) {
  274. $my_item_id = $oItem->get_id();
  275. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  276. $oItem->set_prevent_reinit($this->prevent_reinit);
  277. // Don't use reference here as the next loop will make the pointed object change.
  278. $this->items[$my_item_id] = $oItem;
  279. $this->refs_list[$my_item_id] = $my_item_id;
  280. if ($this->debug > 2) {
  281. error_log(
  282. 'New LP - learnpath::__construct() ' . __LINE__ .
  283. ' - object with id ' . $my_item_id . ' set in items[]',
  284. 0);
  285. }
  286. }
  287. break;
  288. }
  289. // Setting the object level with variable $this->items[$i][parent]
  290. foreach ($this->items as $itemLPObject) {
  291. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  292. $itemLPObject->level = $level;
  293. }
  294. // Setting the view in the item object.
  295. if (is_object($this->items[$row['id']])) {
  296. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  297. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  298. $this->items[$row['id']]->current_start_time = 0;
  299. $this->items[$row['id']]->current_stop_time = 0;
  300. }
  301. }
  302. }
  303. if ($this->debug > 2) {
  304. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  305. }
  306. if (!empty($lp_item_id_list)) {
  307. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  308. if (!empty($lp_item_id_list_to_string)) {
  309. // Get last viewing vars.
  310. $lp_item_view_table = Database:: get_course_table(
  311. TABLE_LP_ITEM_VIEW
  312. );
  313. // This query should only return one or zero result.
  314. $sql = "SELECT lp_item_id, status
  315. FROM $lp_item_view_table
  316. WHERE
  317. c_id = $course_id AND
  318. lp_view_id = ".$this->lp_view_id." AND
  319. lp_item_id IN ('".$lp_item_id_list_to_string."')
  320. ORDER BY view_count DESC ";
  321. if ($this->debug > 2) {
  322. error_log(
  323. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  324. 0
  325. );
  326. }
  327. $status_list = array();
  328. $res = Database::query($sql);
  329. while ($row = Database:: fetch_array($res)) {
  330. $status_list[$row['lp_item_id']] = $row['status'];
  331. }
  332. foreach ($lp_item_id_list as $item_id) {
  333. if (isset($status_list[$item_id])) {
  334. $status = $status_list[$item_id];
  335. if (is_object($this->items[$item_id])) {
  336. $this->items[$item_id]->set_status($status);
  337. if (empty($status)) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. }
  343. } else {
  344. if (!api_is_invitee()) {
  345. if (is_object($this->items[$item_id])) {
  346. $this->items[$item_id]->set_status(
  347. $this->default_status
  348. );
  349. }
  350. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  351. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  352. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  353. if ($this->debug > 2) {
  354. error_log(
  355. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  356. 0
  357. );
  358. }
  359. $this->items[$item_id]->set_lp_view(
  360. $this->lp_view_id,
  361. $course_id
  362. );
  363. Database::query($sql);
  364. $insertId = Database::insert_id();
  365. $sql = "UPDATE $lp_item_view_table SET id = iid
  366. WHERE iid = $insertId";
  367. Database::query($sql);
  368. }
  369. }
  370. }
  371. }
  372. }
  373. $this->ordered_items = self::get_flat_ordered_items_list(
  374. $this->get_id(),
  375. 0,
  376. $course_id
  377. );
  378. $this->max_ordered_items = 0;
  379. foreach ($this->ordered_items as $index => $dummy) {
  380. if ($index > $this->max_ordered_items && !empty($dummy)) {
  381. $this->max_ordered_items = $index;
  382. }
  383. }
  384. // TODO: Define the current item better.
  385. $this->first();
  386. if ($this->debug > 2) {
  387. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  388. }
  389. return true;
  390. }
  391. /**
  392. * @return string
  393. */
  394. public function getCourseCode()
  395. {
  396. return $this->cc;
  397. }
  398. /**
  399. * @return int
  400. */
  401. public function get_course_int_id()
  402. {
  403. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  404. }
  405. /**
  406. * @param $course_id
  407. * @return int
  408. */
  409. public function set_course_int_id($course_id)
  410. {
  411. return $this->course_int_id = intval($course_id);
  412. }
  413. /**
  414. * Get the depth level of LP item
  415. * @param $in_tab_items
  416. * @param $in_current_item_id
  417. * @return int
  418. */
  419. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  420. {
  421. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  422. if ($parent_item_id == 0) {
  423. return 0;
  424. } else {
  425. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  426. }
  427. }
  428. /**
  429. * Function rewritten based on old_add_item() from Yannick Warnier.
  430. * Due the fact that users can decide where the item should come, I had to overlook this function and
  431. * I found it better to rewrite it. Old function is still available.
  432. * Added also the possibility to add a description.
  433. *
  434. * @param int $parent
  435. * @param int $previous
  436. * @param string $type
  437. * @param int resource ID (ref)
  438. * @param string $title
  439. * @param string $description
  440. * @param int $prerequisites
  441. * @param int $max_time_allowed
  442. * @param int $userId
  443. *
  444. * @return int
  445. */
  446. public function add_item(
  447. $parent,
  448. $previous,
  449. $type = 'dokeos_chapter',
  450. $id,
  451. $title,
  452. $description,
  453. $prerequisites = 0,
  454. $max_time_allowed = 0,
  455. $userId = 0
  456. ) {
  457. $course_id = $this->course_info['real_id'];
  458. if ($this->debug > 0) {
  459. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  460. }
  461. if (empty($course_id)) {
  462. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  463. $this->course_info = api_get_course_info($this->cc);
  464. $course_id = $this->course_info['real_id'];
  465. }
  466. $userId = empty($userId) ? api_get_user_id() : $userId;
  467. $sessionId = api_get_session_id();
  468. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  469. $_course = $this->course_info;
  470. $parent = intval($parent);
  471. $previous = intval($previous);
  472. $id = intval($id);
  473. $max_time_allowed = htmlentities($max_time_allowed);
  474. if (empty($max_time_allowed)) {
  475. $max_time_allowed = 0;
  476. }
  477. $sql = "SELECT COUNT(id) AS num
  478. FROM $tbl_lp_item
  479. WHERE
  480. c_id = $course_id AND
  481. lp_id = " . $this->get_id() . " AND
  482. parent_item_id = " . $parent;
  483. $res_count = Database::query($sql);
  484. $row = Database :: fetch_array($res_count);
  485. $num = $row['num'];
  486. if ($num > 0) {
  487. if ($previous == 0) {
  488. $sql = "SELECT id, next_item_id, display_order
  489. FROM " . $tbl_lp_item . "
  490. WHERE
  491. c_id = $course_id AND
  492. lp_id = " . $this->get_id() . " AND
  493. parent_item_id = " . $parent . " AND
  494. previous_item_id = 0 OR
  495. previous_item_id=" . $parent;
  496. $result = Database::query($sql);
  497. $row = Database :: fetch_array($result);
  498. $tmp_previous = 0;
  499. $next = $row['id'];
  500. $display_order = 0;
  501. } else {
  502. $previous = (int) $previous;
  503. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  504. FROM $tbl_lp_item
  505. WHERE
  506. c_id = $course_id AND
  507. lp_id = " . $this->get_id() . " AND
  508. id = " . $previous;
  509. $result = Database::query($sql);
  510. $row = Database:: fetch_array($result);
  511. $tmp_previous = $row['id'];
  512. $next = $row['next_item_id'];
  513. $display_order = $row['display_order'];
  514. }
  515. } else {
  516. $tmp_previous = 0;
  517. $next = 0;
  518. $display_order = 0;
  519. }
  520. $id = intval($id);
  521. $typeCleaned = Database::escape_string($type);
  522. if ($type == 'quiz') {
  523. $sql = 'SELECT SUM(ponderation)
  524. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  525. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  526. ON
  527. quiz_question.id = quiz_rel_question.question_id AND
  528. quiz_question.c_id = quiz_rel_question.c_id
  529. WHERE
  530. quiz_rel_question.exercice_id = '.$id." AND
  531. quiz_question.c_id = $course_id AND
  532. quiz_rel_question.c_id = $course_id ";
  533. $rsQuiz = Database::query($sql);
  534. $max_score = Database :: result($rsQuiz, 0, 0);
  535. // Disabling the exercise if we add it inside a LP
  536. $exercise = new Exercise($course_id);
  537. $exercise->read($id);
  538. $exercise->disable();
  539. $exercise->save();
  540. } else {
  541. $max_score = 100;
  542. }
  543. $params = array(
  544. "c_id" => $course_id,
  545. "lp_id" => $this->get_id(),
  546. "item_type" => $typeCleaned,
  547. "ref" => '',
  548. "title" => $title,
  549. "description" => $description,
  550. "path" => $id,
  551. "max_score" => $max_score,
  552. "parent_item_id" => $parent,
  553. "previous_item_id" => $previous,
  554. "next_item_id" => intval($next),
  555. "display_order" => $display_order + 1,
  556. "prerequisite" => $prerequisites,
  557. "max_time_allowed" => $max_time_allowed,
  558. 'min_score' => 0,
  559. 'launch_data' => '',
  560. );
  561. if ($prerequisites != 0) {
  562. $params['prerequisite'] = $prerequisites;
  563. }
  564. $new_item_id = Database::insert($tbl_lp_item, $params);
  565. if ($this->debug > 2) {
  566. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  567. }
  568. if ($new_item_id) {
  569. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. // Update the item that should come after the new item.
  572. $sql = " UPDATE $tbl_lp_item SET
  573. previous_item_id = $new_item_id,
  574. next_item_id = $new_item_id,
  575. id = $new_item_id
  576. WHERE iid = $new_item_id";
  577. Database::query($sql);
  578. // Update all the items after the new item.
  579. $sql = "UPDATE " . $tbl_lp_item . "
  580. SET display_order = display_order + 1
  581. WHERE
  582. c_id = $course_id AND
  583. lp_id = " . $this->get_id() . " AND
  584. id <> " . $new_item_id . " AND
  585. parent_item_id = " . $parent . " AND
  586. display_order > " . $display_order;
  587. Database::query($sql);
  588. // Update the item that should come after the new item.
  589. $sql = "UPDATE " . $tbl_lp_item . "
  590. SET ref = " . $new_item_id . "
  591. WHERE c_id = $course_id AND id = " . $new_item_id;
  592. Database::query($sql);
  593. // Upload audio.
  594. if (!empty($_FILES['mp3']['name'])) {
  595. // Create the audio folder if it does not exist yet.
  596. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  597. if (!is_dir($filepath . 'audio')) {
  598. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  599. $audio_id = add_document(
  600. $_course,
  601. '/audio',
  602. 'folder',
  603. 0,
  604. 'audio',
  605. '',
  606. 0,
  607. true,
  608. null,
  609. $sessionId,
  610. $userId
  611. );
  612. api_item_property_update(
  613. $_course,
  614. TOOL_DOCUMENT,
  615. $audio_id,
  616. 'FolderCreated',
  617. $userId,
  618. null,
  619. null,
  620. null,
  621. null,
  622. $sessionId
  623. );
  624. api_item_property_update(
  625. $_course,
  626. TOOL_DOCUMENT,
  627. $audio_id,
  628. 'invisible',
  629. $userId,
  630. null,
  631. null,
  632. null,
  633. null,
  634. $sessionId
  635. );
  636. }
  637. $file_path = handle_uploaded_document(
  638. $_course,
  639. $_FILES['mp3'],
  640. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  641. '/audio',
  642. $userId,
  643. '',
  644. '',
  645. '',
  646. '',
  647. false
  648. );
  649. // Getting the filename only.
  650. $file_components = explode('/', $file_path);
  651. $file = $file_components[count($file_components) - 1];
  652. // Store the mp3 file in the lp_item table.
  653. $sql = "UPDATE $tbl_lp_item SET
  654. audio = '" . Database::escape_string($file) . "'
  655. WHERE id = '" . intval($new_item_id) . "'";
  656. Database::query($sql);
  657. }
  658. }
  659. return $new_item_id;
  660. }
  661. /**
  662. * Static admin function allowing addition of a learnpath to a course.
  663. * @param string Course code
  664. * @param string Learnpath name
  665. * @param string Learnpath description string, if provided
  666. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  667. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  668. * @param string Zip file containing the learnpath or directory containing the learnpath
  669. * @return integer The new learnpath ID on success, 0 on failure
  670. */
  671. public static function add_lp(
  672. $courseCode,
  673. $name,
  674. $description = '',
  675. $learnpath = 'guess',
  676. $origin = 'zip',
  677. $zipname = '',
  678. $publicated_on = '',
  679. $expired_on = '',
  680. $categoryId = 0,
  681. $userId = 0
  682. ) {
  683. global $charset;
  684. if (!empty($courseCode)) {
  685. $courseInfo = api_get_course_info($courseCode);
  686. $course_id = $courseInfo['real_id'];
  687. } else {
  688. $course_id = api_get_course_int_id();
  689. $courseInfo = api_get_course_info();
  690. }
  691. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  692. // Check course code exists.
  693. // Check lp_name doesn't exist, otherwise append something.
  694. $i = 0;
  695. $name = Database::escape_string($name);
  696. $categoryId = intval($categoryId);
  697. // Session id.
  698. $session_id = api_get_session_id();
  699. $userId = empty($userId) ? api_get_user_id() : $userId;
  700. $check_name = "SELECT * FROM $tbl_lp
  701. WHERE c_id = $course_id AND name = '$name'";
  702. $res_name = Database::query($check_name);
  703. if (empty($publicated_on)) {
  704. //by default the publication date is the same that the creation date
  705. //The behaviour above was changed due BT#2800
  706. global $_custom;
  707. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  708. $publicated_on = '';
  709. } else {
  710. $publicated_on = api_get_utc_datetime();
  711. }
  712. } else {
  713. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  714. }
  715. if (empty($expired_on)) {
  716. $expired_on = null;
  717. } else {
  718. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  719. }
  720. while (Database :: num_rows($res_name)) {
  721. // There is already one such name, update the current one a bit.
  722. $i++;
  723. $name = $name . ' - ' . $i;
  724. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  725. $res_name = Database::query($check_name);
  726. }
  727. // New name does not exist yet; keep it.
  728. // Escape description.
  729. // Kevin: added htmlentities().
  730. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  731. $type = 1;
  732. switch ($learnpath) {
  733. case 'guess':
  734. break;
  735. case 'dokeos':
  736. case 'chamilo':
  737. $type = 1;
  738. break;
  739. case 'aicc':
  740. break;
  741. }
  742. switch ($origin) {
  743. case 'zip':
  744. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  745. break;
  746. case 'manual':
  747. default:
  748. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  749. $res_max = Database::query($get_max);
  750. if (Database :: num_rows($res_max) < 1) {
  751. $dsp = 1;
  752. } else {
  753. $row = Database :: fetch_array($res_max);
  754. $dsp = $row[0] + 1;
  755. }
  756. $params = [
  757. 'c_id' => $course_id,
  758. 'lp_type' => $type,
  759. 'name' => $name,
  760. 'description' => $description,
  761. 'path' => '',
  762. 'default_view_mod' => 'embedded',
  763. 'default_encoding' => 'UTF-8',
  764. 'display_order' => $dsp,
  765. 'content_maker' => 'Chamilo',
  766. 'content_local' => 'local',
  767. 'js_lib' => '',
  768. 'session_id' => $session_id,
  769. 'created_on' => api_get_utc_datetime(),
  770. 'modified_on' => api_get_utc_datetime(),
  771. 'publicated_on' => $publicated_on,
  772. 'expired_on' => $expired_on,
  773. 'category_id' => $categoryId,
  774. 'force_commit' => 0,
  775. 'content_license' => '',
  776. 'debug' => 0,
  777. 'theme' => '',
  778. 'preview_image' => '',
  779. 'author' => '',
  780. 'prerequisite' => 0,
  781. 'hide_toc_frame' => 0,
  782. 'seriousgame_mode' => 0,
  783. 'autolaunch' => 0,
  784. 'max_attempts' => 0,
  785. 'subscribe_users' => 0
  786. ];
  787. $id = Database::insert($tbl_lp, $params);
  788. if ($id > 0) {
  789. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  790. Database::query($sql);
  791. // Insert into item_property.
  792. api_item_property_update(
  793. $courseInfo,
  794. TOOL_LEARNPATH,
  795. $id,
  796. 'LearnpathAdded',
  797. $userId
  798. );
  799. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  800. return $id;
  801. }
  802. break;
  803. }
  804. }
  805. /**
  806. * Auto completes the parents of an item in case it's been completed or passed
  807. * @param integer $item Optional ID of the item from which to look for parents
  808. */
  809. public function autocomplete_parents($item)
  810. {
  811. $debug = $this->debug;
  812. if ($debug) {
  813. error_log('Learnpath::autocomplete_parents()', 0);
  814. }
  815. if (empty($item)) {
  816. $item = $this->current;
  817. }
  818. $currentItem = $this->getItem($item);
  819. if ($currentItem) {
  820. $parent_id = $currentItem->get_parent();
  821. $parent = $this->getItem($parent_id);
  822. if ($parent) {
  823. // if $item points to an object and there is a parent.
  824. if ($debug) {
  825. error_log(
  826. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  827. 0
  828. );
  829. }
  830. // New experiment including failed and browsed in completed status.
  831. //$current_status = $currentItem->get_status();
  832. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  833. // Fixes chapter auto complete
  834. if (true) {
  835. // If the current item is completed or passes or succeeded.
  836. $updateParentStatus = true;
  837. if ($debug) {
  838. error_log('Status of current item is alright', 0);
  839. }
  840. foreach ($parent->get_children() as $childItemId) {
  841. $childItem = $this->getItem($childItemId);
  842. // If children was not set try to get the info
  843. if (empty($childItem->db_item_view_id)) {
  844. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  845. }
  846. // Check all his brothers (parent's children) for completion status.
  847. if ($childItemId != $item) {
  848. if ($debug) {
  849. error_log(
  850. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  851. 0
  852. );
  853. }
  854. // Trying completing parents of failed and browsed items as well.
  855. if ($childItem->status_is(
  856. array(
  857. 'completed',
  858. 'passed',
  859. 'succeeded',
  860. 'browsed',
  861. 'failed'
  862. )
  863. )
  864. ) {
  865. // Keep completion status to true.
  866. continue;
  867. } else {
  868. if ($debug > 2) {
  869. error_log(
  870. '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,
  871. 0
  872. );
  873. }
  874. $updateParentStatus = false;
  875. break;
  876. }
  877. }
  878. }
  879. if ($updateParentStatus) {
  880. // If all the children were completed:
  881. $parent->set_status('completed');
  882. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  883. // Force the status to "completed"
  884. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  885. $this->update_queue[$parent->get_id()] = 'completed';
  886. if ($debug) {
  887. error_log(
  888. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  889. print_r($this->update_queue, 1),
  890. 0
  891. );
  892. }
  893. // Recursive call.
  894. $this->autocomplete_parents($parent->get_id());
  895. }
  896. }
  897. } else {
  898. if ($debug) {
  899. error_log("Parent #$parent_id does not exists");
  900. }
  901. }
  902. } else {
  903. if ($debug) {
  904. error_log("#$item is an item that doesn't have parents");
  905. }
  906. }
  907. }
  908. /**
  909. * Auto saves the current results into the database for the whole learnpath
  910. */
  911. public function autosave()
  912. {
  913. if ($this->debug > 0) {
  914. error_log('New LP - In learnpath::autosave()', 0);
  915. }
  916. // TODO: Add save operations for the learnpath itself.
  917. }
  918. /**
  919. * Closes the current resource
  920. *
  921. * Stops the timer
  922. * Saves into the database if required
  923. * Clears the current resource data from this object
  924. * @return boolean True on success, false on failure
  925. */
  926. public function close()
  927. {
  928. if ($this->debug > 0) {
  929. error_log('New LP - In learnpath::close()', 0);
  930. }
  931. if (empty ($this->lp_id)) {
  932. $this->error = 'Trying to close this learnpath but no ID is set';
  933. return false;
  934. }
  935. $this->current_time_stop = time();
  936. $this->ordered_items = array();
  937. $this->index = 0;
  938. unset ($this->lp_id);
  939. //unset other stuff
  940. return true;
  941. }
  942. /**
  943. * Static admin function allowing removal of a learnpath
  944. * @param array $courseInfo
  945. * @param integer Learnpath ID
  946. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  947. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  948. */
  949. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  950. {
  951. $course_id = api_get_course_int_id();
  952. if (!empty($courseInfo)) {
  953. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  954. }
  955. // TODO: Implement a way of getting this to work when the current object is not set.
  956. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  957. // If an ID is specifically given and the current LP is not the same, prevent delete.
  958. if (!empty ($id) && ($id != $this->lp_id)) {
  959. return false;
  960. }
  961. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  962. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  963. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  964. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  965. // Delete lp item id.
  966. foreach ($this->items as $id => $dummy) {
  967. $sql = "DELETE FROM $lp_item_view
  968. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  969. Database::query($sql);
  970. }
  971. // Proposed by Christophe (nickname: clefevre)
  972. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  973. Database::query($sql);
  974. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  975. Database::query($sql);
  976. self::toggle_publish($this->lp_id, 'i');
  977. if ($this->type == 2 || $this->type == 3) {
  978. // This is a scorm learning path, delete the files as well.
  979. $sql = "SELECT path FROM $lp
  980. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  981. $res = Database::query($sql);
  982. if (Database :: num_rows($res) > 0) {
  983. $row = Database :: fetch_array($res);
  984. $path = $row['path'];
  985. $sql = "SELECT id FROM $lp
  986. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  987. $res = Database::query($sql);
  988. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  989. if ($this->debug > 2) {
  990. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  991. }
  992. } else {
  993. // No other LP uses that directory, delete it.
  994. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  995. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  996. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  997. if ($this->debug > 2) {
  998. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  999. }
  1000. // Proposed by Christophe (clefevre).
  1001. if (strcmp(substr($path, -2), "/.") == 0) {
  1002. $path = substr($path, 0, -1); // Remove "." at the end.
  1003. }
  1004. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1005. rmdirr($course_scorm_dir . $path);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  1011. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1012. // Delete tools
  1013. $sql = "DELETE FROM $tbl_tool
  1014. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1015. Database::query($sql);
  1016. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1017. Database::query($sql);
  1018. // Updates the display order of all lps.
  1019. $this->update_display_order();
  1020. api_item_property_update(
  1021. api_get_course_info(),
  1022. TOOL_LEARNPATH,
  1023. $this->lp_id,
  1024. 'delete',
  1025. api_get_user_id()
  1026. );
  1027. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1028. if ($link_info !== false) {
  1029. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1030. }
  1031. if (api_get_setting('search_enabled') == 'true') {
  1032. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1033. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1034. }
  1035. }
  1036. /**
  1037. * Removes all the children of one item - dangerous!
  1038. * @param integer $id Element ID of which children have to be removed
  1039. * @return integer Total number of children removed
  1040. */
  1041. public function delete_children_items($id)
  1042. {
  1043. $course_id = $this->course_info['real_id'];
  1044. if ($this->debug > 0) {
  1045. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1046. }
  1047. $num = 0;
  1048. if (empty ($id) || $id != strval(intval($id))) {
  1049. return false;
  1050. }
  1051. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1052. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1053. $res = Database::query($sql);
  1054. while ($row = Database :: fetch_array($res)) {
  1055. $num += $this->delete_children_items($row['id']);
  1056. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1057. Database::query($sql_del);
  1058. $num++;
  1059. }
  1060. return $num;
  1061. }
  1062. /**
  1063. * Removes an item from the current learnpath
  1064. * @param integer $id Elem ID (0 if first)
  1065. * @param integer $remove Whether to remove the resource/data from the
  1066. * system or leave it (default: 'keep', others 'remove')
  1067. * @return integer Number of elements moved
  1068. * @todo implement resource removal
  1069. */
  1070. public function delete_item($id, $remove = 'keep')
  1071. {
  1072. $course_id = api_get_course_int_id();
  1073. if ($this->debug > 0) {
  1074. error_log('New LP - In learnpath::delete_item()', 0);
  1075. }
  1076. // TODO: Implement the resource removal.
  1077. if (empty ($id) || $id != strval(intval($id))) {
  1078. return false;
  1079. }
  1080. // First select item to get previous, next, and display order.
  1081. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1082. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1083. $res_sel = Database::query($sql_sel);
  1084. if (Database :: num_rows($res_sel) < 1) {
  1085. return false;
  1086. }
  1087. $row = Database :: fetch_array($res_sel);
  1088. $previous = $row['previous_item_id'];
  1089. $next = $row['next_item_id'];
  1090. $display = $row['display_order'];
  1091. $parent = $row['parent_item_id'];
  1092. $lp = $row['lp_id'];
  1093. // Delete children items.
  1094. $num = $this->delete_children_items($id);
  1095. if ($this->debug > 2) {
  1096. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1097. }
  1098. // Now delete the item.
  1099. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1100. if ($this->debug > 2) {
  1101. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1102. }
  1103. Database::query($sql_del);
  1104. // Now update surrounding items.
  1105. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1106. WHERE c_id = ".$course_id." AND id = $previous";
  1107. Database::query($sql_upd);
  1108. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1109. WHERE c_id = ".$course_id." AND id = $next";
  1110. Database::query($sql_upd);
  1111. // Now update all following items with new display order.
  1112. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1113. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1114. Database::query($sql_all);
  1115. //Removing prerequisites since the item will not longer exist
  1116. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1117. Database::query($sql_all);
  1118. // Remove from search engine if enabled.
  1119. if (api_get_setting('search_enabled') == 'true') {
  1120. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1121. $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';
  1122. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1123. $res = Database::query($sql);
  1124. if (Database :: num_rows($res) > 0) {
  1125. $row2 = Database :: fetch_array($res);
  1126. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1127. $di = new ChamiloIndexer();
  1128. $di->remove_document((int) $row2['search_did']);
  1129. }
  1130. $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';
  1131. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1132. Database::query($sql);
  1133. }
  1134. }
  1135. /**
  1136. * Updates an item's content in place
  1137. * @param integer $id Element ID
  1138. * @param integer $parent Parent item ID
  1139. * @param integer $previous Previous item ID
  1140. * @param string $title Item title
  1141. * @param string $description Item description
  1142. * @param string $prerequisites Prerequisites (optional)
  1143. * @param array $audio The array resulting of the $_FILES[mp3] element
  1144. * @param int $max_time_allowed
  1145. * @param string $url
  1146. * @return boolean True on success, false on error
  1147. */
  1148. public function edit_item(
  1149. $id,
  1150. $parent,
  1151. $previous,
  1152. $title,
  1153. $description,
  1154. $prerequisites = '0',
  1155. $audio = array(),
  1156. $max_time_allowed = 0,
  1157. $url = ''
  1158. ) {
  1159. $course_id = api_get_course_int_id();
  1160. $_course = api_get_course_info();
  1161. if ($this->debug > 0) {
  1162. error_log('New LP - In learnpath::edit_item()', 0);
  1163. }
  1164. if (empty ($max_time_allowed)) {
  1165. $max_time_allowed = 0;
  1166. }
  1167. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1168. return false;
  1169. }
  1170. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1171. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1172. $res_select = Database::query($sql_select);
  1173. $row_select = Database :: fetch_array($res_select);
  1174. $audio_update_sql = '';
  1175. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1176. // Create the audio folder if it does not exist yet.
  1177. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1178. if (!is_dir($filepath . 'audio')) {
  1179. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1180. $audio_id = add_document(
  1181. $_course,
  1182. '/audio',
  1183. 'folder',
  1184. 0,
  1185. 'audio'
  1186. );
  1187. api_item_property_update(
  1188. $_course,
  1189. TOOL_DOCUMENT,
  1190. $audio_id,
  1191. 'FolderCreated',
  1192. api_get_user_id(),
  1193. null,
  1194. null,
  1195. null,
  1196. null,
  1197. api_get_session_id()
  1198. );
  1199. api_item_property_update(
  1200. $_course,
  1201. TOOL_DOCUMENT,
  1202. $audio_id,
  1203. 'invisible',
  1204. api_get_user_id(),
  1205. null,
  1206. null,
  1207. null,
  1208. null,
  1209. api_get_session_id()
  1210. );
  1211. }
  1212. // Upload file in documents.
  1213. $pi = pathinfo($audio['name']);
  1214. if ($pi['extension'] == 'mp3') {
  1215. $c_det = api_get_course_info($this->cc);
  1216. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1217. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1218. $path = substr($path, 7);
  1219. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1220. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1221. }
  1222. }
  1223. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1224. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1225. // TODO: htmlspecialchars to be checked for encoding related problems.
  1226. if ($same_parent && $same_previous) {
  1227. // Only update title and description.
  1228. $sql = "UPDATE " . $tbl_lp_item . "
  1229. SET title = '" . Database::escape_string($title) . "',
  1230. prerequisite = '" . $prerequisites . "',
  1231. description = '" . Database::escape_string($description) . "'
  1232. " . $audio_update_sql . ",
  1233. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1234. WHERE c_id = ".$course_id." AND id = " . $id;
  1235. Database::query($sql);
  1236. } else {
  1237. $old_parent = $row_select['parent_item_id'];
  1238. $old_previous = $row_select['previous_item_id'];
  1239. $old_next = $row_select['next_item_id'];
  1240. $old_order = $row_select['display_order'];
  1241. $old_prerequisite = $row_select['prerequisite'];
  1242. $old_max_time_allowed = $row_select['max_time_allowed'];
  1243. /* BEGIN -- virtually remove the current item id */
  1244. /* for the next and previous item it is like the current item doesn't exist anymore */
  1245. if ($old_previous != 0) {
  1246. // Next
  1247. $sql = "UPDATE " . $tbl_lp_item . "
  1248. SET next_item_id = " . $old_next . "
  1249. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1250. Database::query($sql);
  1251. }
  1252. if ($old_next != 0) {
  1253. // Previous
  1254. $sql = "UPDATE " . $tbl_lp_item . "
  1255. SET previous_item_id = " . $old_previous . "
  1256. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1257. Database::query($sql);
  1258. }
  1259. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1260. $sql = "UPDATE " . $tbl_lp_item . "
  1261. SET display_order = display_order - 1
  1262. WHERE
  1263. c_id = ".$course_id." AND
  1264. display_order > " . $old_order . " AND
  1265. lp_id = " . $this->lp_id . " AND
  1266. parent_item_id = " . $old_parent;
  1267. Database::query($sql);
  1268. /* END -- virtually remove the current item id */
  1269. /* BEGIN -- update the current item id to his new location */
  1270. if ($previous == 0) {
  1271. // Select the data of the item that should come after the current item.
  1272. $sql = "SELECT id, display_order
  1273. FROM " . $tbl_lp_item . "
  1274. WHERE
  1275. c_id = ".$course_id." AND
  1276. lp_id = " . $this->lp_id . " AND
  1277. parent_item_id = " . $parent . " AND
  1278. previous_item_id = " . $previous;
  1279. $res_select_old = Database::query($sql);
  1280. $row_select_old = Database::fetch_array($res_select_old);
  1281. // If the new parent didn't have children before.
  1282. if (Database :: num_rows($res_select_old) == 0) {
  1283. $new_next = 0;
  1284. $new_order = 1;
  1285. } else {
  1286. $new_next = $row_select_old['id'];
  1287. $new_order = $row_select_old['display_order'];
  1288. }
  1289. } else {
  1290. // Select the data of the item that should come before the current item.
  1291. $sql = "SELECT next_item_id, display_order
  1292. FROM " . $tbl_lp_item . "
  1293. WHERE c_id = ".$course_id." AND id = " . $previous;
  1294. $res_select_old = Database::query($sql);
  1295. $row_select_old = Database :: fetch_array($res_select_old);
  1296. $new_next = $row_select_old['next_item_id'];
  1297. $new_order = $row_select_old['display_order'] + 1;
  1298. }
  1299. // TODO: htmlspecialchars to be checked for encoding related problems.
  1300. // Update the current item with the new data.
  1301. $sql = "UPDATE " . $tbl_lp_item . "
  1302. SET
  1303. title = '" . Database::escape_string($title) . "',
  1304. description = '" . Database::escape_string($description) . "',
  1305. parent_item_id = " . $parent . ",
  1306. previous_item_id = " . $previous . ",
  1307. next_item_id = " . $new_next . ",
  1308. display_order = " . $new_order . "
  1309. " . $audio_update_sql . "
  1310. WHERE c_id = ".$course_id." AND id = " . $id;
  1311. Database::query($sql);
  1312. if ($previous != 0) {
  1313. // Update the previous item's next_item_id.
  1314. $sql = "UPDATE " . $tbl_lp_item . "
  1315. SET next_item_id = " . $id . "
  1316. WHERE c_id = ".$course_id." AND id = " . $previous;
  1317. Database::query($sql);
  1318. }
  1319. if ($new_next != 0) {
  1320. // Update the next item's previous_item_id.
  1321. $sql = "UPDATE " . $tbl_lp_item . "
  1322. SET previous_item_id = " . $id . "
  1323. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1324. Database::query($sql);
  1325. }
  1326. if ($old_prerequisite != $prerequisites) {
  1327. $sql = "UPDATE " . $tbl_lp_item . "
  1328. SET prerequisite = '" . $prerequisites . "'
  1329. WHERE c_id = ".$course_id." AND id = " . $id;
  1330. Database::query($sql);
  1331. }
  1332. if ($old_max_time_allowed != $max_time_allowed) {
  1333. // update max time allowed
  1334. $sql = "UPDATE " . $tbl_lp_item . "
  1335. SET max_time_allowed = " . $max_time_allowed . "
  1336. WHERE c_id = ".$course_id." AND id = " . $id;
  1337. Database::query($sql);
  1338. }
  1339. // Update all the items with the same or a bigger display_order than the current item.
  1340. $sql = "UPDATE " . $tbl_lp_item . "
  1341. SET display_order = display_order + 1
  1342. WHERE
  1343. c_id = ".$course_id." AND
  1344. lp_id = " . $this->get_id() . " AND
  1345. id <> " . $id . " AND
  1346. parent_item_id = " . $parent . " AND
  1347. display_order >= " . $new_order;
  1348. Database::query($sql);
  1349. }
  1350. if ($row_select['item_type'] == 'link') {
  1351. $link = new Link();
  1352. $linkId = $row_select['path'];
  1353. $link->updateLink($linkId, $url);
  1354. }
  1355. }
  1356. /**
  1357. * Updates an item's prereq in place
  1358. * @param integer $id Element ID
  1359. * @param string $prerequisite_id Prerequisite Element ID
  1360. * @param int $mastery_score Prerequisite min score
  1361. * @param int $max_score Prerequisite max score
  1362. *
  1363. * @return boolean True on success, false on error
  1364. */
  1365. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1366. {
  1367. $course_id = api_get_course_int_id();
  1368. if ($this->debug > 0) {
  1369. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1370. }
  1371. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1372. return false;
  1373. }
  1374. $prerequisite_id = intval($prerequisite_id);
  1375. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1376. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1377. $mastery_score = 0;
  1378. }
  1379. if (!is_numeric($max_score) || $max_score < 0) {
  1380. $max_score = 100;
  1381. }
  1382. /*if ($mastery_score > $max_score) {
  1383. $max_score = $mastery_score;
  1384. }*/
  1385. if (!is_numeric($prerequisite_id)) {
  1386. $prerequisite_id = 'NULL';
  1387. }
  1388. $mastery_score = floatval($mastery_score);
  1389. $max_score = floatval($max_score);
  1390. $sql = " UPDATE $tbl_lp_item
  1391. SET
  1392. prerequisite = $prerequisite_id ,
  1393. prerequisite_min_score = $mastery_score ,
  1394. prerequisite_max_score = $max_score
  1395. WHERE c_id = $course_id AND id = $id";
  1396. Database::query($sql);
  1397. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1398. // Will this be enough to ensure unicity?
  1399. /*$sql = " UPDATE $tbl_lp_item
  1400. SET mastery_score = $mastery_score
  1401. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1402. Database::query($sql);*/
  1403. }
  1404. // TODO: Update the item object (can be ignored for now because refreshed).
  1405. return true;
  1406. }
  1407. /**
  1408. * Escapes a string with the available database escape function
  1409. * @param string String to escape
  1410. * @return string String escaped
  1411. * @deprecated use Database::escape_string
  1412. */
  1413. public function escape_string($string)
  1414. {
  1415. return Database::escape_string($string);
  1416. }
  1417. /**
  1418. * Static admin function exporting a learnpath into a zip file
  1419. * @param string Export type (scorm, zip, cd)
  1420. * @param string Course code
  1421. * @param integer Learnpath ID
  1422. * @param string Zip file name
  1423. * @return string Zip file path (or false on error)
  1424. */
  1425. public function export_lp($type, $course, $id, $zipname)
  1426. {
  1427. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1428. return false;
  1429. }
  1430. $url = '';
  1431. switch ($type) {
  1432. case 'scorm':
  1433. break;
  1434. case 'zip':
  1435. break;
  1436. case 'cdrom':
  1437. break;
  1438. }
  1439. return $url;
  1440. }
  1441. /**
  1442. * Gets all the chapters belonging to the same parent as the item/chapter given
  1443. * Can also be called as abstract method
  1444. * @param integer Item ID
  1445. * @return array A list of all the "brother items" (or an empty array on failure)
  1446. */
  1447. public function get_brother_chapters($id)
  1448. {
  1449. $course_id = api_get_course_int_id();
  1450. if ($this->debug > 0) {
  1451. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1452. }
  1453. if (empty($id)|| $id != strval(intval($id))) {
  1454. return array ();
  1455. }
  1456. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1457. $sql_parent = "SELECT * FROM $lp_item
  1458. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1459. $res_parent = Database::query($sql_parent);
  1460. if (Database :: num_rows($res_parent) > 0) {
  1461. $row_parent = Database :: fetch_array($res_parent);
  1462. $parent = $row_parent['parent_item_id'];
  1463. $sql_bros = "SELECT * FROM $lp_item
  1464. WHERE
  1465. c_id = ".$course_id." AND
  1466. parent_item_id = $parent AND
  1467. id = $id AND
  1468. item_type='dokeos_chapter'
  1469. ORDER BY display_order";
  1470. $res_bros = Database::query($sql_bros);
  1471. $list = array ();
  1472. while ($row_bro = Database :: fetch_array($res_bros)) {
  1473. $list[] = $row_bro;
  1474. }
  1475. return $list;
  1476. }
  1477. return array ();
  1478. }
  1479. /**
  1480. * Gets all the items belonging to the same parent as the item given
  1481. * Can also be called as abstract method
  1482. * @param integer Item ID
  1483. * @return array A list of all the "brother items" (or an empty array on failure)
  1484. */
  1485. public function get_brother_items($id)
  1486. {
  1487. $course_id = api_get_course_int_id();
  1488. if ($this->debug > 0) {
  1489. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1490. }
  1491. if (empty ($id) || $id != strval(intval($id))) {
  1492. return array ();
  1493. }
  1494. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1495. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1496. $res_parent = Database::query($sql_parent);
  1497. if (Database :: num_rows($res_parent) > 0) {
  1498. $row_parent = Database :: fetch_array($res_parent);
  1499. $parent = $row_parent['parent_item_id'];
  1500. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1501. ORDER BY display_order";
  1502. $res_bros = Database::query($sql_bros);
  1503. $list = array ();
  1504. while ($row_bro = Database :: fetch_array($res_bros)) {
  1505. $list[] = $row_bro;
  1506. }
  1507. return $list;
  1508. }
  1509. return array ();
  1510. }
  1511. /**
  1512. * Get the specific prefix index terms of this learning path
  1513. * @param string $prefix
  1514. * @return array Array of terms
  1515. */
  1516. public function get_common_index_terms_by_prefix($prefix)
  1517. {
  1518. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1519. $terms = get_specific_field_values_list_by_prefix(
  1520. $prefix,
  1521. $this->cc,
  1522. TOOL_LEARNPATH,
  1523. $this->lp_id
  1524. );
  1525. $prefix_terms = array();
  1526. if (!empty($terms)) {
  1527. foreach ($terms as $term) {
  1528. $prefix_terms[] = $term['value'];
  1529. }
  1530. }
  1531. return $prefix_terms;
  1532. }
  1533. /**
  1534. * Gets the number of items currently completed
  1535. * @return integer The number of items currently completed
  1536. */
  1537. public function get_complete_items_count()
  1538. {
  1539. if ($this->debug > 0) {
  1540. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1541. }
  1542. $i = 0;
  1543. $completedStatusList = array(
  1544. 'completed',
  1545. 'passed',
  1546. 'succeeded',
  1547. 'browsed',
  1548. 'failed'
  1549. );
  1550. foreach ($this->items as $id => $dummy) {
  1551. // Trying failed and browsed considered "progressed" as well.
  1552. if ($this->items[$id]->status_is($completedStatusList) &&
  1553. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1554. $this->items[$id]->get_type() != 'dir'
  1555. ) {
  1556. $i++;
  1557. }
  1558. }
  1559. return $i;
  1560. }
  1561. /**
  1562. * Gets the current item ID
  1563. * @return integer The current learnpath item id
  1564. */
  1565. public function get_current_item_id()
  1566. {
  1567. $current = 0;
  1568. if ($this->debug > 0) {
  1569. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1570. }
  1571. if (!empty($this->current)) {
  1572. $current = $this->current;
  1573. }
  1574. if ($this->debug > 2) {
  1575. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1576. }
  1577. return $current;
  1578. }
  1579. /**
  1580. * Force to get the first learnpath item id
  1581. * @return integer The current learnpath item id
  1582. */
  1583. public function get_first_item_id()
  1584. {
  1585. $current = 0;
  1586. if (is_array($this->ordered_items)) {
  1587. $current = $this->ordered_items[0];
  1588. }
  1589. return $current;
  1590. }
  1591. /**
  1592. * Gets the total number of items available for viewing in this SCORM
  1593. * @return integer The total number of items
  1594. */
  1595. public function get_total_items_count()
  1596. {
  1597. if ($this->debug > 0) {
  1598. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1599. }
  1600. return count($this->items);
  1601. }
  1602. /**
  1603. * Gets the total number of items available for viewing in this SCORM but without chapters
  1604. * @return integer The total no-chapters number of items
  1605. */
  1606. public function get_total_items_count_without_chapters()
  1607. {
  1608. if ($this->debug > 0) {
  1609. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1610. }
  1611. $total = 0;
  1612. $typeListNotToCount = self::getChapterTypes();
  1613. foreach ($this->items as $temp2) {
  1614. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1615. $total++;
  1616. }
  1617. }
  1618. return $total;
  1619. }
  1620. /**
  1621. * Gets the first element URL.
  1622. * @return string URL to load into the viewer
  1623. */
  1624. public function first()
  1625. {
  1626. if ($this->debug > 0) {
  1627. error_log('New LP - In learnpath::first()', 0);
  1628. error_log('$this->last_item_seen '.$this->last_item_seen);
  1629. }
  1630. // Test if the last_item_seen exists and is not a dir.
  1631. if (count($this->ordered_items) == 0) {
  1632. $this->index = 0;
  1633. }
  1634. if ($this->debug > 0) {
  1635. if (isset($this->items[$this->last_item_seen])) {
  1636. $status = $this->items[$this->last_item_seen]->get_status();
  1637. }
  1638. error_log('status '.$status);
  1639. }
  1640. if (!empty($this->last_item_seen) &&
  1641. !empty($this->items[$this->last_item_seen]) &&
  1642. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1643. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1644. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1645. //&& !$this->items[$this->last_item_seen]->is_done()
  1646. ) {
  1647. if ($this->debug > 2) {
  1648. 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);
  1649. }
  1650. $index = -1;
  1651. foreach ($this->ordered_items as $myindex => $item_id) {
  1652. if ($item_id == $this->last_item_seen) {
  1653. $index = $myindex;
  1654. break;
  1655. }
  1656. }
  1657. if ($index == -1) {
  1658. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1659. if ($this->debug > 2) {
  1660. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1661. }
  1662. return false;
  1663. } else {
  1664. $this->last = $this->last_item_seen;
  1665. $this->current = $this->last_item_seen;
  1666. $this->index = $index;
  1667. }
  1668. } else {
  1669. if ($this->debug > 2) {
  1670. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1671. }
  1672. $index = 0;
  1673. // Loop through all ordered items and stop at the first item that is
  1674. // not a directory *and* that has not been completed yet.
  1675. while ( !empty($this->ordered_items[$index]) AND
  1676. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1677. (
  1678. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1679. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1680. $this->items[$this->ordered_items[$index]]->is_done() === true
  1681. ) AND $index < $this->max_ordered_items) {
  1682. $index++;
  1683. }
  1684. $this->last = $this->current;
  1685. // current is
  1686. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1687. $this->index = $index;
  1688. if ($this->debug > 2) {
  1689. error_log('$index ' . $index);
  1690. }
  1691. if ($this->debug > 2) {
  1692. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1693. }
  1694. }
  1695. if ($this->debug > 2) {
  1696. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1697. }
  1698. }
  1699. /**
  1700. * Gets the information about an item in a format usable as JavaScript to update
  1701. * the JS API by just printing this content into the <head> section of the message frame
  1702. * @param int $item_id
  1703. * @return string
  1704. */
  1705. public function get_js_info($item_id = '')
  1706. {
  1707. if ($this->debug > 0) {
  1708. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1709. }
  1710. $info = '';
  1711. $item_id = intval($item_id);
  1712. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1713. //if item is defined, return values from DB
  1714. $oItem = $this->items[$item_id];
  1715. $info .= '<script language="javascript">';
  1716. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1717. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1718. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1719. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1720. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1721. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1722. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1723. $info .= "top.set_flag_synchronized();";
  1724. $info .= '</script>';
  1725. if ($this->debug > 2) {
  1726. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1727. }
  1728. return $info;
  1729. } else {
  1730. // If item_id is empty, just update to default SCORM data.
  1731. $info .= '<script language="javascript">';
  1732. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1733. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1734. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1735. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1736. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1737. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1738. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1739. $info .= "top.set_flag_synchronized();";
  1740. $info .= '</script>';
  1741. if ($this->debug > 2) {
  1742. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1743. }
  1744. return $info;
  1745. }
  1746. }
  1747. /**
  1748. * Gets the js library from the database
  1749. * @return string The name of the javascript library to be used
  1750. */
  1751. public function get_js_lib()
  1752. {
  1753. $lib = '';
  1754. if (!empty ($this->js_lib)) {
  1755. $lib = $this->js_lib;
  1756. }
  1757. return $lib;
  1758. }
  1759. /**
  1760. * Gets the learnpath database ID
  1761. * @return integer Learnpath ID in the lp table
  1762. */
  1763. public function get_id()
  1764. {
  1765. if (!empty ($this->lp_id)) {
  1766. return $this->lp_id;
  1767. } else {
  1768. return 0;
  1769. }
  1770. }
  1771. /**
  1772. * Gets the last element URL.
  1773. * @return string URL to load into the viewer
  1774. */
  1775. public function get_last()
  1776. {
  1777. if ($this->debug > 0) {
  1778. error_log('New LP - In learnpath::get_last()', 0);
  1779. }
  1780. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1781. if (count($this->ordered_items) > 0) {
  1782. $this->index = count($this->ordered_items) - 1;
  1783. return $this->ordered_items[$this->index];
  1784. }
  1785. return false;
  1786. }
  1787. /**
  1788. * Gets the navigation bar for the learnpath display screen
  1789. * @return string The HTML string to use as a navigation bar
  1790. */
  1791. public function get_navigation_bar($idBar = null, $display=null) {
  1792. if ($this->debug > 0) {
  1793. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1794. }
  1795. if(empty($idBar)){
  1796. $idBar='control-top';
  1797. }
  1798. /* if(empty($display)){
  1799. $display='display:block';
  1800. } */
  1801. $navbar = null;
  1802. $lp_id = $this->lp_id;
  1803. $mycurrentitemid = $this->get_current_item_id();
  1804. if ($this->mode == 'fullscreen') {
  1805. $navbar = '
  1806. <span id="'.$idBar.'" class="buttons">
  1807. <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">
  1808. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1809. </a>
  1810. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1811. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1812. </a>
  1813. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1814. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1815. </a>
  1816. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1817. <span class="fa fa-columns"></span><span class="sr-only">' . get_lang('ScormExitFullScreen') . '</span>
  1818. </a>
  1819. </span>';
  1820. } else {
  1821. $navbar = '
  1822. <span id="'.$idBar.'" class="buttons text-right">
  1823. <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">
  1824. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1825. </a>
  1826. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1827. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1828. </a>
  1829. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1830. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1831. </a>
  1832. </span>';
  1833. }
  1834. return $navbar;
  1835. }
  1836. /**
  1837. * Gets the next resource in queue (url).
  1838. * @return string URL to load into the viewer
  1839. */
  1840. public function get_next_index()
  1841. {
  1842. if ($this->debug > 0) {
  1843. error_log('New LP - In learnpath::get_next_index()', 0);
  1844. }
  1845. // TODO
  1846. $index = $this->index;
  1847. $index++;
  1848. if ($this->debug > 2) {
  1849. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1850. }
  1851. 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) {
  1852. $index++;
  1853. if ($index == $this->max_ordered_items){
  1854. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1855. return $this->index;
  1856. } else {
  1857. return $index;
  1858. }
  1859. }
  1860. }
  1861. if (empty ($this->ordered_items[$index])) {
  1862. return $this->index;
  1863. }
  1864. if ($this->debug > 2) {
  1865. error_log('New LP - index is now ' . $index, 0);
  1866. }
  1867. return $index;
  1868. }
  1869. /**
  1870. * Gets item_id for the next element
  1871. * @return integer Next item (DB) ID
  1872. */
  1873. public function get_next_item_id()
  1874. {
  1875. if ($this->debug > 0) {
  1876. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1877. }
  1878. $new_index = $this->get_next_index();
  1879. if (!empty ($new_index)) {
  1880. if (isset ($this->ordered_items[$new_index])) {
  1881. if ($this->debug > 2) {
  1882. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1883. }
  1884. return $this->ordered_items[$new_index];
  1885. }
  1886. }
  1887. if ($this->debug > 2) {
  1888. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1889. }
  1890. return 0;
  1891. }
  1892. /**
  1893. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1894. *
  1895. * Generally, the package provided is in the form of a zip file, so the function
  1896. * has been written to test a zip file. If not a zip, the function will return the
  1897. * default return value: ''
  1898. * @param string the path to the file
  1899. * @param string the original name of the file
  1900. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1901. */
  1902. public static function get_package_type($file_path, $file_name)
  1903. {
  1904. // Get name of the zip file without the extension.
  1905. $file_info = pathinfo($file_name);
  1906. $filename = $file_info['basename']; // Name including extension.
  1907. $extension = $file_info['extension']; // Extension only.
  1908. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1909. 'dll',
  1910. 'exe'
  1911. ))) {
  1912. return 'oogie';
  1913. }
  1914. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1915. 'dll',
  1916. 'exe'
  1917. ))) {
  1918. return 'woogie';
  1919. }
  1920. // Filename without its extension.
  1921. $file_base_name = str_replace('.' . $extension, '', $filename);
  1922. $zipFile = new PclZip($file_path);
  1923. // Check the zip content (real size and file extension).
  1924. $zipContentArray = $zipFile->listContent();
  1925. $package_type = '';
  1926. $at_root = false;
  1927. $manifest = '';
  1928. $aicc_match_crs = 0;
  1929. $aicc_match_au = 0;
  1930. $aicc_match_des = 0;
  1931. $aicc_match_cst = 0;
  1932. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1933. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1934. foreach ($zipContentArray as $thisContent) {
  1935. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1936. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1937. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1938. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1939. $package_type = 'scorm';
  1940. break; // Exit the foreach loop.
  1941. } elseif (
  1942. preg_match('/aicc\//i', $thisContent['filename']) ||
  1943. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1944. ) {
  1945. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1946. switch ($ext) {
  1947. case 'crs':
  1948. $aicc_match_crs = 1;
  1949. break;
  1950. case 'au':
  1951. $aicc_match_au = 1;
  1952. break;
  1953. case 'des':
  1954. $aicc_match_des = 1;
  1955. break;
  1956. case 'cst':
  1957. $aicc_match_cst = 1;
  1958. break;
  1959. default:
  1960. break;
  1961. }
  1962. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1963. } else {
  1964. $package_type = '';
  1965. }
  1966. }
  1967. }
  1968. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1969. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1970. $package_type = 'aicc';
  1971. }
  1972. return $package_type;
  1973. }
  1974. /**
  1975. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1976. * @return string URL to load into the viewer
  1977. */
  1978. public function get_previous_index()
  1979. {
  1980. if ($this->debug > 0) {
  1981. error_log('New LP - In learnpath::get_previous_index()', 0);
  1982. }
  1983. $index = $this->index;
  1984. if (isset ($this->ordered_items[$index -1])) {
  1985. $index--;
  1986. 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')) {
  1987. $index--;
  1988. if ($index < 0) {
  1989. return $this->index;
  1990. }
  1991. }
  1992. } else {
  1993. if ($this->debug > 2) {
  1994. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1995. }
  1996. // There is no previous item.
  1997. }
  1998. return $index;
  1999. }
  2000. /**
  2001. * Gets item_id for the next element
  2002. * @return integer Previous item (DB) ID
  2003. */
  2004. public function get_previous_item_id()
  2005. {
  2006. if ($this->debug > 0) {
  2007. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2008. }
  2009. $new_index = $this->get_previous_index();
  2010. return $this->ordered_items[$new_index];
  2011. }
  2012. /**
  2013. * Gets the progress value from the progress_db attribute
  2014. * @return integer Current progress value
  2015. */
  2016. public function get_progress()
  2017. {
  2018. if ($this->debug > 0) {
  2019. error_log('New LP - In learnpath::get_progress()', 0);
  2020. }
  2021. if (!empty ($this->progress_db)) {
  2022. return $this->progress_db;
  2023. }
  2024. return 0;
  2025. }
  2026. /**
  2027. * Returns the HTML necessary to print a mediaplayer block inside a page
  2028. * @return string The mediaplayer HTML
  2029. */
  2030. public function get_mediaplayer($autostart = 'true')
  2031. {
  2032. $course_id = api_get_course_int_id();
  2033. $_course = api_get_course_info();
  2034. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2035. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2036. // Getting all the information about the item.
  2037. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2038. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2039. ON lp.id = lp_view.lp_item_id
  2040. WHERE
  2041. lp.id = '".$_SESSION['oLP']->current."' AND
  2042. lp.c_id = $course_id AND
  2043. lp_view.c_id = $course_id";
  2044. $result = Database::query($sql);
  2045. $row = Database::fetch_assoc($result);
  2046. $output = '';
  2047. if (!empty ($row['audio'])) {
  2048. $list = $_SESSION['oLP']->get_toc();
  2049. $type_quiz = false;
  2050. foreach($list as $toc) {
  2051. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2052. $type_quiz = true;
  2053. }
  2054. }
  2055. if ($type_quiz) {
  2056. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2057. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2058. } else {
  2059. $autostart_audio = $autostart;
  2060. }
  2061. } else {
  2062. $autostart_audio = 'true';
  2063. }
  2064. $courseInfo = api_get_course_info();
  2065. $audio = $row['audio'];
  2066. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2067. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2068. if (!file_exists($file)) {
  2069. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2070. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2071. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2072. }
  2073. $player = Display::getMediaPlayer(
  2074. $file,
  2075. array(
  2076. 'id' => 'lp_audio_media_player',
  2077. 'url' => $url,
  2078. 'autoplay' => $autostart_audio,
  2079. 'width' => '100%'
  2080. )
  2081. );
  2082. // The mp3 player.
  2083. $output = '<div id="container">';
  2084. $output .= $player;
  2085. $output .= '</div>';
  2086. }
  2087. return $output;
  2088. }
  2089. /**
  2090. * Checks if the learning path is visible for student after the progress
  2091. * of its prerequisite is completed, considering the time availability and
  2092. * the LP visibility.
  2093. * @param int $lp_id
  2094. * @param int $student_id
  2095. * @param string Course code (optional)
  2096. * @param int $sessionId
  2097. * @return bool
  2098. */
  2099. public static function is_lp_visible_for_student(
  2100. $lp_id,
  2101. $student_id,
  2102. $courseCode = null,
  2103. $sessionId = null
  2104. ) {
  2105. $lp_id = (int)$lp_id;
  2106. $courseInfo = api_get_course_info($courseCode);
  2107. $sessionId = intval($sessionId);
  2108. if (empty($sessionId)) {
  2109. $sessionId = api_get_session_id();
  2110. }
  2111. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2112. // Get current prerequisite
  2113. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2114. FROM $tbl_learnpath
  2115. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2116. $itemInfo = api_get_item_property_info(
  2117. $courseInfo['real_id'],
  2118. TOOL_LEARNPATH,
  2119. $lp_id,
  2120. $sessionId
  2121. );
  2122. // If the item was deleted.
  2123. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2124. return false;
  2125. }
  2126. $rs = Database::query($sql);
  2127. $now = time();
  2128. if (Database::num_rows($rs) > 0) {
  2129. $row = Database::fetch_array($rs, 'ASSOC');
  2130. $prerequisite = $row['prerequisite'];
  2131. $is_visible = true;
  2132. if (!empty($prerequisite)) {
  2133. $progress = self::getProgress(
  2134. $prerequisite,
  2135. $student_id,
  2136. $courseInfo['real_id'],
  2137. $sessionId
  2138. );
  2139. $progress = intval($progress);
  2140. if ($progress < 100) {
  2141. $is_visible = false;
  2142. }
  2143. }
  2144. // Also check the time availability of the LP
  2145. if ($is_visible) {
  2146. // Adding visibility restrictions
  2147. if (!empty($row['publicated_on'])) {
  2148. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2149. //api_not_allowed();
  2150. $is_visible = false;
  2151. }
  2152. }
  2153. // Blocking empty start times see BT#2800
  2154. global $_custom;
  2155. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2156. $_custom['lps_hidden_when_no_start_date']
  2157. ) {
  2158. if (empty($row['publicated_on'])) {
  2159. //api_not_allowed();
  2160. $is_visible = false;
  2161. }
  2162. }
  2163. if (!empty($row['expired_on'])) {
  2164. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2165. //api_not_allowed();
  2166. $is_visible = false;
  2167. }
  2168. }
  2169. }
  2170. // Check if the subscription users/group to a LP is ON
  2171. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2172. // Try group
  2173. $is_visible = false;
  2174. // Checking only the user visibility
  2175. $userVisibility = api_get_item_visibility(
  2176. $courseInfo,
  2177. 'learnpath',
  2178. $row['id'],
  2179. $sessionId,
  2180. $student_id,
  2181. 'LearnpathSubscription'
  2182. );
  2183. if ($userVisibility == 1) {
  2184. $is_visible = true;
  2185. } else {
  2186. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2187. if (!empty($userGroups)) {
  2188. foreach ($userGroups as $groupInfo) {
  2189. $groupId = $groupInfo['iid'];
  2190. $userVisibility = api_get_item_visibility(
  2191. $courseInfo,
  2192. 'learnpath',
  2193. $row['id'],
  2194. $sessionId,
  2195. null,
  2196. 'LearnpathSubscription',
  2197. $groupId
  2198. );
  2199. if ($userVisibility == 1) {
  2200. $is_visible = true;
  2201. break;
  2202. }
  2203. }
  2204. }
  2205. }
  2206. }
  2207. return $is_visible;
  2208. }
  2209. return false;
  2210. }
  2211. /**
  2212. * @param int $lpId
  2213. * @param int $userId
  2214. * @param int $courseId
  2215. * @param int $sessionId
  2216. * @return int
  2217. */
  2218. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2219. {
  2220. $lpId = intval($lpId);
  2221. $userId = intval($userId);
  2222. $courseId = intval($courseId);
  2223. $sessionId = intval($sessionId);
  2224. $progress = 0;
  2225. $sessionCondition = api_get_session_condition($sessionId);
  2226. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2227. $sql = "SELECT * FROM $table
  2228. WHERE
  2229. c_id = ".$courseId." AND
  2230. lp_id = $lpId AND
  2231. user_id = $userId $sessionCondition";
  2232. $res = Database::query($sql);
  2233. if (Database :: num_rows($res) > 0) {
  2234. $row = Database:: fetch_array($res);
  2235. $progress = $row['progress'];
  2236. }
  2237. return $progress;
  2238. }
  2239. /**
  2240. * Displays a progress bar
  2241. * completed so far.
  2242. * @param integer $percentage Progress value to display
  2243. * @param string $text_add Text to display near the progress value
  2244. * @return string HTML string containing the progress bar
  2245. */
  2246. public static function get_progress_bar($percentage = -1, $text_add = '')
  2247. {
  2248. $text = $percentage . $text_add;
  2249. $output = '<div class="progress">
  2250. <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.';">
  2251. '. $text .'
  2252. </div>
  2253. </div>';
  2254. return $output;
  2255. }
  2256. /**
  2257. * @param string $mode can be '%' or 'abs'
  2258. * otherwise this value will be used $this->progress_bar_mode
  2259. * @return string
  2260. */
  2261. public function getProgressBar($mode = null)
  2262. {
  2263. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2264. return self::get_progress_bar($percentage, $text_add);
  2265. }
  2266. /**
  2267. * Gets the progress bar info to display inside the progress bar.
  2268. * Also used by scorm_api.php
  2269. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2270. * we display a number of completed elements per total elements
  2271. * @param integer $add Additional steps to fake as completed
  2272. * @return list array Percentage or number and symbol (% or /xx)
  2273. */
  2274. public function get_progress_bar_text($mode = '', $add = 0)
  2275. {
  2276. if ($this->debug > 0) {
  2277. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2278. }
  2279. if (empty($mode)) {
  2280. $mode = $this->progress_bar_mode;
  2281. }
  2282. $total_items = $this->get_total_items_count_without_chapters();
  2283. if ($this->debug > 2) {
  2284. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2285. }
  2286. $completeItems = $this->get_complete_items_count();
  2287. if ($this->debug > 2) {
  2288. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2289. }
  2290. if ($add != 0) {
  2291. $completeItems += $add;
  2292. if ($this->debug > 2) {
  2293. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2294. }
  2295. }
  2296. $text = '';
  2297. if ($completeItems > $total_items) {
  2298. $completeItems = $total_items;
  2299. }
  2300. $percentage = 0;
  2301. if ($mode == '%') {
  2302. if ($total_items > 0) {
  2303. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2304. } else {
  2305. $percentage = 0;
  2306. }
  2307. $percentage = number_format($percentage, 0);
  2308. $text = '%';
  2309. } elseif ($mode == 'abs') {
  2310. $percentage = $completeItems;
  2311. $text = '/' . $total_items;
  2312. }
  2313. return array(
  2314. $percentage,
  2315. $text
  2316. );
  2317. }
  2318. /**
  2319. * Gets the progress bar mode
  2320. * @return string The progress bar mode attribute
  2321. */
  2322. public function get_progress_bar_mode()
  2323. {
  2324. if ($this->debug > 0) {
  2325. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2326. }
  2327. if (!empty ($this->progress_bar_mode)) {
  2328. return $this->progress_bar_mode;
  2329. } else {
  2330. return '%';
  2331. }
  2332. }
  2333. /**
  2334. * Gets the learnpath proximity (remote or local)
  2335. * @return string Learnpath proximity
  2336. */
  2337. public function get_proximity()
  2338. {
  2339. if ($this->debug > 0) {
  2340. error_log('New LP - In learnpath::get_proximity()', 0);
  2341. }
  2342. if (!empty ($this->proximity)) {
  2343. return $this->proximity;
  2344. } else {
  2345. return '';
  2346. }
  2347. }
  2348. /**
  2349. * Gets the learnpath theme (remote or local)
  2350. * @return string Learnpath theme
  2351. */
  2352. public function get_theme()
  2353. {
  2354. if ($this->debug > 0) {
  2355. error_log('New LP - In learnpath::get_theme()', 0);
  2356. }
  2357. if (!empty ($this->theme)) {
  2358. return $this->theme;
  2359. } else {
  2360. return '';
  2361. }
  2362. }
  2363. /**
  2364. * Gets the learnpath session id
  2365. * @return string Learnpath theme
  2366. */
  2367. public function get_lp_session_id()
  2368. {
  2369. if ($this->debug > 0) {
  2370. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2371. }
  2372. if (!empty ($this->lp_session_id)) {
  2373. return $this->lp_session_id;
  2374. } else {
  2375. return 0;
  2376. }
  2377. }
  2378. /**
  2379. * Gets the learnpath image
  2380. * @return string Web URL of the LP image
  2381. */
  2382. public function get_preview_image()
  2383. {
  2384. if ($this->debug > 0) {
  2385. error_log('New LP - In learnpath::get_preview_image()', 0);
  2386. }
  2387. if (!empty($this->preview_image)) {
  2388. return $this->preview_image;
  2389. } else {
  2390. return '';
  2391. }
  2392. }
  2393. /**
  2394. * @param string $size
  2395. * @param string $path_type
  2396. * @return bool|string
  2397. */
  2398. public function get_preview_image_path($size = null, $path_type = 'web')
  2399. {
  2400. $preview_image = $this->get_preview_image();
  2401. if (isset($preview_image) && !empty($preview_image)) {
  2402. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2403. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2404. if (isset($size)) {
  2405. $info = pathinfo($preview_image);
  2406. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2407. if (file_exists($image_sys_path.$image_custom_size)) {
  2408. if ($path_type == 'web') {
  2409. return $image_path.$image_custom_size;
  2410. } else {
  2411. return $image_sys_path.$image_custom_size;
  2412. }
  2413. }
  2414. } else {
  2415. if ($path_type == 'web') {
  2416. return $image_path.$preview_image;
  2417. } else {
  2418. return $image_sys_path.$preview_image;
  2419. }
  2420. }
  2421. }
  2422. return false;
  2423. }
  2424. /**
  2425. * Gets the learnpath author
  2426. * @return string LP's author
  2427. */
  2428. public function get_author()
  2429. {
  2430. if ($this->debug > 0) {
  2431. error_log('New LP - In learnpath::get_author()', 0);
  2432. }
  2433. if (!empty ($this->author)) {
  2434. return $this->author;
  2435. } else {
  2436. return '';
  2437. }
  2438. }
  2439. /**
  2440. * Gets the learnpath author
  2441. * @return string LP's author
  2442. */
  2443. public function get_hide_toc_frame()
  2444. {
  2445. if ($this->debug > 0) {
  2446. error_log('New LP - In learnpath::get_author()', 0);
  2447. }
  2448. if (!empty ($this->hide_toc_frame)) {
  2449. return $this->hide_toc_frame;
  2450. } else {
  2451. return '';
  2452. }
  2453. }
  2454. /**
  2455. * Generate a new prerequisites string for a given item. If this item was a sco and
  2456. * its prerequisites were strings (instead of IDs), then transform those strings into
  2457. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2458. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2459. * same rule as the scorm_export() method
  2460. * @param integer Item ID
  2461. * @return string Prerequisites string ready for the export as SCORM
  2462. */
  2463. public function get_scorm_prereq_string($item_id)
  2464. {
  2465. if ($this->debug > 0) {
  2466. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2467. }
  2468. if (!is_object($this->items[$item_id])) {
  2469. return false;
  2470. }
  2471. /** @var learnpathItem $oItem */
  2472. $oItem = $this->items[$item_id];
  2473. $prereq = $oItem->get_prereq_string();
  2474. if (empty($prereq)) {
  2475. return '';
  2476. }
  2477. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2478. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2479. // then simply return it (with the ITEM_ prefix).
  2480. //return 'ITEM_' . $prereq;
  2481. return $this->items[$prereq]->ref;
  2482. } else {
  2483. if (isset($this->refs_list[$prereq])) {
  2484. // It's a simple string item from which the ID can be found in the refs list,
  2485. // so we can transform it directly to an ID for export.
  2486. return $this->items[$this->refs_list[$prereq]]->ref;
  2487. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2488. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2489. } else {
  2490. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2491. // and replace them, one by one, by the internal IDs (chamilo db)
  2492. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2493. // by a space as well.
  2494. $find = array (
  2495. '&',
  2496. '|',
  2497. '~',
  2498. '=',
  2499. '<>',
  2500. '{',
  2501. '}',
  2502. '*',
  2503. '(',
  2504. ')'
  2505. );
  2506. $replace = array (
  2507. ' ',
  2508. ' ',
  2509. ' ',
  2510. ' ',
  2511. ' ',
  2512. ' ',
  2513. ' ',
  2514. ' ',
  2515. ' ',
  2516. ' '
  2517. );
  2518. $prereq_mod = str_replace($find, $replace, $prereq);
  2519. $ids = explode(' ', $prereq_mod);
  2520. foreach ($ids as $id) {
  2521. $id = trim($id);
  2522. if (isset ($this->refs_list[$id])) {
  2523. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2524. }
  2525. }
  2526. return $prereq;
  2527. }
  2528. }
  2529. }
  2530. /**
  2531. * Returns the XML DOM document's node
  2532. * @param resource Reference to a list of objects to search for the given ITEM_*
  2533. * @param string The identifier to look for
  2534. * @return mixed The reference to the element found with that identifier. False if not found
  2535. */
  2536. public function get_scorm_xml_node(& $children, $id)
  2537. {
  2538. for ($i = 0; $i < $children->length; $i++) {
  2539. $item_temp = $children->item($i);
  2540. if ($item_temp->nodeName == 'item') {
  2541. if ($item_temp->getAttribute('identifier') == $id) {
  2542. return $item_temp;
  2543. }
  2544. }
  2545. $subchildren = $item_temp->childNodes;
  2546. if ($subchildren->length > 0) {
  2547. $val = $this->get_scorm_xml_node($subchildren, $id);
  2548. if (is_object($val)) {
  2549. return $val;
  2550. }
  2551. }
  2552. }
  2553. return false;
  2554. }
  2555. /**
  2556. * Returns a usable array of stats related to the current learnpath and user
  2557. * @return array Well-formatted array containing status for the current learnpath
  2558. */
  2559. public function get_stats()
  2560. {
  2561. if ($this->debug > 0) {
  2562. error_log('New LP - In learnpath::get_stats()', 0);
  2563. }
  2564. }
  2565. /**
  2566. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2567. * the given course (for all learnpaths and all users)
  2568. * @param string Course code
  2569. * @return array Well-formatted array containing status for the course's learnpaths
  2570. */
  2571. public function get_stats_course($course)
  2572. {
  2573. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2574. // TODO
  2575. }
  2576. /**
  2577. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2578. * the given course and learnpath (for all users)
  2579. * @param string Course code
  2580. * @param integer Learnpath ID
  2581. * @return array Well-formatted array containing status for the specified learnpath
  2582. */
  2583. public function get_stats_lp($course, $lp)
  2584. {
  2585. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2586. // TODO
  2587. }
  2588. /**
  2589. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2590. * the given course, learnpath and user.
  2591. * @param string Course code
  2592. * @param integer Learnpath ID
  2593. * @param integer User ID
  2594. * @return array Well-formatted array containing status for the specified learnpath and user
  2595. */
  2596. public function get_stats_lp_user($course, $lp, $user)
  2597. {
  2598. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2599. // TODO
  2600. }
  2601. /**
  2602. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2603. * the given course and learnpath (for all users)
  2604. * @param string Course code
  2605. * @param integer User ID
  2606. * @return array Well-formatted array containing status for the user's learnpaths
  2607. */
  2608. public function get_stats_user($course, $user) {
  2609. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2610. // TODO
  2611. }
  2612. /**
  2613. * Gets the status list for all LP's items
  2614. * @return array Array of [index] => [item ID => current status]
  2615. */
  2616. public function get_items_status_list()
  2617. {
  2618. if ($this->debug > 0) {
  2619. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2620. }
  2621. $list = array ();
  2622. foreach ($this->ordered_items as $item_id) {
  2623. $list[] = array (
  2624. $item_id => $this->items[$item_id]->get_status()
  2625. );
  2626. }
  2627. return $list;
  2628. }
  2629. /**
  2630. * Return the number of interactions for the given learnpath Item View ID.
  2631. * This method can be used as static.
  2632. * @param integer Item View ID
  2633. * @param integer course id
  2634. * @return integer Number of interactions
  2635. */
  2636. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2637. {
  2638. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2639. $lp_iv_id = intval($lp_iv_id);
  2640. $course_id = intval($course_id);
  2641. $sql = "SELECT count(*) FROM $table
  2642. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2643. $res = Database::query($sql);
  2644. $num = 0;
  2645. if (Database::num_rows($res)) {
  2646. $row = Database::fetch_array($res);
  2647. $num = $row[0];
  2648. }
  2649. return $num;
  2650. }
  2651. /**
  2652. * Return the interactions as an array for the given lp_iv_id.
  2653. * This method can be used as static.
  2654. * @param integer Learnpath Item View ID
  2655. * @return array
  2656. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2657. */
  2658. public static function get_iv_interactions_array($lp_iv_id)
  2659. {
  2660. $course_id = api_get_course_int_id();
  2661. $list = array();
  2662. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2663. if (empty($lp_iv_id)) {
  2664. return array();
  2665. }
  2666. $sql = "SELECT * FROM $table
  2667. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2668. ORDER BY order_id ASC";
  2669. $res = Database::query($sql);
  2670. $num = Database :: num_rows($res);
  2671. if ($num > 0) {
  2672. $list[] = array (
  2673. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2674. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2675. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2676. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2677. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2678. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2679. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2680. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2681. );
  2682. while ($row = Database :: fetch_array($res)) {
  2683. $list[] = array (
  2684. 'order_id' => ($row['order_id'] + 1),
  2685. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2686. 'type' => $row['interaction_type'],
  2687. 'time' => $row['completion_time'],
  2688. //'correct_responses' => $row['correct_responses'],
  2689. 'correct_responses' => '', // Hide correct responses from students.
  2690. 'student_response' => $row['student_response'],
  2691. 'result' => $row['result'],
  2692. 'latency' => $row['latency']
  2693. );
  2694. }
  2695. }
  2696. return $list;
  2697. }
  2698. /**
  2699. * Return the number of objectives for the given learnpath Item View ID.
  2700. * This method can be used as static.
  2701. * @param integer Item View ID
  2702. * @return integer Number of objectives
  2703. */
  2704. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2705. {
  2706. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2707. $course_id = intval($course_id);
  2708. $lp_iv_id = intval($lp_iv_id);
  2709. $sql = "SELECT count(*) FROM $table
  2710. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2711. //@todo seems that this always returns 0
  2712. $res = Database::query($sql);
  2713. $num = 0;
  2714. if (Database::num_rows($res)) {
  2715. $row = Database :: fetch_array($res);
  2716. $num = $row[0];
  2717. }
  2718. return $num;
  2719. }
  2720. /**
  2721. * Return the objectives as an array for the given lp_iv_id.
  2722. * This method can be used as static.
  2723. * @param integer Learnpath Item View ID
  2724. * @return array
  2725. * @todo Translate labels
  2726. */
  2727. public static function get_iv_objectives_array($lp_iv_id = 0)
  2728. {
  2729. $course_id = api_get_course_int_id();
  2730. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2731. $sql = "SELECT * FROM $table
  2732. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2733. ORDER BY order_id ASC";
  2734. $res = Database::query($sql);
  2735. $num = Database :: num_rows($res);
  2736. $list = array();
  2737. if ($num > 0) {
  2738. $list[] = array(
  2739. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2740. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2741. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2742. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2743. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2744. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2745. );
  2746. while ($row = Database :: fetch_array($res)) {
  2747. $list[] = array (
  2748. 'order_id' => ($row['order_id'] + 1),
  2749. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2750. 'score_raw' => $row['score_raw'],
  2751. 'score_max' => $row['score_max'],
  2752. 'score_min' => $row['score_min'],
  2753. 'status' => $row['status']
  2754. );
  2755. }
  2756. }
  2757. return $list;
  2758. }
  2759. /**
  2760. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2761. * used by get_html_toc() to be ready to display
  2762. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2763. */
  2764. public function get_toc()
  2765. {
  2766. if ($this->debug > 0) {
  2767. error_log('learnpath::get_toc()', 0);
  2768. }
  2769. $toc = array();
  2770. foreach ($this->ordered_items as $item_id) {
  2771. if ($this->debug > 2) {
  2772. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2773. }
  2774. // TODO: Change this link generation and use new function instead.
  2775. $toc[] = array (
  2776. 'id' => $item_id,
  2777. 'title' => $this->items[$item_id]->get_title(),
  2778. 'status' => $this->items[$item_id]->get_status(),
  2779. 'level' => $this->items[$item_id]->get_level(),
  2780. 'type' => $this->items[$item_id]->get_type(),
  2781. 'description' => $this->items[$item_id]->get_description(),
  2782. 'path' => $this->items[$item_id]->get_path(),
  2783. );
  2784. }
  2785. if ($this->debug > 2) {
  2786. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2787. }
  2788. return $toc;
  2789. }
  2790. /**
  2791. * Generate and return the table of contents for this learnpath. The JS
  2792. * table returned is used inside of scorm_api.php
  2793. * @return string A JS array vairiable construction
  2794. */
  2795. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2796. {
  2797. if ($this->debug > 0) {
  2798. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2799. }
  2800. $toc = $varname.' = new Array();';
  2801. foreach ($this->ordered_items as $item_id) {
  2802. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2803. }
  2804. if ($this->debug > 2) {
  2805. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2806. }
  2807. return $toc;
  2808. }
  2809. /**
  2810. * Gets the learning path type
  2811. * @param boolean Return the name? If false, return the ID. Default is false.
  2812. * @return mixed Type ID or name, depending on the parameter
  2813. */
  2814. public function get_type($get_name = false)
  2815. {
  2816. $res = false;
  2817. if ($this->debug > 0) {
  2818. error_log('New LP - In learnpath::get_type()', 0);
  2819. }
  2820. if (!empty ($this->type)) {
  2821. if ($get_name) {
  2822. // Get it from the lp_type table in main db.
  2823. } else {
  2824. $res = $this->type;
  2825. }
  2826. }
  2827. if ($this->debug > 2) {
  2828. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2829. }
  2830. return $res;
  2831. }
  2832. /**
  2833. * Gets the learning path type as static method
  2834. * @param boolean Return the name? If false, return the ID. Default is false.
  2835. * @return mixed Type ID or name, depending on the parameter
  2836. */
  2837. public static function get_type_static($lp_id = 0)
  2838. {
  2839. $course_id = api_get_course_int_id();
  2840. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2841. $lp_id = intval($lp_id);
  2842. $sql = "SELECT lp_type FROM $tbl_lp
  2843. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2844. $res = Database::query($sql);
  2845. if ($res === false) {
  2846. return null;
  2847. }
  2848. if (Database :: num_rows($res) <= 0) {
  2849. return null;
  2850. }
  2851. $row = Database :: fetch_array($res);
  2852. return $row['lp_type'];
  2853. }
  2854. /**
  2855. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2856. * This method can be used as abstract and is recursive
  2857. * @param integer Learnpath ID
  2858. * @param integer Parent ID of the items to look for
  2859. * @return mixed Ordered list of item IDs or false on error
  2860. */
  2861. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2862. {
  2863. if (empty($course_id)) {
  2864. $course_id = api_get_course_int_id();
  2865. } else {
  2866. $course_id = intval($course_id);
  2867. }
  2868. $list = array();
  2869. if (empty($lp)) {
  2870. return false;
  2871. }
  2872. $lp = intval($lp);
  2873. $parent = intval($parent);
  2874. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2875. $sql = "SELECT id FROM $tbl_lp_item
  2876. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2877. ORDER BY display_order";
  2878. $res = Database::query($sql);
  2879. while ($row = Database :: fetch_array($res)) {
  2880. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2881. $list[] = $row['id'];
  2882. foreach ($sublist as $item) {
  2883. $list[] = $item;
  2884. }
  2885. }
  2886. return $list;
  2887. }
  2888. /**
  2889. * @return array
  2890. */
  2891. public static function getChapterTypes()
  2892. {
  2893. return array(
  2894. 'dokeos_chapter',
  2895. 'dokeos_module',
  2896. 'chapter',
  2897. 'dir'
  2898. );
  2899. }
  2900. /**
  2901. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2902. * @return string HTML TOC ready to display
  2903. */
  2904. public function get_html_toc($toc_list = null)
  2905. {
  2906. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2907. if ($this->debug > 0) {
  2908. error_log('In learnpath::get_html_toc()', 0);
  2909. }
  2910. if (empty($toc_list)) {
  2911. $toc_list = $this->get_toc();
  2912. }
  2913. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2914. $html = '<div class="scorm-body">';
  2915. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2916. require_once 'resourcelinker.inc.php';
  2917. // Temporary variables.
  2918. $mycurrentitemid = $this->get_current_item_id();
  2919. $color_counter = 0;
  2920. $i = 0;
  2921. foreach ($toc_list as $item) {
  2922. // Style Status
  2923. $class_name = [
  2924. 'not attempted' => 'scorm_not_attempted',
  2925. 'incomplete' => 'scorm_not_attempted',
  2926. 'failed' => 'scorm_failed',
  2927. 'completed' => 'scorm_completed',
  2928. 'passed' => 'scorm_completed',
  2929. 'succeeded' => 'scorm_completed',
  2930. 'browsed' => 'scorm_completed',
  2931. ];
  2932. $scorm_color_background = 'row_odd';
  2933. $style_item = '';
  2934. if ($color_counter % 2 == 0) {
  2935. $scorm_color_background = 'row_even';
  2936. }
  2937. $dirTypes = self::getChapterTypes();
  2938. if (in_array($item['type'], $dirTypes)) {
  2939. $scorm_color_background ='scorm_item_section ';
  2940. $style_item = '';
  2941. }
  2942. if ($item['id'] == $this->current) {
  2943. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2944. } elseif (!in_array($item['type'], $dirTypes)) {
  2945. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2946. }
  2947. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2948. // Learning path title
  2949. $title = $item['title'];
  2950. if (empty ($title)) {
  2951. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2952. }
  2953. $title = Security::remove_XSS($title);
  2954. // Learning path personalization
  2955. // build the LP tree
  2956. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2957. $description = $item['description'];
  2958. if (empty($description)) {
  2959. $description = $title;
  2960. }
  2961. if (in_array($item['type'], $dirTypes)) {
  2962. // Chapters
  2963. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2964. } else {
  2965. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2966. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2967. }
  2968. if (in_array($item['type'], $dirTypes)) {
  2969. // Chapter
  2970. // if you want to put an image before, you should use css
  2971. $html .= stripslashes($title);
  2972. } else {
  2973. $this->get_link('http', $item['id'], $toc_list);
  2974. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2975. }
  2976. $html .= "</div>";
  2977. if ($scorm_color_background != '') {
  2978. $html .= '</div>';
  2979. }
  2980. $color_counter++;
  2981. }
  2982. $html .= "</div>";
  2983. $html .= "</div>";
  2984. return $html;
  2985. }
  2986. /**
  2987. * Returns an HTML-formatted string ready to display with teacher buttons
  2988. * in LP view menu
  2989. * @return string HTML TOC ready to display
  2990. */
  2991. public function get_teacher_toc_buttons()
  2992. {
  2993. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2994. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2995. $html = '';
  2996. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2997. $gradebook = '';
  2998. if (!empty($_GET['gradebook'])) {
  2999. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  3000. }
  3001. if ($this->get_lp_session_id() == api_get_session_id()) {
  3002. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3003. $html .= '<div class="btn-group">';
  3004. $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'>" .
  3005. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  3006. $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'>" .
  3007. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  3008. $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">' .
  3009. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  3010. $html .= '</div>';
  3011. $html .= '</div>';
  3012. }
  3013. }
  3014. return $html;
  3015. }
  3016. /**
  3017. * Gets the learnpath maker name - generally the editor's name
  3018. * @return string Learnpath maker name
  3019. */
  3020. public function get_maker()
  3021. {
  3022. if ($this->debug > 0) {
  3023. error_log('New LP - In learnpath::get_maker()', 0);
  3024. }
  3025. if (!empty ($this->maker)) {
  3026. return $this->maker;
  3027. } else {
  3028. return '';
  3029. }
  3030. }
  3031. /**
  3032. * Gets the learnpath name/title
  3033. * @return string Learnpath name/title
  3034. */
  3035. public function get_name()
  3036. {
  3037. if ($this->debug > 0) {
  3038. error_log('New LP - In learnpath::get_name()', 0);
  3039. }
  3040. if (!empty ($this->name)) {
  3041. return $this->name;
  3042. } else {
  3043. return 'N/A';
  3044. }
  3045. }
  3046. /**
  3047. * Gets a link to the resource from the present location, depending on item ID.
  3048. * @param string $type Type of link expected
  3049. * @param integer $item_id Learnpath item ID
  3050. * @return string $provided_toc Link to the lp_item resource
  3051. */
  3052. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3053. {
  3054. $course_id = $this->get_course_int_id();
  3055. if ($this->debug > 0) {
  3056. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3057. }
  3058. if (empty($item_id)) {
  3059. if ($this->debug > 2) {
  3060. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3061. }
  3062. $item_id = $this->get_current_item_id();
  3063. }
  3064. if (empty($item_id)) {
  3065. if ($this->debug > 2) {
  3066. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3067. }
  3068. //still empty, this means there was no item_id given and we are not in an object context or
  3069. //the object property is empty, return empty link
  3070. $item_id = $this->first();
  3071. return '';
  3072. }
  3073. $file = '';
  3074. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3075. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3076. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3077. $item_id = intval($item_id);
  3078. $sql = "SELECT
  3079. l.lp_type as ltype,
  3080. l.path as lpath,
  3081. li.item_type as litype,
  3082. li.path as lipath,
  3083. li.parameters as liparams
  3084. FROM $lp_table l
  3085. INNER JOIN $lp_item_table li
  3086. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3087. WHERE li.id = $item_id ";
  3088. if ($this->debug > 2) {
  3089. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3090. }
  3091. $res = Database::query($sql);
  3092. if (Database :: num_rows($res) > 0) {
  3093. $row = Database :: fetch_array($res);
  3094. $lp_type = $row['ltype'];
  3095. $lp_path = $row['lpath'];
  3096. $lp_item_type = $row['litype'];
  3097. $lp_item_path = $row['lipath'];
  3098. $lp_item_params = $row['liparams'];
  3099. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3100. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3101. }
  3102. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3103. if ($type == 'http') {
  3104. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3105. } else {
  3106. $course_path = $sys_course_path; //system path
  3107. }
  3108. // 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.
  3109. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3110. $lp_type = 1;
  3111. }
  3112. if ($this->debug > 2) {
  3113. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3114. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3115. }
  3116. // Now go through the specific cases to get the end of the path
  3117. // @todo Use constants instead of int values.
  3118. switch ($lp_type) {
  3119. case 1 :
  3120. require_once 'resourcelinker.inc.php';
  3121. $file = rl_get_resource_link_for_learnpath(
  3122. $course_id,
  3123. $this->get_id(),
  3124. $item_id,
  3125. $this->get_view_id()
  3126. );
  3127. if ($this->debug > 0) {
  3128. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3129. }
  3130. switch ($lp_item_type) {
  3131. case 'dokeos_chapter':
  3132. $file = 'lp_content.php?type=dir';
  3133. break;
  3134. case 'link':
  3135. if (Link::is_youtube_link($file)) {
  3136. $src = Link::get_youtube_video_id($file);
  3137. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3138. } elseif (Link::isVimeoLink($file)) {
  3139. $src = Link::getVimeoLinkId($file);
  3140. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3141. } else {
  3142. // If the current site is HTTPS and the link is
  3143. // HTTP, browsers will refuse opening the link
  3144. $urlId = api_get_current_access_url_id();
  3145. $url = api_get_access_url($urlId, false);
  3146. $protocol = substr($url['url'], 0, 5);
  3147. if ($protocol === 'https') {
  3148. $linkProtocol = substr($file, 0, 5);
  3149. if ($linkProtocol === 'http:') {
  3150. //this is the special intervention case
  3151. $file = api_get_path(
  3152. WEB_CODE_PATH
  3153. ).'newscorm/embed.php?type=nonhttps&source='.urlencode($file);
  3154. }
  3155. }
  3156. }
  3157. break;
  3158. case 'quiz':
  3159. // Check how much attempts of a exercise exits in lp
  3160. $lp_item_id = $this->get_current_item_id();
  3161. $lp_view_id = $this->get_view_id();
  3162. $prevent_reinit = null;
  3163. if (isset($this->items[$this->current])) {
  3164. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3165. }
  3166. if (empty($provided_toc)) {
  3167. if ($this->debug > 0) {
  3168. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3169. }
  3170. $list = $this->get_toc();
  3171. } else {
  3172. if ($this->debug > 0) {
  3173. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3174. }
  3175. $list = $provided_toc;
  3176. }
  3177. $type_quiz = false;
  3178. foreach ($list as $toc) {
  3179. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3180. $type_quiz = true;
  3181. }
  3182. }
  3183. if ($type_quiz) {
  3184. $lp_item_id = intval($lp_item_id);
  3185. $lp_view_id = intval($lp_view_id);
  3186. $sql = "SELECT count(*) FROM $lp_item_view_table
  3187. WHERE
  3188. c_id = $course_id AND
  3189. lp_item_id='".$lp_item_id."' AND
  3190. lp_view_id ='".$lp_view_id."' AND
  3191. status='completed'";
  3192. $result = Database::query($sql);
  3193. $row_count = Database:: fetch_row($result);
  3194. $count_item_view = (int)$row_count[0];
  3195. $not_multiple_attempt = 0;
  3196. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3197. $not_multiple_attempt = 1;
  3198. }
  3199. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3200. }
  3201. break;
  3202. }
  3203. $tmp_array = explode('/', $file);
  3204. $document_name = $tmp_array[count($tmp_array) - 1];
  3205. if (strpos($document_name, '_DELETED_')) {
  3206. $file = 'blank.php?error=document_deleted';
  3207. }
  3208. break;
  3209. case 2 :
  3210. if ($this->debug > 2) {
  3211. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3212. }
  3213. if ($lp_item_type != 'dir') {
  3214. // Quite complex here:
  3215. // We want to make sure 'http://' (and similar) links can
  3216. // be loaded as is (withouth the Chamilo path in front) but
  3217. // some contents use this form: resource.htm?resource=http://blablabla
  3218. // which means we have to find a protocol at the path's start, otherwise
  3219. // it should not be considered as an external URL.
  3220. //if ($this->prerequisites_match($item_id)) {
  3221. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3222. if ($this->debug > 2) {
  3223. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3224. }
  3225. // Distant url, return as is.
  3226. $file = $lp_item_path;
  3227. } else {
  3228. if ($this->debug > 2) {
  3229. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3230. }
  3231. // Prevent getting untranslatable urls.
  3232. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3233. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3234. // Prepare the path.
  3235. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3236. // TODO: Fix this for urls with protocol header.
  3237. $file = str_replace('//', '/', $file);
  3238. $file = str_replace(':/', '://', $file);
  3239. if (substr($lp_path, -1) == '/') {
  3240. $lp_path = substr($lp_path, 0, -1);
  3241. }
  3242. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3243. // if file not found.
  3244. $decoded = html_entity_decode($lp_item_path);
  3245. list ($decoded) = explode('?', $decoded);
  3246. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3247. require_once 'resourcelinker.inc.php';
  3248. $file = rl_get_resource_link_for_learnpath(
  3249. $course_id,
  3250. $this->get_id(),
  3251. $item_id,
  3252. $this->get_view_id()
  3253. );
  3254. if (empty($file)) {
  3255. $file = 'blank.php?error=document_not_found';
  3256. } else {
  3257. $tmp_array = explode('/', $file);
  3258. $document_name = $tmp_array[count($tmp_array) - 1];
  3259. if (strpos($document_name, '_DELETED_')) {
  3260. $file = 'blank.php?error=document_deleted';
  3261. } else {
  3262. $file = 'blank.php?error=document_not_found';
  3263. }
  3264. }
  3265. } else {
  3266. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3267. }
  3268. }
  3269. }
  3270. // We want to use parameters if they were defined in the imsmanifest
  3271. if (strpos($file, 'blank.php') === false) {
  3272. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3273. }
  3274. } else {
  3275. $file = 'lp_content.php?type=dir';
  3276. }
  3277. break;
  3278. case 3 :
  3279. if ($this->debug > 2) {
  3280. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3281. }
  3282. // Formatting AICC HACP append URL.
  3283. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3284. if (!empty($lp_item_params)) {
  3285. $aicc_append .= $lp_item_params . '&';
  3286. }
  3287. if ($lp_item_type != 'dir') {
  3288. // Quite complex here:
  3289. // We want to make sure 'http://' (and similar) links can
  3290. // be loaded as is (withouth the Chamilo path in front) but
  3291. // some contents use this form: resource.htm?resource=http://blablabla
  3292. // which means we have to find a protocol at the path's start, otherwise
  3293. // it should not be considered as an external URL.
  3294. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3295. if ($this->debug > 2) {
  3296. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3297. }
  3298. // Distant url, return as is.
  3299. $file = $lp_item_path;
  3300. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3301. /*
  3302. if (stristr($file,'<servername>') !== false) {
  3303. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3304. }
  3305. */
  3306. if (stripos($file, '<servername>') !== false) {
  3307. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3308. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3309. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3310. }
  3311. //
  3312. $file .= $aicc_append;
  3313. } else {
  3314. if ($this->debug > 2) {
  3315. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3316. }
  3317. // Prevent getting untranslatable urls.
  3318. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3319. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3320. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3321. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3322. // TODO: Fix this for urls with protocol header.
  3323. $file = str_replace('//', '/', $file);
  3324. $file = str_replace(':/', '://', $file);
  3325. $file .= $aicc_append;
  3326. }
  3327. } else {
  3328. $file = 'lp_content.php?type=dir';
  3329. }
  3330. break;
  3331. case 4 :
  3332. break;
  3333. default :
  3334. break;
  3335. }
  3336. // Replace &amp; by & because &amp; will break URL with params
  3337. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3338. }
  3339. if ($this->debug > 2) {
  3340. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3341. }
  3342. return $file;
  3343. }
  3344. /**
  3345. * Gets the latest usable view or generate a new one
  3346. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3347. * @return integer DB lp_view id
  3348. */
  3349. public function get_view($attempt_num = 0)
  3350. {
  3351. if ($this->debug > 0) {
  3352. error_log('New LP - In learnpath::get_view()', 0);
  3353. }
  3354. $search = '';
  3355. // Use $attempt_num to enable multi-views management (disabled so far).
  3356. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3357. $search = 'AND view_count = ' . $attempt_num;
  3358. }
  3359. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3360. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3361. $course_id = api_get_course_int_id();
  3362. $sessionId = api_get_session_id();
  3363. $sql = "SELECT id, view_count FROM $lp_view_table
  3364. WHERE
  3365. c_id = " . $course_id . " AND
  3366. lp_id = " . $this->get_id() . " AND
  3367. user_id = " . $this->get_user_id() . " AND
  3368. session_id = $sessionId
  3369. $search
  3370. ORDER BY view_count DESC";
  3371. $res = Database::query($sql);
  3372. if (Database :: num_rows($res) > 0) {
  3373. $row = Database :: fetch_array($res);
  3374. $this->lp_view_id = $row['id'];
  3375. } else if (!api_is_invitee()) {
  3376. // There is no database record, create one.
  3377. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3378. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3379. Database::query($sql);
  3380. $id = Database :: insert_id();
  3381. $this->lp_view_id = $id;
  3382. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3383. Database::query($sql);
  3384. }
  3385. return $this->lp_view_id;
  3386. }
  3387. /**
  3388. * Gets the current view id
  3389. * @return integer View ID (from lp_view)
  3390. */
  3391. public function get_view_id()
  3392. {
  3393. if ($this->debug > 0) {
  3394. error_log('New LP - In learnpath::get_view_id()', 0);
  3395. }
  3396. if (!empty ($this->lp_view_id)) {
  3397. return $this->lp_view_id;
  3398. } else {
  3399. return 0;
  3400. }
  3401. }
  3402. /**
  3403. * Gets the update queue
  3404. * @return array Array containing IDs of items to be updated by JavaScript
  3405. */
  3406. public function get_update_queue()
  3407. {
  3408. if ($this->debug > 0) {
  3409. error_log('New LP - In learnpath::get_update_queue()', 0);
  3410. }
  3411. return $this->update_queue;
  3412. }
  3413. /**
  3414. * Gets the user ID
  3415. * @return integer User ID
  3416. */
  3417. public function get_user_id()
  3418. {
  3419. if ($this->debug > 0) {
  3420. error_log('New LP - In learnpath::get_user_id()', 0);
  3421. }
  3422. if (!empty ($this->user_id)) {
  3423. return $this->user_id;
  3424. } else {
  3425. return false;
  3426. }
  3427. }
  3428. /**
  3429. * Checks if any of the items has an audio element attached
  3430. * @return bool True or false
  3431. */
  3432. public function has_audio()
  3433. {
  3434. if ($this->debug > 1) {
  3435. error_log('New LP - In learnpath::has_audio()', 0);
  3436. }
  3437. $has = false;
  3438. foreach ($this->items as $i => $item) {
  3439. if (!empty ($this->items[$i]->audio)) {
  3440. $has = true;
  3441. break;
  3442. }
  3443. }
  3444. return $has;
  3445. }
  3446. /**
  3447. * Logs a message into a file
  3448. * @param string Message to log
  3449. * @return boolean True on success, false on error or if msg empty
  3450. */
  3451. public function log($msg)
  3452. {
  3453. if ($this->debug > 0) {
  3454. error_log('New LP - In learnpath::log()', 0);
  3455. }
  3456. // TODO
  3457. $this->error .= $msg;
  3458. return true;
  3459. }
  3460. /**
  3461. * Moves an item up and down at its level
  3462. * @param integer Item to move up and down
  3463. * @param string Direction 'up' or 'down'
  3464. * @return integer New display order, or false on error
  3465. */
  3466. public function move_item($id, $direction)
  3467. {
  3468. $course_id = api_get_course_int_id();
  3469. if ($this->debug > 0) {
  3470. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3471. }
  3472. if (empty($id) || empty($direction)) {
  3473. return false;
  3474. }
  3475. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3476. $sql_sel = "SELECT *
  3477. FROM " . $tbl_lp_item . "
  3478. WHERE c_id = ".$course_id." AND id = " . $id;
  3479. $res_sel = Database::query($sql_sel);
  3480. // Check if elem exists.
  3481. if (Database :: num_rows($res_sel) < 1) {
  3482. return false;
  3483. }
  3484. // Gather data.
  3485. $row = Database :: fetch_array($res_sel);
  3486. $previous = $row['previous_item_id'];
  3487. $next = $row['next_item_id'];
  3488. $display = $row['display_order'];
  3489. $parent = $row['parent_item_id'];
  3490. $lp = $row['lp_id'];
  3491. // Update the item (switch with previous/next one).
  3492. switch ($direction) {
  3493. case 'up':
  3494. if ($this->debug > 2) {
  3495. error_log('Movement up detected', 0);
  3496. }
  3497. if ($display <= 1) { /*do nothing*/
  3498. } else {
  3499. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3500. WHERE c_id = ".$course_id." AND id = $previous";
  3501. if ($this->debug > 2) {
  3502. error_log('Selecting previous: ' . $sql_sel2, 0);
  3503. }
  3504. $res_sel2 = Database::query($sql_sel2);
  3505. if (Database :: num_rows($res_sel2) < 1) {
  3506. $previous_previous = 0;
  3507. }
  3508. // Gather data.
  3509. $row2 = Database :: fetch_array($res_sel2);
  3510. $previous_previous = $row2['previous_item_id'];
  3511. // Update previous_previous item (switch "next" with current).
  3512. if ($previous_previous != 0) {
  3513. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3514. next_item_id = $id
  3515. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3516. if ($this->debug > 2) {
  3517. error_log($sql_upd2, 0);
  3518. }
  3519. Database::query($sql_upd2);
  3520. }
  3521. // Update previous item (switch with current).
  3522. if ($previous != 0) {
  3523. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3524. next_item_id = $next,
  3525. previous_item_id = $id,
  3526. display_order = display_order +1
  3527. WHERE c_id = ".$course_id." AND id = $previous";
  3528. if ($this->debug > 2) {
  3529. error_log($sql_upd2, 0);
  3530. }
  3531. Database::query($sql_upd2);
  3532. }
  3533. // Update current item (switch with previous).
  3534. if ($id != 0) {
  3535. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3536. next_item_id = $previous,
  3537. previous_item_id = $previous_previous,
  3538. display_order = display_order-1
  3539. WHERE c_id = ".$course_id." AND id = $id";
  3540. if ($this->debug > 2) {
  3541. error_log($sql_upd2, 0);
  3542. }
  3543. Database::query($sql_upd2);
  3544. }
  3545. // Update next item (new previous item).
  3546. if ($next != 0) {
  3547. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3548. WHERE c_id = ".$course_id." AND id = $next";
  3549. if ($this->debug > 2) {
  3550. error_log($sql_upd2, 0);
  3551. }
  3552. Database::query($sql_upd2);
  3553. }
  3554. $display = $display -1;
  3555. }
  3556. break;
  3557. case 'down':
  3558. if ($this->debug > 2) {
  3559. error_log('Movement down detected', 0);
  3560. }
  3561. if ($next == 0) { /* Do nothing. */
  3562. } else {
  3563. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3564. if ($this->debug > 2) {
  3565. error_log('Selecting next: ' . $sql_sel2, 0);
  3566. }
  3567. $res_sel2 = Database::query($sql_sel2);
  3568. if (Database :: num_rows($res_sel2) < 1) {
  3569. $next_next = 0;
  3570. }
  3571. // Gather data.
  3572. $row2 = Database :: fetch_array($res_sel2);
  3573. $next_next = $row2['next_item_id'];
  3574. // Update previous item (switch with current).
  3575. if ($previous != 0) {
  3576. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3577. WHERE c_id = ".$course_id." AND id = $previous";
  3578. Database::query($sql_upd2);
  3579. }
  3580. // Update current item (switch with previous).
  3581. if ($id != 0) {
  3582. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3583. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3584. WHERE c_id = ".$course_id." AND id = $id";
  3585. Database::query($sql_upd2);
  3586. }
  3587. // Update next item (new previous item).
  3588. if ($next != 0) {
  3589. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3590. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3591. WHERE c_id = ".$course_id." AND id = $next";
  3592. Database::query($sql_upd2);
  3593. }
  3594. // Update next_next item (switch "previous" with current).
  3595. if ($next_next != 0) {
  3596. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3597. previous_item_id = $id
  3598. WHERE c_id = ".$course_id." AND id = $next_next";
  3599. Database::query($sql_upd2);
  3600. }
  3601. $display = $display +1;
  3602. }
  3603. break;
  3604. default :
  3605. return false;
  3606. }
  3607. return $display;
  3608. }
  3609. /**
  3610. * Move a learnpath up (display_order)
  3611. * @param integer $lp_id Learnpath ID
  3612. */
  3613. public static function move_up($lp_id)
  3614. {
  3615. $course_id = api_get_course_int_id();
  3616. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3617. $sql = "SELECT * FROM $lp_table
  3618. WHERE c_id = ".$course_id."
  3619. ORDER BY display_order";
  3620. $res = Database::query($sql);
  3621. if ($res === false)
  3622. return false;
  3623. $lps = array ();
  3624. $lp_order = array ();
  3625. $num = Database :: num_rows($res);
  3626. // First check the order is correct, globally (might be wrong because
  3627. // of versions < 1.8.4)
  3628. if ($num > 0) {
  3629. $i = 1;
  3630. while ($row = Database :: fetch_array($res)) {
  3631. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3632. $need_fix = true;
  3633. $sql_u = "UPDATE $lp_table SET display_order = $i
  3634. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3635. Database::query($sql_u);
  3636. }
  3637. $row['display_order'] = $i;
  3638. $lps[$row['id']] = $row;
  3639. $lp_order[$i] = $row['id'];
  3640. $i++;
  3641. }
  3642. }
  3643. if ($num > 1) { // If there's only one element, no need to sort.
  3644. $order = $lps[$lp_id]['display_order'];
  3645. if ($order > 1) { // If it's the first element, no need to move up.
  3646. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3647. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3648. Database::query($sql_u1);
  3649. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3650. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3651. Database::query($sql_u2);
  3652. }
  3653. }
  3654. }
  3655. /**
  3656. * Move a learnpath down (display_order)
  3657. * @param integer $lp_id Learnpath ID
  3658. */
  3659. public static function move_down($lp_id)
  3660. {
  3661. $course_id = api_get_course_int_id();
  3662. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3663. $sql = "SELECT * FROM $lp_table
  3664. WHERE c_id = ".$course_id."
  3665. ORDER BY display_order";
  3666. $res = Database::query($sql);
  3667. if ($res === false) {
  3668. return false;
  3669. }
  3670. $lps = array ();
  3671. $lp_order = array ();
  3672. $num = Database :: num_rows($res);
  3673. $max = 0;
  3674. // First check the order is correct, globally (might be wrong because
  3675. // of versions < 1.8.4).
  3676. if ($num > 0) {
  3677. $i = 1;
  3678. while ($row = Database :: fetch_array($res)) {
  3679. $max = $i;
  3680. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3681. $need_fix = true;
  3682. $sql_u = "UPDATE $lp_table SET display_order = $i
  3683. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3684. Database::query($sql_u);
  3685. }
  3686. $row['display_order'] = $i;
  3687. $lps[$row['id']] = $row;
  3688. $lp_order[$i] = $row['id'];
  3689. $i++;
  3690. }
  3691. }
  3692. if ($num > 1) { // If there's only one element, no need to sort.
  3693. $order = $lps[$lp_id]['display_order'];
  3694. if ($order < $max) { // If it's the first element, no need to move up.
  3695. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3696. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3697. Database::query($sql_u1);
  3698. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3699. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3700. Database::query($sql_u2);
  3701. }
  3702. }
  3703. }
  3704. /**
  3705. * Updates learnpath attributes to point to the next element
  3706. * The last part is similar to set_current_item but processing the other way around
  3707. */
  3708. public function next()
  3709. {
  3710. if ($this->debug > 0) {
  3711. error_log('New LP - In learnpath::next()', 0);
  3712. }
  3713. $this->last = $this->get_current_item_id();
  3714. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3715. $this->autocomplete_parents($this->last);
  3716. $new_index = $this->get_next_index();
  3717. if ($this->debug > 2) {
  3718. error_log('New LP - New index: ' . $new_index, 0);
  3719. }
  3720. $this->index = $new_index;
  3721. if ($this->debug > 2) {
  3722. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3723. }
  3724. $this->current = $this->ordered_items[$new_index];
  3725. if ($this->debug > 2) {
  3726. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3727. }
  3728. }
  3729. /**
  3730. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3731. * class, this might be redefined to allow several behaviours depending on the document type.
  3732. * @param integer Resource ID
  3733. * @return boolean True on success, false otherwise
  3734. */
  3735. public function open($id)
  3736. {
  3737. if ($this->debug > 0) {
  3738. error_log('New LP - In learnpath::open()', 0);
  3739. }
  3740. // TODO:
  3741. // set the current resource attribute to this resource
  3742. // switch on element type (redefine in child class?)
  3743. // set status for this item to "opened"
  3744. // start timer
  3745. // initialise score
  3746. $this->index = 0; //or = the last item seen (see $this->last)
  3747. }
  3748. /**
  3749. * Check that all prerequisites are fulfilled. Returns true and an
  3750. * empty string on success, returns false
  3751. * and the prerequisite string on error.
  3752. * This function is based on the rules for aicc_script language as
  3753. * described in the SCORM 1.2 CAM documentation page 108.
  3754. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3755. * @return boolean True if prerequisites are matched, false otherwise -
  3756. * Empty string if true returned, prerequisites string otherwise.
  3757. */
  3758. public function prerequisites_match($itemId = null)
  3759. {
  3760. $debug = $this->debug;
  3761. if ($debug > 0) {
  3762. error_log('In learnpath::prerequisites_match()', 0);
  3763. }
  3764. if (empty($itemId)) {
  3765. $itemId = $this->current;
  3766. }
  3767. $currentItem = $this->getItem($itemId);
  3768. if ($currentItem) {
  3769. if ($this->type == 2) {
  3770. // Getting prereq from scorm
  3771. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3772. } else {
  3773. $prereq_string = $currentItem->get_prereq_string();
  3774. }
  3775. if (empty($prereq_string)) {
  3776. if ($debug > 0) {
  3777. error_log('Found prereq_string is empty return true');
  3778. }
  3779. return true;
  3780. }
  3781. // Clean spaces.
  3782. $prereq_string = str_replace(' ', '', $prereq_string);
  3783. if ($debug > 0) {
  3784. error_log('Found prereq_string: ' . $prereq_string, 0);
  3785. }
  3786. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3787. $result = $currentItem->parse_prereq(
  3788. $prereq_string,
  3789. $this->items,
  3790. $this->refs_list,
  3791. $this->get_user_id()
  3792. );
  3793. if ($result === false) {
  3794. $this->set_error_msg($currentItem->prereq_alert);
  3795. }
  3796. } else {
  3797. $result = true;
  3798. if ($debug > 1) {
  3799. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3800. }
  3801. }
  3802. if ($debug > 1) {
  3803. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3804. }
  3805. return $result;
  3806. }
  3807. /**
  3808. * Updates learnpath attributes to point to the previous element
  3809. * The last part is similar to set_current_item but processing the other way around
  3810. */
  3811. public function previous()
  3812. {
  3813. if ($this->debug > 0) {
  3814. error_log('New LP - In learnpath::previous()', 0);
  3815. }
  3816. $this->last = $this->get_current_item_id();
  3817. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3818. $this->autocomplete_parents($this->last);
  3819. $new_index = $this->get_previous_index();
  3820. $this->index = $new_index;
  3821. $this->current = $this->ordered_items[$new_index];
  3822. }
  3823. /**
  3824. * Publishes a learnpath. This basically means show or hide the learnpath
  3825. * to normal users.
  3826. * Can be used as abstract
  3827. * @param integer Learnpath ID
  3828. * @param string New visibility
  3829. */
  3830. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3831. {
  3832. $action = 'visible';
  3833. if ($set_visibility != 1) {
  3834. $action = 'invisible';
  3835. self::toggle_publish($lp_id, 'i');
  3836. }
  3837. return api_item_property_update(
  3838. api_get_course_info(),
  3839. TOOL_LEARNPATH,
  3840. $lp_id,
  3841. $action,
  3842. api_get_user_id()
  3843. );
  3844. }
  3845. /**
  3846. * Publishes a learnpath. This basically means show or hide the learnpath
  3847. * on the course homepage
  3848. * Can be used as abstract
  3849. * @param integer $lp_id Learnpath id
  3850. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3851. * @return bool
  3852. */
  3853. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3854. {
  3855. $course_id = api_get_course_int_id();
  3856. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3857. $lp_id = intval($lp_id);
  3858. $sql = "SELECT * FROM $tbl_lp
  3859. WHERE c_id = ".$course_id." AND id = $lp_id";
  3860. $result = Database::query($sql);
  3861. if (Database::num_rows($result)) {
  3862. $row = Database :: fetch_array($result);
  3863. $name = domesticate($row['name']);
  3864. if ($set_visibility == 'i') {
  3865. $s = $name . " " . get_lang('LearnpathNotPublished');
  3866. $dialogBox = $s;
  3867. $v = 0;
  3868. }
  3869. if ($set_visibility == 'v') {
  3870. $s = $name . " " . get_lang('LearnpathPublished');
  3871. $dialogBox = $s;
  3872. $v = 1;
  3873. }
  3874. } else {
  3875. return false;
  3876. }
  3877. $session_id = api_get_session_id();
  3878. $session_condition = api_get_session_condition($session_id);
  3879. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3880. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3881. $sql = "SELECT * FROM $tbl_tool
  3882. WHERE
  3883. c_id = ".$course_id." AND
  3884. link='$link' and
  3885. image='scormbuilder.gif' and
  3886. link LIKE '$link%'
  3887. $session_condition
  3888. ";
  3889. $result = Database::query($sql);
  3890. $num = Database :: num_rows($result);
  3891. if ($set_visibility == 'i' && $num > 0) {
  3892. $sql = "DELETE FROM $tbl_tool
  3893. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3894. Database::query($sql);
  3895. } elseif ($set_visibility == 'v' && $num == 0) {
  3896. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3897. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3898. Database::query($sql);
  3899. $insertId = Database::insert_id();
  3900. if ($insertId) {
  3901. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3902. Database::query($sql);
  3903. }
  3904. } elseif ($set_visibility == 'v' && $num > 0) {
  3905. $sql = "UPDATE $tbl_tool SET
  3906. c_id = $course_id,
  3907. name = '$name',
  3908. link = '$link',
  3909. image = 'scormbuilder.gif',
  3910. visibility = '$v',
  3911. admin = '0',
  3912. address = 'pastillegris.gif',
  3913. added_tool = 0,
  3914. session_id = $session_id
  3915. WHERE
  3916. c_id = ".$course_id." AND
  3917. (link='$link' and image='scormbuilder.gif' $session_condition)
  3918. ";
  3919. Database::query($sql);
  3920. } else {
  3921. // Parameter and database incompatible, do nothing, exit.
  3922. return false;
  3923. }
  3924. }
  3925. /**
  3926. * Restart the whole learnpath. Return the URL of the first element.
  3927. * Make sure the results are saved with anoter method. This method should probably be
  3928. * redefined in children classes.
  3929. * To use a similar method statically, use the create_new_attempt() method
  3930. * @return string URL to load in the viewer
  3931. */
  3932. public function restart()
  3933. {
  3934. if ($this->debug > 0) {
  3935. error_log('New LP - In learnpath::restart()', 0);
  3936. }
  3937. // TODO
  3938. // Call autosave method to save the current progress.
  3939. //$this->index = 0;
  3940. if (api_is_invitee()) {
  3941. return false;
  3942. }
  3943. $session_id = api_get_session_id();
  3944. $course_id = api_get_course_int_id();
  3945. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3946. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3947. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3948. if ($this->debug > 2) {
  3949. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3950. }
  3951. $res = Database::query($sql);
  3952. $view_id = Database::insert_id();
  3953. if ($view_id) {
  3954. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3955. Database::query($sql);
  3956. $this->lp_view_id = $view_id;
  3957. $this->attempt = $this->attempt + 1;
  3958. } else {
  3959. $this->error = 'Could not insert into item_view table...';
  3960. return false;
  3961. }
  3962. $this->autocomplete_parents($this->current);
  3963. foreach ($this->items as $index => $dummy) {
  3964. $this->items[$index]->restart();
  3965. $this->items[$index]->set_lp_view($this->lp_view_id);
  3966. }
  3967. $this->first();
  3968. return true;
  3969. }
  3970. /**
  3971. * Saves the current item
  3972. * @return boolean
  3973. */
  3974. public function save_current()
  3975. {
  3976. if ($this->debug > 0) {
  3977. error_log('learnpath::save_current()', 0);
  3978. }
  3979. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3980. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3981. if ($this->debug > 2) {
  3982. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3983. }
  3984. if ($this->debug > 2) {
  3985. error_log('' . print_r($this->items, true), 0);
  3986. }
  3987. if (isset($this->items[$this->current]) &&
  3988. is_object($this->items[$this->current])
  3989. ) {
  3990. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3991. $this->autocomplete_parents($this->current);
  3992. $status = $this->items[$this->current]->get_status();
  3993. $this->update_queue[$this->current] = $status;
  3994. return $res;
  3995. }
  3996. return false;
  3997. }
  3998. /**
  3999. * Saves the given item
  4000. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  4001. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4002. * @return boolean
  4003. */
  4004. public function save_item($item_id = null, $from_outside = true)
  4005. {
  4006. $debug = $this->debug;
  4007. if ($debug) {
  4008. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4009. }
  4010. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4011. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4012. if (empty($item_id)) {
  4013. $item_id = intval($_REQUEST['id']);
  4014. }
  4015. if (empty($item_id)) {
  4016. $item_id = $this->get_current_item_id();
  4017. }
  4018. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4019. if ($debug) {
  4020. error_log('Object exists');
  4021. }
  4022. // Saving the item.
  4023. $res = $this->items[$item_id]->save(
  4024. $from_outside,
  4025. $this->prerequisites_match($item_id)
  4026. );
  4027. if ($debug) {
  4028. error_log('update_queue before:');
  4029. error_log(print_r($this->update_queue,1));
  4030. }
  4031. $this->autocomplete_parents($item_id);
  4032. $status = $this->items[$item_id]->get_status();
  4033. $this->update_queue[$item_id] = $status;
  4034. if ($debug) {
  4035. error_log('get_status(): ' . $status);
  4036. error_log('update_queue after:');
  4037. error_log(print_r($this->update_queue,1));
  4038. }
  4039. return $res;
  4040. }
  4041. return false;
  4042. }
  4043. /**
  4044. * Saves the last item seen's ID only in case
  4045. */
  4046. public function save_last()
  4047. {
  4048. $course_id = api_get_course_int_id();
  4049. if ($this->debug > 0) {
  4050. error_log('New LP - In learnpath::save_last()', 0);
  4051. }
  4052. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4053. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4054. if (isset($this->current) && !api_is_invitee()) {
  4055. if ($this->debug > 2) {
  4056. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4057. }
  4058. $sql = "UPDATE $table SET
  4059. last_item = " . intval($this->get_current_item_id()). "
  4060. WHERE
  4061. c_id = $course_id AND
  4062. lp_id = " . $this->get_id() . " AND
  4063. user_id = " . $this->get_user_id()." ".$session_condition;
  4064. if ($this->debug > 2) {
  4065. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4066. }
  4067. Database::query($sql);
  4068. }
  4069. if (!api_is_invitee()) {
  4070. // Save progress.
  4071. list($progress, $text) = $this->get_progress_bar_text('%');
  4072. if ($progress >= 0 && $progress <= 100) {
  4073. $progress = (int) $progress;
  4074. $sql = "UPDATE $table SET
  4075. progress = $progress
  4076. WHERE
  4077. c_id = ".$course_id." AND
  4078. lp_id = " . $this->get_id() . " AND
  4079. user_id = " . $this->get_user_id()." ".$session_condition;
  4080. // Ignore errors as some tables might not have the progress field just yet.
  4081. Database::query($sql);
  4082. $this->progress_db = $progress;
  4083. }
  4084. }
  4085. }
  4086. /**
  4087. * Sets the current item ID (checks if valid and authorized first)
  4088. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4089. */
  4090. public function set_current_item($item_id = null)
  4091. {
  4092. if ($this->debug > 0) {
  4093. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4094. }
  4095. if (empty ($item_id)) {
  4096. if ($this->debug > 2) {
  4097. error_log('New LP - No new current item given, ignore...', 0);
  4098. }
  4099. // Do nothing.
  4100. } else {
  4101. if ($this->debug > 2) {
  4102. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4103. }
  4104. if (is_numeric($item_id)) {
  4105. $item_id = intval($item_id);
  4106. // TODO: Check in database here.
  4107. $this->last = $this->current;
  4108. $this->current = $item_id;
  4109. // TODO: Update $this->index as well.
  4110. foreach ($this->ordered_items as $index => $item) {
  4111. if ($item == $this->current) {
  4112. $this->index = $index;
  4113. break;
  4114. }
  4115. }
  4116. if ($this->debug > 2) {
  4117. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4118. }
  4119. } else {
  4120. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4121. }
  4122. }
  4123. }
  4124. /**
  4125. * Sets the encoding
  4126. * @param string New encoding
  4127. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4128. */
  4129. public function set_encoding($enc = 'UTF-8')
  4130. {
  4131. if ($this->debug > 0) {
  4132. error_log('New LP - In learnpath::set_encoding()', 0);
  4133. }
  4134. $course_id = api_get_course_int_id();
  4135. $enc = api_refine_encoding_id($enc);
  4136. if (empty($enc)) {
  4137. $enc = api_get_system_encoding();
  4138. }
  4139. if (api_is_encoding_supported($enc)) {
  4140. $lp = $this->get_id();
  4141. if ($lp != 0) {
  4142. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4143. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4144. $res = Database::query($sql);
  4145. return $res;
  4146. }
  4147. }
  4148. return false;
  4149. }
  4150. /**
  4151. * Sets the JS lib setting in the database directly.
  4152. * This is the JavaScript library file this lp needs to load on startup
  4153. * @param string Proximity setting
  4154. * @return boolean True on update success. False otherwise.
  4155. */
  4156. public function set_jslib($lib = '')
  4157. {
  4158. if ($this->debug > 0) {
  4159. error_log('New LP - In learnpath::set_jslib()', 0);
  4160. }
  4161. $lp = $this->get_id();
  4162. $course_id = api_get_course_int_id();
  4163. if ($lp != 0) {
  4164. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4165. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4166. $res = Database::query($sql);
  4167. return $res;
  4168. } else {
  4169. return false;
  4170. }
  4171. }
  4172. /**
  4173. * Sets the name of the LP maker (publisher) (and save)
  4174. * @param string Optional string giving the new content_maker of this learnpath
  4175. * @return boolean True
  4176. */
  4177. public function set_maker($name = '')
  4178. {
  4179. if ($this->debug > 0) {
  4180. error_log('New LP - In learnpath::set_maker()', 0);
  4181. }
  4182. if (empty ($name))
  4183. return false;
  4184. $this->maker = $name;
  4185. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4186. $course_id = api_get_course_int_id();
  4187. $lp_id = $this->get_id();
  4188. $sql = "UPDATE $lp_table SET
  4189. content_maker = '" . Database::escape_string($this->maker) . "'
  4190. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4191. if ($this->debug > 2) {
  4192. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4193. }
  4194. Database::query($sql);
  4195. return true;
  4196. }
  4197. /**
  4198. * Sets the name of the current learnpath (and save)
  4199. * @param string $name Optional string giving the new name of this learnpath
  4200. * @return boolean True/False
  4201. */
  4202. public function set_name($name = null)
  4203. {
  4204. if ($this->debug > 0) {
  4205. error_log('New LP - In learnpath::set_name()', 0);
  4206. }
  4207. if (empty($name)) {
  4208. return false;
  4209. }
  4210. $this->name = Database::escape_string($name);
  4211. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4212. $lp_id = $this->get_id();
  4213. $course_id = $this->course_info['real_id'];
  4214. $sql = "UPDATE $lp_table SET
  4215. name = '" . Database::escape_string($this->name). "'
  4216. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4217. if ($this->debug > 2) {
  4218. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4219. }
  4220. $result = Database::query($sql);
  4221. // If the lp is visible on the homepage, change his name there.
  4222. if (Database::affected_rows($result)) {
  4223. $session_id = api_get_session_id();
  4224. $session_condition = api_get_session_condition($session_id);
  4225. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4226. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4227. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4228. WHERE
  4229. c_id = $course_id AND
  4230. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4231. Database::query($sql);
  4232. return true;
  4233. } else {
  4234. return false;
  4235. }
  4236. }
  4237. /**
  4238. * Set index specified prefix terms for all items in this path
  4239. * @param string Comma-separated list of terms
  4240. * @param char Xapian term prefix
  4241. * @return boolean False on error, true otherwise
  4242. */
  4243. public function set_terms_by_prefix($terms_string, $prefix)
  4244. {
  4245. $course_id = api_get_course_int_id();
  4246. if (api_get_setting('search_enabled') !== 'true')
  4247. return false;
  4248. if (!extension_loaded('xapian')) {
  4249. return false;
  4250. }
  4251. $terms_string = trim($terms_string);
  4252. $terms = explode(',', $terms_string);
  4253. array_walk($terms, 'trim_value');
  4254. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4255. // Don't do anything if no change, verify only at DB, not the search engine.
  4256. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4257. return false;
  4258. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4259. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4260. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4261. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4262. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4263. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4264. $lp_id = intval($_POST['lp_id']);
  4265. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4266. $result = Database::query($sql);
  4267. $di = new ChamiloIndexer();
  4268. while ($lp_item = Database :: fetch_array($result)) {
  4269. // Get search_did.
  4270. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4271. $sql = 'SELECT * FROM %s
  4272. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4273. LIMIT 1';
  4274. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4275. //echo $sql; echo '<br>';
  4276. $res = Database::query($sql);
  4277. if (Database::num_rows($res) > 0) {
  4278. $se_ref = Database :: fetch_array($res);
  4279. // Compare terms.
  4280. $doc = $di->get_document($se_ref['search_did']);
  4281. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4282. $xterms = array();
  4283. foreach ($xapian_terms as $xapian_term) {
  4284. $xterms[] = substr($xapian_term['name'], 1);
  4285. }
  4286. $dterms = $terms;
  4287. $missing_terms = array_diff($dterms, $xterms);
  4288. $deprecated_terms = array_diff($xterms, $dterms);
  4289. // Save it to search engine.
  4290. foreach ($missing_terms as $term) {
  4291. $doc->add_term($prefix . $term, 1);
  4292. }
  4293. foreach ($deprecated_terms as $term) {
  4294. $doc->remove_term($prefix . $term);
  4295. }
  4296. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4297. $di->getDb()->flush();
  4298. } else {
  4299. //@todo What we should do here?
  4300. }
  4301. }
  4302. return true;
  4303. }
  4304. /**
  4305. * Sets the theme of the LP (local/remote) (and save)
  4306. * @param string Optional string giving the new theme of this learnpath
  4307. * @return bool Returns true if theme name is not empty
  4308. */
  4309. public function set_theme($name = '')
  4310. {
  4311. $course_id = api_get_course_int_id();
  4312. if ($this->debug > 0) {
  4313. error_log('New LP - In learnpath::set_theme()', 0);
  4314. }
  4315. $this->theme = $name;
  4316. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4317. $lp_id = $this->get_id();
  4318. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4319. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4320. if ($this->debug > 2) {
  4321. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4322. }
  4323. Database::query($sql);
  4324. return true;
  4325. }
  4326. /**
  4327. * Sets the image of an LP (and save)
  4328. * @param string Optional string giving the new image of this learnpath
  4329. * @return bool Returns true if theme name is not empty
  4330. */
  4331. public function set_preview_image($name = '')
  4332. {
  4333. $course_id = api_get_course_int_id();
  4334. if ($this->debug > 0) {
  4335. error_log('New LP - In learnpath::set_preview_image()', 0);
  4336. }
  4337. $this->preview_image = $name;
  4338. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4339. $lp_id = $this->get_id();
  4340. $sql = "UPDATE $lp_table SET
  4341. preview_image = '" . Database::escape_string($this->preview_image). "'
  4342. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4343. if ($this->debug > 2) {
  4344. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4345. }
  4346. Database::query($sql);
  4347. return true;
  4348. }
  4349. /**
  4350. * Sets the author of a LP (and save)
  4351. * @param string Optional string giving the new author of this learnpath
  4352. * @return bool Returns true if author's name is not empty
  4353. */
  4354. public function set_author($name = '')
  4355. {
  4356. $course_id = api_get_course_int_id();
  4357. if ($this->debug > 0) {
  4358. error_log('New LP - In learnpath::set_author()', 0);
  4359. }
  4360. $this->author = $name;
  4361. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4362. $lp_id = $this->get_id();
  4363. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4364. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4365. if ($this->debug > 2) {
  4366. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4367. }
  4368. Database::query($sql);
  4369. return true;
  4370. }
  4371. /**
  4372. * Sets the hide_toc_frame parameter of a LP (and save)
  4373. * @param int 1 if frame is hidden 0 then else
  4374. * @return bool Returns true if author's name is not empty
  4375. */
  4376. public function set_hide_toc_frame($hide)
  4377. {
  4378. $course_id = api_get_course_int_id();
  4379. if ($this->debug > 0) {
  4380. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4381. }
  4382. if (intval($hide) == $hide) {
  4383. $this->hide_toc_frame = $hide;
  4384. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4385. $lp_id = $this->get_id();
  4386. $sql = "UPDATE $lp_table SET
  4387. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4388. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4389. if ($this->debug > 2) {
  4390. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4391. }
  4392. Database::query($sql);
  4393. return true;
  4394. } else {
  4395. return false;
  4396. }
  4397. }
  4398. /**
  4399. * Sets the prerequisite of a LP (and save)
  4400. * @param int integer giving the new prerequisite of this learnpath
  4401. * @return bool returns true if prerequisite is not empty
  4402. */
  4403. public function set_prerequisite($prerequisite)
  4404. {
  4405. $course_id = api_get_course_int_id();
  4406. if ($this->debug > 0) {
  4407. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4408. }
  4409. $this->prerequisite = intval($prerequisite);
  4410. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4411. $lp_id = $this->get_id();
  4412. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4413. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4414. if ($this->debug > 2) {
  4415. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4416. }
  4417. Database::query($sql);
  4418. return true;
  4419. }
  4420. /**
  4421. * Sets the location/proximity of the LP (local/remote) (and save)
  4422. * @param string Optional string giving the new location of this learnpath
  4423. * @return boolean True on success / False on error
  4424. */
  4425. public function set_proximity($name = '')
  4426. {
  4427. $course_id = api_get_course_int_id();
  4428. if ($this->debug > 0) {
  4429. error_log('New LP - In learnpath::set_proximity()', 0);
  4430. }
  4431. if (empty ($name))
  4432. return false;
  4433. $this->proximity = $name;
  4434. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4435. $lp_id = $this->get_id();
  4436. $sql = "UPDATE $lp_table SET
  4437. content_local = '" . Database::escape_string($name) . "'
  4438. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4439. if ($this->debug > 2) {
  4440. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4441. }
  4442. Database::query($sql);
  4443. return true;
  4444. }
  4445. /**
  4446. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4447. * @param integer DB ID of the item
  4448. */
  4449. public function set_previous_item($id)
  4450. {
  4451. if ($this->debug > 0) {
  4452. error_log('New LP - In learnpath::set_previous_item()', 0);
  4453. }
  4454. $this->last = $id;
  4455. }
  4456. /**
  4457. * Sets use_max_score
  4458. * @param string $use_max_score Optional string giving the new location of this learnpath
  4459. * @return boolean True on success / False on error
  4460. */
  4461. public function set_use_max_score($use_max_score = 1)
  4462. {
  4463. $course_id = api_get_course_int_id();
  4464. if ($this->debug > 0) {
  4465. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4466. }
  4467. $use_max_score = intval($use_max_score);
  4468. $this->use_max_score = $use_max_score;
  4469. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4470. $lp_id = $this->get_id();
  4471. $sql = "UPDATE $lp_table SET
  4472. use_max_score = '" . $this->use_max_score . "'
  4473. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4474. if ($this->debug > 2) {
  4475. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4476. }
  4477. Database::query($sql);
  4478. return true;
  4479. }
  4480. /**
  4481. * Sets and saves the expired_on date
  4482. * @param string $expired_on Optional string giving the new author of this learnpath
  4483. * @return bool Returns true if author's name is not empty
  4484. */
  4485. public function set_expired_on($expired_on)
  4486. {
  4487. $course_id = api_get_course_int_id();
  4488. if ($this->debug > 0) {
  4489. error_log('New LP - In learnpath::set_expired_on()', 0);
  4490. }
  4491. if (!empty($expired_on)) {
  4492. $this->expired_on = api_get_utc_datetime($expired_on);
  4493. } else {
  4494. $this->expired_on = null;
  4495. }
  4496. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4497. $lp_id = $this->get_id();
  4498. if ($this->debug > 2) {
  4499. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4500. }
  4501. $params = [
  4502. 'expired_on' => $this->expired_on,
  4503. ];
  4504. Database::update($lp_table, $params, ['c_id = ? AND id = ?' => [$course_id, $lp_id]], true);
  4505. return true;
  4506. }
  4507. /**
  4508. * Sets and saves the publicated_on date
  4509. * @param string $publicated_on Optional string giving the new author of this learnpath
  4510. * @return bool Returns true if author's name is not empty
  4511. */
  4512. public function set_publicated_on($publicated_on)
  4513. {
  4514. $course_id = api_get_course_int_id();
  4515. if ($this->debug > 0) {
  4516. error_log('New LP - In learnpath::set_expired_on()', 0);
  4517. }
  4518. if (!empty($publicated_on)) {
  4519. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4520. } else {
  4521. $this->publicated_on = '';
  4522. }
  4523. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4524. $lp_id = $this->get_id();
  4525. $sql = "UPDATE $lp_table SET
  4526. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4527. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4528. if ($this->debug > 2) {
  4529. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4530. }
  4531. Database::query($sql);
  4532. return true;
  4533. }
  4534. /**
  4535. * Sets and saves the expired_on date
  4536. * @return bool Returns true if author's name is not empty
  4537. */
  4538. public function set_modified_on()
  4539. {
  4540. $course_id = api_get_course_int_id();
  4541. if ($this->debug > 0) {
  4542. error_log('New LP - In learnpath::set_expired_on()', 0);
  4543. }
  4544. $this->modified_on = api_get_utc_datetime();
  4545. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4546. $lp_id = $this->get_id();
  4547. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4548. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4549. if ($this->debug > 2) {
  4550. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4551. }
  4552. Database::query($sql);
  4553. return true;
  4554. }
  4555. /**
  4556. * Sets the object's error message
  4557. * @param string Error message. If empty, reinits the error string
  4558. * @return void
  4559. */
  4560. public function set_error_msg($error = '')
  4561. {
  4562. if ($this->debug > 0) {
  4563. error_log('New LP - In learnpath::set_error_msg()', 0);
  4564. }
  4565. if (empty ($error)) {
  4566. $this->error = '';
  4567. } else {
  4568. $this->error .= $error;
  4569. }
  4570. }
  4571. /**
  4572. * Launches the current item if not 'sco'
  4573. * (starts timer and make sure there is a record ready in the DB)
  4574. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4575. * @return boolean
  4576. */
  4577. public function start_current_item($allow_new_attempt = false)
  4578. {
  4579. if ($this->debug > 0) {
  4580. error_log('New LP - In learnpath::start_current_item()', 0);
  4581. }
  4582. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4583. $type = $this->get_type();
  4584. $item_type = $this->items[$this->current]->get_type();
  4585. if (($type == 2 && $item_type != 'sco') ||
  4586. ($type == 3 && $item_type != 'au') ||
  4587. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4588. ) {
  4589. $this->items[$this->current]->open($allow_new_attempt);
  4590. $this->autocomplete_parents($this->current);
  4591. $prereq_check = $this->prerequisites_match($this->current);
  4592. $this->items[$this->current]->save(false, $prereq_check);
  4593. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4594. } else {
  4595. // If sco, then it is supposed to have been updated by some other call.
  4596. }
  4597. if ($item_type == 'sco') {
  4598. $this->items[$this->current]->restart();
  4599. }
  4600. }
  4601. if ($this->debug > 0) {
  4602. error_log('New LP - End of learnpath::start_current_item()', 0);
  4603. }
  4604. return true;
  4605. }
  4606. /**
  4607. * Stops the processing and counters for the old item (as held in $this->last)
  4608. * @return boolean True/False
  4609. */
  4610. public function stop_previous_item()
  4611. {
  4612. if ($this->debug > 0) {
  4613. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4614. }
  4615. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4616. if ($this->debug > 2) {
  4617. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4618. }
  4619. switch ($this->get_type()) {
  4620. case '3' :
  4621. if ($this->items[$this->last]->get_type() != 'au') {
  4622. if ($this->debug > 2) {
  4623. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4624. }
  4625. $this->items[$this->last]->close();
  4626. //$this->autocomplete_parents($this->last);
  4627. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4628. } else {
  4629. if ($this->debug > 2) {
  4630. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4631. }
  4632. }
  4633. case '2' :
  4634. if ($this->items[$this->last]->get_type() != 'sco') {
  4635. if ($this->debug > 2) {
  4636. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4637. }
  4638. $this->items[$this->last]->close();
  4639. //$this->autocomplete_parents($this->last);
  4640. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4641. } else {
  4642. if ($this->debug > 2) {
  4643. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4644. }
  4645. }
  4646. break;
  4647. case '1' :
  4648. default :
  4649. if ($this->debug > 2) {
  4650. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4651. }
  4652. $this->items[$this->last]->close();
  4653. break;
  4654. }
  4655. } else {
  4656. if ($this->debug > 2) {
  4657. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4658. }
  4659. return false;
  4660. }
  4661. return true;
  4662. }
  4663. /**
  4664. * Updates the default view mode from fullscreen to embedded and inversely
  4665. * @return string The current default view mode ('fullscreen' or 'embedded')
  4666. */
  4667. public function update_default_view_mode()
  4668. {
  4669. $course_id = api_get_course_int_id();
  4670. if ($this->debug > 0) {
  4671. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4672. }
  4673. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4674. $sql = "SELECT * FROM $lp_table
  4675. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4676. $res = Database::query($sql);
  4677. if (Database :: num_rows($res) > 0) {
  4678. $row = Database :: fetch_array($res);
  4679. $default_view_mode = $row['default_view_mod'];
  4680. $view_mode = $default_view_mode;
  4681. switch ($default_view_mode) {
  4682. case 'fullscreen': // default with popup
  4683. $view_mode = 'embedded';
  4684. break;
  4685. case 'embedded': // default view with left menu
  4686. $view_mode = 'embedframe';
  4687. break;
  4688. case 'embedframe': //folded menu
  4689. $view_mode = 'impress';
  4690. break;
  4691. case 'impress':
  4692. $view_mode = 'fullscreen';
  4693. break;
  4694. }
  4695. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4696. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4697. Database::query($sql);
  4698. $this->mode = $view_mode;
  4699. return $view_mode;
  4700. } else {
  4701. if ($this->debug > 2) {
  4702. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4703. }
  4704. }
  4705. return -1;
  4706. }
  4707. /**
  4708. * Updates the default behaviour about auto-commiting SCORM updates
  4709. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4710. */
  4711. public function update_default_scorm_commit()
  4712. {
  4713. $course_id = api_get_course_int_id();
  4714. if ($this->debug > 0) {
  4715. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4716. }
  4717. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4718. $sql = "SELECT * FROM $lp_table
  4719. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4720. $res = Database::query($sql);
  4721. if (Database :: num_rows($res) > 0) {
  4722. $row = Database :: fetch_array($res);
  4723. $force = $row['force_commit'];
  4724. if ($force == 1) {
  4725. $force = 0;
  4726. $force_return = false;
  4727. } elseif ($force == 0) {
  4728. $force = 1;
  4729. $force_return = true;
  4730. }
  4731. $sql = "UPDATE $lp_table SET force_commit = $force
  4732. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4733. Database::query($sql);
  4734. $this->force_commit = $force_return;
  4735. return $force_return;
  4736. } else {
  4737. if ($this->debug > 2) {
  4738. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4739. }
  4740. }
  4741. return -1;
  4742. }
  4743. /**
  4744. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4745. * @return bool True on success, false on failure
  4746. */
  4747. public function update_display_order()
  4748. {
  4749. $course_id = api_get_course_int_id();
  4750. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4751. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4752. $res = Database::query($sql);
  4753. if ($res === false)
  4754. return false;
  4755. $num = Database :: num_rows($res);
  4756. // First check the order is correct, globally (might be wrong because
  4757. // of versions < 1.8.4).
  4758. if ($num > 0) {
  4759. $i = 1;
  4760. while ($row = Database :: fetch_array($res)) {
  4761. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4762. $need_fix = true;
  4763. $sql = "UPDATE $lp_table SET display_order = $i
  4764. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4765. Database::query($sql);
  4766. }
  4767. $i++;
  4768. }
  4769. }
  4770. return true;
  4771. }
  4772. /**
  4773. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4774. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4775. */
  4776. public function update_reinit()
  4777. {
  4778. $course_id = api_get_course_int_id();
  4779. if ($this->debug > 0) {
  4780. error_log('New LP - In learnpath::update_reinit()', 0);
  4781. }
  4782. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4783. $sql = "SELECT * FROM $lp_table
  4784. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4785. $res = Database::query($sql);
  4786. if (Database :: num_rows($res) > 0) {
  4787. $row = Database :: fetch_array($res);
  4788. $force = $row['prevent_reinit'];
  4789. if ($force == 1) {
  4790. $force = 0;
  4791. } elseif ($force == 0) {
  4792. $force = 1;
  4793. }
  4794. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4795. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4796. Database::query($sql);
  4797. $this->prevent_reinit = $force;
  4798. return $force;
  4799. } else {
  4800. if ($this->debug > 2) {
  4801. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4802. }
  4803. }
  4804. return -1;
  4805. }
  4806. /**
  4807. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4808. *
  4809. * @return string 'single', 'multi' or 'seriousgame'
  4810. * @author ndiechburg <noel@cblue.be>
  4811. **/
  4812. public function get_attempt_mode()
  4813. {
  4814. //Set default value for seriousgame_mode
  4815. if (!isset($this->seriousgame_mode)) {
  4816. $this->seriousgame_mode=0;
  4817. }
  4818. // Set default value for prevent_reinit
  4819. if (!isset($this->prevent_reinit)) {
  4820. $this->prevent_reinit =1;
  4821. }
  4822. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4823. return 'seriousgame';
  4824. }
  4825. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4826. return 'single';
  4827. }
  4828. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4829. return 'multiple';
  4830. }
  4831. return 'single';
  4832. }
  4833. /**
  4834. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4835. *
  4836. * @param string 'seriousgame', 'single' or 'multiple'
  4837. * @return boolean
  4838. * @author ndiechburg <noel@cblue.be>
  4839. **/
  4840. public function set_attempt_mode($mode)
  4841. {
  4842. $course_id = api_get_course_int_id();
  4843. switch ($mode) {
  4844. case 'seriousgame' :
  4845. $sg_mode = 1;
  4846. $prevent_reinit = 1;
  4847. break;
  4848. case 'single' :
  4849. $sg_mode = 0;
  4850. $prevent_reinit = 1;
  4851. break;
  4852. case 'multiple' :
  4853. $sg_mode = 0;
  4854. $prevent_reinit = 0;
  4855. break;
  4856. default :
  4857. $sg_mode = 0;
  4858. $prevent_reinit = 0;
  4859. break;
  4860. }
  4861. $this->prevent_reinit = $prevent_reinit;
  4862. $this->seriousgame_mode = $sg_mode;
  4863. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4864. $sql = "UPDATE $lp_table SET
  4865. prevent_reinit = $prevent_reinit ,
  4866. seriousgame_mode = $sg_mode
  4867. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4868. $res = Database::query($sql);
  4869. if ($res) {
  4870. return true;
  4871. } else {
  4872. return false;
  4873. }
  4874. }
  4875. /**
  4876. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4877. *
  4878. * @return boolean
  4879. * @author ndiechburg <noel@cblue.be>
  4880. **/
  4881. public function switch_attempt_mode()
  4882. {
  4883. if ($this->debug > 0) {
  4884. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4885. }
  4886. $mode = $this->get_attempt_mode();
  4887. switch ($mode) {
  4888. case 'single' :
  4889. $next_mode = 'multiple';
  4890. break;
  4891. case 'multiple' :
  4892. $next_mode = 'seriousgame';
  4893. break;
  4894. case 'seriousgame' :
  4895. $next_mode = 'single';
  4896. break;
  4897. default :
  4898. $next_mode = 'single';
  4899. break;
  4900. }
  4901. $this->set_attempt_mode($next_mode);
  4902. }
  4903. /**
  4904. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4905. * but possibility to do again a completed item.
  4906. *
  4907. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4908. * @author ndiechburg <noel@cblue.be>
  4909. **/
  4910. public function set_seriousgame_mode()
  4911. {
  4912. $course_id = api_get_course_int_id();
  4913. if ($this->debug > 0) {
  4914. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4915. }
  4916. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4917. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4918. $res = Database::query($sql);
  4919. if (Database :: num_rows($res) > 0) {
  4920. $row = Database :: fetch_array($res);
  4921. $force = $row['seriousgame_mode'];
  4922. if ($force == 1) {
  4923. $force = 0;
  4924. } elseif ($force == 0) {
  4925. $force = 1;
  4926. }
  4927. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4928. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4929. Database::query($sql);
  4930. $this->seriousgame_mode = $force;
  4931. return $force;
  4932. } else {
  4933. if ($this->debug > 2) {
  4934. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4935. }
  4936. }
  4937. return -1;
  4938. }
  4939. /**
  4940. * Updates the "scorm_debug" value that shows or hide the debug window
  4941. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4942. */
  4943. public function update_scorm_debug()
  4944. {
  4945. $course_id = api_get_course_int_id();
  4946. if ($this->debug > 0) {
  4947. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4948. }
  4949. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4950. $sql = "SELECT * FROM $lp_table
  4951. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4952. $res = Database::query($sql);
  4953. if (Database :: num_rows($res) > 0) {
  4954. $row = Database :: fetch_array($res);
  4955. $force = $row['debug'];
  4956. if ($force == 1) {
  4957. $force = 0;
  4958. } elseif ($force == 0) {
  4959. $force = 1;
  4960. }
  4961. $sql = "UPDATE $lp_table SET debug = $force
  4962. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4963. $res = Database::query($sql);
  4964. $this->scorm_debug = $force;
  4965. return $force;
  4966. } else {
  4967. if ($this->debug > 2) {
  4968. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4969. }
  4970. }
  4971. return -1;
  4972. }
  4973. /**
  4974. * Function that makes a call to the function sort_tree_array and create_tree_array
  4975. * @author Kevin Van Den Haute
  4976. * @param array
  4977. */
  4978. public function tree_array($array)
  4979. {
  4980. if ($this->debug > 1) {
  4981. error_log('New LP - In learnpath::tree_array()', 0);
  4982. }
  4983. $array = $this->sort_tree_array($array);
  4984. $this->create_tree_array($array);
  4985. }
  4986. /**
  4987. * Creates an array with the elements of the learning path tree in it
  4988. *
  4989. * @author Kevin Van Den Haute
  4990. * @param array $array
  4991. * @param int $parent
  4992. * @param int $depth
  4993. * @param array $tmp
  4994. */
  4995. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4996. {
  4997. if ($this->debug > 1) {
  4998. error_log('New LP - In learnpath::create_tree_array())', 0);
  4999. }
  5000. if (is_array($array)) {
  5001. for ($i = 0; $i < count($array); $i++) {
  5002. if ($array[$i]['parent_item_id'] == $parent) {
  5003. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5004. $tmp[] = $array[$i]['parent_item_id'];
  5005. $depth++;
  5006. }
  5007. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5008. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5009. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5010. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5011. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5012. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5013. $this->arrMenu[] = array(
  5014. 'id' => $array[$i]['id'],
  5015. 'ref' => $ref,
  5016. 'item_type' => $array[$i]['item_type'],
  5017. 'title' => $array[$i]['title'],
  5018. 'path' => $path,
  5019. 'description' => $array[$i]['description'],
  5020. 'parent_item_id' => $array[$i]['parent_item_id'],
  5021. 'previous_item_id' => $array[$i]['previous_item_id'],
  5022. 'next_item_id' => $array[$i]['next_item_id'],
  5023. 'min_score' => $array[$i]['min_score'],
  5024. 'max_score' => $array[$i]['max_score'],
  5025. 'mastery_score' => $array[$i]['mastery_score'],
  5026. 'display_order' => $array[$i]['display_order'],
  5027. 'prerequisite' => $preq,
  5028. 'depth' => $depth,
  5029. 'audio' => $audio,
  5030. 'prerequisite_min_score' => $prerequisiteMinScore,
  5031. 'prerequisite_max_score' => $prerequisiteMaxScore
  5032. );
  5033. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5034. }
  5035. }
  5036. }
  5037. }
  5038. /**
  5039. * Sorts a multi dimensional array by parent id and display order
  5040. * @author Kevin Van Den Haute
  5041. *
  5042. * @param array $array (array with al the learning path items in it)
  5043. *
  5044. * @return array
  5045. */
  5046. public function sort_tree_array($array) {
  5047. foreach ($array as $key => $row) {
  5048. $parent[$key] = $row['parent_item_id'];
  5049. $position[$key] = $row['display_order'];
  5050. }
  5051. if (count($array) > 0)
  5052. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5053. return $array;
  5054. }
  5055. /**
  5056. * Function that creates a html list of learning path items so that we can add audio files to them
  5057. * @author Kevin Van Den Haute
  5058. * @param int $lp_id
  5059. * @return string
  5060. */
  5061. public function overview()
  5062. {
  5063. if ($this->debug > 0) {
  5064. error_log('New LP - In learnpath::overview()', 0);
  5065. }
  5066. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5067. $return = '';
  5068. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5069. // we need to start a form when we want to update all the mp3 files
  5070. if ($update_audio == 'true') {
  5071. $return .= '<form action="' . api_get_self() . '?'.api_get_cidreq().'&updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&action=' . Security :: remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5072. }
  5073. $return .= '<div id="message"></div>';
  5074. if (count($this->items) == 0) {
  5075. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5076. } else {
  5077. $return_audio = '<table class="data_table">';
  5078. $return_audio .= '<tr>';
  5079. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5080. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5081. $return_audio .= '</tr>';
  5082. if ($update_audio != 'true') {
  5083. $return .= '<div class="col-md-12">';
  5084. $return .= self::return_new_tree($update_audio);
  5085. $return .='</div>';
  5086. $return .= Display::div(
  5087. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5088. array('style' => 'float:left; margin-top:15px;width:100%')
  5089. );
  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(
  5098. 'save_audio',
  5099. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5100. array('class' => 'btn btn-primary', 'type' => 'submit')
  5101. );
  5102. $return .= '</div>';
  5103. }
  5104. }
  5105. // We need to close the form when we are updating the mp3 files.
  5106. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5107. $return .= '</form>';
  5108. }
  5109. return $return;
  5110. }
  5111. /**
  5112. * @param string string $update_audio
  5113. * @param bool $drop_element_here
  5114. * @return string
  5115. */
  5116. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5117. {
  5118. $return = '';
  5119. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5120. $course_id = api_get_course_int_id();
  5121. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5122. $sql = "SELECT * FROM $tbl_lp_item
  5123. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5124. $result = Database::query($sql);
  5125. $arrLP = array();
  5126. while ($row = Database :: fetch_array($result)) {
  5127. $arrLP[] = array(
  5128. 'id' => $row['id'],
  5129. 'item_type' => $row['item_type'],
  5130. 'title' => Security :: remove_XSS($row['title']),
  5131. 'path' => $row['path'],
  5132. 'description' => Security::remove_XSS($row['description']),
  5133. 'parent_item_id' => $row['parent_item_id'],
  5134. 'previous_item_id' => $row['previous_item_id'],
  5135. 'next_item_id' => $row['next_item_id'],
  5136. 'max_score' => $row['max_score'],
  5137. 'min_score' => $row['min_score'],
  5138. 'mastery_score' => $row['mastery_score'],
  5139. 'prerequisite' => $row['prerequisite'],
  5140. 'display_order' => $row['display_order'],
  5141. 'audio' => $row['audio'],
  5142. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5143. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5144. );
  5145. }
  5146. $this->tree_array($arrLP);
  5147. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5148. unset ($this->arrMenu);
  5149. $default_data = null;
  5150. $default_content = null;
  5151. $elements = array();
  5152. $return_audio = null;
  5153. for ($i = 0; $i < count($arrLP); $i++) {
  5154. $title = $arrLP[$i]['title'];
  5155. $title_cut = cut($arrLP[$i]['title'], 25);
  5156. // Link for the documents
  5157. if ($arrLP[$i]['item_type'] == 'document') {
  5158. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5159. $title_cut = Display::url(
  5160. $title_cut,
  5161. $url,
  5162. array(
  5163. 'class' => 'ajax moved',
  5164. 'data-title' => $title_cut
  5165. )
  5166. );
  5167. }
  5168. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5169. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5170. $_SESSION['pathItem'] = $arrLP[$i]['path'];
  5171. }
  5172. if (($i % 2) == 0) {
  5173. $oddClass = 'row_odd';
  5174. } else {
  5175. $oddClass = 'row_even';
  5176. }
  5177. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5178. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5179. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5180. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5181. } else {
  5182. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5183. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5184. } else {
  5185. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5186. $icon = Display::return_icon('certificate.png');
  5187. } else {
  5188. $icon = Display::return_icon('folder_document.gif');
  5189. }
  5190. }
  5191. }
  5192. // The audio column.
  5193. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5194. $audio = '';
  5195. if (!$update_audio || $update_audio <> 'true') {
  5196. if (!empty($arrLP[$i]['audio'])) {
  5197. } else {
  5198. $audio .= '';
  5199. }
  5200. } else {
  5201. $types = self::getChapterTypes();
  5202. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5203. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5204. if (!empty ($arrLP[$i]['audio'])) {
  5205. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5206. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5207. }
  5208. }
  5209. }
  5210. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5211. $return_audio .= '</td>';
  5212. $move_icon = '';
  5213. $move_item_icon = '';
  5214. $edit_icon = '';
  5215. $delete_icon = '';
  5216. $audio_icon = '';
  5217. $prerequisities_icon = '';
  5218. $forumIcon = '';
  5219. $previewIcon = '';
  5220. if ($is_allowed_to_edit) {
  5221. if (!$update_audio || $update_audio <> 'true') {
  5222. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5223. $move_icon .= '<a class="moved" href="#">';
  5224. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5225. $move_icon .= '</a>';
  5226. }
  5227. }
  5228. // No edit for this item types
  5229. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5230. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5231. $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">';
  5232. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5233. $edit_icon .= '</a>';
  5234. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5235. if (
  5236. $this->items[$arrLP[$i]['id']]->getForumThread(
  5237. $this->course_int_id,
  5238. $this->lp_session_id
  5239. )
  5240. ) {
  5241. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5242. 'action' => 'dissociate_forum',
  5243. 'id' => $arrLP[$i]['id'],
  5244. 'lp_id' => $this->lp_id
  5245. ]);
  5246. $forumIcon = Display::url(
  5247. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5248. $forumIconUrl,
  5249. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5250. );
  5251. } else {
  5252. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5253. 'action' => 'create_forum',
  5254. 'id' => $arrLP[$i]['id'],
  5255. 'lp_id' => $this->lp_id
  5256. ]);
  5257. $forumIcon = Display::url(
  5258. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5259. $forumIconUrl,
  5260. ['class' => "btn btn-default lp-btn-associate-forum"]
  5261. );
  5262. }
  5263. }
  5264. } else {
  5265. $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">';
  5266. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5267. $edit_icon .= '</a>';
  5268. }
  5269. }
  5270. $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">';
  5271. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5272. $delete_icon .= '</a>';
  5273. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5274. if ($arrLP[$i]['item_type'] == 'document') {
  5275. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5276. $previewIcon = Display::url(
  5277. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5278. $urlPreviewLink,
  5279. array(
  5280. 'class' => 'btn btn-default ajax',
  5281. 'data-title' => $arrLP[$i]['title']
  5282. )
  5283. );
  5284. } else {
  5285. $previewIcon = Display::url(
  5286. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5287. $url.'&action=view_item', ['class' => 'btn btn-default']
  5288. );
  5289. }
  5290. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5291. $prerequisities_icon = Display::url(
  5292. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5293. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5294. );
  5295. $move_item_icon = Display::url(
  5296. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5297. $url.'&action=move_item',
  5298. ['class' => 'btn btn-default']
  5299. );
  5300. $audio_icon = Display::url(
  5301. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5302. $url.'&action=add_audio',
  5303. ['class' => 'btn btn-default']
  5304. );
  5305. }
  5306. }
  5307. if ($update_audio != 'true') {
  5308. $row = $move_icon . ' ' . $icon .
  5309. Display::span($title_cut) .
  5310. Display::tag(
  5311. 'div',
  5312. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5313. array('class'=>'btn-toolbar button_actions')
  5314. );
  5315. } else {
  5316. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5317. }
  5318. $parent_id = $arrLP[$i]['parent_item_id'];
  5319. $default_data[$arrLP[$i]['id']] = $row;
  5320. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5321. if (empty($parent_id)) {
  5322. $elements[$arrLP[$i]['id']]['data'] = $row;
  5323. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5324. } else {
  5325. $parent_arrays = array();
  5326. if ($arrLP[$i]['depth'] > 1) {
  5327. //Getting list of parents
  5328. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5329. foreach($arrLP as $item) {
  5330. if ($item['id'] == $parent_id) {
  5331. if ($item['parent_item_id'] == 0) {
  5332. $parent_id = $item['id'];
  5333. break;
  5334. } else {
  5335. $parent_id = $item['parent_item_id'];
  5336. if (empty($parent_arrays)) {
  5337. $parent_arrays[] = intval($item['id']);
  5338. }
  5339. $parent_arrays[] = $parent_id;
  5340. break;
  5341. }
  5342. }
  5343. }
  5344. }
  5345. }
  5346. if (!empty($parent_arrays)) {
  5347. $parent_arrays = array_reverse($parent_arrays);
  5348. $val = '$elements';
  5349. $x = 0;
  5350. foreach($parent_arrays as $item) {
  5351. if ($x != count($parent_arrays) -1) {
  5352. $val .= '["'.$item.'"]["children"]';
  5353. } else {
  5354. $val .= '["'.$item.'"]["children"]';
  5355. }
  5356. $x++;
  5357. }
  5358. $val .= "";
  5359. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5360. eval($code_str);
  5361. } else {
  5362. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5363. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5364. }
  5365. }
  5366. }
  5367. $list = '<ul id="lp_item_list">';
  5368. $tree = self::print_recursive($elements, $default_data, $default_content);
  5369. if (!empty($tree)) {
  5370. $list .= $tree;
  5371. } else {
  5372. if ($drop_element_here) {
  5373. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5374. }
  5375. }
  5376. $list .= '</ul>';
  5377. $return .= Display::panelCollapse(
  5378. $this->name,
  5379. $list,
  5380. 'scorm-list',
  5381. null,
  5382. 'scorm-list-accordion',
  5383. 'scorm-list-collapse'
  5384. );
  5385. if ($update_audio == 'true') {
  5386. $return = $return_audio;
  5387. }
  5388. return $return;
  5389. }
  5390. /**
  5391. * @param array $elements
  5392. * @param array $default_data
  5393. * @param array $default_content
  5394. * @return string
  5395. */
  5396. public function print_recursive($elements, $default_data, $default_content)
  5397. {
  5398. $return = '';
  5399. foreach ($elements as $key => $item) {
  5400. if (isset($item['load_data']) || empty($item['data'])) {
  5401. $item['data'] = $default_data[$item['load_data']];
  5402. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5403. }
  5404. $sub_list = '';
  5405. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5406. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5407. }
  5408. if (empty($item['children'])) {
  5409. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5410. $active = null;
  5411. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5412. $active = 'active';
  5413. }
  5414. $return .= Display::tag(
  5415. 'li',
  5416. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5417. array('id'=>$key, 'class'=>'record li_container')
  5418. );
  5419. } else {
  5420. // Sections
  5421. if (isset($item['children'])) {
  5422. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5423. }
  5424. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5425. $return .= Display::tag(
  5426. 'li',
  5427. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5428. array('id'=>$key, 'class'=>'record li_container')
  5429. );
  5430. }
  5431. }
  5432. return $return;
  5433. }
  5434. /**
  5435. * This function builds the action menu
  5436. * @param bool $returnContent
  5437. * @return void
  5438. */
  5439. public function build_action_menu($returnContent = false)
  5440. {
  5441. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5442. $return = '<div class="actions">';
  5443. $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> ';
  5444. $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>';
  5445. $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>';
  5446. $buttons = array(
  5447. array(
  5448. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5449. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5450. ),
  5451. array(
  5452. 'title' => get_lang('ClearAllPrerequisites'),
  5453. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5454. ),
  5455. );
  5456. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5457. $return .= '</div>';
  5458. if ($returnContent) {
  5459. return $return;
  5460. }
  5461. echo $return;
  5462. }
  5463. /**
  5464. * Creates the default learning path folder
  5465. * @param array $course
  5466. * @param int $creatorId
  5467. *
  5468. * @return bool
  5469. */
  5470. public static function generate_learning_path_folder($course, $creatorId = 0)
  5471. {
  5472. // Creating learning_path folder
  5473. $dir = '/learning_path';
  5474. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5475. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5476. $folder = false;
  5477. if (!is_dir($filepath.'/'.$dir)) {
  5478. $folderData = create_unexisting_directory(
  5479. $course,
  5480. $creatorId,
  5481. api_get_session_id(),
  5482. 0,
  5483. 0,
  5484. $filepath,
  5485. $dir,
  5486. get_lang('LearningPaths'),
  5487. 0
  5488. );
  5489. if (!empty($folderData)) {
  5490. $folder = true;
  5491. }
  5492. } else {
  5493. $folder = true;
  5494. }
  5495. return $folder;
  5496. }
  5497. /**
  5498. * @param array $course
  5499. * @param string $lp_name
  5500. * @param int $creatorId
  5501. *
  5502. * @return array
  5503. */
  5504. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5505. {
  5506. $filepath = '';
  5507. $dir = '/learning_path/';
  5508. if (empty($lp_name)) {
  5509. $lp_name = $this->name;
  5510. }
  5511. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5512. $folder = self::generate_learning_path_folder($course, $creatorId);
  5513. // Limits title size
  5514. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5515. $dir = $dir.$title;
  5516. // Creating LP folder
  5517. $documentId = null;
  5518. if ($folder) {
  5519. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5520. if (!is_dir($filepath.'/'.$dir)) {
  5521. $folderData = create_unexisting_directory(
  5522. $course,
  5523. $creatorId,
  5524. 0,
  5525. 0,
  5526. 0,
  5527. $filepath,
  5528. $dir,
  5529. $lp_name
  5530. );
  5531. if (!empty($folderData)) {
  5532. $folder = true;
  5533. }
  5534. $documentId = $folderData['id'];
  5535. } else {
  5536. $folder = true;
  5537. }
  5538. $dir = $dir.'/';
  5539. if ($folder) {
  5540. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5541. }
  5542. }
  5543. if (empty($documentId)) {
  5544. $dir = api_remove_trailing_slash($dir);
  5545. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5546. }
  5547. $array = array(
  5548. 'dir' => $dir,
  5549. 'filepath' => $filepath,
  5550. 'folder' => $folder,
  5551. 'id' => $documentId
  5552. );
  5553. return $array;
  5554. }
  5555. /**
  5556. * Create a new document //still needs some finetuning
  5557. * @param array $courseInfo
  5558. * @param string $content
  5559. * @param string $title
  5560. * @param string $extension
  5561. * @param int $parentId
  5562. * @param int $creatorId creator id
  5563. *
  5564. * @return string
  5565. */
  5566. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5567. {
  5568. if (!empty($courseInfo)) {
  5569. $course_id = $courseInfo['real_id'];
  5570. } else {
  5571. $course_id = api_get_course_int_id();
  5572. }
  5573. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5574. $sessionId = api_get_session_id();
  5575. // Generates folder
  5576. $result = $this->generate_lp_folder($courseInfo);
  5577. $dir = $result['dir'];
  5578. if (empty($parentId)) {
  5579. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5580. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5581. // Please, do not modify this dirname formatting.
  5582. if (strstr($dir, '..')) {
  5583. $dir = '/';
  5584. }
  5585. if (!empty($dir[0]) && $dir[0] == '.') {
  5586. $dir = substr($dir, 1);
  5587. }
  5588. if (!empty($dir[0]) && $dir[0] != '/') {
  5589. $dir = '/'.$dir;
  5590. }
  5591. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5592. $dir .= '/';
  5593. }
  5594. } else {
  5595. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5596. if (!empty($parentInfo)) {
  5597. $dir = $parentInfo['path'].'/';
  5598. }
  5599. }
  5600. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5601. if (!is_dir($filepath)) {
  5602. $dir = '/';
  5603. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5604. }
  5605. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5606. // is already escaped twice when it gets here.
  5607. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5608. if (!empty($title)) {
  5609. $title = api_replace_dangerous_char(stripslashes($title));
  5610. } else {
  5611. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5612. }
  5613. $title = disable_dangerous_file($title);
  5614. $filename = $title;
  5615. $content = !empty($content) ? $content : $_POST['content_lp'];
  5616. $tmp_filename = $filename;
  5617. $i = 0;
  5618. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5619. $tmp_filename = $filename . '_' . ++ $i;
  5620. }
  5621. $filename = $tmp_filename . '.'.$extension;
  5622. if ($extension == 'html') {
  5623. $content = stripslashes($content);
  5624. $content = str_replace(
  5625. api_get_path(WEB_COURSE_PATH),
  5626. api_get_path(REL_PATH).'courses/',
  5627. $content
  5628. );
  5629. // Change the path of mp3 to absolute.
  5630. // The first regexp deals with :// urls.
  5631. $content = preg_replace(
  5632. "|(flashvars=\"file=)([^:/]+)/|",
  5633. "$1".api_get_path(
  5634. REL_COURSE_PATH
  5635. ).$courseInfo['path'].'/document/',
  5636. $content
  5637. );
  5638. // The second regexp deals with audio/ urls.
  5639. $content = preg_replace(
  5640. "|(flashvars=\"file=)([^/]+)/|",
  5641. "$1".api_get_path(
  5642. REL_COURSE_PATH
  5643. ).$courseInfo['path'].'/document/$2/',
  5644. $content
  5645. );
  5646. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5647. $content = str_replace(
  5648. '</body>',
  5649. '<style type="text/css">body{}</style></body>',
  5650. $content
  5651. );
  5652. }
  5653. if (!file_exists($filepath . $filename)) {
  5654. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5655. fputs($fp, $content);
  5656. fclose($fp);
  5657. $file_size = filesize($filepath . $filename);
  5658. $save_file_path = $dir.$filename;
  5659. $document_id = add_document(
  5660. $courseInfo,
  5661. $save_file_path,
  5662. 'file',
  5663. $file_size,
  5664. $tmp_filename,
  5665. '',
  5666. 0, //readonly
  5667. true,
  5668. null,
  5669. $sessionId,
  5670. $creatorId
  5671. );
  5672. if ($document_id) {
  5673. api_item_property_update(
  5674. $courseInfo,
  5675. TOOL_DOCUMENT,
  5676. $document_id,
  5677. 'DocumentAdded',
  5678. $creatorId,
  5679. null,
  5680. null,
  5681. null,
  5682. null,
  5683. $sessionId
  5684. );
  5685. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5686. $new_title = $originalTitle;
  5687. if ($new_comment || $new_title) {
  5688. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5689. $ct = '';
  5690. if ($new_comment)
  5691. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5692. if ($new_title)
  5693. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5694. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5695. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5696. Database::query($sql);
  5697. }
  5698. }
  5699. return $document_id;
  5700. }
  5701. }
  5702. }
  5703. /**
  5704. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5705. * @param array $_course array
  5706. * @return void
  5707. */
  5708. public function edit_document($_course)
  5709. {
  5710. $course_id = api_get_course_int_id();
  5711. global $_configuration;
  5712. // Please, do not modify this dirname formatting.
  5713. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5714. if (strstr($dir, '..'))
  5715. $dir = '/';
  5716. if ($dir[0] == '.')
  5717. $dir = substr($dir, 1);
  5718. if ($dir[0] != '/')
  5719. $dir = '/' . $dir;
  5720. if ($dir[strlen($dir) - 1] != '/')
  5721. $dir .= '/';
  5722. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5723. if (!is_dir($filepath)) {
  5724. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5725. }
  5726. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5727. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5728. $document_id = intval($_POST['path']);
  5729. $sql = "SELECT path FROM " . $table_doc . "
  5730. WHERE c_id = $course_id AND id = " . $document_id;
  5731. $res = Database::query($sql);
  5732. $row = Database :: fetch_array($res);
  5733. $content = stripslashes($_POST['content_lp']);
  5734. $file = $filepath . $row['path'];
  5735. if ($fp = @ fopen($file, 'w')) {
  5736. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].api_get_path(REL_COURSE_PATH), $content);
  5737. // Change the path of mp3 to absolute.
  5738. // The first regexp deals with :// urls.
  5739. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5740. // The second regexp deals with audio/ urls.
  5741. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5742. fputs($fp, $content);
  5743. fclose($fp);
  5744. $sql = "UPDATE " . $table_doc ." SET
  5745. title='".Database::escape_string($_POST['title'])."'
  5746. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5747. Database::query($sql);
  5748. }
  5749. }
  5750. }
  5751. /**
  5752. * Displays the selected item, with a panel for manipulating the item
  5753. * @param int $item_id
  5754. * @param string $msg
  5755. * @return string
  5756. */
  5757. public function display_item($item_id, $msg = null, $show_actions = true)
  5758. {
  5759. $course_id = api_get_course_int_id();
  5760. $return = '';
  5761. if (is_numeric($item_id)) {
  5762. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5763. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5764. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5765. $result = Database::query($sql);
  5766. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5767. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5768. // Prevents wrong parent selection for document, see Bug#1251.
  5769. if ($row['item_type'] != 'dokeos_chapter' && $row['item_type'] != 'dokeos_module') {
  5770. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5771. }
  5772. if ($show_actions) {
  5773. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5774. }
  5775. $return .= '<div style="padding:10px;">';
  5776. if ($msg != '')
  5777. $return .= $msg;
  5778. $return .= '<h3>'.$row['title'].'</h3>';
  5779. switch ($row['item_type']) {
  5780. case TOOL_QUIZ:
  5781. if (!empty($row['path'])) {
  5782. $exercise = new Exercise();
  5783. $exercise->read($row['path']);
  5784. $return .= $exercise->description.'<br />';
  5785. $return .= Display::url(
  5786. get_lang('GoToExercise'),
  5787. api_get_path(WEB_CODE_PATH).'exercice/overview.php?'.api_get_cidreq().'&exerciseId='.$exercise->id,
  5788. ['class' => 'btn btn-primary']
  5789. );
  5790. }
  5791. break;
  5792. case TOOL_DOCUMENT:
  5793. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5794. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5795. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5796. $result = Database::query($sql_doc);
  5797. $path_file = Database::result($result, 0, 0);
  5798. $path_parts = pathinfo($path_file);
  5799. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5800. if (in_array($path_parts['extension'], array(
  5801. 'html',
  5802. 'txt',
  5803. 'png',
  5804. 'jpg',
  5805. 'JPG',
  5806. 'jpeg',
  5807. 'JPEG',
  5808. 'gif',
  5809. 'swf'
  5810. ))) {
  5811. $return .= $this->display_document($row['path'], true, true);
  5812. }
  5813. break;
  5814. }
  5815. $return .= '</div>';
  5816. }
  5817. }
  5818. return $return;
  5819. }
  5820. /**
  5821. * Shows the needed forms for editing a specific item
  5822. * @param int $item_id
  5823. * @return string
  5824. */
  5825. public function display_edit_item($item_id)
  5826. {
  5827. $course_id = api_get_course_int_id();
  5828. $return = '';
  5829. if (is_numeric($item_id)) {
  5830. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5831. $sql = "SELECT * FROM $tbl_lp_item
  5832. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5833. $res = Database::query($sql);
  5834. $row = Database::fetch_array($res);
  5835. switch ($row['item_type']) {
  5836. case 'dokeos_chapter' :
  5837. case 'dir' :
  5838. case 'asset' :
  5839. case 'sco' :
  5840. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5841. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5842. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5843. } else {
  5844. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5845. }
  5846. break;
  5847. case TOOL_DOCUMENT :
  5848. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5849. $sql = "SELECT lp.*, doc.path as dir
  5850. FROM " . $tbl_lp_item . " as lp
  5851. LEFT JOIN " . $tbl_doc . " as doc
  5852. ON doc.id = lp.path
  5853. WHERE
  5854. lp.c_id = $course_id AND
  5855. doc.c_id = $course_id AND
  5856. lp.id = " . intval($item_id);
  5857. $res_step = Database::query($sql);
  5858. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5859. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5860. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5861. break;
  5862. case TOOL_LINK :
  5863. $link_id = (string) $row['path'];
  5864. if (ctype_digit($link_id)) {
  5865. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5866. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5867. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5868. $res_link = Database::query($sql_select);
  5869. $row_link = Database :: fetch_array($res_link);
  5870. if (is_array($row_link)) {
  5871. $row['url'] = $row_link['url'];
  5872. }
  5873. }
  5874. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5875. $return .= $this->display_link_form('edit', $item_id, $row);
  5876. break;
  5877. case TOOL_LP_FINAL_ITEM :
  5878. $_SESSION['finalItem'] = true;
  5879. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5880. $sql = "SELECT lp.*, doc.path as dir
  5881. FROM " . $tbl_lp_item . " as lp
  5882. LEFT JOIN " . $tbl_doc . " as doc
  5883. ON doc.id = lp.path
  5884. WHERE
  5885. lp.c_id = $course_id AND
  5886. doc.c_id = $course_id AND
  5887. lp.id = " . intval($item_id);
  5888. $res_step = Database::query($sql);
  5889. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5890. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5891. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5892. break;
  5893. case 'dokeos_module' :
  5894. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5895. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5896. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5897. } else {
  5898. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5899. }
  5900. break;
  5901. case TOOL_QUIZ :
  5902. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5903. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5904. break;
  5905. case TOOL_HOTPOTATOES :
  5906. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5907. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5908. break;
  5909. case TOOL_STUDENTPUBLICATION :
  5910. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5911. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5912. break;
  5913. case TOOL_FORUM :
  5914. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5915. $return .= $this->display_forum_form('edit', $item_id, $row);
  5916. break;
  5917. case TOOL_THREAD :
  5918. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5919. $return .= $this->display_thread_form('edit', $item_id, $row);
  5920. break;
  5921. }
  5922. }
  5923. return $return;
  5924. }
  5925. /**
  5926. * Function that displays a list with al the resources that
  5927. * could be added to the learning path
  5928. * @return string
  5929. */
  5930. public function display_resources()
  5931. {
  5932. $course_code = api_get_course_id();
  5933. // Get all the docs.
  5934. $documents = $this->get_documents(true);
  5935. // Get all the exercises.
  5936. $exercises = $this->get_exercises();
  5937. // Get all the links.
  5938. $links = $this->get_links();
  5939. // Get all the student publications.
  5940. $works = $this->get_student_publications();
  5941. // Get all the forums.
  5942. $forums = $this->get_forums(null, $course_code);
  5943. // Get the final item form (see BT#11048) .
  5944. $finish = $this->getFinalItemForm();
  5945. $headers = array(
  5946. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5947. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5948. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5949. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5950. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5951. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  5952. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  5953. );
  5954. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5955. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5956. echo Display::tabs(
  5957. $headers,
  5958. array($documents, $exercises, $links, $works, $forums, $chapter, $finish), 'resource_tab'
  5959. );
  5960. return true;
  5961. }
  5962. /**
  5963. * Returns the extension of a document
  5964. * @param string filename
  5965. * @return string Extension (part after the last dot)
  5966. */
  5967. public function get_extension($filename)
  5968. {
  5969. $explode = explode('.', $filename);
  5970. return $explode[count($explode) - 1];
  5971. }
  5972. /**
  5973. * Displays a document by id
  5974. *
  5975. * @param int $id
  5976. * @return string
  5977. */
  5978. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5979. {
  5980. $_course = api_get_course_info();
  5981. $course_id = api_get_course_int_id();
  5982. $return = '';
  5983. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5984. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5985. WHERE c_id = ".$course_id." AND id = " . $id;
  5986. $res_doc = Database::query($sql_doc);
  5987. $row_doc = Database :: fetch_array($res_doc);
  5988. // TODO: Add a path filter.
  5989. if ($iframe) {
  5990. $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>';
  5991. } else {
  5992. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5993. }
  5994. return $return;
  5995. }
  5996. /**
  5997. * Return HTML form to add/edit a quiz
  5998. * @param string Action (add/edit)
  5999. * @param integer Item ID if already exists
  6000. * @param mixed Extra information (quiz ID if integer)
  6001. * @return string HTML form
  6002. */
  6003. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6004. {
  6005. $course_id = api_get_course_int_id();
  6006. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6007. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6008. if ($id != 0 && is_array($extra_info)) {
  6009. $item_title = $extra_info['title'];
  6010. $item_description = $extra_info['description'];
  6011. } elseif (is_numeric($extra_info)) {
  6012. $sql = "SELECT title, description
  6013. FROM " . $tbl_quiz . "
  6014. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6015. $result = Database::query($sql);
  6016. $row = Database::fetch_array($result);
  6017. $item_title = $row['title'];
  6018. $item_description = $row['description'];
  6019. } else {
  6020. $item_title = '';
  6021. $item_description = '';
  6022. }
  6023. $item_title = Security::remove_XSS($item_title);
  6024. $item_description = Security::remove_XSS($item_description);
  6025. if ($id != 0 && is_array($extra_info))
  6026. $parent = $extra_info['parent_item_id'];
  6027. else
  6028. $parent = 0;
  6029. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6030. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6031. $result = Database::query($sql);
  6032. $arrLP = array ();
  6033. while ($row = Database :: fetch_array($result)) {
  6034. $arrLP[] = array (
  6035. 'id' => $row['id'],
  6036. 'item_type' => $row['item_type'],
  6037. 'title' => $row['title'],
  6038. 'path' => $row['path'],
  6039. 'description' => $row['description'],
  6040. 'parent_item_id' => $row['parent_item_id'],
  6041. 'previous_item_id' => $row['previous_item_id'],
  6042. 'next_item_id' => $row['next_item_id'],
  6043. 'display_order' => $row['display_order'],
  6044. 'max_score' => $row['max_score'],
  6045. 'min_score' => $row['min_score'],
  6046. 'mastery_score' => $row['mastery_score'],
  6047. 'prerequisite' => $row['prerequisite'],
  6048. 'max_time_allowed' => $row['max_time_allowed']
  6049. );
  6050. }
  6051. $this->tree_array($arrLP);
  6052. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6053. unset ($this->arrMenu);
  6054. $form = new FormValidator('quiz_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6055. $defaults = [];
  6056. if ($action == 'add') {
  6057. $legend = get_lang('CreateTheExercise');
  6058. } elseif ($action == 'move') {
  6059. $legend = get_lang('MoveTheCurrentExercise');
  6060. } else {
  6061. $legend = get_lang('EditCurrentExecice');
  6062. }
  6063. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6064. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6065. }
  6066. $form->addHeader($legend);
  6067. if ($action != 'move') {
  6068. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6069. $defaults['title'] = $item_title;
  6070. }
  6071. // Select for Parent item, root or chapter
  6072. $selectParent = $form->addSelect(
  6073. 'parent',
  6074. get_lang('Parent'),
  6075. [],
  6076. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6077. );
  6078. $selectParent->addOption($this->name, 0);
  6079. $arrHide = array (
  6080. $id
  6081. );
  6082. for ($i = 0; $i < count($arrLP); $i++) {
  6083. if ($action != 'add') {
  6084. if (
  6085. (
  6086. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6087. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6088. $arrLP[$i]['item_type'] == 'dir'
  6089. ) &&
  6090. !in_array($arrLP[$i]['id'], $arrHide) &&
  6091. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6092. ) {
  6093. $selectParent->addOption(
  6094. $arrLP[$i]['title'],
  6095. $arrLP[$i]['id'],
  6096. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6097. );
  6098. if ($parent == $arrLP[$i]['id']) {
  6099. $selectParent->setSelected($arrLP[$i]['id']);
  6100. }
  6101. } else {
  6102. $arrHide[] = $arrLP[$i]['id'];
  6103. }
  6104. } else {
  6105. if (
  6106. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6107. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6108. $arrLP[$i]['item_type'] == 'dir'
  6109. ) {
  6110. $selectParent->addOption(
  6111. $arrLP[$i]['title'],
  6112. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6113. );
  6114. if ($parent == $arrLP[$i]['id']) {
  6115. $selectParent->setSelected($arrLP[$i]['id']);
  6116. }
  6117. }
  6118. }
  6119. }
  6120. if (is_array($arrLP)) {
  6121. reset($arrLP);
  6122. }
  6123. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6124. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6125. for ($i = 0; $i < count($arrLP); $i++) {
  6126. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6127. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6128. if (is_array($extra_info)) {
  6129. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6130. $selectPrevious->setSelected($arrLP[$i]['id']);
  6131. }
  6132. } elseif ($action == 'add') {
  6133. $selectPrevious->setSelected($arrLP[$i]['id']);
  6134. }
  6135. }
  6136. }
  6137. if ($action != 'move') {
  6138. $id_prerequisite = 0;
  6139. if (is_array($arrLP)) {
  6140. foreach ($arrLP as $key => $value) {
  6141. if ($value['id'] == $id) {
  6142. $id_prerequisite = $value['prerequisite'];
  6143. break;
  6144. }
  6145. }
  6146. }
  6147. $arrHide = array ();
  6148. for ($i = 0; $i < count($arrLP); $i++) {
  6149. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6150. if (is_array($extra_info)) {
  6151. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6152. $s_selected_position = $arrLP[$i]['id'];
  6153. }
  6154. } elseif ($action == 'add') {
  6155. $s_selected_position = 0;
  6156. }
  6157. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6158. }
  6159. }
  6160. /*// Commented the prerequisites, only visible in edit (exercise).
  6161. $return .= '<tr>';
  6162. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6163. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6164. foreach($arrHide as $key => $value){
  6165. if($key==$s_selected_position && $action == 'add'){
  6166. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6167. }
  6168. elseif($key==$id_prerequisite && $action == 'edit'){
  6169. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6170. }
  6171. else{
  6172. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6173. }
  6174. }
  6175. $return .= "</select></td>";
  6176. */
  6177. /*$return .= '<tr>';
  6178. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6179. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6180. // Remove temporarily the test description.
  6181. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6182. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6183. $return .= '</tr>'; */
  6184. }
  6185. if ($action == 'add') {
  6186. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6187. } else {
  6188. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6189. }
  6190. if ($action == 'move') {
  6191. $form->addHidden('title', $item_title);
  6192. $form->addHidden('description', $item_description);
  6193. }
  6194. if (is_numeric($extra_info)) {
  6195. $form->addHidden('path', $extra_info);
  6196. } elseif (is_array($extra_info)) {
  6197. $form->addHidden('path', $extra_info['path']);
  6198. }
  6199. $form->addHidden('type', TOOL_QUIZ);
  6200. $form->addHidden('post_time', time());
  6201. $form->setDefaults($defaults);
  6202. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6203. }
  6204. /**
  6205. * Addition of Hotpotatoes tests
  6206. * @param string Action
  6207. * @param integer Internal ID of the item
  6208. * @param mixed Extra information - can be an array with title and description indexes
  6209. * @return string HTML structure to display the hotpotatoes addition formular
  6210. */
  6211. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6212. {
  6213. $course_id = api_get_course_int_id();
  6214. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6215. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6216. if ($id != 0 && is_array($extra_info)) {
  6217. $item_title = stripslashes($extra_info['title']);
  6218. $item_description = stripslashes($extra_info['description']);
  6219. } elseif (is_numeric($extra_info)) {
  6220. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6221. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6222. WHERE
  6223. c_id = ".$course_id." AND
  6224. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6225. id = " . (int) $extra_info . "
  6226. ORDER BY id ASC";
  6227. $res_hot = Database::query($sql);
  6228. $row = Database::fetch_array($res_hot);
  6229. $item_title = $row['title'];
  6230. $item_description = $row['description'];
  6231. if (!empty ($row['comment'])) {
  6232. $item_title = $row['comment'];
  6233. }
  6234. } else {
  6235. $item_title = '';
  6236. $item_description = '';
  6237. }
  6238. if ($id != 0 && is_array($extra_info)) {
  6239. $parent = $extra_info['parent_item_id'];
  6240. } else {
  6241. $parent = 0;
  6242. }
  6243. $sql = "SELECT * FROM $tbl_lp_item
  6244. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6245. $result = Database::query($sql);
  6246. $arrLP = array ();
  6247. while ($row = Database :: fetch_array($result)) {
  6248. $arrLP[] = array (
  6249. 'id' => $row['id'],
  6250. 'item_type' => $row['item_type'],
  6251. 'title' => $row['title'],
  6252. 'path' => $row['path'],
  6253. 'description' => $row['description'],
  6254. 'parent_item_id' => $row['parent_item_id'],
  6255. 'previous_item_id' => $row['previous_item_id'],
  6256. 'next_item_id' => $row['next_item_id'],
  6257. 'display_order' => $row['display_order'],
  6258. 'max_score' => $row['max_score'],
  6259. 'min_score' => $row['min_score'],
  6260. 'mastery_score' => $row['mastery_score'],
  6261. 'prerequisite' => $row['prerequisite'],
  6262. 'max_time_allowed' => $row['max_time_allowed']
  6263. );
  6264. }
  6265. $legend = '<legend>';
  6266. if ($action == 'add')
  6267. $legend .= get_lang('CreateTheExercise');
  6268. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6269. else
  6270. $legend .= get_lang('EditCurrentExecice');
  6271. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6272. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6273. }
  6274. $legend .= '</legend>';
  6275. $return = '<form method="POST">';
  6276. $return .= $legend;
  6277. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6278. $return .= '<tr>';
  6279. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6280. $return .= '<td class="input">';
  6281. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6282. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6283. $arrHide = array (
  6284. $id
  6285. );
  6286. if (count($arrLP) > 0) {
  6287. for ($i = 0; $i < count($arrLP); $i++) {
  6288. if ($action != 'add') {
  6289. 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)) {
  6290. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6291. } else {
  6292. $arrHide[] = $arrLP[$i]['id'];
  6293. }
  6294. } else {
  6295. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6296. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6297. }
  6298. }
  6299. reset($arrLP);
  6300. }
  6301. $return .= '</select>';
  6302. $return .= '</td>';
  6303. $return .= '</tr>';
  6304. $return .= '<tr>';
  6305. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6306. $return .= '<td class="input">';
  6307. $return .= '<select id="previous" name="previous" size="1">';
  6308. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6309. for ($i = 0; $i < count($arrLP); $i++) {
  6310. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6311. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6312. $selected = 'selected="selected" ';
  6313. elseif ($action == 'add') $selected = 'selected="selected" ';
  6314. else
  6315. $selected = '';
  6316. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6317. }
  6318. }
  6319. $return .= '</select>';
  6320. $return .= '</td>';
  6321. $return .= '</tr>';
  6322. if ($action != 'move') {
  6323. $return .= '<tr>';
  6324. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6325. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6326. $return .= '</tr>';
  6327. $id_prerequisite = 0;
  6328. if (is_array($arrLP) && count($arrLP) > 0) {
  6329. foreach ($arrLP as $key => $value) {
  6330. if ($value['id'] == $id) {
  6331. $id_prerequisite = $value['prerequisite'];
  6332. break;
  6333. }
  6334. }
  6335. $arrHide = array ();
  6336. for ($i = 0; $i < count($arrLP); $i++) {
  6337. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6338. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6339. $s_selected_position = $arrLP[$i]['id'];
  6340. elseif ($action == 'add') $s_selected_position = 0;
  6341. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6342. }
  6343. }
  6344. }
  6345. }
  6346. $return .= '<tr>';
  6347. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6348. $return .= '</tr>';
  6349. $return .= '</table>';
  6350. if ($action == 'move') {
  6351. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6352. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6353. }
  6354. if (is_numeric($extra_info)) {
  6355. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6356. } elseif (is_array($extra_info)) {
  6357. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6358. }
  6359. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6360. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6361. $return .= '</form>';
  6362. return $return;
  6363. }
  6364. /**
  6365. * Return the form to display the forum edit/add option
  6366. * @param string Action (add/edit)
  6367. * @param integer ID of the lp_item if already exists
  6368. * @param mixed Forum ID or title
  6369. * @return string HTML form
  6370. */
  6371. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6372. {
  6373. $course_id = api_get_course_int_id();
  6374. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6375. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6376. if ($id != 0 && is_array($extra_info)) {
  6377. $item_title = stripslashes($extra_info['title']);
  6378. } elseif (is_numeric($extra_info)) {
  6379. $sql = "SELECT forum_title as title, forum_comment as comment
  6380. FROM " . $tbl_forum . "
  6381. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6382. $result = Database::query($sql);
  6383. $row = Database :: fetch_array($result);
  6384. $item_title = $row['title'];
  6385. $item_description = $row['comment'];
  6386. } else {
  6387. $item_title = '';
  6388. $item_description = '';
  6389. }
  6390. if ($id != 0 && is_array($extra_info)) {
  6391. $parent = $extra_info['parent_item_id'];
  6392. } else {
  6393. $parent = 0;
  6394. }
  6395. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6396. WHERE
  6397. c_id = ".$course_id." AND
  6398. lp_id = " . $this->lp_id;
  6399. $result = Database::query($sql);
  6400. $arrLP = array();
  6401. while ($row = Database :: fetch_array($result)) {
  6402. $arrLP[] = array (
  6403. 'id' => $row['id'],
  6404. 'item_type' => $row['item_type'],
  6405. 'title' => $row['title'],
  6406. 'path' => $row['path'],
  6407. 'description' => $row['description'],
  6408. 'parent_item_id' => $row['parent_item_id'],
  6409. 'previous_item_id' => $row['previous_item_id'],
  6410. 'next_item_id' => $row['next_item_id'],
  6411. 'display_order' => $row['display_order'],
  6412. 'max_score' => $row['max_score'],
  6413. 'min_score' => $row['min_score'],
  6414. 'mastery_score' => $row['mastery_score'],
  6415. 'prerequisite' => $row['prerequisite']
  6416. );
  6417. }
  6418. $this->tree_array($arrLP);
  6419. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6420. unset($this->arrMenu);
  6421. if ($action == 'add') {
  6422. $legend = get_lang('CreateTheForum');
  6423. } elseif ($action == 'move') {
  6424. $legend = get_lang('MoveTheCurrentForum');
  6425. } else {
  6426. $legend = get_lang('EditCurrentForum');
  6427. }
  6428. $form = new FormValidator('forum_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6429. $defaults = [];
  6430. $form->addHeader($legend);
  6431. if ($action != 'move') {
  6432. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  6433. $defaults['title'] = $item_title;
  6434. }
  6435. $selectParent = $form->addSelect(
  6436. 'parent',
  6437. get_lang('Parent'),
  6438. [],
  6439. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6440. );
  6441. $selectParent->addOption($this->name, 0);
  6442. $arrHide = array(
  6443. $id
  6444. );
  6445. //$parent_item_id = $_SESSION['parent_item_id'];
  6446. for ($i = 0; $i < count($arrLP); $i++) {
  6447. if ($action != 'add') {
  6448. 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)) {
  6449. $selectParent->addOption(
  6450. $arrLP[$i]['title'],
  6451. $arrLP[$i]['id'],
  6452. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6453. );
  6454. if ($parent == $arrLP[$i]['id']) {
  6455. $selectParent->setSelected($arrLP[$i]['id']);
  6456. }
  6457. } else {
  6458. $arrHide[] = $arrLP[$i]['id'];
  6459. }
  6460. } else {
  6461. if (
  6462. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6463. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6464. $arrLP[$i]['item_type'] == 'dir'
  6465. ) {
  6466. $selectParent->addOption(
  6467. $arrLP[$i]['title'],
  6468. $arrLP[$i]['id'],
  6469. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6470. );
  6471. if ($parent == $arrLP[$i]['id']) {
  6472. $selectParent->setSelected($arrLP[$i]['id']);
  6473. }
  6474. }
  6475. }
  6476. }
  6477. if (is_array($arrLP)) {
  6478. reset($arrLP);
  6479. }
  6480. $selectPrevious = $form->addSelect(
  6481. 'previous',
  6482. get_lang('Position'),
  6483. [],
  6484. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6485. );
  6486. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6487. for ($i = 0; $i < count($arrLP); $i++) {
  6488. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6489. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6490. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6491. $selectPrevious->setSelected($arrLP[$i]['id']);
  6492. } elseif ($action == 'add') {
  6493. $selectPrevious->setSelected($arrLP[$i]['id']);
  6494. }
  6495. }
  6496. }
  6497. if ($action != 'move') {
  6498. $id_prerequisite = 0;
  6499. if (is_array($arrLP)) {
  6500. foreach ($arrLP as $key => $value) {
  6501. if ($value['id'] == $id) {
  6502. $id_prerequisite = $value['prerequisite'];
  6503. break;
  6504. }
  6505. }
  6506. }
  6507. $arrHide = array();
  6508. for ($i = 0; $i < count($arrLP); $i++) {
  6509. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6510. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6511. $s_selected_position = $arrLP[$i]['id'];
  6512. elseif ($action == 'add') $s_selected_position = 0;
  6513. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6514. }
  6515. }
  6516. }
  6517. if ($action == 'add') {
  6518. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6519. } else {
  6520. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6521. }
  6522. if ($action == 'move') {
  6523. $form->addHidden('title', $item_title);
  6524. $form->addHidden('description', $item_description);
  6525. }
  6526. if (is_numeric($extra_info)) {
  6527. $form->addHidden('path', $extra_info);
  6528. } elseif (is_array($extra_info)) {
  6529. $form->addHidden('path', $extra_info['path']);
  6530. }
  6531. $form->addHidden('type', TOOL_FORUM);
  6532. $form->addHidden('post_time', time());
  6533. $form->setDefaults($defaults);
  6534. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6535. }
  6536. /**
  6537. * Return HTML form to add/edit forum threads
  6538. * @param string Action (add/edit)
  6539. * @param integer Item ID if already exists in learning path
  6540. * @param mixed Extra information (thread ID if integer)
  6541. * @return string HTML form
  6542. */
  6543. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6544. {
  6545. $course_id = api_get_course_int_id();
  6546. if (empty($course_id)) {
  6547. return null;
  6548. }
  6549. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6550. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6551. if ($id != 0 && is_array($extra_info)) {
  6552. $item_title = stripslashes($extra_info['title']);
  6553. } elseif (is_numeric($extra_info)) {
  6554. $sql = "SELECT thread_title as title FROM $tbl_forum
  6555. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6556. $result = Database::query($sql);
  6557. $row = Database :: fetch_array($result);
  6558. $item_title = $row['title'];
  6559. $item_description = '';
  6560. } else {
  6561. $item_title = '';
  6562. $item_description = '';
  6563. }
  6564. if ($id != 0 && is_array($extra_info)) {
  6565. $parent = $extra_info['parent_item_id'];
  6566. } else {
  6567. $parent = 0;
  6568. }
  6569. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6570. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6571. $result = Database::query($sql);
  6572. $arrLP = array ();
  6573. while ($row = Database :: fetch_array($result)) {
  6574. $arrLP[] = array (
  6575. 'id' => $row['id'],
  6576. 'item_type' => $row['item_type'],
  6577. 'title' => $row['title'],
  6578. 'path' => $row['path'],
  6579. 'description' => $row['description'],
  6580. 'parent_item_id' => $row['parent_item_id'],
  6581. 'previous_item_id' => $row['previous_item_id'],
  6582. 'next_item_id' => $row['next_item_id'],
  6583. 'display_order' => $row['display_order'],
  6584. 'max_score' => $row['max_score'],
  6585. 'min_score' => $row['min_score'],
  6586. 'mastery_score' => $row['mastery_score'],
  6587. 'prerequisite' => $row['prerequisite']
  6588. );
  6589. }
  6590. $this->tree_array($arrLP);
  6591. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6592. unset ($this->arrMenu);
  6593. $form = new FormValidator('thread_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6594. $defaults = [];
  6595. if ($action == 'add') {
  6596. $legend = get_lang('CreateTheForum');
  6597. } elseif ($action == 'move') {
  6598. $legend = get_lang('MoveTheCurrentForum');
  6599. } else {
  6600. $legend = get_lang('EditCurrentForum');
  6601. }
  6602. $form->addHeader($legend);
  6603. $selectParent = $form->addSelect(
  6604. 'parent',
  6605. get_lang('Parent'),
  6606. [],
  6607. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6608. );
  6609. $selectParent->addOption($this->name, 0);
  6610. $arrHide = array (
  6611. $id
  6612. );
  6613. for ($i = 0; $i < count($arrLP); $i++) {
  6614. if ($action != 'add') {
  6615. if (
  6616. (
  6617. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6618. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6619. $arrLP[$i]['item_type'] == 'dir'
  6620. ) &&
  6621. !in_array($arrLP[$i]['id'], $arrHide) &&
  6622. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6623. ) {
  6624. $selectParent->addOption(
  6625. $arrLP[$i]['title'],
  6626. $arrLP[$i]['id'],
  6627. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6628. );
  6629. if ($parent == $arrLP[$i]['id']) {
  6630. $selectParent->setSelected($arrLP[$i]['id']);
  6631. }
  6632. } else {
  6633. $arrHide[] = $arrLP[$i]['id'];
  6634. }
  6635. } else {
  6636. if (
  6637. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6638. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6639. $arrLP[$i]['item_type'] == 'dir'
  6640. ) {
  6641. $selectParent->addOption(
  6642. $arrLP[$i]['title'],
  6643. $arrLP[$i]['id'],
  6644. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6645. );
  6646. if ($parent == $arrLP[$i]['id']) {
  6647. $selectParent->setSelected($arrLP[$i]['id']);
  6648. }
  6649. }
  6650. }
  6651. }
  6652. if ($arrLP != null) {
  6653. reset($arrLP);
  6654. }
  6655. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6656. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6657. for ($i = 0; $i < count($arrLP); $i++) {
  6658. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6659. $selectPrevious->addOption(
  6660. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6661. $arrLP[$i]['id']
  6662. );
  6663. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6664. $selectPrevious->setSelected($arrLP[$i]['id']);
  6665. } elseif ($action == 'add') {
  6666. $selectPrevious->setSelected($arrLP[$i]['id']);
  6667. }
  6668. }
  6669. }
  6670. if ($action != 'move') {
  6671. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6672. $defaults['title'] = $item_title;
  6673. $id_prerequisite = 0;
  6674. if ($arrLP != null) {
  6675. foreach ($arrLP as $key => $value) {
  6676. if ($value['id'] == $id) {
  6677. $id_prerequisite = $value['prerequisite'];
  6678. break;
  6679. }
  6680. }
  6681. }
  6682. $arrHide = array();
  6683. $s_selected_position = 0;
  6684. for ($i = 0; $i < count($arrLP); $i++) {
  6685. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6686. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6687. $s_selected_position = $arrLP[$i]['id'];
  6688. elseif ($action == 'add') $s_selected_position = 0;
  6689. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6690. }
  6691. }
  6692. $selectPrerequisites = $form->addSelect(
  6693. 'prerequisites',
  6694. get_lang('LearnpathPrerequisites'),
  6695. [],
  6696. ['id' => 'prerequisites']
  6697. );
  6698. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6699. foreach ($arrHide as $key => $value) {
  6700. $selectPrerequisites->addOption($value['value'], $key);
  6701. if ($key == $s_selected_position && $action == 'add') {
  6702. $selectPrerequisites->setSelected($key);
  6703. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6704. $selectPrerequisites->setSelected($key);
  6705. }
  6706. }
  6707. }
  6708. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6709. if ($action == 'move') {
  6710. $form->addHidden('title', $item_title);
  6711. $form->addHidden('description', $item_description);
  6712. }
  6713. if (is_numeric($extra_info)) {
  6714. $form->addHidden('path', $extra_info);
  6715. }
  6716. elseif (is_array($extra_info)) {
  6717. $form->addHidden('path', $extra_info['path']);
  6718. }
  6719. $form->addHidden('type', TOOL_THREAD);
  6720. $form->addHidden('post_time', time());
  6721. $form->setDefaults($defaults);
  6722. return $form->returnForm();
  6723. }
  6724. /**
  6725. * Return the HTML form to display an item (generally a section/module item)
  6726. * @param string Item type (module/dokeos_module)
  6727. * @param string Title (optional, only when creating)
  6728. * @param string Action ('add'/'edit')
  6729. * @param integer lp_item ID
  6730. * @param mixed Extra info
  6731. * @return string HTML form
  6732. */
  6733. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6734. {
  6735. $course_id = api_get_course_int_id();
  6736. $_course = api_get_course_info();
  6737. global $charset;
  6738. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6739. if ($id != 0 && is_array($extra_info)) {
  6740. $item_title = $extra_info['title'];
  6741. $item_description = $extra_info['description'];
  6742. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6743. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6744. } else {
  6745. $item_title = '';
  6746. $item_description = '';
  6747. $item_path_fck = '';
  6748. }
  6749. if ($id != 0 && is_array($extra_info)) {
  6750. $parent = $extra_info['parent_item_id'];
  6751. } else {
  6752. $parent = 0;
  6753. }
  6754. $id = intval($id);
  6755. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6756. WHERE
  6757. c_id = ".$course_id." AND
  6758. lp_id = " . $this->lp_id . " AND
  6759. id != $id";
  6760. if ($item_type == 'module')
  6761. $sql .= " AND parent_item_id = 0";
  6762. $result = Database::query($sql);
  6763. $arrLP = array ();
  6764. while ($row = Database :: fetch_array($result)) {
  6765. $arrLP[] = array(
  6766. 'id' => $row['id'],
  6767. 'item_type' => $row['item_type'],
  6768. 'title' => $row['title'],
  6769. 'path' => $row['path'],
  6770. 'description' => $row['description'],
  6771. 'parent_item_id' => $row['parent_item_id'],
  6772. 'previous_item_id' => $row['previous_item_id'],
  6773. 'next_item_id' => $row['next_item_id'],
  6774. 'max_score' => $row['max_score'],
  6775. 'min_score' => $row['min_score'],
  6776. 'mastery_score' => $row['mastery_score'],
  6777. 'prerequisite' => $row['prerequisite'],
  6778. 'display_order' => $row['display_order']
  6779. );
  6780. }
  6781. $this->tree_array($arrLP);
  6782. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6783. unset ($this->arrMenu);
  6784. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6785. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6786. $form = new FormValidator('form', 'POST', $url);
  6787. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6788. $defaults['description'] = $item_description;
  6789. $form->addElement('header', $title);
  6790. //$arrHide = array($id);
  6791. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6792. $arrHide[0]['padding'] = 20;
  6793. $charset = api_get_system_encoding();
  6794. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6795. for ($i = 0; $i < count($arrLP); $i++) {
  6796. if ($action != 'add') {
  6797. 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)) {
  6798. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6799. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6800. if ($parent == $arrLP[$i]['id']) {
  6801. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6802. }
  6803. }
  6804. } else {
  6805. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6806. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6807. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6808. if ($parent == $arrLP[$i]['id']) {
  6809. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6810. }
  6811. }
  6812. }
  6813. }
  6814. if ($action != 'move') {
  6815. $form->addElement('text', 'title', get_lang('Title'));
  6816. $form->applyFilter('title', 'html_filter');
  6817. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6818. } else {
  6819. $form->addElement('hidden', 'title');
  6820. }
  6821. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6822. foreach ($arrHide as $key => $value) {
  6823. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6824. }
  6825. if (!empty($s_selected_parent)) {
  6826. $parent_select->setSelected($s_selected_parent);
  6827. }
  6828. }
  6829. if (is_array($arrLP)) {
  6830. reset($arrLP);
  6831. }
  6832. $arrHide = array();
  6833. // POSITION
  6834. for ($i = 0; $i < count($arrLP); $i++) {
  6835. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6836. //this is the same!
  6837. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6838. $s_selected_position = $arrLP[$i]['id'];
  6839. } elseif ($action == 'add') {
  6840. $s_selected_position = $arrLP[$i]['id'];
  6841. }
  6842. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6843. }
  6844. }
  6845. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6846. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6847. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6848. foreach ($arrHide as $key => $value) {
  6849. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6850. }
  6851. if (!empty ($s_selected_position)) {
  6852. $position->setSelected($s_selected_position);
  6853. }
  6854. if (is_array($arrLP)) {
  6855. reset($arrLP);
  6856. }
  6857. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6858. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6859. $form->addElement('hidden', 'parent', '0');
  6860. }
  6861. //fix in order to use the tab
  6862. if ($item_type == 'chapter') {
  6863. $form->addElement('hidden', 'type', 'chapter');
  6864. }
  6865. $extension = null;
  6866. if (!empty($item_path)) {
  6867. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6868. }
  6869. //assets can't be modified
  6870. //$item_type == 'asset' ||
  6871. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6872. if ($item_type == 'sco') {
  6873. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6874. }
  6875. $renderer = $form->defaultRenderer();
  6876. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6877. $relative_prefix = '';
  6878. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6879. 'Width' => '100%',
  6880. 'Height' => '500',
  6881. 'FullPage' => true,
  6882. 'CreateDocumentDir' => $relative_prefix,
  6883. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6884. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6885. );
  6886. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6887. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6888. //$defaults['content_lp'] = file_get_contents($item_path);
  6889. $defaults['content_lp'] = file_get_contents($content_path);
  6890. }
  6891. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6892. $form->addElement('hidden', 'post_time', time());
  6893. $form->setDefaults($defaults);
  6894. return $form->return_form();
  6895. }
  6896. /**
  6897. * Returns the form to update or create a document
  6898. * @param string Action (add/edit)
  6899. * @param integer ID of the lp_item (if already exists)
  6900. * @param mixed Integer if document ID, string if info ('new')
  6901. * @return string HTML form
  6902. */
  6903. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6904. {
  6905. $course_id = api_get_course_int_id();
  6906. $_course = api_get_course_info();
  6907. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6908. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6909. $no_display_edit_textarea = false;
  6910. $item_description = '';
  6911. //If action==edit document
  6912. //We don't display the document form if it's not an editable document (html or txt file)
  6913. if ($action == "edit") {
  6914. if (is_array($extra_info)) {
  6915. $path_parts = pathinfo($extra_info['dir']);
  6916. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6917. $no_display_edit_textarea = true;
  6918. }
  6919. }
  6920. }
  6921. $no_display_add = false;
  6922. // If action==add an existing document
  6923. // We don't display the document form if it's not an editable document (html or txt file).
  6924. if ($action == "add") {
  6925. if (is_numeric($extra_info)) {
  6926. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6927. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6928. $result = Database::query($sql_doc);
  6929. $path_file = Database :: result($result, 0, 0);
  6930. $path_parts = pathinfo($path_file);
  6931. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6932. $no_display_add = true;
  6933. }
  6934. }
  6935. }
  6936. if ($id != 0 && is_array($extra_info)) {
  6937. $item_title = stripslashes($extra_info['title']);
  6938. $item_description = stripslashes($extra_info['description']);
  6939. $item_terms = stripslashes($extra_info['terms']);
  6940. if (empty ($item_title)) {
  6941. $path_parts = pathinfo($extra_info['path']);
  6942. $item_title = stripslashes($path_parts['filename']);
  6943. }
  6944. } elseif (is_numeric($extra_info)) {
  6945. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6946. WHERE
  6947. c_id = ".$course_id." AND
  6948. id = " . intval($extra_info);
  6949. $result = Database::query($sql_doc);
  6950. $row = Database::fetch_array($result);
  6951. $item_title = $row['title'];
  6952. $item_title = str_replace('_', ' ', $item_title);
  6953. if (empty ($item_title)) {
  6954. $path_parts = pathinfo($row['path']);
  6955. $item_title = stripslashes($path_parts['filename']);
  6956. }
  6957. } else {
  6958. $item_title = '';
  6959. $item_description = '';
  6960. }
  6961. $return = '<legend>';
  6962. if ($id != 0 && is_array($extra_info)) {
  6963. $parent = $extra_info['parent_item_id'];
  6964. } else {
  6965. $parent = 0;
  6966. }
  6967. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6968. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6969. $result = Database::query($sql);
  6970. $arrLP = array ();
  6971. while ($row = Database :: fetch_array($result)) {
  6972. $arrLP[] = array(
  6973. 'id' => $row['id'],
  6974. 'item_type' => $row['item_type'],
  6975. 'title' => $row['title'],
  6976. 'path' => $row['path'],
  6977. 'description' => $row['description'],
  6978. 'parent_item_id' => $row['parent_item_id'],
  6979. 'previous_item_id' => $row['previous_item_id'],
  6980. 'next_item_id' => $row['next_item_id'],
  6981. 'display_order' => $row['display_order'],
  6982. 'max_score' => $row['max_score'],
  6983. 'min_score' => $row['min_score'],
  6984. 'mastery_score' => $row['mastery_score'],
  6985. 'prerequisite' => $row['prerequisite']
  6986. );
  6987. }
  6988. $this->tree_array($arrLP);
  6989. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6990. unset ($this->arrMenu);
  6991. if ($action == 'add') {
  6992. $return .= get_lang('CreateTheDocument');
  6993. } elseif ($action == 'move') {
  6994. $return .= get_lang('MoveTheCurrentDocument');
  6995. } else {
  6996. $return .= get_lang('EditTheCurrentDocument');
  6997. }
  6998. $return .= '</legend>';
  6999. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7000. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7001. }
  7002. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  7003. $defaults['title'] = Security :: remove_XSS($item_title);
  7004. if (empty($item_title)) {
  7005. $defaults['title'] = Security::remove_XSS($item_title);
  7006. }
  7007. $defaults['description'] = $item_description;
  7008. $form->addElement('html', $return);
  7009. if ($action != 'move') {
  7010. $data = $this->generate_lp_folder($_course);
  7011. $folders = DocumentManager::get_all_document_folders(
  7012. $_course,
  7013. 0,
  7014. true
  7015. );
  7016. DocumentManager::build_directory_selector(
  7017. $folders,
  7018. '',
  7019. array(),
  7020. true,
  7021. $form,
  7022. 'directory_parent_id'
  7023. );
  7024. if (isset($data['id'])) {
  7025. $defaults['directory_parent_id'] = $data['id'];
  7026. }
  7027. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  7028. $form->applyFilter('title', 'html_filter');
  7029. }
  7030. $arrHide[0]['value'] = $this->name;
  7031. $arrHide[0]['padding'] = 20;
  7032. for ($i = 0; $i < count($arrLP); $i++) {
  7033. if ($action != 'add') {
  7034. 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)) {
  7035. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7036. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7037. if ($parent == $arrLP[$i]['id']) {
  7038. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7039. }
  7040. }
  7041. } else {
  7042. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7043. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7044. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7045. if ($parent == $arrLP[$i]['id']) {
  7046. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7047. }
  7048. }
  7049. }
  7050. }
  7051. $parent_select = $form->addSelect('parent', get_lang('Parent'), [], ['id' => 'idParent', 'onchange' => 'javascript: load_cbo(this.value);']);
  7052. $my_count=0;
  7053. foreach ($arrHide as $key => $value) {
  7054. if ($my_count!=0) {
  7055. // The LP name is also the first section and is not in the same charset like the other sections.
  7056. $value['value'] = Security :: remove_XSS($value['value']);
  7057. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7058. } else {
  7059. $value['value'] = Security :: remove_XSS($value['value']);
  7060. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7061. }
  7062. $my_count++;
  7063. }
  7064. if (!empty($id)) {
  7065. $parent_select->setSelected($parent);
  7066. } else {
  7067. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7068. $parent_select->setSelected($parent_item_id);
  7069. }
  7070. if (is_array($arrLP)) {
  7071. reset($arrLP);
  7072. }
  7073. $arrHide = array();
  7074. $s_selected_position = null;
  7075. //POSITION
  7076. for ($i = 0; $i < count($arrLP); $i++) {
  7077. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id || $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  7078. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'] || $action == 'add') {
  7079. $s_selected_position = $arrLP[$i]['id'];
  7080. }
  7081. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7082. }
  7083. }
  7084. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7085. $position->addOption(get_lang('FirstPosition'), 0);
  7086. foreach ($arrHide as $key => $value) {
  7087. $padding = isset($value['padding']) ? $value['padding']: 20;
  7088. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  7089. }
  7090. $position->setSelected($s_selected_position);
  7091. if (is_array($arrLP)) {
  7092. reset($arrLP);
  7093. }
  7094. if ($action != 'move') {
  7095. $id_prerequisite = 0;
  7096. if (is_array($arrLP)) {
  7097. foreach ($arrLP as $key => $value) {
  7098. if ($value['id'] == $id) {
  7099. $id_prerequisite = $value['prerequisite'];
  7100. break;
  7101. }
  7102. }
  7103. }
  7104. $arrHide = array();
  7105. for ($i = 0; $i < count($arrLP); $i++) {
  7106. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7107. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7108. $s_selected_position = $arrLP[$i]['id'];
  7109. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7110. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7111. }
  7112. }
  7113. if (!$no_display_add) {
  7114. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7115. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7116. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true')) {
  7117. if (isset ($_POST['content']))
  7118. $content = stripslashes($_POST['content']);
  7119. elseif (is_array($extra_info)) {
  7120. //If it's an html document or a text file
  7121. if (!$no_display_edit_textarea) {
  7122. $content = $this->display_document($extra_info['path'], false, false);
  7123. }
  7124. } elseif (is_numeric($extra_info))
  7125. $content = $this->display_document($extra_info, false, false);
  7126. else
  7127. $content = '';
  7128. if (!$no_display_edit_textarea) {
  7129. // We need to calculate here some specific settings for the online editor.
  7130. // The calculated settings work for documents in the Documents tool
  7131. // (on the root or in subfolders).
  7132. // For documents in native scorm packages it is unclear whether the
  7133. // online editor should be activated or not.
  7134. // A new document, it is in the root of the repository.
  7135. $relative_path = '';
  7136. $relative_prefix = '';
  7137. if (is_array($extra_info) && $extra_info != 'new') {
  7138. // The document already exists. Whe have to determine its relative path towards the repository root.
  7139. $relative_path = explode('/', $extra_info['dir']);
  7140. $cnt = count($relative_path) - 2;
  7141. if ($cnt < 0) {
  7142. $cnt = 0;
  7143. }
  7144. $relative_prefix = str_repeat('../', $cnt);
  7145. $relative_path = array_slice($relative_path, 1, $cnt);
  7146. $relative_path = implode('/', $relative_path);
  7147. if (strlen($relative_path) > 0) {
  7148. $relative_path = $relative_path . '/';
  7149. }
  7150. } else {
  7151. $result = $this->generate_lp_folder($_course);
  7152. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7153. $relative_prefix = '../../';
  7154. }
  7155. $editor_config = array(
  7156. 'ToolbarSet'=> 'LearningPathDocuments',
  7157. 'Width' => '100%',
  7158. 'Height' => '500',
  7159. 'FullPage' => true,
  7160. 'CreateDocumentDir' => $relative_prefix,
  7161. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  7162. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  7163. );
  7164. if ($_GET['action'] == 'add_item') {
  7165. $class = 'add';
  7166. $text = get_lang('LPCreateDocument');
  7167. } else {
  7168. if ($_GET['action'] == 'edit_item') {
  7169. $class = 'save';
  7170. $text = get_lang('SaveDocument');
  7171. }
  7172. }
  7173. $form->addButtonSave($text, 'submit_button');
  7174. $renderer = $form->defaultRenderer();
  7175. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7176. $form->addElement('html', '<div class="editor-lp">');
  7177. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7178. $form->addElement('html', '</div>');
  7179. $defaults['content_lp'] = $content;
  7180. }
  7181. } elseif (is_numeric($extra_info)) {
  7182. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7183. $return = $this->display_document($extra_info, true, true, true);
  7184. $form->addElement('html', $return);
  7185. }
  7186. }
  7187. }
  7188. if ($action == 'move') {
  7189. $form->addElement('hidden', 'title', $item_title);
  7190. $form->addElement('hidden', 'description', $item_description);
  7191. }
  7192. if (is_numeric($extra_info)) {
  7193. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7194. $form->addElement('hidden', 'path', $extra_info);
  7195. } elseif (is_array($extra_info)) {
  7196. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7197. $form->addElement('hidden', 'path', $extra_info['path']);
  7198. }
  7199. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7200. $form->addElement('hidden', 'post_time', time());
  7201. $form->setDefaults($defaults);
  7202. return $form->returnForm();
  7203. }
  7204. /**
  7205. * Return HTML form to add/edit a link item
  7206. * @param string $action (add/edit)
  7207. * @param integer $id Item ID if exists
  7208. * @param mixed $extra_info
  7209. * @return string HTML form
  7210. */
  7211. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7212. {
  7213. $course_id = api_get_course_int_id();
  7214. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7215. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7216. if ($id != 0 && is_array($extra_info)) {
  7217. $item_title = stripslashes($extra_info['title']);
  7218. $item_description = stripslashes($extra_info['description']);
  7219. $item_url = stripslashes($extra_info['url']);
  7220. } elseif (is_numeric($extra_info)) {
  7221. $extra_info = intval($extra_info);
  7222. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7223. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7224. $result = Database::query($sql);
  7225. $row = Database :: fetch_array($result);
  7226. $item_title = $row['title'];
  7227. $item_description = $row['description'];
  7228. $item_url = $row['url'];
  7229. } else {
  7230. $item_title = '';
  7231. $item_description = '';
  7232. $item_url = '';
  7233. }
  7234. $form = new FormValidator('edit_link', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  7235. $defaults = [];
  7236. if ($id != 0 && is_array($extra_info)) {
  7237. $parent = $extra_info['parent_item_id'];
  7238. } else {
  7239. $parent = 0;
  7240. }
  7241. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7242. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7243. $result = Database::query($sql);
  7244. $arrLP = array();
  7245. while ($row = Database :: fetch_array($result)) {
  7246. $arrLP[] = array(
  7247. 'id' => $row['id'],
  7248. 'item_type' => $row['item_type'],
  7249. 'title' => $row['title'],
  7250. 'path' => $row['path'],
  7251. 'description' => $row['description'],
  7252. 'parent_item_id' => $row['parent_item_id'],
  7253. 'previous_item_id' => $row['previous_item_id'],
  7254. 'next_item_id' => $row['next_item_id'],
  7255. 'display_order' => $row['display_order'],
  7256. 'max_score' => $row['max_score'],
  7257. 'min_score' => $row['min_score'],
  7258. 'mastery_score' => $row['mastery_score'],
  7259. 'prerequisite' => $row['prerequisite']
  7260. );
  7261. }
  7262. $this->tree_array($arrLP);
  7263. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7264. unset ($this->arrMenu);
  7265. if ($action == 'add') {
  7266. $legend = get_lang('CreateTheLink');
  7267. } elseif ($action == 'move') {
  7268. $legend = get_lang('MoveCurrentLink');
  7269. } else {
  7270. $legend = get_lang('EditCurrentLink');
  7271. }
  7272. $form->addHeader($legend);
  7273. if ($action != 'move') {
  7274. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7275. $defaults['title'] = $item_title;
  7276. }
  7277. $selectParent = $form->addSelect(
  7278. 'parent',
  7279. get_lang('Parent'),
  7280. [],
  7281. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7282. );
  7283. $selectParent->addOption($this->name, 0);
  7284. $arrHide = array(
  7285. $id
  7286. );
  7287. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7288. for ($i = 0; $i < count($arrLP); $i++) {
  7289. if ($action != 'add') {
  7290. if (
  7291. (
  7292. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7293. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7294. $arrLP[$i]['item_type'] == 'dir'
  7295. ) &&
  7296. !in_array($arrLP[$i]['id'], $arrHide) &&
  7297. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7298. ) {
  7299. $selectParent->addOption(
  7300. $arrLP[$i]['title'],
  7301. $arrLP[$i]['id'],
  7302. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7303. );
  7304. if ($parent == $arrLP[$i]['id']) {
  7305. $selectParent->setSelected($arrLP[$i]['id']);
  7306. }
  7307. } else {
  7308. $arrHide[] = $arrLP[$i]['id'];
  7309. }
  7310. } else {
  7311. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7312. $selectParent->addOption(
  7313. $arrLP[$i]['title'],
  7314. $arrLP[$i]['id'],
  7315. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7316. );
  7317. if ($parent_item_id == $arrLP[$i]['id']) {
  7318. $selectParent->setSelected($arrLP[$i]['id']);
  7319. }
  7320. }
  7321. }
  7322. }
  7323. if (is_array($arrLP)) {
  7324. reset($arrLP);
  7325. }
  7326. $selectPrevious = $form->addSelect(
  7327. 'previous',
  7328. get_lang('Position'),
  7329. [],
  7330. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7331. );
  7332. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7333. for ($i = 0; $i < count($arrLP); $i++) {
  7334. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7335. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7336. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7337. $selectPrevious->setSelected($arrLP[$i]['id']);
  7338. } elseif ($action == 'add') {
  7339. $selectPrevious->setSelected($arrLP[$i]['id']);
  7340. }
  7341. }
  7342. }
  7343. if ($action != 'move') {
  7344. $urlAttributes = ['class' => 'learnpath_item_form'];
  7345. if (is_numeric($extra_info)) {
  7346. $urlAttributes['disabled'] = 'disabled';
  7347. }
  7348. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7349. $defaults['url'] = $item_url;
  7350. $id_prerequisite = 0;
  7351. if (is_array($arrLP)) {
  7352. foreach ($arrLP as $key => $value) {
  7353. if ($value['id'] == $id) {
  7354. $id_prerequisite = $value['prerequisite'];
  7355. break;
  7356. }
  7357. }
  7358. }
  7359. $arrHide = array();
  7360. for ($i = 0; $i < count($arrLP); $i++) {
  7361. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7362. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7363. $s_selected_position = $arrLP[$i]['id'];
  7364. elseif ($action == 'add') $s_selected_position = 0;
  7365. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7366. }
  7367. }
  7368. }
  7369. if ($action == 'add') {
  7370. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7371. } else {
  7372. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7373. }
  7374. if ($action == 'move') {
  7375. $form->addHidden('title', $item_title);
  7376. $form->addHidden('description', $item_description);
  7377. }
  7378. if (is_numeric($extra_info)) {
  7379. $form->addHidden('path', $extra_info);
  7380. } elseif (is_array($extra_info)) {
  7381. $form->addHidden('path', $extra_info['path']);
  7382. }
  7383. $form->addHidden('type', TOOL_LINK);
  7384. $form->addHidden('post_time', time());
  7385. $form->setDefaults($defaults);
  7386. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7387. }
  7388. /**
  7389. * Return HTML form to add/edit a student publication (work)
  7390. * @param string Action (add/edit)
  7391. * @param integer Item ID if already exists
  7392. * @param mixed Extra info (work ID if integer)
  7393. * @return string HTML form
  7394. */
  7395. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7396. {
  7397. $course_id = api_get_course_int_id();
  7398. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7399. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7400. if ($id != 0 && is_array($extra_info)) {
  7401. $item_title = stripslashes($extra_info['title']);
  7402. $item_description = stripslashes($extra_info['description']);
  7403. } elseif (is_numeric($extra_info)) {
  7404. $extra_info = intval($extra_info);
  7405. $sql = "SELECT title, description
  7406. FROM " . $tbl_publication . "
  7407. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7408. $result = Database::query($sql);
  7409. $row = Database :: fetch_array($result);
  7410. $item_title = $row['title'];
  7411. } else {
  7412. $item_title = get_lang('Student_publication');
  7413. }
  7414. if ($id != 0 && is_array($extra_info)) {
  7415. $parent = $extra_info['parent_item_id'];
  7416. } else {
  7417. $parent = 0;
  7418. }
  7419. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7420. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7421. $result = Database::query($sql);
  7422. $arrLP = array();
  7423. while ($row = Database :: fetch_array($result)) {
  7424. $arrLP[] = array (
  7425. 'id' => $row['id'],
  7426. 'item_type' => $row['item_type'],
  7427. 'title' => $row['title'],
  7428. 'path' => $row['path'],
  7429. 'description' => $row['description'],
  7430. 'parent_item_id' => $row['parent_item_id'],
  7431. 'previous_item_id' => $row['previous_item_id'],
  7432. 'next_item_id' => $row['next_item_id'],
  7433. 'display_order' => $row['display_order'],
  7434. 'max_score' => $row['max_score'],
  7435. 'min_score' => $row['min_score'],
  7436. 'mastery_score' => $row['mastery_score'],
  7437. 'prerequisite' => $row['prerequisite']
  7438. );
  7439. }
  7440. $this->tree_array($arrLP);
  7441. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7442. unset ($this->arrMenu);
  7443. $form = new FormValidator('frm_student_publication', 'post', '#');
  7444. if ($action == 'add') {
  7445. $form->addHeader(get_lang('Student_publication'));
  7446. } elseif ($action == 'move') {
  7447. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7448. } else {
  7449. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7450. }
  7451. if ($action != 'move') {
  7452. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7453. }
  7454. $parentSelect = $form->addSelect(
  7455. 'parent',
  7456. get_lang('Parent'),
  7457. ['0' => $this->name],
  7458. [
  7459. 'onchange' => 'javascript: load_cbo(this.value);',
  7460. 'class' => 'learnpath_item_form',
  7461. 'id' => 'idParent'
  7462. ]
  7463. );
  7464. $arrHide = array (
  7465. $id
  7466. );
  7467. for ($i = 0; $i < count($arrLP); $i++) {
  7468. if ($action != 'add') {
  7469. if (
  7470. (
  7471. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7472. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7473. $arrLP[$i]['item_type'] == 'dir'
  7474. ) &&
  7475. !in_array($arrLP[$i]['id'], $arrHide) &&
  7476. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7477. ) {
  7478. $parentSelect->addOption(
  7479. $arrLP[$i]['title'],
  7480. $arrLP[$i]['id'],
  7481. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7482. );
  7483. if ($parent == $arrLP[$i]['id']) {
  7484. $parentSelect->setSelected($arrLP[$i]['id']);
  7485. }
  7486. } else {
  7487. $arrHide[] = $arrLP[$i]['id'];
  7488. }
  7489. } else {
  7490. if (
  7491. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7492. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7493. ) {
  7494. $parentSelect->addOption(
  7495. $arrLP[$i]['title'],
  7496. $arrLP[$i]['id'],
  7497. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7498. );
  7499. if ($parent == $arrLP[$i]['id']) {
  7500. $parentSelect->setSelected($arrLP[$i]['id']);
  7501. }
  7502. }
  7503. }
  7504. }
  7505. if (is_array($arrLP)) {
  7506. reset($arrLP);
  7507. }
  7508. $previousSelect = $form->addSelect(
  7509. 'previous',
  7510. get_lang('Position'),
  7511. ['0' => get_lang('FirstPosition')],
  7512. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7513. );
  7514. for ($i = 0; $i < count($arrLP); $i++) {
  7515. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7516. $previousSelect->addOption(
  7517. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7518. $arrLP[$i]['id']
  7519. );
  7520. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7521. $previousSelect->setSelected($arrLP[$i]['id']);
  7522. } elseif ($action == 'add') {
  7523. $previousSelect->setSelected($arrLP[$i]['id']);
  7524. }
  7525. }
  7526. }
  7527. if ($action != 'move') {
  7528. $id_prerequisite = 0;
  7529. if (is_array($arrLP)) {
  7530. foreach ($arrLP as $key => $value) {
  7531. if ($value['id'] == $id) {
  7532. $id_prerequisite = $value['prerequisite'];
  7533. break;
  7534. }
  7535. }
  7536. }
  7537. $arrHide = array ();
  7538. for ($i = 0; $i < count($arrLP); $i++) {
  7539. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7540. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7541. $s_selected_position = $arrLP[$i]['id'];
  7542. elseif ($action == 'add') $s_selected_position = 0;
  7543. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7544. }
  7545. }
  7546. }
  7547. if ($action == 'add') {
  7548. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7549. } else {
  7550. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7551. }
  7552. if ($action == 'move') {
  7553. $form->addHidden('title', $item_title);
  7554. $form->addHidden('description', $item_description);
  7555. }
  7556. if (is_numeric($extra_info)) {
  7557. $form->addHidden('path', $extra_info);
  7558. } elseif (is_array($extra_info)) {
  7559. $form->addHidden('path', $extra_info['path']);
  7560. }
  7561. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7562. $form->addHidden('post_time', time());
  7563. $form->setDefaults(['title' => $item_title]);
  7564. $return = '<div class="sectioncomment">';
  7565. $return .= $form->returnForm();
  7566. $return .= '</div>';
  7567. return $return;
  7568. }
  7569. /**
  7570. * Displays the menu for manipulating a step
  7571. *
  7572. * @param $item_id
  7573. * @param string $item_type
  7574. * @return string
  7575. */
  7576. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7577. {
  7578. $_course = api_get_course_info();
  7579. $course_id = api_get_course_int_id();
  7580. $course_code = api_get_course_id();
  7581. $return = '<div class="actions">';
  7582. switch ($item_type) {
  7583. case 'dokeos_chapter' :
  7584. case 'chapter' :
  7585. // Commented the message cause should not show it.
  7586. //$lang = get_lang('TitleManipulateChapter');
  7587. break;
  7588. case 'dokeos_module' :
  7589. case 'module' :
  7590. // Commented the message cause should not show it.
  7591. //$lang = get_lang('TitleManipulateModule');
  7592. break;
  7593. case TOOL_LP_FINAL_ITEM :
  7594. case TOOL_DOCUMENT :
  7595. // Commented the message cause should not show it.
  7596. //$lang = get_lang('TitleManipulateDocument');
  7597. break;
  7598. case TOOL_LINK :
  7599. case 'link' :
  7600. // Commented the message cause should not show it.
  7601. //$lang = get_lang('TitleManipulateLink');
  7602. break;
  7603. case TOOL_QUIZ :
  7604. // Commented the message cause should not show it.
  7605. //$lang = get_lang('TitleManipulateQuiz');
  7606. break;
  7607. case TOOL_STUDENTPUBLICATION :
  7608. // Commented the message cause should not show it.
  7609. //$lang = get_lang('TitleManipulateStudentPublication');
  7610. break;
  7611. }
  7612. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7613. $item_id = intval($item_id);
  7614. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7615. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7616. $result = Database::query($sql);
  7617. $row = Database::fetch_assoc($result);
  7618. $audio_player = null;
  7619. // We display an audio player if needed.
  7620. if (!empty($row['audio'])) {
  7621. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7622. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7623. </div>';
  7624. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7625. $audio_player .= '<script>
  7626. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7627. s1.addParam("allowscriptaccess","always");
  7628. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7629. s1.write("container");
  7630. </script>';
  7631. }
  7632. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7633. $return .= Display::url(
  7634. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7635. $url.'&action=edit_item&path_item=' . $row['path']
  7636. );
  7637. $return .= Display::url(
  7638. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7639. $url.'&action=move_item'
  7640. );
  7641. // Commented for now as prerequisites cannot be added to chapters.
  7642. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7643. $return .= Display::url(
  7644. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7645. $url.'&action=edit_item_prereq'
  7646. );
  7647. }
  7648. $return .= Display::url(
  7649. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7650. $url.'&action=delete_item'
  7651. );
  7652. if ($item_type == TOOL_HOTPOTATOES ) {
  7653. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7654. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7655. }
  7656. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7657. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7658. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7659. }
  7660. $return .= '</div>';
  7661. if (!empty($audio_player)) {
  7662. $return .= '<br />'.$audio_player;
  7663. }
  7664. return $return;
  7665. }
  7666. /**
  7667. * Creates the javascript needed for filling up the checkboxes without page reload
  7668. * @return string
  7669. */
  7670. public function get_js_dropdown_array()
  7671. {
  7672. $course_id = api_get_course_int_id();
  7673. $return = 'var child_name = new Array();' . "\n";
  7674. $return .= 'var child_value = new Array();' . "\n\n";
  7675. $return .= 'child_name[0] = new Array();' . "\n";
  7676. $return .= 'child_value[0] = new Array();' . "\n\n";
  7677. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7678. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7679. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7680. ORDER BY display_order ASC";
  7681. $res_zero = Database::query($sql_zero);
  7682. $i = 0;
  7683. while ($row_zero = Database :: fetch_array($res_zero)) {
  7684. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7685. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7686. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7687. }
  7688. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7689. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7690. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7691. }
  7692. }
  7693. $return .= "\n";
  7694. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7695. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7696. $res = Database::query($sql);
  7697. while ($row = Database :: fetch_array($res)) {
  7698. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7699. WHERE
  7700. c_id = ".$course_id." AND
  7701. parent_item_id = " . $row['id'] . "
  7702. ORDER BY display_order ASC";
  7703. $res_parent = Database::query($sql_parent);
  7704. $i = 0;
  7705. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7706. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7707. while ($row_parent = Database :: fetch_array($res_parent)) {
  7708. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7709. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7710. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7711. }
  7712. $return .= "\n";
  7713. }
  7714. return $return;
  7715. }
  7716. /**
  7717. * Display the form to allow moving an item
  7718. * @param integer Item ID
  7719. * @return string HTML form
  7720. */
  7721. public function display_move_item($item_id)
  7722. {
  7723. $course_id = api_get_course_int_id();
  7724. $return = '';
  7725. if (is_numeric($item_id)) {
  7726. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7727. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7728. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7729. $res = Database::query($sql);
  7730. $row = Database :: fetch_array($res);
  7731. switch ($row['item_type']) {
  7732. case 'dokeos_chapter' :
  7733. case 'dir' :
  7734. case 'asset' :
  7735. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7736. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7737. break;
  7738. case 'dokeos_module' :
  7739. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7740. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7741. break;
  7742. case TOOL_DOCUMENT :
  7743. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7744. $return .= $this->display_document_form('move', $item_id, $row);
  7745. break;
  7746. case TOOL_LINK :
  7747. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7748. $return .= $this->display_link_form('move', $item_id, $row);
  7749. break;
  7750. case TOOL_HOTPOTATOES :
  7751. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7752. $return .= $this->display_link_form('move', $item_id, $row);
  7753. break;
  7754. case TOOL_QUIZ :
  7755. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7756. $return .= $this->display_quiz_form('move', $item_id, $row);
  7757. break;
  7758. case TOOL_STUDENTPUBLICATION :
  7759. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7760. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7761. break;
  7762. case TOOL_FORUM :
  7763. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7764. $return .= $this->display_forum_form('move', $item_id, $row);
  7765. break;
  7766. case TOOL_THREAD :
  7767. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7768. $return .= $this->display_forum_form('move', $item_id, $row);
  7769. break;
  7770. }
  7771. }
  7772. return $return;
  7773. }
  7774. /**
  7775. * Displays a basic form on the overview page for changing the item title and the item description.
  7776. * @param string $item_type
  7777. * @param string $title
  7778. * @param array $data
  7779. * @return string
  7780. */
  7781. public function display_item_small_form($item_type, $title = '', $data = array())
  7782. {
  7783. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7784. $form = new FormValidator('small_form', 'post', $url);
  7785. $form->addElement('header', $title);
  7786. $form->addElement('text', 'title', get_lang('Title'));
  7787. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7788. $form->addElement('hidden', 'id', $data['id']);
  7789. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7790. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7791. $form->setDefaults(array('title' => $data['title']));
  7792. return $form->toHtml();
  7793. }
  7794. /**
  7795. * Return HTML form to allow prerequisites selection
  7796. * @todo use FormValidator
  7797. * @param integer Item ID
  7798. * @return string HTML form
  7799. */
  7800. public function display_item_prerequisites_form($item_id)
  7801. {
  7802. $course_id = api_get_course_int_id();
  7803. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7804. $item_id = intval($item_id);
  7805. /* Current prerequisite */
  7806. $sql = "SELECT * FROM $tbl_lp_item
  7807. WHERE c_id = $course_id AND id = " . $item_id;
  7808. $result = Database::query($sql);
  7809. $row = Database::fetch_array($result);
  7810. $prerequisiteId = $row['prerequisite'];
  7811. $return = '<legend>';
  7812. $return .= get_lang('AddEditPrerequisites');
  7813. $return .= '</legend>';
  7814. $return .= '<form method="POST">';
  7815. $return .= '<table class="data_table">';
  7816. $return .= '<tr>';
  7817. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7818. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7819. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7820. $return .= '</tr>';
  7821. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7822. $return .= '<tr >';
  7823. $return .= '<td colspan="3" class="radio">';
  7824. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7825. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7826. $return .= '</tr>';
  7827. $sql = "SELECT * FROM $tbl_lp_item
  7828. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7829. $result = Database::query($sql);
  7830. $arrLP = array();
  7831. $selectedMinScore = array();
  7832. $selectedMaxScore = array();
  7833. while ($row = Database :: fetch_array($result)) {
  7834. if ($row['id'] == $item_id) {
  7835. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7836. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7837. }
  7838. $arrLP[] = array(
  7839. 'id' => $row['id'],
  7840. 'item_type' => $row['item_type'],
  7841. 'title' => $row['title'],
  7842. 'ref' => $row['ref'],
  7843. 'description' => $row['description'],
  7844. 'parent_item_id' => $row['parent_item_id'],
  7845. 'previous_item_id' => $row['previous_item_id'],
  7846. 'next_item_id' => $row['next_item_id'],
  7847. 'max_score' => $row['max_score'],
  7848. 'min_score' => $row['min_score'],
  7849. 'mastery_score' => $row['mastery_score'],
  7850. 'prerequisite' => $row['prerequisite'],
  7851. 'next_item_id' => $row['next_item_id'],
  7852. 'display_order' => $row['display_order'],
  7853. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7854. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7855. );
  7856. }
  7857. $this->tree_array($arrLP);
  7858. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7859. unset($this->arrMenu);
  7860. for ($i = 0; $i < count($arrLP); $i++) {
  7861. $item = $arrLP[$i];
  7862. if ($item['id'] == $item_id) {
  7863. break;
  7864. }
  7865. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7866. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7867. $return .= '<tr>';
  7868. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7869. $return .= '<label for="id' . $item['id'] . '">';
  7870. $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'] . '" />';
  7871. $icon_name = str_replace(' ', '', $item['item_type']);
  7872. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7873. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  7874. } else {
  7875. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7876. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  7877. } else {
  7878. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7879. }
  7880. }
  7881. $return .= $item['title'] . '</label>';
  7882. $return .= '</td>';
  7883. if ($item['item_type'] == TOOL_QUIZ) {
  7884. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7885. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7886. $tmp_obj_exercice = new Exercise();
  7887. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7888. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7889. $tmp_obj_lp_item->update_in_bdd();
  7890. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7891. $return .= '<td class="exercise">';
  7892. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7893. $return .= '</td>';
  7894. $return .= '<td class="exercise">';
  7895. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7896. $return .= '</td>';
  7897. }
  7898. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7899. $return .= '<td class="exercise">';
  7900. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7901. $return .= '</td>';
  7902. $return .= '<td class="exercise"">';
  7903. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7904. $return .= '</td>';
  7905. }
  7906. $return .= '</tr>';
  7907. }
  7908. $return .= '<tr>';
  7909. $return .= '</tr>';
  7910. $return .= '</table>';
  7911. $return .= '<div style="padding-top:3px;">';
  7912. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7913. $return .= '</form>';
  7914. return $return;
  7915. }
  7916. /**
  7917. * Return HTML list to allow prerequisites selection for lp
  7918. * @param integer Item ID
  7919. * @return string HTML form
  7920. */
  7921. public function display_lp_prerequisites_list()
  7922. {
  7923. $course_id = api_get_course_int_id();
  7924. $lp_id = $this->lp_id;
  7925. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7926. // get current prerequisite
  7927. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7928. $result = Database::query($sql);
  7929. $row = Database :: fetch_array($result);
  7930. $prerequisiteId = $row['prerequisite'];
  7931. $session_id = api_get_session_id();
  7932. $session_condition = api_get_session_condition($session_id);
  7933. $sql = "SELECT * FROM $tbl_lp
  7934. WHERE c_id = $course_id $session_condition
  7935. ORDER BY display_order ";
  7936. $rs = Database::query($sql);
  7937. $return = '';
  7938. $return .= '<select name="prerequisites" class="form-control">';
  7939. $return .= '<option value="0">'.get_lang('None').'</option>';
  7940. if (Database::num_rows($rs) > 0) {
  7941. while ($row = Database::fetch_array($rs)) {
  7942. if ($row['id'] == $lp_id) {
  7943. continue;
  7944. }
  7945. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7946. }
  7947. }
  7948. $return .= '</select>';
  7949. return $return;
  7950. }
  7951. /**
  7952. * Creates a list with all the documents in it
  7953. * @param bool $showInvisibleFiles
  7954. * @return string
  7955. */
  7956. public function get_documents($showInvisibleFiles = false)
  7957. {
  7958. $course_info = api_get_course_info();
  7959. $sessionId = api_get_session_id();
  7960. $documentTree = DocumentManager::get_document_preview(
  7961. $course_info,
  7962. $this->lp_id,
  7963. null,
  7964. $sessionId,
  7965. true,
  7966. null,
  7967. null,
  7968. $showInvisibleFiles,
  7969. true
  7970. );
  7971. $headers = array(
  7972. get_lang('Files'),
  7973. get_lang('NewDocument'),
  7974. get_lang('Upload'),
  7975. );
  7976. $form = new FormValidator(
  7977. 'form_upload',
  7978. 'POST',
  7979. api_get_self().'?'.$_SERVER['QUERY_STRING'],
  7980. '',
  7981. array('enctype' => "multipart/form-data")
  7982. );
  7983. $folders = DocumentManager::get_all_document_folders(
  7984. api_get_course_info(),
  7985. 0,
  7986. true
  7987. );
  7988. DocumentManager::build_directory_selector(
  7989. $folders,
  7990. '',
  7991. array(),
  7992. true,
  7993. $form,
  7994. 'directory_parent_id'
  7995. );
  7996. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  7997. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  7998. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  7999. $form->setDefaults(['if_exists' => 'rename']);
  8000. // Check box options
  8001. $form->addElement(
  8002. 'checkbox',
  8003. 'unzip',
  8004. get_lang('Options'),
  8005. get_lang('Uncompress')
  8006. );
  8007. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8008. $form->addMultipleUpload($url);
  8009. $new = $this->display_document_form('add', 0);
  8010. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8011. return $tabs;
  8012. }
  8013. /**
  8014. * Creates a list with all the exercises (quiz) in it
  8015. * @return string
  8016. */
  8017. public function get_exercises()
  8018. {
  8019. $course_id = api_get_course_int_id();
  8020. // New for hotpotatoes.
  8021. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8022. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8023. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8024. $session_id = api_get_session_id();
  8025. $condition_session = api_get_session_condition($session_id);
  8026. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8027. $activeCondition = " active <> -1 ";
  8028. if ($setting) {
  8029. $activeCondition = " active = 1 ";
  8030. }
  8031. $sql_quiz = "SELECT * FROM $tbl_quiz
  8032. WHERE c_id = $course_id AND $activeCondition $condition_session
  8033. ORDER BY title ASC";
  8034. $sql_hot = "SELECT * FROM $tbl_doc
  8035. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8036. ORDER BY id ASC";
  8037. $res_quiz = Database::query($sql_quiz);
  8038. $res_hot = Database::query($sql_hot);
  8039. $return = '<ul class="lp_resource">';
  8040. $return .= '<li class="lp_resource_element">';
  8041. $return .= Display::return_icon('new_test_small.gif');
  8042. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8043. get_lang('NewExercise') . '</a>';
  8044. $return .= '</li>';
  8045. // Display hotpotatoes
  8046. while ($row_hot = Database :: fetch_array($res_hot)) {
  8047. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8048. $return .= '<a class="moved" href="#">';
  8049. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8050. $return .= '</a> ';
  8051. $return .= Display::return_icon('hotpotatoes_s.png');
  8052. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8053. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  8054. $return .= '</li>';
  8055. }
  8056. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8057. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8058. $return .= '<a class="moved" href="#">';
  8059. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8060. $return .= '</a> ';
  8061. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8062. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8063. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  8064. '</a>';
  8065. $return .= '</li>';
  8066. }
  8067. $return .= '</ul>';
  8068. return $return;
  8069. }
  8070. /**
  8071. * Creates a list with all the links in it
  8072. * @return string
  8073. */
  8074. public function get_links()
  8075. {
  8076. $selfUrl = api_get_self();
  8077. $courseIdReq = api_get_cidreq();
  8078. $course = api_get_course_info();
  8079. $course_id = $course['real_id'];
  8080. $tbl_link = Database::get_course_table(TABLE_LINK);
  8081. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8082. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8083. $session_id = api_get_session_id();
  8084. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  8085. $sql = "SELECT link.id as link_id,
  8086. link.title as link_title,
  8087. link.category_id as category_id,
  8088. link_category.category_title as category_title
  8089. FROM $tbl_link as link
  8090. LEFT JOIN $linkCategoryTable as link_category
  8091. ON link.category_id = link_category.id
  8092. WHERE link.c_id = ".$course_id." $condition_session
  8093. ORDER BY link_category.category_title ASC, link.title ASC";
  8094. $links = Database::query($sql);
  8095. $categorizedLinks = array();
  8096. $categories = array();
  8097. while ($link = Database :: fetch_array($links)) {
  8098. if (!$link['category_id']) {
  8099. $link['category_title'] = get_lang('Uncategorized');
  8100. }
  8101. $categories[$link['category_id']] = $link['category_title'];
  8102. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  8103. }
  8104. $linksHtmlCode =
  8105. '<script>
  8106. function toggle_tool(tool, id){
  8107. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8108. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8109. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8110. } else {
  8111. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8112. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8113. }
  8114. }
  8115. </script>
  8116. <ul class="lp_resource">
  8117. <li class="lp_resource_element">
  8118. '.Display::return_icon('linksnew.gif').'
  8119. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8120. get_lang('LinkAdd').'
  8121. </a>
  8122. </li>';
  8123. foreach ($categorizedLinks as $categoryId => $links) {
  8124. $linkNodes = null;
  8125. foreach ($links as $key => $title) {
  8126. $link = Display::url(
  8127. Display::return_icon('preview_view.png', get_lang('Preview')),
  8128. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8129. ['target' => '_blank']
  8130. );
  8131. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8132. $linkNodes .=
  8133. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8134. <a class="moved" href="#">'.
  8135. $moveEverywhereIcon.
  8136. '</a>
  8137. '.Display::return_icon('lp_link.png').'
  8138. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8139. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8140. Security::remove_XSS($title).
  8141. '</a>
  8142. </li>';
  8143. }
  8144. }
  8145. $linksHtmlCode .=
  8146. '<li>
  8147. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8148. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8149. align="absbottom" />
  8150. </a>
  8151. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8152. </li>
  8153. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8154. }
  8155. $linksHtmlCode .= '</ul>';
  8156. return $linksHtmlCode;
  8157. }
  8158. /**
  8159. * Creates a list with all the student publications in it
  8160. * @return string
  8161. */
  8162. public function get_student_publications()
  8163. {
  8164. $return = '<ul class="lp_resource" >';
  8165. $return .= '<li class="lp_resource_element">';
  8166. $return .= Display::return_icon('works_new.gif');
  8167. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8168. get_lang('AddAssignmentPage') . '</a>';
  8169. $return .= '</li>';
  8170. $sessionId = api_get_session_id();
  8171. if (empty($sessionId)) {
  8172. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8173. $works = getWorkListTeacher(0, 100, null, null, null);
  8174. if (!empty($works)) {
  8175. foreach ($works as $work) {
  8176. $link = Display::url(
  8177. Display::return_icon('preview_view.png', get_lang('Preview')),
  8178. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8179. ['target' => '_blank']
  8180. );
  8181. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security :: remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8182. $return .= '<a class="moved" href="#">';
  8183. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8184. $return .= '</a> ';
  8185. $return .= Display::return_icon('works.gif');
  8186. $return .= ' <a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&file=' . $work['iid'] . '&lp_id=' . $this->lp_id . '">' .
  8187. Security :: remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8188. </a>';
  8189. $return .= '</li>';
  8190. }
  8191. }
  8192. }
  8193. $return .= '</ul>';
  8194. return $return;
  8195. }
  8196. /**
  8197. * Creates a list with all the forums in it
  8198. * @return string
  8199. */
  8200. public function get_forums()
  8201. {
  8202. require_once '../forum/forumfunction.inc.php';
  8203. require_once '../forum/forumconfig.inc.php';
  8204. $a_forums = get_forums();
  8205. $return = '<ul class="lp_resource">';
  8206. //First add link
  8207. $return .= '<li class="lp_resource_element">';
  8208. $return .= Display::return_icon('forum_new_small.gif');
  8209. $return .= Display::url(
  8210. get_lang('CreateANewForum'),
  8211. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8212. 'action' => 'add',
  8213. 'content' => 'forum',
  8214. 'lp_id' => $this->lp_id
  8215. ]),
  8216. ['title' => get_lang('CreateANewForum')]
  8217. );
  8218. $return .= '</li>';
  8219. $return .= '<script>
  8220. function toggle_forum(forum_id){
  8221. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8222. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8223. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8224. } else {
  8225. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8226. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8227. }
  8228. }
  8229. </script>';
  8230. foreach ($a_forums as $forum) {
  8231. if (!empty($forum['forum_id'])) {
  8232. $link = Display::url(
  8233. Display::return_icon('preview_view.png', get_lang('Preview')),
  8234. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8235. ['target' => '_blank']
  8236. );
  8237. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8238. $return .= '<a class="moved" href="#">';
  8239. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8240. $return .= ' </a>';
  8241. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8242. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  8243. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8244. </a>
  8245. <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">' .
  8246. Security :: remove_XSS($forum['forum_title']) . '</a>';
  8247. $return .= '</li>';
  8248. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8249. $a_threads = get_threads($forum['forum_id']);
  8250. if (is_array($a_threads)) {
  8251. foreach ($a_threads as $thread) {
  8252. $link = Display::url(
  8253. Display::return_icon('preview_view.png', get_lang('Preview')),
  8254. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8255. ['target' => '_blank']
  8256. );
  8257. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8258. $return .= '&nbsp;<a class="moved" href="#">';
  8259. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8260. $return .= ' </a>';
  8261. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8262. $return .= '<a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  8263. Security :: remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8264. $return .= '</li>';
  8265. }
  8266. }
  8267. $return .= '</div>';
  8268. }
  8269. }
  8270. $return .= '</ul>';
  8271. return $return;
  8272. }
  8273. /**
  8274. * // TODO: The output encoding should be equal to the system encoding.
  8275. *
  8276. * Exports the learning path as a SCORM package. This is the main function that
  8277. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8278. * whole thing and returns the zip.
  8279. *
  8280. * This method needs to be called in PHP5, as it will fail with non-adequate
  8281. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8282. * you need to call it on a learnpath object.
  8283. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8284. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8285. * path has been modified, it should use the generic method here below.
  8286. * @TODO link this function with the export_lp() function in the same class
  8287. * @param string Optional name of zip file. If none, title of learnpath is
  8288. * domesticated and trailed with ".zip"
  8289. * @return string Returns the zip package string, or null if error
  8290. */
  8291. public function scorm_export()
  8292. {
  8293. $_course = api_get_course_info();
  8294. $course_id = $_course['real_id'];
  8295. // Remove memory and time limits as much as possible as this might be a long process...
  8296. if (function_exists('ini_set')) {
  8297. api_set_memory_limit('128M');
  8298. ini_set('max_execution_time', 600);
  8299. }
  8300. // Create the zip handler (this will remain available throughout the method).
  8301. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8302. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8303. $temp_dir_short = uniqid();
  8304. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8305. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8306. $zip_folder = new PclZip($temp_zip_file);
  8307. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8308. $root_path = $main_path = api_get_path(SYS_PATH);
  8309. $files_cleanup = array();
  8310. // Place to temporarily stash the zip file.
  8311. // create the temp dir if it doesn't exist
  8312. // or do a cleanup before creating the zip file.
  8313. if (!is_dir($temp_zip_dir)) {
  8314. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8315. } else {
  8316. // Cleanup: Check the temp dir for old files and delete them.
  8317. $handle = opendir($temp_zip_dir);
  8318. while (false !== ($file = readdir($handle))) {
  8319. if ($file != '.' && $file != '..') {
  8320. unlink("$temp_zip_dir/$file");
  8321. }
  8322. }
  8323. closedir($handle);
  8324. }
  8325. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8326. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8327. // Remove the possible . at the end of the path.
  8328. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8329. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8330. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8331. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  8332. }
  8333. // Build a dummy imsmanifest structure.
  8334. // Do not add to the zip yet (we still need it).
  8335. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8336. // Aggregation Model official document, section "2.3 Content Packaging".
  8337. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8338. $xmldoc = new DOMDocument('1.0');
  8339. $root = $xmldoc->createElement('manifest');
  8340. $root->setAttribute('identifier', 'SingleCourseManifest');
  8341. $root->setAttribute('version', '1.1');
  8342. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8343. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8344. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8345. $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');
  8346. // Build mandatory sub-root container elements.
  8347. $metadata = $xmldoc->createElement('metadata');
  8348. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8349. $metadata->appendChild($md_schema);
  8350. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8351. $metadata->appendChild($md_schemaversion);
  8352. $root->appendChild($metadata);
  8353. $organizations = $xmldoc->createElement('organizations');
  8354. $resources = $xmldoc->createElement('resources');
  8355. // Build the only organization we will use in building our learnpaths.
  8356. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8357. $organization = $xmldoc->createElement('organization');
  8358. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8359. // To set the title of the SCORM entity (=organization), we take the name given
  8360. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8361. // learning path charset) as it is the encoding that defines how it is stored
  8362. // in the database. Then we convert it to HTML entities again as the "&" character
  8363. // alone is not authorized in XML (must be &amp;).
  8364. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8365. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8366. $organization->appendChild($org_title);
  8367. $folder_name = 'document';
  8368. // Removes the learning_path/scorm_folder path when exporting see #4841
  8369. $path_to_remove = null;
  8370. $result = $this->generate_lp_folder($_course);
  8371. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8372. $path_to_remove = 'document'.$result['dir'];
  8373. $path_to_replace = $folder_name.'/';
  8374. }
  8375. //Fixes chamilo scorm exports
  8376. if ($this->ref == 'chamilo_scorm_export') {
  8377. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8378. }
  8379. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8380. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8381. $link_updates = array();
  8382. $links_to_create = array();
  8383. foreach ($this->items as $index => $item) {
  8384. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8385. // Get included documents from this item.
  8386. if ($item->type == 'sco') {
  8387. $inc_docs = $item->get_resources_from_source(
  8388. null,
  8389. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8390. );
  8391. } else {
  8392. $inc_docs = $item->get_resources_from_source();
  8393. }
  8394. // Give a child element <item> to the <organization> element.
  8395. $my_item_id = $item->get_id();
  8396. $my_item = $xmldoc->createElement('item');
  8397. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8398. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8399. $my_item->setAttribute('isvisible', 'true');
  8400. // Give a child element <title> to the <item> element.
  8401. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8402. $my_item->appendChild($my_title);
  8403. // Give a child element <adlcp:prerequisites> to the <item> element.
  8404. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8405. $my_prereqs->setAttribute('type', 'aicc_script');
  8406. $my_item->appendChild($my_prereqs);
  8407. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8408. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8409. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8410. //$xmldoc->createElement('adlcp:timelimitaction','');
  8411. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8412. //$xmldoc->createElement('adlcp:datafromlms','');
  8413. // Give a child element <adlcp:masteryscore> to the <item> element.
  8414. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8415. $my_item->appendChild($my_masteryscore);
  8416. // Attach this item to the organization element or hits parent if there is one.
  8417. if (!empty($item->parent) && $item->parent != 0) {
  8418. $children = $organization->childNodes;
  8419. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8420. if (is_object($possible_parent)) {
  8421. $possible_parent->appendChild($my_item);
  8422. } else {
  8423. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8424. }
  8425. } else {
  8426. if ($this->debug > 0) { error_log('No parent'); }
  8427. $organization->appendChild($my_item);
  8428. }
  8429. // Get the path of the file(s) from the course directory root.
  8430. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8431. if (!empty($path_to_remove)) {
  8432. //From docs
  8433. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8434. //From quiz
  8435. if ($this->ref == 'chamilo_scorm_export') {
  8436. $path_to_remove = 'scorm/'.$this->path.'/';
  8437. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8438. }
  8439. } else {
  8440. $my_xml_file_path = $my_file_path;
  8441. }
  8442. $my_sub_dir = dirname($my_file_path);
  8443. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8444. $my_xml_sub_dir = $my_sub_dir;
  8445. // Give a <resource> child to the <resources> element
  8446. $my_resource = $xmldoc->createElement('resource');
  8447. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8448. $my_resource->setAttribute('type', 'webcontent');
  8449. $my_resource->setAttribute('href', $my_xml_file_path);
  8450. // adlcp:scormtype can be either 'sco' or 'asset'.
  8451. if ($item->type == 'sco') {
  8452. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8453. } else {
  8454. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8455. }
  8456. // xml:base is the base directory to find the files declared in this resource.
  8457. $my_resource->setAttribute('xml:base', '');
  8458. // Give a <file> child to the <resource> element.
  8459. $my_file = $xmldoc->createElement('file');
  8460. $my_file->setAttribute('href', $my_xml_file_path);
  8461. $my_resource->appendChild($my_file);
  8462. // Dependency to other files - not yet supported.
  8463. $i = 1;
  8464. foreach ($inc_docs as $doc_info) {
  8465. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8466. $my_dep = $xmldoc->createElement('resource');
  8467. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8468. $my_dep->setAttribute('identifier', $res_id);
  8469. $my_dep->setAttribute('type', 'webcontent');
  8470. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8471. $my_dep_file = $xmldoc->createElement('file');
  8472. // Check type of URL.
  8473. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8474. if ($doc_info[1] == 'remote') {
  8475. // Remote file. Save url as is.
  8476. $my_dep_file->setAttribute('href', $doc_info[0]);
  8477. $my_dep->setAttribute('xml:base', '');
  8478. } elseif ($doc_info[1] == 'local') {
  8479. switch ($doc_info[2]) {
  8480. case 'url': // Local URL - save path as url for now, don't zip file.
  8481. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8482. $current_dir = dirname($abs_path);
  8483. $current_dir = str_replace('\\', '/', $current_dir);
  8484. $file_path = realpath($abs_path);
  8485. $file_path = str_replace('\\', '/', $file_path);
  8486. $my_dep_file->setAttribute('href', $file_path);
  8487. $my_dep->setAttribute('xml:base', '');
  8488. if (strstr($file_path, $main_path) !== false) {
  8489. // The calculated real path is really inside Chamilo's root path.
  8490. // Reduce file path to what's under the DocumentRoot.
  8491. $file_path = substr($file_path, strlen($root_path) - 1);
  8492. //echo $file_path;echo '<br /><br />';
  8493. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8494. $zip_files_abs[] = $file_path;
  8495. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8496. $my_dep_file->setAttribute('href', $file_path);
  8497. $my_dep->setAttribute('xml:base', '');
  8498. } elseif (empty($file_path)) {
  8499. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8500. if (strpos($document_root, -1) == '/') {
  8501. $document_root = substr(0, -1, $document_root);
  8502. }*/
  8503. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8504. $file_path = str_replace('//', '/', $file_path);
  8505. if (file_exists($file_path)) {
  8506. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8507. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8508. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8509. $my_dep_file->setAttribute('href', $file_path);
  8510. $my_dep->setAttribute('xml:base', '');
  8511. }
  8512. }
  8513. break;
  8514. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8515. $my_dep_file->setAttribute('href', $doc_info[0]);
  8516. $my_dep->setAttribute('xml:base', '');
  8517. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8518. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8519. $abs_img_path_without_subdir = $doc_info[0];
  8520. $relp = api_get_path(REL_PATH); // The url-append config param.
  8521. $pos = strpos($abs_img_path_without_subdir, $relp);
  8522. if ($pos === 0) {
  8523. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8524. }
  8525. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8526. $file_path = str_replace('\\', '/', $file_path);
  8527. $file_path = str_replace('//', '/', $file_path);
  8528. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8529. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8530. // Check if the current document is in that path.
  8531. if (strstr($file_path, $cur_path) !== false) {
  8532. // The document is in that path, now get the relative path
  8533. // to the containing document.
  8534. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8535. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8536. $relative_path = '';
  8537. if (strstr($file_path, $cur_path) !== false) {
  8538. //$relative_path = substr($file_path, strlen($orig_file_path));
  8539. $relative_path = str_replace($cur_path, '', $file_path);
  8540. $file_path = substr($file_path, strlen($cur_path));
  8541. } else {
  8542. // This case is still a problem as it's difficult to calculate a relative path easily
  8543. // might still generate wrong links.
  8544. //$file_path = substr($file_path,strlen($cur_path));
  8545. // Calculate the directory path to the current file (without trailing slash).
  8546. $my_relative_path = dirname($file_path);
  8547. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8548. $my_relative_file = basename($file_path);
  8549. // Calculate the directory path to the containing file (without trailing slash).
  8550. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8551. $dotdot = '';
  8552. $subdir = '';
  8553. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8554. $my_relative_path2 = dirname($my_relative_path);
  8555. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8556. $my_orig_file_path = dirname($my_orig_file_path);
  8557. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8558. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8559. $dotdot += '../';
  8560. $my_relative_path = $my_relative_path2;
  8561. }
  8562. $relative_path = $dotdot.$subdir.$my_relative_file;
  8563. }
  8564. // Put the current document in the zip (this array is the array
  8565. // that will manage documents already in the course folder - relative).
  8566. $zip_files[] = $file_path;
  8567. // Update the links to the current document in the containing document (make them relative).
  8568. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8569. $my_dep_file->setAttribute('href', $file_path);
  8570. $my_dep->setAttribute('xml:base', '');
  8571. } elseif (strstr($file_path, $main_path) !== false) {
  8572. // The calculated real path is really inside Chamilo's root path.
  8573. // Reduce file path to what's under the DocumentRoot.
  8574. $file_path = substr($file_path, strlen($root_path));
  8575. //echo $file_path;echo '<br /><br />';
  8576. //error_log('Reduced path: '.$file_path, 0);
  8577. $zip_files_abs[] = $file_path;
  8578. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8579. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8580. $my_dep->setAttribute('xml:base', '');
  8581. } elseif (empty($file_path)) {
  8582. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8583. if(strpos($document_root,-1) == '/') {
  8584. $document_root = substr(0, -1, $document_root);
  8585. }*/
  8586. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8587. $file_path = str_replace('//', '/', $file_path);
  8588. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8589. $current_dir = dirname($abs_path);
  8590. $current_dir = str_replace('\\', '/', $current_dir);
  8591. if (file_exists($file_path)) {
  8592. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8593. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8594. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8595. $my_dep_file->setAttribute('href','document/'.$file_path);
  8596. $my_dep->setAttribute('xml:base', '');
  8597. }
  8598. }
  8599. break;
  8600. case 'rel':
  8601. // Path relative to the current document.
  8602. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8603. if (substr($doc_info[0], 0, 2) == '..') {
  8604. // Relative path going up.
  8605. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8606. $current_dir = str_replace('\\', '/', $current_dir);
  8607. $file_path = realpath($current_dir.$doc_info[0]);
  8608. $file_path = str_replace('\\', '/', $file_path);
  8609. //error_log($file_path.' <-> '.$main_path,0);
  8610. if (strstr($file_path, $main_path) !== false) {
  8611. // The calculated real path is really inside Chamilo's root path.
  8612. // Reduce file path to what's under the DocumentRoot.
  8613. $file_path = substr($file_path, strlen($root_path));
  8614. //error_log('Reduced path: '.$file_path, 0);
  8615. $zip_files_abs[] = $file_path;
  8616. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8617. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8618. $my_dep->setAttribute('xml:base', '');
  8619. }
  8620. } else {
  8621. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8622. $my_dep_file->setAttribute('href', $doc_info[0]);
  8623. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8624. }
  8625. break;
  8626. default:
  8627. $my_dep_file->setAttribute('href', $doc_info[0]);
  8628. $my_dep->setAttribute('xml:base', '');
  8629. break;
  8630. }
  8631. }
  8632. $my_dep->appendChild($my_dep_file);
  8633. $resources->appendChild($my_dep);
  8634. $dependency = $xmldoc->createElement('dependency');
  8635. $dependency->setAttribute('identifierref', $res_id);
  8636. $my_resource->appendChild($dependency);
  8637. $i++;
  8638. }
  8639. $resources->appendChild($my_resource);
  8640. $zip_files[] = $my_file_path;
  8641. } else {
  8642. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8643. switch ($item->type) {
  8644. case TOOL_LINK:
  8645. $my_item = $xmldoc->createElement('item');
  8646. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8647. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8648. $my_item->setAttribute('isvisible', 'true');
  8649. // Give a child element <title> to the <item> element.
  8650. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8651. $my_item->appendChild($my_title);
  8652. // Give a child element <adlcp:prerequisites> to the <item> element.
  8653. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8654. $my_prereqs->setAttribute('type', 'aicc_script');
  8655. $my_item->appendChild($my_prereqs);
  8656. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8657. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8658. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8659. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8660. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8661. //$xmldoc->createElement('adlcp:datafromlms', '');
  8662. // Give a child element <adlcp:masteryscore> to the <item> element.
  8663. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8664. $my_item->appendChild($my_masteryscore);
  8665. // Attach this item to the organization element or its parent if there is one.
  8666. if (!empty($item->parent) && $item->parent != 0) {
  8667. $children = $organization->childNodes;
  8668. for ($i = 0; $i < $children->length; $i++) {
  8669. $item_temp = $children->item($i);
  8670. if ($item_temp -> nodeName == 'item') {
  8671. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8672. $item_temp -> appendChild($my_item);
  8673. }
  8674. }
  8675. }
  8676. } else {
  8677. $organization->appendChild($my_item);
  8678. }
  8679. $my_file_path = 'link_'.$item->get_id().'.html';
  8680. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8681. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8682. $rs = Database::query($sql);
  8683. if ($link = Database :: fetch_array($rs)) {
  8684. $url = $link['url'];
  8685. $title = stripslashes($link['title']);
  8686. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8687. $my_xml_file_path = $my_file_path;
  8688. $my_sub_dir = dirname($my_file_path);
  8689. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8690. $my_xml_sub_dir = $my_sub_dir;
  8691. // Give a <resource> child to the <resources> element.
  8692. $my_resource = $xmldoc->createElement('resource');
  8693. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8694. $my_resource->setAttribute('type', 'webcontent');
  8695. $my_resource->setAttribute('href', $my_xml_file_path);
  8696. // adlcp:scormtype can be either 'sco' or 'asset'.
  8697. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8698. // xml:base is the base directory to find the files declared in this resource.
  8699. $my_resource->setAttribute('xml:base', '');
  8700. // give a <file> child to the <resource> element.
  8701. $my_file = $xmldoc->createElement('file');
  8702. $my_file->setAttribute('href', $my_xml_file_path);
  8703. $my_resource->appendChild($my_file);
  8704. $resources->appendChild($my_resource);
  8705. }
  8706. break;
  8707. case TOOL_QUIZ:
  8708. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8709. $exe = new Exercise();
  8710. $exe->read($exe_id);
  8711. $my_item = $xmldoc->createElement('item');
  8712. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8713. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8714. $my_item->setAttribute('isvisible', 'true');
  8715. // Give a child element <title> to the <item> element.
  8716. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8717. $my_item->appendChild($my_title);
  8718. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8719. //$my_item->appendChild($my_max_score);
  8720. // Give a child element <adlcp:prerequisites> to the <item> element.
  8721. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8722. $my_prereqs->setAttribute('type','aicc_script');
  8723. $my_item->appendChild($my_prereqs);
  8724. // Give a child element <adlcp:masteryscore> to the <item> element.
  8725. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8726. $my_item->appendChild($my_masteryscore);
  8727. // Attach this item to the organization element or hits parent if there is one.
  8728. if (!empty($item->parent) && $item->parent != 0) {
  8729. $children = $organization->childNodes;
  8730. for ($i = 0; $i < $children->length; $i++) {
  8731. $item_temp = $children->item($i);
  8732. if ($item_temp -> nodeName == 'item') {
  8733. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8734. $item_temp -> appendChild($my_item);
  8735. }
  8736. }
  8737. }
  8738. } else {
  8739. $organization->appendChild($my_item);
  8740. }
  8741. // Get the path of the file(s) from the course directory root
  8742. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8743. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8744. // Write the contents of the exported exercise into a (big) html file
  8745. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8746. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8747. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8748. $res = file_put_contents($tmp_file_path, $contents);
  8749. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8750. $files_cleanup[] = $tmp_file_path;
  8751. //error_log($tmp_path); die();
  8752. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8753. $my_xml_file_path = $my_file_path;
  8754. $my_sub_dir = dirname($my_file_path);
  8755. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8756. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8757. $my_xml_sub_dir = $my_sub_dir;
  8758. // Give a <resource> child to the <resources> element.
  8759. $my_resource = $xmldoc->createElement('resource');
  8760. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8761. $my_resource->setAttribute('type', 'webcontent');
  8762. $my_resource->setAttribute('href', $my_xml_file_path);
  8763. // adlcp:scormtype can be either 'sco' or 'asset'.
  8764. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8765. // xml:base is the base directory to find the files declared in this resource.
  8766. $my_resource->setAttribute('xml:base', '');
  8767. // Give a <file> child to the <resource> element.
  8768. $my_file = $xmldoc->createElement('file');
  8769. $my_file->setAttribute('href', $my_xml_file_path);
  8770. $my_resource->appendChild($my_file);
  8771. // Get included docs.
  8772. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8773. // Dependency to other files - not yet supported.
  8774. $i = 1;
  8775. foreach ($inc_docs as $doc_info) {
  8776. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8777. $my_dep = $xmldoc->createElement('resource');
  8778. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8779. $my_dep->setAttribute('identifier', $res_id);
  8780. $my_dep->setAttribute('type', 'webcontent');
  8781. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8782. $my_dep_file = $xmldoc->createElement('file');
  8783. // Check type of URL.
  8784. if ($doc_info[1] == 'remote') {
  8785. // Remote file. Save url as is.
  8786. $my_dep_file->setAttribute('href', $doc_info[0]);
  8787. $my_dep->setAttribute('xml:base', '');
  8788. } elseif ($doc_info[1] == 'local') {
  8789. switch ($doc_info[2]) {
  8790. case 'url': // Local URL - save path as url for now, don't zip file.
  8791. // Save file but as local file (retrieve from URL).
  8792. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8793. $current_dir = dirname($abs_path);
  8794. $current_dir = str_replace('\\', '/', $current_dir);
  8795. $file_path = realpath($abs_path);
  8796. $file_path = str_replace('\\', '/', $file_path);
  8797. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8798. $my_dep->setAttribute('xml:base', '');
  8799. if (strstr($file_path, $main_path) !== false) {
  8800. // The calculated real path is really inside the chamilo root path.
  8801. // Reduce file path to what's under the DocumentRoot.
  8802. $file_path = substr($file_path, strlen($root_path));
  8803. //echo $file_path;echo '<br /><br />';
  8804. //error_log('Reduced path: '.$file_path, 0);
  8805. $zip_files_abs[] = $file_path;
  8806. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8807. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8808. $my_dep->setAttribute('xml:base', '');
  8809. } elseif (empty($file_path)) {
  8810. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8811. if (strpos($document_root,-1) == '/') {
  8812. $document_root = substr(0, -1, $document_root);
  8813. }*/
  8814. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8815. $file_path = str_replace('//', '/', $file_path);
  8816. if (file_exists($file_path)) {
  8817. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8818. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8819. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8820. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8821. $my_dep->setAttribute('xml:base', '');
  8822. }
  8823. }
  8824. break;
  8825. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8826. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8827. $current_dir = str_replace('\\', '/', $current_dir);
  8828. $file_path = realpath($doc_info[0]);
  8829. $file_path = str_replace('\\', '/', $file_path);
  8830. $my_dep_file->setAttribute('href', $file_path);
  8831. $my_dep->setAttribute('xml:base', '');
  8832. if (strstr($file_path,$main_path) !== false) {
  8833. // The calculated real path is really inside the chamilo root path.
  8834. // Reduce file path to what's under the DocumentRoot.
  8835. $file_path = substr($file_path, strlen($root_path));
  8836. //echo $file_path;echo '<br /><br />';
  8837. //error_log('Reduced path: '.$file_path, 0);
  8838. $zip_files_abs[] = $file_path;
  8839. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8840. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8841. $my_dep->setAttribute('xml:base', '');
  8842. } elseif (empty($file_path)) {
  8843. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8844. if (strpos($document_root,-1) == '/') {
  8845. $document_root = substr(0, -1, $document_root);
  8846. }*/
  8847. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8848. $file_path = str_replace('//', '/', $file_path);
  8849. if (file_exists($file_path)) {
  8850. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8851. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8852. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8853. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8854. $my_dep->setAttribute('xml:base', '');
  8855. }
  8856. }
  8857. break;
  8858. 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
  8859. if (substr($doc_info[0], 0, 2) == '..') {
  8860. // Relative path going up.
  8861. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8862. $current_dir = str_replace('\\', '/', $current_dir);
  8863. $file_path = realpath($current_dir.$doc_info[0]);
  8864. $file_path = str_replace('\\', '/', $file_path);
  8865. //error_log($file_path.' <-> '.$main_path, 0);
  8866. if (strstr($file_path, $main_path) !== false) {
  8867. // The calculated real path is really inside Chamilo's root path.
  8868. // Reduce file path to what's under the DocumentRoot.
  8869. $file_path = substr($file_path, strlen($root_path));
  8870. $file_path_dest = $file_path;
  8871. // File path is courses/CHAMILO/document/....
  8872. $info_file_path = explode('/', $file_path);
  8873. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8874. $file_path_dest = 'document/'.$file_path;
  8875. }
  8876. //error_log('Reduced path: '.$file_path, 0);
  8877. $zip_files_abs[] = $file_path;
  8878. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8879. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8880. $my_dep->setAttribute('xml:base', '');
  8881. }
  8882. } else {
  8883. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8884. $my_dep_file->setAttribute('href', $doc_info[0]);
  8885. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8886. }
  8887. break;
  8888. default:
  8889. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8890. $my_dep->setAttribute('xml:base', '');
  8891. break;
  8892. }
  8893. }
  8894. $my_dep->appendChild($my_dep_file);
  8895. $resources->appendChild($my_dep);
  8896. $dependency = $xmldoc->createElement('dependency');
  8897. $dependency->setAttribute('identifierref', $res_id);
  8898. $my_resource->appendChild($dependency);
  8899. $i++;
  8900. }
  8901. $resources->appendChild($my_resource);
  8902. $zip_files[] = $my_file_path;
  8903. break;
  8904. default:
  8905. // Get the path of the file(s) from the course directory root
  8906. $my_file_path = 'non_exportable.html';
  8907. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8908. $my_xml_file_path = $my_file_path;
  8909. $my_sub_dir = dirname($my_file_path);
  8910. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8911. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8912. $my_xml_sub_dir = $my_sub_dir;
  8913. // Give a <resource> child to the <resources> element.
  8914. $my_resource = $xmldoc->createElement('resource');
  8915. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8916. $my_resource->setAttribute('type', 'webcontent');
  8917. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8918. // adlcp:scormtype can be either 'sco' or 'asset'.
  8919. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8920. // xml:base is the base directory to find the files declared in this resource.
  8921. $my_resource->setAttribute('xml:base', '');
  8922. // Give a <file> child to the <resource> element.
  8923. $my_file = $xmldoc->createElement('file');
  8924. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8925. $my_resource->appendChild($my_file);
  8926. $resources->appendChild($my_resource);
  8927. break;
  8928. }
  8929. }
  8930. }
  8931. $organizations->appendChild($organization);
  8932. $root->appendChild($organizations);
  8933. $root->appendChild($resources);
  8934. $xmldoc->appendChild($root);
  8935. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8936. // then add the file to the zip, then destroy the file (this is done automatically).
  8937. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8938. foreach ($zip_files as $file_path) {
  8939. if (empty($file_path)) {
  8940. continue;
  8941. }
  8942. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8943. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8944. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8945. }
  8946. $this->create_path($dest_file);
  8947. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8948. // Check if the file needs a link update.
  8949. if (in_array($file_path, array_keys($link_updates))) {
  8950. $string = file_get_contents($dest_file);
  8951. unlink($dest_file);
  8952. foreach ($link_updates[$file_path] as $old_new) {
  8953. // This is an ugly hack that allows .flv files to be found by the flv player that
  8954. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8955. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8956. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8957. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8958. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8959. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8960. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8961. }
  8962. //Fix to avoid problems with default_course_document
  8963. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8964. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8965. $newDestination = $old_new['dest'];
  8966. } else {
  8967. $newDestination = str_replace('document/', '', $old_new['dest']);
  8968. }
  8969. $string = str_replace($old_new['orig'], $newDestination, $string);
  8970. // Add files inside the HTMLs
  8971. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  8972. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8973. if (file_exists($sys_course_path.$new_path)) {
  8974. copy($sys_course_path.$new_path, $destinationFile);
  8975. }
  8976. }
  8977. file_put_contents($dest_file, $string);
  8978. }
  8979. }
  8980. foreach ($zip_files_abs as $file_path) {
  8981. if (empty($file_path)) {
  8982. continue;
  8983. }
  8984. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8985. continue;
  8986. }
  8987. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8988. $this->create_path($dest_file);
  8989. copy($main_path.$file_path, $dest_file);
  8990. // Check if the file needs a link update.
  8991. if (in_array($file_path, array_keys($link_updates))) {
  8992. $string = file_get_contents($dest_file);
  8993. unlink($dest_file);
  8994. foreach ($link_updates[$file_path] as $old_new) {
  8995. // This is an ugly hack that allows .flv files to be found by the flv player that
  8996. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8997. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8998. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8999. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9000. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9001. }
  9002. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9003. }
  9004. file_put_contents($dest_file, $string);
  9005. }
  9006. }
  9007. if (is_array($links_to_create)) {
  9008. foreach ($links_to_create as $file => $link) {
  9009. $file_content = '<!DOCTYPE html><head>
  9010. <meta charset="'.api_get_language_isocode().'" />
  9011. <title>'.$link['title'].'</title>
  9012. </head>
  9013. <body dir="'.api_get_text_direction().'">
  9014. <div style="text-align:center">
  9015. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9016. </body>
  9017. </html>';
  9018. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9019. }
  9020. }
  9021. // Add non exportable message explanation.
  9022. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9023. $file_content = '<!DOCTYPE html><head>
  9024. <meta charset="'.api_get_language_isocode().'" />
  9025. <title>'.$lang_not_exportable.'</title>
  9026. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9027. </head>
  9028. <body dir="'.api_get_text_direction().'">';
  9029. $file_content .=
  9030. <<<EOD
  9031. <style>
  9032. .error-message {
  9033. font-family: arial, verdana, helvetica, sans-serif;
  9034. border-width: 1px;
  9035. border-style: solid;
  9036. left: 50%;
  9037. margin: 10px auto;
  9038. min-height: 30px;
  9039. padding: 5px;
  9040. right: 50%;
  9041. width: 500px;
  9042. background-color: #FFD1D1;
  9043. border-color: #FF0000;
  9044. color: #000;
  9045. }
  9046. </style>
  9047. <body>
  9048. <div class="error-message">
  9049. $lang_not_exportable
  9050. </div>
  9051. </body>
  9052. </html>
  9053. EOD;
  9054. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9055. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9056. }
  9057. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9058. // Add the extra files that go along with a SCORM package.
  9059. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9060. $extra_files = scandir($main_code_path);
  9061. foreach ($extra_files as $extra_file) {
  9062. if (strpos($extra_file, '.') === 0)
  9063. continue;
  9064. else {
  9065. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  9066. $this->create_path($dest_file);
  9067. copy($main_code_path.$extra_file, $dest_file);
  9068. }
  9069. }
  9070. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9071. $manifest = @$xmldoc->saveXML();
  9072. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9073. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9074. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  9075. // Clean possible temporary files.
  9076. foreach ($files_cleanup as $file) {
  9077. $res = unlink($file);
  9078. if ($res === false) {
  9079. error_log(
  9080. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9081. 0
  9082. );
  9083. }
  9084. }
  9085. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9086. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9087. }
  9088. /**
  9089. * @param int $lp_id
  9090. * @return bool
  9091. */
  9092. public function scorm_export_to_pdf($lp_id)
  9093. {
  9094. $lp_id = intval($lp_id);
  9095. $files_to_export = array();
  9096. $course_data = api_get_course_info($this->cc);
  9097. if (!empty($course_data)) {
  9098. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9099. $list = self::get_flat_ordered_items_list($lp_id);
  9100. if (!empty($list)) {
  9101. foreach ($list as $item_id) {
  9102. $item = $this->items[$item_id];
  9103. switch ($item->type) {
  9104. case 'document':
  9105. //Getting documents from a LP with chamilo documents
  9106. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9107. // Try loading document from the base course.
  9108. if (empty($file_data) && !empty($sessionId)) {
  9109. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9110. }
  9111. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9112. if (file_exists($file_path)) {
  9113. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9114. }
  9115. break;
  9116. case 'asset': //commes from a scorm package generated by chamilo
  9117. case 'sco':
  9118. $file_path = $scorm_path.'/'.$item->path;
  9119. if (file_exists($file_path)) {
  9120. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9121. }
  9122. break;
  9123. case 'dokeos_chapter':
  9124. case 'dir':
  9125. case 'chapter':
  9126. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9127. break;
  9128. }
  9129. }
  9130. }
  9131. $pdf = new PDF();
  9132. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9133. return $result;
  9134. }
  9135. return false;
  9136. }
  9137. /**
  9138. * Temp function to be moved in main_api or the best place around for this.
  9139. * Creates a file path if it doesn't exist
  9140. * @param string $path
  9141. */
  9142. public function create_path($path)
  9143. {
  9144. $path_bits = explode('/', dirname($path));
  9145. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9146. $path_built = IS_WINDOWS_OS ? '' : '/';
  9147. foreach ($path_bits as $bit) {
  9148. if (!empty ($bit)) {
  9149. $new_path = $path_built . $bit;
  9150. if (is_dir($new_path)) {
  9151. $path_built = $new_path . '/';
  9152. } else {
  9153. mkdir($new_path, api_get_permissions_for_new_directories());
  9154. $path_built = $new_path . '/';
  9155. }
  9156. }
  9157. }
  9158. }
  9159. /**
  9160. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9161. * @return boolean The results of the unlink function, or false if there was no image to start with
  9162. */
  9163. public function delete_lp_image()
  9164. {
  9165. $img = $this->get_preview_image();
  9166. if ($img != '') {
  9167. $del_file = $this->get_preview_image_path(null, 'sys');
  9168. if (isset($del_file) && file_exists($del_file)) {
  9169. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9170. if (file_exists($del_file_2)) {
  9171. unlink($del_file_2);
  9172. }
  9173. $this->set_preview_image('');
  9174. return @unlink($del_file);
  9175. }
  9176. }
  9177. return false;
  9178. }
  9179. /**
  9180. * Uploads an author image to the upload/learning_path/images directory
  9181. * @param array The image array, coming from the $_FILES superglobal
  9182. * @return boolean True on success, false on error
  9183. */
  9184. public function upload_image($image_array)
  9185. {
  9186. $image_moved = false;
  9187. if (!empty ($image_array['name'])) {
  9188. $upload_ok = process_uploaded_file($image_array);
  9189. $has_attachment = true;
  9190. } else {
  9191. $image_moved = true;
  9192. }
  9193. if ($upload_ok) {
  9194. if ($has_attachment) {
  9195. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9196. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9197. $updir = $sys_course_path . $courseDir;
  9198. // Try to add an extension to the file if it hasn't one.
  9199. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9200. if (!filter_extension($new_file_name)) {
  9201. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9202. $image_moved = false;
  9203. } else {
  9204. $file_extension = explode('.', $image_array['name']);
  9205. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9206. $filename = uniqid('');
  9207. $new_file_name = $filename.'.'.$file_extension;
  9208. $new_path = $updir.'/'.$new_file_name;
  9209. // Resize the image.
  9210. $temp = new Image($image_array['tmp_name']);
  9211. $temp->resize(104);
  9212. $result = $temp->send_image($new_path);
  9213. // Storing the image filename.
  9214. if ($result) {
  9215. $image_moved = true;
  9216. $this->set_preview_image($new_file_name);
  9217. //Resize to 64px to use on course homepage
  9218. $temp->resize(64);
  9219. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9220. return true;
  9221. }
  9222. }
  9223. }
  9224. }
  9225. return false;
  9226. }
  9227. /**
  9228. * @param int $lp_id
  9229. * @param string $status
  9230. */
  9231. public function set_autolaunch($lp_id, $status)
  9232. {
  9233. $course_id = api_get_course_int_id();
  9234. $lp_id = intval($lp_id);
  9235. $status = intval($status);
  9236. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9237. // Setting everything to autolaunch = 0
  9238. $attributes['autolaunch'] = 0;
  9239. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9240. Database::update($lp_table, $attributes, $where);
  9241. if ($status == 1) {
  9242. //Setting my lp_id to autolaunch = 1
  9243. $attributes['autolaunch'] = 1;
  9244. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9245. Database::update($lp_table, $attributes, $where );
  9246. }
  9247. }
  9248. /**
  9249. * Gets previous_item_id for the next element of the lp_item table
  9250. * @author Isaac flores paz
  9251. * @return integer Previous item ID
  9252. */
  9253. public function select_previous_item_id()
  9254. {
  9255. $course_id = api_get_course_int_id();
  9256. if ($this->debug > 0) {
  9257. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9258. }
  9259. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9260. // Get the max order of the items
  9261. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9262. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9263. $rs_max_order = Database::query($sql_max_order);
  9264. $row_max_order = Database::fetch_object($rs_max_order);
  9265. $max_order = $row_max_order->display_order;
  9266. // Get the previous item ID
  9267. $sql = "SELECT id as previous FROM $table_lp_item
  9268. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9269. $rs_max = Database::query($sql);
  9270. $row_max = Database::fetch_object($rs_max);
  9271. // Return the previous item ID
  9272. return $row_max->previous;
  9273. }
  9274. /**
  9275. * Copies an LP
  9276. */
  9277. public function copy()
  9278. {
  9279. $main_path = api_get_path(SYS_CODE_PATH);
  9280. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9281. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9282. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9283. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9284. //Course builder
  9285. $cb = new CourseBuilder();
  9286. //Setting tools that will be copied
  9287. $cb->set_tools_to_build(array('learnpaths'));
  9288. //Setting elements that will be copied
  9289. $cb->set_tools_specific_id_list(
  9290. array('learnpaths' => array($this->lp_id))
  9291. );
  9292. $course = $cb->build();
  9293. //Course restorer
  9294. $course_restorer = new CourseRestorer($course);
  9295. $course_restorer->set_add_text_in_items(true);
  9296. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9297. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9298. }
  9299. public function verify_document_size($s)
  9300. {
  9301. $post_max = ini_get('post_max_size');
  9302. if (substr($post_max, -1, 1) == 'M') {
  9303. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9304. } elseif (substr($post_max, -1, 1) == 'G') {
  9305. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9306. }
  9307. $upl_max = ini_get('upload_max_filesize');
  9308. if (substr($upl_max, -1, 1) == 'M') {
  9309. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9310. } elseif (substr($upl_max, -1, 1) == 'G') {
  9311. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9312. }
  9313. $documents_total_space = DocumentManager::documents_total_space();
  9314. $course_max_space = DocumentManager::get_course_quota();
  9315. $total_size = filesize($s) + $documents_total_space;
  9316. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9317. return true;
  9318. } else{
  9319. return false;
  9320. }
  9321. }
  9322. /**
  9323. * Clear LP prerequisites
  9324. */
  9325. public function clear_prerequisites()
  9326. {
  9327. $course_id = $this->get_course_int_id();
  9328. if ($this->debug > 0) {
  9329. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9330. }
  9331. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9332. $lp_id = $this->get_id();
  9333. //Cleaning prerequisites
  9334. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9335. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9336. Database::query($sql);
  9337. //Cleaning mastery score for exercises
  9338. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9339. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9340. Database::query($sql);
  9341. }
  9342. public function set_previous_step_as_prerequisite_for_all_items()
  9343. {
  9344. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9345. $course_id = $this->get_course_int_id();
  9346. $lp_id = $this->get_id();
  9347. if (!empty($this->items)) {
  9348. $previous_item_id = null;
  9349. $previous_item_max = 0;
  9350. $previous_item_type = null;
  9351. $last_item_not_chapter = null;
  9352. $last_item_not_chapter_type = null;
  9353. $last_item_not_chapter_max = null;
  9354. foreach ($this->items as $item) {
  9355. // if there was a previous item... (otherwise jump to set it)
  9356. if (!empty($previous_item_id)) {
  9357. $current_item_id = $item->get_id(); //save current id
  9358. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9359. // Current item is not a folder, so it qualifies to get a prerequisites
  9360. if ($last_item_not_chapter_type == 'quiz') {
  9361. // if previous is quiz, mark its max score as default score to be achieved
  9362. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9363. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9364. Database::query($sql);
  9365. }
  9366. // now simply update the prerequisite to set it to the last non-chapter item
  9367. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9368. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9369. Database::query($sql);
  9370. // record item as 'non-chapter' reference
  9371. $last_item_not_chapter = $item->get_id();
  9372. $last_item_not_chapter_type = $item->get_type();
  9373. $last_item_not_chapter_max = $item->get_max();
  9374. }
  9375. } else {
  9376. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9377. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9378. $last_item_not_chapter = $item->get_id();
  9379. $last_item_not_chapter_type = $item->get_type();
  9380. $last_item_not_chapter_max = $item->get_max();
  9381. }
  9382. }
  9383. // Saving the item as "previous item" for the next loop
  9384. $previous_item_id = $item->get_id();
  9385. $previous_item_max = $item->get_max();
  9386. $previous_item_type = $item->get_type();
  9387. }
  9388. }
  9389. }
  9390. /**
  9391. * @param array $params
  9392. */
  9393. public static function createCategory($params)
  9394. {
  9395. $em = Database::getManager();
  9396. $item = new CLpCategory();
  9397. $item->setName($params['name']);
  9398. $item->setCId($params['c_id']);
  9399. $em->persist($item);
  9400. $em->flush();
  9401. }
  9402. /**
  9403. * @param array $params
  9404. */
  9405. public static function updateCategory($params)
  9406. {
  9407. $em = Database::getManager();
  9408. /** @var CLpCategory $item */
  9409. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9410. if ($item) {
  9411. $item->setName($params['name']);
  9412. $em->merge($item);
  9413. $em->flush();
  9414. }
  9415. }
  9416. /**
  9417. * @param int $id
  9418. */
  9419. public static function moveUpCategory($id)
  9420. {
  9421. $em = Database::getManager();
  9422. /** @var CLpCategory $item */
  9423. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9424. if ($item) {
  9425. $position = $item->getPosition() - 1;
  9426. $item->setPosition($position);
  9427. $em->persist($item);
  9428. $em->flush();
  9429. }
  9430. }
  9431. /**
  9432. * @param int $id
  9433. */
  9434. public static function moveDownCategory($id)
  9435. {
  9436. $em = Database::getManager();
  9437. /** @var CLpCategory $item */
  9438. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9439. if ($item) {
  9440. $position = $item->getPosition() + 1;
  9441. $item->setPosition($position);
  9442. $em->persist($item);
  9443. $em->flush();
  9444. }
  9445. }
  9446. /**
  9447. * @param int $courseId
  9448. * @return int|mixed
  9449. */
  9450. public static function getCountCategories($courseId)
  9451. {
  9452. if (empty($course_id)) {
  9453. return 0;
  9454. }
  9455. $em = Database::getManager();
  9456. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9457. $query->setParameter('id', $courseId);
  9458. return $query->getSingleScalarResult();
  9459. }
  9460. /**
  9461. * @param int $courseId
  9462. *
  9463. * @return mixed
  9464. */
  9465. public static function getCategories($courseId)
  9466. {
  9467. $em = Database::getManager();
  9468. //Default behaviour
  9469. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9470. array('cId' => $course_id),
  9471. array('name' => 'ASC')
  9472. );*/
  9473. // Using doctrine extensions
  9474. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9475. array('cId' => $courseId)
  9476. )->getResult();
  9477. return $items;
  9478. }
  9479. /**
  9480. * @param int $id
  9481. *
  9482. * @return mixed
  9483. */
  9484. public static function getCategory($id)
  9485. {
  9486. $em = Database::getManager();
  9487. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9488. return $item;
  9489. }
  9490. /**
  9491. * @param int $courseId
  9492. * @return array
  9493. */
  9494. public static function getCategoryByCourse($courseId)
  9495. {
  9496. $em = Database::getManager();
  9497. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9498. return $items;
  9499. }
  9500. /**
  9501. * @param int $id
  9502. *
  9503. * @return mixed
  9504. */
  9505. public static function deleteCategory($id)
  9506. {
  9507. $em = Database::getManager();
  9508. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9509. if ($item) {
  9510. $courseId = $item->getCId();
  9511. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9512. $query->setParameter('id', $courseId);
  9513. $query->setParameter('catId', $item->getId());
  9514. $lps = $query->getResult();
  9515. // Setting category = 0.
  9516. if ($lps) {
  9517. foreach ($lps as $lpItem) {
  9518. $lpItem->setCategoryId(0);
  9519. }
  9520. }
  9521. // Removing category.
  9522. $em->remove($item);
  9523. $em->flush();
  9524. }
  9525. }
  9526. /**
  9527. * @param int $courseId
  9528. * @param bool $addSelectOption
  9529. *
  9530. * @return mixed
  9531. */
  9532. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9533. {
  9534. $items = self::getCategoryByCourse($courseId);
  9535. $cats = array();
  9536. if ($addSelectOption) {
  9537. $cats = array(get_lang('SelectACategory'));
  9538. }
  9539. if (!empty($items)) {
  9540. foreach ($items as $cat) {
  9541. $cats[$cat->getId()] = $cat->getName();
  9542. }
  9543. }
  9544. return $cats;
  9545. }
  9546. /**
  9547. * Return the scorm item type object with spaces replaced with _
  9548. * The return result is use to build a css classname like scorm_type_$return
  9549. * @param $in_type
  9550. * @return mixed
  9551. */
  9552. private static function format_scorm_type_item($in_type)
  9553. {
  9554. return str_replace(' ', '_', $in_type);
  9555. }
  9556. /**
  9557. * @return \learnpath
  9558. */
  9559. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9560. {
  9561. $lpObject = Session::read('lpobject');
  9562. $learnPath = null;
  9563. if (isset($lpObject)) {
  9564. $learnPath = unserialize($lpObject);
  9565. }
  9566. if (!is_object($learnPath)) {
  9567. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9568. }
  9569. return $learnPath;
  9570. }
  9571. /**
  9572. * @param int $itemId
  9573. * @return learnpathItem|false
  9574. */
  9575. public function getItem($itemId)
  9576. {
  9577. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9578. return $this->items[$itemId];
  9579. }
  9580. return false;
  9581. }
  9582. /**
  9583. * @return int
  9584. */
  9585. public function getCategoryId()
  9586. {
  9587. return $this->categoryId;
  9588. }
  9589. /**
  9590. * @param int $categoryId
  9591. * @return bool
  9592. */
  9593. public function setCategoryId($categoryId)
  9594. {
  9595. $this->categoryId = intval($categoryId);
  9596. $courseId = api_get_course_int_id();
  9597. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9598. $lp_id = $this->get_id();
  9599. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9600. WHERE c_id = $courseId AND id = $lp_id";
  9601. Database::query($sql);
  9602. return true;
  9603. }
  9604. /**
  9605. * Get whether this is a learning path with the possibility to subscribe
  9606. * users or not
  9607. * @return int
  9608. */
  9609. public function getSubscribeUsers()
  9610. {
  9611. return $this->subscribeUsers;
  9612. }
  9613. /**
  9614. * Set whether this is a learning path with the possibility to subscribe
  9615. * users or not
  9616. * @param int $subscribeUsers (0 = false, 1 = true)
  9617. */
  9618. public function setSubscribeUsers($value)
  9619. {
  9620. if ($this->debug > 0) {
  9621. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9622. }
  9623. $this->subscribeUsers = intval($value);;
  9624. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9625. $lp_id = $this->get_id();
  9626. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9627. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9628. Database::query($sql);
  9629. return true;
  9630. }
  9631. /**
  9632. * Calculate the count of stars for a user in this LP
  9633. * This calculation is based on the following rules:
  9634. * - the student gets one star when he gets to 50% of the learning path
  9635. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9636. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9637. * - the student gets the final star when the score for the *last* test is >= 80%
  9638. * @param int $sessionId Optional. The session ID
  9639. * @return int The count of stars
  9640. */
  9641. public function getCalculateStars($sessionId = 0)
  9642. {
  9643. $stars = 0;
  9644. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9645. if ($progress >= 50) {
  9646. $stars++;
  9647. }
  9648. // Calculate stars chapters evaluation
  9649. $exercisesItems = $this->getExercisesItems();
  9650. if (!empty($exercisesItems)) {
  9651. $totalResult = 0;
  9652. foreach ($exercisesItems as $exerciseItem) {
  9653. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9654. $this->user_id,
  9655. $exerciseItem->path,
  9656. $this->course_int_id,
  9657. $sessionId,
  9658. $this->lp_id,
  9659. $exerciseItem->db_id
  9660. );
  9661. $exerciseResultInfo = end($exerciseResultInfo);
  9662. if (!$exerciseResultInfo) {
  9663. continue;
  9664. }
  9665. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9666. $totalResult += $exerciseResult;
  9667. }
  9668. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9669. if ($totalExerciseAverage >= 50) {
  9670. $stars++;
  9671. }
  9672. if ($totalExerciseAverage >= 80) {
  9673. $stars++;
  9674. }
  9675. }
  9676. // Calculate star for final evaluation
  9677. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9678. if (!empty($finalEvaluationItem)) {
  9679. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9680. $this->user_id,
  9681. $finalEvaluationItem->path,
  9682. $this->course_int_id,
  9683. $sessionId,
  9684. $this->lp_id,
  9685. $finalEvaluationItem->db_id
  9686. );
  9687. $evaluationResultInfo = end($evaluationResultInfo);
  9688. if ($evaluationResultInfo) {
  9689. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9690. if ($evaluationResult >= 80) {
  9691. $stars++;
  9692. }
  9693. }
  9694. }
  9695. return $stars;
  9696. }
  9697. /**
  9698. * Get the items of exercise type
  9699. * @return array The items. Otherwise return false
  9700. */
  9701. public function getExercisesItems()
  9702. {
  9703. $exercises = [];
  9704. foreach ($this->items as $item) {
  9705. if ($item->type != 'quiz') {
  9706. continue;
  9707. }
  9708. $exercises[] = $item;
  9709. }
  9710. array_pop($exercises);
  9711. return $exercises;
  9712. }
  9713. /**
  9714. * Get the item of exercise type (evaluation type)
  9715. * @return array The final evaluation. Otherwise return false
  9716. */
  9717. public function getFinalEvaluationItem()
  9718. {
  9719. $exercises = [];
  9720. foreach ($this->items as $item) {
  9721. if ($item->type != 'quiz') {
  9722. continue;
  9723. }
  9724. $exercises[] = $item;
  9725. }
  9726. return array_pop($exercises);
  9727. }
  9728. /**
  9729. * Calculate the total points achieved for the current user in this learning path
  9730. * @param int $sessionId Optional. The session Id
  9731. * @return int
  9732. */
  9733. public function getCalculateScore($sessionId = 0)
  9734. {
  9735. // Calculate stars chapters evaluation
  9736. $exercisesItems = $this->getExercisesItems();
  9737. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9738. $totalExercisesResult = 0;
  9739. $totalEvaluationResult = 0;
  9740. if ($exercisesItems !== false) {
  9741. foreach ($exercisesItems as $exerciseItem) {
  9742. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9743. $this->user_id,
  9744. $exerciseItem->path,
  9745. $this->course_int_id,
  9746. $sessionId,
  9747. $this->lp_id,
  9748. $exerciseItem->db_id
  9749. );
  9750. $exerciseResultInfo = end($exerciseResultInfo);
  9751. if (!$exerciseResultInfo) {
  9752. continue;
  9753. }
  9754. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  9755. }
  9756. }
  9757. if (!empty($finalEvaluationItem)) {
  9758. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9759. $this->user_id,
  9760. $finalEvaluationItem->path,
  9761. $this->course_int_id,
  9762. $sessionId,
  9763. $this->lp_id,
  9764. $finalEvaluationItem->db_id
  9765. );
  9766. $evaluationResultInfo = end($evaluationResultInfo);
  9767. if ($evaluationResultInfo) {
  9768. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  9769. }
  9770. }
  9771. return $totalExercisesResult + $totalEvaluationResult;
  9772. }
  9773. /**
  9774. * Check if URL is not allowed to be show in a iframe
  9775. * @param string $src
  9776. *
  9777. * @return string
  9778. */
  9779. public function fixBlockedLinks($src)
  9780. {
  9781. $urlInfo = parse_url($src);
  9782. //$platformProtocol = api_get_protocol();
  9783. $platformProtocol = 'https';
  9784. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9785. $platformProtocol = 'http';
  9786. }
  9787. $protocolFixApplied = false;
  9788. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9789. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9790. if ($platformProtocol != $scheme) {
  9791. $_SESSION['x_frame_source'] = $src;
  9792. $src = 'blank.php?error=x_frames_options';
  9793. $protocolFixApplied = true;
  9794. }
  9795. if ($protocolFixApplied == false) {
  9796. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9797. // Check X-Frame-Options
  9798. $ch = curl_init();
  9799. $options = array(
  9800. CURLOPT_URL => $src,
  9801. CURLOPT_RETURNTRANSFER => true,
  9802. CURLOPT_HEADER => true,
  9803. CURLOPT_FOLLOWLOCATION => true,
  9804. CURLOPT_ENCODING => "",
  9805. CURLOPT_AUTOREFERER => true,
  9806. CURLOPT_CONNECTTIMEOUT => 120,
  9807. CURLOPT_TIMEOUT => 120,
  9808. CURLOPT_MAXREDIRS => 10,
  9809. );
  9810. curl_setopt_array($ch, $options);
  9811. $response = curl_exec($ch);
  9812. $httpCode = curl_getinfo($ch);
  9813. $headers = substr($response, 0, $httpCode['header_size']);
  9814. $error = false;
  9815. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9816. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9817. ) {
  9818. $error = true;
  9819. }
  9820. if ($error) {
  9821. $_SESSION['x_frame_source'] = $src;
  9822. $src = 'blank.php?error=x_frames_options';
  9823. }
  9824. }
  9825. }
  9826. return $src;
  9827. }
  9828. /**
  9829. * Check if this LP has a created forum in the basis course
  9830. * @return boolean
  9831. */
  9832. public function lpHasForum()
  9833. {
  9834. $forumTable = Database::get_course_table(TABLE_FORUM);
  9835. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9836. $fakeFrom = "
  9837. $forumTable f
  9838. INNER JOIN $itemProperty ip
  9839. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9840. ";
  9841. $resultData = Database::select(
  9842. 'COUNT(f.iid) AS qty',
  9843. $fakeFrom,
  9844. [
  9845. 'where' => [
  9846. 'ip.visibility != ? AND ' => 2,
  9847. 'ip.tool = ? AND ' => TOOL_FORUM,
  9848. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9849. 'f.lp_id = ?' => intval($this->lp_id)
  9850. ]
  9851. ],
  9852. 'first'
  9853. );
  9854. if ($resultData['qty'] > 0) {
  9855. return true;
  9856. }
  9857. return false;
  9858. }
  9859. /**
  9860. * Get the forum for this learning path
  9861. * @return boolean
  9862. */
  9863. public function getForum($sessionId = 0)
  9864. {
  9865. $forumTable = Database::get_course_table(TABLE_FORUM);
  9866. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9867. $fakeFrom = "$forumTable f
  9868. INNER JOIN $itemProperty ip ";
  9869. if ($this->lp_session_id == 0) {
  9870. $fakeFrom .= "
  9871. ON (
  9872. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9873. f.session_id = ip.session_id OR ip.session_id IS NULL
  9874. )
  9875. )
  9876. ";
  9877. } else {
  9878. $fakeFrom .= "
  9879. ON (
  9880. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9881. )
  9882. ";
  9883. }
  9884. $resultData = Database::select(
  9885. 'f.*',
  9886. $fakeFrom,
  9887. [
  9888. 'where' => [
  9889. 'ip.visibility != ? AND ' => 2,
  9890. 'ip.tool = ? AND ' => TOOL_FORUM,
  9891. 'f.session_id = ? AND ' => $sessionId,
  9892. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9893. 'f.lp_id = ?' => intval($this->lp_id)
  9894. ]
  9895. ],
  9896. 'first'
  9897. );
  9898. if (empty($resultData)) {
  9899. return false;
  9900. }
  9901. return $resultData;
  9902. }
  9903. /**
  9904. * Create a forum for this learning path
  9905. * @param type $forumCategoryId
  9906. * @return int The forum ID if was created. Otherwise return false
  9907. */
  9908. public function createForum($forumCategoryId)
  9909. {
  9910. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9911. $forumId = store_forum(
  9912. [
  9913. 'lp_id' => $this->lp_id,
  9914. 'forum_title' => $this->name,
  9915. 'forum_comment' => null,
  9916. 'forum_category' => intval($forumCategoryId),
  9917. 'students_can_edit_group' => ['students_can_edit' => 0],
  9918. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9919. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9920. 'group_forum' => 0,
  9921. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9922. ],
  9923. [],
  9924. true
  9925. );
  9926. return $forumId;
  9927. }
  9928. /**
  9929. * Check and obtain the lp final item if exist
  9930. *
  9931. * @return array lp items
  9932. */
  9933. private function getFinalItem()
  9934. {
  9935. if (empty($this->items)) {
  9936. return null;
  9937. }
  9938. foreach ($this->items as $item) {
  9939. if ($item->type !== 'final_item') {
  9940. continue;
  9941. }
  9942. return $item;
  9943. }
  9944. }
  9945. /**
  9946. * Get the LP Final Item Template
  9947. *
  9948. * @return html
  9949. */
  9950. private function getFinalItemTemplate()
  9951. {
  9952. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'newscorm/final_item_template/template.html');
  9953. }
  9954. /**
  9955. * Get the LP Final Item Url
  9956. *
  9957. * @return String
  9958. */
  9959. private function getSavedFinalItem()
  9960. {
  9961. $finalItem = $this->getFinalItem();
  9962. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  9963. return file_get_contents($doc['absolute_path']);
  9964. }
  9965. /**
  9966. * Get the LP Final Item form
  9967. *
  9968. * @return html
  9969. */
  9970. public function getFinalItemForm()
  9971. {
  9972. $finalItem = $this->getFinalItem();
  9973. $title = '';
  9974. $content = '';
  9975. if ($finalItem) {
  9976. $title = $finalItem->title;
  9977. $buttonText = get_lang('Save');
  9978. $content = $this->getSavedFinalItem();
  9979. } else {
  9980. $buttonText = get_lang('LPCreateDocument');
  9981. $content = $this->getFinalItemTemplate();
  9982. }
  9983. $courseInfo = api_get_course_info();
  9984. $result = $this->generate_lp_folder($courseInfo);
  9985. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  9986. $relative_prefix = '../../';
  9987. $editorConfig = [
  9988. 'ToolbarSet' => 'LearningPathDocuments',
  9989. 'Width' => '100%',
  9990. 'Height' => '500',
  9991. 'FullPage' => true,
  9992. 'CreateDocumentDir' => $relative_prefix,
  9993. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  9994. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  9995. ];
  9996. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  9997. 'type' => 'document',
  9998. 'lp_id' => $this->lp_id
  9999. ]);
  10000. $form = new FormValidator('final_item', 'POST', $url);
  10001. $form->addText('title', get_lang('Title'));
  10002. $form->addButtonSave($buttonText);
  10003. $form->addHtml('<div class="alert alert-info">Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b></div>');
  10004. $renderer = $form->defaultRenderer();
  10005. $renderer->setElementTemplate('<div class="editor-lp">&nbsp;{label}{element}</div>', 'content_lp');
  10006. $form->addHtmlEditor('content_lp', null, null, true, $editorConfig, true);
  10007. $form->addHidden('action', 'add_final_item');
  10008. $form->addHidden('path', isset($_SESSION['pathItem']) ? $_SESSION['pathItem'] : '');
  10009. $form->addHidden('previous', $this->get_last());
  10010. $form->setDefaults(['title' => $title, 'content_lp' => $content]);
  10011. if ($form->validate()) {
  10012. $values = $form->exportValues();
  10013. $lastItemId = $this->get_last();
  10014. if (!$finalItem) {
  10015. $documentId = $this->create_document($this->course_info, $values['content_lp'], $values['title']);
  10016. $this->add_item(
  10017. 0,
  10018. $lastItemId,
  10019. 'final_item',
  10020. $documentId,
  10021. $values['title'],
  10022. ''
  10023. );
  10024. } else {
  10025. $this->edit_document($this->course_info);
  10026. }
  10027. }
  10028. return $form->returnForm();
  10029. }
  10030. }
  10031. if (!function_exists('trim_value')) {
  10032. function trim_value(& $value) {
  10033. $value = trim($value);
  10034. }
  10035. }